More on declarations :).

devel
Justyna Att Ilczuk 2012-12-09 12:57:51 +01:00
parent eefe0852bc
commit c2981eea75
20 changed files with 88 additions and 39 deletions

View File

@ -15,6 +15,7 @@ public:
virtual SenchaObject evaluate() = 0;
virtual void execute() = 0;
virtual void execute_quietly() = 0;
ASTExpression();
virtual ~ASTExpression();
};

View File

@ -19,7 +19,7 @@
#include "PostfixExpression.h"
#include "IncorrectExpression.h"
#include "Assignment.h"
#include "DeclarationStatement.h"
//And probably more
//TODO actualize it

View File

@ -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)

View File

@ -17,6 +17,7 @@ public:
SenchaObject evaluate();
void execute();
void execute_quietly();
void add_lvalue(ASTExpression *);
void add_rvalue(ASTExpression *);
virtual std::string debug() ;

View File

@ -49,6 +49,10 @@ void BasicExpression::execute()
std::cout << evaluate().repr() << std::endl;
}
void BasicExpression::execute_quietly()
{
evaluate();
}
SenchaObject BasicExpression::evaluate()
{
//TODO refactor it ;)

View File

@ -20,7 +20,7 @@ public:
void set_right_operand(ASTNode * right);
virtual SenchaObject evaluate();
virtual void execute();
virtual void execute_quietly();
std::string debug() ;

View File

@ -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)
{

View File

@ -26,6 +26,7 @@ public:
virtual SenchaObject evaluate();
virtual void execute() ;
virtual void execute_quietly() ;
};
#endif /* CONSTANTEXPRESSION_H_ */

View File

@ -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() ;
}
}

View File

@ -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;

View File

@ -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);

View File

@ -21,6 +21,8 @@ public:
virtual SenchaObject evaluate();
virtual void execute();
virtual void execute_quietly(){//do nothing
};
std::string debug() ;

View File

@ -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;

View File

@ -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:

View File

@ -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:

View File

@ -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:

Binary file not shown.

View File

@ -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:

View File

@ -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);

View File

@ -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();
}
}