-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cs
146 lines (137 loc) · 3.93 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Compiler{
class Program
{
static void Main(string[] args)
{
StreamReader istr = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding);
StreamWriter ostr = new StreamWriter(System.Console.OpenStandardOutput(), Console.OutputEncoding);
if (args.Count() < 2)
{
Help();
return;
}
bool low_opt = false, hight_opt = false;
int index_in = 2, index_out = 3;
switch (args[1])
{
case "-low":
low_opt = true;
break;
case "-hight":
hight_opt = true;
break;
case "-low-hight":
low_opt = hight_opt = true;
break;
default:
index_in = 1;
index_out = 2;
break;
}
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:
Help();
return;
}
istr.Close();
ostr.Close();
}
static void Help()
{
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("Compiler supposed to compile C language with some restrictions:");
Console.WriteLine("1) not supported preprocessor");
Console.WriteLine("2) not supported type float");
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine();
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine("how to used:");
Console.WriteLine(">compiler mode [optimize] input_file [output_file]");
Console.WriteLine("mode:");
Console.WriteLine("\t-l\tdo lexical analysis; print lexem");
Console.WriteLine("\t-p\tparse; print syntax trees, vars, types");
Console.WriteLine("\t-c\tgenerate asm code");
Console.WriteLine("\t-cexe\tcompile exe file");
Console.WriteLine();
Console.WriteLine("optimize:");
Console.WriteLine("\t-low\toptimize asm code");
Console.WriteLine("\t-hight\toptimize syntax trees");
Console.WriteLine("\t-low-height\t optimize syntax trees and asm code");
Console.WriteLine("input_file:");
Console.WriteLine("\t\tfile contains sourse code C language");
Console.WriteLine("output_file:");
Console.WriteLine("\t\tif no path specified, the program outputs to stdout");
Console.WriteLine("-----------------------------------------------------------------------------");
}
}
}