summaryrefslogtreecommitdiffstats
path: root/Sencha-lang/main.cpp
blob: 6269f0adf971bf9e04d564ed2b2ab70b574688d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <fstream>
#include <iostream>
#include <string>
#include <math.h>
#include "Token.h"
#include "Lexer.h"
#include "Parser.h"
#include "Tests/TestLexer.h"
#include "Context.h"

using namespace std;


/*TODO
 * Known bugs:
 *
 * >> print("I like bananas", dupa");
I like bananasnull
Context: 0: dupa"); type: null
null

dupa"); declaration? hello? It's probably temporary effect of searching for this name in variable tables

>>
 */

SenchaObject print(vector<ASTExpression *> arguments)
{
		for (auto argument: arguments)
		{
			auto value = argument->evaluate();

			std::cout  << value.str();

		}
		std::cout << std::endl;
		return SenchaObject();
}

SenchaObject s_sin(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject sin_argument = arguments[0]->evaluate();
	if(sin_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(sin(sin_argument.integer));
	}
	else if(sin_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(sin(sin_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

SenchaObject s_cos(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject cos_argument = arguments[0]->evaluate();
	if(cos_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(cos(cos_argument.integer));
	}
	else if(cos_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(cos(cos_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

SenchaObject s_tan(vector<ASTExpression *> arguments)
{
	SenchaObject result;
	SenchaObject tan_argument = arguments[0]->evaluate();
	if(tan_argument.type == SenchaObject::integer_number)
	{
		result = SenchaObject(tan(tan_argument.integer));
	}
	else if(tan_argument.type == SenchaObject::float_number)
	{
		result = SenchaObject(tan(tan_argument.number));
	}
	else
	{
		result.type = SenchaObject::invalid;
	}

	return result;
}

void test_lexer()
{
    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(unsigned 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; }");
    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(unsigned int i=0; i<lines.size(); i++)
    {
        tokens = lexer.parse_line(lines[i]);
        Parser parser(&context);
        parser.add_tokens(tokens);
        parser.interpret();
        cout << "<<<Parsing number: " << i << " >>>" << endl;
        cout << "Instructions: " << endl ;
        cout << lines[i] << endl << endl;
        cout << parser.report_message;
        cout << parser.error_message << endl;
    }

}

int how_depth_change(vector<Token> tokens)
{
	int change = 0;
	for (unsigned 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;
}

void interactive()
{
	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 input;
	int level_of_depth = 0;
	string indentation = "";
	while(true)
	{
		cout << ">> " + indentation;
		getline(cin, input);
		if(input == "quit()") break;
		tokens = lexer.parse_line(input);

		level_of_depth += how_depth_change(tokens);
		indentation = compute_indentation(level_of_depth);

		parser.add_tokens(tokens);

		if(level_of_depth == 0) {
			parser.interpret();
			parser.program->execute_last();
			//cout << parser.report_message << endl;
			//cout << parser.error_message << endl;
			//cout << parser.show_tokens() << endl;
			//cout << "My tree:\n";
			//cout << parser.program->debug();
			cout << parser.context->debug();
		}
	}

}

int main(int argc, char *argv[])
{
	if(argc <= 1)
	{
		//TestLexer test_l;
		//test_l.run_tests();


		//test_parser();
		//test_lexer();
		cout << "Sencha-lang interpreter, version 0.12" << endl;
		interactive();
	}
	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() )
			{
				getline (input_file,line);
				tokens = lexer.parse_line(line);
				parser.add_tokens(tokens);
			}
			input_file.close();
		}

		parser.interpret();
		parser.program->execute();
	}

	return 0;
}