-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (113 loc) · 5.04 KB
/
Copy pathProgram.cs
File metadata and controls
127 lines (113 loc) · 5.04 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
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
using System;
using System.IO;
using System.Text;
using System.Collections;
using CommonAST = antlr.CommonAST;
using Token = antlr.Token;
using IToken = antlr.IToken;
using CommonToken = antlr.CommonToken;
using AST = antlr.collections.AST;
using CharScanner = antlr.CharScanner;
using CharBuffer = antlr.CharBuffer;
using ASTFrame = antlr.debug.misc.ASTFrame;
using RecognitionException = antlr.RecognitionException;
using TokenStreamException = antlr.TokenStreamException;
using DumpASTVisitor = antlr.DumpASTVisitor;
namespace fpc2il
{
class Program
{
static void ShowCompilerInfo()
{
Console.WriteLine("fpc2il Compiler v0.0.0.1: Mini Pascal to .NET Compiler");
Console.WriteLine("[ Sergio Paracuellos <sergio.paracuellos@gmail.com> ]");
}
static void Main(string[] args)
{
Errors err = Errors.Instance;
bool dumpAST = false;
bool verbose = false;
//Parseamos la linea de comandos
if ( args.Length > 0 )
{
for ( int i = 1; i < args.Length; i++ )
{
if ( args[i] == "--dumpAST" )
dumpAST = true;
else if ( args[i] == "--verbose" || args[i] == "-v" )
verbose = true;
else
Console.Error.WriteLine( "Ignoring unknown flag '" + args[i] + "'" );
}
FileInfo srcFile = new FileInfo( args[0] );
if ( srcFile.Extension == ".pas" )
{
ShowCompilerInfo();
fpc2ilLexer lexer = new fpc2ilLexer( new CharBuffer( srcFile.OpenText() ) );
lexer.setFilename( args[0] );
lexer.setTokenObjectClass("fpc2ilCommonToken");
fpc2ilParser parser = new fpc2ilParser ( lexer );
parser.setFilename( args[0] );
parser.setASTNodeClass("fpc2ilASTWalker");
parser.programa();
if (err.NumErrors != 0)
{
Console.Error.WriteLine();
err.showErrors(args[0]);
Console.Error.WriteLine("Compilation failed: " + err.NumErrors + " error(s)");
}
else
{
//CommonAST t = (CommonAST)parser. getAST();
fpc2ilASTWalker t = (fpc2ilASTWalker)parser. getAST();
//debug: AST grafico para los humanos :-)
//ASTFrame frame = new ASTFrame(args[0], t);
//frame.ShowDialog();
if (dumpAST)
Console.Out.WriteLine(t.ToStringTree());
//DumpASTVisitor visitor = new DumpASTVisitor();
//visitor.visit(t);
fpc2ilTreeParser treeParser = new fpc2ilTreeParser();
treeParser.setASTNodeClass("fpc2ilASTWalker");
treeParser.programa(t);
if (err.NumErrors != 0)
{
Console.Error.WriteLine();
err.showErrors(args[0]);
Console.Error.WriteLine("Compilation failed: " + err.NumErrors + " error(s)");
}
else
{
fpc2ilCodeGenerator gen = new fpc2ilCodeGenerator();
gen.programa(t);
if (err.NumErrors != 0)
{
Console.Error.WriteLine();
err.showErrors(args[0]);
Console.Error.WriteLine("Compilation failed: " + err.NumErrors + " error(s)");
}
else
{
gen.FinishCode();
Console.Error.WriteLine("Compilation succeeded.");
}
}
if (verbose)
{
Symboltable symtab = Symboltable.Instance;
symtab.dump();
}
}
}
else
{
Console.Error.WriteLine("Compilation failed: source filename must have '.pas' extension.");
}
}
else
{
Console.Error.WriteLine( "Usage: compiler.exe src (must have extension '.pas') [flags...]" );
}
}
}
}