forked from DulLabs/bhai-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/refact: error handling, format (DulLabs#30)
* fix: file naming * feat/refact: error handling, format
- Loading branch information
1 parent
914c2d7
commit 49dcca1
Showing
39 changed files
with
192 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require("config/eslint-preset"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 9 additions & 44 deletions
53
packages/interpreter/src/components/visitor/assignmentExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,27 @@ | ||
import Visitor from "."; | ||
import { ASTNode } from "bhai-lang-parser"; | ||
|
||
import { checkNumberOperands, checkStringOperands } from "../../helpers"; | ||
import { getOperationValue } from "../../helpers"; | ||
import InterpreterModule from "../../module/interpreterModule"; | ||
|
||
|
||
export default class AssignmentExpression extends Visitor { | ||
export default class AssignmentExpression implements Visitor { | ||
visitNode(node: ASTNode) { | ||
let identifier = node.left?.name; | ||
let value: unknown; | ||
const currentScope = InterpreterModule.getCurrentScope(); | ||
|
||
if (node.right) { | ||
value = InterpreterModule.getVisitor(node.right.type)?.visitNode(node.right); | ||
value = InterpreterModule.getVisitor(node.right.type).visitNode( | ||
node.right | ||
); | ||
} | ||
|
||
if (identifier && node.operator) { | ||
const newValue = this._getNewvalue({ left:currentScope.get(identifier), right: value }, node.operator); | ||
const newValue = getOperationValue( | ||
{ left: currentScope.get(identifier), right: value }, | ||
node.operator | ||
); | ||
currentScope.assign(identifier, newValue); | ||
} | ||
} | ||
|
||
private _getNewvalue(operands:{left:unknown, right:unknown}, operator: string) { | ||
switch (operator) { | ||
case "=": | ||
return operands.right; | ||
|
||
case "+=": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left + operands.right; | ||
} | ||
|
||
if (checkStringOperands(operands)) { | ||
return operands.left + operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "-=": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left - operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "*=": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left * operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "/=": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left / operands.right; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} |
56 changes: 14 additions & 42 deletions
56
packages/interpreter/src/components/visitor/binaryExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,24 @@ | ||
import Visitor from "."; | ||
import { ASTNode } from "bhai-lang-parser"; | ||
|
||
import { checkNumberOperands, checkStringOperands } from "../../helpers"; | ||
import InvalidStateException from "../../exceptions/invalidStateException"; | ||
import { getOperationValue } from "../../helpers"; | ||
import InterpreterModule from "../../module/interpreterModule"; | ||
|
||
|
||
export default class BinaryExpression extends Visitor { | ||
export default class BinaryExpression implements Visitor { | ||
visitNode(node: ASTNode) { | ||
if (node.left && node.right && node.operator) { | ||
const left = InterpreterModule.getVisitor(node.left.type)?.visitNode(node.left); | ||
const right = InterpreterModule.getVisitor(node.right.type)?.visitNode(node.right); | ||
return this._getValue({ left, right }, node.operator); | ||
if (!node.left || !node.right || !node.operator) { | ||
throw new InvalidStateException( | ||
`Left , right or operator not found for: ${node.type}` | ||
); | ||
} | ||
} | ||
|
||
_getValue(operands:{ left: unknown, right: unknown }, operator: string) { | ||
switch (operator) { | ||
case "+": | ||
if(checkNumberOperands(operands)){ | ||
return operands.left + operands.right; | ||
} | ||
|
||
if(checkStringOperands(operands)){ | ||
return operands.left + operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "-": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left - operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "*": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left * operands.right; | ||
} | ||
|
||
break; | ||
|
||
case "/": | ||
if (checkNumberOperands(operands)) { | ||
return operands.left / operands.right; | ||
} | ||
|
||
break; | ||
} | ||
const left = InterpreterModule.getVisitor(node.left.type).visitNode( | ||
node.left | ||
); | ||
const right = InterpreterModule.getVisitor(node.right.type).visitNode( | ||
node.right | ||
); | ||
return getOperationValue({ left, right }, node.operator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { ASTNode } from "bhai-lang-parser"; | ||
|
||
|
||
export default abstract class Visitor { | ||
abstract visitNode(node: ASTNode): unknown; | ||
export default interface Visitor { | ||
visitNode(node: ASTNode): unknown; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 13 additions & 9 deletions
22
packages/interpreter/src/components/visitor/variableDeclaration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import Visitor from "."; | ||
import { ASTNode } from "bhai-lang-parser"; | ||
|
||
import InvalidStateException from "../../exceptions/invalidStateException"; | ||
import InterpreterModule from "../../module/interpreterModule"; | ||
|
||
|
||
export default class VariableDeclaration extends Visitor{ | ||
export default class VariableDeclaration implements Visitor { | ||
visitNode(node: ASTNode) { | ||
if (node.id && node.init) { | ||
const identifier = node.id?.name; | ||
const value = InterpreterModule.getVisitor(node.init.type)?.visitNode(node.init); | ||
const currentScope = InterpreterModule.getCurrentScope(); | ||
if ( identifier) { | ||
currentScope.declare(identifier, value); | ||
} | ||
if (!node.id || !node.init) { | ||
throw new InvalidStateException(`id or init not found for ${node.type}`); | ||
} | ||
|
||
const identifier = node.id?.name; | ||
const value = InterpreterModule.getVisitor(node.init.type).visitNode( | ||
node.init | ||
); | ||
const currentScope = InterpreterModule.getCurrentScope(); | ||
if (identifier) { | ||
currentScope.declare(identifier, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class InvalidStateException extends Error { | ||
constructor(errorMessage: string) { | ||
super(errorMessage); | ||
this.name = this.constructor.name; | ||
this.message = errorMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default class RuntimeException extends Error { | ||
constructor(errorMessage: string) { | ||
super(errorMessage); | ||
this.name = this.constructor.name; | ||
this.message = `Are bhai bhai bhai.... : ${errorMessage}`; | ||
} | ||
} |
Oops, something went wrong.