summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Sencha-lang/AST/AST.cpp5
-rw-r--r--Sencha-lang/AST/AST.h1
-rw-r--r--Sencha-lang/AST/ASTExpression.h1
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp29
-rw-r--r--Sencha-lang/AST/BasicExpression.h8
-rw-r--r--Sencha-lang/AST/BasicStatement.cpp2
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp6
-rw-r--r--Sencha-lang/AST/ConstantExpression.h2
-rw-r--r--Sencha-lang/AST/PostfixExpression.cpp36
-rw-r--r--Sencha-lang/AST/PostfixExpression.h17
-rw-r--r--Sencha-lang/AST/SenchaObject.cpp78
-rw-r--r--Sencha-lang/AST/SenchaObject.h4
-rw-r--r--Sencha-lang/Debug/AST/PostfixExpression.d4
-rwxr-xr-xSencha-lang/Debug/Sencha-langbin769707 -> 842039 bytes
-rw-r--r--Sencha-lang/Parser.cpp91
-rw-r--r--Sencha-lang/Parser.h7
-rw-r--r--Sencha-lang/corebin0 -> 23388160 bytes
-rw-r--r--Sencha-lang/main.cpp4
18 files changed, 262 insertions, 33 deletions
diff --git a/Sencha-lang/AST/AST.cpp b/Sencha-lang/AST/AST.cpp
index 5fcf37e..8c36289 100644
--- a/Sencha-lang/AST/AST.cpp
+++ b/Sencha-lang/AST/AST.cpp
@@ -14,6 +14,11 @@ AST::AST() {
level_of_depth = 0;
}
+void AST::delete_all_children()
+{
+ //TODO perform deleting
+}
+
AST::~AST() {
}
diff --git a/Sencha-lang/AST/AST.h b/Sencha-lang/AST/AST.h
index c29207e..113ec4c 100644
--- a/Sencha-lang/AST/AST.h
+++ b/Sencha-lang/AST/AST.h
@@ -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();
};
diff --git a/Sencha-lang/AST/ASTExpression.h b/Sencha-lang/AST/ASTExpression.h
index af5f487..eb56b19 100644
--- a/Sencha-lang/AST/ASTExpression.h
+++ b/Sencha-lang/AST/ASTExpression.h
@@ -14,6 +14,7 @@ class ASTExpression : public ASTNode {
public:
virtual SenchaObject evaluate() = 0;
+ virtual void execute() = 0;
ASTExpression();
virtual ~ASTExpression();
};
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index 5e1dc7a..b297aa9 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -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;
}
diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h
index c9a79e3..00dcac2 100644
--- a/Sencha-lang/AST/BasicExpression.h
+++ b/Sencha-lang/AST/BasicExpression.h
@@ -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() ;
diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp
index 3229321..02ac7c3 100644
--- a/Sencha-lang/AST/BasicStatement.cpp
+++ b/Sencha-lang/AST/BasicStatement.cpp
@@ -28,5 +28,5 @@ std::string BasicStatement::debug()
void BasicStatement::execute()
{
- std::cout << children[0]->debug() << std::endl ;
+ children[0]->execute() ;
}
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 9858bb9..d0ade2e 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -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;
diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h
index 5ba474c..f74b28f 100644
--- a/Sencha-lang/AST/ConstantExpression.h
+++ b/Sencha-lang/AST/ConstantExpression.h
@@ -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();
diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp
index 4ceb6b4..82876cd 100644
--- a/Sencha-lang/AST/PostfixExpression.cpp
+++ b/Sencha-lang/AST/PostfixExpression.cpp
@@ -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!";
+}
diff --git a/Sencha-lang/AST/PostfixExpression.h b/Sencha-lang/AST/PostfixExpression.h
index 0a2f94a..4aa5974 100644
--- a/Sencha-lang/AST/PostfixExpression.h
+++ b/Sencha-lang/AST/PostfixExpression.h
@@ -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();
};
diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp
index 46694f3..ed86bf4 100644
--- a/Sencha-lang/AST/SenchaObject.cpp
+++ b/Sencha-lang/AST/SenchaObject.cpp
@@ -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
diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h
index 9a4f1bb..fbaf064 100644
--- a/Sencha-lang/AST/SenchaObject.h
+++ b/Sencha-lang/AST/SenchaObject.h
@@ -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();
};
diff --git a/Sencha-lang/Debug/AST/PostfixExpression.d b/Sencha-lang/Debug/AST/PostfixExpression.d
index 4160736..352b923 100644
--- a/Sencha-lang/Debug/AST/PostfixExpression.d
+++ b/Sencha-lang/Debug/AST/PostfixExpression.d
@@ -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:
diff --git a/Sencha-lang/Debug/Sencha-lang b/Sencha-lang/Debug/Sencha-lang
index ee9f9ac..0add6c8 100755
--- a/Sencha-lang/Debug/Sencha-lang
+++ b/Sencha-lang/Debug/Sencha-lang
Binary files differ
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 9cde1dc..a4e1575 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -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;
}
diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h
index 1d3f3e2..714c91d 100644
--- a/Sencha-lang/Parser.h
+++ b/Sencha-lang/Parser.h
@@ -20,6 +20,9 @@ class Parser
string error_message;
void add_tokens(vector<Token> tokens);
string show_tokens();
+ AST tree;
+ ProgramNode * program;
+ void erase_all();
protected:
@@ -28,8 +31,8 @@ class Parser
string tok_value;
vector<Token> token_stream;
int position_in_stream;
- AST tree;
- ProgramNode * program;
+
+
bool in_statement;
bool read_next();
diff --git a/Sencha-lang/core b/Sencha-lang/core
new file mode 100644
index 0000000..d9522ee
--- /dev/null
+++ b/Sencha-lang/core
Binary files differ
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp
index 0f6a449..3564ef0 100644
--- a/Sencha-lang/main.cpp
+++ b/Sencha-lang/main.cpp
@@ -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;
}
}