forked from karbis/bruhlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
63 lines (62 loc) · 2.56 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
using System.Text;
using System.Diagnostics;
namespace bruhlang {
internal class Program {
static void Main(string[] args) {
string fileName = args.ElementAtOrDefault(0) ?? "code.txt";
if (!File.Exists(fileName)) {
Console.WriteLine("File doesn't exist!");
return;
}
bool debugMode = (args.ElementAtOrDefault(1) ?? "") == "-d" || Debugger.IsAttached;
List<Token> tokens = Lexer.Parse(File.ReadAllText(fileName));
if (debugMode) {
Console.WriteLine(" -- Tokens -- ");
Console.WriteLine(ReadTokens(tokens));
}
Node tree = TreeCreator.Create(tokens);
if (debugMode) {
Console.WriteLine(" -- AST -- ");
Console.WriteLine(ReadAST(tree));
Console.WriteLine(" -- Runtime -- ");
}
Stopwatch watch = new Stopwatch();
watch.Start();
Parser result = new Parser(tree);
watch.Stop();
if (debugMode) {
Console.WriteLine("");
Console.WriteLine(" -- Performance -- ");
Console.WriteLine("Took " + watch.ElapsedMilliseconds + "ms to run");
Tests.Test();
}
}
public static string ReadScopes(Scope inputScope, int depth = 0) {
string indent = new string(' ', depth * 4);
string str = indent + "Scope: \n";
indent += " ";
foreach (KeyValuePair<string, dynamic?> variable in inputScope.Variables) {
str += indent + variable.Key + ": " + variable.Value + "\n";
}
//foreach (Scope newScope in inputScope.Scopes) {
// str += ReadScopes(newScope, depth + 1);
//}
return str;
}
public static string ReadAST(Node inputNode, Node? currentNode = null, int depth = 0) {
string indent = new string(' ', depth * 4);
string str = indent + inputNode.Type + ": " + inputNode.Value + (currentNode == inputNode ? " !!!" : "") + "\n";
foreach (Node node in inputNode.Nodes) {
str += ReadAST(node, currentNode, depth+1);
}
return str;
}
public static string ReadTokens(List<Token> result) {
string visual = "";
foreach (Token token in result) {
visual += "[" + token.Type + ": [" + token.Value + "]]\n";
}
return visual;
}
}
}