summaryrefslogtreecommitdiffstats
path: root/Sencha-lang
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-22 17:56:32 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-22 17:56:32 +0100
commit6721f24be8b1504f62bc458dab08e3e74defac30 (patch)
treec694726426d15053f767b8375fb121540adc2f50 /Sencha-lang
parent4b8baa58e6c791102f08958cac974241ea9dfe6b (diff)
downloadsencha-lang-6721f24be8b1504f62bc458dab08e3e74defac30.tar.gz
sencha-lang-6721f24be8b1504f62bc458dab08e3e74defac30.tar.bz2
sencha-lang-6721f24be8b1504f62bc458dab08e3e74defac30.tar.xz
sencha-lang-6721f24be8b1504f62bc458dab08e3e74defac30.zip
Logical and and or is ready to use now.
Diffstat (limited to 'Sencha-lang')
-rw-r--r--Sencha-lang/Lexer.cpp8
-rw-r--r--Sencha-lang/Parser.cpp44
-rw-r--r--Sencha-lang/Parser.h1
-rw-r--r--Sencha-lang/Tests/TestParser.cpp18
-rw-r--r--Sencha-lang/Tests/TestParser.h41
5 files changed, 99 insertions, 13 deletions
diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp
index 9da8145..681e927 100644
--- a/Sencha-lang/Lexer.cpp
+++ b/Sencha-lang/Lexer.cpp
@@ -2,14 +2,14 @@
Lexer::Lexer()
{
- string keys[] = {"function", "class", "for", "while", "if", "else", "true", "false"};
- keywords.assign(keys, keys+8);
+ string keys[] = {"function", "class", "for", "while", "if", "else", "true", "false", "and", "or"};
+ keywords.assign(keys, keys+10);
char punct[] = {'.', ',', ';', '{', '}', '[', ']', '(', ')'};
punctuation.assign(punct, punct+9);
- string oper[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!=", "&&", "||"};
- operators.assign(oper, oper +19);
+ string oper[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!="};
+ operators.assign(oper, oper +17);
}
Lexer::~Lexer()
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 36a3c82..8a367b2 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -367,8 +367,7 @@ ASTExpression * Parser::mul_expr(ASTNode * expression)
}
be->set_right_operand(mul_expr(be));
}
-
- if(be->get_operator() == "")
+ else
{
ASTExpression * ae;
ae = static_cast<ASTExpression *>(be->children[0]);
@@ -398,8 +397,7 @@ ASTExpression * Parser::add_expr(ASTNode * expression)
}
be->set_right_operand(add_expr(be));
}
-
- if(be->get_operator() == "")
+ else
{
ASTExpression * ae;
ae = static_cast<ASTExpression *>(be->children[0]);
@@ -429,8 +427,7 @@ ASTExpression * Parser::rel_expr(ASTNode * expression)
}
be->set_right_operand(rel_expr(be));
}
-
- if(be->get_operator() == "")
+ else
{
ASTExpression * ae;
ae = static_cast<ASTExpression *>(be->children[0]);
@@ -461,8 +458,7 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
}
be->set_right_operand(eq_expr(be));
}
-
- if(be->get_operator() == "")
+ else
{
ASTExpression * ae;
ae = static_cast<ASTExpression *>(be->children[0]);
@@ -472,13 +468,43 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
}
return be;
+}
+
+ASTExpression * Parser::log_expr(ASTNode * expression)
+{
+ BasicExpression * be = new BasicExpression(expression);
+ be->set_left_operand(eq_expr(be));
+ if(peek("and") || peek("or"))
+ {
+ if(accept("and"))
+ {
+ be->set_operator("&&");
+
+ }
+ else if(accept("or"))
+ {
+ be->set_operator("||");
+
+ }
+ be->set_right_operand(log_expr(be));
+ }
+ else
+ {
+ ASTExpression * ae;
+ ae = static_cast<ASTExpression *>(be->children[0]);
+ ae->parent = expression;
+ delete be;
+ return ae;
+ }
+
+ return be;
}
ASTExpression * Parser::expr(ASTNode * expression)
{
Assignment * assignment = new Assignment(expression, context);
auto name = tok_value;
- ASTExpression * left = eq_expr(assignment);
+ ASTExpression * left = log_expr(assignment);
if(accept("="))
{
diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h
index 824b7a8..80033b7 100644
--- a/Sencha-lang/Parser.h
+++ b/Sencha-lang/Parser.h
@@ -46,6 +46,7 @@ class Parser
ASTStatement * statement(ASTNode * node);
+ ASTExpression * log_expr(ASTNode * node);
ASTExpression * mul_expr(ASTNode * node);
ASTExpression * add_expr(ASTNode * node);
ASTExpression * prim_expr(ASTNode * node);
diff --git a/Sencha-lang/Tests/TestParser.cpp b/Sencha-lang/Tests/TestParser.cpp
new file mode 100644
index 0000000..aa40b2c
--- /dev/null
+++ b/Sencha-lang/Tests/TestParser.cpp
@@ -0,0 +1,18 @@
+/*
+ * TestParser.cpp
+ *
+ * Created on: Dec 22, 2012
+ * Author: att
+ */
+
+#include "TestParser.h"
+
+TestParser::TestParser() {
+ // TODO Auto-generated constructor stub
+
+}
+
+TestParser::~TestParser() {
+ // TODO Auto-generated destructor stub
+}
+
diff --git a/Sencha-lang/Tests/TestParser.h b/Sencha-lang/Tests/TestParser.h
new file mode 100644
index 0000000..6a11a6c
--- /dev/null
+++ b/Sencha-lang/Tests/TestParser.h
@@ -0,0 +1,41 @@
+/*
+ * TestParser.h
+ *
+ * Created on: Dec 22, 2012
+ * Author: att
+ */
+
+#ifndef TESTPARSER_H_
+#define TESTPARSER_H_
+
+#include "TestSuite.h"
+
+class TestParser: public TestSuite {
+public:
+ TestParser();
+ virtual ~TestParser();
+};
+
+#endif /* TESTPARSER_H_ */
+
+/*TODO
+ * write test of evaluating logical expression
+ * possible input:
+ * false and false
+ * false and true
+ * true and false
+ * true and true
+ * false or false
+ * false or true
+ * true or false
+ * true or true
+ * 7 + 2 > 55 or 4-5 == -1
+ * some variable == "g" and other != "a"
+ *
+ * some easy expression like:
+ * if("aaa" != "bbb" and 2 < 5) {
+>> print(1);
+>> } else {
+>> print(0);
+>> }
+ */