diff --git a/boxedcpp/bootstrap.hpp b/boxedcpp/bootstrap.hpp index f6df879..0074d66 100644 --- a/boxedcpp/bootstrap.hpp +++ b/boxedcpp/bootstrap.hpp @@ -115,7 +115,7 @@ bool pod_less_than(Boxed_POD_Value l, Boxed_POD_Value r) { return l < r; } - + template bool greater_than(P1 p1, P2 p2) { @@ -126,7 +126,7 @@ bool pod_greater_than(Boxed_POD_Value l, Boxed_POD_Value r) { return l > r; } - + template bool less_than_equals(P1 p1, P2 p2) { @@ -163,7 +163,7 @@ P1 ×equal_pod(P1 &p1, Boxed_POD_Value r) return (p1 *= r.d); } else { return (p1 *= r.i); - } + } } @@ -181,7 +181,7 @@ P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r) return (p1 /= r.d); } else { return (p1 /= r.i); - } + } } @@ -199,7 +199,7 @@ P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r) return (p1 += r.d); } else { return (p1 += r.i); - } + } } template @@ -216,7 +216,7 @@ P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r) return (p1 -= r.d); } else { return (p1 -= r.i); - } + } } template @@ -348,7 +348,7 @@ void add_opers_arithmetic_pod(BoxedCPP_System &s) register_function(s, &pod_divide, "/"); register_function(s, &pod_multiply, "*"); } - + template void add_opers_comparison(BoxedCPP_System &s) { @@ -460,10 +460,28 @@ void bootstrap_pod_type(BoxedCPP_System &s, const std::string &name) register_function(s, &to_string, "to_string"); } +/* void print(const std::string &s) { std::cout << s << std::endl; } +*/ + +template +void print(const T &t) +{ + std::cout << t << std::endl; +} + +template<> void print(const bool &t) +{ + if (t) { + std::cout << "true" << std::endl; + } + else { + std::cout << "false" << std::endl; + } +} void bootstrap(BoxedCPP_System &s) { @@ -493,7 +511,11 @@ void bootstrap(BoxedCPP_System &s) add_oper_add(s); add_oper_add_equals (s); - register_function(s, &print, "print"); + register_function(s, &print, "print"); + register_function(s, &print, "print"); + register_function(s, &print, "print"); + register_function(s, &print, "print"); + register_function(s, &print, "print"); s.register_function(boost::function(boost::bind(&dump_system, boost::ref(s))), "dump_system"); s.register_function(boost::function(boost::bind(&dump_object, _1)), "dump_object"); diff --git a/samples/lambda.wes b/samples/lambda.wes new file mode 100644 index 0000000..ed93346 --- /dev/null +++ b/samples/lambda.wes @@ -0,0 +1,3 @@ +var add_them = function(x, y) { x + y } +//print(add_them(3, 4)) +print(7) diff --git a/wesley/wesley.hpp b/wesley/wesley.hpp index 245d51f..acc9849 100644 --- a/wesley/wesley.hpp +++ b/wesley/wesley.hpp @@ -14,14 +14,14 @@ class TokenType { public: enum Type { File, Whitespace, Identifier, Integer, Operator, Parens_Open, Parens_Close, Square_Open, Square_Close, Curly_Open, Curly_Close, Comma, Quoted_String, Single_Quoted_String, Carriage_Return, Semicolon, - Function_Def, Scoped_Block, Statement, Equation, Return, Expression, Term, Factor, Negate, Comment, + Function_Def, Lambda_Def, Scoped_Block, Statement, Equation, Return, Expression, Term, Factor, Negate, Comment, Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean, Real_Number, Array_Call, Variable_Decl, Array_Init, For_Block, Prefix, Break }; }; const char *tokentype_to_string(int tokentype) { const char *token_types[] = {"File", "Whitespace", "Identifier", "Integer", "Operator", "Parens_Open", "Parens_Close", "Square_Open", "Square_Close", "Curly_Open", "Curly_Close", "Comma", "Quoted_String", "Single_Quoted_String", "Carriage_Return", "Semicolon", - "Function_Def", "Scoped_Block", "Statement", "Equation", "Return", "Expression", "Term", "Factor", "Negate", "Comment", + "Function_Def", "Lambda_Def", "Scoped_Block", "Statement", "Equation", "Return", "Expression", "Term", "Factor", "Negate", "Comment", "Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean", "Real Number", "Array_Call", "Variable_Decl", "Array_Init", "For_Block", "Prefix", "Break" }; diff --git a/wesley/wesley_engine.hpp b/wesley/wesley_engine.hpp index 08480a6..84df81f 100644 --- a/wesley/wesley_engine.hpp +++ b/wesley/wesley_engine.hpp @@ -102,6 +102,7 @@ public: Rule params; Rule block(TokenType::Scoped_Block); Rule fundef(TokenType::Function_Def); + Rule lambda_def(TokenType::Lambda_Def); Rule statement; Rule equation(TokenType::Equation); Rule boolean(TokenType::Boolean); @@ -160,7 +161,7 @@ public: expression = term >> *((Str("+") >> term) | (Str("-") >> term)); term = factor >> *((Str("*") >> factor) | (Str("/") >> factor)); factor = methodcall | arraycall | value | negate | prefix | (Ign(Str("+")) >> value); - value = vardecl | arrayinit | block | paren_block | return_statement | break_statement | + value = vardecl | arrayinit | block | paren_block | lambda_def | return_statement | break_statement | funcall | Id(TokenType::Identifier) | Id(TokenType::Real_Number) | Id(TokenType::Integer) | Id(TokenType::Quoted_String) | Id(TokenType::Single_Quoted_String) ; @@ -175,6 +176,7 @@ public: return_statement = Ign(Str("return")) >> ~boolean; break_statement = Wrap(Ign(Str("break"))); paren_block = (Ign(Id(TokenType::Parens_Open)) >> equation >> Ign(Id(TokenType::Parens_Close))); + lambda_def = Ign(Str("function")) >> ~(Ign(Id(TokenType::Parens_Open)) >> ~params >> Ign(Id(TokenType::Parens_Close))) >> block; return rule; } diff --git a/wesley/wesley_eval.hpp b/wesley/wesley_eval.hpp index 0667851..d7ca4af 100644 --- a/wesley/wesley_eval.hpp +++ b/wesley/wesley_eval.hpp @@ -186,20 +186,27 @@ Boxed_Value eval_token(Eval_System &ss, TokenPtr node) { } break; case (TokenType::Fun_Call) : { + + //BoxedCPP_System::Stack prev_stack = ss.set_stack(BoxedCPP_System::Stack()); + Param_List_Builder plb; for (i = 1; i < node->children.size(); ++i) { plb << eval_token(ss, node->children[i]); } try { retval = dispatch(ss.get_function(node->children[0]->text), plb); + //ss.set_stack(prev_stack); } catch(EvalError &ee) { + //ss.set_stack(prev_stack); throw EvalError(ee.reason, node->children[0]); } catch(std::exception &e){ - throw EvalError("Can not find appropriate '" + node->children[0]->text + "'", node->children[0]); + //ss.set_stack(prev_stack); + throw EvalError("Engine error: " + std::string(e.what()), node->children[0]); } catch(ReturnValue &rv) { + //ss.set_stack(prev_stack); retval = rv.retval; } } @@ -349,6 +356,18 @@ Boxed_Value eval_token(Eval_System &ss, TokenPtr node) { new Dynamic_Proxy_Function(boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1))), node->children[0]->text); } break; + case (TokenType::Lambda_Def) : { + unsigned int num_args = node->children.size() - 1; + std::vector param_names; + for (i = 0; i < num_args; ++i) { + param_names.push_back(node->children[i]->text); + } + + //retval = boost::shared_ptr(new Proxy_Function_Impl >(&test)); + retval = Boxed_Value(boost::shared_ptr(new Dynamic_Proxy_Function( + boost::bind(&eval_function, boost::ref(ss), node->children.back(), param_names, _1)))); + } + break; case (TokenType::Scoped_Block) : { ss.new_scope(); for (i = 0; i < node->children.size(); ++i) {