-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleDSLCompiler.pas
239 lines (213 loc) · 7.49 KB
/
SimpleDSLCompiler.pas
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
unit SimpleDSLCompiler;
/// DSL definition
///
/// program = {function}
///
/// function = identifier "(" [ identifier { "," identifier } ] ")" [ "[" attribute_list "]" ] block
///
/// attribute_list = identifier { ',' identifier }
///
/// block = "{" statement { ";" statement } [";"] "}"
///
/// statement = if
/// | return
///
/// if = "if" expression block "else" block
///
/// return = "return" expression
///
/// expression = term
/// | term operator term
///
/// term = numeric_constant
/// | function_call
/// | identifier
///
/// operator = "+" | "-" | "<"
///
/// function_call = identifier "(" [expression { "," expression } ] ")"
///
/// - Spacing is ignored.
/// - Only data type is integer.
/// - "If" executes "then" block if expression is <> 0 and "else" block if expression = 0.
/// - There is no assignment.
/// - Only operations are: +, -, <.
/// - Parameters are always passed by value.
/// - "Return" just sets a return value, it doesn't interrupt control flow.
/// - Function without a "return" statement returns 0.
///
/// Example:
/// fib(i) {
/// if i < 2 {
/// return 1
/// } else {
/// return fib(i-2) + fib(i-1)
/// }
/// }
///
/// mult(a,b) {
/// if b < 2 {
/// return a
/// } else {
/// return mult(a, b-1) + a
/// }
/// }
interface
uses
SimpleDSLCompiler.Runnable,
SimpleDSLCompiler.AST,
SimpleDSLCompiler.Tokenizer,
SimpleDSLCompiler.Parser,
SimpleDSLCompiler.Compiler;
type
ISimpleDSLCompiler = interface ['{7CF78EC7-023B-4571-B310-42873921B0BC}']
function GetAST: ISimpleDSLAST;
function GetASTFactory: TSimpleDSLASTFactory;
function GetCode: ISimpleDSLProgram;
function GetCodegenFactory: TSimpleDSLCodegenFactory;
function GetParserFactory: TSimpleDSLParserFactory;
function GetTokenizerFactory: TSimpleDSLTokenizerFactory;
procedure SetASTFactory(const value: TSimpleDSLASTFactory);
procedure SetCodegenFactory(const value: TSimpleDSLCodegenFactory);
procedure SetParserFactory(const value: TSimpleDSLParserFactory);
procedure SetTokenizerFactory(const value: TSimpleDSLTokenizerFactory);
//
function Codegen: boolean;
function Compile(const code: string): boolean;
function Parse(const code: string): boolean;
property AST: ISimpleDSLAST read GetAST;
property Code: ISimpleDSLProgram read GetCode;
property ASTFactory: TSimpleDSLASTFactory read GetASTFactory write SetASTFactory;
property CodegenFactory: TSimpleDSLCodegenFactory read GetCodegenFactory write SetCodegenFactory;
property ParserFactory: TSimpleDSLParserFactory read GetParserFactory write SetParserFactory;
property TokenizerFactory: TSimpleDSLTokenizerFactory read GetTokenizerFactory write SetTokenizerFactory;
end; { TSimpleDSLCompiler }
function CreateSimpleDSLCompiler: ISimpleDSLCompiler;
implementation
uses
SimpleDSLCompiler.Base,
SimpleDSLCompiler.ErrorInfo;
type
TSimpleDSLCompiler = class(TSimpleDSLCompilerBase, ISimpleDSLCompiler)
strict private
FAST : ISimpleDSLAST;
FASTFactory : TSimpleDSLASTFactory;
FCode : ISimpleDSLProgram;
FCodegenFactory : TSimpleDSLCodegenFactory;
FParserFactory : TSimpleDSLParserFactory;
FTokenizerFactory: TSimpleDSLTokenizerFactory;
strict protected
function GetAST: ISimpleDSLAST;
function GetASTFactory: TSimpleDSLASTFactory; inline;
function GetCode: ISimpleDSLProgram; inline;
function GetCodegenFactory: TSimpleDSLCodegenFactory; inline;
function GetParserFactory: TSimpleDSLParserFactory; inline;
function GetTokenizerFactory: TSimpleDSLTokenizerFactory; inline;
procedure SetASTFactory(const value: TSimpleDSLASTFactory); inline;
procedure SetCodegenFactory(const value: TSimpleDSLCodegenFactory); inline;
procedure SetParserFactory(const value: TSimpleDSLParserFactory); inline;
procedure SetTokenizerFactory(const value: TSimpleDSLTokenizerFactory); inline;
public
constructor Create;
function Codegen: boolean;
function Compile(const code: string): boolean;
function Parse(const code: string): boolean;
property AST: ISimpleDSLAST read GetAST;
property Code: ISimpleDSLProgram read GetCode;
property ASTFactory: TSimpleDSLASTFactory read GetASTFactory write SetASTFactory;
property CodegenFactory: TSimpleDSLCodegenFactory read GetCodegenFactory write SetCodegenFactory;
property ParserFactory: TSimpleDSLParserFactory read GetParserFactory write SetParserFactory;
property TokenizerFactory: TSimpleDSLTokenizerFactory read GetTokenizerFactory write SetTokenizerFactory;
end; { TSimpleDSLCompiler }
{ exports }
function CreateSimpleDSLCompiler: ISimpleDSLCompiler;
begin
Result := TSimpleDSLCompiler.Create;
end; { CreateSimpleDSLCompiler }
{ TSimpleDSLCompiler }
constructor TSimpleDSLCompiler.Create;
begin
inherited Create;
ASTFactory := CreateSimpleDSLAST;
CodegenFactory := CreateSimpleDSLCodegen;
ParserFactory := CreateSimpleDSLParser;
TokenizerFactory := CreateSimpleDSLTokenizer;
end; { TSimpleDSLCompiler.Create }
function TSimpleDSLCompiler.Codegen: boolean;
var
codegen : ISimpleDSLCodegen;
begin
LastError := '';
if not assigned(FAST) then
Exit(SetError('Nothing to do'))
else begin
codegen := CodegenFactory();
Result := codegen.Generate(FAST, FCode);
if not Result then begin
FCode := nil;
LastError := (codegen as ISimpleDSLErrorInfo).ErrorInfo;
end;
end;
end; { TSimpleDSLCompiler.Codegen }
function TSimpleDSLCompiler.Compile(const code: string): boolean;
begin
Result := Parse(code);
if Result then
Result := Codegen;
end; { TSimpleDSLCompiler.Compile }
function TSimpleDSLCompiler.GetAST: ISimpleDSLAST;
begin
Result := FAST;
end; { TSimpleDSLCompiler.GetAST }
function TSimpleDSLCompiler.GetASTFactory: TSimpleDSLASTFactory;
begin
Result := FASTFactory;
end; { TSimpleDSLCompiler.GetASTFactory }
function TSimpleDSLCompiler.GetCode: ISimpleDSLProgram;
begin
Result := FCode;
end; { TSimpleDSLCompiler.GetCode }
function TSimpleDSLCompiler.GetCodegenFactory: TSimpleDSLCodegenFactory;
begin
Result := FCodegenFactory;
end; { TSimpleDSLCompiler.GetCodegenFactory }
function TSimpleDSLCompiler.GetParserFactory: TSimpleDSLParserFactory;
begin
Result := FParserFactory;
end; { TSimpleDSLCompiler.GetParserFactory }
function TSimpleDSLCompiler.GetTokenizerFactory: TSimpleDSLTokenizerFactory;
begin
Result := FTokenizerFactory;
end; { TSimpleDSLCompiler.GetTokenizerFactory }
function TSimpleDSLCompiler.Parse(const code: string): boolean;
var
parser : ISimpleDSLParser;
tokenizer: ISimpleDSLTokenizer;
begin
LastError := '';
parser := ParserFactory();
tokenizer := TokenizerFactory();
FAST := ASTFactory();
Result := parser.Parse(code, tokenizer, FAST);
if not Result then begin
FAST := nil;
LastError := (parser as ISimpleDSLErrorInfo).ErrorInfo;
end
end; { TSimpleDSLCompiler.Parse }
procedure TSimpleDSLCompiler.SetASTFactory(const value: TSimpleDSLASTFactory);
begin
FASTFactory := value;
end; { TSimpleDSLCompiler.SetASTFactory }
procedure TSimpleDSLCompiler.SetCodegenFactory(const value: TSimpleDSLCodegenFactory);
begin
FCodegenFactory := value;
end; { TSimpleDSLCompiler.SetCodegenFactory }
procedure TSimpleDSLCompiler.SetParserFactory(const value: TSimpleDSLParserFactory);
begin
FParserFactory := value;
end; { TSimpleDSLCompiler.SetParserFactory }
procedure TSimpleDSLCompiler.SetTokenizerFactory(const value: TSimpleDSLTokenizerFactory);
begin
FTokenizerFactory := value;
end; { TSimpleDSLCompiler.SetTokenizerFactory }
end.