summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-09 11:29:08 +0100
committerJustyna Att Ilczuk <justyna.ilczuk@gmail.com>2012-12-09 11:29:08 +0100
commiteefe0852bcd4cba9a1af7ae1594bd7b6de1da71f (patch)
tree1463d367c5c1de4910add0c59e9f66a1f7cef9d3
parent01614b2bf42d3e0140b90b213b8168759f2113da (diff)
downloadsencha-lang-eefe0852bcd4cba9a1af7ae1594bd7b6de1da71f.tar.gz
sencha-lang-eefe0852bcd4cba9a1af7ae1594bd7b6de1da71f.tar.bz2
sencha-lang-eefe0852bcd4cba9a1af7ae1594bd7b6de1da71f.tar.xz
sencha-lang-eefe0852bcd4cba9a1af7ae1594bd7b6de1da71f.zip
Declaration stuff
-rw-r--r--Sencha-lang/AST/AllTypesOfASTNodes.h1
-rw-r--r--Sencha-lang/AST/Assignment.cpp48
-rw-r--r--Sencha-lang/AST/Assignment.h14
-rw-r--r--Sencha-lang/AST/DeclarationStatement.cpp61
-rw-r--r--Sencha-lang/AST/DeclarationStatement.h37
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp5
-rw-r--r--Sencha-lang/AST/PostfixExpression.h1
-rw-r--r--Sencha-lang/Context.h2
-rw-r--r--Sencha-lang/Debug/AST/AST.d9
-rw-r--r--Sencha-lang/Debug/AST/Assignment.d16
-rw-r--r--Sencha-lang/Debug/AST/DeclarationStatement.d20
-rw-r--r--Sencha-lang/Debug/AST/LogicalExpression.d13
-rw-r--r--Sencha-lang/Debug/AST/subdir.mk6
-rw-r--r--Sencha-lang/Debug/Parser.d7
-rwxr-xr-xSencha-lang/Debug/Sencha-langbin1207961 -> 1345963 bytes
-rw-r--r--Sencha-lang/Debug/main.d7
-rw-r--r--Sencha-lang/Parser.cpp120
17 files changed, 288 insertions, 79 deletions
diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h
index 60aa468..09f7597 100644
--- a/Sencha-lang/AST/AllTypesOfASTNodes.h
+++ b/Sencha-lang/AST/AllTypesOfASTNodes.h
@@ -18,6 +18,7 @@
#include "ConstantExpression.h"
#include "PostfixExpression.h"
#include "IncorrectExpression.h"
+#include "Assignment.h"
//And probably more
//TODO actualize it
diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp
index dd8215d..9925475 100644
--- a/Sencha-lang/AST/Assignment.cpp
+++ b/Sencha-lang/AST/Assignment.cpp
@@ -7,11 +7,55 @@
#include "Assignment.h"
-Assignment::Assignment() {
- // TODO Auto-generated constructor stub
+SenchaObject Assignment::evaluate()
+{
+ return static_cast<ASTExpression *>(children[1])->evaluate();
+}
+
+void Assignment::execute()
+{
+ auto left_value = static_cast<ASTExpression *>(children[0])->evaluate();
+ auto right_value = static_cast<ASTExpression *>(children[1])->evaluate();
+ if(left_value.name != "")
+ {
+ right_value.name = left_value.name;
+ context->set(left_value.name, right_value);
+ }
+ std::cout << right_value.repr() << std::endl;
+}
+
+void Assignment::add_lvalue(ASTExpression * left)
+{
+ if(children.size()==0)
+ children.push_back(left);
+ else
+ children[0] = left;
+}
+
+void Assignment::add_rvalue(ASTExpression * right)
+{
+ //TODO should note something if it doesn't look like that. Other possibilities like
+ if(children.size()==1)
+ children.push_back(right);
+ else if(children.size()>1)
+ children[1] = right;
}
+std::string Assignment::debug()
+{
+ std::string debug_note = static_cast<ASTExpression *>(children[0])->evaluate().repr();
+ debug_note += " = " + static_cast<ASTExpression *>(children[1])->evaluate().repr() + "\n";
+ return debug_note;
+}
+
+Assignment::Assignment(ASTNode * parent, Context * context)
+{
+ this->parent = parent;
+ this->context = context;
+}
+
+
Assignment::~Assignment() {
// TODO Auto-generated destructor stub
}
diff --git a/Sencha-lang/AST/Assignment.h b/Sencha-lang/AST/Assignment.h
index c503f6b..6ef3a48 100644
--- a/Sencha-lang/AST/Assignment.h
+++ b/Sencha-lang/AST/Assignment.h
@@ -7,10 +7,20 @@
#ifndef ASSIGNMENT_H_
#define ASSIGNMENT_H_
+#include <string>
+#include "ASTExpression.h"
+#include "../Context.h"
-class Assignment {
+class Assignment : public ASTExpression {
public:
- Assignment();
+ Context * context;
+
+ SenchaObject evaluate();
+ void execute();
+ void add_lvalue(ASTExpression *);
+ void add_rvalue(ASTExpression *);
+ virtual std::string debug() ;
+ Assignment(ASTNode * parent, Context * context);
virtual ~Assignment();
};
diff --git a/Sencha-lang/AST/DeclarationStatement.cpp b/Sencha-lang/AST/DeclarationStatement.cpp
new file mode 100644
index 0000000..241120a
--- /dev/null
+++ b/Sencha-lang/AST/DeclarationStatement.cpp
@@ -0,0 +1,61 @@
+/*
+ * DeclarationStatement.cpp
+ *
+ * Created on: Dec 9, 2012
+ * Author: attero
+ */
+
+#include "DeclarationStatement.h"
+
+
+
+DeclarationStatement::~DeclarationStatement() {
+ // TODO Auto-generated destructor stub
+}
+
+void DeclarationStatement::add_right_value(ASTExpression * right)
+{
+
+}
+
+DeclarationStatement::DeclarationStatement(ASTNode * parent, Context * context)
+{
+ is_function = false;
+ body = NULL;
+}
+
+std::string DeclarationStatement::debug()
+{
+ std::string debug_note = "Declaration";
+ return debug_note;
+}
+
+void DeclarationStatement::add_name(std::string name)
+{
+ this->name = name;
+}
+
+
+void DeclarationStatement::add_argument(std::string name)
+{
+ arguments.push_back(name);
+}
+
+void DeclarationStatement::execute()
+{
+ if(is_function)
+ {
+ //TODO implement
+ //do function registering stuff
+ }
+ else
+ {
+
+ }
+}
+
+void DeclarationStatement::add_body(ASTStatement * statement)
+{
+ is_function = true;
+ body = statement;
+}
diff --git a/Sencha-lang/AST/DeclarationStatement.h b/Sencha-lang/AST/DeclarationStatement.h
new file mode 100644
index 0000000..cbadafe
--- /dev/null
+++ b/Sencha-lang/AST/DeclarationStatement.h
@@ -0,0 +1,37 @@
+/*
+ * DeclarationStatement.h
+ *
+ * Created on: Dec 9, 2012
+ * Author: attero
+ */
+
+#ifndef DECLARATIONSTATEMENT_H_
+#define DECLARATIONSTATEMENT_H_
+
+#include "ASTStatement.h"
+#include "ASTExpression.h"
+
+#include "../Context.h"
+
+class DeclarationStatement: public ASTStatement {
+public:
+ std::string name;
+ SenchaObject right_value;
+ ASTStatement * body;
+ std::vector<std::string> arguments;
+ bool is_function;
+
+ void add_right_value(ASTExpression * right);
+ DeclarationStatement(ASTNode * parent, Context * context);
+ virtual std::string debug();
+ void add_name(std::string);
+
+
+ void add_argument(std::string name);
+ void add_body(ASTStatement * statement);
+
+ virtual void execute();
+ virtual ~DeclarationStatement();
+};
+
+#endif /* DECLARATIONSTATEMENT_H_ */
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 71611fb..dd0d27c 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -30,11 +30,6 @@ void PostfixExpression::add_argument(ASTExpression * expression)
arguments.push_back(expression);
}
-void PostfixExpression::set_body(ASTStatement * statement)
-{
- children.push_back(statement);
- body_set = true;
-}
SenchaObject PostfixExpression::evaluate()
{
diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h
index 4aa5974..124a0ad 100644
--- a/Sencha-lang/AST/PostfixExpression.h
+++ b/Sencha-lang/AST/PostfixExpression.h
@@ -18,7 +18,6 @@ public:
std::vector<ASTExpression *> arguments;
void set_head(ASTExpression * expression);
void add_argument(ASTExpression * expression);
- void set_body(ASTStatement * statement);
virtual SenchaObject evaluate();
virtual void execute();
diff --git a/Sencha-lang/Context.h b/Sencha-lang/Context.h
index 8b63286..be21e92 100644
--- a/Sencha-lang/Context.h
+++ b/Sencha-lang/Context.h
@@ -37,7 +37,7 @@ public:
ExecutionContext global_context;
- // std::map<FunctionIndex , ExecutionContext> function_contexts;
+ std::map<FunctionIndex , ExecutionContext *> function_contexts;
virtual ~Context();
};
diff --git a/Sencha-lang/Debug/AST/AST.d b/Sencha-lang/Debug/AST/AST.d
index 5ed72ff..83b0a1c 100644
--- a/Sencha-lang/Debug/AST/AST.d
+++ b/Sencha-lang/Debug/AST/AST.d
@@ -3,7 +3,8 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \
../AST/ProgramNode.h ../AST/ASTStatement.h ../AST/ASTExpression.h \
../AST/BasicStatement.h ../AST/BasicExpression.h \
../AST/ConstantExpression.h ../AST/PostfixExpression.h \
- ../AST/IncorrectExpression.h
+ ../AST/IncorrectExpression.h ../AST/Assignment.h ../AST/../Context.h \
+ ../AST/../AST/SenchaObject.h
../AST/AST.h:
@@ -30,3 +31,9 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \
../AST/PostfixExpression.h:
../AST/IncorrectExpression.h:
+
+../AST/Assignment.h:
+
+../AST/../Context.h:
+
+../AST/../AST/SenchaObject.h:
diff --git a/Sencha-lang/Debug/AST/Assignment.d b/Sencha-lang/Debug/AST/Assignment.d
index 692a84c..85772f8 100644
--- a/Sencha-lang/Debug/AST/Assignment.d
+++ b/Sencha-lang/Debug/AST/Assignment.d
@@ -1,3 +1,17 @@
-AST/Assignment.d: ../AST/Assignment.cpp ../AST/Assignment.h
+AST/Assignment.d: ../AST/Assignment.cpp ../AST/Assignment.h \
+ ../AST/ASTExpression.h ../AST/ASTNode.h ../AST/SenchaObject.h \
+ ../AST/to_string.h ../AST/../Context.h ../AST/../AST/SenchaObject.h
../AST/Assignment.h:
+
+../AST/ASTExpression.h:
+
+../AST/ASTNode.h:
+
+../AST/SenchaObject.h:
+
+../AST/to_string.h:
+
+../AST/../Context.h:
+
+../AST/../AST/SenchaObject.h:
diff --git a/Sencha-lang/Debug/AST/DeclarationStatement.d b/Sencha-lang/Debug/AST/DeclarationStatement.d
new file mode 100644
index 0000000..6c49f90
--- /dev/null
+++ b/Sencha-lang/Debug/AST/DeclarationStatement.d
@@ -0,0 +1,20 @@
+AST/DeclarationStatement.d: ../AST/DeclarationStatement.cpp \
+ ../AST/DeclarationStatement.h ../AST/ASTStatement.h ../AST/ASTNode.h \
+ ../AST/SenchaObject.h ../AST/to_string.h ../AST/ASTExpression.h \
+ ../AST/../Context.h ../AST/../AST/SenchaObject.h
+
+../AST/DeclarationStatement.h:
+
+../AST/ASTStatement.h:
+
+../AST/ASTNode.h:
+
+../AST/SenchaObject.h:
+
+../AST/to_string.h:
+
+../AST/ASTExpression.h:
+
+../AST/../Context.h:
+
+../AST/../AST/SenchaObject.h:
diff --git a/Sencha-lang/Debug/AST/LogicalExpression.d b/Sencha-lang/Debug/AST/LogicalExpression.d
new file mode 100644
index 0000000..d56b1fc
--- /dev/null
+++ b/Sencha-lang/Debug/AST/LogicalExpression.d
@@ -0,0 +1,13 @@
+AST/LogicalExpression.d: ../AST/LogicalExpression.cpp \
+ ../AST/LogicalExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
+ ../AST/SenchaObject.h ../AST/to_string.h
+
+../AST/LogicalExpression.h:
+
+../AST/ASTExpression.h:
+
+../AST/ASTNode.h:
+
+../AST/SenchaObject.h:
+
+../AST/to_string.h:
diff --git a/Sencha-lang/Debug/AST/subdir.mk b/Sencha-lang/Debug/AST/subdir.mk
index 353c216..4f9d09f 100644
--- a/Sencha-lang/Debug/AST/subdir.mk
+++ b/Sencha-lang/Debug/AST/subdir.mk
@@ -12,8 +12,10 @@ CPP_SRCS += \
../AST/BasicExpression.cpp \
../AST/BasicStatement.cpp \
../AST/ConstantExpression.cpp \
+../AST/DeclarationStatement.cpp \
../AST/IfNode.cpp \
../AST/IncorrectExpression.cpp \
+../AST/LogicalExpression.cpp \
../AST/PostfixExpression.cpp \
../AST/ProgramNode.cpp \
../AST/SenchaObject.cpp
@@ -27,8 +29,10 @@ OBJS += \
./AST/BasicExpression.o \
./AST/BasicStatement.o \
./AST/ConstantExpression.o \
+./AST/DeclarationStatement.o \
./AST/IfNode.o \
./AST/IncorrectExpression.o \
+./AST/LogicalExpression.o \
./AST/PostfixExpression.o \
./AST/ProgramNode.o \
./AST/SenchaObject.o
@@ -42,8 +46,10 @@ CPP_DEPS += \
./AST/BasicExpression.d \
./AST/BasicStatement.d \
./AST/ConstantExpression.d \
+./AST/DeclarationStatement.d \
./AST/IfNode.d \
./AST/IncorrectExpression.d \
+./AST/LogicalExpression.d \
./AST/PostfixExpression.d \
./AST/ProgramNode.d \
./AST/SenchaObject.d
diff --git a/Sencha-lang/Debug/Parser.d b/Sencha-lang/Debug/Parser.d
index b99bfd6..64827a4 100644
--- a/Sencha-lang/Debug/Parser.d
+++ b/Sencha-lang/Debug/Parser.d
@@ -3,7 +3,8 @@ Parser.d: ../Parser.cpp ../Parser.h ../Token.h ../Context.h \
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/ProgramNode.h \
../AST/ASTStatement.h ../AST/ASTExpression.h ../AST/BasicStatement.h \
../AST/BasicExpression.h ../AST/ConstantExpression.h \
- ../AST/PostfixExpression.h ../AST/IncorrectExpression.h ../AST/AST.h
+ ../AST/PostfixExpression.h ../AST/IncorrectExpression.h \
+ ../AST/Assignment.h ../AST/../Context.h ../AST/AST.h
../Parser.h:
@@ -37,4 +38,8 @@ Parser.d: ../Parser.cpp ../Parser.h ../Token.h ../Context.h \
../AST/IncorrectExpression.h:
+../AST/Assignment.h:
+
+../AST/../Context.h:
+
../AST/AST.h:
diff --git a/Sencha-lang/Debug/Sencha-lang b/Sencha-lang/Debug/Sencha-lang
index da85945..5ab001c 100755
--- a/Sencha-lang/Debug/Sencha-lang
+++ b/Sencha-lang/Debug/Sencha-lang
Binary files differ
diff --git a/Sencha-lang/Debug/main.d b/Sencha-lang/Debug/main.d
index 0057093..8248229 100644
--- a/Sencha-lang/Debug/main.d
+++ b/Sencha-lang/Debug/main.d
@@ -3,7 +3,8 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h ../Context.h \
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/ProgramNode.h \
../AST/ASTStatement.h ../AST/ASTExpression.h ../AST/BasicStatement.h \
../AST/BasicExpression.h ../AST/ConstantExpression.h \
- ../AST/PostfixExpression.h ../AST/IncorrectExpression.h ../AST/AST.h \
+ ../AST/PostfixExpression.h ../AST/IncorrectExpression.h \
+ ../AST/Assignment.h ../AST/../Context.h ../AST/AST.h \
../Tests/TestLexer.h ../Tests/TestSuite.h ../Tests/minunit.h \
../Tests/../Lexer.h
@@ -41,6 +42,10 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h ../Context.h \
../AST/IncorrectExpression.h:
+../AST/Assignment.h:
+
+../AST/../Context.h:
+
../AST/AST.h:
../Tests/TestLexer.h:
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 143cc1c..7f124eb 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -150,7 +150,6 @@ ASTStatement * Parser::statement(ASTNode * parent)
if(accept("="))
{
-
ASTExpression * ae = expr(stat);
context->add(identifier, ae->evaluate());
@@ -160,7 +159,6 @@ ASTStatement * Parser::statement(ASTNode * parent)
report(" := \n");
if(accept(";"))
{
-
report("Variable definition\n");
return stat;
}
@@ -247,49 +245,49 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
{
ConstantExpression * ce;
BasicExpression * be;
-//TODO add reference type to prims, syblol is now just a literal... and floats
- if(current_token.get_type() == t_integer)
- {
- report("Number: " + tok_value + "\n");
-
- ce = new ConstantExpression(expression, std::atoi(tok_value.c_str()));
- read_next();
- }
- else if(current_token.get_type() == t_literal)
- {
+ //TODO add reference type to prims, syblol is now just a literal... and floats
+ if(current_token.get_type() == t_integer)
+ {
+ report("Number: " + tok_value + "\n");
+
+ ce = new ConstantExpression(expression, std::atoi(tok_value.c_str()));
+ read_next();
+ }
+ else if(current_token.get_type() == t_literal)
+ {
- report("Character literal: " + tok_value + "\n");
- ce = new ConstantExpression(expression, tok_value);
- read_next();
- }
- else if(current_token.get_type() == t_symbol)
- {
- report("variable: " + tok_value + "\n");
- SenchaObject variable;
- //TODO is it right?
- string name = current_token.value;
- variable = context->get(name);
- variable.name = name;
- ce = new ConstantExpression(expression, variable, name);
- read_next();
- }
- else if(accept("("))
- {
- report("( ");
- be = static_cast<BasicExpression *>(expr(expression));
- report(" ) ");
- expect(")");
- return be;
- }
- else
- {
- string error_message = "ERROR: unexpected primary expression " + tok_value + "\n";
- error(error_message);
- read_next();
- return new IncorrectExpression(expression, error_message);
- }
+ report("Character literal: " + tok_value + "\n");
+ ce = new ConstantExpression(expression, tok_value);
+ read_next();
+ }
+ else if(current_token.get_type() == t_symbol)
+ {
+ report("variable: " + tok_value + "\n");
+ SenchaObject variable;
+ //TODO is it right?
+ string name = current_token.value;
+ variable = context->get(name);
+ variable.name = name;
+ ce = new ConstantExpression(expression, variable, name);
+ read_next();
+ }
+ else if(accept("("))
+ {
+ report("( ");
+ be = static_cast<BasicExpression *>(expr(expression));
+ report(" ) ");
+ expect(")");
+ return be;
+ }
+ else
+ {
+ string error_message = "ERROR: unexpected primary expression " + tok_value + "\n";
+ error(error_message);
+ read_next();
+ return new IncorrectExpression(expression, error_message);
+ }
- return ce;
+ return ce;
}
ASTExpression * Parser::postfix_expr(ASTNode * expression)
@@ -452,31 +450,25 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
ASTExpression * Parser::expr(ASTNode * expression)
{
-
- BasicExpression * be = new BasicExpression(expression);
- ASTExpression * left = eq_expr(be);
- SenchaObject left_value = left->evaluate();
- be->set_left_operand(left);
+ Assignment * assignment = new Assignment(expression, context);
+ ASTExpression * left = eq_expr(assignment);
+
if(accept("="))
{
- be->set_operator("=");
- ASTExpression * right = expr(be);
- be->set_right_operand(right);
- if(left_value.name != "")
- {
- context->set(left_value.name, right->evaluate());
- }
- report(" :=\n");
+ ASTExpression * right = expr(assignment);
+
+ assignment->add_lvalue(left);
+ assignment->add_rvalue(right);
+
+ report(" :=\n");
+ return assignment;
}
- if(be->oper == "")
+ else
{
- ASTExpression * ae;
- ae = static_cast<ASTExpression *>(be->children[0]);
- ae->parent = expression;
- delete be;
- return ae;
+ left->parent = expression;
+ delete assignment;
+ return left;
}
-
- return be;
+
}