-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
246 lines (218 loc) · 8.7 KB
/
main.cpp
File metadata and controls
246 lines (218 loc) · 8.7 KB
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
#include "tests/TestsExpression.h"
#include "tests/TestsInterpreter.h"
#include "tests/TestsLexer.h"
#include "tests/TestsList.h"
#include "tests/TestsParser.h"
#include "tests/TestsStatement.h"
#include "tests/TestsToken.h"
#include "src/utils/FileReader.h"
#include "src/utils/REPL.h"
#include "src/interpreter/Interpreter/Interpreter.h"
#include "src/lexer/Lexer/Lexer.h"
#include "src/parser/Parser/Parser.h"
#include "src/native/InputOutput.h"
#include "src/native/Network.h"
#include "src/native/String.h"
#include "src/native/System.h"
#include "src/native/Time.h"
void registerNatives(Interpreter &interpreter) {
// Time.h
interpreter.addNativeFunction("__native_time", __nativeTime);
interpreter.addNativeFunction("__native_time_double", __nativeTimeDouble);
interpreter.addNativeFunction("__native_sleep", __nativeSleep);
interpreter.addNativeFunction("__native_sleep_double", __nativeSleepDouble);
interpreter.addNativeFunction("__native_ctime", __nativeCTime);
// InputOutput.h
interpreter.addNativeFunction("__native_print", __nativePrint);
interpreter.addNativeFunction("__native_input_string", __nativeInputString);
interpreter.addNativeFunction("__native_input_int", __nativeInputInt);
interpreter.addNativeFunction("__native_input_double", __nativeInputDouble);
interpreter.addNativeFunction("__native_file_read", __nativeFileRead);
interpreter.addNativeFunction("__native_file_write", __nativeFileWrite);
interpreter.addNativeFunction("__native_file_append", __nativeFileAppend);
interpreter.addNativeFunction("__native_read_stdin", __nativeReadStdin);
interpreter.addNativeFunction("__native_write_stdout", __nativeWriteStdout);
interpreter.addNativeFunction("__native_clear_console", __nativeClearConsole);
interpreter.addNativeFunction("__native_set_cursor",
__nativeSetCursorPosition);
interpreter.addNativeFunction("__native_get_cursor",
__nativeGetCursorPosition);
// System.h
interpreter.addNativeFunction("__native_exec_cmd", __nativeExecCmd);
interpreter.addNativeFunction("__native_is_windows", __nativeIsWindows);
interpreter.addNativeFunction("__native_is_linux", __nativeIsLinux);
interpreter.addNativeFunction("__native_is_mac", __nativeIsMac);
interpreter.addNativeFunction("__native_exit", __nativeExit);
interpreter.addNativeFunction("__native_set_env", __nativeSetEnv);
interpreter.addNativeFunction("__native_get_pid", __nativeGetPID);
// Network.h
interpreter.addNativeFunction("__native_socket_create", __nativeSocketCreate);
interpreter.addNativeFunction("__native_socket_close", __nativeSocketClose);
interpreter.addNativeFunction("__native_socket_connect",
__nativeSocketConnect);
interpreter.addNativeFunction("__native_socket_send", __nativeSocketSend);
interpreter.addNativeFunction("__native_socket_receive",
__nativeSocketReceive);
interpreter.addNativeFunction("__native_socket_bind", __nativeSocketBind);
interpreter.addNativeFunction("__native_socket_listen", __nativeSocketListen);
interpreter.addNativeFunction("__native_socket_accept", __nativeSocketAccept);
// System Args
interpreter.addNativeFunction("__native_get_arg_count", __nativeGetArgCount);
interpreter.addNativeFunction("__native_get_arg", __nativeGetArg);
// String.h
interpreter.addNativeFunction("__native_string_length", __nativeStringLength);
interpreter.addNativeFunction("__native_string_at", __nativeStringAt);
interpreter.addNativeFunction("__native_string_to_int", __nativeStringToInt);
interpreter.addNativeFunction("__native_string_to_double",
__nativeStringToDouble);
}
void runAllTests() {
try {
std::cout << "Running tests..." << std::endl;
std::cout << "testIntList" << std::endl;
testIntList();
std::cout << "testStringList" << std::endl;
testStringList();
std::cout << "testToken" << std::endl;
testToken();
std::cout << "testLexerIntegers" << std::endl;
testLexerIntegers();
std::cout << "testLexerNumbers" << std::endl;
testLexerNumbers();
std::cout << "testLexerBooleans" << std::endl;
testLexerBooleans();
std::cout << "testLexerStrings" << std::endl;
testLexerStrings();
std::cout << "testLexerMathOperators" << std::endl;
testLexerMathOperators();
std::cout << "testLexerComparisonOperators" << std::endl;
testLexerComparisonOperators();
std::cout << "testLexerBooleanOperators" << std::endl;
testLexerBooleanOperators();
std::cout << "testLexerComments" << std::endl;
testLexerComments();
std::cout << "testLexerIdentifiers" << std::endl;
testLexerIdentifiers();
std::cout << "testLexerComplexExpression" << std::endl;
testLexerComplexExpression();
std::cout << "testLexerMultipleLines" << std::endl;
testLexerMultipleLines();
std::cout << "testLexerStringWithSpecialChars" << std::endl;
testLexerStringWithSpecialChars();
std::cout << "testLexerParenthesesAndBrackets" << std::endl;
testLexerParenthesesAndBrackets();
std::cout << "testLexerEmptyString" << std::endl;
testLexerEmptyString();
std::cout << "testLexerWhitespaceOnly" << std::endl;
testLexerWhitespaceOnly();
std::cout << "testExpression" << std::endl;
testExpression();
std::cout << "testStatement" << std::endl;
testStatement();
std::cout << "testParserExpressions" << std::endl;
testParserExpressions();
std::cout << "testParserVariables" << std::endl;
testParserVariables();
std::cout << "testParserStatements" << std::endl;
testParserStatements();
std::cout << "testParserPrecedence" << std::endl;
testParserPrecedence();
std::cout << "testInterpreterPrint" << std::endl;
testInterpreterPrint();
std::cout << "testInterpreterVariables" << std::endl;
testInterpreterVariables();
std::cout << "testInterpreterFunctions" << std::endl;
testInterpreterFunctions();
std::cout << "testInterpreterLoops" << std::endl;
testInterpreterLoops();
std::cout << "All tests passed!" << std::endl;
} catch (const std::exception &e) {
std::cerr << "Test failed: " << e.what() << std::endl;
exit(1);
}
}
int main(int argc, char *argv[]) {
bool testing = false;
bool help = false;
std::string filename = "";
int scriptArgsIndex = argc; // Default: no args
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--test" || arg == "--tests" || arg == "-t") {
testing = true;
} else if (arg == "--help" || arg == "-h") {
help = true;
} else {
filename = arg;
scriptArgsIndex = i + 1; // All args AFTER filename are for the script
break; // Stop parsing options for GW, rest is for script
}
}
// Pass args to System module
GW_setArgs(argc, argv, scriptArgsIndex);
if (testing) {
runAllTests();
if (filename == "")
return 0;
}
if (help) {
std::cout << "GW Informations: \n"
<< "- GitHub: https://github.com/Program132/GW/ \n"
<< "- Version: 1.1.0\n"
<< "How to use: \n"
<< "- ./GW <filename> : Run the program and interpret the code "
"from the file\n"
<< "- ./GW --test : Run all tests\n"
<< "- ./GW --help : Show this message\n"
<< "- ./GW : Start the REPL" << std::endl;
if (filename == "")
return 0;
}
if (filename != "") {
FileReader fileReader(filename);
std::string code = fileReader.getContent();
if (code.empty() && !testing) {
std::cerr << "Error: File is empty or could not be read: " << filename
<< std::endl;
return 1;
}
try {
Lexer lex(code);
lex.lex();
Parser parser(lex.getTokens());
parser.parse();
Interpreter interpreter(parser.getStatements());
registerNatives(interpreter);
interpreter.interpret();
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
} else {
REPL repl = REPL("GW> ");
repl.start();
std::cout << "GW Interpreter" << std::endl;
std::cout << "Type 'exit' or 'quit' to leave." << std::endl;
std::cout.flush();
while (true) {
std::string line = repl.readLine();
if (line == "exit" || line == "quit")
break;
if (line.empty())
continue;
try {
Lexer lex(line);
lex.lex();
Parser p(lex.getTokens());
p.parse();
Interpreter interp(p.getStatements());
registerNatives(interp);
interp.interpret();
std::cout << std::endl;
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
}
return 0;
}