Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit ad4cb6b

Browse files
committed
Merge branch 'feature/reassign' into develop
2 parents b6c69a9 + 679b695 commit ad4cb6b

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/debug-print.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ export function logTree(node: SyntaxTree, indent: number = 0, side: string = ">"
5959

6060
/* Infix traversal */
6161
logTree(node.argument1, indent + 1, '/')
62-
console.log(
63-
prefix + (`[${colors.magenta(Type[node.content.type])}] - ${colors.cyan(node.content.name)}, ${colors.grey(node.content.lexeme)}\t`)
64-
)
62+
if (node.content != null) {
63+
console.log(
64+
prefix + (`[${colors.magenta(Type[node.content.type])}] - ${colors.cyan(node.content.name)}, ${colors.grey(node.content.lexeme)}\t`)
65+
)
66+
} else {
67+
console.log(
68+
prefix + (colors.yellow(`[NULL]\t`))
69+
)
70+
}
6571
logTree(node.argument2, indent + 1, '\\')
6672
}
6773

src/parser.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ function parsePrint(): SyntaxTree {
235235
* 0 1 2 3 4
236236
* a = b + c
237237
*
238+
* 0 1 2
239+
* a = b
240+
*
238241
* 01
239242
* a++
240243
* a--
@@ -250,15 +253,14 @@ function parseExpression(): SyntaxTree {
250253
throw `${result.lexeme} not declared at ${getLocation(result)}.`
251254
}
252255

253-
if (type.name != "NUMBER") {
254-
throw `${result.lexeme} is not of number type.`
255-
}
256-
257256
let operation: Token = getToken(currentIndex + 1)
258257

259258
/* Check type of operation */
260259
if (operation.lexeme != "=") {
261260
if (operation.lexeme == "++" || operation.lexeme == "--") {
261+
if (type.name != "NUMBER") {
262+
throw `${result.lexeme} is not of number type.`
263+
}
262264
if (operation.lexeme == "++") {
263265
operation = {
264266
type: Type.PUNCTUATION,
@@ -306,25 +308,40 @@ function parseExpression(): SyntaxTree {
306308

307309
let operation: Token = getToken(currentIndex + 3)
308310

311+
/* Assign */
309312
if (operation.type != Type.PUNCTUATION) {
310313
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+
}
311328
expression = {
312329
content: ASSIGN,
313330
argument1: {
314331
content: VARIABLE,
315332
argument1: { content: result },
316333
argument2: { content: type }
317334
},
318-
argument2: {
319-
content: VARIABLE,
320-
argument1: { content: address1 },
321-
argument2: { content: type1 }
322-
}
335+
argument2: argument2
323336
}
324337
} else {
325338
let address2: Token = getToken(currentIndex + 4)
326339
let type2: Token = getType(address2, root)
327340

341+
if (type.name != "NUMBER") {
342+
throw `${result.lexeme} is not of number type.`
343+
}
344+
328345
if (type1.name != type2.name) {
329346
throw `${address1.lexeme} and ${address2.lexeme} are not of same type at ${getLocation(operation)}.`
330347
}
@@ -361,6 +378,16 @@ function parseExpression(): SyntaxTree {
361378

362379
function getType(target: Token, tree: SyntaxTree = root): Token {
363380
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 */
364391
if (tree.argument1 != null) {
365392
if (tree.argument1.content.lexeme == target.lexeme) {
366393
return tree.argument2.content

0 commit comments

Comments
 (0)