-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (64 loc) · 1.77 KB
/
Program.cs
File metadata and controls
78 lines (64 loc) · 1.77 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
using CustomCompiler.CodeAnalysis;
bool showTree = false;
while (true)
{
Console.Write("> ");
var line = Console.ReadLine();
if (string.IsNullOrWhiteSpace(line))
{
return;
}
if (line == "#showTree")
{
showTree = !showTree;
Console.WriteLine(showTree ? "Showing parse trees." : "Not showing parse trees.");
continue;
}
else if (line == "#cls")
{
Console.Clear();
continue;
}
var syntaxTree = SyntaxTree.Parse(line);
var color = Console.ForegroundColor;
if (showTree)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
PrettyPrint(syntaxTree.Root);
Console.ForegroundColor = color;
}
if (syntaxTree.Diagnostics.Count == 0)
{
var e = new Evaluator(syntaxTree.Root);
var result = e.Evaluate();
Console.WriteLine(result);
}
else
{
Console.ForegroundColor = ConsoleColor.DarkRed;
foreach (var diagnostic in syntaxTree.Diagnostics)
{
Console.WriteLine(diagnostic);
}
Console.ForegroundColor = color;
}
}
static void PrettyPrint(SyntaxNode node, string indent = "", bool isLast = true)
{
var marker = isLast ? "└──" : "├──";
Console.Write(indent);
Console.Write(marker);
Console.Write(node.Kind);
if (node is SyntaxToken t && t.Value != null)
{
Console.Write(" ");
Console.Write(t.Value);
}
Console.WriteLine();
indent += isLast ? " " : "│ ";
var lastChild = node.GetChildren().LastOrDefault();
foreach (var child in node.GetChildren())
{
PrettyPrint(child, indent, child == lastChild);
}
}