-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerateIcarus.cs
356 lines (298 loc) · 12.5 KB
/
GenerateIcarus.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DEvahebLib.Enums;
using DEvahebLib.Exceptions;
using DEvahebLib.Nodes;
namespace DEvahebLib.Visitors
{
public enum SourceCodeParity
{
BehavED = 0
}
public class GenerateIcarus : StackVisitorBase
{
public Variables Variables { get; protected set; }
public SourceCodeParity Parity { get; set; } = SourceCodeParity.BehavED;
public string Indentation { get; set; } = "\t";
public StringBuilder SourceCode { get; protected set; } = new StringBuilder();
protected string indent = String.Empty;
public GenerateIcarus()
: this(variables: null)
{
}
public GenerateIcarus(Variables variables)
: base()
{
Variables = variables;
}
public override void Visit(Node node)
{
if (blockStack.Count == 0) // in case of indent at root level
{
if (Parity == SourceCodeParity.BehavED)
{
if (node is BlockNode) // new empty line before block node
{
SourceCode.AppendLine();
}
}
SourceCode.Append(indent);
}
base.Visit(node);
}
public override void VisitValueNode(ValueNode node)
{
if (Parity == SourceCodeParity.BehavED)
{
if (argumentStack.Peek().Item1 is Set set && set.VariableName is StringValue variableName)
{
// if we're handling the second argument of the set function
if (node == set.Value)
{
if (Variables.Exists(variableName.String))
{
var typeName = Variables.GetVariableType(variableName.String);
if (typeName.StartsWith("\"") && typeName.EndsWith("\""))
{
SourceCode.Append($"/*@{typeName.Substring(1, typeName.Length - 2)}*/ ");
}
else if (typeName == "INT" && node is FloatValue f)
{
node = new IntegerValue((Int32)f.Float);
}
}
}
// if we're handling the first argument of the set function
else if (node == set.VariableName)
{
if (Variables.Exists(variableName.String))
{
SourceCode.Append($"/*@SET_TYPES*/ ");
}
}
}
}
if (node is VectorValue vectorValue)
{
if (vectorValue.Values[0] == null)
throw new MissingArgumentException("Vector is missing first value argument");
if (vectorValue.Values[1] == null)
throw new MissingArgumentException("Vector is missing second value argument");
if (vectorValue.Values[2] == null)
throw new MissingArgumentException("Vector is missing third value argument");
SourceCode.Append("<");
base.VisitValueNode(node);
SourceCode.Append(" >");
}
else
{
base.VisitValueNode(node);
if (node is FloatValue floatValue)
{
if (floatValue.Float == null)
throw new MissingArgumentException("Float is missing a value");
if (argumentStack.Peek().Item1 is Nodes.Random && Parity == SourceCodeParity.BehavED)
{
// weirdly BehavED doesn't format random float with 3 decimal points by default
SourceCode.Append(((float)floatValue.Float).ToString(CultureInfo.InvariantCulture));
}
else
{
SourceCode.Append(((float)floatValue.Float).ToString("0.000", CultureInfo.InvariantCulture));
}
}
else if (node is IntegerValue integerValue)
{
if (integerValue.Integer == null)
throw new MissingArgumentException("Integer is missing a value");
SourceCode.Append(((int)integerValue.Integer).ToString(CultureInfo.InvariantCulture));
}
else if (node is IdentifierValue identifierValue)
{
if (string.IsNullOrWhiteSpace(identifierValue.String))
throw new MissingArgumentException("String is missing a value");
SourceCode.Append(identifierValue.String);
}
else if (node is StringValue stringValue)
{
if (stringValue.String == null)
throw new MissingArgumentException("String is missing a value");
SourceCode.Append($"\"{stringValue.String}\"");
}
else if (node is CharValue charValue)
{
if (charValue.Char == null)
throw new MissingArgumentException("Char is missing a value");
SourceCode.Append(charValue.Char);
}
else if (node is EnumValue enumValue)
{
if (Parity == SourceCodeParity.BehavED)
{
// skip types for second argument of tag (tag type) and first argument of get (declare type)
if (!(argumentStack.Peek().Item1 is Tag skipTag && node == skipTag.TagType)
&& !(argumentStack.Peek().Item1 is Get skipGet && node == skipGet.Type))
{
string enumName = enumValue.Name;
// if we don't know the enum name but we're trying to handle the variable name for get()
if (string.IsNullOrEmpty(enumName) && argumentStack.Peek().Item1 is Get get
&& node == get.VariableName && get.VariableName is StringValue getVariable)
{
// Check if this variable name is in the set types list
var typeName = Variables.GetVariableType(getVariable.String);
// only care if the set type is an enum type (which is in quotes)
if (typeName.StartsWith("\"") && typeName.EndsWith("\""))
{
enumName = typeName.Substring(1, typeName.Length - 2);
}
}
if (!string.IsNullOrWhiteSpace(enumName))
{
SourceCode.Append($"/*@{enumValue.Name}*/ ");
}
}
}
SourceCode.Append(enumValue.Text);
}
}
}
public override void VisitVectorValue(VectorValue vector, Node node)
{
SourceCode.Append(" ");
base.VisitVectorValue(vector, node);
}
public override void VisitBlockNode(BlockNode node)
{
SourceCode.Append($"{node.Name}");
base.VisitBlockNode(node);
}
public override void VisitBlockNodeArguments(BlockNode node)
{
SourceCode.Append(" (");
if (Parity == SourceCodeParity.BehavED && node.Arguments?.FirstOrDefault() == null)
{
SourceCode.Append(" ");
}
base.VisitBlockNodeArguments(node);
SourceCode.AppendLine(" )");
}
public override void VisitBlockNodeSubNodes(BlockNode node)
{
SourceCode.Append(indent);
SourceCode.AppendLine("{");
int length = indent.Length;
indent += Indentation ?? "\t";
base.VisitBlockNodeSubNodes(node);
indent = indent.Substring(0, length);
SourceCode.Append(indent);
SourceCode.AppendLine("}");
if (Parity == SourceCodeParity.BehavED)
{
SourceCode.AppendLine();
}
}
public override void VisitBlockNodeSubNode(BlockNode blockNode, Node subNode)
{
if (Parity == SourceCodeParity.BehavED)
{
if (subNode is BlockNode) // new empty line before block node
{
SourceCode.AppendLine();
}
}
SourceCode.Append(indent);
base.VisitBlockNodeSubNode(blockNode, subNode);
if (!(subNode is BlockNode))
{
SourceCode.AppendLine(";");
}
}
public override void VisitOperatorNode(OperatorNode node)
{
char op = '=';
switch (node.Operator)
{
case Operator.Gt:
op = '>';
break;
case Operator.Lt:
op = '<';
break;
case Operator.Eq:
op = '=';
break;
case Operator.Ne:
op = '!';
break;
}
SourceCode.Append(op.ToString());
}
public override void VisitFunctionArgument(FunctionNode function, Node argument)
{
if (argumentStack.Peek().Item2.Count > 0) // if we're not the first argument
{
SourceCode.Append(",");
}
SourceCode.Append(" ");
if (function is If)
{
// NOTE:
// $-signs around values don't seem to be required:
// - Behaved saves to source files like that, but can read fine without
// - IBIZE compiles with or without
// For the If operators (=, !, >, <) behaved can NOT read without $, but IBIZE compiles fine
SourceCode.Append("$");
base.VisitFunctionArgument(function, argument);
SourceCode.Append("$");
}
else if (argument is Get || argument is Tag || argument is Nodes.Random)
{
SourceCode.Append("$");
base.VisitFunctionArgument(function, argument);
SourceCode.Append("$");
}
else
{
base.VisitFunctionArgument(function, argument);
}
}
public override void VisitFunctionNode(FunctionNode node)
{
if (Parity == SourceCodeParity.BehavED &&
(node is Tag || node is Get || node is Nodes.Random))
{
SourceCode.Append($"{node.Name}(");
}
else
{
SourceCode.Append($"{node.Name} (");
}
if (Parity == SourceCodeParity.BehavED && node.Arguments?.FirstOrDefault() == null)
{
SourceCode.Append(" ");
}
base.VisitFunctionNode(node);
if (Parity == SourceCodeParity.BehavED && (node is Get || node is Tag))
{
SourceCode.Append(")");
}
else
{
SourceCode.Append(" )");
}
if (blockStack.Count == 0 && argumentStack.Count == 0) // root level, inside blocks is handled by visitblocknodes
{
SourceCode.AppendLine(";");
}
}
public override void VisitMisteryNode(Node node)
{
base.VisitMisteryNode(node);
SourceCode.Append($"**MISTERY**");
}
}
}