diff options
-rw-r--r-- | Sencha-lang/AST/ASTExpression.h | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/AllTypesOfASTNodes.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/Assignment.cpp | 14 | ||||
-rw-r--r-- | Sencha-lang/AST/Assignment.h | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicExpression.cpp | 4 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicExpression.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/ConstantExpression.cpp | 13 | ||||
-rw-r--r-- | Sencha-lang/AST/ConstantExpression.h | 1 | ||||
-rw-r--r-- | Sencha-lang/AST/DeclarationStatement.cpp | 9 | ||||
-rw-r--r-- | Sencha-lang/AST/DeclarationStatement.h | 4 | ||||
-rw-r--r-- | Sencha-lang/AST/IncorrectExpression.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/PostfixExpression.h | 2 | ||||
-rw-r--r-- | Sencha-lang/Context.cpp | 12 | ||||
-rw-r--r-- | Sencha-lang/Debug/AST/AST.d | 4 | ||||
-rw-r--r-- | Sencha-lang/Debug/AST/DeclarationStatement.d | 5 | ||||
-rw-r--r-- | Sencha-lang/Debug/Parser.d | 5 | ||||
-rwxr-xr-x | Sencha-lang/Debug/Sencha-lang | bin | 1345963 -> 1375919 bytes | |||
-rw-r--r-- | Sencha-lang/Debug/main.d | 8 | ||||
-rw-r--r-- | Sencha-lang/Parser.cpp | 33 | ||||
-rw-r--r-- | Sencha-lang/main.cpp | 5 |
20 files changed, 88 insertions, 39 deletions
diff --git a/Sencha-lang/AST/ASTExpression.h b/Sencha-lang/AST/ASTExpression.h index eb56b19..13d1f48 100644 --- a/Sencha-lang/AST/ASTExpression.h +++ b/Sencha-lang/AST/ASTExpression.h @@ -15,6 +15,7 @@ public: virtual SenchaObject evaluate() = 0; virtual void execute() = 0; + virtual void execute_quietly() = 0; ASTExpression(); virtual ~ASTExpression(); }; diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h index 09f7597..4d29993 100644 --- a/Sencha-lang/AST/AllTypesOfASTNodes.h +++ b/Sencha-lang/AST/AllTypesOfASTNodes.h @@ -19,7 +19,7 @@ #include "PostfixExpression.h" #include "IncorrectExpression.h" #include "Assignment.h" - +#include "DeclarationStatement.h" //And probably more //TODO actualize it diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp index 9925475..05f5bc5 100644 --- a/Sencha-lang/AST/Assignment.cpp +++ b/Sencha-lang/AST/Assignment.cpp @@ -16,6 +16,7 @@ void Assignment::execute() { auto left_value = static_cast<ASTExpression *>(children[0])->evaluate(); auto right_value = static_cast<ASTExpression *>(children[1])->evaluate(); + static_cast<ASTExpression *>(children[1])->execute_quietly(); if(left_value.name != "") { right_value.name = left_value.name; @@ -24,6 +25,19 @@ void Assignment::execute() std::cout << right_value.repr() << std::endl; } +void Assignment::execute_quietly() +{ + auto left_value = static_cast<ASTExpression *>(children[0])->evaluate(); + auto right_value = static_cast<ASTExpression *>(children[1])->evaluate(); + static_cast<ASTExpression *>(children[1])->execute_quietly(); + if(left_value.name != "") + { + right_value.name = left_value.name; + context->set(left_value.name, right_value); + } + +} + void Assignment::add_lvalue(ASTExpression * left) { if(children.size()==0) diff --git a/Sencha-lang/AST/Assignment.h b/Sencha-lang/AST/Assignment.h index 6ef3a48..c851ccf 100644 --- a/Sencha-lang/AST/Assignment.h +++ b/Sencha-lang/AST/Assignment.h @@ -17,6 +17,7 @@ public: SenchaObject evaluate(); void execute(); + void execute_quietly(); void add_lvalue(ASTExpression *); void add_rvalue(ASTExpression *); virtual std::string debug() ; diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp index a6f9136..2201950 100644 --- a/Sencha-lang/AST/BasicExpression.cpp +++ b/Sencha-lang/AST/BasicExpression.cpp @@ -49,6 +49,10 @@ void BasicExpression::execute() std::cout << evaluate().repr() << std::endl; } +void BasicExpression::execute_quietly() +{ + evaluate(); +} SenchaObject BasicExpression::evaluate() { //TODO refactor it ;) diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h index 00dcac2..3984fb8 100644 --- a/Sencha-lang/AST/BasicExpression.h +++ b/Sencha-lang/AST/BasicExpression.h @@ -20,7 +20,7 @@ public: void set_right_operand(ASTNode * right); virtual SenchaObject evaluate(); virtual void execute(); - + virtual void execute_quietly(); std::string debug() ; diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp index 78937ca..e913bea 100644 --- a/Sencha-lang/AST/ConstantExpression.cpp +++ b/Sencha-lang/AST/ConstantExpression.cpp @@ -28,9 +28,16 @@ SenchaObject ConstantExpression::evaluate() return value; } -void ConstantExpression::execute() { - std::cout << evaluate().repr() << std::endl;//Do nothing - }; +void ConstantExpression::execute() +{ + std::cout << evaluate().repr() << std::endl;//Do nothing +} + +void ConstantExpression::execute_quietly() +{ + evaluate(); +} + ConstantExpression::ConstantExpression(ASTNode * parent, int number) { diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h index e02392e..ad28956 100644 --- a/Sencha-lang/AST/ConstantExpression.h +++ b/Sencha-lang/AST/ConstantExpression.h @@ -26,6 +26,7 @@ public: virtual SenchaObject evaluate(); virtual void execute() ; + virtual void execute_quietly() ; }; #endif /* CONSTANTEXPRESSION_H_ */ diff --git a/Sencha-lang/AST/DeclarationStatement.cpp b/Sencha-lang/AST/DeclarationStatement.cpp index 241120a..571c5e6 100644 --- a/Sencha-lang/AST/DeclarationStatement.cpp +++ b/Sencha-lang/AST/DeclarationStatement.cpp @@ -15,13 +15,17 @@ DeclarationStatement::~DeclarationStatement() { void DeclarationStatement::add_right_value(ASTExpression * right) { - + right_value = right->evaluate(); + children[0] = right; } DeclarationStatement::DeclarationStatement(ASTNode * parent, Context * context) { + this->context = context; is_function = false; body = NULL; + right_value = SenchaObject(); + children.push_back(new ConstantExpression(this)); } std::string DeclarationStatement::debug() @@ -50,7 +54,8 @@ void DeclarationStatement::execute() } else { - + context->add(name, right_value); + children[0]->execute() ; } } diff --git a/Sencha-lang/AST/DeclarationStatement.h b/Sencha-lang/AST/DeclarationStatement.h index cbadafe..573dc38 100644 --- a/Sencha-lang/AST/DeclarationStatement.h +++ b/Sencha-lang/AST/DeclarationStatement.h @@ -10,12 +10,14 @@ #include "ASTStatement.h" #include "ASTExpression.h" - +#include "ConstantExpression.h" #include "../Context.h" + class DeclarationStatement: public ASTStatement { public: std::string name; + Context * context; SenchaObject right_value; ASTStatement * body; std::vector<std::string> arguments; diff --git a/Sencha-lang/AST/IncorrectExpression.h b/Sencha-lang/AST/IncorrectExpression.h index 58c8abd..cba11ae 100644 --- a/Sencha-lang/AST/IncorrectExpression.h +++ b/Sencha-lang/AST/IncorrectExpression.h @@ -19,7 +19,9 @@ public: virtual SenchaObject evaluate() ; void execute() { std::cout << debug(); } + void execute_quietly() { //do nothing + } std::string debug() { return "Incorrect Expression:\n" + error_message; } IncorrectExpression(ASTNode * parent, std::string error_message); diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h index 124a0ad..1590123 100644 --- a/Sencha-lang/AST/PostfixExpression.h +++ b/Sencha-lang/AST/PostfixExpression.h @@ -21,6 +21,8 @@ public: virtual SenchaObject evaluate(); virtual void execute(); + virtual void execute_quietly(){//do nothing + }; std::string debug() ; diff --git a/Sencha-lang/Context.cpp b/Sencha-lang/Context.cpp index 36c7c43..24802f7 100644 --- a/Sencha-lang/Context.cpp +++ b/Sencha-lang/Context.cpp @@ -36,7 +36,15 @@ void Context::add(std::string name, SenchaObject object) void Context::set(std::string name, SenchaObject object) { - object_store[interpreter_context[name]] = object; + if(interpreter_context[name] != 0) + { + object_store[interpreter_context[name]] = object; + } + else + { + add(name, object); + + } } SenchaObject Context::get(std::string name) @@ -49,7 +57,7 @@ std::string Context::debug() { for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++) { - debug_note += "Context: " + to_string(index) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n"; + debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n"; } return debug_note; diff --git a/Sencha-lang/Debug/AST/AST.d b/Sencha-lang/Debug/AST/AST.d index 83b0a1c..2fe84fb 100644 --- a/Sencha-lang/Debug/AST/AST.d +++ b/Sencha-lang/Debug/AST/AST.d @@ -4,7 +4,7 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \ ../AST/BasicStatement.h ../AST/BasicExpression.h \ ../AST/ConstantExpression.h ../AST/PostfixExpression.h \ ../AST/IncorrectExpression.h ../AST/Assignment.h ../AST/../Context.h \ - ../AST/../AST/SenchaObject.h + ../AST/../AST/SenchaObject.h ../AST/DeclarationStatement.h ../AST/AST.h: @@ -37,3 +37,5 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \ ../AST/../Context.h: ../AST/../AST/SenchaObject.h: + +../AST/DeclarationStatement.h: diff --git a/Sencha-lang/Debug/AST/DeclarationStatement.d b/Sencha-lang/Debug/AST/DeclarationStatement.d index 6c49f90..38b7449 100644 --- a/Sencha-lang/Debug/AST/DeclarationStatement.d +++ b/Sencha-lang/Debug/AST/DeclarationStatement.d @@ -1,7 +1,8 @@ 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/ConstantExpression.h ../AST/../Context.h \ + ../AST/../AST/SenchaObject.h ../AST/DeclarationStatement.h: @@ -15,6 +16,8 @@ AST/DeclarationStatement.d: ../AST/DeclarationStatement.cpp \ ../AST/ASTExpression.h: +../AST/ConstantExpression.h: + ../AST/../Context.h: ../AST/../AST/SenchaObject.h: diff --git a/Sencha-lang/Debug/Parser.d b/Sencha-lang/Debug/Parser.d index 64827a4..121cbe6 100644 --- a/Sencha-lang/Debug/Parser.d +++ b/Sencha-lang/Debug/Parser.d @@ -4,7 +4,8 @@ Parser.d: ../Parser.cpp ../Parser.h ../Token.h ../Context.h \ ../AST/ASTStatement.h ../AST/ASTExpression.h ../AST/BasicStatement.h \ ../AST/BasicExpression.h ../AST/ConstantExpression.h \ ../AST/PostfixExpression.h ../AST/IncorrectExpression.h \ - ../AST/Assignment.h ../AST/../Context.h ../AST/AST.h + ../AST/Assignment.h ../AST/../Context.h ../AST/DeclarationStatement.h \ + ../AST/AST.h ../Parser.h: @@ -42,4 +43,6 @@ Parser.d: ../Parser.cpp ../Parser.h ../Token.h ../Context.h \ ../AST/../Context.h: +../AST/DeclarationStatement.h: + ../AST/AST.h: diff --git a/Sencha-lang/Debug/Sencha-lang b/Sencha-lang/Debug/Sencha-lang Binary files differindex 5ab001c..97b6255 100755 --- a/Sencha-lang/Debug/Sencha-lang +++ b/Sencha-lang/Debug/Sencha-lang diff --git a/Sencha-lang/Debug/main.d b/Sencha-lang/Debug/main.d index 8248229..cf23d64 100644 --- a/Sencha-lang/Debug/main.d +++ b/Sencha-lang/Debug/main.d @@ -4,9 +4,9 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h ../Context.h \ ../AST/ASTStatement.h ../AST/ASTExpression.h ../AST/BasicStatement.h \ ../AST/BasicExpression.h ../AST/ConstantExpression.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 + ../AST/Assignment.h ../AST/../Context.h ../AST/DeclarationStatement.h \ + ../AST/AST.h ../Tests/TestLexer.h ../Tests/TestSuite.h \ + ../Tests/minunit.h ../Tests/../Lexer.h ../Token.h: @@ -46,6 +46,8 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h ../Context.h \ ../AST/../Context.h: +../AST/DeclarationStatement.h: + ../AST/AST.h: ../Tests/TestLexer.h: diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index 7f124eb..d7ab86f 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -143,34 +143,26 @@ ASTStatement * Parser::statement(ASTNode * parent) else if(is_type()) - { + {
+ DeclarationStatement * declaration = new DeclarationStatement(parent, context); std::string identifier = tok_value;
report("Identifier: " + identifier + "\n");
read_next();
+ declaration->add_name(identifier);
+
if(accept("="))
{
ASTExpression * ae = expr(stat);
-
- context->add(identifier, ae->evaluate());
-
- stat->add_expression(ae);
+ declaration->add_right_value(ae);
report(" := \n");
- if(accept(";"))
- {
- report("Variable definition\n");
- return stat;
- }
+ accept(";");
}
-
if(accept(";"))
{
- context->add(identifier, SenchaObject());
- report("Variable definition\n");
- stat->add_expression(new ConstantExpression(stat, SenchaObject(), identifier));
- return stat;
+ report("Variable declaration\n");
}
if(expect("("))
@@ -181,6 +173,7 @@ ASTStatement * Parser::statement(ASTNode * parent) argc++;
is_type();
report("function argument: " + tok_value + "\n");
+ declaration->add_argument(tok_value);
read_next();
if(peek(")"))
{
@@ -193,17 +186,14 @@ ASTStatement * Parser::statement(ASTNode * parent) if(!accept(";"))
{
report("function body:\n");
- statement(stat);
+ declaration->add_body(statement(stat));
report("function definition\n");
}
- else
- {
- expect(";");
- report("function declaration\n");
- }
}
+ delete stat;
+ return declaration;
} else if(accept("if")) @@ -266,6 +256,7 @@ ASTExpression * Parser::prim_expr(ASTNode * expression) SenchaObject variable;
//TODO is it right?
string name = current_token.value;
+
variable = context->get(name);
variable.name = name;
ce = new ConstantExpression(expression, variable, name);
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index 7a8d031..9130c3f 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -98,13 +98,14 @@ void interactive() if(level_of_depth == 0) { parser.interpret(); + parser.program->execute_last(); + //cout << parser.report_message << endl; //cout << parser.error_message << endl; //cout << parser.show_tokens() << endl; //cout << "My tree:\n"; //cout << parser.program->debug(); - //cout << parser.context->debug(); - parser.program->execute_last(); + cout << parser.context->debug(); } } |