First, unsuccessful, stab at not and negate

This commit is contained in:
Jonathan Turner
2009-06-11 03:11:05 +00:00
parent c20502cc81
commit 8c689d2017
4 changed files with 23 additions and 18 deletions

View File

@@ -231,6 +231,19 @@ P1 &prefixdecrement(P1 &p1)
return (--p1); return (--p1);
} }
template<typename P1>
P1 &prefixnegate(P1 &p1)
{
return (p1);
}
template<typename P1>
P1 &prefixnot(P1 &p1)
{
return (p1);
}
//Add canonical forms of operators //Add canonical forms of operators
template<typename T> template<typename T>
void add_oper_equals(BoxedCPP_System &s) void add_oper_equals(BoxedCPP_System &s)
@@ -368,6 +381,8 @@ void add_opers_arithmetic_overload(BoxedCPP_System &s)
register_function(s, &addsequal<T, R>, "+="); register_function(s, &addsequal<T, R>, "+=");
register_function(s, &prefixincrement<T>, "++"); register_function(s, &prefixincrement<T>, "++");
register_function(s, &prefixdecrement<T>, "--"); register_function(s, &prefixdecrement<T>, "--");
register_function(s, &prefixnegate<T>, "-");
register_function(s, &prefixnot<T>, "!");
} }
template<typename T> template<typename T>

View File

@@ -14,14 +14,14 @@
class TokenType { public: enum Type { File, Whitespace, Identifier, Integer, Operator, Parens_Open, Parens_Close, 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, Square_Open, Square_Close, Curly_Open, Curly_Close, Comma, Quoted_String, Single_Quoted_String, Carriage_Return, Semicolon,
Function_Def, Lambda_Def, Scoped_Block, Statement, Equation, Return, Expression, Term, Factor, Negate, Comment, Function_Def, Lambda_Def, Scoped_Block, Statement, Equation, Return, Expression, Term, Factor, Negate, Not, Comment,
Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean, Real_Number, Array_Call, Variable_Decl, Array_Init, Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean, Real_Number, Array_Call, Variable_Decl, Array_Init,
For_Block, Prefix, Break }; }; For_Block, Prefix, Break }; };
const char *tokentype_to_string(int tokentype) { const char *tokentype_to_string(int tokentype) {
const char *token_types[] = {"File", "Whitespace", "Identifier", "Integer", "Operator", "Parens_Open", "Parens_Close", 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", "Square_Open", "Square_Close", "Curly_Open", "Curly_Close", "Comma", "Quoted_String", "Single_Quoted_String", "Carriage_Return", "Semicolon",
"Function_Def", "Lambda_Def", "Scoped_Block", "Statement", "Equation", "Return", "Expression", "Term", "Factor", "Negate", "Comment", "Function_Def", "Lambda_Def", "Scoped_Block", "Statement", "Equation", "Return", "Expression", "Term", "Factor", "Negate", "Not", "Comment",
"Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean", "Real Number", "Array_Call", "Variable_Decl", "Array_Init", "Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean", "Real Number", "Array_Call", "Variable_Decl", "Array_Init",
"For_Block", "Prefix", "Break" }; "For_Block", "Prefix", "Break" };

View File

@@ -111,6 +111,7 @@ public:
Rule term(TokenType::Term); Rule term(TokenType::Term);
Rule factor(TokenType::Factor); Rule factor(TokenType::Factor);
Rule negate(TokenType::Negate); Rule negate(TokenType::Negate);
Rule boolean_not(TokenType::Not);
Rule prefix(TokenType::Prefix); Rule prefix(TokenType::Prefix);
Rule funcall(TokenType::Fun_Call); Rule funcall(TokenType::Fun_Call);
@@ -160,14 +161,15 @@ public:
(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 | arraycall | value | negate | prefix | (Ign(Str("+")) >> value); factor = methodcall | arraycall | value | negate | boolean_not | prefix | (Ign(Str("+")) >> value);
value = vardecl | arrayinit | block | paren_block | lambda_def | 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) | funcall | Id(TokenType::Identifier) | Id(TokenType::Real_Number) | Id(TokenType::Integer) | Id(TokenType::Quoted_String) |
Id(TokenType::Single_Quoted_String) ; Id(TokenType::Single_Quoted_String) ;
funcall = Id(TokenType::Identifier) >> Ign(Id(TokenType::Parens_Open)) >> ~(boolean >> *(Ign(Str("," )) >> boolean)) >> 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); methodcall = value >> +(Ign(Str(".")) >> funcall);
negate = Ign(Str("-")) >> boolean; negate = (Str("-") >> (boolean | arraycall));
boolean_not = (Str("!") >> (boolean | arraycall));
prefix = (Str("++") >> (boolean | arraycall)) | (Str("--") >> (boolean | arraycall)); prefix = (Str("++") >> (boolean | arraycall)) | (Str("--") >> (boolean | arraycall));
arraycall = value >> +((Ign(Id(TokenType::Square_Open)) >> boolean >> Ign(Id(TokenType::Square_Close)))); arraycall = value >> +((Ign(Id(TokenType::Square_Open)) >> boolean >> Ign(Id(TokenType::Square_Close))));

View File

@@ -140,20 +140,8 @@ Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
} }
} }
break; break;
case (TokenType::Negate) : { case (TokenType::Negate) :
retval = eval_token(ss, node->children[0]); case (TokenType::Not) :
Param_List_Builder plb;
plb << retval;
//plb << Boxed_Value(-1);
try {
retval = dispatch(ss.get_function("-"), plb);
}
catch(std::exception &e){
throw EvalError("Can not find appropriate negation", node->children[0]);
}
}
break;
case (TokenType::Prefix) : { case (TokenType::Prefix) : {
retval = eval_token(ss, node->children[1]); retval = eval_token(ss, node->children[1]);
Param_List_Builder plb; Param_List_Builder plb;