-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathAstBuilder.cs
More file actions
178 lines (153 loc) · 4.57 KB
/
AstBuilder.cs
File metadata and controls
178 lines (153 loc) · 4.57 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
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
namespace ReClassNET.AddressParser
{
public class AstBuilder
{
private readonly Dictionary<char, int> operationPrecedence;
private readonly Stack<IOperation> resultStack = new Stack<IOperation>();
private readonly Stack<Token> operatorStack = new Stack<Token>();
public AstBuilder()
{
Contract.Ensures(operationPrecedence != null);
operationPrecedence = new Dictionary<char, int>
{
['\r'] = 0,
['['] = 1,
['+'] = 2,
['-'] = 2,
['*'] = 3,
['/'] = 3,
};
}
public IOperation Build(IEnumerable<Token> tokens)
{
Contract.Requires(tokens != null);
Contract.Ensures(Contract.ForAll(tokens, t => t != null));
resultStack.Clear();
operatorStack.Clear();
foreach (var token in tokens)
{
switch (token.TokenType)
{
case TokenType.Offset:
resultStack.Push(new OffsetOperation((IntPtr)token.Value));
break;
case TokenType.ModuleOffset:
resultStack.Push(new ModuleOffsetOperation(((string)token.Value).ToLowerInvariant()));
break;
case TokenType.LeftBracket:
operatorStack.Push(token);
break;
case TokenType.RightBracket:
PopOperations(true);
break;
case TokenType.Operation:
var operation1 = (char)token.Value;
while (operatorStack.Count > 0 && (operatorStack.Peek().TokenType == TokenType.Operation || operatorStack.Peek().TokenType == TokenType.ModuleOffset))
{
var other = operatorStack.Peek();
var operation2 = (char)other.Value;
if ((IsLeftAssociativeOperation(operation1) && operationPrecedence[operation1] <= operationPrecedence[operation2])
|| operationPrecedence[operation1] < operationPrecedence[operation2])
{
operatorStack.Pop();
resultStack.Push(ConvertOperation(other));
}
else
{
break;
}
}
operatorStack.Push(token);
break;
}
}
PopOperations(false);
VerifyResultStack();
return resultStack.FirstOrDefault();
}
private void PopOperations(bool untillLeftBracket)
{
while (operatorStack.Count > 0 && operatorStack.Peek().TokenType != TokenType.LeftBracket)
{
var token = operatorStack.Pop();
var lastOperation = ConvertOperation(token);
resultStack.Push(lastOperation);
}
if (untillLeftBracket)
{
if (operatorStack.Count > 0 && operatorStack.Peek().TokenType == TokenType.LeftBracket)
{
operatorStack.Pop();
resultStack.Push(ConvertOperation(new Token(TokenType.ReadPointer, '\r')));
}
else
{
throw new ParseException("No matching left bracket found for the right bracket.");
}
}
else
{
if (operatorStack.Count > 0 && operatorStack.Peek().TokenType == TokenType.LeftBracket)
{
throw new ParseException("No matching right bracket found for the left bracket.");
}
}
}
private IOperation ConvertOperation(Token operationToken)
{
Contract.Requires(operationToken != null);
try
{
IOperation argument1;
IOperation argument2;
switch ((char)operationToken.Value)
{
case '+':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
return new AdditionOperation(argument1, argument2);
case '-':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
return new SubtractionOperation(argument1, argument2);
case '*':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
return new MultiplicationOperation(argument1, argument2);
case '/':
argument2 = resultStack.Pop();
argument1 = resultStack.Pop();
return new DivisionOperation(argument1, argument2);
case '\r':
argument1 = resultStack.Pop();
return new ReadPointerOperation(argument1);
default:
throw new ArgumentException($"Unknown operation '{operationToken.Value}'.");
}
}
catch (InvalidOperationException)
{
throw new ParseException($"There is a syntax issue for the operation '{operationToken.Value}'.");
}
}
private void VerifyResultStack()
{
if (resultStack.Count > 1)
{
if (resultStack.Skip(1).FirstOrDefault(o => o is OffsetOperation) is OffsetOperation offset)
{
throw new ParseException($"Unexpected offset '{offset.Value}' found.");
}
throw new ParseException("The syntax of the provided formula is not valid.");
}
}
private bool IsLeftAssociativeOperation(char character)
{
return character == '+' || character == '-' || character == '*' || character == '/';
}
}
}