Multiple lines in interactive mode.
parent
8fbc6c1b7d
commit
10bf1865c5
|
@ -4,15 +4,16 @@
|
|||
* Created on: Nov 4, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#include "AST.h"
|
||||
#include "AllTypesOfASTNodes.h"
|
||||
|
||||
AST::AST() {
|
||||
// TODO Auto-generated constructor stub
|
||||
|
||||
root = new ASTProgram();
|
||||
current_node = root;
|
||||
number_of_nodes = 1;
|
||||
level_of_depth = 0;
|
||||
}
|
||||
|
||||
AST::~AST() {
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,16 @@
|
|||
|
||||
#ifndef AST_H_
|
||||
#define AST_H_
|
||||
#include "ASTNode.h"
|
||||
|
||||
class AST {
|
||||
public:
|
||||
AST();
|
||||
ASTNode * root;
|
||||
ASTNode * current_node;
|
||||
int number_of_nodes;
|
||||
int level_of_depth;
|
||||
ASTNode * add_node(ASTNode * node);
|
||||
virtual ~AST();
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* ASTProgram.cpp
|
||||
*
|
||||
* Created on: Nov 5, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#include "ASTProgram.h"
|
||||
|
||||
ASTProgram::ASTProgram() {
|
||||
// TODO Auto-generated constructor stub
|
||||
|
||||
}
|
||||
|
||||
ASTProgram::~ASTProgram() {
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
void ASTProgram::execute() {
|
||||
std::cout << "Program started!\n";
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* ASTProgram.h
|
||||
*
|
||||
* Created on: Nov 5, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#ifndef ASTPROGRAM_H_
|
||||
#define ASTPROGRAM_H_
|
||||
#include <iostream>
|
||||
#include "ASTNode.h"
|
||||
#include "ASTStatement.h"
|
||||
|
||||
class ASTProgram : public ASTNode {
|
||||
public:
|
||||
ASTProgram();
|
||||
ASTStatement * add_child(ASTStatement * node);
|
||||
virtual ~ASTProgram();
|
||||
virtual void execute();
|
||||
};
|
||||
|
||||
#endif /* ASTPROGRAM_H_ */
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* AllTypesOfASTNodes.h
|
||||
*
|
||||
* Created on: Nov 5, 2012
|
||||
* Author: attero
|
||||
*/
|
||||
|
||||
#ifndef ALLTYPESOFASTNODES_H_
|
||||
#define ALLTYPESOFASTNODES_H_
|
||||
|
||||
#include "ASTNode.h"
|
||||
#include "ASTProgram.h"
|
||||
#include "ASTStatement.h"
|
||||
#include "ASTExpression.h"
|
||||
#include "ASTPrimary.h"
|
||||
|
||||
//And probably more
|
||||
//TODO actualize it
|
||||
|
||||
#endif /* ALLTYPESOFASTNODES_H_ */
|
|
@ -1,3 +1,16 @@
|
|||
AST.d: ../AST.cpp ../AST.h
|
||||
AST.d: ../AST.cpp ../AST.h ../ASTNode.h ../AllTypesOfASTNodes.h \
|
||||
../ASTProgram.h ../ASTStatement.h ../ASTExpression.h ../ASTPrimary.h
|
||||
|
||||
../AST.h:
|
||||
|
||||
../ASTNode.h:
|
||||
|
||||
../AllTypesOfASTNodes.h:
|
||||
|
||||
../ASTProgram.h:
|
||||
|
||||
../ASTStatement.h:
|
||||
|
||||
../ASTExpression.h:
|
||||
|
||||
../ASTPrimary.h:
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,8 @@
|
|||
ASTProgram.d: ../ASTProgram.cpp ../ASTProgram.h ../ASTNode.h \
|
||||
../ASTStatement.h
|
||||
|
||||
../ASTProgram.h:
|
||||
|
||||
../ASTNode.h:
|
||||
|
||||
../ASTStatement.h:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -8,6 +8,7 @@ CPP_SRCS += \
|
|||
../ASTExpression.cpp \
|
||||
../ASTNode.cpp \
|
||||
../ASTPrimary.cpp \
|
||||
../ASTProgram.cpp \
|
||||
../ASTStatement.cpp \
|
||||
../Lexer.cpp \
|
||||
../Object.cpp \
|
||||
|
@ -20,6 +21,7 @@ OBJS += \
|
|||
./ASTExpression.o \
|
||||
./ASTNode.o \
|
||||
./ASTPrimary.o \
|
||||
./ASTProgram.o \
|
||||
./ASTStatement.o \
|
||||
./Lexer.o \
|
||||
./Object.o \
|
||||
|
@ -32,6 +34,7 @@ CPP_DEPS += \
|
|||
./ASTExpression.d \
|
||||
./ASTNode.d \
|
||||
./ASTPrimary.d \
|
||||
./ASTProgram.d \
|
||||
./ASTStatement.d \
|
||||
./Lexer.d \
|
||||
./Object.d \
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "Token.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Lexer
|
||||
{
|
||||
public:
|
||||
|
|
|
@ -1,19 +1,26 @@
|
|||
#include "Parser.h"
|
||||
#include "iostream"
|
||||
|
||||
Parser::Parser(vector<Token> tokens)
|
||||
Parser::Parser()
|
||||
{
|
||||
error_message = "***ERRORS DURING PARSING***\n";
|
||||
report_message = "***PARSER REPORT***\n";
|
||||
token_stream = tokens;
|
||||
position_in_stream = 0;
|
||||
read_next();
|
||||
position_in_stream = 0;
|
||||
}
|
||||
|
||||
Parser::~Parser()
|
||||
{
|
||||
//dtor
|
||||
}
|
||||
|
||||
void Parser::add_tokens(vector<Token> tokens)
|
||||
{
|
||||
for (int i = 0; i < tokens.size(); i++)
|
||||
{
|
||||
token_stream.push_back(tokens[i]);
|
||||
}
|
||||
read_next();
|
||||
}
|
||||
|
||||
void Parser::report(string s)
|
||||
{
|
||||
|
@ -94,7 +101,9 @@ void Parser::interpret()
|
|||
report("Regular statement:\n");
|
||||
statement();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Parser::peek(string s)
|
||||
|
|
|
@ -9,12 +9,12 @@ using namespace std;
|
|||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser(vector<Token> tokens);
|
||||
Parser();
|
||||
virtual ~Parser();
|
||||
void interpret();
|
||||
string report_message;
|
||||
string error_message;
|
||||
|
||||
void add_tokens(vector<Token> tokens);
|
||||
|
||||
protected:
|
||||
private:
|
||||
|
@ -22,6 +22,7 @@ class Parser
|
|||
string tok_value;
|
||||
vector<Token> token_stream;
|
||||
int position_in_stream;
|
||||
|
||||
|
||||
bool read_next();
|
||||
bool peek(string s);
|
||||
|
|
|
@ -36,7 +36,8 @@ void test_parser()
|
|||
for(int i=0; i<lines.size(); i++)
|
||||
{
|
||||
tokens = lexer.parse_line(lines[i]);
|
||||
Parser parser = Parser(tokens);
|
||||
Parser parser;
|
||||
parser.add_tokens(tokens);
|
||||
parser.interpret();
|
||||
cout << "<<<Parsing number: " << i << " >>>" << endl;
|
||||
cout << "Instructions: " << endl ;
|
||||
|
@ -47,23 +48,44 @@ void test_parser()
|
|||
|
||||
}
|
||||
|
||||
|
||||
void interactive()
|
||||
{
|
||||
Lexer lexer;
|
||||
Parser parser;
|
||||
|
||||
vector<Token> tokens;
|
||||
|
||||
string input = "start";
|
||||
string input;
|
||||
int level_of_depth = 0;
|
||||
string indentation = "";
|
||||
while(true)
|
||||
{
|
||||
cout << ">> ";
|
||||
cout << ">> " + indentation;
|
||||
getline(cin, input);
|
||||
if(input == "quit()") break;
|
||||
tokens = lexer.parse_line(input);
|
||||
Parser parser(tokens);
|
||||
parser.interpret();
|
||||
cout << parser.report_message;
|
||||
cout << parser.error_message << endl;
|
||||
if(tokens[tokens.size() -1].value == "{")
|
||||
{
|
||||
level_of_depth++;
|
||||
indentation += " ";
|
||||
}
|
||||
else if(tokens[tokens.size() -1].value == "}")
|
||||
{
|
||||
level_of_depth--;
|
||||
indentation = indentation.substr(0,indentation.size()-4); //TODO see if it can be nicer
|
||||
}
|
||||
|
||||
parser.add_tokens(tokens);
|
||||
if(level_of_depth == 0) {
|
||||
parser.interpret();
|
||||
cout << parser.report_message;
|
||||
cout << parser.error_message << endl;
|
||||
}
|
||||
else if(level_of_depth < 0)
|
||||
{
|
||||
cout << "Something really nasty happend, breaking" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,7 +93,7 @@ void interactive()
|
|||
|
||||
int main()
|
||||
{
|
||||
cout << "Sencha-lang intrepreter, version 0.02" << endl;
|
||||
cout << "Sencha-lang interpreter, version 0.02" << endl;
|
||||
|
||||
TestLexer test_l;
|
||||
//test_l.run_tests();
|
||||
|
|
Reference in New Issue