-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
190 lines (154 loc) · 5.15 KB
/
Copy pathParser.cs
File metadata and controls
190 lines (154 loc) · 5.15 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
using System.Collections.Generic;
using System.Numerics;
namespace JCore
{
public class LetNode : Node
{
public string Name { get; }
public Node Value { get; }
public LetNode(string name, Node value)
{
Name = name;
Value = value;
}
}
public class ConstNode : Node
{
public string Name { get; }
public Node Value { get; }
public ConstNode(string name, Node value)
{
Name = name;
Value = value;
}
}
public class Parser
{
private readonly List<Token> _tokens;
private int _pos = 0;
public Parser(List<Token> tokens)
{
_tokens = tokens;
}
public List<Node> Parse()
{
var nodes = new List<Node>();
while (!IsAtEnd())
{
nodes.Add(ParseStatement());
}
return nodes;
}
private Node ParseStatement()
{
if (Match("SAY"))
{
if (IsAtEnd())
throw new System.Exception("Expected something after 'say'");
var expr = ParseExpression();
return new SayNode(expr);
}
if (Match("LET"))
{
if (IsAtEnd())
throw new System.Exception("Expected variable name after 'let'");
var name = Consume("IDENT").Value;
if (IsAtEnd())
throw new System.Exception("Expected '=' after variable name");
Consume("EQUAL");
if (IsAtEnd())
throw new System.Exception("Expected value after '='");
var expr = ParseExpression();
return new LetNode(name, expr);
}
if (Match("MAKE"))
{
if (IsAtEnd())
throw new System.Exception("Expected constant name after 'make'");
var name = Consume("IDENT").Value;
if (IsAtEnd())
throw new System.Exception("Expected '=' after constant name");
Consume("EQUAL");
if (IsAtEnd())
throw new System.Exception("Expected value after '='");
var expr = ParseExpression();
return new ConstNode(name, expr);
}
if (IsAtEnd())
throw new System.Exception("Unexpected end of input");
var unknown = _tokens[_pos++];
return new UnknownNode(unknown.Value);
}
private Node ParseExpression(int precedence = 0)
{
if (IsAtEnd())
throw new System.Exception("Unexpected end of expression");
Node left;
var token = _tokens[_pos++];
if (token.Type == "NUMBER")
{
left = new NumberNode(BigInteger.Parse(token.Value));
}
else if (token.Type == "STRING")
{
left = new StringNode(token.Value);
}
else if (token.Type == "IDENT")
{
left = new VariableNode(token.Value);
}
else if (token.Type == "LPAREN")
{
left = ParseExpression();
Consume("RPAREN");
}
else
{
throw new System.Exception($"Unexpected token '{token.Value}'");
}
while (!IsAtEnd() && GetPrecedence(Current()) > precedence)
{
var opToken = _tokens[_pos++];
var right = ParseExpression(GetPrecedence(opToken));
left = new BinaryOpNode(left, opToken.Type, right);
}
return left;
}
private Token Current() => _tokens[_pos];
private int GetPrecedence(Token token)
{
return token.Type switch
{
"PLUS" => 1,
"MINUS" => 1,
"STAR" => 2,
"SLASH" => 2,
_ => 0,
};
}
private Token Consume(params string[] expectedTypes)
{
if (IsAtEnd())
throw new System.Exception($"Expected {string.Join(" or ", expectedTypes)} but found end of input");
var token = _tokens[_pos];
if (System.Array.IndexOf(expectedTypes, token.Type) == -1)
throw new System.Exception($"Expected token of type: {string.Join(" or ", expectedTypes)}, but got '{token.Value}'");
_pos++;
return token;
}
private bool Match(string type)
{
if (IsAtEnd()) return false;
if (_tokens[_pos].Type == type)
{
_pos++;
return true;
}
return false;
}
private bool IsAtEnd()
{
return _pos >= _tokens.Count;
}
}
}