forked from karbis/bruhlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
378 lines (363 loc) · 16.6 KB
/
Parser.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace bruhlang {
internal class Parser {
public static Scope Env = new Scope();
public string? ErrorMsg;
Node Tree;
Scope CurrentScope;
FunctionContext CurrentContext;
public dynamic? Result;
public Parser(Node tree) {
Tree = tree;
CurrentScope = Env;
Env.Variables = new GlobalEnvironment().Env;
CurrentContext = new FunctionContext();
ParseNode(tree);
Result = CurrentContext.Result;
}
dynamic? ParseNode(Node node) {
if (StoppedExecution()) return null;
if (node.Type == "Keyword" && node.Value == "var") {
CheckValidNode(node.Nodes[0], node, "Can not declare variable because variable name is invalid", "Identifier");
if (StoppedExecution()) return null;
CurrentScope.Variables.Add(node.Nodes[0].Value, null);
if (node.Nodes[1].Value != "Identifier") {
ParseNode(node.Nodes[1]);
}
} else if (node.Type == "Assignment") {
// errors to add: node.nodes[0] is not a identifier
// node.nodes[0] is not defined
ParseAssignment(node);
} else if (node.Type == "Operator") {
return ParseOperator(node);
} else if (node.Type == "Identifier") {
Scope scope = GetIdentifierScope(node.Value);
if (scope == null) {
Error("Variable '" + node.Value + "' doesn't exist", node);
return null;
}
return scope.Variables[node.Value];
} else if (node.Type == "Statement") {
return ParseNode(node.Nodes[0]);
} else if (node.Type == "UnaryMinus") {
return -ParseNode(node.Nodes[0]);
} else if (node.Type == "Number") {
return double.Parse(node.Value);
} else if (node.Type == "Bool") {
return node.Value == "true";
} else if (node.Type == "String") {
return node.Value;
} else if (node.Type == "Scope") {
foreach (Node node2 in node.Nodes) {
CurrentScope = new Scope(CurrentScope);
Scope assignedScope = CurrentScope;
dynamic? result = ParseNode(node2);
if (StoppedExecution()) return null;
//CurrentScope = assignedScope;
}
} else if (node.Type == "Keyword" && node.Value == "if") {
dynamic? result = ParseNode(node.Nodes[0]);
if (ToBool(result)) {
ParseNode(node.Nodes[1]);
} else if (node.Nodes[^1].Type == "Keyword") {
ParseNode(node.Nodes[^1].Nodes[0]);
}
} else if (node.Type == "Equality") {
return ParseEquality(node);
} else if (node.Type == "Keyword" && node.Value == "for") {
// check for identifiedr WHATEVER
CheckValidNode(node.Nodes[0], node, "Can not start for loop because variable is invalid", "Identifier");
CheckValidNode(node.Nodes[1], node, "Can not start for loop because missing keyword 'in'", "Keyword", "in");
CheckValidNode(node.Nodes[3], node, "Can not start for loop because of a missing comma", "Separator");
if (StoppedExecution()) return null;
dynamic? start = ParseNode(node.Nodes[2]);
CurrentScope = new Scope(CurrentScope);
Scope assignedScope = CurrentScope;
if (node.Nodes[5].Type != "Separator" && node.Nodes[5].Type != "Scope") {
Error("Invalid for loop structure, perhaps you're missing a comma?", node);
return null;
}
int scopeIndex = 5;
dynamic? incrementAmount = 1d;
if (node.Nodes[5].Type == "Separator") {
scopeIndex = 7;
incrementAmount = ParseNode(node.Nodes[6]);
if (StoppedExecution()) return null;
}
//CurrentScope.Variables[node.Nodes[0].Value] = start;
dynamic? end = ParseNode(node.Nodes[4]);
if (!(start is double) || !(end is double) || !(incrementAmount is double)) {
Error("For loop start/end/increment values weren't a number, types were:\nStart - " + start.GetType() + "\nEnd - " + end.GetType() + "\nIncrement - "
+ incrementAmount.GetType(), node);
return null;
} else {
start = (double)start;
end = (double)end;
incrementAmount = (double)incrementAmount;
}
if (incrementAmount > 0) {
for (double i = start; i <= end; i += incrementAmount) {
assignedScope.Variables[node.Nodes[0].Value] = i;
ParseNode(node.Nodes[scopeIndex]);
if (StoppedExecution()) return null;
}
} else {
for (double i = start; i >= end; i += incrementAmount) {
assignedScope.Variables[node.Nodes[0].Value] = i;
ParseNode(node.Nodes[scopeIndex]);
if (StoppedExecution()) return null;
}
}
} else if (node.Type == "Keyword" && node.Value == "while") {
while (true) {
dynamic? result = ParseNode(node.Nodes[0]);
if (!ToBool(result) || StoppedExecution()) break;
ParseNode(node.Nodes[1]);
}
} else if (node.Type == "ShortHand") {
Dictionary<string, dynamic?> scope = GetIdentifierScope(node.Nodes[0].Value).Variables;
if (node.Value == "++") {
scope[node.Nodes[0].Value]++;
} else if (node.Value == "--") {
scope[node.Nodes[0].Value]--;
}
} else if (node.Type == "Negate") {
return !ToBool(ParseNode(node.Nodes[0]));
} else if (node.Type == "Keyword" && node.Value == "function") {
bool anonymous = node.Nodes[0].Type == "Statement";
string? funcName = anonymous ? null : node.Nodes[0].Value;
if (!anonymous) {
CheckValidNode(node.Nodes[0], node, "Can not create function because name is invalid", "Identifier");
node.Nodes.RemoveAt(0);
}
int j = -1;
foreach (Node node2 in node.Nodes[0].Nodes) {
j++;
if (node2.Type == "Separator") continue;
CheckValidNode(node2, node, "Function argument list had invalid argument name");
if (j % 2 == 1 && node2.Type != "Separator") {
Error("Argument list did not have a comma", node);
return null;
}
}
Func<dynamic?[],dynamic?> funcReference = (dynamic?[] args) => {
CurrentScope = new Scope(CurrentScope);
Scope oldScope = CurrentScope;
int i = 0;
foreach (Node node2 in node.Nodes[0].Nodes) {
CurrentScope.Variables.Add(node2.Value, args.ElementAtOrDefault(i));
i++;
}
dynamic? result = ParseNode(node.Nodes[1]);
//oldScope.Parent.Scopes.Remove(oldScope);
return result;
};
if (!anonymous) {
CurrentScope.Variables.Add(funcName, funcReference);
} else {
return funcReference;
}
} else if (node.Type == "FunctionCall") {
CheckValidNode(node.Nodes[0], node, "Can not call function because function is invalid", "Identifier");
List<dynamic?> args = new List<dynamic?>();
int i = -1;
foreach (Node node2 in node.Nodes[1].Nodes) {
i++;
if (i%2 == 1 && node2.Type != "Separator") {
Error("Argument list did not have a comma", node);
return null;
}
args.Add(ParseNode(node2));
if (StoppedExecution()) return null;
}
FunctionContext oldContext = CurrentContext;
CurrentContext = new FunctionContext();
FunctionContext newContext = CurrentContext;
GetIdentifierScope(node.Nodes[0].Value).Variables[node.Nodes[0].Value](args.ToArray());
CurrentContext = oldContext;
return newContext.Result;
} else if (node.Type == "Keyword" && node.Value == "return") {
if (CurrentContext.Result != null) {
Error("Attempt to return more than once",node);
return null;
}
CurrentContext.Result = ParseNode(node.Nodes[0]);
return null;
} else if (node.Type == "Keyword" && node.Value == "and") {
dynamic? result1 = ParseNode(node.Nodes[0]);
dynamic? result2 = ParseNode(node.Nodes[1]);
if (ToBool(result1) && ToBool(result2)) {
return result2;
} else {
return result1;
}
} else if (node.Type == "Keyword" && node.Value == "or") {
dynamic? result1 = ParseNode(node.Nodes[0]);
dynamic? result2 = ParseNode(node.Nodes[1]);
if (!ToBool(result1) && ToBool(result2)) {
return result2;
} else {
return result1;
}
} else if (node.Type == "List") {
if (node.Nodes.Count % 2 == 0) {
Error("Can not create list as there is a separator with no value following it in the list", node);
return null;
}
int i = 0;
LangList list = new LangList();
foreach (Node node2 in node.Nodes) {
if (i % 2 == 1 && node2.Type != "Separator") {
Error("Missing comma in list", node);
return null;
}
list.Insert(ParseNode(node2));
if (StoppedExecution()) return null;
}
return list;
}
return null;
}
bool ToBool(dynamic? val) {
return !(val is null) && (!(val is bool) || (val is bool && val != false));
}
bool StoppedExecution() {
return ErrorMsg != null || !(CurrentContext.Result is null);
}
Scope? GetIdentifierScope(string name) {
Scope scope = CurrentScope;
while (scope!= null) {
if (scope.Variables.ContainsKey(name)) {
return scope;
}
scope = scope.Parent;
}
return null;
}
void Error(string msg, Node node) {
if (ErrorMsg != null) return;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(msg);
Console.ForegroundColor = ConsoleColor.DarkCyan;
//Console.WriteLine("-- Info --");
string indent = " ";
if (node.AttachedToken != null) {
Console.WriteLine(indent+"Line " + node.AttachedToken.LineCount + ", Character " + node.AttachedToken.CharacterCount);
Console.WriteLine(indent+"Token " + node.AttachedToken.Value + ", Type " + node.AttachedToken.Type);
}
Console.WriteLine(indent+"Node " + node.Value + ", Type " + node.Type);
//Console.WriteLine("----------");
Console.ResetColor();
ErrorMsg = msg;
//throw new Exception(msg);
}
void CheckValidNode(Node node, Node currentNode, string task = "", string intendedType = "Identifier", string? intendedValue = null) {
if (node.Type == intendedType) return;
if (intendedValue != null && node.Value == intendedValue) return;
Error(task + ". Expected " + intendedType + ", got " + node.Type, currentNode);
}
dynamic? ParseOperator(Node node) {
dynamic? leftSide = ParseNode(node.Nodes[0]);
dynamic? rightSide = ParseNode(node.Nodes[1]);
switch (node.Value) {
case "+":
if ((leftSide is string) || (rightSide is string)) {
Error("Attempt to add string", (leftSide is string) ? node.Nodes[0] : node.Nodes[1]);
return null;
}
return leftSide + rightSide;
case "-":
return leftSide - rightSide;
case "*":
return leftSide * rightSide;
case "/":
return leftSide / rightSide;
case "^":
return Math.Pow(leftSide, rightSide);
case "%":
return leftSide % rightSide;
case "..":
if (!(leftSide is string) && !(rightSide is string)) {
Error("Attempt to concatenate a non-string", !(leftSide is string) ? node.Nodes[0] : node.Nodes[1]);
return null;
}
if (leftSide.GetType() == typeof(double)) {
leftSide = leftSide.ToString();
}
if (rightSide.GetType() == typeof(double)) {
rightSide = rightSide.ToString();
}
return leftSide + rightSide;
}
return null;
}
bool? ParseEquality(Node node) {
dynamic? leftSide = ParseNode(node.Nodes[0]);
dynamic? rightSide = ParseNode(node.Nodes[1]);
switch (node.Value) {
case "==":
return leftSide == rightSide;
case ">=":
return leftSide >= rightSide;
case "<=":
return leftSide <= rightSide;
case ">":
return leftSide > rightSide;
case "<":
return leftSide < rightSide;
case "!=":
return leftSide != rightSide;
}
return null;
}
void ParseAssignment(Node node) {
dynamic? rightSide = ParseNode(node.Nodes[1]);
string nodeVal = node.Nodes[0].Value;
Dictionary<string, dynamic?> scope = GetIdentifierScope(nodeVal).Variables;
switch (node.Value) {
case "=":
scope[nodeVal] = rightSide;
break;
case "+=":
if (scope[nodeVal] is string) {
Error("Attempt to add string", node.Nodes[0]);
break;
}
scope[nodeVal] += rightSide;
break;
case "-=":
scope[nodeVal] -= rightSide;
break;
case "*=":
scope[nodeVal] *= rightSide;
break;
case "/=":
scope[nodeVal] /= rightSide;
break;
case "^=":
dynamic? val = scope[nodeVal];
scope[nodeVal] = Math.Pow((double)val, (double)rightSide);
break;
case "%=":
scope[nodeVal] %= rightSide;
break;
case "..=":
if (!(scope[nodeVal] is string)) {
Error("Attempt to concatenate non-string", node.Nodes[0]);
break;
}
if (rightSide.GetType() == typeof(double)) {
rightSide = rightSide.ToString();
}
scope[nodeVal] += rightSide;
break;
}
}
}
}