diff options
author | Justyna Att Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-08 20:59:05 +0100 |
---|---|---|
committer | Justyna Att Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-08 20:59:05 +0100 |
commit | bdd319ecb8081146f19a40294c9bf4b7fa0010c5 (patch) | |
tree | ce7b80b720297687a15cf894b54f99fa88458933 | |
parent | 30e3fb729c1cd8d057d0872a8cb285987432e83b (diff) | |
download | sencha-lang-bdd319ecb8081146f19a40294c9bf4b7fa0010c5.tar.gz sencha-lang-bdd319ecb8081146f19a40294c9bf4b7fa0010c5.tar.bz2 sencha-lang-bdd319ecb8081146f19a40294c9bf4b7fa0010c5.tar.xz sencha-lang-bdd319ecb8081146f19a40294c9bf4b7fa0010c5.zip |
variabuls und stuff
29 files changed, 303 insertions, 96 deletions
diff --git a/Sencha-lang/.cproject b/Sencha-lang/.cproject index ef991be..a4fe393 100644 --- a/Sencha-lang/.cproject +++ b/Sencha-lang/.cproject @@ -27,6 +27,7 @@ <tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.495732557" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler"> <option id="gnu.cpp.compiler.option.optimization.level.793014755" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/> <option id="gnu.cpp.compiler.option.debugging.level.1513381051" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/> + <option id="gnu.cpp.compiler.option.other.other.335578218" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=gnu++0x " valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.859439950" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> </tool> <tool id="cdt.managedbuild.tool.gnu.cross.c.linker.693029150" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker"/> diff --git a/Sencha-lang/AST/ASTNode.cpp b/Sencha-lang/AST/ASTNode.cpp index 80429c2..6ee2df0 100644 --- a/Sencha-lang/AST/ASTNode.cpp +++ b/Sencha-lang/AST/ASTNode.cpp @@ -8,7 +8,6 @@ #include "ASTNode.h" ASTNode::ASTNode() { - // TODO Auto-generated constructor stub parent = 0; } diff --git a/Sencha-lang/AST/Assignment.cpp b/Sencha-lang/AST/Assignment.cpp new file mode 100644 index 0000000..dd8215d --- /dev/null +++ b/Sencha-lang/AST/Assignment.cpp @@ -0,0 +1,18 @@ +/* + * Assignment.cpp + * + * Created on: Dec 7, 2012 + * Author: attero + */ + +#include "Assignment.h" + +Assignment::Assignment() { + // TODO Auto-generated constructor stub + +} + +Assignment::~Assignment() { + // TODO Auto-generated destructor stub +} + diff --git a/Sencha-lang/AST/Assignment.h b/Sencha-lang/AST/Assignment.h new file mode 100644 index 0000000..c503f6b --- /dev/null +++ b/Sencha-lang/AST/Assignment.h @@ -0,0 +1,17 @@ +/* + * Assignment.h + * + * Created on: Dec 7, 2012 + * Author: attero + */ + +#ifndef ASSIGNMENT_H_ +#define ASSIGNMENT_H_ + +class Assignment { +public: + Assignment(); + virtual ~Assignment(); +}; + +#endif /* ASSIGNMENT_H_ */ diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp index b297aa9..76f4be8 100644 --- a/Sencha-lang/AST/BasicExpression.cpp +++ b/Sencha-lang/AST/BasicExpression.cpp @@ -52,22 +52,31 @@ void BasicExpression::execute() SenchaObject BasicExpression::evaluate() { //TODO refactor it ;) + + SenchaObject left = static_cast<ASTExpression *>(children[0])->evaluate(); + SenchaObject right = static_cast<ASTExpression *>(children[1])->evaluate(); + + SenchaObject so; if(oper == "+") { - so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() + static_cast<ASTExpression *>(children[1])->evaluate()); + so = SenchaObject(left + right); } else if(oper == "-") { - so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() - static_cast<ASTExpression *>(children[1])->evaluate()); + so = SenchaObject(left - right); } else if(oper == "*") { - so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() * static_cast<ASTExpression *>(children[1])->evaluate()); + so = SenchaObject(left * right); } else if(oper == "/") { - so = SenchaObject(static_cast<ASTExpression *>(children[0])->evaluate() / static_cast<ASTExpression *>(children[1])->evaluate()); + so = SenchaObject(left / right); + } + else if(oper == "=") + { + so = SenchaObject(right); } return so; } diff --git a/Sencha-lang/AST/BasicStatement.cpp b/Sencha-lang/AST/BasicStatement.cpp index 02ac7c3..ffdfe1f 100644 --- a/Sencha-lang/AST/BasicStatement.cpp +++ b/Sencha-lang/AST/BasicStatement.cpp @@ -8,7 +8,6 @@ #include "BasicStatement.h" BasicStatement::BasicStatement(ASTNode * parent) { - // TODO Auto-generated constructor stub this->parent = parent; } diff --git a/Sencha-lang/AST/ConstantExpression.cpp b/Sencha-lang/AST/ConstantExpression.cpp index d0ade2e..c151688 100644 --- a/Sencha-lang/AST/ConstantExpression.cpp +++ b/Sencha-lang/AST/ConstantExpression.cpp @@ -8,7 +8,6 @@ #include "ConstantExpression.h" ConstantExpression::ConstantExpression(ASTNode * parent) { - // TODO Auto-generated constructor stub this->parent = parent; value = SenchaObject(); } @@ -17,10 +16,11 @@ ConstantExpression::~ConstantExpression() { // TODO Auto-generated destructor stub } -ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value) +ConstantExpression::ConstantExpression(ASTNode * parent, SenchaObject value, std::string name) { this->parent = parent; this->value = value; + this->value.name = name; } SenchaObject ConstantExpression::evaluate() diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h index f74b28f..4078e24 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); + ConstantExpression(ASTNode * parent, SenchaObject value, std::string name); SenchaObject value; std::string debug(); diff --git a/Sencha-lang/AST/PostfixExpression.cpp b/Sencha-lang/AST/PostfixExpression.cpp index 82876cd..71611fb 100644 --- a/Sencha-lang/AST/PostfixExpression.cpp +++ b/Sencha-lang/AST/PostfixExpression.cpp @@ -8,7 +8,6 @@ #include "PostfixExpression.h" PostfixExpression::PostfixExpression(ASTNode * parent) { - // TODO Auto-generated constructor stub this->parent = parent; head_set = false; body_set = false; diff --git a/Sencha-lang/AST/ProgramNode.cpp b/Sencha-lang/AST/ProgramNode.cpp index 787b9fc..8240529 100644 --- a/Sencha-lang/AST/ProgramNode.cpp +++ b/Sencha-lang/AST/ProgramNode.cpp @@ -17,12 +17,14 @@ 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"; +void ProgramNode::execute_last() +{ + children[children.size() -1]->execute(); } std::string ProgramNode::debug() diff --git a/Sencha-lang/AST/ProgramNode.h b/Sencha-lang/AST/ProgramNode.h index 375ab4b..1232699 100644 --- a/Sencha-lang/AST/ProgramNode.h +++ b/Sencha-lang/AST/ProgramNode.h @@ -18,6 +18,7 @@ public: void add_statement(ASTStatement * statement); virtual ~ProgramNode(); virtual void execute(); + virtual void execute_last(); }; #endif /* PROGRAMNODE_H_ */ diff --git a/Sencha-lang/AST/SenchaObject.cpp b/Sencha-lang/AST/SenchaObject.cpp index ed86bf4..0ccfd6a 100644 --- a/Sencha-lang/AST/SenchaObject.cpp +++ b/Sencha-lang/AST/SenchaObject.cpp @@ -9,9 +9,6 @@ SenchaObject::SenchaObject() { set_null_value(); - - // TODO Auto-generated constructor stub - } SenchaObject::~SenchaObject() { @@ -39,6 +36,10 @@ std::string SenchaObject::repr() representation = "type: null\n"; representation += "null"; break; + case symbol: + representation = "type: symbol\n"; + representation += this->text; + break; case invalid: representation = "type: invalid\n"; representation += "some crap"; diff --git a/Sencha-lang/AST/SenchaObject.h b/Sencha-lang/AST/SenchaObject.h index fbaf064..350ff43 100644 --- a/Sencha-lang/AST/SenchaObject.h +++ b/Sencha-lang/AST/SenchaObject.h @@ -17,6 +17,7 @@ public: integer_number, float_number, null, + symbol, invalid } Type; @@ -25,13 +26,17 @@ public: virtual std::string repr(); std::string text; + std::string name; int integer; double number; void set_value(std::string text) { - this->text = text; - type = string_literal; + if(text[0] == '\"' && text[text.size()-1] == '\"') + { + this->text = text.substr(1, text.size()-2); + type = string_literal; + } } void set_null_value() @@ -58,6 +63,7 @@ public: SenchaObject(int integer) { set_value(integer); } SenchaObject(double number) { set_value(number); } SenchaObject(std::string text) { set_value(text); } + //SenchaObject(std::string name, int i) {this->name = name; type = symbol;} //TODO overload operators as it should be done diff --git a/Sencha-lang/Context.cpp b/Sencha-lang/Context.cpp new file mode 100644 index 0000000..36c7c43 --- /dev/null +++ b/Sencha-lang/Context.cpp @@ -0,0 +1,57 @@ +/* + * Context.cpp + * + * Created on: Dec 7, 2012 + * Author: attero + */ + +#include "Context.h" + +Context::Context() { + // TODO Auto-generated constructor stub + index = 0; +} + +Context::~Context() { + // TODO Auto-generated destructor stub +} + +ObjectIndex Context::add_to_store(SenchaObject & object) +{ + index++; + object_store[index] = object; + + return index; +} + +SenchaObject Context::get_from_store(ObjectIndex index) +{ + return object_store[index]; +} + +void Context::add(std::string name, SenchaObject object) +{ + interpreter_context[name] = add_to_store(object); +} + +void Context::set(std::string name, SenchaObject object) +{ + object_store[interpreter_context[name]] = object; +} + +SenchaObject Context::get(std::string name) +{ + return get_from_store(interpreter_context[name]); +} + +std::string Context::debug() { + std::string debug_note = ""; + + for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++) + { + debug_note += "Context: " + to_string(index) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n"; + } + return debug_note; + + + } diff --git a/Sencha-lang/Context.h b/Sencha-lang/Context.h new file mode 100644 index 0000000..8b63286 --- /dev/null +++ b/Sencha-lang/Context.h @@ -0,0 +1,44 @@ +/* + * Context.h + * + * Created on: Dec 7, 2012 + * Author: attero + */ + +#ifndef CONTEXT_H_ +#define CONTEXT_H_ +#include <map> +#include "AST/SenchaObject.h" +#include <string> +#include <iostream> + +typedef unsigned long ObjectIndex; +typedef unsigned long FunctionIndex; + +class Context { +public: + Context(); + typedef unsigned long ObjectIndex; + ObjectIndex index; + + std::map<ObjectIndex, SenchaObject> object_store; + ObjectIndex add_to_store(SenchaObject & object); + SenchaObject get_from_store(ObjectIndex index); + + //Overload it to use contexts + void add(std::string name, SenchaObject object); + void set(std::string name, SenchaObject object); + SenchaObject get(std::string name); + typedef std::map<std::string, ObjectIndex> ExecutionContext; + ExecutionContext interpreter_context; + + std::string debug() ; + + + + ExecutionContext global_context; + // std::map<FunctionIndex , ExecutionContext> function_contexts; + virtual ~Context(); +}; + +#endif /* CONTEXT_H_ */ diff --git a/Sencha-lang/Debug/.metadata/.plugins/org.eclipse.cdt.make.core/subdir.mk b/Sencha-lang/Debug/.metadata/.plugins/org.eclipse.cdt.make.core/subdir.mk index 6ec27b7..4687af4 100644 --- a/Sencha-lang/Debug/.metadata/.plugins/org.eclipse.cdt.make.core/subdir.mk +++ b/Sencha-lang/Debug/.metadata/.plugins/org.eclipse.cdt.make.core/subdir.mk @@ -30,7 +30,7 @@ CPP_DEPS += \ .metadata/.plugins/org.eclipse.cdt.make.core/%.o: ../.metadata/.plugins/org.eclipse.cdt.make.core/%.cpp @echo 'Building file: $<' @echo 'Invoking: Cross G++ Compiler' - g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' diff --git a/Sencha-lang/Debug/AST/Assignment.d b/Sencha-lang/Debug/AST/Assignment.d new file mode 100644 index 0000000..692a84c --- /dev/null +++ b/Sencha-lang/Debug/AST/Assignment.d @@ -0,0 +1,3 @@ +AST/Assignment.d: ../AST/Assignment.cpp ../AST/Assignment.h + +../AST/Assignment.h: diff --git a/Sencha-lang/Debug/AST/subdir.mk b/Sencha-lang/Debug/AST/subdir.mk index bd9cee4..353c216 100644 --- a/Sencha-lang/Debug/AST/subdir.mk +++ b/Sencha-lang/Debug/AST/subdir.mk @@ -8,6 +8,7 @@ CPP_SRCS += \ ../AST/ASTExpression.cpp \ ../AST/ASTNode.cpp \ ../AST/ASTStatement.cpp \ +../AST/Assignment.cpp \ ../AST/BasicExpression.cpp \ ../AST/BasicStatement.cpp \ ../AST/ConstantExpression.cpp \ @@ -22,6 +23,7 @@ OBJS += \ ./AST/ASTExpression.o \ ./AST/ASTNode.o \ ./AST/ASTStatement.o \ +./AST/Assignment.o \ ./AST/BasicExpression.o \ ./AST/BasicStatement.o \ ./AST/ConstantExpression.o \ @@ -36,6 +38,7 @@ CPP_DEPS += \ ./AST/ASTExpression.d \ ./AST/ASTNode.d \ ./AST/ASTStatement.d \ +./AST/Assignment.d \ ./AST/BasicExpression.d \ ./AST/BasicStatement.d \ ./AST/ConstantExpression.d \ @@ -50,7 +53,7 @@ CPP_DEPS += \ AST/%.o: ../AST/%.cpp @echo 'Building file: $<' @echo 'Invoking: Cross G++ Compiler' - g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' diff --git a/Sencha-lang/Debug/Context.d b/Sencha-lang/Debug/Context.d new file mode 100644 index 0000000..8ae02c9 --- /dev/null +++ b/Sencha-lang/Debug/Context.d @@ -0,0 +1,8 @@ +Context.d: ../Context.cpp ../Context.h ../AST/SenchaObject.h \ + ../AST/to_string.h + +../Context.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: diff --git a/Sencha-lang/Debug/Parser.d b/Sencha-lang/Debug/Parser.d index 0dafb63..b99bfd6 100644 --- a/Sencha-lang/Debug/Parser.d +++ b/Sencha-lang/Debug/Parser.d @@ -1,22 +1,26 @@ -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.d: ../Parser.cpp ../Parser.h ../Token.h ../Context.h \ + ../AST/SenchaObject.h ../AST/to_string.h ../AST/AllTypesOfASTNodes.h \ + ../AST/ASTNode.h ../AST/SenchaObject.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: +../Context.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: + ../AST/AllTypesOfASTNodes.h: ../AST/ASTNode.h: ../AST/SenchaObject.h: -../AST/to_string.h: - ../AST/ProgramNode.h: ../AST/ASTStatement.h: diff --git a/Sencha-lang/Debug/Sencha-lang b/Sencha-lang/Debug/Sencha-lang Binary files differindex 0add6c8..fb7d9be 100755 --- a/Sencha-lang/Debug/Sencha-lang +++ b/Sencha-lang/Debug/Sencha-lang diff --git a/Sencha-lang/Debug/Tests/subdir.mk b/Sencha-lang/Debug/Tests/subdir.mk index c4239d3..f0dca8b 100644 --- a/Sencha-lang/Debug/Tests/subdir.mk +++ b/Sencha-lang/Debug/Tests/subdir.mk @@ -20,7 +20,7 @@ CPP_DEPS += \ Tests/%.o: ../Tests/%.cpp @echo 'Building file: $<' @echo 'Invoking: Cross G++ Compiler' - g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' diff --git a/Sencha-lang/Debug/main.d b/Sencha-lang/Debug/main.d index 6f48a87..0057093 100644 --- a/Sencha-lang/Debug/main.d +++ b/Sencha-lang/Debug/main.d @@ -1,10 +1,11 @@ -main.d: ../main.cpp ../Token.h ../Lexer.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 ../Tests/minunit.h ../Tests/../Lexer.h +main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h ../Context.h \ + ../AST/SenchaObject.h ../AST/to_string.h ../AST/AllTypesOfASTNodes.h \ + ../AST/ASTNode.h ../AST/SenchaObject.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: @@ -12,14 +13,18 @@ main.d: ../main.cpp ../Token.h ../Lexer.h ../Parser.h \ ../Parser.h: +../Context.h: + +../AST/SenchaObject.h: + +../AST/to_string.h: + ../AST/AllTypesOfASTNodes.h: ../AST/ASTNode.h: ../AST/SenchaObject.h: -../AST/to_string.h: - ../AST/ProgramNode.h: ../AST/ASTStatement.h: diff --git a/Sencha-lang/Debug/subdir.mk b/Sencha-lang/Debug/subdir.mk index bd71042..50fc5d7 100644 --- a/Sencha-lang/Debug/subdir.mk +++ b/Sencha-lang/Debug/subdir.mk @@ -4,6 +4,7 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../Context.cpp \ ../Lexer.cpp \ ../Object.cpp \ ../Parser.cpp \ @@ -11,6 +12,7 @@ CPP_SRCS += \ ../main.cpp OBJS += \ +./Context.o \ ./Lexer.o \ ./Object.o \ ./Parser.o \ @@ -18,6 +20,7 @@ OBJS += \ ./main.o CPP_DEPS += \ +./Context.d \ ./Lexer.d \ ./Object.d \ ./Parser.d \ @@ -29,7 +32,7 @@ CPP_DEPS += \ %.o: ../%.cpp @echo 'Building file: $<' @echo 'Invoking: Cross G++ Compiler' - g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" + g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=gnu++0x -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<" @echo 'Finished building: $<' @echo ' ' diff --git a/Sencha-lang/Lexer.cpp b/Sencha-lang/Lexer.cpp index 112af3b..7abb5e8 100644 --- a/Sencha-lang/Lexer.cpp +++ b/Sencha-lang/Lexer.cpp @@ -117,7 +117,7 @@ pair<string, Token> Lexer::parse_token(string line) bool Lexer::is_keyword(string value) { - for(int i=0; i< keywords.size(); i++) + for(unsigned int i=0; i< keywords.size(); i++) { if(value == keywords[i]) return true; } @@ -127,7 +127,7 @@ bool Lexer::is_keyword(string value) bool Lexer::is_punctuation(char c) { - for(int i=0; i< punctuation.size(); i++) + for(unsigned int i=0; i< punctuation.size(); i++) { if(c == punctuation[i]) return true; } @@ -136,7 +136,7 @@ bool Lexer::is_punctuation(char c) bool Lexer::is_operator(string value) { - for(int i=0; i< operators.size(); i++) + for(unsigned int i=0; i< operators.size(); i++) { if(value == operators[i]) return true; } @@ -155,7 +155,7 @@ type_of_token Lexer::guess_type(string value) if(isdigit(value[0])) { bool is_integer = true; - for(int i=1; i<value.size(); i++) + for(unsigned int i=1; i<value.size(); i++) { if(!isdigit(value[i])) is_integer = false; } diff --git a/Sencha-lang/Parser.cpp b/Sencha-lang/Parser.cpp index a4e1575..302706c 100644 --- a/Sencha-lang/Parser.cpp +++ b/Sencha-lang/Parser.cpp @@ -1,8 +1,9 @@ #include "Parser.h"
#include "iostream" -Parser::Parser()
-{ +Parser::Parser(Context * context)
+{
+ this->context = context; error_message = "***ERRORS DURING PARSING***\n"; report_message = "***PARSER REPORT***\n"; position_in_stream = 0;
@@ -10,6 +11,7 @@ Parser::Parser() program = static_cast<ProgramNode *>(tree.root);
}
+
Parser::~Parser()
{
//dtor
@@ -30,7 +32,7 @@ void Parser::erase_all() string Parser::show_tokens()
{
string tokens = "";
- for (int i = 0; i < token_stream.size(); i++)
+ for (unsigned int i = 0; i < token_stream.size(); i++)
{
tokens += token_stream[i].value + " ";
}
@@ -39,7 +41,7 @@ string Parser::show_tokens() void Parser::add_tokens(vector<Token> tokens)
{
- for (int i = 0; i < tokens.size(); i++)
+ for (unsigned int i = 0; i < tokens.size(); i++)
{
token_stream.push_back(tokens[i]);
}
@@ -113,18 +115,18 @@ bool Parser::expect(string s) } bool Parser::is_type() -{ - if(current_token.get_type() == t_symbol || current_token.get_type() == t_keyword) - { - if(tok_value == "def" || tok_value == "string" || tok_value == "num") - { - read_next(); - return true; - } - else return false; +{
+ if(tok_value == "def" || tok_value == "string" || tok_value == "num") + { + read_next(); + return true; + } + + else
+ {
+ return false;
} - else return false; } ASTStatement * Parser::statement(ASTNode * parent) @@ -142,24 +144,35 @@ ASTStatement * Parser::statement(ASTNode * parent) else if(is_type()) { -
- report("Identifier: " + tok_value + "\n");
-
+ std::string identifier = tok_value;
+ report("Identifier: " + identifier + "\n");
read_next();
if(accept("="))
{
- report("Identifier: " + tok_value + "\n");
- read_next();
- stat->add_expression(expr(stat));
- ;
+
+ ASTExpression * ae = expr(stat);
+
+ context->add(identifier, ae->evaluate());
+
+ stat->add_expression(ae);
+
report(" := \n");
+ if(accept(";"))
+ {
+
+ report("Variable definition\n");
+ return stat;
+ }
}
if(accept(";"))
{
+ cout << "dupa blada" << endl;
+ context->add(identifier, SenchaObject());
report("Variable definition\n");
+ stat->add_expression(new ConstantExpression(stat, SenchaObject(), identifier));
return stat;
}
@@ -186,12 +199,13 @@ ASTStatement * Parser::statement(ASTNode * parent) statement(stat);
report("function definition\n");
}
- }
+
else
{
expect(";");
report("function declaration\n");
}
+ }
} @@ -219,7 +233,8 @@ ASTStatement * Parser::statement(ASTNode * parent) } else - { + {
+ stat->add_expression(expr(stat)); while(!expect(";") && tok_value != "") read_next();
return stat; @@ -241,18 +256,23 @@ ASTExpression * Parser::prim_expr(ASTNode * expression) 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(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, name);
+ read_next();
} else if(accept("(")) {
@@ -433,12 +453,20 @@ ASTExpression * Parser::eq_expr(ASTNode * expression) ASTExpression * Parser::expr(ASTNode * expression) {
+
BasicExpression * be = new BasicExpression(expression);
- be->set_left_operand(eq_expr(be)); + ASTExpression * left = eq_expr(be);
+ SenchaObject left_value = left->evaluate();
+ be->set_left_operand(left); if(accept("=")) {
be->set_operator("=");
- be->set_right_operand(expr(be)); + ASTExpression * right = expr(be);
+ be->set_right_operand(right);
+ if(left_value.name != "")
+ {
+ context->set(left_value.name, right->evaluate());
+ } report(" :=\n"); }
diff --git a/Sencha-lang/Parser.h b/Sencha-lang/Parser.h index 714c91d..b5978dc 100644 --- a/Sencha-lang/Parser.h +++ b/Sencha-lang/Parser.h @@ -4,7 +4,7 @@ #include <vector>
#include <cstdlib> #include "Token.h"
-
+#include "Context.h"
#include "AST/AllTypesOfASTNodes.h" #include "AST/AST.h"
@@ -13,7 +13,9 @@ using namespace std; class Parser
{
public:
- Parser();
+ Parser(Context * context);
+ Context * context;
+
virtual ~Parser(); void interpret(); string report_message; @@ -30,9 +32,7 @@ class Parser Token current_token; string tok_value; vector<Token> token_stream; - int position_in_stream; -
-
+ unsigned int position_in_stream;
bool in_statement; bool read_next(); @@ -43,8 +43,7 @@ class Parser void error(string s); void report(string s); -
- //TODO change functions below to use AST nodes + ASTStatement * statement(ASTNode * node); ASTExpression * mul_expr(ASTNode * node); diff --git a/Sencha-lang/Tests/TestSuite.cpp b/Sencha-lang/Tests/TestSuite.cpp index 81a0f27..bb1a3b1 100644 --- a/Sencha-lang/Tests/TestSuite.cpp +++ b/Sencha-lang/Tests/TestSuite.cpp @@ -9,7 +9,6 @@ TestSuite::TestSuite() { - // TODO Auto-generated constructor stub tests_passed = 0; tests_failed = 0; tests = 0; diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index 3564ef0..7a8d031 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -4,18 +4,19 @@ #include "Lexer.h" #include "Parser.h" #include "Tests/TestLexer.h" +#include "Context.h" using namespace std;
void test_lexer() { - string test_line = "def i; bulb; i + 3; string banan; banan = \"banan and other stuff\"; string kaboom(num how_many_times) { def z; }"; + string test_line = "dupa"; string test_line2 = "def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree( num how_many) {return how_many -1; != <=}"; Lexer lexer; vector<Token> tokens = lexer.parse_line(test_line); - for(int i=0; i< tokens.size(); i++) + for(unsigned int i=0; i< tokens.size(); i++) { cout << tokens[i].get_value() << " type: " << tokens[i].get_type() << endl; } @@ -32,12 +33,13 @@ void test_parser() lines.push_back("num pun"); lines.push_back("def how_many_trees = 1; how_many_trees + 3 == 2; num cut_tree(num how_many) {return how_many -1}"); Lexer lexer; + Context context; vector<Token> tokens; - for(int i=0; i<lines.size(); i++) + for(unsigned int i=0; i<lines.size(); i++) { tokens = lexer.parse_line(lines[i]); - Parser parser; + Parser parser(&context); parser.add_tokens(tokens); parser.interpret(); cout << "<<<Parsing number: " << i << " >>>" << endl; @@ -52,7 +54,7 @@ void test_parser() int how_depth_change(vector<Token> tokens) { int change = 0; - for (int i = 0; i < tokens.size(); i++) + for (unsigned int i = 0; i < tokens.size(); i++) { if(tokens[i].value == "{") change++; else if(tokens[i].value == "}") change--; @@ -73,7 +75,9 @@ string compute_indentation(int depth_level) void interactive() { Lexer lexer; - Parser parser; + + Context context; + Parser parser(&context); vector<Token> tokens; @@ -94,23 +98,21 @@ void interactive() if(level_of_depth == 0) { parser.interpret(); - cout << parser.report_message << endl; - cout << parser.error_message << endl; - cout << parser.show_tokens() << endl; - cout << "My tree:\n"; + //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; + //cout << parser.context->debug(); + parser.program->execute_last(); } - } - } int main()
{
- cout << "Sencha-lang interpreter, version 0.02" << endl; + cout << "Sencha-lang interpreter, version 0.12" << endl; TestLexer test_l; //test_l.run_tests(); |