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 :).functions
parent
442d8c640f
commit
736458f1fd
|
@ -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"/>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -102,6 +102,10 @@ BasicExpression::~BasicExpression() {
|
|||
|
||||
}
|
||||
|
||||
void BasicExpression::accept(Visitor * visitor)
|
||||
{
|
||||
visitor->visit(this);
|
||||
}
|
||||
|
||||
std::string BasicExpression::debug()
|
||||
{
|
||||
|
|
|
@ -24,7 +24,7 @@ public:
|
|||
|
||||
|
||||
std::string debug() ;
|
||||
void accept(Visitor * visitor);
|
||||
virtual void accept(Visitor * visitor);
|
||||
|
||||
BasicExpression(ASTNode * parent);
|
||||
virtual ~BasicExpression();
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -66,7 +66,7 @@ public:
|
|||
std::string debug();
|
||||
|
||||
/** Implements visitor pattern */
|
||||
void accept(Visitor * visitor);
|
||||
virtual void accept(Visitor * visitor);
|
||||
|
||||
virtual ~ConstantExpression();
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -19,6 +19,7 @@ public:
|
|||
virtual ~ProgramNode();
|
||||
virtual void execute();
|
||||
virtual void execute_last();
|
||||
virtual void accept(Visitor * visitor);
|
||||
};
|
||||
|
||||
#endif /* PROGRAMNODE_H_ */
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 = "";
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -16,3 +16,12 @@ Visitor::~Visitor() {
|
|||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
Visitable::Visitable()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Visitable::~Visitable()
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -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_ */
|
||||
|
|
|
@ -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);
|
||||
std::string argument1 = argv[1];
|
||||
if(argument1 == "--test") {
|
||||
|
||||
Parser parser(&context);
|
||||
vector<Token> tokens;
|
||||
string line;
|
||||
ifstream input_file (name);
|
||||
|
||||
if (input_file.is_open())
|
||||
{
|
||||
while ( input_file.good() )
|
||||
{
|
||||
getline (input_file,line);
|
||||
tokens = lexer.parse_line(line);
|
||||
parser.add_tokens(tokens);
|
||||
}
|
||||
input_file.close();
|
||||
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.interpret();
|
||||
parser.program->execute();
|
||||
Parser parser(&context);
|
||||
vector<Token> tokens;
|
||||
string line;
|
||||
ifstream input_file (name);
|
||||
|
||||
if (input_file.is_open())
|
||||
{
|
||||
while ( input_file.good() )
|
||||
{
|
||||
getline (input_file,line);
|
||||
tokens = lexer.parse_line(line);
|
||||
parser.add_tokens(tokens);
|
||||
}
|
||||
input_file.close();
|
||||
}
|
||||
|
||||
parser.interpret();
|
||||
parser.program->execute();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue