Added booleans. Add if/elseif/else and while blocks. Added more comparisons.

This commit is contained in:
Jonathan Turner 2009-06-05 11:45:57 +00:00
parent 5d1bae709a
commit 3f4aed0db9
2 changed files with 130 additions and 10 deletions

View File

@ -28,12 +28,54 @@ Ret multiply(P1 p1, P2 p2)
return p1 * p2;
}
template<typename P1, typename P2>
bool bool_and(P1 p1, P2 p2)
{
return p1 && p2;
}
template<typename P1, typename P2>
bool bool_or(P1 p1, P2 p2)
{
return p1 || p2;
}
template<typename P1, typename P2>
bool equals(P1 p1, P2 p2)
{
return p1 == p2;
}
template<typename P1, typename P2>
bool not_equals(P1 p1, P2 p2)
{
return p1 != p2;
}
template<typename P1, typename P2>
bool less_than(P1 p1, P2 p2)
{
return p1 < p2;
}
template<typename P1, typename P2>
bool greater_than(P1 p1, P2 p2)
{
return p1 > p2;
}
template<typename P1, typename P2>
bool less_than_equals(P1 p1, P2 p2)
{
return p1 <= p2;
}
template<typename P1, typename P2>
bool greater_than_equals(P1 p1, P2 p2)
{
return p1 >= p2;
}
template<typename P1, typename P2>
P1 &timesequal(P1 &p1, P2 p2)
{
@ -72,11 +114,39 @@ void bootstrap(BoxedCPP_System &s)
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<bool (bool, bool)>(&bool_and<bool, bool>), "&&");
s.register_function(boost::function<bool (bool, bool)>(&bool_or<bool, bool>), "||");
s.register_function(boost::function<bool (int, int)>(&equals<int, int>), "==");
s.register_function(boost::function<bool (int, double)>(&equals<int, double>), "==");
s.register_function(boost::function<bool (double, int)>(&equals<double, int>), "==");
s.register_function(boost::function<bool (double, double)>(&equals<double, double>), "==");
s.register_function(boost::function<bool (int, int)>(&not_equals<int, int>), "!=");
s.register_function(boost::function<bool (int, double)>(&not_equals<int, double>), "!=");
s.register_function(boost::function<bool (double, int)>(&not_equals<double, int>), "!=");
s.register_function(boost::function<bool (double, double)>(&not_equals<double, double>), "!=");
s.register_function(boost::function<bool (int, int)>(&less_than<int, int>), "<");
s.register_function(boost::function<bool (int, double)>(&less_than<int, double>), "<");
s.register_function(boost::function<bool (double, int)>(&less_than<double, int>), "<");
s.register_function(boost::function<bool (double, double)>(&less_than<double, double>), "<");
s.register_function(boost::function<bool (int, int)>(&greater_than<int, int>), ">");
s.register_function(boost::function<bool (int, double)>(&greater_than<int, double>), ">");
s.register_function(boost::function<bool (double, int)>(&greater_than<double, int>), ">");
s.register_function(boost::function<bool (double, double)>(&greater_than<double, double>), ">");
s.register_function(boost::function<bool (int, int)>(&less_than_equals<int, int>), "<=");
s.register_function(boost::function<bool (int, double)>(&less_than_equals<int, double>), "<=");
s.register_function(boost::function<bool (double, int)>(&less_than_equals<double, int>), "<=");
s.register_function(boost::function<bool (double, double)>(&less_than_equals<double, double>), "<=");
s.register_function(boost::function<bool (int, int)>(&greater_than_equals<int, int>), ">=");
s.register_function(boost::function<bool (int, double)>(&greater_than_equals<int, double>), ">=");
s.register_function(boost::function<bool (double, int)>(&greater_than_equals<double, int>), ">=");
s.register_function(boost::function<bool (double, double)>(&greater_than_equals<double, double>), ">=");
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 (double, int)>(&multiply<double, double, int>), "*");

View File

@ -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, Comparison }; };
Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean }; };
const char *tokentype_to_string(int tokentype) {
const 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", "Comparison" };
"Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean" };
return token_types[tokentype];
}
@ -170,6 +170,7 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
case (TokenType::Factor) :
case (TokenType::Expression) :
case (TokenType::Term) :
case (TokenType::Boolean) :
case (TokenType::Comparison) : {
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
@ -217,7 +218,6 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
}
break;
case (TokenType::Method_Call) : {
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); ++i) {
@ -238,6 +238,43 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
}
}
break;
case(TokenType::If_Block) : {
retval = eval_token(ss, node->children[0]);
bool cond = Cast_Helper<bool &>()(retval);
if (cond) {
retval = eval_token(ss, node->children[1]);
}
else {
if (node->children.size() > 2) {
i = 2;
while ((!cond) && (i < node->children.size())) {
if (node->children[i]->text == "else") {
retval = eval_token(ss, node->children[i+1]);
cond = true;
}
else if (node->children[i]->text == "elseif") {
retval = eval_token(ss, node->children[i+1]);
cond = Cast_Helper<bool &>()(retval);
if (cond) {
retval = eval_token(ss, node->children[i+2]);
}
}
i = i + 3;
}
}
}
}
break;
case(TokenType::While_Block) : {
retval = eval_token(ss, node->children[0]);
bool cond = Cast_Helper<bool &>()(retval);
while (cond) {
eval_token(ss, node->children[1]);
retval = eval_token(ss, node->children[0]);
cond = Cast_Helper<bool &>()(retval);
}
}
break;
case (TokenType::Function_Def) : {
unsigned int num_args = node->children.size() - 2;
std::vector<std::string> param_names;
@ -276,7 +313,12 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
}
}
break;
case (TokenType::Scoped_Block) :
case (TokenType::Scoped_Block) : {
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
}
}
break;
case (TokenType::Statement) :
case (TokenType::Return) :
case (TokenType::Carriage_Return) :
@ -301,9 +343,10 @@ Rule build_parser_rules() {
Rule params;
Rule block(TokenType::Scoped_Block);
Rule fundef(TokenType::Function_Def);
Rule statement(TokenType::Statement);
Rule statement;
Rule return_statement(TokenType::Return);
Rule equation(TokenType::Equation);
Rule boolean(TokenType::Boolean);
Rule comparison(TokenType::Comparison);
Rule expression(TokenType::Expression);
Rule term(TokenType::Term);
@ -311,27 +354,34 @@ Rule build_parser_rules() {
Rule negate(TokenType::Negate);
Rule funcall(TokenType::Fun_Call);
Rule methodcall(TokenType::Method_Call);
Rule if_block(TokenType::If_Block);
Rule while_block(TokenType::While_Block);
Rule value;
Rule statements;
Rule rule = *(fundef | statements);
statements = (equation >> *(Ign(Id(TokenType::Semicolon)) >> equation) >> *(Ign(Id(TokenType::Semicolon))));
statements = (statement >> *(Ign(Id(TokenType::Semicolon)) >> statement) >> *(Ign(Id(TokenType::Semicolon))));
statement = if_block | while_block | equation;
if_block = Ign(Str("if")) >> boolean >> block >> *(Str("elseif") >> boolean >> block) >> ~(Str("else") >> block);
while_block = Ign(Str("while")) >> boolean >> block;
fundef = Ign(Str("def")) >> Id(TokenType::Identifier) >> ~(Ign(Id(TokenType::Parens_Open)) >> ~params >> Ign(Id(TokenType::Parens_Close))) >>
block >> ~Ign(Id(TokenType::Semicolon));
params = Id(TokenType::Identifier) >> *(Ign(Str(",")) >> Id(TokenType::Identifier));
block = Ign(Str("{")) >> ~statements >> Ign(Str("}"));
block = Ign(Id(TokenType::Curly_Open)) >> ~statements >> Ign(Id(TokenType::Curly_Close));
equation = *(Id(TokenType::Identifier) >> Ign(Str("="))) >> comparison;
boolean = comparison >> *((Str("&&") >> comparison) | (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 | value | negate | (Ign(Str("+")) >> value);
funcall = Id(TokenType::Identifier) >> Ign(Id(TokenType::Parens_Open)) >> ~(expression >> *(Ign(Str("," )) >> expression)) >> Ign(Id(TokenType::Parens_Close));
funcall = Id(TokenType::Identifier) >> Ign(Id(TokenType::Parens_Open)) >> ~(boolean >> *(Ign(Str("," )) >> boolean)) >> Ign(Id(TokenType::Parens_Close));
methodcall = value >> +(Ign(Str(".")) >> funcall);
negate = Ign(Str("-")) >> factor;
return_statement = Ign(Str("return")) >> expression;
return_statement = Ign(Str("return")) >> boolean;
value = (Ign(Id(TokenType::Parens_Open)) >> expression >> Ign(Id(TokenType::Parens_Close))) | return_statement |
value = (Ign(Id(TokenType::Parens_Open)) >> boolean >> Ign(Id(TokenType::Parens_Close))) | return_statement |
funcall | Id(TokenType::Identifier) | Id(TokenType::Number) | Id(TokenType::Quoted_String) | Id(TokenType::Single_Quoted_String) ;
return rule;