| 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250 |
22x
22x
22x
22x
22x
22x
22x
88x
88x
1x
87x
87x
87x
26x
1x
1x
5x
5x
36x
36x
1x
1x
28x
28x
1x
167x
167x
113x
113x
2x
76x
76x
76x
76x
2x
1x
1x
1x
114x
114x
114x
114x
110x
4x
167x
167x
2x
165x
165x
165x
165x
165x
3x
106x
1x
1x
3x
3x
3x
1x
1x
87x
87x
10x
22x
10x
| // @flow
// AST Validator
import Syntax from 'walt-syntax';
import walkNode from 'walt-parser-tools/walk-node';
import error from '../utils/generate-error';
import { isBuiltinType } from '../generator/utils';
import { TYPE_CONST, AST_METADATA } from '../semantics/metadata';
import { typeWeight } from '../types';
import type { NodeType } from '../flow/types';
const GLOBAL_LABEL = 'global';
// We walk the the entire tree and perform syntax validation before we continue
// onto the generator. This may throw sometimes
export default function validate(
ast: NodeType,
{
filename,
}: {
filename: string,
}
) {
const metadata = ast.meta[AST_METADATA];
if (metadata == null) {
throw new Error('Missing AST metadata!');
}
const { types, functions, userTypes } = metadata;
const problems = [];
walkNode({
[Syntax.Import]: (importNode, _) => {
walkNode({
[Syntax.BinaryExpression]: (binary, __) => {
const [start, end] = binary.range;
problems.push(
error(
"Using an 'as' import without a type.",
'A type for original import ' +
binary.params[0].value +
' is not defined nor could it be inferred.',
{ start, end },
filename,
GLOBAL_LABEL
)
);
},
[Syntax.Identifier]: (identifier, __) => {
const [start, end] = identifier.range;
problems.push(
error(
'Infered type not supplied.',
"Looks like you'd like to infer a type, but it was never provided by a linker. Non-concrete types cannot be compiled.",
{ start, end },
filename,
GLOBAL_LABEL
)
);
},
[Syntax.Pair]: (pair, __) => {
const type = pair.params[1];
if (!isBuiltinType(type.value) && types[type.value] == null) {
const [start, end] = type.range;
problems.push(
error(
`Undefined Type ${type.value}`,
`Invalid Import. ${type.value} type does not exist`,
{ start, end },
filename,
GLOBAL_LABEL
)
);
}
},
})(importNode);
},
// All of the validators below need to be implemented
[Syntax.Struct]: (_, __) => {},
[Syntax.ImmutableDeclaration]: (_, __) => {},
[Syntax.Declaration]: (decl, _validator) => {
const [start, end] = decl.range;
if (
!isBuiltinType(decl.type) &&
!types[decl.type] &&
!userTypes[decl.type]
) {
problems.push(
error(
'Unknown type used in a declaration, ' + `"${String(decl.type)}"`,
'Variables must be assigned with a known type.',
{ start, end },
filename,
GLOBAL_LABEL
)
);
}
},
[Syntax.FunctionDeclaration]: (func, __) => {
const functionName = `${func.value}()`;
walkNode({
[Syntax.Declaration]: (node, _validator) => {
const [start, end] = node.range;
if (
!isBuiltinType(node.type) &&
!types[node.type] &&
!userTypes[node.type]
) {
problems.push(
error(
'Unknown type used in a declartion, ' +
`"${String(node.type)}"`,
'Variables must be assigned with a known type.',
{ start, end },
filename,
functionName
)
);
}
},
[Syntax.Assignment]: node => {
const [identifier] = node.params;
const [start, end] = node.range;
const isConst = identifier.meta[TYPE_CONST];
if (isConst) {
problems.push(
error(
`Cannot reassign a const variable ${identifier.value}`,
'const variables cannot be reassigned, use let instead.',
{ start, end },
filename,
functionName
)
);
}
},
[Syntax.ArraySubscript]: node => {
const [target] = node.params;
const [start, end] = node.range;
problems.push(
error(
'Invalid subscript target',
`Expected array type for ${target.value}, received ${
target.type
}`,
{ start, end },
filename,
functionName
)
);
},
[Syntax.NativeMethod]: (node, _validator) => {
const { value } = node;
const [offset = {}, rhs] = node.params;
const [start, end] = node.range;
if (
!(
offset.value === 'unreachable' &&
(value.includes('store') || value.includes('load'))
)
) {
return;
}
problems.push(
error(
'Cannot generate property access',
`Cannot assign "${rhs.value}". Key is "${offset.value}"`,
{ start, end },
filename,
functionName
)
);
},
[Syntax.ReturnStatement]: (node, validator) => {
node.params.map(validator);
if (func.type == null) {
return;
}
const [expression] = node.params;
const [start] = node.range;
const end = expression != null ? expression.range[1] : node.range[1];
const type = node.type;
if (typeWeight(type) !== typeWeight(func.type)) {
problems.push(
error(
'Missing return value',
'Inconsistent return value. Expected ' +
func.type +
' received ' +
String(type),
{ start, end },
filename,
functionName
)
);
}
},
[Syntax.FunctionCall]: (node, _validator) => {
if (functions[node.value] == null) {
const [start, end] = node.range;
problems.push(
error(
'Undefined function reference',
`${node.value} is not defined.`,
{ start, end },
filename,
functionName
)
);
}
},
[Syntax.IndirectFunctionCall]: (node, _validator) => {
const identifier = node.params[node.params.length - 1];
const type = types[identifier.type];
if (!isBuiltinType(identifier.type) && type == null) {
const [start, end] = node.range;
problems.push(
error(
'Cannot make an indirect call without a valid function type',
`${identifier.value} has type ${String(
identifier.type
)} which is not defined. Indirect calls must have pre-defined types.`,
{ start, end },
filename,
functionName
)
);
}
},
})(func);
},
})(ast);
const problemCount = problems.length;
if (problemCount > 0) {
const errorString = problems.reduce((acc, value) => {
return acc + '\n' + `${value}\n`;
}, `Cannot generate WebAssembly for ${filename}. ${problemCount} problems.\n`);
throw new Error(errorString);
}
}
|