Skip to content

Commit

Permalink
De-smelling code (brain overload)
Browse files Browse the repository at this point in the history
  • Loading branch information
deleterium committed Feb 17, 2024
1 parent ca48c97 commit 6b2340d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/syntaxProcessor/createTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,10 @@ function KeywordToAST (tokens: TOKEN[], keywordLoc: number) : AST {
Center: createTree(tokens.slice(1))
}
case 'register':
if (tokens.length === 1 || tokens[1].type !== 'Keyword') {
if (tokens.length === 1) {
throw new Error(`At line: ${tokens[0].line}. Missing the variable type for 'register' use.`)
}
if (tokens[1].value !== 'long' && tokens[1].value !== 'fixed' && tokens[1].value !== 'void') {
throw new Error(`At line: ${tokens[0].line}. 'registers' can be only types: 'long', 'fixed' or 'void'.`)
}
validateRegisterNextToken(tokens[1])
return {
type: 'unaryASN',
Operation: tokens[0],
Expand All @@ -260,6 +258,16 @@ function KeywordToAST (tokens: TOKEN[], keywordLoc: number) : AST {
}
}

function validateRegisterNextToken (nextToken: TOKEN) {
if (nextToken.type !== 'Keyword') {
throw new Error(`At line: ${nextToken.line}. Missing the variable type for 'register' use.`)
}
if (nextToken.value !== 'long' && nextToken.value !== 'fixed' && nextToken.value !== 'void') {
throw new Error(`At line: ${nextToken.line}. 'registers' can be only types: 'long', 'fixed' or 'void'. ` +
`Found '${nextToken.value}'.`)
}
}

function UnaryOperatorToAST (tokens: TOKEN[], operatorLoc: number) : AST {
if (operatorLoc !== 0) {
throw new Error(`At line: ${tokens[operatorLoc].line}.` +
Expand Down

0 comments on commit 6b2340d

Please sign in to comment.