Skip to content
Browse files

Get all for loop related unit tests passing and expand the types of e…

…xpressions

that can exist in a for loop
  • Loading branch information...
1 parent c999548 commit 48f538438d7f45f106cf819b1bcc3cf85e241ae3 @lefticus lefticus committed Feb 23, 2013
View
8 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];
}
View
19 include/chaiscript/language/chaiscript_eval.hpp
@@ -1151,14 +1151,31 @@ 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<std::string> &t_fname=boost::shared_ptr<std::string>(), 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<std::string> &t_fname=boost::shared_ptr<std::string>(), 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 boost::shared_ptr<std::string> &t_fname=boost::shared_ptr<std::string>(), 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<std::string> &t_fname=boost::shared_ptr<std::string>(), int t_start_line = 0, int t_start_col = 0, int t_end_line = 0, int t_end_col = 0) :
View
32 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;
}
/**
View
8 unittests/for.chai
@@ -40,17 +40,19 @@ for (;;l = 1)
assert_equal(0, l)
-def isTrue(x)
+def isNotFive(x)
{
- if (x == 5)
+ if (x != 5)
{
return true
} else {
return false
}
}
-for (var m = 0; isTrue(m); m = m + 1)
+var m;
+
+for (m = 0; isNotFive(m); m = m + 1)
{
}

0 comments on commit 48f5384

Please sign in to comment.
Something went wrong with that request. Please try again.