-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
275 lines (247 loc) · 8.65 KB
/
Program.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
namespace Compiler;
public class Program
{
public static void Main()
{
try
{
string filePath = @"C:\Pro\Compiler-Extension\Example.txt";
string text = File.ReadAllText(filePath);
Lexer l = new Lexer(text);
List<Token> tokens = l.Tokenize();
Console.WriteLine(text);
foreach (Token t in tokens)
{
Console.WriteLine(t.Type.ToString()+ " in " + t.PositionError.Row+" line "+ " and " + t.PositionError.Column+ " column ");
}
Parser parser = new(tokens);
Expression root = parser.Parse();
Errors.PrintAll();
Console.WriteLine(parser.position);
PrintExpressionTree(root);
root.CheckSemantic(null);
PrintExpressionTree(root);
Errors.PrintAll();
List<Card> cards= (List<Card>)root.Evaluate(null!,null!);
CheckingContext context = new();
foreach (Card item in cards)
{
context.Deck.Add(item);
}
if(cards!= null)
for(int i = cards.Count-1; i>= 0 ; i--)
{
Card card= cards[i];
Console.WriteLine(card.ToString());
card.Execute(context);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void PrintExpressionTree(Expression node, int indentLevel = 0)
{
node.Print(indentLevel);
if (node is BinaryExpression binaryNode)
{
PrintExpressionTree(binaryNode.Left, indentLevel + 1);
PrintExpressionTree(binaryNode.Right, indentLevel + 1);
}
else if(node is Action action)
{
PrintExpressionTree(action.Context,indentLevel+1);
PrintExpressionTree(action.Targets,indentLevel+1);
PrintExpressionTree(action.Instructions,indentLevel+1);
}
else if (node is Atom numberNode)
{
Console.WriteLine(new string(' ', indentLevel * 4) + $"Value: {numberNode.ValueForPrint}");
}
else if (node is ProgramExpression prognode)
{
foreach(Expression eff in prognode.Instances)
{
PrintExpressionTree(eff, indentLevel + 1);
}
}
else if (node is EffectInstance effNode)
{
if(effNode.Name!= null)
PrintExpressionTree(effNode.Name, indentLevel + 1);
if(effNode.Params != null)
{
Console.WriteLine(new string(' ', (indentLevel+1) * 4) + $"Params");
foreach(Expression param in effNode.Params)
PrintExpressionTree(param, indentLevel + 2);
}
if(effNode.Action!= null)
{
PrintExpressionTree(effNode.Action);
}
}
else if(node is EffectParam effassign)
{
if(effassign.Effect!= null)
{
foreach(Expression exp in effassign.Effect)
{
PrintExpressionTree(exp, indentLevel +1);
}
}
if(effassign.Selector!= null)
{
PrintExpressionTree(effassign.Selector,indentLevel+1);
}
if(effassign.PostAction!= null)
{
PrintExpressionTree(effassign.PostAction);
}
}
else if(node is OnActivation onact)
{
foreach(Expression eff in onact.Effects)
PrintExpressionTree(eff, indentLevel+1);
}
else if (node is CardInstance card)
{
if(card.Name!= null)
PrintExpressionTree(card.Name, indentLevel + 1);
if(card.Power!= null)
PrintExpressionTree(card.Power, indentLevel + 1);
if(card.Type!= null)
PrintExpressionTree(card.Type, indentLevel + 1);
if(card.Range != null)
{
Console.WriteLine(new string(' ', (indentLevel+1) * 4) + $"Range");
foreach(Expression range in card.Range)
PrintExpressionTree(range, indentLevel + 2);
}
}
else if(node is UnaryExpression unaryOperator)
PrintExpressionTree(unaryOperator.Parameter, indentLevel + 1);
else if(node is InstructionBlock instructionBlock)
{
foreach(Expression exp in instructionBlock.Instructions)
{
PrintExpressionTree(exp, indentLevel + 1);
}
}
else if(node is ForExpression forexp)
{
PrintExpressionTree(forexp.Variable, indentLevel+1);
PrintExpressionTree(forexp.Collection,indentLevel +1);
PrintExpressionTree(forexp.Instructions,indentLevel +1);
}
else if(node is WhileExpression whilexp)
{
PrintExpressionTree(whilexp.Condition,indentLevel +1);
PrintExpressionTree(whilexp.Instructions,indentLevel +1);
}
else if(node is Selector selector)
{
PrintExpressionTree(selector.Source,indentLevel+1);
PrintExpressionTree(selector.Single,indentLevel+1);
PrintExpressionTree(selector.Predicate,indentLevel+1);
}
else if(node is Predicate predicate)
{
PrintExpressionTree(predicate.Unit,indentLevel+1);
PrintExpressionTree(predicate.Condition,indentLevel+1);
}
}
}
public class CheckingContext: DeckContext
{
public bool Turn{get;}
public List<Card> Find(Expression exp)
{
List<Card> result= new();
if(exp is Predicate predicate)
{
foreach(Card card in Board)
{
predicate.Unit!.Result= card;
if((bool)predicate.Condition!.Evaluate(exp.SemScope, null!))
{
result.Add(card);
}
}
}
return result;
}
public override List<Card> Deck{get;}= new List<Card>
{
};
public override List<Card> OtherDeck{get;}= new List<Card>
{
};
public override List<Card> DeckOfPlayer(Player player)
{
if(player.Turn)
{
return Deck;
}
else
{
//Here OtherDeck
return Deck;
}
}
public override List<Card> GraveYard{get;}= new List<Card>
{
};
public override List<Card> OtherGraveYard{get;}= new List<Card>
{
};
public override List<Card> GraveYardOfPlayer(Player player)
{
if(player.Turn)
{
return GraveYard;
}
else
{
return OtherGraveYard;
}
}
public override List<Card> Field{get;}= new List<Card>
{
};
public override List<Card> OtherField{get;}= new List<Card>
{
};
public override List<Card> FieldOfPlayer(Player player)
{
if(player.Turn)
{
return Field;
}
else
{
return OtherField;
}
}
public override List<Card> Hand{get;}= new List<Card>
{
};
public override List<Card> OtherHand{get;}= new List<Card>
{
};
public override List<Card> HandOfPlayer(Player player)
{
if(player.Turn)
{
return Hand;
}
else
{
return OtherHand;
}
}
public override List<Card> Board{get;}= new List<Card>
{
};
public override Player TriggerPlayer{get;}
}