-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantics.js
More file actions
820 lines (698 loc) · 18.7 KB
/
Copy pathsemantics.js
File metadata and controls
820 lines (698 loc) · 18.7 KB
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
'use strict';
var ICode = require('./icode.js'),
Stack = require('./semantic-stack.js'),
SymbolTable = require('./symbol-table.js'),
SymbolTypes = SymbolTable.SymbolTypes,
SAR = require('./sars/index.js'),
TypeInference = require('./type-inference.js'),
Semantics;
function throwSemanticError(message) {
if (TypeInference.enabled) {
return;
}
throw new Error(message);
}
Semantics = {
enabled: false,
/****************
* SAR PUSHES *
****************/
/**
* Pushes an identifier on to the action stack
* @param {string} identifier
*/
iPush: function(identifier) {
if (!this.enabled) {
return;
}
var symbol = SymbolTable().findSymbol(identifier);
if (symbol) {
if (symbol.data.type === null) {
throwSemanticError('Cannot determine type for ' + symbol.value);
} else if (symbol.data.isFunction && symbol.data.returnType === null) {
throwSemanticError('Cannot determine return type for ' + symbol.value);
}
Stack.action.push(new SAR.Identifier(symbol));
} else {
Stack.action.push(new SAR.Identifier(identifier));
}
},
/**
* Determine if the identifier exists in the current scope
*/
iExist: function() {
if (!this.enabled) {
return;
}
var identifier = Stack.action.top.identifier,
symbol = SymbolTable().findSymbol(identifier);
if (!symbol) {
throwSemanticError(identifier + ' does not exist in current scope');
}
},
/**
* Pushes a literal value on to the action stack
* @param {string} type Type of value
* @param {string} value Value of literal
*/
lPush: function(type, value) {
if (!this.enabled) {
return;
}
var symbol = SymbolTable().findLiteral(value);
if (symbol === null) {
throwSemanticError('Unknown literal value: ' + type + ' ' + value);
}
TypeInference.addKnownType(symbol.ID, type);
Stack.action.push(new SAR.Literal(type, value, symbol.ID));
},
/**
* Pushes an operator on to the stack
* @param {string} operator
*/
oPush: function(operator) {
if (!this.enabled) {
return;
}
var precedenceSet = [
['*', '/' ],
['+', '-' ],
['<', '>', '<=', '>='],
['==', '!=' ],
['&&' ],
['||' ],
['=' ]
];
function findPrecedence(op) {
var len = precedenceSet.length;
for (var i = 0; i < len; i++) {
if (precedenceSet[i].indexOf(op) > -1) {
return i;
}
}
return len + 1;
}
if (operator === ')') {
// Evaluate current expression
while (Stack.operator.top !== '(') {
var op = Stack.operator.pop();
if (!op) {
throw new Error('Failed to find opening parenthesis');
}
Semantics[op]();
}
// Pop the matching '('
Stack.operator.pop();
return;
}
if (['(', '['].indexOf(operator) > -1) {
Stack.operator.push(operator);
} else {
var precedence = findPrecedence(operator);
while (Stack.operator.top && findPrecedence(Stack.operator.top) <= precedence) {
var op = Stack.operator.pop();
try {
Semantics[op]();
} catch(e) {
if (e.message === 'undefined is not a function') {
e.message = op + ' is not yet implemented';
}
throw e;
}
}
Stack.operator.push(operator);
}
},
/**
* Pushes a scope on to the scope stack
* @param {Symbol} symbol
*/
sPush: function(symbol) {
if (!this.enabled) {
return;
}
Stack.scope.push(new SAR.Scope(symbol));
},
/**
* Pops off the most recent scope
*/
sPop: function() {
if (!this.enabled) {
return;
}
Stack.action.push(Stack.scope.pop());
},
/**
* Pushes a type on to the action stack
* @param {string} type
*/
tPush: function(type, returnType) {
if (!this.enabled) {
return;
}
var idSar = Stack.action.top;
TypeInference.addKnownType(idSar.ID, type);
if (returnType) {
TypeInference.addKnownReturnType(idSar.ID, returnType);
}
Stack.action.push(new SAR.Type(type));
},
/***************
* FUNCTIONS *
***************/
/**
* Add a parameter to the scope
*/
param: function() {
if (!this.enabled) {
return;
}
var parameter;
if (Stack.action.top instanceof SAR.Type) {
// Type declaration, assign it to the variable
var typeSar = Stack.action.pop();
parameter = Stack.action.pop();
TypeInference.addKnownType(parameter.ID, typeSar.type);
} else {
parameter = Stack.action.pop();
}
// Only add parameters on initial type pass
if (TypeInference.enabled) {
var fnSymbol = SymbolTable().symbol,
parameterSymbol = SymbolTable.getSymbol(parameter.ID);
fnSymbol.data.params = fnSymbol.data.params || [];
fnSymbol.data.params.push(parameterSymbol);
TypeInference.addExistingParameter(fnSymbol.ID, parameter.ID);
}
},
/**
* Marks the end of a parameters list
* @param {string} funcID
*/
endParameters: function(funcID) {
if (!this.enabled) {
return;
}
TypeInference.lockParameters(funcID);
},
/**
* Mark the beginning of an argument list
*/
BAL: function() {
if (!this.enabled) {
return;
}
Stack.action.push(new SAR.BAL());
},
/**
* Add an argument to the current argument list
*/
',': function() {
if (!this.enabled) {
return;
}
this.EOE(false);
},
/**
* Mark the end of an argument list
*/
EAL: function() {
if (!this.enabled) {
return;
}
var argList = new SAR.ArgList();
while (!(Stack.action.top instanceof SAR.BAL)) {
argList.args.push(Stack.action.pop());
}
// Pop off the BAL
Stack.action.pop();
// Infer types for the parameters
var funcSar = Stack.action.top,
args = argList.args,
numOfArgs = args.length,
existingParams = SymbolTable.getSymbol(funcSar.ID).data.params || [],
params = existingParams.slice().reverse();
if (numOfArgs !== params.length) {
throwSemanticError('Expected ' + params.length + ' arguments, got ' + numOfArgs);
} else if (TypeInference.enabled) {
for (var i = 0; i < numOfArgs; i++) {
var arg = args[i];
TypeInference.addParameterDependency(funcSar.ID, i, arg.ID);
}
} else {
for (var i = 0; i < numOfArgs; i++) {
var arg = args[i],
param = params[i];
if (param.data.type !== arg.type) {
throwSemanticError(arg.value + ' must be of type ' + param.data.type);
}
}
}
Stack.action.push(argList);
},
/**
* Finish a function call
*/
func: function() {
if (!this.enabled) {
return;
}
var argList = Stack.action.pop(),
identifier = Stack.action.pop(),
fnSymbol = (identifier.ID) ? SymbolTable.getSymbol(identifier.ID) : null,
func = (fnSymbol) ? new SAR.Func(argList.args, fnSymbol) : null;
// Determine if this is a function
if (func) {
TypeInference.addReturnTypeDependency(identifier.ID, func.ID);
if (TypeInference.enabled) {
for (var i = 0, len = argList.args.length; i < len; i++) {
TypeInference.addParameterDependency(identifier.ID, i, argList.args[i].ID);
}
}
}
if (!fnSymbol) {
throwSemanticError(identifier.identifier + ' does not exist');
} else if (!fnSymbol.data.returnType) {
throwSemanticError('Cannot use ' + identifier.identifier + ' as a function');
}
Stack.action.push(func);
ICode.FunctionCall(fnSymbol, argList.args.slice().reverse(), func);
},
/******************
* FLOW CONTROL *
******************/
/**
* Verify that an expression is a bool
*/
if: function() {
if (!this.enabled) {
return;
}
this.EOE(false);
var expression = Stack.action.pop();
TypeInference.addKnownType(expression.ID, 'bool');
if (expression.type !== 'bool') {
throwSemanticError('Expression must be of type bool');
}
ICode.If(expression);
},
/**
* Verify that an expression is a bool
*/
while: function() {
if (!this.enabled) {
return;
}
this.EOE(false);
var expression = Stack.action.pop();
TypeInference.addKnownType(expression.ID, 'bool');
if (expression.type !== 'bool') {
throwSemanticError('Expression must be of type bool');
}
ICode.While(expression);
},
/**
* Evaluate the expression and
* make sure it matches the return type of the function
*/
rtn: function() {
if (!this.enabled) {
return;
}
// Evaluate the current expression
this.EOE(false);
var result = Stack.action.pop(),
scope = Stack.scope.top;
if (!result) {
ICode.Rtn();
result = { type: 'void' };
} else {
ICode.Return(result);
}
if (result.type) {
if (!scope.returnType) {
scope.returnType = result.type;
TypeInference.addKnownReturnType(scope.ID, result.type);
} else if (result.type !== scope.returnType) {
throwSemanticError('Expected value of type ' + scope.returnType + ', found type ' + result.type);
}
} else {
TypeInference.addReturnTypeDependency(scope.ID, result.ID);
}
},
/********
* IO *
********/
/**
* Write a value to standard out
*/
write: function() {
if (!this.enabled) {
return;
}
this.EOE(false);
var expression = Stack.action.pop();
if (expression.type !== 'char' && expression.type !== 'int') {
throwSemanticError('Cannot write out type ' + expression.type + '. Must be a char or an int');
}
ICode.Write(expression);
},
/**
* Reads in a value from standard in
*/
read: function() {
if (!this.enabled) {
return;
}
var expression = Stack.action.pop();
if (expression.type !== 'char' && expression.type !== 'int') {
throwSemanticError('Cannot read to type ' + expression.type + '. Must be a char or an int');
}
ICode.Read(expression);
},
/*****************
* CONVERSIONS *
*****************/
/**
* Verify an expression is a char
*/
atoi: function() {
if (!this.enabled) {
return;
}
var expression = Stack.action.pop(),
temp = new SAR.Temp('int');
TypeInference.addKnownType(expression.ID, 'char');
TypeInference.addKnownType(temp.ID, 'int');
if (expression.type !== 'char') {
throwSemanticError('Cannot convert ' + expression.type + ' to an int, must be a char');
}
Stack.action.push(temp);
ICode.atoi(expression, temp);
},
/**
* Verify an expression is an integer
*/
itoa: function() {
if (!this.enabled) {
return;
}
var expression = Stack.action.pop(),
temp = new SAR.Temp('char');
TypeInference.addKnownType(expression.ID, 'int');
TypeInference.addKnownType(temp.ID, 'char');
if (expression.type !== 'int') {
throwSemanticError('Cannot convert ' + expression.type + ' to a char, must be a int');
}
Stack.action.push(temp);
ICode.itoa(expression, temp);
},
/***************
* OPERATORS *
***************/
/**
* End of expression
* @param {bool} [force=true] If true, clear the action stack
*/
EOE: function(force) {
if (!this.enabled) {
return;
}
if (force === undefined) {
force = true;
}
while (Stack.operator.length && Stack.operator.top !== '(') {
var op = Stack.operator.pop();
Semantics[op]();
}
if (force) {
while (Stack.action.length) {
Stack.action.pop();
}
}
},
/**
* Helper for performing what every
* mathematical semantic action does
*/
_mathOperation: function() {
var a = Stack.action.pop(),
b = Stack.action.pop(),
temp = new SAR.Temp('int');
TypeInference.addKnownType(a.ID, 'int');
TypeInference.addKnownType(b.ID, 'int');
TypeInference.addKnownType(temp.ID, 'int');
if (a.type !== 'int' || b.type !== 'int') {
var aSymbol = SymbolTable.getSymbol(a.ID),
bSymbol = SymbolTable.getSymbol(b.ID),
identifier,
wrongType;
if (a.type !== 'int' && aSymbol.type !== SymbolTypes.Temp) {
identifier = aSymbol.value;
wrongType = a.type;
} else if (b.type !== 'int' && bSymbol.type !== SymbolTypes.Temp) {
identifier = bSymbol.value;
wrongType = b.type;
} else {
identifier = 'an expression';
}
throwSemanticError('Attempted to use ' + identifier + ' of type ' + wrongType + ' as an int');
}
Stack.action.push(temp);
return {
a: a,
b: b,
temp: temp
};
},
/**
* Addition
*/
'+': function() {
if (!this.enabled) {
return;
}
var result = this._mathOperation();
ICode.Add(result.b, result.a, result.temp);
},
/**
* Subtraction
*/
'-': function() {
if (!this.enabled) {
return;
}
var result = this._mathOperation();
ICode.Subtract(result.b, result.a, result.temp);
},
/**
* Multiplication
*/
'*': function() {
if (!this.enabled) {
return;
}
var result = this._mathOperation();
ICode.Multiply(result.b, result.a, result.temp);
},
/**
* Division
*/
'/': function() {
if (!this.enabled) {
return;
}
var result = this._mathOperation();
ICode.Divide(result.b, result.a, result.temp);
},
/**
* Performs an assignment
*/
'=': function() {
if (!this.enabled) {
return;
}
var rhs = Stack.action.pop(),
lhs = Stack.action.pop();
TypeInference.addTypeDependency(lhs.ID, rhs.ID);
if (lhs.type !== rhs.type) {
throwSemanticError(lhs.identifier + ' is of type ' + lhs.type + ', cannot assign a ' + rhs.type);
}
ICode.Assignment(lhs, rhs);
},
/*************************
* RELATIONS OPERATORS *
*************************/
_relationalOperation: function() {
if (!this.enabled) {
return;
}
var expressionA = Stack.action.pop(),
expressionB = Stack.action.pop(),
temp = new SAR.Temp('bool');
TypeInference.addKnownType(expressionA.ID, 'int');
TypeInference.addKnownType(expressionB.ID, 'int');
TypeInference.addKnownType(temp.ID, 'bool');
if (expressionA.type !== 'int' || expressionB.type !== 'int') {
var aSymbol = SymbolTable.getSymbol(expressionA.ID),
bSymbol = SymbolTable.getSymbol(expressionB.ID),
identifier;
if (expressionA.type !== 'int' && aSymbol.type !== SymbolTypes.Temp) {
identifier = aSymbol.value;
} else if (expressionB.type !== 'int' && bSymbol.type !== SymbolTypes.Temp) {
identifier = bSymbol.value;
} else {
identifier = 'Expression';
}
throwSemanticError(identifier + ' must be an integer for comparisons');
}
Stack.action.push(temp);
return {
a: expressionA,
b: expressionB,
temp: temp
};
},
/**
* Verify both expressions are numbers
*/
'<': function() {
if (!this.enabled) {
return;
}
var result = this._relationalOperation();
ICode.LessThan(result.b, result.a, result.temp);
},
/**
* Verify both expressions are numbers
*/
'>': function() {
if (!this.enabled) {
return;
}
var result = this._relationalOperation();
ICode.GreaterThan(result.b, result.a, result.temp);
},
/**
* Verify both expressions are numbers
*/
'<=': function() {
if (!this.enabled) {
return;
}
var result = this._relationalOperation();
ICode.LessThanEqual(result.b, result.a, result.temp);
},
/**
* Verify both expressions are numbers
*/
'>=': function() {
if (!this.enabled) {
return;
}
var result = this._relationalOperation();
ICode.GreaterThanEqual(result.b, result.a, result.temp);
},
/************************
* BOOLEAN OPERATIONS *
************************/
/**
* Verify both expressions are booleans
*/
_booleanOperation: function() {
if (!this.enabled) {
return;
}
var expressionA = Stack.action.pop(),
expressionB = Stack.action.pop(),
temp = new SAR.Temp('bool');
TypeInference.addKnownType(expressionA.ID, 'bool');
TypeInference.addKnownType(expressionB.ID, 'bool');
TypeInference.addKnownType(temp.ID, 'bool');
if (expressionA.type !== 'bool' || expressionB.type !== 'bool') {
var aSymbol = SymbolTable.getSymbol(expressionA.ID),
bSymbol = SymbolTable.getSymbol(expressionB.ID),
identifier;
if (expressionA.type !== 'bool' && aSymbol.type !== SymbolTypes.Temp) {
identifier = aSymbol.value;
} else if (expressionB.type !== 'bool' && bSymbol.type !== SymbolTypes.Temp) {
identifier = bSymbol.value;
} else {
identifier = 'Expression';
}
throwSemanticError(identifier + ' must be a bool.');
}
Stack.action.push(temp);
return {
a: expressionA,
b: expressionB,
temp: temp
};
},
/**
* Verify both expressions are booleans
*/
'&&': function() {
if (!this.enabled) {
return;
}
var result = this._booleanOperation();
ICode.And(result.b, result.a, result.temp);
},
/**
* Verify both expressions are booleans
*/
'||': function() {
if (!this.enabled) {
return;
}
var result = this._booleanOperation();
ICode.Or(result.b, result.a, result.temp);
},
/************************
* EQUALITY OPERATORS *
************************/
/**
* Verify both expressions are the same type
*/
_equalOperation: function() {
if (!this.enabled) {
return;
}
var expressionA = Stack.action.pop(),
expressionB = Stack.action.pop(),
temp = new SAR.Temp('bool');
TypeInference.addTypeDependency(expressionA.ID, expressionB.ID);
TypeInference.addKnownType(temp.ID, 'bool');
if (expressionA.type !== expressionB.type) {
throwSemanticError('Cannot compare a ' + expressionA.type + ' with a ' + expressionB.type);
}
Stack.action.push(temp);
return {
a: expressionA,
b: expressionB,
temp: temp
};
},
/**
* Verify both expressions are the same type
*/
'==': function() {
if (!this.enabled) {
return;
}
var result = this._equalOperation();
ICode.Equal(result.b, result.a, result.temp);
},
/**
* Verify both expressions are the same type
*/
'!=': function() {
if (!this.enabled) {
return;
}
var result = this._equalOperation();
ICode.NotEqual(result.b, result.a, result.temp);
}
};
module.exports = Semantics;