summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/Parser.cpp
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-28 17:39:43 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-28 17:39:43 +0100
commit93a63d43e950c53230c1658e86111ee962faf7f9 (patch)
treea283f69f78082926e8f8713cf5222ecec20eed94 /Sencha-lang/Parser.cpp
parent4d3eaee1b6f0ea0c0c9a28f11ce9ba249a0def96 (diff)
downloadsencha-lang-93a63d43e950c53230c1658e86111ee962faf7f9.tar.gz
sencha-lang-93a63d43e950c53230c1658e86111ee962faf7f9.tar.bz2
sencha-lang-93a63d43e950c53230c1658e86111ee962faf7f9.tar.xz
sencha-lang-93a63d43e950c53230c1658e86111ee962faf7f9.zip
Silly mistake, and lots of fun, heh :P
Diffstat (limited to 'Sencha-lang/Parser.cpp')
-rw-r--r--Sencha-lang/Parser.cpp89
1 files changed, 25 insertions, 64 deletions
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index ca52866..9a862fe 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -69,16 +69,10 @@ bool Parser::read_next()
void Parser::interpret()
{
read_next();
- while(tok_value!= "")
- {
- program->add_statement(statement());
- }
+ while(tok_value!= "") program->add_statement(statement());
}
-bool Parser::peek(string s)
-{
- return tok_value == s;
-}
+bool Parser::peek(string s) {return tok_value == s;}
bool Parser::accept(string s)
{
@@ -110,10 +104,7 @@ bool Parser::is_type()
read_next();
return true;
}
- else
- {
- return false;
- }
+ else return false;
}
bool Parser::is_function_name()
@@ -123,21 +114,16 @@ bool Parser::is_function_name()
read_next();
return true;
}
- else
- {
- return false;
- }
+ else return false;
}
ASTStatement * Parser::statement()
-{
- BasicStatement * stat = new BasicStatement();
+{
if(accept("{"))
- {
- while(!accept("}"))
- {
- stat->children.push_back( statement());
- }
+ {
+ BasicStatement * stat = new BasicStatement();
+ while(!accept("}")) stat->children.push_back(statement());
+ return stat;
}
else if(is_type())
{
@@ -158,23 +144,14 @@ ASTStatement * Parser::statement()
{
argc++;
is_type();
-
declaration->add_argument(tok_value);
read_next();
- if(peek(")"))
- {
- break;
- }
+ if(peek(")")) break;
expect(",");
}
expect(")");
- if(!accept(";"))
- {
-
- declaration->add_body(statement());
- }
+ if(!accept(";")) declaration->add_body(statement());
}
- delete stat;
return declaration;
}
else if(accept("if"))
@@ -182,11 +159,7 @@ ASTStatement * Parser::statement()
IfNode * ifStatement = new IfNode();
ifStatement->add_condition(expr());
ifStatement->add_body(statement());
- if(accept("else"))
- {
- ifStatement->add_else_block(statement());
- }
- delete stat;
+ if(accept("else")) ifStatement->add_else_block(statement());
return ifStatement;
}
else if(accept("repeat"))
@@ -194,34 +167,27 @@ ASTStatement * Parser::statement()
RepeatStatement * repeat = new RepeatStatement();
repeat->add_iteration_number(expr()->evaluate());
repeat->add_body(statement());
- delete stat;
return repeat;
}
else if(accept("while"))
{
- WhileNode * while_node = new WhileNode();
- while_node->add_condition(expr());
- while_node->add_body(statement());
- delete stat;
+ WhileNode * while_node = new WhileNode(expr(), statement());
return while_node;
}
else if(accept("return"))
- {
- if(!peek(";"))
- {
- stat->add_expression(expr());
- }
+ {
+ BasicStatement * stat = new BasicStatement();
+ if(!peek(";")) stat->add_expression(expr());
expect(";");
return stat;
}
else
- {
+ {
+ BasicStatement * stat = new BasicStatement();
stat->add_expression(expr());
while(!expect(";") && tok_value != "") read_next();
return stat;
-
- }
- return stat;
+ }
}
ASTExpression * Parser::prim_expr()
@@ -262,18 +228,15 @@ ASTExpression * Parser::prim_expr()
else if(current_token.get_type() == t_symbol)
{
string name = current_token.value;
- VariableExpression * ve;
- ve = new VariableExpression(name, context);
+ VariableExpression * ve = new VariableExpression(name, context);
read_next();
return ve;
}
else if(accept("("))
{
- BasicExpression * be;
- be = static_cast<BasicExpression *>(expr());
-
+ ASTExpression * expression = expr();
expect(")");
- return be;
+ return expression;
}
else
{
@@ -294,7 +257,6 @@ ASTExpression * Parser::postfix_expr()
{
if(!accept(")"))
{
-
function_call->add_argument(expr());
while(accept(","))
{
@@ -350,9 +312,8 @@ ASTExpression * Parser::eq_expr()
ASTExpression * left = rel_expr();
if(peek("==") || peek("!="))
{
- string oper;
- if(accept("==")) oper = "==";
- else oper = "!=";
+ string oper = tok_value;
+ read_next();
return new BasicExpression(left, eq_expr(), oper);
}
else return left;
@@ -365,7 +326,7 @@ ASTExpression * Parser::log_expr()
{
string oper;
if(accept("and")) oper = "&&";
- else oper = "||";
+ else if(accept("or")) oper = "||";
ASTExpression * right = log_expr();
return new BasicExpression(left, right, oper);
}