/
main.cs
109 lines (101 loc) · 3.54 KB
/
main.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Compiler.Forms;
using System.Windows.Forms;
namespace Compiler{
class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public static bool Compile(string[] args)
{
StreamReader istr = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding);
StreamWriter ostr = new StreamWriter(System.Console.OpenStandardOutput(), Console.OutputEncoding);
if (args.Count() < 2)
{
return false;
}
int index_in = 1, index_out = 2;
istr = new StreamReader(args[index_in]);
if (index_out < args.Count())
{
ostr = new StreamWriter(args[index_out]);
}
Scaner scaner = null;
Parser parser = null;
Fasm.CodeGen codegen = null;
switch (args[0])
{
case "-l":
scaner = new Scaner(istr);
Token t = null;
while (t == null || t.type != Token.Type.EOF)
{
try
{
t = scaner.Read();
Console.WriteLine(t.ToString());
}
catch (Scaner.Exception e)
{
ostr.WriteLine(e.Message);
}
}
break;
case "-p":
parser = new Parser(new Scaner(istr));
parser.Parse();
parser.PrintTree(ostr);
ostr.WriteLine(parser.logger.ToString());
break;
case "-c":
parser = new Parser(new Scaner(istr));
parser.Parse();
if (!parser.logger.isEmpty())
{
ostr.WriteLine(parser.logger.ToString());
break;
}
codegen = new Fasm.CodeGen(parser.tstack);
codegen.Generate(ostr);
break;
case "-cexe":
parser = new Parser(new Scaner(istr));
parser.Parse();
if (!parser.logger.isEmpty())
{
ostr.WriteLine(parser.logger.ToString());
break;
}
codegen = new Fasm.CodeGen(parser.tstack);
codegen.Generate(ostr);
ostr.Flush();
ostr.Close();
if (index_out < args.Count())
{
Process.Start(new ProcessStartInfo
{
FileName = "C:/fasm/fasm.exe",
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = string.Format("{0} {1}", args[index_out], args[index_out])
});
}
break;
default:
return false;
}
istr.Close();
ostr.Close();
return true;
}
}
}