From 00430e4592065730b6cdf6c91ef8ae3384798755 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 4 Jun 2009 15:28:45 +0000 Subject: [PATCH] Experimenting with adding comparisons --- boxedcpp/bootstrap.hpp | 12 ++++++++++++ boxedcpp/boxedcpp.hpp | 3 ++- wesley/main.cpp | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/boxedcpp/bootstrap.hpp b/boxedcpp/bootstrap.hpp index ebd0d79..084c480 100644 --- a/boxedcpp/bootstrap.hpp +++ b/boxedcpp/bootstrap.hpp @@ -28,6 +28,12 @@ Ret multiply(P1 p1, P2 p2) return p1 * p2; } +template +Ret equals(P1 p1, P2 p2) +{ + return p1 == p2; +} + template P1 ×equal(P1 &p1, P2 p2) { @@ -46,6 +52,7 @@ void bootstrap(BoxedCPP_System &s) s.register_type("double"); s.register_type("int"); s.register_type("char"); + s.register_type("bool"); s.register_type("string"); s.register_function(boost::function(&add), "+"); @@ -65,6 +72,11 @@ void bootstrap(BoxedCPP_System &s) s.register_function(boost::function), "/"); s.register_function(boost::function), "/"); + s.register_function(boost::function(&equals), "=="); + s.register_function(boost::function(&equals), "=="); + s.register_function(boost::function(&equals), "=="); + s.register_function(boost::function(&equals), "=="); + s.register_function(boost::function(&multiply), "*"); s.register_function(boost::function(&multiply), "*"); s.register_function(boost::function(&multiply), "*"); diff --git a/boxedcpp/boxedcpp.hpp b/boxedcpp/boxedcpp.hpp index facd578..d89f068 100644 --- a/boxedcpp/boxedcpp.hpp +++ b/boxedcpp/boxedcpp.hpp @@ -36,7 +36,8 @@ class BoxedCPP_System template void add_object(const std::string &name, const Class &obj) { - m_objects.insert(std::make_pair(name, Boxed_Value(obj))); + //m_objects.insert(std::make_pair(name, Boxed_Value(obj))); + m_objects[name] = Boxed_Value(obj); } Boxed_Value get_object(const std::string &name) const diff --git a/wesley/main.cpp b/wesley/main.cpp index fe14706..8afdbab 100644 --- a/wesley/main.cpp +++ b/wesley/main.cpp @@ -16,13 +16,13 @@ class TokenType { public: enum Type { File, Whitespace, Identifier, Number, 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, - Value, Fun_Call, Method_Call, Poetry_Call }; }; + Value, Fun_Call, Method_Call, Poetry_Call, Comparison }; }; char *tokentype_to_string(int tokentype) { char *token_types[] = {"File", "Whitespace", "Identifier", "Number", "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", - "Value", "Fun_Call", "Method_Call", "Poetry_Call" }; + "Value", "Fun_Call", "Method_Call", "Poetry_Call", "Comparison" }; return token_types[tokentype]; } @@ -58,17 +58,28 @@ void debug_print(std::vector &tokens) { } //A function that prints any string passed to it + template void print(const T &t) { - std::cout << t << std::endl; + std::cout << t << std::endl; +} + +template<> void print(const bool &t) +{ + if (t) { + std::cout << "true" << std::endl; + } + else { + std::cout << "false" << std::endl; + } } std::string concat_string(const std::string &s1, const std::string &s2) { return s1+s2; } -Boxed_Value add_two(Boxed_Value val1, Boxed_Value val2) { +const Boxed_Value add_two(const Boxed_Value &val1, const Boxed_Value &val2) { return Boxed_Value(1); } @@ -129,7 +140,15 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) { } break; case (TokenType::Identifier) : - retval = ss.get_object(node->text); + if (node->text == "true") { + retval = Boxed_Value(true); + } + else if (node->text == "false") { + retval = Boxed_Value(false); + } + else { + retval = ss.get_object(node->text); + } break; case (TokenType::Number) : retval = Boxed_Value(double(atof(node->text.c_str()))); @@ -150,7 +169,8 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) { break; case (TokenType::Factor) : case (TokenType::Expression) : - case (TokenType::Term) : { + case (TokenType::Term) : + case (TokenType::Comparison) : { retval = eval_token(ss, node->children[0]); if (node->children.size() > 1) { for (i = 1; i < node->children.size(); i += 2) { @@ -300,6 +320,7 @@ Rule build_parser_rules() { Rule statement(TokenType::Statement); Rule return_statement(TokenType::Return); Rule equation(TokenType::Equation); + Rule comparison(TokenType::Comparison); Rule expression(TokenType::Expression); Rule term(TokenType::Term); Rule factor(TokenType::Factor); @@ -315,7 +336,9 @@ Rule build_parser_rules() { fundef = Ign(Str("def")) >> Id(TokenType::Identifier) >> ~(Ign(Str("(")) >> ~params >> Ign(Str(")"))) >> block; params = Id(TokenType::Identifier) >> *(Ign(Str(",")) >> Id(TokenType::Identifier)); block = Ign(Str("{")) >> ~statements >> Ign(Str("}")); - equation = *(Id(TokenType::Identifier) >> Ign(Str("="))) >> expression; + equation = *(Id(TokenType::Identifier) >> Ign(Str("="))) >> comparison; + comparison = expression >> *((Str("==") >> expression) | (Str("!=") >> expression) | (Str("<") >> expression) | + (Str("<=") >> expression) |(Str(">") >> expression) | (Str(">=") >> expression)); expression = term >> *((Str("+") >> term) | (Str("-") >> term)); term = factor >> *((Str("*") >> factor) | (Str("/") >> factor)); factor = methodcall | poetrycall | value | negate | (Ign(Str("+")) >> value); @@ -363,10 +386,11 @@ BoxedCPP_System build_eval_system() { //Register a new function, this one with typing for us, so we don't have to ubox anything //right here + ss.register_function(boost::function(&print), "print"); ss.register_function(boost::function(&print), "print"); ss.register_function(boost::function(&print), "print"); ss.register_function(boost::function(concat_string), "concat_string"); - ss.register_function(boost::function(add_two), "add"); + ss.register_function(boost::function(add_two), "add_two"); return ss; }