Merge branch 'devel' of hackerspace.pl:attero/sencha-lang into devel
commit
8f8e54524e
|
@ -14,6 +14,11 @@ AST::AST() {
|
|||
level_of_depth = 0;
|
||||
}
|
||||
|
||||
void AST::delete_all_children()
|
||||
{
|
||||
//TODO perform deleting
|
||||
}
|
||||
|
||||
AST::~AST() {
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ public:
|
|||
ASTNode * current_node;
|
||||
int number_of_nodes;
|
||||
int level_of_depth;
|
||||
void delete_all_children();
|
||||
ASTNode * add_node(ASTNode * node);
|
||||
virtual ~AST();
|
||||
};
|
||||
|
|
|
@ -14,6 +14,7 @@ class ASTExpression : public ASTNode {
|
|||
public:
|
||||
|
||||
virtual SenchaObject evaluate() = 0;
|
||||
virtual void execute() = 0;
|
||||
ASTExpression();
|
||||
virtual ~ASTExpression();
|
||||
};
|
||||
|
|
|
@ -42,16 +42,34 @@ void BasicExpression::set_right_operand(ASTNode * right)
|
|||
if(children.size() == 2)
|
||||
children_set = true;
|
||||
}
|
||||
void BasicExpression::execute()
|
||||
{
|
||||
children[0]->execute();
|
||||
children[1]->execute();
|
||||
std::cout << evaluate().repr() << std::endl;
|
||||
}
|
||||
|
||||
SenchaObject BasicExpression::evaluate()
|
||||
{
|
||||
|
||||
//TODO refactor it ;)
|
||||
SenchaObject so;
|
||||
if(oper == "+")
|
||||
{
|
||||
return SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate());
|
||||
so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate());
|
||||
}
|
||||
|
||||
return SenchaObject();
|
||||
else if(oper == "-")
|
||||
{
|
||||
so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() - static_cast<ASTExpression *>(children[1])->evaluate());
|
||||
}
|
||||
else if(oper == "*")
|
||||
{
|
||||
so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() * static_cast<ASTExpression *>(children[1])->evaluate());
|
||||
}
|
||||
else if(oper == "/")
|
||||
{
|
||||
so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() / static_cast<ASTExpression *>(children[1])->evaluate());
|
||||
}
|
||||
return so;
|
||||
}
|
||||
|
||||
BasicExpression::BasicExpression(ASTNode * parent) {
|
||||
|
@ -70,6 +88,7 @@ std::string BasicExpression::debug()
|
|||
std::string debug_note = "Basic expression:\n";
|
||||
debug_note += "left operand: \n";
|
||||
debug_note += this->children[0]->debug();
|
||||
debug_note += "\nright operand:\n" + this->children[1]->debug() +"\n###\n";
|
||||
debug_note += "\nright operand:\n" + this->children[1]->debug();
|
||||
debug_note += "\nOperator: " + this->oper +"\n###\n";
|
||||
return debug_note;
|
||||
}
|
||||
|
|
|
@ -8,12 +8,9 @@
|
|||
#ifndef BASICEXPRESSION_H_
|
||||
#define BASICEXPRESSION_H_
|
||||
#include "ASTExpression.h"
|
||||
|
||||
#include <iostream>
|
||||
class BasicExpression : public ASTExpression {
|
||||
public:
|
||||
//TODO change oper to enum
|
||||
|
||||
|
||||
|
||||
std::string oper;
|
||||
|
||||
|
@ -22,7 +19,8 @@ public:
|
|||
void set_left_operand(ASTNode * left);
|
||||
void set_right_operand(ASTNode * right);
|
||||
virtual SenchaObject evaluate();
|
||||
virtual void execute() { evaluate(); };
|
||||
virtual void execute();
|
||||
|
||||
|
||||
|
||||
std::string debug() ;
|
||||
|
|
|
@ -28,5 +28,5 @@ std::string BasicStatement::debug()
|
|||
|
||||
void BasicStatement::execute()
|
||||
{
|
||||
std::cout << children[0]->debug() << std::endl ;
|
||||
children[0]->execute() ;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,12 @@ ConstantExpression::~ConstantExpression() {
|
|||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value)
|
||||
{
|
||||
this->parent = parent;
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
SenchaObject ConstantExpression::evaluate()
|
||||
{
|
||||
return value;
|
||||
|
|
|
@ -15,7 +15,7 @@ public:
|
|||
ConstantExpression(ASTNode * parent, int number) ;
|
||||
ConstantExpression(ASTNode * parent, double number) ;
|
||||
ConstantExpression(ASTNode * parent, std::string text);
|
||||
|
||||
ConstantExpression(ASTNode * parent, SenchaObject value);
|
||||
SenchaObject value;
|
||||
|
||||
std::string debug();
|
||||
|
|
|
@ -7,8 +7,11 @@
|
|||
|
||||
#include "PostfixExpression.h"
|
||||
|
||||
PostfixExpression::PostfixExpression() {
|
||||
PostfixExpression::PostfixExpression(ASTNode * parent) {
|
||||
// TODO Auto-generated constructor stub
|
||||
this->parent = parent;
|
||||
head_set = false;
|
||||
body_set = false;
|
||||
|
||||
}
|
||||
|
||||
|
@ -16,3 +19,34 @@ PostfixExpression::~PostfixExpression() {
|
|||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
|
||||
void PostfixExpression::set_head(ASTExpression * expression)
|
||||
{
|
||||
//How should it look like?
|
||||
head_set = true;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
return SenchaObject();
|
||||
}
|
||||
|
||||
void PostfixExpression::execute() { evaluate(); }
|
||||
|
||||
|
||||
|
||||
std::string PostfixExpression::debug()
|
||||
{
|
||||
return "Postfix expression tadadah!";
|
||||
}
|
||||
|
|
|
@ -8,10 +8,25 @@
|
|||
#ifndef POSTFIXEXPRESSION_H_
|
||||
#define POSTFIXEXPRESSION_H_
|
||||
#include "ASTExpression.h"
|
||||
#include "ASTStatement.h"
|
||||
|
||||
class PostfixExpression : public ASTExpression {
|
||||
public:
|
||||
PostfixExpression();
|
||||
|
||||
bool body_set;
|
||||
bool head_set;
|
||||
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();
|
||||
|
||||
|
||||
std::string debug() ;
|
||||
|
||||
PostfixExpression(ASTNode * parent);
|
||||
virtual ~PostfixExpression();
|
||||
};
|
||||
|
||||
|
|
|
@ -63,7 +63,6 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
|
|||
result.set_value(this->number + right.number);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -73,3 +72,80 @@ SenchaObject SenchaObject::operator+(const SenchaObject& right)const
|
|||
return result;
|
||||
|
||||
}
|
||||
|
||||
SenchaObject SenchaObject::operator-(const SenchaObject& right)const
|
||||
{
|
||||
SenchaObject result;
|
||||
if(type == right.type)
|
||||
{
|
||||
switch(type){
|
||||
case string_literal:
|
||||
result.type = invalid;
|
||||
break;
|
||||
case integer_number:
|
||||
result.set_value(this->integer - right.integer);
|
||||
break;
|
||||
case float_number:
|
||||
result.set_value(this->number - right.number);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.type = invalid;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SenchaObject SenchaObject::operator*(const SenchaObject& right)const
|
||||
{
|
||||
SenchaObject result;
|
||||
if(type == right.type)
|
||||
{
|
||||
switch(type){
|
||||
case string_literal:
|
||||
result.type = invalid;
|
||||
break;
|
||||
case integer_number:
|
||||
result.set_value(this->integer * right.integer);
|
||||
break;
|
||||
case float_number:
|
||||
result.set_value(this->number * right.number);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.type = invalid;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
SenchaObject SenchaObject::operator/(const SenchaObject& right)const
|
||||
{
|
||||
SenchaObject result;
|
||||
if(type == right.type)
|
||||
{
|
||||
switch(type){
|
||||
case string_literal:
|
||||
result.type = invalid;
|
||||
break;
|
||||
case integer_number:
|
||||
result.set_value(this->integer / right.integer);
|
||||
break;
|
||||
case float_number:
|
||||
result.set_value(this->number / right.number);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result.type = invalid;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//TODO change code above to something more generic
|
||||
|
|
|
@ -62,8 +62,10 @@ public:
|
|||
//TODO overload operators as it should be done
|
||||
|
||||
virtual SenchaObject operator+(const SenchaObject& right)const;
|
||||
virtual SenchaObject operator-(const SenchaObject& right)const;
|
||||
virtual SenchaObject operator*(const SenchaObject& right)const;
|
||||
virtual SenchaObject operator/(const SenchaObject& right)const;
|
||||
|
||||
virtual SenchaObject operator-(const SenchaObject& right)const { return SenchaObject(); };
|
||||
virtual ~SenchaObject();
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
AST/PostfixExpression.d: ../AST/PostfixExpression.cpp \
|
||||
../AST/PostfixExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
|
||||
../AST/SenchaObject.h ../AST/to_string.h
|
||||
../AST/SenchaObject.h ../AST/to_string.h ../AST/ASTStatement.h
|
||||
|
||||
../AST/PostfixExpression.h:
|
||||
|
||||
|
@ -11,3 +11,5 @@ AST/PostfixExpression.d: ../AST/PostfixExpression.cpp \
|
|||
../AST/SenchaObject.h:
|
||||
|
||||
../AST/to_string.h:
|
||||
|
||||
../AST/ASTStatement.h:
|
||||
|
|
Binary file not shown.
|
@ -15,6 +15,18 @@ Parser::~Parser()
|
|||
//dtor
|
||||
}
|
||||
|
||||
void Parser::erase_all()
|
||||
{
|
||||
tree.delete_all_children();
|
||||
tree.root = new ProgramNode();
|
||||
error_message = "***ERRORS DURING PARSING***\n";
|
||||
report_message = "***PARSER REPORT***\n";
|
||||
position_in_stream = 0;
|
||||
in_statement = false;
|
||||
program = static_cast<ProgramNode *>(tree.root);
|
||||
|
||||
|
||||
}
|
||||
string Parser::show_tokens()
|
||||
{
|
||||
string tokens = "";
|
||||
|
@ -181,8 +193,6 @@ ASTStatement * Parser::statement(ASTNode * parent)
|
|||
report("function declaration\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
else if(accept("if"))
|
||||
|
@ -258,7 +268,8 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
|
|||
error(error_message);
|
||||
read_next();
|
||||
return new IncorrectExpression(expression, error_message);
|
||||
}
|
||||
}
|
||||
|
||||
return ce;
|
||||
}
|
||||
|
||||
|
@ -290,6 +301,8 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression)
|
|||
}
|
||||
report("FUNC_CALL\n");
|
||||
}
|
||||
|
||||
|
||||
//TODO implement postfix_expression
|
||||
return be;
|
||||
}
|
||||
|
@ -297,8 +310,8 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression)
|
|||
ASTExpression * Parser::mul_expr(ASTNode * expression)
|
||||
{
|
||||
BasicExpression * be = new BasicExpression(expression);
|
||||
be->set_left_operand(postfix_expr(be));
|
||||
while(peek("*") || peek("/"))
|
||||
be->set_left_operand(prim_expr(be));
|
||||
if(peek("*") || peek("/"))
|
||||
{
|
||||
if(accept("*"))
|
||||
{
|
||||
|
@ -309,8 +322,18 @@ ASTExpression * Parser::mul_expr(ASTNode * expression)
|
|||
be->set_operator("/");
|
||||
report(" /\n");
|
||||
}
|
||||
be->set_right_operand(postfix_expr(be));
|
||||
be->set_right_operand(mul_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
ae->parent = expression;
|
||||
delete be;
|
||||
return ae;
|
||||
}
|
||||
|
||||
return be;
|
||||
}
|
||||
|
||||
|
@ -318,7 +341,7 @@ ASTExpression * Parser::add_expr(ASTNode * expression)
|
|||
{
|
||||
BasicExpression * be = new BasicExpression(expression);
|
||||
be->set_left_operand(mul_expr(be));
|
||||
while(peek("+") || peek("-"))
|
||||
if(peek("+") || peek("-"))
|
||||
{
|
||||
if(accept("+"))
|
||||
{
|
||||
|
@ -330,8 +353,18 @@ ASTExpression * Parser::add_expr(ASTNode * expression)
|
|||
report(" -\n");
|
||||
be->set_operator("-");
|
||||
}
|
||||
be->set_right_operand(mul_expr(be));
|
||||
be->set_right_operand(add_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
ae->parent = expression;
|
||||
delete be;
|
||||
return ae;
|
||||
}
|
||||
|
||||
return be;
|
||||
}
|
||||
|
||||
|
@ -339,7 +372,7 @@ ASTExpression * Parser::rel_expr(ASTNode * expression)
|
|||
{
|
||||
BasicExpression * be = new BasicExpression(expression);
|
||||
be->set_left_operand(add_expr(be));
|
||||
while(peek("<") || peek(">"))
|
||||
if(peek("<") || peek(">"))
|
||||
{
|
||||
if(accept("<"))
|
||||
{
|
||||
|
@ -351,8 +384,19 @@ ASTExpression * Parser::rel_expr(ASTNode * expression)
|
|||
be->set_operator(">");
|
||||
report(" >\n");
|
||||
}
|
||||
be->set_right_operand(add_expr(be));
|
||||
be->set_right_operand(rel_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
ae->parent = expression;
|
||||
delete be;
|
||||
return ae;
|
||||
}
|
||||
|
||||
|
||||
return be;
|
||||
}
|
||||
|
||||
|
@ -360,7 +404,7 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
|
|||
{
|
||||
BasicExpression * be = new BasicExpression(expression);
|
||||
be->set_left_operand(rel_expr(be));
|
||||
while(peek("==") || peek("!="))
|
||||
if(peek("==") || peek("!="))
|
||||
{
|
||||
if(accept("=="))
|
||||
{
|
||||
|
@ -372,8 +416,18 @@ ASTExpression * Parser::eq_expr(ASTNode * expression)
|
|||
be->set_operator("!=");
|
||||
report("!=\n");
|
||||
}
|
||||
be->set_right_operand(rel_expr(be));
|
||||
be->set_right_operand(eq_expr(be));
|
||||
}
|
||||
|
||||
if(be->oper == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
ae->parent = expression;
|
||||
delete be;
|
||||
return ae;
|
||||
}
|
||||
|
||||
return be;
|
||||
}
|
||||
|
||||
|
@ -387,6 +441,15 @@ ASTExpression * Parser::expr(ASTNode * expression)
|
|||
be->set_right_operand(expr(be));
|
||||
report(" :=\n");
|
||||
}
|
||||
//return be;
|
||||
return new ConstantExpression(expression, 3);
|
||||
|
||||
if(be->oper == "")
|
||||
{
|
||||
ASTExpression * ae;
|
||||
ae = static_cast<ASTExpression *>(be->children[0]);
|
||||
ae->parent = expression;
|
||||
delete be;
|
||||
return ae;
|
||||
}
|
||||
|
||||
return be;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,9 @@ class Parser
|
|||
string error_message;
|
||||
void add_tokens(vector<Token> tokens);
|
||||
string show_tokens();
|
||||
AST tree;
|
||||
ProgramNode * program;
|
||||
void erase_all();
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -27,8 +30,8 @@ class Parser
|
|||
string tok_value;
|
||||
vector<Token> token_stream;
|
||||
int position_in_stream;
|
||||
AST tree;
|
||||
ProgramNode * program;
|
||||
|
||||
|
||||
|
||||
bool in_statement;
|
||||
bool read_next();
|
||||
|
|
Binary file not shown.
|
@ -97,6 +97,10 @@ void interactive()
|
|||
cout << parser.report_message << endl;
|
||||
cout << parser.error_message << endl;
|
||||
cout << parser.show_tokens() << endl;
|
||||
cout << "My tree:\n";
|
||||
//cout << parser.program->debug();
|
||||
parser.program->execute();
|
||||
cout << "Tadah!" << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in New Issue