-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtypes.ts
200 lines (179 loc) · 3.78 KB
/
types.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
import * as assert from 'assert';
import { describe, test } from 'vitest';
import { utils } from '../src';
import { NUM, STR, NULL, ARR, OBJ, BOOL, TRUE, FALSE, ERROR ,FN_NATIVE } from '../src/interpreter/value';
import { AiScriptRuntimeError } from '../src/error';
import { exe, getMeta, eq } from './testutils';
describe('function types', () => {
test.concurrent('multiple params', async () => {
const res = await exe(`
let f: @(str, num) => bool = @() { true }
<: f('abc', 123)
`);
eq(res, TRUE);
});
});
describe('generics', () => {
describe('function', () => {
test.concurrent('expr', async () => {
const res = await exe(`
let f = @<T>(v: T): void {}
<: f("a")
`);
eq(res, NULL);
});
test.concurrent('consumer', async () => {
const res = await exe(`
@f<T>(v: T): void {}
<: f("a")
`);
eq(res, NULL);
});
test.concurrent('identity function', async () => {
const res = await exe(`
@f<T>(v: T): T { v }
<: f(1)
`);
eq(res, NUM(1));
});
test.concurrent('use as inner type', async () => {
const res = await exe(`
@vals<T>(v: obj<T>): arr<T> {
Obj:vals(v)
}
<: vals({ a: 1, b: 2, c: 3 })
`);
eq(res, ARR([NUM(1), NUM(2), NUM(3)]));
});
test.concurrent('use as variable type', async () => {
const res = await exe(`
@f<T>(v: T): void {
let v2: T = v
}
<: f(1)
`);
eq(res, NULL);
});
test.concurrent('use as function type', async () => {
const res = await exe(`
@f<T>(v: T): @() => T {
let g: @() => T = @() { v }
g
}
<: f(1)()
`);
eq(res, NUM(1))
});
test.concurrent('curried', async () => {
const res = await exe(`
@concat<A>(a: A): @<B>(B) => str {
@<B>(b: B) {
\`{a}{b}\`
}
}
<: concat("abc")(123)
`);
eq(res, STR('abc123'));
});
test.concurrent('new lines', async () => {
const res = await exe(`
@f<
T
U
>(x: T, y: U): arr<T | U> {
[x, y]
}
<: f("abc", 123)
`);
eq(res, ARR([STR('abc'), NUM(123)]));
});
test.concurrent('duplicate', async () => {
await assert.rejects(() => exe(`
@f<T, T>(v: T) {}
`));
});
test.concurrent('empty', async () => {
await assert.rejects(() => exe(`
@f<>() {}
`));
});
});
});
describe('union', () => {
test.concurrent('variable type', async () => {
const res = await exe(`
let a: num | null = null
<: a
`);
eq(res, NULL);
});
test.concurrent('more inners', async () => {
const res = await exe(`
let a: str | num | null = null
<: a
`);
eq(res, NULL);
});
test.concurrent('inner type', async () => {
const res = await exe(`
let a: arr<num | str> = ["abc", 123]
<: a
`);
eq(res, ARR([STR('abc'), NUM(123)]));
});
test.concurrent('param type', async () => {
const res = await exe(`
@f(x: num | str): str {
\`{x}\`
}
<: f(1)
`);
eq(res, STR('1'));
});
test.concurrent('return type', async () => {
const res = await exe(`
@f(): num | str { 1 }
<: f()
`);
eq(res, NUM(1));
});
test.concurrent('type parameter', async () => {
const res = await exe(`
@f<T>(v: T): T | null { null }
<: f(1)
`);
eq(res, NULL);
});
test.concurrent('function type', async () => {
const res = await exe(`
let f: @(num | str) => str = @(x) { \`{x}\` }
<: f(1)
`);
eq(res, STR('1'));
});
test.concurrent('invalid inner', async () => {
await assert.rejects(() => exe(`
let a: ThisIsAnInvalidTypeName | null = null
`));
});
});
describe('simple', () => {
test.concurrent('error', async () => {
const res = await exe(`
let a: error = Error:create("Ai")
<: a
`);
eq(res, ERROR('Ai'));
});
test.concurrent('never', async () => {
const res = await exe(`
@f() {
let a: never = eval {
return 1
}
}
<: f()
`);
eq(res, NUM(1));
});
});