From a5b2ec300662c36e236b52499e8d52573879f6b5 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 23 Sep 2009 05:00:14 +0000 Subject: [PATCH] Start code cleanups by moving chaiscript common structures into their own file. --- include/chaiscript/chaiscript.hpp | 125 ---------------- .../chaiscript/language/chaiscript_common.hpp | 135 ++++++++++++++++++ .../chaiscript/language/chaiscript_engine.hpp | 6 +- .../chaiscript/language/chaiscript_eval.hpp | 2 + 4 files changed, 141 insertions(+), 127 deletions(-) create mode 100644 include/chaiscript/language/chaiscript_common.hpp diff --git a/include/chaiscript/chaiscript.hpp b/include/chaiscript/chaiscript.hpp index 767218e..de53280 100644 --- a/include/chaiscript/chaiscript.hpp +++ b/include/chaiscript/chaiscript.hpp @@ -23,131 +23,6 @@ #include "dispatchkit/function_call.hpp" #include "dispatchkit/dynamic_object.hpp" -#ifdef BOOST_HAS_DECLSPEC -#define CHAISCRIPT_MODULE_EXPORT extern "C" __declspec(dllexport) -#else -#define CHAISCRIPT_MODULE_EXPORT extern "C" -#endif - - - -namespace chaiscript -{ - typedef ModulePtr (*Create_Module_Func)(); - - /** - * Types of AST nodes available to the parser and eval - */ - class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl, - Expression, Comparison, Additive, Multiplicative, Negate, Not, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String, - Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range, - Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl }; }; - - namespace - { - /** - * Helper lookup to get the name of each node type - */ - const char *token_type_to_string(int tokentype) { - const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", - "Expression", "Comparison", "Additive", "Multiplicative", "Negate", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", - "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range", - "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl"}; - - return token_types[tokentype]; - } - } - - /** - * Convenience type for file positions - */ - struct File_Position { - int line; - int column; - - File_Position(int file_line, int file_column) - : line(file_line), column(file_column) { } - - File_Position() : line(0), column(0) { } - }; - - typedef boost::shared_ptr TokenPtr; - - /** - * The struct that doubles as both a parser token and an AST node - */ - struct Token { - std::string text; - int identifier; - const char *filename; - File_Position start, end; - bool is_cached; - Boxed_Value cached_value; - - std::vector children; - TokenPtr annotation; - - Token(const std::string &token_text, int id, const char *fname) : - text(token_text), identifier(id), filename(fname), is_cached(false) { } - - Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) : - text(token_text), identifier(id), filename(fname), is_cached(false) { - - start.line = start_line; - start.column = start_col; - end.line = end_line; - end.column = end_col; - } - }; - - /** - * Errors generated during parsing or evaluation - */ - struct Eval_Error : public std::runtime_error { - std::string reason; - File_Position start_position; - File_Position end_position; - const char *filename; - - Eval_Error(const std::string &why, const File_Position &where, const char *fname) : - std::runtime_error("Error: \"" + why + "\" " + - (std::string(fname) != "__EVAL__" ? ("in '" + std::string(fname) + "' ") : "during evaluation ") + - + "at (" + boost::lexical_cast(where.line) + ", " + - boost::lexical_cast(where.column) + ")"), - reason(why), start_position(where), end_position(where), filename(fname) - { } - - Eval_Error(const std::string &why, const TokenPtr &where) - : std::runtime_error("Error: \"" + why + "\" " + - (std::string(where->filename) != "__EVAL__" ? ("in '" + std::string(where->filename) + "' ") : "during evaluation ") + - "at (" + boost::lexical_cast(where->start.line) + ", " + - boost::lexical_cast(where->start.column) + ")"), - reason(why), start_position(where->start), end_position(where->end), filename(where->filename) { - } - - virtual ~Eval_Error() throw() {} - }; - - /** - * Special type for returned values - */ - struct Return_Value { - Boxed_Value retval; - TokenPtr location; - - Return_Value(const Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { } - }; - - /** - * Special type indicating a call to 'break' - */ - struct Break_Loop { - TokenPtr location; - - Break_Loop(const TokenPtr where) : location(where) { } - }; -} - #include "language/chaiscript_eval.hpp" #include "language/chaiscript_engine.hpp" diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp new file mode 100644 index 0000000..d748e0c --- /dev/null +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -0,0 +1,135 @@ +// This file is distributed under the BSD License. +// See "license.txt" for details. +// Copyright 2009, Jonathan Turner (jturner@minnow-lang.org) +// and Jason Turner (lefticus@gmail.com) +// http://www.chaiscript.com + +#ifndef _CHAISCRIPT_COMMON_HPP +#define _CHAISCRIPT_COMMON_HPP + + +#ifdef BOOST_HAS_DECLSPEC +#define CHAISCRIPT_MODULE_EXPORT extern "C" __declspec(dllexport) +#else +#define CHAISCRIPT_MODULE_EXPORT extern "C" +#endif + +namespace chaiscript +{ + typedef ModulePtr (*Create_Module_Func)(); + + /** + * Types of AST nodes available to the parser and eval + */ + class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl, + Expression, Comparison, Additive, Multiplicative, Negate, Not, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String, + Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range, + Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl }; }; + + namespace + { + /** + * Helper lookup to get the name of each node type + */ + const char *token_type_to_string(int tokentype) { + const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", + "Expression", "Comparison", "Additive", "Multiplicative", "Negate", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", + "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range", + "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl"}; + + return token_types[tokentype]; + } + } + + /** + * Convenience type for file positions + */ + struct File_Position { + int line; + int column; + + File_Position(int file_line, int file_column) + : line(file_line), column(file_column) { } + + File_Position() : line(0), column(0) { } + }; + + typedef boost::shared_ptr TokenPtr; + + /** + * The struct that doubles as both a parser token and an AST node + */ + struct Token { + std::string text; + int identifier; + const char *filename; + File_Position start, end; + bool is_cached; + Boxed_Value cached_value; + + std::vector children; + TokenPtr annotation; + + Token(const std::string &token_text, int id, const char *fname) : + text(token_text), identifier(id), filename(fname), is_cached(false) { } + + Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) : + text(token_text), identifier(id), filename(fname), is_cached(false) { + + start.line = start_line; + start.column = start_col; + end.line = end_line; + end.column = end_col; + } + }; + + /** + * Errors generated during parsing or evaluation + */ + struct Eval_Error : public std::runtime_error { + std::string reason; + File_Position start_position; + File_Position end_position; + const char *filename; + + Eval_Error(const std::string &why, const File_Position &where, const char *fname) : + std::runtime_error("Error: \"" + why + "\" " + + (std::string(fname) != "__EVAL__" ? ("in '" + std::string(fname) + "' ") : "during evaluation ") + + + "at (" + boost::lexical_cast(where.line) + ", " + + boost::lexical_cast(where.column) + ")"), + reason(why), start_position(where), end_position(where), filename(fname) + { } + + Eval_Error(const std::string &why, const TokenPtr &where) + : std::runtime_error("Error: \"" + why + "\" " + + (std::string(where->filename) != "__EVAL__" ? ("in '" + std::string(where->filename) + "' ") : "during evaluation ") + + "at (" + boost::lexical_cast(where->start.line) + ", " + + boost::lexical_cast(where->start.column) + ")"), + reason(why), start_position(where->start), end_position(where->end), filename(where->filename) { + } + + virtual ~Eval_Error() throw() {} + }; + + /** + * Special type for returned values + */ + struct Return_Value { + Boxed_Value retval; + TokenPtr location; + + Return_Value(const Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { } + }; + + /** + * Special type indicating a call to 'break' + */ + struct Break_Loop { + TokenPtr location; + + Break_Loop(const TokenPtr where) : location(where) { } + }; +} + +#endif /* _CHAISCRIPT_COMMON_HPP */ + diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index f4ae4c7..9cfb6a2 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -10,6 +10,8 @@ #include #include +#include + #ifdef _POSIX_VERSION #include #else @@ -18,8 +20,8 @@ #endif #endif -#include "chaiscript_prelude.hpp" -#include "chaiscript_parser.hpp" +#include +#include namespace chaiscript { diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index d845a3a..b7c508d 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -9,6 +9,8 @@ #include +#include + namespace chaiscript { /**