-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (44 loc) · 1.72 KB
/
Program.cs
File metadata and controls
53 lines (44 loc) · 1.72 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
using System;
using CodeAnalysis;
namespace compiler {
class Translator {
static void Main(){
Console.WriteLine(">");
var line = Console.ReadLine();
if ( string.IsNullOrWhiteSpace(line)){
return;
}
var syntaxTree = Parser.Parse(line);
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkGray;
PrettyPrint(syntaxTree.Root);
var lexer = new Lexer(line);
if(syntaxTree.Diagnostics.Any()){
Console.ForegroundColor = ConsoleColor.DarkRed;
foreach( var diagtnostics in syntaxTree.Diagnostics){
Console.Write(diagtnostics);
}
Console.ForegroundColor = color;
}else{
var e = new Evaluator(syntaxTree.Root);
var result = e.Evaluate();
Console.WriteLine(result);
}
}
static void PrettyPrint(SyntaxNode node , string indent = "" , bool isLast = false){
Console.Write(indent);
Console.Write(node.Kind);
if(node is SyntaxToken t && t._value != null){
Console.Write(" ");
Console.Write(t._value);
}
Console.WriteLine("");
//indent += " ";
indent += isLast ? "|-" : "|--";
var lastChild = node.GetChildren().LastOrDefault();
foreach(var child in node.GetChildren()){
PrettyPrint(child , indent , node== lastChild);
}
}
}
}