Experimenting with adding comparisons

This commit is contained in:
Jonathan Turner
2009-06-04 15:28:45 +00:00
parent 8f89e44608
commit 00430e4592
3 changed files with 46 additions and 9 deletions

View File

@@ -28,6 +28,12 @@ Ret multiply(P1 p1, P2 p2)
return p1 * p2; return p1 * p2;
} }
template<typename Ret, typename P1, typename P2>
Ret equals(P1 p1, P2 p2)
{
return p1 == p2;
}
template<typename P1, typename P2> template<typename P1, typename P2>
P1 &timesequal(P1 &p1, P2 p2) P1 &timesequal(P1 &p1, P2 p2)
{ {
@@ -46,6 +52,7 @@ void bootstrap(BoxedCPP_System &s)
s.register_type<double>("double"); s.register_type<double>("double");
s.register_type<int>("int"); s.register_type<int>("int");
s.register_type<char>("char"); s.register_type<char>("char");
s.register_type<bool>("bool");
s.register_type<std::string>("string"); s.register_type<std::string>("string");
s.register_function(boost::function<std::string (const std::string &, const std::string&)>(&add<std::string, const std::string &, const std::string &>), "+"); s.register_function(boost::function<std::string (const std::string &, const std::string&)>(&add<std::string, const std::string &, const std::string &>), "+");
@@ -65,6 +72,11 @@ void bootstrap(BoxedCPP_System &s)
s.register_function(boost::function<double (double, int)>(&divide<double, double, int>), "/"); s.register_function(boost::function<double (double, int)>(&divide<double, double, int>), "/");
s.register_function(boost::function<double (double, double)>(&divide<double, double, double>), "/"); s.register_function(boost::function<double (double, double)>(&divide<double, double, double>), "/");
s.register_function(boost::function<int (int, int)>(&equals<bool, int, int>), "==");
s.register_function(boost::function<double (int, double)>(&equals<bool, int, double>), "==");
s.register_function(boost::function<double (double, int)>(&equals<bool, double, int>), "==");
s.register_function(boost::function<double (double, double)>(&equals<bool, double, double>), "==");
s.register_function(boost::function<int (int, int)>(&multiply<int, int, int>), "*"); s.register_function(boost::function<int (int, int)>(&multiply<int, int, int>), "*");
s.register_function(boost::function<double (int, double)>(&multiply<double, int, double>), "*"); s.register_function(boost::function<double (int, double)>(&multiply<double, int, double>), "*");
s.register_function(boost::function<double (double, int)>(&multiply<double, double, int>), "*"); s.register_function(boost::function<double (double, int)>(&multiply<double, double, int>), "*");

View File

@@ -36,7 +36,8 @@ class BoxedCPP_System
template<typename Class> template<typename Class>
void add_object(const std::string &name, const Class &obj) 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 Boxed_Value get_object(const std::string &name) const

View File

@@ -16,13 +16,13 @@
class TokenType { public: enum Type { File, Whitespace, Identifier, Number, Operator, Parens_Open, Parens_Close, 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, 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, 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 *tokentype_to_string(int tokentype) {
char *token_types[] = {"File", "Whitespace", "Identifier", "Number", "Operator", "Parens_Open", "Parens_Close", 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", "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", "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]; return token_types[tokentype];
} }
@@ -58,17 +58,28 @@ void debug_print(std::vector<TokenPtr> &tokens) {
} }
//A function that prints any string passed to it //A function that prints any string passed to it
template <typename T> template <typename T>
void print(const T &t) void print(const T &t)
{ {
std::cout << t << std::endl; std::cout << t << std::endl;
}
template<> void print<bool>(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) { std::string concat_string(const std::string &s1, const std::string &s2) {
return s1+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); return Boxed_Value(1);
} }
@@ -129,7 +140,15 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
} }
break; break;
case (TokenType::Identifier) : 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; break;
case (TokenType::Number) : case (TokenType::Number) :
retval = Boxed_Value(double(atof(node->text.c_str()))); retval = Boxed_Value(double(atof(node->text.c_str())));
@@ -150,7 +169,8 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
break; break;
case (TokenType::Factor) : case (TokenType::Factor) :
case (TokenType::Expression) : case (TokenType::Expression) :
case (TokenType::Term) : { case (TokenType::Term) :
case (TokenType::Comparison) : {
retval = eval_token(ss, node->children[0]); retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) { if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) { for (i = 1; i < node->children.size(); i += 2) {
@@ -300,6 +320,7 @@ Rule build_parser_rules() {
Rule statement(TokenType::Statement); Rule statement(TokenType::Statement);
Rule return_statement(TokenType::Return); Rule return_statement(TokenType::Return);
Rule equation(TokenType::Equation); Rule equation(TokenType::Equation);
Rule comparison(TokenType::Comparison);
Rule expression(TokenType::Expression); Rule expression(TokenType::Expression);
Rule term(TokenType::Term); Rule term(TokenType::Term);
Rule factor(TokenType::Factor); 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; fundef = Ign(Str("def")) >> Id(TokenType::Identifier) >> ~(Ign(Str("(")) >> ~params >> Ign(Str(")"))) >> block;
params = Id(TokenType::Identifier) >> *(Ign(Str(",")) >> Id(TokenType::Identifier)); params = Id(TokenType::Identifier) >> *(Ign(Str(",")) >> Id(TokenType::Identifier));
block = Ign(Str("{")) >> ~statements >> Ign(Str("}")); 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)); expression = term >> *((Str("+") >> term) | (Str("-") >> term));
term = factor >> *((Str("*") >> factor) | (Str("/") >> factor)); term = factor >> *((Str("*") >> factor) | (Str("/") >> factor));
factor = methodcall | poetrycall | value | negate | (Ign(Str("+")) >> value); 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 //Register a new function, this one with typing for us, so we don't have to ubox anything
//right here //right here
ss.register_function(boost::function<void (const bool &)>(&print<bool>), "print");
ss.register_function(boost::function<void (const std::string &)>(&print<std::string>), "print"); ss.register_function(boost::function<void (const std::string &)>(&print<std::string>), "print");
ss.register_function(boost::function<void (const double &)>(&print<double>), "print"); ss.register_function(boost::function<void (const double &)>(&print<double>), "print");
ss.register_function(boost::function<std::string (const std::string &, const std::string &)>(concat_string), "concat_string"); ss.register_function(boost::function<std::string (const std::string &, const std::string &)>(concat_string), "concat_string");
ss.register_function(boost::function<Boxed_Value (Boxed_Value, Boxed_Value)>(add_two), "add"); ss.register_function(boost::function<Boxed_Value (Boxed_Value, Boxed_Value)>(add_two), "add_two");
return ss; return ss;
} }