This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.script.ts
234 lines (202 loc) · 9.73 KB
/
core.script.ts
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
/// <reference path="../typings/chai-as-promised/chai-as-promised.d.ts"/>
/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/es6-polyfill/es6-polyfill.d.ts" />
import Script = require('../core/src/script');
import Chai = require('chai');
const assert = Chai.assert;
function tokeniseAssert (input: string, expectedOutput: string[])
{
const output = Script.tokenise(input);
return assert.deepEqual(output, expectedOutput);
}
function parseAssert (input: string, expectedOutput: Script.Expression)
{
const output = Script.parse(input);
return assert.deepEqual(output, expectedOutput);
}
function evalAssert (
input: string,
expectedOutput: any,
env: Script.Env = Script.standardEnv())
{
const output = Script.parseEval(input, env, 0);
return assert.deepEqual(output, expectedOutput);
}
describe('Script', function () {
describe('tokenise', function () {
it('should return empty string', function () {
const input = '';
tokeniseAssert(input, []);
})
});
describe('tokenise', function () {
it('should split string into tokens', function () {
const input = '() (testing x y z)';
const output =
['(', ')', '(', 'testing', 'x', 'y', 'z', ')'];
tokeniseAssert(input, output);
})
});
describe('ast', function () {
it('should return empty AST', function () {
const input = '';
parseAssert(input, []);
})
it('should generate AST from boolean atom', function () {
const input = 'true';
const output = true;
parseAssert(input, output);
})
it('should generate AST from number atom', function () {
const input = '5';
const output = 5;
parseAssert(input, output);
})
it('should generate AST from string atom', function () {
const input = '"emily"';
const output = '"emily"';
parseAssert(input, output);
})
it('should generate AST from an expression', function () {
const input = '(set x (+ x 1))';
const output = ['set', 'x', ['+', 'x', 1]];
parseAssert(input, output);
})
});
describe('evaluate', function () {
describe('empty script', function () {
it('should do nothing', function () {
const input = '';
const env = Script.standardEnv();
Script.parseEval(input, env, 0);
})
});
describe('atoms', function () {
it('should evaluate to true', function () {
const input = 'true';
const output = true;
evalAssert(input, output);
})
it('should evaluate to false', function () {
const input = 'false';
const output = false;
evalAssert(input, output);
})
it('should evaluate to a number', function () {
const input = '5';
const output = 5;
evalAssert(input, output);
})
it('should evaluate to a string', function () {
const input = '"emily"';
const output = 'emily';
evalAssert(input, output);
})
});
describe('operators', function () {
it('add', function () {
const input = '(+ 1 2 3)';
const output = 6;
evalAssert(input, output);
})
it('subtract', function () {
const input = '(- 1 2 3)';
const output = -4;
evalAssert(input, output);
})
it('multiply', function () {
const input = '(* 1 2 3)';
const output = 6;
evalAssert(input, output);
})
it('divide', function () {
const input = '(/ 6 3 2)';
const output = 1;
evalAssert(input, output);
})
it('lessThan', function () {
const falseInput = '(< 1 3 2)';
const trueInput = '(< 1 2 3)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
it('greaterThan', function () {
const falseInput = '(> 1 3 2)';
const trueInput = '(> 3 2 1)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
it('equals', function () {
const falseInput = '(= 1 1 2)';
const trueInput = '(= 1 1 1)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
it('not', function () {
const falseInput = '(not true)';
const trueInput = '(not false)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
it('and', function () {
const falseInput = '(and true true false)';
const trueInput = '(and true true true)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
it('or', function () {
const falseInput = '(or false false false)';
const trueInput = '(or false true false)';
evalAssert(falseInput, false);
evalAssert(trueInput, true);
})
});
describe('define', function () {
it('should define new variable', function () {
const input = '(define x 5)';
const env = Script.standardEnv();
Script.parseEval(input, env, 0);
assert.deepEqual(env.custom['x'], 5);
})
});
describe('set', function () {
it('should set variable value', function () {
const input = '((define x 0)(set x 5))';
const env = Script.standardEnv();
Script.parseEval(input, env, 0);
assert.deepEqual(env.custom['x'], 5);
})
});
describe('read variable', function () {
it('should return the variable value', function () {
const define = '(define x "emily")';
const read = 'x';
const env = Script.standardEnv();
const output = 'emily';
Script.parseEval(define, env, 0);
evalAssert(read, output, env);
})
});
describe('update variable using previous value', function () {
it('should return the variable value', function () {
const input = '((define x 5)(set x (+ x 1)))';
const env = Script.standardEnv();
const read = 'x';
const output = 6;
Script.parseEval(input, env, 0);
evalAssert(read, output, env);
})
});
describe('functions', function () {
it('getGameDay', function () {
const input = '(getGameDay)';
const env = Script.standardEnv();
const timestampMs = Date.now();
env.custom['utcStartDate'] = timestampMs;
const day = Script.parseEval(
input, env, timestampMs);
assert.deepEqual(day, 0);
})
});
});
});