Repository navigation
Expand file tree
/
Copy pathJavascript4D.dpr
More file actions
192 lines (164 loc) · 7.86 KB
/
Copy pathJavascript4D.dpr
File metadata and controls
192 lines (164 loc) · 7.86 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
189
190
191
192
program Javascript4D;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
JS4D.Types in 'Source\Core\JS4D.Types.pas',
JS4D.ArrayObject in 'Source\Core\JS4D.ArrayObject.pas',
JS4D.RegExpObject in 'Source\Core\JS4D.RegExpObject.pas',
JS4D.SymbolObject in 'Source\Core\JS4D.SymbolObject.pas',
JS4D.Errors in 'Source\Core\JS4D.Errors.pas',
JS4D.RegExpSyntax in 'Source\Core\JS4D.RegExpSyntax.pas',
JS4D.RegExpTranslator in 'Source\Core\JS4D.RegExpTranslator.pas',
JS4D.PcreMatcher in 'Source\Core\JS4D.PcreMatcher.pas',
JS4D.Tokens in 'Source\Lexer\JS4D.Tokens.pas',
JS4D.Lexer in 'Source\Lexer\JS4D.Lexer.pas',
JS4D.LexicalCharacters in 'Source\Lexer\JS4D.LexicalCharacters.pas',
JS4D.SlashContext in 'Source\Lexer\JS4D.SlashContext.pas',
JS4D.AST in 'Source\Parser\JS4D.AST.pas',
JS4D.Parser in 'Source\Parser\JS4D.Parser.pas',
JS4D.ParserScope in 'Source\Parser\JS4D.ParserScope.pas',
JS4D.ParserConstants in 'Source\Parser\JS4D.ParserConstants.pas',
JS4D.ParserTypes in 'Source\Parser\JS4D.ParserTypes.pas',
JS4D.ParserBase in 'Source\Parser\JS4D.ParserBase.pas',
JS4D.ParserPatterns in 'Source\Parser\JS4D.ParserPatterns.pas',
JS4D.ParserExpressions in 'Source\Parser\JS4D.ParserExpressions.pas',
JS4D.ParserFunctions in 'Source\Parser\JS4D.ParserFunctions.pas',
JS4D.ParserRules in 'Source\Parser\JS4D.ParserRules.pas',
JS4D.ParserLookahead in 'Source\Parser\JS4D.ParserLookahead.pas',
JS4D.PatternConverter in 'Source\Parser\JS4D.PatternConverter.pas',
JS4D.PropertyKey in 'Source\Core\JS4D.PropertyKey.pas',
JS4D.BinaryData in 'Source\Runtime\JS4D.BinaryData.pas',
JS4D.ControlFlow in 'Source\Runtime\JS4D.ControlFlow.pas',
JS4D.LexicalStates in 'Source\Runtime\JS4D.LexicalStates.pas',
JS4D.JsonSyntax in 'Source\API\JS4D.JsonSyntax.pas',
JS4D.Builtins in 'Source\Runtime\JS4D.Builtins.pas',
JS4D.PatternOperations in 'Source\Runtime\JS4D.PatternOperations.pas',
JS4D.Interpreter in 'Source\Runtime\JS4D.Interpreter.pas',
JS4D.JsonWriter in 'Source\API\JS4D.JsonWriter.pas',
JS4D.Engine in 'Source\API\JS4D.Engine.pas';
begin
try
var Engine := TJSEngine.Create;
try
WriteLn('JavaScript4D - JavaScript Parser & Interpreter for Delphi');
WriteLn('ECMAScript 5.1 with ES2015 features');
WriteLn('');
WriteLn('Test 1: Simple expression');
var Result := Engine.Evaluate('1 + 2 * 3');
WriteLn(' 1 + 2 * 3 = ' + Result.ToString);
WriteLn('');
WriteLn('Test 2: Variables');
Engine.Execute('var x = 10;');
Result := Engine.Evaluate('x + 5');
WriteLn(' var x = 10; x + 5 = ' + Result.ToString);
WriteLn('');
WriteLn('Test 3: Function declaration and call');
Engine.Execute('function add(a, b) { return a + b; }');
Result := Engine.Evaluate('add(2, 3)');
WriteLn(' function add(a, b) { return a + b; } add(2, 3) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 4: Object literals');
Engine.Execute('var obj = { x: 1, y: 2 };');
Result := Engine.Evaluate('obj.x + obj.y');
WriteLn(' var obj = { x: 1, y: 2 }; obj.x + obj.y = ' + Result.ToString);
WriteLn('');
WriteLn('Test 5: Array literals');
Engine.Execute('var arr = [1, 2, 3, 4, 5];');
Result := Engine.Evaluate('arr[2]');
WriteLn(' var arr = [1, 2, 3, 4, 5]; arr[2] = ' + Result.ToString);
WriteLn('');
WriteLn('Test 6: Control flow (if statement)');
Engine.Execute('var result = ""; if (x > 5) { result = "positive"; } else { result = "non-positive"; }');
Result := Engine.GetVariable('result');
WriteLn(' if (x > 5) result = "positive" else result = "non-positive" -> result = ' + Result.ToString);
WriteLn('');
WriteLn('Test 7: For loop');
Engine.Execute('var sum = 0; for (var i = 1; i <= 5; i++) { sum += i; }');
Result := Engine.GetVariable('sum');
WriteLn(' for (var i = 1; i <= 5; i++) sum += i; -> sum = ' + Result.ToString);
WriteLn('');
WriteLn('Test 8: Math object');
Result := Engine.Evaluate('Math.sqrt(16) + Math.pow(2, 3)');
WriteLn(' Math.sqrt(16) + Math.pow(2, 3) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 9: String concatenation');
Result := Engine.Evaluate('"Hello, " + "World!"');
WriteLn(' "Hello, " + "World!" = ' + Result.ToString);
WriteLn('');
WriteLn('Test 10: Console output');
Engine.Execute('console.log("This is a console.log test");');
WriteLn('');
WriteLn('Test 11: Array.map');
Engine.Execute('var nums = [1, 2, 3, 4, 5];');
Engine.Execute('var doubled = nums.map(function(x) { return x * 2; });');
Result := Engine.Evaluate('doubled.join(",")');
WriteLn(' [1,2,3,4,5].map(x => x*2) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 12: Array.filter');
Engine.Execute('var evens = nums.filter(function(x) { return x % 2 == 0; });');
Result := Engine.Evaluate('evens.join(",")');
WriteLn(' [1,2,3,4,5].filter(x => x%2==0) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 13: Array.reduce');
Result := Engine.Evaluate('nums.reduce(function(acc, x) { return acc + x; }, 0)');
WriteLn(' [1,2,3,4,5].reduce((acc,x) => acc+x, 0) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 14: String.split and join');
Result := Engine.Evaluate('"hello world".split(" ").join("-")');
WriteLn(' "hello world".split(" ").join("-") = ' + Result.ToString);
WriteLn('');
WriteLn('Test 15: String.toUpperCase');
Result := Engine.Evaluate('"hello".toUpperCase()');
WriteLn(' "hello".toUpperCase() = ' + Result.ToString);
WriteLn('');
WriteLn('Test 16: Array.find');
Result := Engine.Evaluate('nums.find(function(x) { return x > 3; })');
WriteLn(' [1,2,3,4,5].find(x => x>3) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 17: Array.some and every');
Result := Engine.Evaluate('nums.some(function(x) { return x > 3; })');
WriteLn(' [1,2,3,4,5].some(x => x>3) = ' + Result.ToString);
Result := Engine.Evaluate('nums.every(function(x) { return x > 0; })');
WriteLn(' [1,2,3,4,5].every(x => x>0) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 18: Array.sort');
Engine.Execute('var unsorted = [3, 1, 4, 1, 5, 9, 2, 6];');
Result := Engine.Evaluate('unsorted.sort().join(",")');
WriteLn(' [3,1,4,1,5,9,2,6].sort() = ' + Result.ToString);
WriteLn('');
WriteLn('Test 19: Object.keys');
Engine.Execute('var person = { name: "John", age: 30, city: "NYC" };');
Result := Engine.Evaluate('Object.keys(person).join(",")');
WriteLn(' Object.keys({name,age,city}) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 20: Object.values');
Result := Engine.Evaluate('Object.values(person).join(",")');
WriteLn(' Object.values({name:"John",age:30,city:"NYC"}) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 21: JSON.stringify');
Result := Engine.Evaluate('JSON.stringify(person)');
WriteLn(' JSON.stringify(person) = ' + Result.ToString);
WriteLn('');
WriteLn('Test 22: JSON.parse');
Engine.Execute('var parsed = JSON.parse(''{"x":1,"y":2}'');');
Result := Engine.Evaluate('parsed.x + parsed.y');
WriteLn(' JSON.parse(''{"x":1,"y":2}'').x + .y = ' + Result.ToString);
WriteLn('');
WriteLn('Test 23: Array.isArray');
Result := Engine.Evaluate('Array.isArray([1,2,3])');
WriteLn(' Array.isArray([1,2,3]) = ' + Result.ToString);
Result := Engine.Evaluate('Array.isArray("hello")');
WriteLn(' Array.isArray("hello") = ' + Result.ToString);
WriteLn('');
WriteLn('All tests completed successfully!');
finally
Engine.Free;
end;
except
on E: Exception do
WriteLn('Error: ', E.ClassName, ': ', E.Message);
end;
WriteLn('');
WriteLn('Press Enter to exit...');
ReadLn;
end.