summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-17 10:41:48 +0100
committerJustyna Ilczuk <justyna.ilczuk@gmail.com>2012-12-17 10:41:48 +0100
commitb9d9788da24cea3d0e193ac318d07f6c8e1b54ab (patch)
treee26f3027b71af16aade5bf482caa3c33e5bc12c4
parent46ac5b352b7668047c0ca19745f82b8dbd0c0270 (diff)
downloadsencha-lang-b9d9788da24cea3d0e193ac318d07f6c8e1b54ab.tar.gz
sencha-lang-b9d9788da24cea3d0e193ac318d07f6c8e1b54ab.tar.bz2
sencha-lang-b9d9788da24cea3d0e193ac318d07f6c8e1b54ab.tar.xz
sencha-lang-b9d9788da24cea3d0e193ac318d07f6c8e1b54ab.zip
New class for variables. It's much more appropriate than storing
variables in constant expression class isn't it?
-rw-r--r--Sencha-lang/AST/AllTypesOfASTNodes.h1
-rw-r--r--Sencha-lang/AST/Assignment.cpp2
-rw-r--r--Sencha-lang/AST/BasicExpression.cpp2
-rw-r--r--Sencha-lang/AST/ConstantExpression.cpp26
-rw-r--r--Sencha-lang/AST/ConstantExpression.h3
-rw-r--r--Sencha-lang/AST/DeclarationStatement.cpp2
-rw-r--r--Sencha-lang/AST/VariableExpression.cpp41
-rw-r--r--Sencha-lang/AST/VariableExpression.h30
-rw-r--r--Sencha-lang/Parser.cpp32
-rw-r--r--Sencha-lang/main.cpp4
10 files changed, 96 insertions, 47 deletions
diff --git a/Sencha-lang/AST/AllTypesOfASTNodes.h b/Sencha-lang/AST/AllTypesOfASTNodes.h
index 22c4abb..52cb8df 100644
--- a/Sencha-lang/AST/AllTypesOfASTNodes.h
+++ b/Sencha-lang/AST/AllTypesOfASTNodes.h
@@ -23,6 +23,7 @@
#include "IfNode.h"
#include "WhileNode.h"
#include "RepeatStatement.h"
+#include "VariableExpression.h"
//And probably more
//TODO actualize it
diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp
index b7761ef..20ff698 100644
--- a/Sencha-lang/AST/Assignment.cpp
+++ b/Sencha-lang/AST/Assignment.cpp
@@ -25,7 +25,6 @@ void Assignment::execute()
if(name != "")
{
- right_value.name = left_value.name;
context->set(name, right_value);
}
}
@@ -37,7 +36,6 @@ void Assignment::execute_quietly()
static_cast<ASTExpression *>(children[1])->execute_quietly();
if(left_value.name != "")
{
- right_value.name = left_value.name;
context->set(left_value.name, right_value);
}
diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp
index ab38dde..46a9d75 100644
--- a/Sencha-lang/AST/BasicExpression.cpp
+++ b/Sencha-lang/AST/BasicExpression.cpp
@@ -99,7 +99,7 @@ BasicExpression::BasicExpression(ASTNode * parent) {
}
BasicExpression::~BasicExpression() {
- //Do nothing
+
}
diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp
index 25a3889..64b84ba 100644
--- a/Sencha-lang/AST/ConstantExpression.cpp
+++ b/Sencha-lang/AST/ConstantExpression.cpp
@@ -10,47 +10,26 @@
ConstantExpression::ConstantExpression(ASTNode * parent) {
this->parent = parent;
value = SenchaObject();
- this->context = NULL;
}
ConstantExpression::~ConstantExpression() {
- // Do nothing
-}
-ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, std::string name)
-{
- this->parent = parent;
- this->value = value;
- this->value.name = name;
- this->context = NULL;
-}
-
-ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, Context * context)
-{
- this->context = context;
- this->parent = parent;
- this->value = value;
}
ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value)
{
this->parent = parent;
this->value = value;
- this->context = NULL;
}
SenchaObject ConstantExpression::evaluate()
{
- if(value.name != "" && context != NULL)
- {
- return context->get(value.name);
- }
return value;
}
void ConstantExpression::execute()
{
- std::cout << evaluate().repr() << std::endl;//Do nothing
+ std::cout << evaluate().repr() << std::endl;
}
void ConstantExpression::execute_quietly()
@@ -62,20 +41,17 @@ void ConstantExpression::execute_quietly()
ConstantExpression::ConstantExpression(ASTNode * parent, int number)
{
this->parent = parent; value = SenchaObject(number);
- this->context = NULL;
}
ConstantExpression::ConstantExpression(ASTNode * parent, double number)
{
this->parent = parent;
value = SenchaObject(number);
- this->context = NULL;
}
ConstantExpression::ConstantExpression(ASTNode * parent, std::string text)
{
this->parent = parent; value = SenchaObject(text);
- this->context = NULL;
}
std::string ConstantExpression::debug() { return "Constant expression:\n" + value.repr() + "\n"; }
diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h
index 285972e..3016529 100644
--- a/Sencha-lang/AST/ConstantExpression.h
+++ b/Sencha-lang/AST/ConstantExpression.h
@@ -17,12 +17,9 @@ public:
ConstantExpression(ASTNode * parent, int number) ;
ConstantExpression(ASTNode * parent, double number) ;
ConstantExpression(ASTNode * parent, std::string text);
- ConstantExpression(ASTNode * parent, SenchaObject value, std::string name);
- ConstantExpression(ASTNode * parent, SenchaObject value, Context * context);
ConstantExpression(ASTNode * parent, SenchaObject value);
SenchaObject value;
- Context * context;
std::string debug();
diff --git a/Sencha-lang/AST/DeclarationStatement.cpp b/Sencha-lang/AST/DeclarationStatement.cpp
index ad9e86d..a734cbb 100644
--- a/Sencha-lang/AST/DeclarationStatement.cpp
+++ b/Sencha-lang/AST/DeclarationStatement.cpp
@@ -63,7 +63,7 @@ void DeclarationStatement::execute()
else
{
context->add(name, right_value);
- children[0]->execute() ;
+ children[0]->execute();
}
}
diff --git a/Sencha-lang/AST/VariableExpression.cpp b/Sencha-lang/AST/VariableExpression.cpp
new file mode 100644
index 0000000..93856f0
--- /dev/null
+++ b/Sencha-lang/AST/VariableExpression.cpp
@@ -0,0 +1,41 @@
+/*
+ * VariableExpression.cpp
+ *
+ * Created on: Dec 17, 2012
+ * Author: att
+ */
+
+#include "VariableExpression.h"
+
+VariableExpression::VariableExpression(ASTNode * parent, Context * context, std::string name) {
+
+ this->name = name;
+ this->parent = parent;
+ this->context = context;
+}
+
+std::string VariableExpression::debug()
+{
+ std::string debug_note = "variable: " + name;
+ return debug_note;
+}
+VariableExpression::~VariableExpression() {
+ // Do nothing
+}
+
+SenchaObject VariableExpression::evaluate()
+{
+ SenchaObject result = context->get(name);
+ result.name = name;
+ return result;
+}
+
+void VariableExpression::execute()
+{
+ std::cout << evaluate().repr() << std::endl;
+}
+
+void VariableExpression::execute_quietly()
+{
+ evaluate();
+}
diff --git a/Sencha-lang/AST/VariableExpression.h b/Sencha-lang/AST/VariableExpression.h
new file mode 100644
index 0000000..522b71b
--- /dev/null
+++ b/Sencha-lang/AST/VariableExpression.h
@@ -0,0 +1,30 @@
+/*
+ * VariableExpression.h
+ *
+ * Created on: Dec 17, 2012
+ * Author: att
+ */
+
+#ifndef VARIABLEEXPRESSION_H_
+#define VARIABLEEXPRESSION_H_
+#include <iostream>
+#include <string>
+#include "../Context.h"
+#include "ASTExpression.h"
+
+class VariableExpression: public ASTExpression {
+public:
+ VariableExpression();
+ VariableExpression(ASTNode * parent, Context * context, std::string name);
+
+ Context * context;
+ std::string debug();
+ std::string name;
+
+ void execute();
+ void execute_quietly();
+ SenchaObject evaluate();
+ virtual ~VariableExpression();
+};
+
+#endif /* VARIABLEEXPRESSION_H_ */
diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp
index 5721beb..e30e337 100644
--- a/Sencha-lang/Parser.cpp
+++ b/Sencha-lang/Parser.cpp
@@ -273,31 +273,37 @@ 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");
+ ConstantExpression * ce;
ce = new ConstantExpression(expression, std::atoi(tok_value.c_str()));
- read_next();
+ read_next();
+ return ce;
}
else if(current_token.get_type() == t_float)
{
report("Number: " + tok_value + "\n");
-
+ ConstantExpression * ce;
ce = new ConstantExpression(expression, std::atof(tok_value.c_str()));
read_next();
+ return ce;
}
else if(current_token.get_type() == t_literal)
{
report("Character literal: " + tok_value + "\n");
+ ConstantExpression * ce;
ce = new ConstantExpression(expression, tok_value);
- read_next();
+ read_next();
+ return ce;
}
else if(current_token.get_type() == t_keyword)
{
+ ConstantExpression * ce;
if(tok_value == "true")
{
report("Boolean: " + tok_value + "\n");
@@ -310,23 +316,22 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
ce = new ConstantExpression(expression, SenchaObject(false));
read_next();
}
+ return ce;
}
else if(current_token.get_type() == t_symbol)
{
report("variable: " + tok_value + "\n");
- SenchaObject variable;
- //TODO is it right?
string name = current_token.value;
- //variable = context->get(name);
- variable.name = name;
-
- ce = new ConstantExpression(expression, variable, context);
+ VariableExpression * ve;
+ ve = new VariableExpression(expression, context, name);
read_next();
+ return ve;
}
else if(accept("("))
{
- report("( ");
+ report("( ");
+ BasicExpression * be;
be = static_cast<BasicExpression *>(expr(expression));
report(" ) ");
expect(")");
@@ -340,7 +345,6 @@ ASTExpression * Parser::prim_expr(ASTNode * expression)
return new IncorrectExpression(expression, error_message);
}
- return ce;
}
ASTExpression * Parser::postfix_expr(ASTNode * expression)
diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp
index a0486a0..74d523d 100644
--- a/Sencha-lang/main.cpp
+++ b/Sencha-lang/main.cpp
@@ -19,7 +19,9 @@ I like bananasnull
Context: 0: dupa"); type: null
null
-dupa"); declaration? hello?
+dupa"); declaration? hello? It's probably temporary effect of searching for this name in variable tables
+
+>>
*/
SenchaObject print(vector<ASTExpression *> arguments)