From e14931f389cbb91ef306ed5faf69087dd9928a23 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 10 Nov 2009 14:07:51 +0000 Subject: [PATCH] Move completely over to new operators --- .../chaiscript/language/chaiscript_common.hpp | 8 +- .../chaiscript/language/chaiscript_eval.hpp | 90 +------------------ .../chaiscript/language/chaiscript_parser.hpp | 24 ++--- 3 files changed, 17 insertions(+), 105 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 4543b40..b538e6d 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -22,9 +22,9 @@ namespace chaiscript * 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, - Comparison, Additive, Multiplicative, Unary_Minus, Unary_Plus, Not, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String, + Comparison, Additive, Multiplicative, 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, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or, Bitwise_Not, + Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or, Logical_And, Logical_Or}; }; namespace @@ -34,9 +34,9 @@ namespace chaiscript */ 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", - "Comparison", "Additive", "Multiplicative", "Unary_Minus", "Unary_Plus", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String", + "Comparison", "Additive", "Multiplicative", "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", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or", "Bitwise_Not", + "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or", "Logical_And", "Logical_Or"}; return token_types[tokentype]; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 1061978..a2f2077 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -334,78 +334,6 @@ namespace chaiscript return retval; } - /** - * Evaluates a unary negation - */ - template - Boxed_Value eval_unary_minus(Eval_System &ss, const TokenPtr &node) { - Boxed_Value retval = eval_token(ss, node->children[0]); - Param_List_Builder plb; - plb << retval; - plb << Boxed_Value(-1.0); - - try { - return ss.call_function("*", plb); - } - catch(std::exception &){ - throw Eval_Error("Can not find appropriate unary minus", node->children[0]); - } - - /* - * Should be unary minus - */ - } - - /** - * Evaluates a unary negation - */ - template - Boxed_Value eval_unary_plus(Eval_System &ss, const TokenPtr &node) { - Boxed_Value retval = eval_token(ss, node->children[0]); - Param_List_Builder plb; - plb << retval; - - try { - return ss.call_function("+", plb); - } - catch(std::exception &){ - throw Eval_Error("Can not find appropriate unary plus", node->children[0]); - } - - } - - /** - * Evaluates a unary boolean not - */ - template - Boxed_Value eval_not(Eval_System &ss, const TokenPtr &node) { - try { - return Boxed_Value(!boxed_cast(eval_token(ss, node->children[0]))); - } - catch (const bad_boxed_cast &) { - throw Eval_Error("Boolean not('!') condition not boolean", node->children[0]); - } - /* - * Should be like above, but use ! - */ - } - - /** - * Evaluates a unary boolean not - */ - template - Boxed_Value eval_bitwise_not(Eval_System &ss, const TokenPtr &node) { - try { - return Boxed_Value(~boxed_cast(eval_token(ss, node->children[0]))); - } - catch (const bad_boxed_cast &) { - throw Eval_Error("Bitwise not condition not integer", node->children[0]); - } - /* - * Should be like above, but use ~ - */ - } - /** * Evaluates any unary prefix */ @@ -418,7 +346,7 @@ namespace chaiscript return ss.call_function(node->children[0]->text, plb); } catch(std::exception &){ - throw Eval_Error("Can not find appropriate prefix", node->children[0]); + throw Eval_Error("Can not find appropriate unary '" + node->children[0]->text + "'", node->children[0]); } } @@ -1154,22 +1082,6 @@ namespace chaiscript return eval_array_call(ss, node); break; - case (Token_Type::Unary_Minus) : - return eval_unary_minus(ss, node); - break; - - case (Token_Type::Unary_Plus) : - return eval_unary_plus(ss, node); - break; - - case (Token_Type::Not) : - return eval_not(ss, node); - break; - - case (Token_Type::Bitwise_Not) : - return eval_bitwise_not(ss, node); - break; - case (Token_Type::Prefix) : return eval_prefix(ss, node); break; diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 7a45371..e0f297c 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -864,7 +864,7 @@ namespace chaiscript if (retval) { //todo: fix this. Hacky workaround for preventing substring matches if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') - || (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.'))) { + || (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) { input_pos = start; col = prev_col; line = prev_line; @@ -883,7 +883,7 @@ namespace chaiscript if (Symbol_(s)) { //todo: fix this. Hacky workaround for preventing substring matches if ((input_pos != input_end) && (disallow_prevention == false) && ((*input_pos == '+') || (*input_pos == '-') || (*input_pos == '*') || (*input_pos == '/') - || (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.'))) { + || (*input_pos == '|') || (*input_pos == '&') || (*input_pos == '^') || (*input_pos == '=') || (*input_pos == '.') || (*input_pos == '<') || (*input_pos == '>'))) { input_pos = start; col = prev_col; line = prev_line; @@ -1511,41 +1511,41 @@ namespace chaiscript build_match(Token_Type::Prefix, prev_stack_top); } - else if (Char('-')) { + else if (Char('-', true)) { retval = true; if (!Operator(operators.size()-1)) { - throw Eval_Error("Incomplete unary - expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete unary '-' expression", File_Position(line, col), filename); } - build_match(Token_Type::Unary_Minus, prev_stack_top); + build_match(Token_Type::Prefix, prev_stack_top); } - else if (Char('+')) { + else if (Char('+', true)) { retval = true; if (!Operator(operators.size()-1)) { - throw Eval_Error("Incomplete unary + expression", File_Position(line, col), filename); + throw Eval_Error("Incomplete unary '+' expression", File_Position(line, col), filename); } - build_match(Token_Type::Unary_Plus, prev_stack_top); + build_match(Token_Type::Prefix, prev_stack_top); } - else if (Char('!')) { + else if (Char('!', true)) { retval = true; if (!Operator(operators.size()-1)) { throw Eval_Error("Incomplete '!' expression", File_Position(line, col), filename); } - build_match(Token_Type::Not, prev_stack_top); + build_match(Token_Type::Prefix, prev_stack_top); } - else if (Char('~')) { + else if (Char('~', true)) { retval = true; if (!Operator(operators.size()-1)) { throw Eval_Error("Incomplete '~' expression", File_Position(line, col), filename); } - build_match(Token_Type::Bitwise_Not, prev_stack_top); + build_match(Token_Type::Prefix, prev_stack_top); } return retval;