From c9995480e6b4bccbafd44fdf858f7cd145262ef5 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 23 Feb 2013 14:49:20 -0700 Subject: [PATCH] Add 'continue' command for loops. Also enhance for() unit tests which are now breaking and need to be fixed --- .../chaiscript/language/chaiscript_common.hpp | 8 +++ .../chaiscript/language/chaiscript_eval.hpp | 38 ++++++++++++-- .../chaiscript/language/chaiscript_parser.hpp | 25 +++++++++ unittests/break_for.chai | 11 ++++ unittests/continue_for.chai | 16 ++++++ unittests/continue_while.chai | 14 +++++ unittests/for.chai | 52 +++++++++++++++++++ 7 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 unittests/break_for.chai create mode 100644 unittests/continue_for.chai create mode 100644 unittests/continue_while.chai diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 911ac95..a694cfd 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -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 b2c684a..3657cb3 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -803,7 +803,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)); @@ -914,13 +920,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); @@ -1129,6 +1149,16 @@ 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) : + 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 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 c829ebb..0deca71 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1760,6 +1760,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 */ @@ -2257,6 +2274,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 (Equation()) { if (!saw_eol) { throw exception::eval_error("Two expressions missing line separator", File_Position(prev_line, prev_col), *m_filename); 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 9799be2..850f4f5 100644 --- a/unittests/for.chai +++ b/unittests/for.chai @@ -5,3 +5,55 @@ for (var 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 isTrue(x) +{ + if (x == 5) + { + return true + } else { + return false + } +} + +for (var m = 0; isTrue(m); m = m + 1) +{ + +} + +assert_equal(5, m); +