-
Notifications
You must be signed in to change notification settings - Fork 0
/
lomp.test.ts
110 lines (97 loc) · 2.65 KB
/
lomp.test.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
import * as lomp from './lomp';
describe('parser', () => {
const testProg = `
($log (+ 2 3))
`;
function toks() {
return testProg.tokenize();
}
test('basic lex', () => {
let exp = ['(', lomp.OpSymbol.from_string('$log'), '(', lomp.OpSymbol.from_string('+'), 2, 3, ')', ')'];
// console.log(toks)
// console.log(exp)
expect(testProg.tokenize()).toEqual(exp);
});
test('basic parse', () => {
const parser = new lomp.Parser({ toks: toks() });
const s = parser.next_form();
let exp = new lomp.SExp([lomp.OpSymbol.vau('log'), new lomp.SExp([lomp.OpSymbol.standard('+'), 2, 3])]);
// console.log(s, exp, equals(exp, s));
expect(s).toEqual(exp);
});
test('broken parse', () => {
const parser = new lomp.Parser({ toks: lomp.Tokenizer.tokenize('($log (+ 1 2)') });
expect(() => {
const s = parser.next_form();
console.log(s);
}).toThrow();
});
test('basic program', () => {
const prog = `
($log (+ 2 3))
($log (* 4 (+ 7 8)))
`;
expect(prog.parse()).toEqual(
new lomp.SExp([
lomp.OpSymbol.vau('progn'),
new lomp.SExp([lomp.OpSymbol.vau('log'), new lomp.SExp([lomp.OpSymbol.standard('+'), 2, 3])]),
new lomp.SExp([lomp.OpSymbol.vau('log'), new lomp.SExp([lomp.OpSymbol.standard('*'), 4, new lomp.SExp([lomp.OpSymbol.standard('+'), 7, 8])])]),
]),
);
});
test('basic map', () => {
const parser = lomp.Parser._from_program(`
($log { x: 5 y: (+ 3 4) })
`);
expect(parser.next_form()).toEqual(
new lomp.SExp([
lomp.OpSymbol.vau('log'),
new lomp.SMap({
x: 5,
y: new lomp.SExp([lomp.OpSymbol.standard('+'), 3, 4])
})
]),
);
});
test('basic string', () => {
expect('($log "test string")'.parse()).toEqual(
new lomp.SExp([
lomp.OpSymbol.vau('log'),
"test string",
])
)
});
});
describe('expressions', () => {
test('basic eval', () => {
expect('(+ 2 3)'.parse().evl(lomp.Env.base_env())).toBe(5);
});
test('deeper math', () => {
expect('(* 7 (+ (/ 12 4) 3))'.parse().evl(lomp.Env.base_env())).toBe(42);
});
test('basic conditional', () => {
expect('($if (= (* 3 4) 12) 42 -1)'.parse().evl(lomp.Env.base_env())).toBe(42);
});
test('basic conditional false', () => {
expect('($if (> (* 9 9) 99) 1 2)'.parse().evl(lomp.Env.base_env())).toBe(2);
})
})
const SC = `
(new ~class {
name: delay-queue
inherits: ~queue
vars: {
delay: (new ~slot-def {
type: !number
default: 1000
doc: "ms amount to wait before pull return"
})
}
methods: {
pull: ($method (
(wait .delay)
(pull @super)
))
}
})
`