It's alive!

devel
Justyna Att Ilczuk 2012-12-06 18:41:16 +01:00
parent d996b89678
commit 02d96d9891
31 changed files with 420 additions and 92 deletions

View File

@ -8,7 +8,7 @@
#include "ASTExpression.h"
ASTExpression::ASTExpression(ASTNode * parent) {
ASTExpression::ASTExpression() {
this->parent = parent;
}

View File

@ -14,7 +14,7 @@ class ASTExpression : public ASTNode {
public:
virtual SenchaObject evaluate() = 0;
ASTExpression(ASTNode * parent);
ASTExpression();
virtual ~ASTExpression();
};

View File

@ -12,6 +12,7 @@
class ASTStatement : public ASTNode {
public:
ASTStatement();
virtual void execute() = 0;
virtual ~ASTStatement();
};

View File

@ -17,6 +17,7 @@
#include "BasicExpression.h"
#include "ConstantExpression.h"
#include "PostfixExpression.h"
#include "IncorrectExpression.h"
//And probably more
//TODO actualize it

View File

@ -14,33 +14,49 @@ void BasicExpression::set_operator(std::string op)
void BasicExpression::set_left_operand(ASTNode * left)
{
if(!this->children_set)
{
children.push_back(left);
}
else
{
this->children[0] = left;
this->left = static_cast<ASTExpression *>(children[0]);
}
if(children.size() == 2)
children_set = true;
}
void BasicExpression::set_right_operand(ASTNode * right)
{
this->children[1] = right;
this->right = static_cast<ASTExpression *>(children[1]);
if(!this->children_set)
{
children.push_back(right);
}
else
{
this->children[1] = right;
}
if(children.size() == 2)
children_set = true;
}
virtual SenchaObject BasicExpression::evaluate()
SenchaObject BasicExpression::evaluate()
{
if(oper == "+")
{
return left->evaluate() + right->evaluate();
return SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate());
}
return 0;
return SenchaObject();
}
BasicExpression::BasicExpression(ASTNode * parent) {
this->parent = parent;
// initialize pointers
this->add_children(0);
this->add_children(0);
left = static_cast<ASTExpression *>(children[0]);
right = static_cast<ASTExpression *>(children[1]);
children_set = false;
}
BasicExpression::~BasicExpression() {
@ -48,3 +64,12 @@ BasicExpression::~BasicExpression() {
// TODO free children memory
}
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";
return debug_note;
}

View File

@ -13,21 +13,19 @@ class BasicExpression : public ASTExpression {
public:
//TODO change oper to enum
std::string all_operators[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!=", "&&", "||"};
std::string oper;
ASTExpression * left;
ASTExpression * right;
bool children_set;
void set_operator(std::string op);
void set_left_operand(ASTNode * left);
void set_right_operand(ASTNode * right);
virtual SenchaObject evaluate();
virtual void execute() { evaluate(); };
virtual std::string debug();
std::string debug() { return "Basic expression:\n" + "left operand: \n" +
left->debug() + "\nright operand:\n" + right->debug +"\n###\n"; }
std::string debug() ;
BasicExpression(ASTNode * parent);
virtual ~BasicExpression();

View File

@ -18,7 +18,7 @@ BasicStatement::~BasicStatement() {
void BasicStatement::add_expression(ASTExpression * expr)
{
this->add_children(expr);
children.push_back(expr);
}
std::string BasicStatement::debug()

View File

@ -8,8 +8,10 @@
#ifndef BASICSTATEMENT_H_
#define BASICSTATEMENT_H_
#include <iostream>
#include "ASTExpression.h"
#include "ASTStatement.h"
class BasicStatement : public ASTStatement{
class BasicStatement : public ASTStatement {
public:
BasicStatement(ASTNode * parent);
virtual std::string debug();

View File

@ -17,7 +17,25 @@ ConstantExpression::~ConstantExpression() {
// TODO Auto-generated destructor stub
}
virtual SenchaObject ConstantExpression::evaluate()
SenchaObject ConstantExpression::evaluate()
{
return value;
}
ConstantExpression::ConstantExpression(ASTNode * parent, int number)
{
this->parent = parent; value = SenchaObject(number);
}
ConstantExpression::ConstantExpression(ASTNode * parent, double number)
{
this->parent = parent;
value = SenchaObject(number);
}
ConstantExpression::ConstantExpression(ASTNode * parent, std::string text)
{
this->parent = parent; value = SenchaObject(text);
}
std::string ConstantExpression::debug() { return "Constant expression:\n" + value.repr() + "\n"; }

View File

@ -12,13 +12,16 @@
class ConstantExpression : public ASTExpression {
public:
ConstantExpression(ASTNode * parent);
ConstantExpression(ASTNode * parent, int number) { this->parent = parent; value = SenchaObject(number);};
ConstantExpression(ASTNode * parent, double number) {this->parent = parent; value = SenchaObject(number); };
ConstantExpression(std::string text){ this->parent = parent; value = SenchaObject(text);};
ConstantExpression(ASTNode * parent, int number) ;
ConstantExpression(ASTNode * parent, double number) ;
ConstantExpression(ASTNode * parent, std::string text);
SenchaObject value;
std::string debug() { return "Constant expression:\n" + value.repr() + "\n"; }
std::string debug();
virtual void execute() { //Do nothing
};
virtual ~ConstantExpression();
virtual SenchaObject evaluate();

View File

@ -0,0 +1,24 @@
/*
* IncorrectExpression.cpp
*
* Created on: Dec 6, 2012
* Author: attero
*/
#include "IncorrectExpression.h"
IncorrectExpression::IncorrectExpression(ASTNode * parent, std::string error_message)
{
this->parent = parent;
this->error_message = error_message;
}
IncorrectExpression::~IncorrectExpression() {
// TODO Auto-generated destructor stub
}
SenchaObject IncorrectExpression::evaluate()
{
SenchaObject null_object;
return null_object;
}

View File

@ -0,0 +1,29 @@
/*
* IncorrectExpression.h
*
* Created on: Dec 6, 2012
* Author: attero
*/
#ifndef INCORRECTEXPRESSION_H_
#define INCORRECTEXPRESSION_H_
#include "ASTExpression.h"
#include <iostream>
class IncorrectExpression: public ASTExpression {
public:
std::string error_message;
virtual SenchaObject evaluate() ;
void execute() { std::cout << debug(); }
std::string debug() { return "Incorrect Expression:\n" + error_message; }
IncorrectExpression(ASTNode * parent, std::string error_message);
virtual ~IncorrectExpression();
};
#endif /* INCORRECTEXPRESSION_H_ */

View File

@ -56,7 +56,7 @@ public:
SenchaObject();
SenchaObject(int integer) { set_value(integer); }
SenchaObject(float number) { set_value(number); }
SenchaObject(double number) { set_value(number); }
SenchaObject(std::string text) { set_value(text); }
//TODO overload operators as it should be done

View File

@ -1,11 +1,18 @@
AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \
../AST/AllTypesOfASTNodes.h ../AST/ProgramNode.h ../AST/ASTStatement.h \
../AST/ASTExpression.h ../AST/ASTPrimary.h
../AST/SenchaObject.h ../AST/to_string.h ../AST/AllTypesOfASTNodes.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/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:
../AST/AllTypesOfASTNodes.h:
../AST/ProgramNode.h:
@ -14,4 +21,12 @@ AST/AST.d: ../AST/AST.cpp ../AST/AST.h ../AST/ASTNode.h \
../AST/ASTExpression.h:
../AST/ASTPrimary.h:
../AST/BasicStatement.h:
../AST/BasicExpression.h:
../AST/ConstantExpression.h:
../AST/PostfixExpression.h:
../AST/IncorrectExpression.h:

View File

@ -1,6 +1,10 @@
AST/ASTExpression.d: ../AST/ASTExpression.cpp ../AST/ASTExpression.h \
../AST/ASTNode.h
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -1,3 +1,8 @@
AST/ASTNode.d: ../AST/ASTNode.cpp ../AST/ASTNode.h
AST/ASTNode.d: ../AST/ASTNode.cpp ../AST/ASTNode.h ../AST/SenchaObject.h \
../AST/to_string.h
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -1,6 +1,10 @@
AST/ASTStatement.d: ../AST/ASTStatement.cpp ../AST/ASTStatement.h \
../AST/ASTNode.h
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h
../AST/ASTStatement.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -0,0 +1,13 @@
AST/BasicExpression.d: ../AST/BasicExpression.cpp \
../AST/BasicExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
../AST/SenchaObject.h ../AST/to_string.h
../AST/BasicExpression.h:
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -0,0 +1,15 @@
AST/BasicStatement.d: ../AST/BasicStatement.cpp ../AST/BasicStatement.h \
../AST/ASTExpression.h ../AST/ASTNode.h ../AST/SenchaObject.h \
../AST/to_string.h ../AST/ASTStatement.h
../AST/BasicStatement.h:
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:
../AST/ASTStatement.h:

View File

@ -0,0 +1,13 @@
AST/ConstantExpression.d: ../AST/ConstantExpression.cpp \
../AST/ConstantExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
../AST/SenchaObject.h ../AST/to_string.h
../AST/ConstantExpression.h:
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -1,5 +1,6 @@
AST/IfNode.d: ../AST/IfNode.cpp ../AST/IfNode.h ../AST/ASTStatement.h \
../AST/ASTNode.h ../AST/ASTExpression.h
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h \
../AST/ASTExpression.h
../AST/IfNode.h:
@ -7,4 +8,8 @@ AST/IfNode.d: ../AST/IfNode.cpp ../AST/IfNode.h ../AST/ASTStatement.h \
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:
../AST/ASTExpression.h:

View File

@ -0,0 +1,13 @@
AST/IncorrectExpression.d: ../AST/IncorrectExpression.cpp \
../AST/IncorrectExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
../AST/SenchaObject.h ../AST/to_string.h
../AST/IncorrectExpression.h:
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -0,0 +1,13 @@
AST/PostfixExpression.d: ../AST/PostfixExpression.cpp \
../AST/PostfixExpression.h ../AST/ASTExpression.h ../AST/ASTNode.h \
../AST/SenchaObject.h ../AST/to_string.h
../AST/PostfixExpression.h:
../AST/ASTExpression.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -1,8 +1,13 @@
AST/ProgramNode.d: ../AST/ProgramNode.cpp ../AST/ProgramNode.h \
../AST/ASTNode.h ../AST/ASTStatement.h
../AST/ASTNode.h ../AST/SenchaObject.h ../AST/to_string.h \
../AST/ASTStatement.h
../AST/ProgramNode.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.h:
../AST/ASTStatement.h:

View File

@ -0,0 +1,6 @@
AST/SenchaObject.d: ../AST/SenchaObject.cpp ../AST/SenchaObject.h \
../AST/to_string.h
../AST/SenchaObject.h:
../AST/to_string.h:

View File

@ -7,28 +7,43 @@ CPP_SRCS += \
../AST/AST.cpp \
../AST/ASTExpression.cpp \
../AST/ASTNode.cpp \
../AST/ASTPrimary.cpp \
../AST/ASTStatement.cpp \
../AST/BasicExpression.cpp \
../AST/BasicStatement.cpp \
../AST/ConstantExpression.cpp \
../AST/IfNode.cpp \
../AST/ProgramNode.cpp
../AST/IncorrectExpression.cpp \
../AST/PostfixExpression.cpp \
../AST/ProgramNode.cpp \
../AST/SenchaObject.cpp
OBJS += \
./AST/AST.o \
./AST/ASTExpression.o \
./AST/ASTNode.o \
./AST/ASTPrimary.o \
./AST/ASTStatement.o \
./AST/BasicExpression.o \
./AST/BasicStatement.o \
./AST/ConstantExpression.o \
./AST/IfNode.o \
./AST/ProgramNode.o
./AST/IncorrectExpression.o \
./AST/PostfixExpression.o \
./AST/ProgramNode.o \
./AST/SenchaObject.o
CPP_DEPS += \
./AST/AST.d \
./AST/ASTExpression.d \
./AST/ASTNode.d \
./AST/ASTPrimary.d \
./AST/ASTStatement.d \
./AST/BasicExpression.d \
./AST/BasicStatement.d \
./AST/ConstantExpression.d \
./AST/IfNode.d \
./AST/ProgramNode.d
./AST/IncorrectExpression.d \
./AST/PostfixExpression.d \
./AST/ProgramNode.d \
./AST/SenchaObject.d
# Each subdirectory must supply rules for building sources it contributes

View File

@ -1,5 +1,36 @@
Parser.d: ../Parser.cpp ../Parser.h ../Token.h
Parser.d: ../Parser.cpp ../Parser.h ../Token.h \
../AST/AllTypesOfASTNodes.h ../AST/ASTNode.h ../AST/SenchaObject.h \
../AST/to_string.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
../Parser.h:
../Token.h:
../AST/AllTypesOfASTNodes.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.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:

Binary file not shown.

View File

@ -1,6 +1,10 @@
main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h \
../Tests/TestLexer.h ../Tests/TestSuite.h ../Tests/minunit.h \
../Tests/../Lexer.h
../AST/AllTypesOfASTNodes.h ../AST/ASTNode.h ../AST/SenchaObject.h \
../AST/to_string.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 ../Tests/TestLexer.h \
../Tests/TestSuite.h ../Tests/minunit.h ../Tests/../Lexer.h
../Token.h:
@ -8,6 +12,32 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h \
../Parser.h:
../AST/AllTypesOfASTNodes.h:
../AST/ASTNode.h:
../AST/SenchaObject.h:
../AST/to_string.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:
../Tests/TestLexer.h:
../Tests/TestSuite.h:

View File

@ -117,13 +117,13 @@ bool Parser::is_type()
ASTStatement * Parser::statement(ASTNode * parent)
{
BasicStatement * stat(parent);
BasicStatement * stat = new BasicStatement(parent);
if(accept("{"))
{
while(!accept("}"))
{
stat = statement(parent);
stat = static_cast<BasicStatement * >(statement(parent));
}
}
@ -220,44 +220,56 @@ ASTStatement * Parser::statement(ASTNode * parent)
}
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_symbol)
{
report("Variable: " + tok_value + "\n");
ce = new ConstantExpression(expression, tok_value);
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(accept("("))
{
report("( ");
expr();
be = static_cast<BasicExpression *>(expr(expression));
report(" ) ");
expect(")");
return be;
}
else
{
error("ERROR: unexpected primary expression " + tok_value + "\n");
read_next();
{
string error_message = "ERROR: unexpected primary expression " + tok_value + "\n";
error(error_message);
read_next();
return new IncorrectExpression(expression, error_message);
}
return ce;
}
ASTExpression * Parser::postfix_expr(ASTNode * expression)
{
prim_expr();
{
//TODO implement postfix expression ASAP
BasicExpression * be = new BasicExpression(expression);
prim_expr(be);
if(accept("["))
{
expr();
expr(be);
expect("]");
report(" [] ");
@ -266,89 +278,115 @@ ASTExpression * Parser::postfix_expr(ASTNode * expression)
{
if(!accept(")"))
{
expr();
expr(be);
report("function argument\n");
while(accept(","))
{
expr();
expr(be);
report("function argument\n");
}
expect(")");
}
report("FUNC_CALL\n");
}
}
//TODO implement postfix_expression
return be;
}
ASTExpression * Parser::mul_expr(ASTNode * expression)
{
postfix_expr();
BasicExpression * be = new BasicExpression(expression);
be->set_left_operand(postfix_expr(be));
while(peek("*") || peek("/"))
{
if(accept("*"))
{
postfix_expr();
be->set_operator("*");
report(" *\n");
} else if(accept("/"))
{
postfix_expr();
be->set_operator("/");
report(" /\n");
}
be->set_right_operand(postfix_expr(be));
}
return be;
}
ASTExpression * Parser::add_expr(ASTNode * expression)
{
mul_expr();
{
BasicExpression * be = new BasicExpression(expression);
be->set_left_operand(mul_expr(be));
while(peek("+") || peek("-"))
{
if(accept("+"))
{
mul_expr();
report(" +\n");
} else if(accept("-"))
report(" +\n");
be->set_operator("+");
}
else if(accept("-"))
{
mul_expr();
report(" -\n");
}
}
report(" -\n");
be->set_operator("-");
}
be->set_right_operand(mul_expr(be));
}
return be;
}
ASTExpression * Parser::rel_expr(ASTNode * expression)
{
add_expr();
while(peek("<"))
{
BasicExpression * be = new BasicExpression(expression);
be->set_left_operand(add_expr(be));
while(peek("<") || peek(">"))
{
accept("<");
add_expr();
report(" <\n");
}
if(accept("<"))
{
be->set_operator("<");
report(" <\n");
}
else if (accept(">"))
{
be->set_operator(">");
report(" >\n");
}
be->set_right_operand(add_expr(be));
}
return be;
}
ASTExpression * Parser::eq_expr(ASTNode * expression)
{
rel_expr();
{
BasicExpression * be = new BasicExpression(expression);
be->set_left_operand(rel_expr(be));
while(peek("==") || peek("!="))
{
if(accept("=="))
{
rel_expr();
be->set_operator("==");
report("==\n");
}
else if(accept("!="))
{
rel_expr();
be->set_operator("!=");
report("!=\n");
}
}
}
be->set_right_operand(rel_expr(be));
}
return be;
}
ASTExpression * Parser::expr(ASTNode * expression)
{
eq_expr(expression);
BasicExpression * be = new BasicExpression(expression);
be->set_left_operand(eq_expr(be));
if(accept("="))
{
expr();
{
be->set_operator("=");
be->set_right_operand(expr(be));
report(" :=\n");
}
}
//return be;
return new ConstantExpression(expression, 3);
}

View File

@ -1,10 +1,12 @@
#ifndef PARSER_H
#define PARSER_H
#include <string>
#include <vector>
#include <vector>
#include <cstdlib>
#include "Token.h"
#include "AST.h"
#include "../AST/AllTypesOfASTNodes.h"
#include "AST/AllTypesOfASTNodes.h"
#include "AST/AST.h"
using namespace std;