2012-11-02 23:07:00 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include "Token.h"
|
|
|
|
#include "Lexer.h"
|
|
|
|
#include "Parser.h"
|
2012-11-04 12:18:26 +00:00
|
|
|
#include "Tests/TestLexer.h"
|
2012-11-02 23:07:00 +00:00
|
|
|
|
|
|
|
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_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++)
|
|
|
|
{
|
|
|
|
cout << tokens[i].get_value() << " type: " << tokens[i].get_type() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tokens = lexer.parse_line(test_line2);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_parser()
|
|
|
|
{
|
|
|
|
vector<string> lines;
|
|
|
|
lines.push_back("def i; bulb; i + 3; string banan = \"kartofel\"; banan = \"banan\"; string kaboom(num how_many_times) { def z; }");
|
2012-11-04 15:21:51 +00:00
|
|
|
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}");
|
2012-11-02 23:07:00 +00:00
|
|
|
Lexer lexer;
|
|
|
|
vector<Token> tokens;
|
2012-11-18 13:44:46 +00:00
|
|
|
|
2012-11-02 23:07:00 +00:00
|
|
|
for(int i=0; i<lines.size(); i++)
|
|
|
|
{
|
|
|
|
tokens = lexer.parse_line(lines[i]);
|
2012-11-08 20:16:00 +00:00
|
|
|
Parser parser;
|
|
|
|
parser.add_tokens(tokens);
|
2012-11-02 23:07:00 +00:00
|
|
|
parser.interpret();
|
|
|
|
cout << "<<<Parsing number: " << i << " >>>" << endl;
|
|
|
|
cout << "Instructions: " << endl ;
|
|
|
|
cout << lines[i] << endl << endl;
|
|
|
|
cout << parser.report_message;
|
|
|
|
cout << parser.error_message << endl;
|
|
|
|
}
|
|
|
|
|
2012-11-04 15:21:51 +00:00
|
|
|
}
|
|
|
|
|
2012-11-18 13:44:46 +00:00
|
|
|
int how_depth_change(vector<Token> tokens)
|
|
|
|
{
|
|
|
|
int change = 0;
|
|
|
|
for (int i = 0; i < tokens.size(); i++)
|
|
|
|
{
|
|
|
|
if(tokens[i].value == "{") change++;
|
|
|
|
else if(tokens[i].value == "}") change--;
|
|
|
|
}
|
|
|
|
return change;
|
|
|
|
}
|
|
|
|
|
|
|
|
string compute_indentation(int depth_level)
|
|
|
|
{
|
|
|
|
string indent = "";
|
|
|
|
for(int i=0; i< depth_level; i++)
|
|
|
|
{
|
|
|
|
indent += " ";
|
|
|
|
}
|
|
|
|
return indent;
|
|
|
|
}
|
2012-11-08 20:16:00 +00:00
|
|
|
|
2012-11-04 15:21:51 +00:00
|
|
|
void interactive()
|
|
|
|
{
|
|
|
|
Lexer lexer;
|
2012-11-08 20:16:00 +00:00
|
|
|
Parser parser;
|
2012-11-04 15:21:51 +00:00
|
|
|
|
|
|
|
vector<Token> tokens;
|
|
|
|
|
2012-11-08 20:16:00 +00:00
|
|
|
string input;
|
|
|
|
int level_of_depth = 0;
|
|
|
|
string indentation = "";
|
2012-11-05 13:17:38 +00:00
|
|
|
while(true)
|
2012-11-04 15:21:51 +00:00
|
|
|
{
|
2012-11-08 20:16:00 +00:00
|
|
|
cout << ">> " + indentation;
|
2012-11-04 15:21:51 +00:00
|
|
|
getline(cin, input);
|
2012-11-05 13:17:38 +00:00
|
|
|
if(input == "quit()") break;
|
2012-11-04 15:21:51 +00:00
|
|
|
tokens = lexer.parse_line(input);
|
2012-11-18 13:44:46 +00:00
|
|
|
|
|
|
|
level_of_depth += how_depth_change(tokens);
|
|
|
|
indentation = compute_indentation(level_of_depth);
|
2012-11-08 20:16:00 +00:00
|
|
|
|
|
|
|
parser.add_tokens(tokens);
|
2012-11-18 13:44:46 +00:00
|
|
|
|
|
|
|
if(level_of_depth == 0) {
|
2012-11-08 20:16:00 +00:00
|
|
|
parser.interpret();
|
2012-11-17 15:54:16 +00:00
|
|
|
cout << parser.report_message << endl;
|
2012-11-08 20:16:00 +00:00
|
|
|
cout << parser.error_message << endl;
|
2012-11-17 15:54:16 +00:00
|
|
|
cout << parser.show_tokens() << endl;
|
2012-11-08 20:16:00 +00:00
|
|
|
}
|
2012-11-18 13:44:46 +00:00
|
|
|
|
2012-11-04 15:21:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-02 23:07:00 +00:00
|
|
|
int main()
|
|
|
|
{
|
2012-11-08 20:16:00 +00:00
|
|
|
cout << "Sencha-lang interpreter, version 0.02" << endl;
|
2012-11-04 12:18:26 +00:00
|
|
|
|
2012-11-04 13:26:36 +00:00
|
|
|
TestLexer test_l;
|
2012-11-04 15:21:51 +00:00
|
|
|
//test_l.run_tests();
|
2012-11-04 13:26:36 +00:00
|
|
|
|
2012-11-04 12:18:26 +00:00
|
|
|
//test_parser();
|
2012-11-04 15:21:51 +00:00
|
|
|
//test_lexer();
|
|
|
|
interactive();
|
2012-11-02 23:07:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|