Lots of stuff done in AST. But still not close to finish.

devel
Justyna Att Ilczuk 2012-12-05 23:27:03 +01:00
parent d40b7c2397
commit d996b89678
16 changed files with 201 additions and 41 deletions

View File

@ -7,12 +7,13 @@
#include "ASTExpression.h"
ASTExpression::ASTExpression() {
// TODO Auto-generated constructor stub
ASTExpression::ASTExpression(ASTNode * parent) {
this->parent = parent;
}
ASTExpression::~ASTExpression() {
// TODO Auto-generated destructor stub
}

View File

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

View File

@ -19,6 +19,7 @@ public:
void add_children(ASTNode *);
void remove_most_right_children();
void set_parent(ASTNode *);
virtual std::string debug() = 0;
virtual void execute() = 0;
virtual ~ASTNode();

View File

@ -12,7 +12,11 @@
#include "ProgramNode.h"
#include "ASTStatement.h"
#include "ASTExpression.h"
#include "ASTPrimary.h"
#include "BasicStatement.h"
#include "BasicExpression.h"
#include "ConstantExpression.h"
#include "PostfixExpression.h"
//And probably more
//TODO actualize it

View File

@ -12,6 +12,9 @@
class BasicExpression : public ASTExpression {
public:
//TODO change oper to enum
std::string all_operators[] = {"<", ">", "+", "-", "/", "*", "%", "&", "|", "=", ":", "==", "+=", "-=", "<=", ">=", "!=", "&&", "||"};
std::string oper;
ASTExpression * left;
ASTExpression * right;
@ -23,6 +26,9 @@ public:
virtual std::string debug();
std::string debug() { return "Basic expression:\n" + "left operand: \n" +
left->debug() + "\nright operand:\n" + right->debug +"\n###\n"; }
BasicExpression(ASTNode * parent);
virtual ~BasicExpression();
};

View File

@ -0,0 +1,32 @@
/*
* BasicStatement.cpp
*
* Created on: Dec 5, 2012
* Author: attero
*/
#include "BasicStatement.h"
BasicStatement::BasicStatement(ASTNode * parent) {
// TODO Auto-generated constructor stub
this->parent = parent;
}
BasicStatement::~BasicStatement() {
// TODO Auto-generated destructor stub
}
void BasicStatement::add_expression(ASTExpression * expr)
{
this->add_children(expr);
}
std::string BasicStatement::debug()
{
return "Basic statement with expression:\n" + children[0]->debug() + "\n;\n";
}
void BasicStatement::execute()
{
std::cout << children[0]->debug() << std::endl ;
}

View File

@ -0,0 +1,21 @@
/*
* BasicStatement.h
*
* Created on: Dec 5, 2012
* Author: attero
*/
#ifndef BASICSTATEMENT_H_
#define BASICSTATEMENT_H_
#include <iostream>
class BasicStatement : public ASTStatement{
public:
BasicStatement(ASTNode * parent);
virtual std::string debug();
void add_expression(ASTExpression * expr);
virtual void execute();
virtual ~BasicStatement();
};
#endif /* BASICSTATEMENT_H_ */

View File

@ -7,8 +7,9 @@
#include "ConstantExpression.h"
ConstantExpression::ConstantExpression() {
ConstantExpression::ConstantExpression(ASTNode * parent) {
// TODO Auto-generated constructor stub
this->parent = parent;
value = SenchaObject();
}

View File

@ -11,12 +11,15 @@
class ConstantExpression : public ASTExpression {
public:
ConstantExpression();
ConstantExpression(int number) { value = SenchaObject(number);};
ConstantExpression(double number) { value = SenchaObject(number); };
ConstantExpression(std::string text){ value = SenchaObject(text);};
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);};
SenchaObject value;
std::string debug() { return "Constant expression:\n" + value.repr() + "\n"; }
virtual ~ConstantExpression();
virtual SenchaObject evaluate();
};

View File

@ -18,4 +18,25 @@ ProgramNode::~ProgramNode() {
void ProgramNode::execute() {
std::cout << "Program started!\n";
for (std::vector<ASTNode *>::iterator it = children.begin(); it!=children.end(); ++it) {
(*it)->execute();
}
std::cout << "END\n";
}
std::string ProgramNode::debug()
{
std::string debug_note = "Program started debugging\n";
for (std::vector<ASTNode *>::iterator it = children.begin(); it!=children.end(); ++it) {
debug_note += (*it)->debug() + "\n";
}
debug_note += "END\n";
return debug_note;
}
void ProgramNode::add_statement(ASTStatement * statement)
{
children.push_back(statement);
}

View File

@ -14,7 +14,8 @@
class ProgramNode : public ASTNode {
public:
ProgramNode();
ASTStatement * add_child(ASTStatement * node);
std::string debug();
void add_statement(ASTStatement * statement);
virtual ~ProgramNode();
virtual void execute();
};

View File

@ -18,6 +18,35 @@ SenchaObject::~SenchaObject() {
// TODO Auto-generated destructor stub
}
std::string SenchaObject::repr()
{
std::string representation = "";
switch(type){
case string_literal:
representation = "type: string\n";
representation += this->text;
break;
case integer_number:
representation = "type: integer\n";
representation += to_string(this->integer);
break;
case float_number:
representation = "type: float\n";
representation += to_string(this->number);
break;
case null:
representation = "type: null\n";
representation += "null";
break;
case invalid:
representation = "type: invalid\n";
representation += "some crap";
break;
}
return representation;
}
SenchaObject SenchaObject::operator+(const SenchaObject& right)const
{
SenchaObject result;

View File

@ -8,6 +8,7 @@
#ifndef SENCHAOBJECT_H_
#define SENCHAOBJECT_H_
#include <string>
#include "to_string.h"
class SenchaObject {
public:
@ -21,6 +22,7 @@ public:
Type type;
virtual std::string repr();
std::string text;
int integer;

View File

@ -0,0 +1,24 @@
/*
* to_string.h
*
* Created on: Dec 5, 2012
* Author: attero
*/
#ifndef TO_STRING_H_
#define TO_STRING_H_
#include <sstream>
template <class T>
inline std::string to_string (const T& t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
#endif /* TO_STRING_H_ */

View File

@ -7,6 +7,7 @@ Parser::Parser()
report_message = "***PARSER REPORT***\n";
position_in_stream = 0;
in_statement = false;
program = static_cast<ProgramNode *>(tree.root);
}
Parser::~Parser()
@ -63,8 +64,8 @@ void Parser::interpret()
{
read_next();
while(tok_value!= "")
{
statement();
{
program->add_statement(statement(program));
}
@ -114,13 +115,15 @@ bool Parser::is_type()
else return false;
}
ASTNode * Parser::statement(ASTNode * statement)
{
ASTStatement * Parser::statement(ASTNode * parent)
{
BasicStatement * stat(parent);
if(accept("{"))
{
while(!accept("}"))
{
statement();
stat = statement(parent);
}
}
@ -136,7 +139,8 @@ ASTNode * Parser::statement(ASTNode * statement)
{
report("Identifier: " + tok_value + "\n");
read_next();
expr();
stat->add_expression(expr(stat));
;
report(" := \n");
}
@ -144,7 +148,7 @@ ASTNode * Parser::statement(ASTNode * statement)
if(accept(";"))
{
report("Variable definition\n");
return;
return stat;
}
if(expect("("))
@ -167,7 +171,7 @@ ASTNode * Parser::statement(ASTNode * statement)
if(!accept(";"))
{
report("function body:\n");
statement();
statement(stat);
report("function definition\n");
}
}
@ -184,32 +188,38 @@ ASTNode * Parser::statement(ASTNode * statement)
else if(accept("if"))
{
//stuff
//TODO implement that
//TODO implement that
return stat;
}
else if(accept("while"))
{
//similar stuff
//similar stuff
return stat;
}
else if(accept("return"))
{
if(!peek(";"))
{
expr();
stat->add_expression(expr(stat));
}
expect(";");
report("RETURN\n");
report("RETURN\n");
return stat;
}
else
{
expr();
while(!expect(";") && tok_value != "") read_next();
stat->add_expression(expr(stat));
while(!expect(";") && tok_value != "") read_next();
return stat;
}
}
return stat;
}
ASTNode * Parser::prim_expr(ASTNode * expression)
ASTExpression * Parser::prim_expr(ASTNode * expression)
{
if(current_token.get_type() == t_integer)
{
@ -242,7 +252,7 @@ ASTNode * Parser::prim_expr(ASTNode * expression)
}
ASTNode * Parser::postfix_expr(ASTNode * expression)
ASTExpression * Parser::postfix_expr(ASTNode * expression)
{
prim_expr();
if(accept("["))
@ -270,7 +280,7 @@ ASTNode * Parser::postfix_expr(ASTNode * expression)
}
}
ASTNode * Parser::mul_expr(ASTNode * expression)
ASTExpression * Parser::mul_expr(ASTNode * expression)
{
postfix_expr();
while(peek("*") || peek("/"))
@ -287,7 +297,7 @@ ASTNode * Parser::mul_expr(ASTNode * expression)
}
}
ASTNode * Parser::add_expr(ASTNode * expression)
ASTExpression * Parser::add_expr(ASTNode * expression)
{
mul_expr();
while(peek("+") || peek("-"))
@ -304,7 +314,7 @@ ASTNode * Parser::add_expr(ASTNode * expression)
}
}
ASTNode * Parser::rel_expr(ASTNode * expression)
ASTExpression * Parser::rel_expr(ASTNode * expression)
{
add_expr();
while(peek("<"))
@ -315,7 +325,7 @@ ASTNode * Parser::rel_expr(ASTNode * expression)
}
}
ASTNode * Parser::eq_expr(ASTNode * expression)
ASTExpression * Parser::eq_expr(ASTNode * expression)
{
rel_expr();
while(peek("==") || peek("!="))
@ -333,7 +343,7 @@ ASTNode * Parser::eq_expr(ASTNode * expression)
}
}
ASTNode * Parser::expr(ASTNode * expression)
ASTExpression * Parser::expr(ASTNode * expression)
{
eq_expr(expression);
if(accept("="))

View File

@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include "Token.h"
#include "AST.h"
#include "../AST/AllTypesOfASTNodes.h"
using namespace std;
@ -25,6 +26,8 @@ class Parser
string tok_value;
vector<Token> token_stream;
int position_in_stream;
AST tree;
ProgramNode * program;
bool in_statement;
bool read_next();
@ -37,15 +40,15 @@ class Parser
void report(string s);
//TODO change functions below to use AST nodes
ASTNode * statement(ASTNode * node);
ASTStatement * statement(ASTNode * node);
ASTNode * mul_expr(ASTNode * node);
ASTNode * add_expr(ASTNode * node);
ASTNode * prim_expr(ASTNode * node);
ASTNode * postfix_expr(ASTNode * node);
ASTNode * rel_expr(ASTNode * node);
ASTNode * eq_expr(ASTNode * node);
ASTNode * expr(ASTNode * node);
ASTExpression * mul_expr(ASTNode * node);
ASTExpression * add_expr(ASTNode * node);
ASTExpression * prim_expr(ASTNode * node);
ASTExpression * postfix_expr(ASTNode * node);
ASTExpression * rel_expr(ASTNode * node);
ASTExpression * eq_expr(ASTNode * node);
ASTExpression * expr(ASTNode * node);
};
#endif // PARSER_H