-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdemo.dpr
122 lines (107 loc) · 3.93 KB
/
demo.dpr
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
program demo;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Classes,
System.Generics.Collections,
DSiWin32,
SimpleDSLCompiler in 'SimpleDSLCompiler.pas',
SimpleDSLCompiler.Parser in 'SimpleDSLCompiler.Parser.pas',
SimpleDSLCompiler.AST in 'SimpleDSLCompiler.AST.pas',
SimpleDSLCompiler.Runnable in 'SimpleDSLCompiler.Runnable.pas',
SimpleDSLCompiler.Compiler in 'SimpleDSLCompiler.Compiler.pas',
SimpleDSLCompiler.ErrorInfo in 'SimpleDSLCompiler.ErrorInfo.pas',
SimpleDSLCompiler.Base in 'SimpleDSLCompiler.Base.pas',
SimpleDSLCompiler.Tokenizer in 'SimpleDSLCompiler.Tokenizer.pas',
SimpleDSLCompiler.Compiler.Dump in 'SimpleDSLCompiler.Compiler.Dump.pas',
SimpleDSLCompiler.Compiler.Codegen in 'SimpleDSLCompiler.Compiler.Codegen.pas',
SimpleDSLCompiler.Interpreter in 'SimpleDSLCompiler.Interpreter.pas';
const
CMultiProcCode =
'fib(i) [memo] { '#13#10 +
' if i < 3 { '#13#10 +
' return 1 '#13#10 +
' } else { '#13#10 +
' return fib(i-2) + fib(i-1) '#13#10 +
' } '#13#10 +
'} '#13#10 +
' '#13#10 +
'mult(a,b) { '#13#10 +
' if b < 2 { '#13#10 +
' return a '#13#10 +
' } else { '#13#10 +
' return mult(a, b-1) + a '#13#10 +
' } '#13#10 +
'} '#13#10 +
' '#13#10 +
'power(a,b) { '#13#10 +
' if b < 2 { '#13#10 +
' return a '#13#10 +
' } '#13#10 +
' else { '#13#10 +
' return mult(power(a, b-1), a) '#13#10 +
' } '#13#10 +
'} '#13#10;
var
compiler : ISimpleDSLCompiler;
exec : ISimpleDSLProgram;
fibComp : TFunctionCall;
interpreter: ISimpleDSLProgram;
res : integer;
sl : TStringList;
time : int64;
function fib(i: integer): integer;
begin
if i < 3 then
Result := 1
else
Result := fib(i-2) + fib(i-1);
end;
begin
try
sl := TStringList.Create;
try
compiler := CreateSimpleDSLCompiler;
compiler.CodegenFactory := function: ISimpleDSLCodegen begin Result := CreateSimpleDSLCodegenDump(sl); end;
compiler.Compile(CMultiProcCode);
Writeln(sl.Text);
finally FreeAndNil(sl); end;
compiler := CreateSimpleDSLCompiler;
if not compiler.Compile(CMultiProcCode) then
Writeln('Compilation/codegen error: ' + (compiler as ISimpleDSLErrorInfo).ErrorInfo)
else begin
exec := compiler.Code;
if exec.Call('mult', [5,3], res) then
Writeln('mult(5,3) = ', res)
else
Writeln('mult: ' + (exec as ISimpleDSLErrorInfo).ErrorInfo);
Writeln('2^10 = ', exec.Make('power')([2,10]));
fibComp := exec.Make('fib');
Writeln(fib(7));
Writeln(fibComp([7]));
time := DSiTimeGetTime64;
res := fib(30);
time := DSiElapsedTime64(time);
Writeln('Native: ', res, ' in ', time, ' ms');
time := DSiTimeGetTime64;
res := fibComp([30]);
time := DSiElapsedTime64(time);
Writeln('Compiled: ', res, ' in ', time, ' ms');
interpreter := CreateSimpleDSLInterpreter(compiler.AST);
time := DSiTimeGetTime64;
res := 0;
if not interpreter.Call('fib', [30], res) then
Writeln('interpreter: ' + (interpreter as ISimpleDSLErrorInfo).ErrorInfo)
else begin
time := DSiElapsedTime64(time);
Writeln('Interpreted: ', res, ' in ', time, ' ms');
end;
end;
Write('> ');
Readln;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.