-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
273 lines (185 loc) · 5.47 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
using System;
using System.Collections.Generic;
namespace LuaV {
public class Expr {
public string Type;
public string Name;
public List<string> DefinedArgs = new List<string>();
public List<Expr> Body = new List<Expr>();
public List<Expr> Args = new List<Expr>();
public Expr Left;
public string Operator;
public Expr Right;
public bool Dictionary = false;
public List<Expr> TKeys = new List<Expr>();
public List<Expr> TValues = new List<Expr>();
public Token Value;
public override string ToString()
{
if (Type == "binary") {
return string.Format("({0} {1} {2})", Left, Operator, Right);
}
return Value.Value;
}
public Expr(Expr left, string op, Expr right) {
Type = "binary";
Left = left;
Operator = op;
Right = right;
}
public Expr(string type, Token value) {
Type = type;
Value = value;
}
public Expr(string type) {
Type = type;
}
}
public class Operator {
public string Op;
public int Precedence;
public bool LeftAssociative = true;
public Operator(string op, int precedence) {
Op = op;
Precedence = precedence;
}
}
public class LuaParser {
public Lexer Lex;
private Stack<Expr> Operands = new Stack<Expr>();
private Stack<Operator> Operators = new Stack<Operator>();
// Lua operators
public Dictionary<string, Operator> OperatorDefs = new Dictionary<string, Operator>() {
{ "+", new Operator("+", 1) },
{ "-", new Operator("-", 1) },
{ "*", new Operator("*", 2) },
{ "/", new Operator("/", 3) },
};
public Expr Function() {
if (Lex.PeekToken().Value == "function") {
Lex.GetToken();
Expr expr = new Expr("function");
if (Lex.PeekToken().Type == "ID") {
expr.Name = Lex.GetToken().Value;
}
if (Lex.GetToken().Type != "LPAREN") {
throw new Exception("Expected '('");
}
while (Lex.PeekToken().Type != "RPAREN") {
expr.DefinedArgs.Add(Lex.GetToken().Value);
if (Lex.PeekToken().Type != "RPAREN") {
if (Lex.PeekToken().Type != "COMMA") {
throw new Exception("Expected ','");
}
Lex.GetToken();
}
}
Lex.GetToken();
while (Lex.PeekToken().Value != "end") {
expr.Body.Add(Function());
}
Lex.GetToken();
return expr;
}
return Math();
}
public Expr Math() {
Operands.Push(Primary());
while (Lex.PeekToken().Type == "OP" && OperatorDefs.ContainsKey(Lex.PeekToken().Value)) {
Operator op = OperatorDefs[Lex.PeekToken().Value];
if (Operators.Count > 0) {
Operator next = Operators.Peek();
while (Operators.Count > 0 && (next.Precedence > op.Precedence || (op.Precedence == next.Precedence && Operators.Peek().LeftAssociative))) {
Expr right = Operands.Pop();
Operands.Push(new Expr(Operands.Pop(), Operators.Pop().Op, right));
}
}
Lex.GetToken();
Operators.Push(op);
Operands.Push(Primary());
}
while (Operators.Count > 0) {
Expr right = Operands.Pop();
Operands.Push(new Expr(Operands.Pop(), Operators.Pop().Op, right));
}
return Operands.Pop();
}
public Expr Primary() {
return Literal();
}
public Expr Table() {
if (Lex.PeekToken().Type == "LBRACE") {
Token open = Lex.GetToken();
Expr table = new Expr("table");
bool dict = false;
bool first = true;
while (Lex.PeekToken().Type != "RBRACE") {
if (first && Lex.PeekToken().Type == "LBRACKET") {
dict = true;
table.Dictionary = true;
}
first = false;
if (dict) {
if (Lex.GetToken().Type != "LBRACKET") {
throw new Exception("Expected '['");
}
table.TKeys.Add(Math());
if (Lex.PeekToken().Type != "RBRACKET") {
throw new Exception($"']' expected near '{Lex.PeekToken().Value}'");
}
Lex.GetToken();
if (Lex.GetToken().Value != "=") {
throw new Exception("Expected '=' near ']'");
}
table.TValues.Add(Math());
}
else {
table.TValues.Add(Math());
}
if (Lex.PeekToken().Type == "COMMA") {
Lex.GetToken();
}
else if (Lex.PeekToken().Type != "RBRACE") {
throw new Exception($"'}}' expected (to close '{{' at line {open.Line}) near '{Lex.PeekToken().Value}'");
}
}
Lex.GetToken();
return table;
}
return Literal();
}
public Expr Literal() {
if (Lex.PeekToken().Type == "LBRACE")
return Table();
Token tok = Lex.GetToken();
if (tok.Type == "NUM")
return new Expr("number", tok);
if (tok.Type == "STR")
return new Expr("string", tok);
Expr callable = null;
if (tok.Type == "ID")
callable = new Expr("id", tok);
if (tok.Type == "LPAREN") {
callable = new Expr("group");
callable.Left = Function();
if (Lex.GetToken().Type != "RPAREN") {
throw new Exception($"Expected ')' near '{tok.Value}'");
}
}
if (Lex.PeekToken().Type == "LPAREN") {
Expr expr = new Expr("call");
expr.Left = callable;
Token open = Lex.GetToken();
// Add argument parsing later
if (Lex.GetToken().Type != "RPAREN") {
throw new Exception($"Expected ')' near '{open.Value}'");
}
return expr;
}
throw new Exception($"Unexpected symbol near '{tok.Value}'");
}
public LuaParser(Lexer lex) {
Lex = lex;
}
}
}