@@ -235,6 +235,9 @@ function parsePrint(): SyntaxTree {
235
235
* 0 1 2 3 4
236
236
* a = b + c
237
237
*
238
+ * 0 1 2
239
+ * a = b
240
+ *
238
241
* 01
239
242
* a++
240
243
* a--
@@ -250,15 +253,14 @@ function parseExpression(): SyntaxTree {
250
253
throw `${ result . lexeme } not declared at ${ getLocation ( result ) } .`
251
254
}
252
255
253
- if ( type . name != "NUMBER" ) {
254
- throw `${ result . lexeme } is not of number type.`
255
- }
256
-
257
256
let operation : Token = getToken ( currentIndex + 1 )
258
257
259
258
/* Check type of operation */
260
259
if ( operation . lexeme != "=" ) {
261
260
if ( operation . lexeme == "++" || operation . lexeme == "--" ) {
261
+ if ( type . name != "NUMBER" ) {
262
+ throw `${ result . lexeme } is not of number type.`
263
+ }
262
264
if ( operation . lexeme == "++" ) {
263
265
operation = {
264
266
type : Type . PUNCTUATION ,
@@ -306,25 +308,40 @@ function parseExpression(): SyntaxTree {
306
308
307
309
let operation : Token = getToken ( currentIndex + 3 )
308
310
311
+ /* Assign */
309
312
if ( operation . type != Type . PUNCTUATION ) {
310
313
adjust = 3
314
+
315
+ let argument2 : SyntaxTree
316
+
317
+ /* Constant */
318
+ if ( address1 . type == Type . CONSTANT ) {
319
+ argument2 = createConstant ( address1 )
320
+ /* Identifier */
321
+ } else {
322
+ argument2 = {
323
+ content : VARIABLE ,
324
+ argument1 : { content : address1 } ,
325
+ argument2 : { content : type1 }
326
+ }
327
+ }
311
328
expression = {
312
329
content : ASSIGN ,
313
330
argument1 : {
314
331
content : VARIABLE ,
315
332
argument1 : { content : result } ,
316
333
argument2 : { content : type }
317
334
} ,
318
- argument2 : {
319
- content : VARIABLE ,
320
- argument1 : { content : address1 } ,
321
- argument2 : { content : type1 }
322
- }
335
+ argument2 : argument2
323
336
}
324
337
} else {
325
338
let address2 : Token = getToken ( currentIndex + 4 )
326
339
let type2 : Token = getType ( address2 , root )
327
340
341
+ if ( type . name != "NUMBER" ) {
342
+ throw `${ result . lexeme } is not of number type.`
343
+ }
344
+
328
345
if ( type1 . name != type2 . name ) {
329
346
throw `${ address1 . lexeme } and ${ address2 . lexeme } are not of same type at ${ getLocation ( operation ) } .`
330
347
}
@@ -361,6 +378,16 @@ function parseExpression(): SyntaxTree {
361
378
362
379
function getType ( target : Token , tree : SyntaxTree = root ) : Token {
363
380
let result : Token = null
381
+ /* Constant */
382
+ if ( target . type == Type . CONSTANT ) {
383
+ return {
384
+ type : Type . TYPE ,
385
+ name : target . name ,
386
+ lexeme : target . name ,
387
+ location : null
388
+ }
389
+ }
390
+ /* Variable */
364
391
if ( tree . argument1 != null ) {
365
392
if ( tree . argument1 . content . lexeme == target . lexeme ) {
366
393
return tree . argument2 . content
0 commit comments