diff options
author | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-18 22:41:39 +0100 |
---|---|---|
committer | Justyna Ilczuk <justyna.ilczuk@gmail.com> | 2012-12-18 22:41:39 +0100 |
commit | 736458f1fd5508b48602feae215749d2db73ebc5 (patch) | |
tree | dd56f48a1b7e3e8eb171cc6344240b4208e92c33 /Sencha-lang | |
parent | 442d8c640fdde22d9f6bf5d1a729d8e17e2fa407 (diff) | |
download | sencha-lang-736458f1fd5508b48602feae215749d2db73ebc5.tar.gz sencha-lang-736458f1fd5508b48602feae215749d2db73ebc5.tar.bz2 sencha-lang-736458f1fd5508b48602feae215749d2db73ebc5.tar.xz sencha-lang-736458f1fd5508b48602feae215749d2db73ebc5.zip |
This crazy setup compiles. However I encountered some problems with
polymorphism. It probably doesn't work as I thought it would. Not
problem at all :).
Diffstat (limited to 'Sencha-lang')
-rw-r--r-- | Sencha-lang/.cproject | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/ASTNode.h | 4 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicExpression.cpp | 4 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicExpression.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/BasicStatement.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/ConstantExpression.h | 2 | ||||
-rw-r--r-- | Sencha-lang/AST/ProgramNode.cpp | 7 | ||||
-rw-r--r-- | Sencha-lang/AST/ProgramNode.h | 1 | ||||
-rw-r--r-- | Sencha-lang/ASTInspector.cpp | 3 | ||||
-rw-r--r-- | Sencha-lang/ASTInspector.h | 2 | ||||
-rw-r--r-- | Sencha-lang/Tests/TestASTInspector.cpp | 5 | ||||
-rw-r--r-- | Sencha-lang/Tests/tests.h | 11 | ||||
-rw-r--r-- | Sencha-lang/Visitor.cpp | 9 | ||||
-rw-r--r-- | Sencha-lang/Visitor.h | 19 | ||||
-rw-r--r-- | Sencha-lang/main.cpp | 59 |
15 files changed, 91 insertions, 41 deletions
diff --git a/Sencha-lang/.cproject b/Sencha-lang/.cproject index a2fe504..7335ba0 100644 --- a/Sencha-lang/.cproject +++ b/Sencha-lang/.cproject @@ -18,7 +18,7 @@ <folderInfo id="cdt.managedbuild.config.gnu.cross.exe.debug.1544392496." name="/" resourcePath=""> <toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.debug.2045041315" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.debug"> <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.1733237476" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/> - <builder buildPath="${workspace_loc:/Sencha-lang/Debug}" id="cdt.managedbuild.builder.gnu.cross.2091580062" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/> + <builder buildPath="${workspace_loc:/Sencha-lang/Debug}" id="cdt.managedbuild.builder.gnu.cross.2091580062" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" parallelBuildOn="true" parallelizationNumber="optimal" superClass="cdt.managedbuild.builder.gnu.cross"/> <tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.1055356392" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler"> <option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.758256537" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" valueType="enumerated"/> <option id="gnu.c.compiler.option.debugging.level.1319397326" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" value="gnu.c.debugging.level.max" valueType="enumerated"/> diff --git a/Sencha-lang/AST/ASTNode.h b/Sencha-lang/AST/ASTNode.h index feee8bd..6a16f96 100644 --- a/Sencha-lang/AST/ASTNode.h +++ b/Sencha-lang/AST/ASTNode.h @@ -12,13 +12,13 @@ #include "SenchaObject.h" #include "../Visitor.h" -class ASTNode { +class ASTNode : public Visitable{ public: ASTNode(); ASTNode * parent; std::vector<ASTNode *> children; - virtual void accept(Visitor *) = 0; + virtual void accept(Visitor * visitor){ visitor->visit(this); }; virtual std::string debug() = 0; virtual void execute() = 0; diff --git a/Sencha-lang/AST/BasicExpression.cpp b/Sencha-lang/AST/BasicExpression.cpp index 46a9d75..d5bf0f3 100644 --- a/Sencha-lang/AST/BasicExpression.cpp +++ b/Sencha-lang/AST/BasicExpression.cpp @@ -102,6 +102,10 @@ BasicExpression::~BasicExpression() { } +void BasicExpression::accept(Visitor * visitor) +{ + visitor->visit(this); +} std::string BasicExpression::debug() { diff --git a/Sencha-lang/AST/BasicExpression.h b/Sencha-lang/AST/BasicExpression.h index c563f1d..cdb0b68 100644 --- a/Sencha-lang/AST/BasicExpression.h +++ b/Sencha-lang/AST/BasicExpression.h @@ -24,7 +24,7 @@ public: std::string debug() ; - void accept(Visitor * visitor); + virtual void accept(Visitor * visitor); BasicExpression(ASTNode * parent); virtual ~BasicExpression(); diff --git a/Sencha-lang/AST/BasicStatement.h b/Sencha-lang/AST/BasicStatement.h index a34c4e7..1b616dc 100644 --- a/Sencha-lang/AST/BasicStatement.h +++ b/Sencha-lang/AST/BasicStatement.h @@ -10,6 +10,7 @@ #include <iostream> #include "ASTExpression.h" #include "ASTStatement.h" +#include "../Visitor.h" class BasicStatement : public ASTStatement { public: @@ -17,6 +18,7 @@ public: virtual std::string debug(); void add_expression(ASTExpression * expr); virtual void execute(); + virtual void accept(Visitor * vistitor); virtual ~BasicStatement(); }; diff --git a/Sencha-lang/AST/ConstantExpression.h b/Sencha-lang/AST/ConstantExpression.h index 5529c72..a728efd 100644 --- a/Sencha-lang/AST/ConstantExpression.h +++ b/Sencha-lang/AST/ConstantExpression.h @@ -66,7 +66,7 @@ public: std::string debug(); /** Implements visitor pattern */ - void accept(Visitor * visitor); + virtual void accept(Visitor * visitor); virtual ~ConstantExpression(); diff --git a/Sencha-lang/AST/ProgramNode.cpp b/Sencha-lang/AST/ProgramNode.cpp index 9d61a4d..aced5bf 100644 --- a/Sencha-lang/AST/ProgramNode.cpp +++ b/Sencha-lang/AST/ProgramNode.cpp @@ -26,9 +26,14 @@ void ProgramNode::execute() { } } +void ProgramNode::accept(Visitor * visitor) +{ + visitor->visit(this); +} + void ProgramNode::execute_last() { - children[children.size() -1]->execute(); + children[children.size() - 1]->execute(); } std::string ProgramNode::debug() diff --git a/Sencha-lang/AST/ProgramNode.h b/Sencha-lang/AST/ProgramNode.h index 1232699..80425df 100644 --- a/Sencha-lang/AST/ProgramNode.h +++ b/Sencha-lang/AST/ProgramNode.h @@ -19,6 +19,7 @@ public: virtual ~ProgramNode(); virtual void execute(); virtual void execute_last(); + virtual void accept(Visitor * visitor); }; #endif /* PROGRAMNODE_H_ */ diff --git a/Sencha-lang/ASTInspector.cpp b/Sencha-lang/ASTInspector.cpp index 656a6c5..d867eab 100644 --- a/Sencha-lang/ASTInspector.cpp +++ b/Sencha-lang/ASTInspector.cpp @@ -16,7 +16,7 @@ ASTInspector::~ASTInspector() { // TODO Auto-generated destructor stub } -void ASTInspector::visit(ASTNode * node) +void ASTInspector::visit(Visitable * node) { } @@ -33,6 +33,7 @@ void ASTInspector::visit(ConstantExpression * constant_expression) void ASTInspector::visit(BasicExpression * basic_expression) { + std::cout << "Inspector visits basic expression!" << std::endl; depth_level++; std::string visit_notes = ""; visit_notes += "Basic expression:\n"; diff --git a/Sencha-lang/ASTInspector.h b/Sencha-lang/ASTInspector.h index 41fad8f..c11db14 100644 --- a/Sencha-lang/ASTInspector.h +++ b/Sencha-lang/ASTInspector.h @@ -23,7 +23,7 @@ public: unsigned int depth_level; - void visit(ASTNode * node); + void visit(Visitable * node); void visit(ConstantExpression * node); void visit(BasicExpression * node); void visit(PostfixExpression * node); diff --git a/Sencha-lang/Tests/TestASTInspector.cpp b/Sencha-lang/Tests/TestASTInspector.cpp index 4194dbe..0d4c9d1 100644 --- a/Sencha-lang/Tests/TestASTInspector.cpp +++ b/Sencha-lang/Tests/TestASTInspector.cpp @@ -25,14 +25,15 @@ std::string TestASTInspector::test_inspecting_basic_expression() BasicExpression * be = build_basic_expression("+", SenchaObject(9), SenchaObject(122)); be->accept(&inspector); std::string assert_report = "Report: " + inspector.inspection_report; - muu_assert("dd", inspector.inspection_report == "tralala"); + muu_assert("dd", inspector.inspection_report == ""); + std::cout << assert_report << " reports..." << std::endl; return test_report; } -virtual std::string TestASTInspector::all_tests() +std::string TestASTInspector::all_tests() { std::string test_report = ""; diff --git a/Sencha-lang/Tests/tests.h b/Sencha-lang/Tests/tests.h index 0fb7aa3..6282788 100644 --- a/Sencha-lang/Tests/tests.h +++ b/Sencha-lang/Tests/tests.h @@ -8,8 +8,11 @@ #ifndef TESTS_H_ #define TESTS_H_ #include <string> -#include "Lexer.h" -#include "Parser.h" +#include <iostream> +#include "TestLexer.h" +#include "TestASTInspector.h" +#include "../Lexer.h" +#include "../Parser.h" using namespace std; @@ -62,6 +65,9 @@ void run_test_suites() TestLexer test_l; test_l.run_tests(); + TestASTInspector test_inspector; + test_inspector.run_tests(); + } @@ -69,6 +75,7 @@ void run_test_suites() void run_tests() { run_test_suites(); + cout << "derp!" << endl; } diff --git a/Sencha-lang/Visitor.cpp b/Sencha-lang/Visitor.cpp index 1fa2479..4f85116 100644 --- a/Sencha-lang/Visitor.cpp +++ b/Sencha-lang/Visitor.cpp @@ -16,3 +16,12 @@ Visitor::~Visitor() { // TODO Auto-generated destructor stub } +Visitable::Visitable() +{ + +} + +Visitable::~Visitable() +{ + +} diff --git a/Sencha-lang/Visitor.h b/Sencha-lang/Visitor.h index 85d4dfa..52d9b05 100644 --- a/Sencha-lang/Visitor.h +++ b/Sencha-lang/Visitor.h @@ -7,13 +7,28 @@ #ifndef VISITOR_H_ #define VISITOR_H_ -#include "AST/ASTNode.h" + + +class Visitable; class Visitor { public: Visitor(); - virtual void visit(ASTNode * node/*something*/) = 0; + virtual void visit(Visitable * node) = 0; virtual ~Visitor(); }; +class Visitable +{ +public: + Visitable(); + virtual void accept(Visitor * visitor) = 0; + virtual ~Visitable(); + +}; + + + + + #endif /* VISITOR_H_ */ diff --git a/Sencha-lang/main.cpp b/Sencha-lang/main.cpp index baf455d..64e0de5 100644 --- a/Sencha-lang/main.cpp +++ b/Sencha-lang/main.cpp @@ -170,37 +170,42 @@ int main(int argc, char *argv[]) cout << "Sencha-lang interpreter, version 0.12" << endl; interactive(); } - else if(argv[1] == "--test") + else { - run_tests(); - } - else { - auto name = argv[1]; - Lexer lexer; - Context context; - context.register_function("print", print); - context.register_function("sin", s_sin); - context.register_function("cos", s_cos); - context.register_function("tan", s_tan); - - Parser parser(&context); - vector<Token> tokens; - string line; - ifstream input_file (name); - - if (input_file.is_open()) - { - while ( input_file.good() ) + std::string argument1 = argv[1]; + if(argument1 == "--test") { + + cout << "I'm running tests" << endl; + run_tests(); + } + else { + auto name = argument1; + Lexer lexer; + Context context; + context.register_function("print", print); + context.register_function("sin", s_sin); + context.register_function("cos", s_cos); + context.register_function("tan", s_tan); + + Parser parser(&context); + vector<Token> tokens; + string line; + ifstream input_file (name); + + if (input_file.is_open()) { - getline (input_file,line); - tokens = lexer.parse_line(line); - parser.add_tokens(tokens); + while ( input_file.good() ) + { + getline (input_file,line); + tokens = lexer.parse_line(line); + parser.add_tokens(tokens); + } + input_file.close(); } - input_file.close(); - } - parser.interpret(); - parser.program->execute(); + parser.interpret(); + parser.program->execute(); + } } return 0;
|