-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (149 loc) · 6.24 KB
/
Program.cs
File metadata and controls
151 lines (149 loc) · 6.24 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
using Automata.Backbone;
using Automata.DefaultFunctions;
using Automata.Parser;
using System.Text;
namespace automataV2
{
internal class Program
{
static void PrintHelp()
{
Console.WriteLine("General Usage:\n amta file_name [--max_while_loops N]\nfile_name is the file which will be interpreted.\nThe file is treated as a function block and ran as such. You can also use 'return' to return an exit value\n\n--max_while_loops N - specify maximum number of times a while block can run.\nIf you want to disable the limit, use -1\nDefault: 10000\n\namta --help");
}
static void Main(string[] args)
{
// automata cli
#if DEBUG
#else
Console.WriteLine("automata CLI.\nMade by devilexe\n\n");
if(args.Length == 0)
{
PrintHelp();
return;
}
string? amta_file = null;
int maxWhileLoops = 10000;
for(int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("--"))
{
switch (args[i])
{
case "--help":
PrintHelp();
return;
case "--max_while_loops":
maxWhileLoops = int.Parse(args[i + 1]);
++i;
break;
}
}
else
amta_file = args[i];
}
if(amta_file == null)
{
PrintHelp();
return;
}
var program = ProgramCleaner.CleanProgram(File.ReadAllText(amta_file));
var tokens = Tokenizer.Tokenize(program);
if (tokens.Count == 0)
{
Console.WriteLine("Input file is empty");
return;
}
var instr = new ProgramParser(tokens).ParseProgram();
var scope = new Scope(maxWhileLoops == -1 ? int.MaxValue : maxWhileLoops);
DefaultFunctions.RegisterFunctions(scope);
// create fake fn runner
var fileRunner = new FunctionRunner([], instr);
// scope file
var fileScope = new Scope(scope);
var retVal = fileRunner.Call(fileScope);
Console.WriteLine("\n--- Program exited with value: " + retVal.Stringify().Value);
return;
#endif
#if DEBUG
while (true)
{
var program = "";
while (true)
{
var line = Console.ReadLine()!;
if (line != "")
program += line + "\n";
else
{
program = ProgramCleaner.CleanProgram(program);
var tokens = Tokenizer.Tokenize(program);
if (tokens.Count == 0)
break;
Token.PrintTokens(tokens);
var instr = new ProgramParser(tokens).ParseProgram();
Console.WriteLine("Parsed program:");
foreach (var ins in instr)
Console.WriteLine(ins);
// create scope and run script
Console.WriteLine("Running program:\n");
var scope = new Scope();
DefaultFunctions.RegisterFunctions(scope);
// create fake fn runner
var fileRunner = new FunctionRunner([], instr);
// scope file
var fileScope = new Scope(scope);
var retVal = fileRunner.Call(fileScope);
Console.WriteLine("\n--- Program exited with value: " + retVal.Stringify().Value);
break;
}
}
}
#endif
// tests
//foreach (var file in Directory.GetFiles("Tests"))
//{
// if (!file.EndsWith(".amta")) continue;
// var program = ProgramCleaner.CleanProgram(File.ReadAllText(file));
// var tokens = Tokenizer.Tokenize(program);
// try
// {
// var instr = new ProgramParser(tokens).ParseProgram();
// var scope = new Scope();
// DefaultFunctions.RegisterFunctions(scope);
// // redifine :print function
// StringBuilder output = new();
// scope.SetVariable(":print", new FunctionValue(new NativeFunction([(new VarNameResolver("!str"), BaseValue.ValueType.AnyType)], args =>
// {
// output.Append(args[0].Stringify().Value);
// return NilValue.Nil;
// })));
// var fileRunner = new FunctionRunner([], instr);
// var fileScope = new Scope(scope);
// try
// {
// var retVal = fileRunner.Call(fileScope);
// // compare output to test
// var expected = File.ReadAllText(file.Replace(".amta", ".out"));
// if (expected == output.ToString())
// Console.WriteLine("[PASS] " + Path.GetFileNameWithoutExtension(file));
// else
// {
// Console.WriteLine("[FAIL] " + Path.GetFileNameWithoutExtension(file));
// Console.WriteLine("GOT:\n" + output.ToString());
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine("[ERRR] " + Path.GetFileNameWithoutExtension(file) + "\n" + ex.ToString());
// continue;
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine("[ERRP] " + Path.GetFileNameWithoutExtension(file) + "\n" + ex.ToString());
// continue;
// }
//}
}
}
}