-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
112 lines (98 loc) · 3.06 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JFICompiler
{
class Program
{
private static string Input;
private static void Main(string[] Args)
{
try
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("> COMPILING");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("- Reader");
if (!ReadFile(Args[0]))
{
Console.WriteLine("The input file could not be read.");
return;
}
Console.WriteLine("- Tokenizer");
Tokenizer Tokenizer = new Tokenizer(Input);
Token[] Tokens = Tokenizer.Run();
/*
foreach (Token T in Tokens)
{
Console.WriteLine(T.Type + " " + T.Body);
}
*/
Console.WriteLine("- Parser");
Parser Parser = new Parser(Tokens);
Block MainBlock = Parser.Run();
Console.WriteLine("- Generator");
Generator Generator = new Generator(MainBlock);
string Output = Generator.Run();
Console.WriteLine("- Writer");
if (!WriteFile(Args[1], Output))
{
Console.WriteLine("The output file could not be written.");
return;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("> DONE");
Console.ForegroundColor = ConsoleColor.White;
}
catch (Exception E)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("> FAILED");
Console.WriteLine(E.Message);
Console.ForegroundColor = ConsoleColor.White;
}
}
private static bool ReadFile(string Path)
{
FileStream Stream = new FileStream(Path, FileMode.Open);
StreamReader Reader = new StreamReader(Stream);
try
{
Input = Reader.ReadToEnd();
}
catch
{
return false;
}
finally
{
Reader.Close();
Stream.Close();
}
return true;
}
private static bool WriteFile(string Path, string Output)
{
FileStream Stream = new FileStream(Path, FileMode.Create);
StreamWriter Writer = new StreamWriter(Stream);
try
{
Writer.Write(Output);
}
catch
{
return false;
}
finally
{
Writer.Flush();
Writer.Close();
Stream.Close();
}
return true;
}
}
}