-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCIdentifierCheck.ts
732 lines (632 loc) · 23.3 KB
/
LCIdentifierCheck.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import { Parser } from './LCParser';
export namespace ProcessAST {
// #region constants and types
const processingErrors: processingError[] = [];
export type processingError = unknown;
export interface processedAST {
imports: [string, Parser.statement][]; // hold a reference to the original AST
// e.g. "/main": { main } or "/group1/my_type": { my_type }
letDict: { [path: string]: Parser.letStatement }; // Map<string, Parser.statement>
typeDict: { [path: string]: Parser.typeStatement }; // Map<string, Parser.statement>
namespaceScopes: [string, Parser.statement][];
}
type typeExprProcessingInfo = {
scope: string;
localIdentifiers: string[];
importFilenames: string[];
filename: string;
typeDict: { [path: string]: Parser.typeStatement };
};
// #endregion
// #region processors
function newProcessingError(
processingError: processingError
): processingError & never {
processingErrors.push(processingError);
return processingError as never;
}
// remove all the groupings and save the important statements into the datastructure
function buildHashMap(
ast: Parser.statement[],
currentScope: string
): processedAST {
const processedAST: processedAST = {
imports: [],
letDict: {},
typeDict: {},
namespaceScopes: []
};
// a list of all the namespaces in this scope
// having twice the same namespace name will yield an error msg
for (let statement of ast) {
switch (statement.type) {
case 'comment':
case 'empty':
// skip
break;
case 'import':
processedAST.imports.push([statement.filename.l, statement]);
break;
case 'let':
const letName: string = currentScope + statement.name;
if (
letName in processedAST.letDict ||
processedAST.namespaceScopes.some(
([name, stmt]) => name === letName
)
)
newProcessingError(
`The let-identifier ${letName} is already defined in this scope.`
);
processedAST.letDict[letName] = statement;
break;
case 'type-alias':
case 'complex-type':
const typeName: string = currentScope + statement.name;
if (
typeName in processedAST.typeDict ||
processedAST.namespaceScopes.some(
([name, stmt]) => name === typeName
)
)
newProcessingError(
`The type-identifier ${typeName} is already defined in this scope.`
);
processedAST.typeDict[typeName] = statement;
break;
case 'group':
const groupName: string = currentScope + statement.name;
// group name cannot be the filename or name of imported file,
// this gets handled at a later stage
if (
processedAST.namespaceScopes.some(
([name, stmt]) => name === groupName
) ||
groupName in processedAST.letDict ||
groupName in processedAST.typeDict
)
newProcessingError(
`The namespace ${groupName} already exists in this scope in the form of another group, a let- or a type-statement.`
);
else processedAST.namespaceScopes.push([groupName, statement]);
const data: processedAST = buildHashMap(
statement.body,
groupName + `/`
);
if (data.imports.length !== 0)
throw new Error(
'Internal processing error: imports in namespaces are not allowed and should have been prohibited by the parser already.'
);
processedAST.namespaceScopes.push(...data.namespaceScopes);
for (const [letName, value] of Object.entries(data.letDict))
if (
letName in processedAST.letDict ||
processedAST.namespaceScopes.some(
([name, stmt]) => name === letName
)
)
newProcessingError(
`The let-statement "${letName}" already existed in form of another let- or group-statement.`
);
else processedAST.letDict[letName] = value;
for (const [typeName, value] of Object.entries(data.typeDict))
if (
typeName in processedAST.typeDict ||
processedAST.namespaceScopes.some(
([name, stmt]) => name === typeName
)
)
newProcessingError(
`The type statement ${typeName} already exists in another type- or group-statement`
);
else processedAST.typeDict[typeName] = value;
break;
}
}
return processedAST;
}
// replace all the identifiers by the global path
// or remains unchanged because it is local to generics
function resolveIdentifiers(data: processedAST, filename: string): void {
const importFilenames: string[] = data.imports.map(([name, stmt]) => name);
// if "use std;" then "std.test => /std/test"
// because we assume "test" exists in "std" is there
// while "thisFilename.test => /thisFilename/test" with the test if it actually is there
// resolve global identifier of types
for (const [key, value] of Object.entries(data.typeDict)) {
const infos: typeExprProcessingInfo = {
scope: `/${get_outer_groups(key, 1).join('/')}/`, // scope
// not the own name for this one:
localIdentifiers: value.isGeneric
? value.genericIdentifiers.map((gIdent) => gIdent.argument.l)
: [],
importFilenames,
filename,
typeDict: data.typeDict
};
if (value.type === 'type-alias')
data.typeDict[key].body = processTypeExprIdent(value.body, infos);
else if (value.type === 'complex-type') {
// resolve type annotations correctly aswell
data.typeDict[key].body = value.body.map((e) => {
e.argument.arguments = e.argument.arguments.map((a) => {
a.argument = processTypeExprIdent(a.argument, infos);
return a;
});
return e;
});
}
}
// resolve global identifiers of lets
for (const [key, value] of Object.entries(data.letDict)) {
data.letDict[key] = processStmtIdent(
value,
`/${get_outer_groups(key, 1).join('/')}/`,
importFilenames,
filename,
data.typeDict,
data.letDict
);
}
}
function processStmtIdent(
stmt: Parser.letStatement,
scope: string,
importFilenames: string[],
filename: string,
typeDict: {
[path: string]: Parser.typeStatement;
},
letDict: {
[path: string]: Parser.letStatement;
}
): Parser.letStatement {
if (stmt.hasExplicitType)
stmt.typeExpression = processTypeExprIdent(stmt.typeExpression, {
scope,
localIdentifiers: stmt.isGeneric
? stmt.genericIdentifiers.map((gIdent) => gIdent.argument.l)
: [],
importFilenames,
filename,
typeDict
});
stmt.body = processExprIdent(stmt.body, {
scope,
localTypeIdentifiers: stmt.isGeneric
? stmt.genericIdentifiers.map((gIdent) => gIdent.argument.l)
: [],
importFilenames,
localExprIdentifiers: [],
filename,
typeDict,
letDict
});
return stmt;
}
// resolve identifiers
function processTypeExprIdent(
typeExpr: Parser.typeExpression,
info: typeExprProcessingInfo
): Parser.typeExpression {
switch (typeExpr.type) {
case 'primitive-type':
return typeExpr;
case 'grouping':
typeExpr.body = processTypeExprIdent(typeExpr.body, info);
return typeExpr.body;
case 'func-type':
typeExpr.returnType = processTypeExprIdent(typeExpr.returnType, info);
typeExpr.parameters = typeExpr.parameters.map((param) => {
param.argument = processTypeExprIdent(param.argument, info);
return param;
});
return typeExpr;
case 'genericSubstitution':
typeExpr.expr = processTypeExprIdent(typeExpr.expr, info);
typeExpr.substitutions = typeExpr.substitutions.map((subst) => {
subst.argument = processTypeExprIdent(subst.argument, info);
return subst;
});
return typeExpr;
case 'identifier':
// recursively do the same thing for the substitutions
if (info.localIdentifiers.includes(typeExpr.identifier))
return typeExpr;
// must be of other scope
for (let i = 0; i < get_outer_groups_len(info.scope); ++i) {
// start by not removing the outer groups
const possiblePath: string = `/${get_outer_groups(info.scope, i).join(
'/'
)}/${typeExpr.identifier}`;
if (possiblePath in info.typeDict) {
typeExpr.identifier = possiblePath;
return typeExpr;
}
}
// out of scope or just erroneous
newProcessingError(
`Can not find the identifier "${typeExpr.identifier}" in scope "${info.scope}".`
);
return typeExpr;
case 'propertyAccess':
let propertyAccessPath: string = typeExpr.propertyToken.l;
// get the given propertyAccessPath
let tmp: Parser.typeExpression = typeExpr.source;
while (tmp.type === 'propertyAccess' || tmp.type === 'grouping') {
if (tmp.type === 'grouping') tmp = tmp.body;
else if (tmp.type === 'propertyAccess') {
propertyAccessPath = tmp.propertyToken.l + '/' + propertyAccessPath;
tmp = tmp.source;
}
}
if (tmp.type === 'identifier') {
propertyAccessPath = '/' + tmp.identifier + '/' + propertyAccessPath;
// it could be, that this property access, accesses a different file
// then the deepest identifier must be the other file name, which must be imported at the beginning
if (info.importFilenames.includes(tmp.identifier))
return {
type: 'identifier',
identifier: propertyAccessPath,
identifierToken: typeExpr.propertyToken,
comments: []
};
} else
newProcessingError(
'A property access should only be possible by having the property being a propertyAccess itself or an identifier.'
);
// got now the given path by the user in propertyAccessPath
for (let i = get_outer_groups_len(info.scope); i >= 0; --i) {
// remove the i times the outer scope, to start at the beginning from 0
const possibleOuterScope: string = get_outer_groups(
info.scope,
i
).join('/');
const possiblePath: string =
possibleOuterScope.length === 0
? propertyAccessPath
: `/${possibleOuterScope}${propertyAccessPath}`;
if (possiblePath in info.typeDict) {
// or to `Object.assign(typeExpr, typeExpr.propertyToken)` and change a couple things
return {
type: 'identifier',
identifier: possiblePath,
identifierToken: typeExpr.propertyToken,
comments: []
};
}
}
newProcessingError(
`Could not find the identifier "${propertyAccessPath}" in type propertyAccess.`
);
return typeExpr;
}
}
// #region helper functions
// TODO copy pasted from LCInterpreter
function deepCpy<T>(value: T): T {
if (value === null) return value;
switch (typeof value) {
case 'object':
if (Array.isArray(value)) return value.map((val) => deepCpy(val)) as T;
const newObj: any = {};
for (const [key, val] of Object.entries(value))
newObj[key] = deepCpy(val);
return newObj as T;
case 'undefined':
case 'symbol':
case 'boolean':
case 'number':
case 'bigint':
case 'string':
case 'function':
default:
return value;
}
}
function resolveAliasOfComplexType(
alias: Parser.typeStatement,
dict: {
[path: string]: Parser.typeStatement;
}
): Parser.typeStatement | undefined {
let maxDepth: number = 2000; // fix `type alias = alias2; type alias2 = alias;`
while (true) {
alias = deepCpy(alias);
--maxDepth;
if (maxDepth <= 0 || alias === undefined) break;
if (alias.type === 'complex-type') break;
// propertyAccess and grouping are already resolved
if (alias.body.type === 'identifier') alias = dict[alias.body.identifier];
else if (alias.body.type === 'genericSubstitution')
alias.body = alias.body.expr; // no problem, since working on a deepCpy
}
return alias;
}
// #endregion
// resolve identifiers of expressions
function processExprIdent(
expr: Parser.expression,
info: {
scope: string;
filename: string;
localTypeIdentifiers: string[];
localExprIdentifiers: string[];
importFilenames: string[];
typeDict: {
[path: string]: Parser.typeStatement;
};
letDict: {
[path: string]: Parser.letStatement;
};
}
): Parser.expression {
switch (expr.type) {
case 'literal':
return expr;
case 'grouping':
expr.body = processExprIdent(expr.body, info);
return expr.body; // remove groups for efficiency
case 'unary':
expr.body = processExprIdent(expr.body, info);
return expr;
case 'binary':
expr.leftSide = processExprIdent(expr.leftSide, info);
expr.rightSide = processExprIdent(expr.rightSide, info);
return expr;
case 'call':
expr.function = processExprIdent(expr.function, info);
expr.arguments = expr.arguments.map((arg) => {
arg.argument = processExprIdent(arg.argument, info);
return arg;
});
return expr;
case 'func':
if (expr.hasExplicitType)
expr.typeExpression = processTypeExprIdent(expr.typeExpression, {
scope: info.scope,
localIdentifiers: info.localTypeIdentifiers,
importFilenames: info.importFilenames,
filename: info.filename,
typeDict: info.typeDict
});
expr.parameters = expr.parameters.map((param) => {
if (param.argument.hasDefaultValue)
param.argument.defaultValue = processExprIdent(
param.argument.defaultValue,
info
);
if (param.argument.hasExplicitType)
param.argument.typeExpression = processTypeExprIdent(
param.argument.typeExpression,
{
scope: info.scope,
localIdentifiers: info.localTypeIdentifiers,
importFilenames: info.importFilenames,
filename: info.filename,
typeDict: info.typeDict
}
);
return param;
});
const newLocalIdentifiers: string[] = expr.parameters.map(
(param) => param.argument.identifierToken.l
);
if (
newLocalIdentifiers.some(
(ident, i) => newLocalIdentifiers.indexOf(ident) !== i
)
)
newProcessingError(
'Cannot have a function with multiple parameters with the same name.'
);
// merge the local identifiers for this case
if (newLocalIdentifiers.length !== 0)
info = {
...info,
localExprIdentifiers: [
...newLocalIdentifiers,
...info.localExprIdentifiers
]
};
expr.body = processExprIdent(expr.body, info);
return expr;
case 'match':
expr.scrutinee = processExprIdent(expr.scrutinee, info);
if (expr.hasExplicitType)
expr.typeExpression = processTypeExprIdent(expr.typeExpression, {
scope: info.scope,
localIdentifiers: info.localTypeIdentifiers,
importFilenames: info.importFilenames,
filename: info.filename,
typeDict: info.typeDict
});
expr.body = expr.body.map((matchLine) => {
const newLocalIdentifiers: string[] =
matchLine.argument.isDefaultVal === false
? matchLine.argument.parameters.map((param) => param.argument.l)
: [];
if (
newLocalIdentifiers.some(
(ident, i) => newLocalIdentifiers.indexOf(ident) !== i
)
)
newProcessingError(
'Cannot have a match body line with two identifiers in the same line.'
);
// merge local identifiers
if (newLocalIdentifiers.length !== 0)
info = {
...info,
localExprIdentifiers: [
...newLocalIdentifiers,
...info.localExprIdentifiers
]
};
matchLine.argument.body = processExprIdent(
matchLine.argument.body,
info
);
return matchLine;
});
return expr;
case 'typeInstantiation':
expr.source = processExprIdent(expr.source, info);
if (
expr.source.type !== 'identifier' ||
!(expr.source.identifier in info.typeDict)
)
newProcessingError(
'Type instantiation must be done with a property of type complex-type.'
);
else if (
info.typeDict[expr.source.identifier].type !== 'complex-type'
) {
// resolve problem:
// type alias = alias2;
// type alias2 = complexType;
// alias->PropertyOfComplexType;
let alias = resolveAliasOfComplexType(
info.typeDict[expr.source.identifier],
info.typeDict
);
if (alias?.type !== 'complex-type')
newProcessingError(
'Type instantiation must be done with a property of type complex-type.'
);
}
return expr;
case 'identifier':
if (info.localExprIdentifiers.includes(expr.identifier)) return expr;
// not a local identifier
for (let i = 0; i < get_outer_groups_len(info.scope); ++i) {
const possiblePath: string = `/${get_outer_groups(info.scope, i).join(
'/'
)}/${expr.identifier}`;
// could be for a type instatiation, and thus be typeDict
if (possiblePath in info.letDict || possiblePath in info.typeDict) {
expr.identifier = possiblePath;
return expr;
}
}
newProcessingError(
`Could not find the identifier "${expr.identifier}" in the scope of an expression.`
);
return expr;
case 'propertyAccess':
let propertyAccessPath: string = expr.propertyToken.l;
// get the given propertyAccessPath
let tmp: Parser.expression = expr.source;
while (tmp.type === 'propertyAccess' || tmp.type === 'grouping') {
if (tmp.type === 'grouping') tmp = tmp.body;
else if (tmp.type === 'propertyAccess') {
propertyAccessPath = tmp.propertyToken.l + '/' + propertyAccessPath;
tmp = tmp.source;
}
}
if (tmp.type === 'identifier') {
propertyAccessPath = '/' + tmp.identifier + '/' + propertyAccessPath;
// it could be, that this property access, accesses a different file
// then the deepest identifier must be the other file name, which must be imported at the beginning
if (info.importFilenames.includes(tmp.identifier))
return {
type: 'identifier',
identifier: propertyAccessPath,
identifierToken: expr.propertyToken,
comments: []
};
} else
newProcessingError(
'An property access should only be possible by having the property being a propertyAccess itself or an identifier.'
);
// got now the given path by the user in propertyAccessPath
for (let i = get_outer_groups_len(info.scope); i >= 0; --i) {
// remove the i times the outer scope, to start at the beginning from 0
const possibleOuterScope: string = get_outer_groups(
info.scope,
i
).join('/');
const possiblePath: string =
possibleOuterScope.length === 0
? propertyAccessPath
: `/${possibleOuterScope}${propertyAccessPath}`;
// could be for a type instatiation, and thus be typeDict
if (possiblePath in info.letDict || possiblePath in info.typeDict)
return {
type: 'identifier',
identifier: possiblePath,
identifierToken: expr.propertyToken,
comments: []
};
}
newProcessingError(
`Could not find the identifier "${propertyAccessPath}" in propertyAccess expression.`
);
return expr;
}
}
// #region helper funcs
function get_outer_groups_len(dict_key: string): number {
return dict_key.split('/').filter((e) => e !== '').length;
}
function get_outer_groups(
dict_key: string,
removeLastNElements: number = 0
): string[] {
const ans: string[] = dict_key.split('/').filter((e) => e !== '');
ans.splice(ans.length - removeLastNElements, ans.length);
return ans;
}
// #endregion
// #endregion
export function processCode(
code: string,
filename: string /*should include the filename at the beginning!*/
):
| { valid: true; value: processedAST }
| {
valid: false;
processingErrors: processingError[];
value?: processedAST;
} {
if (!filename.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/g))
return {
valid: false,
processingErrors: ['tried to process the code with an invalid filename']
};
processingErrors.splice(0, processingErrors.length); // remove all the old errors
const path: string = `/${filename}/`; // a path needs "/"
const parsed = Parser.parse(code, {
ignoreComments: true /*not needed for execution*/
});
if (parsed.valid === false)
return {
valid: false,
processingErrors: [
{
type: 'Was not able to parse the code.',
parserErrors: parsed.parseErrors
}
]
};
const ast: Parser.statement[] = parsed.statements;
// step 1, build the hashmap of important statements
const value: processedAST = buildHashMap(ast, path);
// check if some namespace has the same name as the filename or the imports
for (const [name, stmt] of value.namespaceScopes)
if (name === filename || value.imports.some(([n, s]) => n === name))
newProcessingError(
`The name of a group cannot be the same as the filename "${name}" or the name of one of the imports '[${value.imports
.map(([n, s]) => n)
.join(', ')}]'.`
);
// Can't proceed to step 2 if already had errors
if (processingErrors.length !== 0)
return { valid: false, processingErrors };
// step 2, resolve all outer scope identifiers to their respective outer scope
resolveIdentifiers(value, filename);
if (processingErrors.length !== 0)
return { valid: false, processingErrors, value };
return { valid: true, value };
}
}