-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodes.cs
227 lines (179 loc) · 5.91 KB
/
Nodes.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Лабораторная_работа__6
{
interface INode { }
interface IExpression : INode
{
object Accept(IExpressionVisitor visitor);
}
interface IStatement : INode
{
void Accept(IStatementVisitor visitor);
}
enum OperationType
{
Addition,
Subtraction,
Multiplication,
Division,
Remainder,
UnaryMinus,
LogicalNegation,
Equality,
Relation
}
class BinaryOperation : IExpression
{
static readonly IReadOnlyDictionary<OperationType, string> OperationSymbol = new Dictionary<OperationType, string>() {
{ OperationType.Addition, "+" },
{ OperationType.Subtraction, "-" },
{ OperationType.Multiplication, "*" },
{ OperationType.Division, "/" },
{ OperationType.Remainder, "%" },
{ OperationType.Equality, "==" },
{ OperationType.Relation, "<" }
};
static readonly IReadOnlyDictionary<OperationType, string> OperationShortName = new Dictionary<OperationType, string>() {
{ OperationType.Addition, "Sum" },
{ OperationType.Subtraction, "Sub" },
{ OperationType.Multiplication, "Mult" },
{ OperationType.Division, "Div" },
{ OperationType.Remainder, "Mod" },
{ OperationType.Equality, "Equal" },
{ OperationType.Relation, "Compare" }
};
public readonly IExpression Left;
public readonly OperationType Operation;
public readonly IExpression Right;
public BinaryOperation(IExpression left, OperationType operation, IExpression right)
{
Left = left;
Operation = operation;
Right = right;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class UnaryOperation : IExpression
{
static readonly IReadOnlyDictionary<OperationType, string> OperationSymbol = new Dictionary<OperationType, string>() {
{ OperationType.UnaryMinus, "-" },
{ OperationType.LogicalNegation, "!" }
};
static readonly IReadOnlyDictionary<OperationType, string> OperationShortName = new Dictionary<OperationType, string>() {
{ OperationType.UnaryMinus, "Minus" },
{ OperationType.LogicalNegation, "Not" }
};
public readonly OperationType Operation;
public readonly IExpression Expression;
public UnaryOperation(OperationType operation, IExpression expression)
{
Operation = operation;
Expression = expression;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class Number : IExpression
{
public readonly Token Token;
public Number(Token token)
{
Token = token;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class Identifier : IExpression
{
public readonly string Value;
public Identifier(string value)
{
Value = value;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class Paren : IExpression
{
public readonly IExpression Value;
public Paren(IExpression value)
{
Value = value;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class CallExpression : IExpression
{
public readonly IExpression Function;
public readonly IReadOnlyList<IExpression> Arguments;
public CallExpression(IExpression function, IReadOnlyList<IExpression> arguments)
{
Function = function;
Arguments = arguments;
}
public object Accept(IExpressionVisitor visitor) => visitor.Visit(this);
}
class IfStatement : IStatement
{
public readonly IExpression Condition;
public readonly Block Block;
public IfStatement(IExpression condition, Block block)
{
Condition = condition;
Block = block;
}
public void Accept(IStatementVisitor visitor) => visitor.Visit(this);
}
class WhileStatement : IStatement
{
public readonly IExpression Condition;
public readonly Block Block;
public WhileStatement(IExpression condition, Block block)
{
Condition = condition;
Block = block;
}
public void Accept(IStatementVisitor visitor) => visitor.Visit(this);
}
class ExpressionStatement : IStatement
{
public readonly IExpression Expression;
public ExpressionStatement(IExpression expression)
{
Expression = expression;
}
public void Accept(IStatementVisitor visitor) => visitor.Visit(this);
}
class Assignment : IStatement
{
public readonly string Variable;
public readonly IExpression Expression;
public Assignment(string variable, IExpression expression)
{
Variable = variable;
Expression = expression;
}
public void Accept(IStatementVisitor visitor) => visitor.Visit(this);
}
class Break : IStatement
{
public void Accept(IStatementVisitor visitor) => visitor.Visit(this);
}
class Block : INode
{
public readonly IReadOnlyList<IStatement> Statements;
public Block(IReadOnlyList<IStatement> statements)
{
Statements = statements;
}
}
class ProgramCode : INode
{
public readonly IReadOnlyList<IStatement> Statements;
public ProgramCode(IReadOnlyList<IStatement> statements)
{
Statements = statements;
}
}
}