-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntoAST.cs
1141 lines (977 loc) · 39 KB
/
IntoAST.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Configuration;
using System.CodeDom.Compiler;
using System.Xml;
using System.Globalization;
namespace MCFCompiler
{
static class Reserved
{
public static string[] reservedKeywords = { "desig", "var", "func", "lit", "call", "context", "run", "endrun", ";", "rule" };
// List of names that exist
public static List<VariableDeclaration> vEx = new List<VariableDeclaration>();
public static List<DesigDeclaration> dEx = new List<DesigDeclaration>();
public static List<FunctionDeclaration> fEx = new List<FunctionDeclaration>();
// Whether s exists as name or check if keyword if des
public static bool Exists(string s, bool dec)
{
// If string is a reserved keyword
if (dec && reservedKeywords.Contains(s))
{
throw new Exception("Name " + s + " is a reserved keyword!");
}
return vEx.Exists(v => v.varName == s) || dEx.Exists(d => d.desigName == s) || fEx.Exists(f => false);
}
// String of commands to initialize all variables and desigs
public static string InitializeDeclarationString()
{
// Add variable scoreboard
string coll = "scoreboard objectives add " + IntoAST.nameSpace + "Var dummy\n";
// Set all variables to 0
foreach (VariableDeclaration vd in vEx)
{
coll += "scoreboard players set " + vd.varName + " " + IntoAST.nameSpace + "Var 0\n";
}
// Create all desig scoreboards
foreach (DesigDeclaration dd in dEx)
{
coll += "scoreboard objectives add " + dd.desigName + " " + dd.criteria + "\n";
}
return coll;
}
}
// Can be a child to a function declaration
interface IChildFunctionDeclaration {
string GetLine();
}
class FunctionDeclaration : IChildMainFile
{
// List of things inside function
List<IChildFunctionDeclaration> todo = new List<IChildFunctionDeclaration>();
public string funcName;
public string context = "";
// Parse into function
public int Parse(string[] parts, int start)
{
// Set compilation time context
string currContext = context;
// If doesn't start with keyword func
if (parts[start] != "func")
{
throw new Exception("Func starts with '" + parts[start] + "' not 'func'!");
}
// If the name is already taken
else if (Reserved.Exists(parts[start + 1], true))
{
throw new Exception("Name " + parts[start + 1] + " already exists!");
}
else if (parts[start + 2] != "{")
{
throw new Exception("Expected { not " + parts[start + 2]);
}
else
{
// Save function name
funcName = parts[start + 1];
Reserved.fEx.Add(this);
// Skip to first expression
start += 3;
// Keep identifying expression until end of function declaration
while (start < parts.Length)
{
if (parts[start] == "}")
{
return start + 1;
}
// Identify from function context
IntoAST.IdentifyResponse ir = IntoAST.Identify("function", currContext, parts, start);
// Move to next statement and add function child to todo
start = ir.next;
if (ir.IChild is IChildFunctionDeclaration icfd)
{
todo.Add(icfd);
}
else
{
throw new Exception("Function child '" + ir.IChild + "' cannot follow function '" + funcName + "'");
}
}
// Didn't find end of function
throw new Exception("No } found for function " + funcName);
}
}
public void CreateFunction(string directory)
{
string filepath = directory + funcName + ".mcfunction";
if (File.Exists(filepath))
{
throw new Exception("Function '" + funcName + "'s .mcfunction already exists!");
}
else
{
string alldo = "";
// There is a load rule, and the load function is this function
if (Ruling.hasLoad && IntoAST.maintodo.Exists(x => x is LoadFuncRuling lf && lf.function == funcName))
{
// Begin with the initialization
alldo = Reserved.InitializeDeclarationString();
}
// Open the file to the function
StreamWriter sw = File.CreateText(filepath);
foreach (IChildFunctionDeclaration icdf in todo)
{
alldo += icdf.GetLine() + "\n";
}
// Write to file as text
sw.Write(alldo);
sw.Close();
{}
}
}
}
class VariableDeclaration : IChildMainFile
{
// Name of var
public string varName;
// Parse the declaration and returns index of parts after declaration
public int Parse(string[] parts, int start)
{
// If doesn't start with keyword
if (parts[start] != "var")
{
throw new Exception("Var starts with '" + parts[start] + "' not 'var'!");
}
// If the name is already taken
else if (Reserved.Exists(parts[start + 1], true))
{
throw new Exception("Name " + parts[start + 1] + " already exists!");
}
// Declaration doesn't end with ;
else if (parts[start + 2] != ";")
{
throw new Exception("Var declaration doesn't end in ';'!");
}
else
{
// Save var name
varName = parts[start + 1];
Reserved.vEx.Add(this);
// Return index after var
return start + 3;
}
}
}
// Desig declaration
class DesigDeclaration : IChildMainFile
{
// List of desigs that exist
public static List<string> existing = new List<string>();
// Name of desig
public string criteria;
public string desigName;
// Parse the declaration and returns index of parts after declaration
public int Parse(string[] parts, int start)
{
// If doesn't start with keyword
if (parts[start] != "desig")
{
throw new Exception("Desig starts with '" + parts[start] + "' not 'desig'!");
}
// If the name is already taken
else if (Reserved.Exists(parts[start + 1], true))
{
throw new Exception("Name " + parts[start + 1] + " already exists!");
}
// Declaration doesn't end with ;
else if (parts[start + 3] != ";")
{
throw new Exception("Desig declaration doesn't end in ';'!");
}
else
{
// Save desig name
desigName = parts[start + 1];
criteria = parts[start + 2];
Reserved.dEx.Add(this);
// Return index after desig
return start + 4;
}
}
}
// Runs raw command
class RunCommand : IChildFunctionDeclaration
{
public string context;
public string command;
public int Parse(string[] parts, int start)
{
if (parts[start] != "run")
{
throw new Exception("Run starts with '" + parts[start] + " not 'run'!");
}
else
{
// Reset command
command = "";
start++;
// Gather command until 'endrun'
while (parts[start] != "endrun")
{
// Add to command
command += parts[start];
// Add space between parts if there is next statement
if (parts[start + 1] != "endrun")
command += " ";
start++;
}
// Skip 'endrun'
return start + 1;
}
}
public string GetLine()
{
return context + command;
}
}
// Change in context, with same criteria as function
class ContextChange : IChildFunctionDeclaration
{
// Context taken from
public string context;
// Context things inside run in
public string change;
List<IChildFunctionDeclaration> todo = new List<IChildFunctionDeclaration>();
// Parse into function
public int Parse(string[] parts, int start)
{
// If doesn't start with keyword func
if (parts[start] != "context")
{
throw new Exception("Context change starts with '" + parts[start] + "' not 'context'!");
}
start++;
// Get the change in context
change = context + "execute ";
// While hasn't reached code block start
while (parts[start] != "{")
{
// Add change
change += parts[start] + " ";
start++;
}
change += "run ";
// This is impossible
if (parts[start] != "{")
{
throw new Exception("Expected { not " + parts[start]);
}
else
{
// Skip {
start += 1;
// Keep identifying expression until end of function declaration
while (start < parts.Length)
{
if (parts[start] == "}")
{
return start + 1;
}
// Identify from function context
IntoAST.IdentifyResponse ir = IntoAST.Identify("function", change, parts, start);
// Move to next statement and add function child to todo
start = ir.next;
if (ir.IChild is IChildFunctionDeclaration icfd)
{
todo.Add(icfd);
}
else
{
throw new Exception("Context child '" + ir.IChild + "' cannot follow context change '" + change + "'");
}
}
// Didn't find end of function
throw new Exception("No } found for context change " + change);
}
}
/*
public void CreateFunction(string directory)
{
string filepath = directory + funcName + ".mcfunction";
if (File.Exists(filepath))
{
throw new Exception("Function '" + funcName + "'s .mcfunction already exists!");
}
else
{
StreamWriter sw = File.CreateText(filepath);
string alldo = "";
foreach (IChildFunctionDeclaration icdf in todo)
{
alldo += icdf.GetLine() + "\n";
}
// Write to file as text
sw.Write(alldo);
sw.Close();
{}
}
}*/
public string GetLine()
{
string alldo = "";
foreach (IChildFunctionDeclaration icdf in todo)
{
alldo += icdf.GetLine() + "\n";
}
return alldo;
}
}
// Whether be a child to a variable assignation
interface IChildVariableAssignation
{
string GetExpVar(string op, string varname);
}
class VariableAssignation : IChildFunctionDeclaration
{
// List of assigning operators
public static List<string> assignOps = new string[] { "=", "+=", "-=", "*=", "/=", "%=" }.ToList();
// Context of assignation
public string context;
// Declarationn of variable
public VariableDeclaration var;
public string op;
// Variable expression
public IChildVariableAssignation exp;
// Parses the variable assignation
public int Parse(string[] parts, int start)
{
// Variable is not declared
if (!Reserved.vEx.Exists(p => p.varName == parts[start]))
{
throw new Exception("Variable '" + parts[start] + "' is not declared!");
}
// The second part is not proper assigning operator
else if (!assignOps.Contains(parts[start + 1]))
{
throw new Exception("Variable assignation has improper assigning operator\t\tFormat: [varname] [=, +=, -=, *=, /=, or %=] [const or literal integer] ;!");
}
// Get expression
else
{
// Save operator
op = parts[start + 1];
// Save Variable
var = Reserved.vEx.Find(v => v.varName == parts[start]);
// Identify type
IntoAST.IdentifyResponse ir = IntoAST.Identify("variable expression", context, parts, start + 2);
// Check type
if (ir.IChild is IChildVariableAssignation icva)
{
// Move to next
start = ir.next;
exp = icva;
}
else
{
throw new Exception("Invalid variable subexpression '" + ir.IChild + "'");
}
// Return index after expression
return start;
}
}
public string GetLine()
{
return context + "scoreboard players " + exp.GetExpVar(op, var.varName);
}
}
// Can be a child to a desig assignation
interface IChildDesigAssignation
{
string GetExpDesig(string op, string designame);
}
class DesigAssignation : IChildFunctionDeclaration
{
// Declarationn of variable
public DesigDeclaration desig;
public string op;
// Expression to deconstruct
public IChildDesigAssignation exp;
// Context of assignation
public string context;
// List of assigning operators
public static List<string> desigOps = new string[] { "=", "+=", "-=", "*=", "/=", "%=" }.ToList();
// Parses the variable assignation
public int Parse(string[] parts, int start)
{
// Variable is not declared
if (!Reserved.dEx.Exists(p => p.desigName == parts[start]))
{
throw new Exception("Desig '" + parts[start] + "' is not declared!");
}
// The second part is not proper assigning operator
else if (!desigOps.Contains(parts[start + 1]))
{
throw new Exception("Desig assignation has improper assigning operator '" + parts[start + 1] + "'!");
}
// Get expression
else
{
// Save operator
op = parts[start + 1];
// Save desig
desig = Reserved.dEx.Find(d => d.desigName == parts[start]);
// Identify type
IntoAST.IdentifyResponse ir = IntoAST.Identify("desig expression", context, parts, start + 2);
// Check type
if (ir.IChild is IChildDesigAssignation icda)
{
// Move to next
start = ir.next;
exp = icda;
}
else
{
throw new Exception("Invalid desig subexpression '" + ir.IChild + "'");
}
// Return index after expression
return start;
}
}
public string GetLine()
{
return context + "scoreboard players " + exp.GetExpDesig(op, desig.desigName);
}
}
// Literal can be in a assignation or expression
class Literal : IChildVariableAssignation, IChildDesigAssignation
{
int value = 0;
// Parses expression and keeps value
public int Parse(string[] parts, int start)
{
if (parts[start] != "lit")
{
throw new Exception("Literal starts with " + parts[start] + " not 'lit'!");
}
else if (parts[start + 2] != ";")
{
throw new Exception("Literal ends with with " + parts[start + 2] + " not ';'!");
}
bool parsed = Int32.TryParse(parts[start + 1], out value);
if (!parsed)
{
throw new Exception ("Could not parse '" + parts[start + 1] + "'!");
}
return start + 3;
}
// set <varname> <namespace>Var <value>
public string GetExpVar(string op, string varname)
{
if (op == "=")
{
return "set " + varname + " " + IntoAST.nameSpace + "Var " + value;
}
else if (op == "+=")
{
return "add " + varname + " " + IntoAST.nameSpace + "Var " + value;
}
else if (op == "-=")
{
return "remove " + varname + " " + IntoAST.nameSpace + "Var " + value;
}
else if (op == "*=" || op == "%=" || op == "/=")
{
throw new Exception("Operation '" + op + "' cannot be performed on a literal!");
}
else
{
throw new Exception("Unrecognized operation " + op + "!");
}
}
public string GetExpDesig(string op, string designame)
{
if (op == "=")
{
return "set @s " + designame + " " + value;
}
else if (op == "+=")
{
return "add @s " + designame + " " + value;
}
else if (op == "-=")
{
return "remove @s " + designame + " " + value;
}
else if (op == "*=" || op == "%=" || op == "/=")
{
throw new Exception("Operation '" + op + "' cannot be performed on a literal!");
}
else
{
throw new Exception("Unrecognized operation '" + op + "'!");
}
}
}
// Variable as a subexpression
class VarSubExpression: IChildVariableAssignation, IChildDesigAssignation
{
VariableDeclaration vd;
// Parses into variable
public int Parse(string[] parts, int start)
{
// Ends with ;
if (parts[start + 1] != ";")
{
throw new Exception("Subexpression ends with " + parts[start] + " not ';'!");
}
VariableDeclaration varDec = Reserved.vEx.Find(v => v.varName == parts[start]);
// Checks if variable name
if (varDec == null)
{
throw new Exception (parts[start] + " is not a variable name!");
}
else
{
// Is a variable name
vd = varDec;
// Return position after
return start + 2;
}
}
// operation <varname> <namespace>Var <op> <othervar> <namespace>Var
public string GetExpVar(string op, string varname)
{
return "operation " + varname + " " + IntoAST.nameSpace + "Var " + op + " " + vd.varName + " " + IntoAST.nameSpace + "Var";
}
// operation @s <designame> <op> <othervar> <namespace>Var
public string GetExpDesig(string op, string designame)
{
return "operation @s " + designame + " " + op + " " + vd.varName + " " + IntoAST.nameSpace + "Var";
}
}
// Desig as a subexpression
class DesigSubExpression: IChildDesigAssignation
{
DesigDeclaration dd;
// Parses into variable
public int Parse(string[] parts, int start)
{
// Ends with ;
if (parts[start + 1] != ";")
{
throw new Exception("Subexpression ends with " + parts[start] + " not ';'!");
}
DesigDeclaration desDec = Reserved.dEx.Find(d => d.desigName == parts[start]);
// Checks if desig name
if (desDec == null)
{
throw new Exception (parts[start] + " is not a desig name!");
}
else
{
// Is a desig name
dd = desDec;
// Return position after
return start + 2;
}
}
// operation @s <designame> <op> @s <otherdesigname>
public string GetExpDesig(string op, string designame)
{
return "operation @s " + designame + " " + op + " @s " + dd.desigName;
}
}
// Function call
class FuncCall : IChildFunctionDeclaration
{
FunctionDeclaration fd;
// Context of call
public string context;
// Parses into function call
public int Parse(string[] parts, int start)
{
// Start with call keyword
if (parts[start] != "call")
{
throw new Exception("Function call starts with '" + parts[start] + " not 'call'!");
}
else if (parts[start + 2] != ";")
{
throw new Exception("Function call does not end with ';'!");
}
// Gets declaration
FunctionDeclaration tempFD = Reserved.fEx.Find(f => f.funcName == parts[start + 1]);
if (tempFD == null)
{
throw new Exception("Function '" + parts[start + 1] + "' does not exist!");
}
else
{
fd = tempFD;
return start + 3;
}
}
public string GetLine()
{
return context + "function " + IntoAST.nameSpace + ":" + fd.funcName;
}
}
// Can be a child to the main file
public interface IChildMainFile { }
class IntoAST
{
public static List<IChildMainFile> maintodo;
public static CompilationOfFiles cof = new CompilationOfFiles();
public static string nameSpace;
public IntoAST(string textfile)
{
maintodo = new List<IChildMainFile>();
/* Clean string */
// Newline to spaces
string cleaned = textfile.Replace("\n", " ");
// Tabs to spaces
cleaned = cleaned.Replace("\t", " ");
// Split by spaces
string[] parts = cleaned.Split(new char[] {' '});
// Remove empty parts (aka "")
parts = parts.ToList().FindAll(p => p != null && (p != "")).ToArray();
// Save context
string context = "mainfile";
// For every part
for (int i = 0; i < parts.Length;)
{
// Identify next statement
IdentifyResponse ir = Identify(context, "", parts, i);
// Go to next statement
i = ir.next;
// Add to main todo
maintodo.Add(ir.IChild as IChildMainFile);
}
}
public class IdentifyResponse
{
public string context;
public Object IChild;
public int next;
public IdentifyResponse(Object i, string c, int n)
{
IChild = i;
context = c;
next = n;
}
}
// Identifies type given and parses into type. expContext is context that it will run on
public static IdentifyResponse Identify(string context, string expContext, string[] parts, int start)
{
// If it is a reserved keyword
if (Reserved.reservedKeywords.Contains(parts[start]))
{
/* Main File */
// If declaring desig
if (context == "mainfile" && parts[start] == "desig")
{
DesigDeclaration dd = new DesigDeclaration();
return new IdentifyResponse(dd, expContext, dd.Parse(parts, start));
}
// If declaring variable
else if (context == "mainfile" && parts[start] == "var")
{
VariableDeclaration vd = new VariableDeclaration();
return new IdentifyResponse(vd, expContext, vd.Parse(parts, start));
}
// If declaring function
else if (context == "mainfile" && parts[start] == "func")
{
FunctionDeclaration fd = new FunctionDeclaration();
return new IdentifyResponse(fd, expContext, fd.Parse(parts, start));
}
// If declaring ruling
else if (context == "mainfile" && parts[start] == "rule")
{
RulingResponse rr = Ruling.IdentifyRuling(parts, start);
return new IdentifyResponse(rr.icmf, expContext, rr.start);
}
/* Function */
// If it is a function call
else if (context == "function" && parts[start] == "call")
{
FuncCall fc = new FuncCall();
fc.context = expContext;
return new IdentifyResponse(fc, expContext, fc.Parse(parts, start));
}
// If it is a context change
else if (context == "function" && parts[start] == "context")
{
ContextChange cc = new ContextChange();
cc.context = expContext;
return new IdentifyResponse(cc, expContext, cc.Parse(parts, start));
}
// If it is a specified command
else if (context == "function" && parts[start] == "run")
{
RunCommand rc = new RunCommand();
rc.context = expContext;
return new IdentifyResponse(rc, expContext, rc.Parse(parts, start));
}
/* Variable Assignation */
// If it is a literal
else if (context == "variable expression" && parts[start] == "lit")
{
Literal lit = new Literal();
return new IdentifyResponse(lit, expContext, lit.Parse(parts, start));
}
/* Desig Assignation */
// If it is a literal
else if (context == "desig expression" && parts[start] == "lit")
{
Literal lit = new Literal();
return new IdentifyResponse(lit, expContext, lit.Parse(parts, start));
}
}
// Else if it is a Var assignation
else if (context == "function" && Reserved.vEx.Exists(vd => vd.varName == parts[start]))
{
VariableAssignation va = new VariableAssignation();
va.context = expContext;
return new IdentifyResponse(va, expContext, va.Parse(parts, start));
}
// Else if it is a Desig assignation
else if (context == "function" && Reserved.dEx.Exists(dd => dd.desigName == parts[start]))
{
DesigAssignation da = new DesigAssignation();
da.context = expContext;
return new IdentifyResponse(da, expContext, da.Parse(parts, start));
}
// If it is a variable
else if (context == "variable expression" && Reserved.vEx.Exists(v => v.varName == parts[start]))
{
VarSubExpression vse = new VarSubExpression();
return new IdentifyResponse(vse, expContext, vse.Parse(parts, start));
}
// If it is a variable
else if (context == "desig expression" && Reserved.vEx.Exists(v => v.varName == parts[start]))
{
VarSubExpression vse = new VarSubExpression();
return new IdentifyResponse(vse, expContext, vse.Parse(parts, start));
}
// If it is a desig
else if (context == "desig expression" && Reserved.dEx.Exists(d => d.desigName == parts[start]))
{
DesigSubExpression dse = new DesigSubExpression();
return new IdentifyResponse(dse, expContext, dse.Parse(parts, start));
}
// Nonsensical answer
throw new Exception("Inrecognized expression '" + parts[start] + "' in context '" + context + "'");
}
// Class gathering all file information to be made
public class CompilationOfFiles
{
public class FileContents
{
public string filename;
public string fileContents;
// Directory is to datapack folder, namespace is for namespace folder
public static void CreateFunctionFolders(string directory, string nameSpace)
{
// If no name is ruled
if (!Ruling.hasName)
{
throw new Exception("Name ruling has to be made !");
}
string dirCopy = directory;
/* dir/data/namespace/functions/ */
// Ensure data folder is created
if (!Directory.Exists(directory + "/data"))
{
Directory.CreateDirectory(directory + "/data");
}
directory += "/data";
// Ensure function namespace folder is created
if (!Directory.Exists(directory + "/" + nameSpace))
{
Directory.CreateDirectory(directory + "/" + nameSpace);
}
directory += "/" + nameSpace;
// Ensure functions folder is created
if (!Directory.Exists(directory + "/functions"))
{
Directory.CreateDirectory(directory + "/functions");
}
/* dir/data/minecraft/tags/functions/ */
dirCopy += "/data";
// Ensure minecraft folder is created
if (!Directory.Exists(dirCopy + "/minecraft"))
{
Directory.CreateDirectory(dirCopy + "/minecraft");
}
dirCopy += "/minecraft";
// Ensure tags folder is created
if (!Directory.Exists(dirCopy + "/tags"))
{
Directory.CreateDirectory(dirCopy + "/tags");
}
dirCopy += "/tags";
// Ensure functions folder is created
if (!Directory.Exists(dirCopy + "/functions"))
{
Directory.CreateDirectory(dirCopy + "/functions");
}
}
}
public class ConfigFile
{
// tick or load
public string type;
// namespace:functionname
public string functionCall;
public void Generate(string directory)
{
// Ensure data folder is created
if (!Directory.Exists(directory + "/data"))
{
Directory.CreateDirectory(directory + "/data");
}
directory += "/data";
// Ensure minecraft folder is created
if (!Directory.Exists(directory + "/minecraft"))
{
Directory.CreateDirectory(directory + "/minecraft");
}
directory += "/minecraft";
// Ensure tags folder is created
if (!Directory.Exists(directory + "/tags"))
{
Directory.CreateDirectory(directory + "/tags");
}
directory += "/tags";
// Ensure tags folder is created
if (!Directory.Exists(directory + "/functions"))
{
Directory.CreateDirectory(directory + "/functions");
}
directory += "/functions/";
// Now at datapackname/data/minecraft/tags/functions/
File.WriteAllLines(directory + type + ".json", new string[] {"{","\"values\": [", "\"" + functionCall + "\"", "]","}"});
}
}
// List of functions
List<FileContents> functions;
// List of config files (tags of either tick or load)