-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.pas
More file actions
188 lines (148 loc) · 3.91 KB
/
parser.pas
File metadata and controls
188 lines (148 loc) · 3.91 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
unit parser;
{$mode ObjFPC}{$H+}
interface
uses
Classes,
SysUtils,
Tokens,
Lexer;
type
TSourceCode = TStringList;
TParser = class(TObject)
private
// file handle
pSourceFile: TextFile;
pSourceCode: TSourceCode;
pStringStack: TNamedStrs;
pIntStack: TNamedInts;
pLexer: TLexer;
pProgram: TProgram;
public
constructor Create(aSourceFile: string);
destructor DestroyParser();
// read all the lines of a file and remove all the unneeded chars
procedure readLines();
// once we read the source-code we will call the lexer
// the lexer will then return a AST (abstract syntax tree)
// which is then "transpiled" to assembler, which is compiled by nasm
procedure callLexer();
// Translates all of the internally represented forms, into assembly
// so that we can call nasm on the assembly and get out a native binary!
procedure TranslateASTToASM();
// getter for the source code, lexer is called
// with internal representation, so this is only needed
// for debug printing steps!
property SourceCode: TSourceCode read pSourceCode;
property StringStack: TNamedStrs read pStringStack;
property IntegerStack: TNamedInts read pIntStack;
end;
{ TParser }
implementation
constructor TParser.Create(aSourceFile: string);
begin
AssignFile(pSourceFile, aSourceFile);
Self.pIntStack := TNamedInts.Create;
Self.pStringStack := TNamedStrs.Create;
Self.pProgram := TProgram.Create;
Self.pLexer := TLexer.Create;
end;
destructor TParser.DestroyParser();
begin
CloseFile(Self.pSourceFile);
if Assigned(pProgram) then pProgram.Destroy;
if Assigned(pLexer) then pLexer.Destroy;
if (Assigned(pStringStack)) then Self.pStringStack.Free;
if (Assigned(pIntStack)) then Self.pIntStack.Free;
inherited;
end;
procedure TParser.readLines();
var
lSourceText: string;
begin
Self.pSourceCode := TStringList.Create;
try
// reset seek header, like in c
Reset(pSourceFile);
// read all the file
while not EOF(Self.pSourceFile) do
begin
ReadLn(pSourceFile, lSourceText);
if (lSourceText = string.Empty) then continue;
pSourceCode.Add(lSourceText);
end;
except
on E: EInOutError do
begin
WriteLn('Failed to read from file!!' );
Exit;
end;
end;
end;
procedure TParser.callLexer();
var
lFormDef: TFormDef;
{lCallStack: TCallStack;}
lLine: string;
iCounter: integer;
{iCallDepth: integer;}
begin
iCounter := 0;
for lLine in Self.pSourceCode do
begin
lFormDef := pLexer.eval(lLine, iCounter, pStringStack, pIntStack);
Inc(iCounter);
pProgram.Add(iCounter, lFormDef);
end;
end;
procedure TParser.TranslateASTToASM();
var
lForm: TFormDef;
lArgs: TStringList;
lArgName: string;
lIntResult: integer;
lStrResult: string;
iIntCounter: integer;
begin
for lForm in pProgram.Values do
begin
case lForm.FunToEval of
TOperation.add:
begin
lArgs := lForm.Argumenst;
lIntResult := 0;
for lArgName in lArgs do
begin
lIntResult := lIntResult + IntegerStack.Items[lArgName];
end;
WriteLn('ADD> ' + IntToStr(lIntResult));
end;
TOperation.sub:
begin
lArgs := lForm.Argumenst;
lIntResult := 0;
iIntCounter := 0;
for lArgName in lArgs do
begin
if (iIntCounter = 0) then
begin
lIntResult := IntegerStack.Items[lArgName];
Inc(iIntCounter);
end
else lIntResult := lIntResult - IntegerStack.Items[lArgName];
end;
WriteLn('SUB> ' + IntToStr(lIntResult));
end;
TOperation.Write:
begin
lArgs := lForm.Argumenst;
lStrResult := string.Empty;
for lArgName in lArgs do
begin
lStrResult := lStrResult + StringStack.Items[lArgName];
end;
WriteLn('WRITE> ' + lStrResult);
end;
end;
end;
end;
end.