From 48f538438d7f45f106cf819b1bcc3cf85e241ae3 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 23 Feb 2013 21:14:37 -0700 Subject: [PATCH] Get all for loop related unit tests passing and expand the types of expressions that can exist in a for loop --- .../chaiscript/language/chaiscript_common.hpp | 8 ++--- .../chaiscript/language/chaiscript_eval.hpp | 19 ++++++++++- .../chaiscript/language/chaiscript_parser.hpp | 32 ++++++++++++++++--- unittests/for.chai | 8 +++-- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index a694cfd..ffed54e 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -21,9 +21,9 @@ namespace chaiscript public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl, Comparison, Addition, Subtraction, Multiplication, Division, Modulus, 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, + Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Continue, Map_Pair, Value_Range, Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift, Equality, Bitwise_And, Bitwise_Xor, Bitwise_Or, - Logical_And, Logical_Or, Switch, Case, Default, Ternary_Cond + Logical_And, Logical_Or, Switch, Case, Default, Ternary_Cond, Noop }; }; @@ -35,9 +35,9 @@ namespace chaiscript const char *ast_node_type_to_string(int ast_node_type) { const char *ast_node_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", "Comparison", "Addition", "Subtraction", "Multiplication", "Division", "Modulus", "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", + "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Continue", "Map_Pair", "Value_Range", "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift", "Equality", "Bitwise_And", "Bitwise_Xor", "Bitwise_Or", - "Logical_And", "Logical_Or", "Switch", "Case", "Default", "Ternary Condition"}; + "Logical_And", "Logical_Or", "Switch", "Case", "Default", "Ternary Condition", "Noop"}; return ast_node_types[ast_node_type]; } diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 3657cb3..6a536a9 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -1151,7 +1151,7 @@ namespace chaiscript struct Continue_AST_Node : public AST_Node { public: - Continue_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Break, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + Continue_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Continue, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col) { } virtual ~Continue_AST_Node() {} virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ @@ -1159,6 +1159,23 @@ namespace chaiscript } }; + struct Noop_AST_Node : public AST_Node { + public: + Noop_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Noop, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : + AST_Node(t_ast_node_text, t_id, t_fname, t_start_line, t_start_col, t_end_line, t_end_col), + m_value(const_var(true)) + { } + + virtual ~Noop_AST_Node() {} + virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &){ + // It's a no-op, that evaluates to "true" + return m_value; + } + + private: + Boxed_Value m_value; + }; + struct Map_Pair_AST_Node : public AST_Node { public: Map_Pair_AST_Node(const std::string &t_ast_node_text = "", int t_id = AST_Node_Type::Map_Pair, const boost::shared_ptr &t_fname=boost::shared_ptr(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) : diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 0deca71..a537431 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1574,18 +1574,40 @@ namespace chaiscript return retval; } + /** * Reads the C-style for conditions from input */ bool For_Guards() { - Equation(); + if (!(Equation() && Eol())) + { + if (!Eol()) + { + throw exception::eval_error("'for' loop initial statment missing", File_Position(m_line, m_col), *m_filename); + } else { + AST_NodePtr t(new eval::Noop_AST_Node()); + m_match_stack.push_back(t); + } + } - if (Char(';') && Operator() && Char(';') && Equation()) { - return true; + if (!(Equation() && Eol())) + { + if (!Eol()) + { + throw exception::eval_error("'for' loop condition missing", File_Position(m_line, m_col), *m_filename); + } else { + AST_NodePtr t(new eval::Noop_AST_Node()); + m_match_stack.push_back(t); + } } - else { - throw exception::eval_error("Incomplete conditions in 'for' loop", File_Position(m_line, m_col), *m_filename); + + if (!Equation()) + { + AST_NodePtr t(new eval::Noop_AST_Node()); + m_match_stack.push_back(t); } + + return true; } /** diff --git a/unittests/for.chai b/unittests/for.chai index 850f4f5..389c02e 100644 --- a/unittests/for.chai +++ b/unittests/for.chai @@ -40,9 +40,9 @@ for (;;l = 1) assert_equal(0, l) -def isTrue(x) +def isNotFive(x) { - if (x == 5) + if (x != 5) { return true } else { @@ -50,7 +50,9 @@ def isTrue(x) } } -for (var m = 0; isTrue(m); m = m + 1) +var m; + +for (m = 0; isNotFive(m); m = m + 1) { }