-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
50 lines (43 loc) · 1.85 KB
/
Program.cs
File metadata and controls
50 lines (43 loc) · 1.85 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
using NCCompiler_CompilersCourse.CodeGeneration;
using NCCompiler_CompilersCourse.Lexer;
using NCCompiler_CompilersCourse.Semantics;
namespace NCCompiler_CompilersCourse;
class Program
{
public static void Main(string[] args)
{
while (true)
{
string? fileName = Console.ReadLine();
if (fileName == "stop") break;
// while ((fileName = Console.ReadLine()) == null) { }
// Console.WriteLine(fileName);
string contents = File.ReadAllText(fileName!);
// Console.WriteLine(contents);
Lexer.Lexer lexer = new Lexer.Lexer(contents);
// foreach (var token in lexer.GetTokens())
// {
// Console.WriteLine(
// $"Token: {token.Type}, Lexeme: {token.Lexeme}, Value: {token.Value}, Span: {token.Span}"
// );
// }
Lexer.Scanner scanner = new Lexer.Scanner(lexer);
Parser.Parser parser = new Parser.Parser(scanner);
var res = parser.Parse();
Console.WriteLine($"Syntax analysis: {res}");
if (res)
{
var rootNode = parser.RootNode;
EvalVisitor visitor = new EvalVisitor(true);
var rootSymbolic = rootNode.Accept(visitor);
Console.WriteLine(res);
// if (!((ProgramNode) rootSymbolic).HasMain()) throw new Exception("No main function in code");
var codeGenVisit = new TranslationVisitorCodeGeneration();
rootSymbolic.Accept(codeGenVisit, new Queue<BaseCommand>());
var programStr = codeGenVisit.ResultingProgram;
File.WriteAllText("compiledProgram.il", programStr);
Console.WriteLine("Code generated");
}
}
}
}