diff --git a/contrib/codeanalysis/heterogenous_array_loop.chai b/contrib/codeanalysis/heterogenous_array_loop.chai new file mode 100644 index 0000000..5c688d1 --- /dev/null +++ b/contrib/codeanalysis/heterogenous_array_loop.chai @@ -0,0 +1,10 @@ + +var my_array=["1", 4, 6.6l, 10ul, "1000", 100, 10.9f ]; + +for (var j = 0; j < 10000; ++j) +{ + for (var i = 0; i < 6; ++i) + { + to_string(my_array[i]); + } +} diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index b17f1ec..e784e87 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, Reference, Switch, Case, Default, Ternary_Cond + Logical_And, Logical_Or, Reference, 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", "Reference", "Switch", "Case", "Default", "Ternary Condition"}; + "Logical_And", "Logical_Or", "Reference", "Switch", "Case", "Default", "Ternary Condition", "Noop"}; return ast_node_types[ast_node_type]; } @@ -494,6 +494,14 @@ namespace chaiscript Break_Loop() { } }; + /** + * Special type indicating a call to 'continue' + */ + struct Continue_Loop { + Continue_Loop() { } + }; + + /// Creates a new scope then pops it on destruction struct Scope_Push_Pop { diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 7850a00..1d2b3e1 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -822,7 +822,13 @@ namespace chaiscript } while (cond) { try { - this->children[1]->eval(t_ss); + try { + this->children[1]->eval(t_ss); + } catch (detail::Continue_Loop &) { + // we got a continue exception, which means all of the remaining + // loop implementation is skipped and we just need to continue to + // the next condition test + } try { cond = boxed_cast(this->children[0]->eval(t_ss)); @@ -933,13 +939,27 @@ namespace chaiscript while (cond) { try { if (this->children.size() == 4) { - this->children[3]->eval(t_ss); - this->children[2]->eval(t_ss); + + try { + this->children[3]->eval(t_ss); + } catch (detail::Continue_Loop &) { + // we got a continue exception, which means all of the remaining + // loop implementation is skipped and we just need to continue to + // the next iteration step + } + + this->children[2]->eval(t_ss); cond = boxed_cast(this->children[1]->eval(t_ss)); } else { - this->children[2]->eval(t_ss); + try { + this->children[2]->eval(t_ss); + } catch (detail::Continue_Loop &) { + // we got a continue exception, which means all of the remaining + // loop implementation is skipped and we just need to continue to + // the next iteration step + } this->children[1]->eval(t_ss); @@ -1168,6 +1188,33 @@ 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::Continue, const std::shared_ptr &t_fname=std::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 &){ + throw detail::Continue_Loop(); + } + }; + + 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 std::shared_ptr &t_fname=std::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 std::shared_ptr &t_fname=std::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 c19a55b..e429286 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1565,18 +1565,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; } /** @@ -1751,6 +1773,23 @@ namespace chaiscript return retval; } + /** + * Reads a continue statement from input + */ + bool Continue() { + bool retval = false; + + size_t prev_stack_top = m_match_stack.size(); + + if (Keyword("continue")) { + retval = true; + + build_match(AST_NodePtr(new eval::Continue_AST_Node()), prev_stack_top); + } + + return retval; + } + /** * Reads a dot expression(member access), then proceeds to check if it's a function or array call */ @@ -2276,6 +2315,14 @@ namespace chaiscript retval = true; saw_eol = false; } + else if (Continue()) { + if (!saw_eol) { + throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); + } + has_more = true; + retval = true; + saw_eol = false; + } else if (Block()) { has_more = true; retval = true; diff --git a/unittests/break_for.chai b/unittests/break_for.chai new file mode 100644 index 0000000..aaac7b9 --- /dev/null +++ b/unittests/break_for.chai @@ -0,0 +1,11 @@ +var j = 0; + +for (var i = 0; i < 10; ++i) { + if (i == 5) { + break + } + + j = i +} + +assert_equal(4, j); diff --git a/unittests/continue_for.chai b/unittests/continue_for.chai new file mode 100644 index 0000000..838b8e1 --- /dev/null +++ b/unittests/continue_for.chai @@ -0,0 +1,16 @@ +var j = 0; +var k = 0; + +for (var i = 0; i < 10; ++i) +{ + j = i + if (i > 5) + { + continue + } + + k = i +} + +assert_equal(5, k); +assert_equal(9, j); diff --git a/unittests/continue_while.chai b/unittests/continue_while.chai new file mode 100644 index 0000000..a1b44cb --- /dev/null +++ b/unittests/continue_while.chai @@ -0,0 +1,14 @@ +var i = 0 +var j = 0 + +while (i < 10) { + if (++i > 5) + { + continue + } + + j = i; +} + +assert_equal(10, i); +assert_equal(5, j); diff --git a/unittests/for.chai b/unittests/for.chai index c576999..e2d8370 100644 --- a/unittests/for.chai +++ b/unittests/for.chai @@ -5,3 +5,57 @@ for (auto i = 0; i < 5; ++i) { } assert_equal([0,1,2,3,4], ret); + + +var j = 0; + +for (;j<10; ++j) +{ +} + +assert_equal(10, j); + + +var k = 0; + +for (;k<10; ) +{ + ++k; +} + +assert_equal(10, k); + + +for (;;) +{ + break; +} + +var l = 0; + +for (;;l = 1) +{ + break; +} + +assert_equal(0, l) + +def isNotFive(x) +{ + if (x != 5) + { + return true + } else { + return false + } +} + +var m; + +for (m = 0; isNotFive(m); m = m + 1) +{ + +} + +assert_equal(5, m); + diff --git a/unittests/vector_of_suffixed_numbers.chai b/unittests/vector_of_suffixed_numbers.chai new file mode 100644 index 0000000..aee96b9 --- /dev/null +++ b/unittests/vector_of_suffixed_numbers.chai @@ -0,0 +1,3 @@ +var x = [1, 2u, 3.0l] +assert_equal(3.0l, x[2]) +assert_equal(2u, x[1])