Skip to content

Commit

Permalink
feat/refact: error handling, format (DulLabs#30)
Browse files Browse the repository at this point in the history
* fix: file naming

* feat/refact: error handling, format
  • Loading branch information
aniketsingh0104 authored Jan 22, 2022
1 parent 914c2d7 commit 49dcca1
Show file tree
Hide file tree
Showing 39 changed files with 192 additions and 191 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("config/eslint-preset");
1 change: 0 additions & 1 deletion packages/interpreter/src/components/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import InterpreterModule from "../module/interpreterModule";

import Scope from "./scope";


export default class Interpreter {
_parser: typeof parser;
_scope: Scope;
Expand Down
10 changes: 9 additions & 1 deletion packages/interpreter/src/components/scope.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import RuntimeException from "../exceptions/runtimeException";

export default class Scope {
_variables: Map<string, unknown> = new Map();
_parentScope: Scope | null;
Expand All @@ -14,6 +16,8 @@ export default class Scope {
if (this._parentScope !== null) {
return this._parentScope.get(identifier);
}

throw new RuntimeException(`Variable ${identifier} to bana le pehle.`);
}

assign(identifier: string, value: unknown) {
Expand All @@ -26,9 +30,13 @@ export default class Scope {
this._parentScope.assign(identifier, value);
return;
}

throw new RuntimeException(
`Variable ${identifier} to bana le pehle fir assign karna.`
);
}

declare(identifier: string, value: unknown) {
declare(identifier: string, value: unknown) {
this._variables.set(identifier, value);
}
}
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 packages/interpreter/src/components/visitor/binaryExpression.ts
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);
}
}
5 changes: 2 additions & 3 deletions packages/interpreter/src/components/visitor/blockStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import { ASTNode } from "bhai-lang-parser";
import InterpreterModule from "../../module/interpreterModule";
import Scope from "../scope";


export default class BlockStatement extends Visitor {
export default class BlockStatement implements Visitor {
visitNode(node: ASTNode) {
const parentScope = InterpreterModule.getCurrentScope();
InterpreterModule.setCurrentScope(new Scope(parentScope));

if (Array.isArray(node.body)) {
node.body?.forEach((statement: ASTNode) => {
this.visitNode(statement);
InterpreterModule.getVisitor(statement.type).visitNode(statement);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";


export default class BooleanLiteral extends Visitor {
export default class BooleanLiteral implements Visitor {
visitNode(node: ASTNode) {
return node.value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";


export default class EmptyStatement extends Visitor {
export default class EmptyStatement implements Visitor {
visitNode(_: ASTNode) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";


export default class ExpressionStatement extends Visitor {
export default class ExpressionStatement implements Visitor {
visitNode(node: ASTNode) {
if (node.expression) {
InterpreterModule.getVisitor(node.expression.type)?.visitNode(node.expression);
InterpreterModule.getVisitor(node.expression.type).visitNode(
node.expression
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";
import InvalidStateException from "../../../dist/exceptions/invalidStateException";


export default class IdentifierExpression extends Visitor {
export default class IdentifierExpression implements Visitor {
visitNode(node: ASTNode) {
if (typeof node.name === "string") {
return InterpreterModule.getCurrentScope().get(node.name);
}
if (!node.name) {
throw new InvalidStateException(`Invalid node name for: ${node.type}`);
}

return InterpreterModule.getCurrentScope().get(node.name);
}
}
5 changes: 2 additions & 3 deletions packages/interpreter/src/components/visitor/index.ts
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;
}
5 changes: 2 additions & 3 deletions packages/interpreter/src/components/visitor/initStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";


export default class InitStatement extends Visitor {
export default class InitStatement implements Visitor {
visitNode(node: ASTNode) {
if (Array.isArray(node.body)) {
node.body?.forEach((statement: ASTNode) => {
InterpreterModule.getVisitor(statement.type)?.visitNode(statement);
InterpreterModule.getVisitor(statement.type).visitNode(statement);
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/interpreter/src/components/visitor/nullLiteral.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";


export default class NullLiteral extends Visitor {
export default class NullLiteral implements Visitor {
visitNode(node: ASTNode) {
return node.value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";


export default class NumericLiteral extends Visitor {
export default class NumericLiteral implements Visitor {
visitNode(node: ASTNode) {
return node.value;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/interpreter/src/components/visitor/printStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";


export default class PrintStatement extends Visitor {
export default class PrintStatement implements Visitor {
visitNode(node: ASTNode) {
const value = node.expressions
?.map((expression: ASTNode) =>
InterpreterModule.getVisitor(expression.type)?.visitNode(expression)
InterpreterModule.getVisitor(expression.type).visitNode(expression)
)
.join("");
console.log(value);
Expand Down
5 changes: 2 additions & 3 deletions packages/interpreter/src/components/visitor/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";


export default class Program extends Visitor {
export default class Program implements Visitor {
visitNode(node: ASTNode) {
if (!Array.isArray(node.body) && node.body?.type) {
InterpreterModule.getVisitor(node.body?.type)?.visitNode(node.body);
InterpreterModule.getVisitor(node.body?.type).visitNode(node.body);
}
}
}
3 changes: 1 addition & 2 deletions packages/interpreter/src/components/visitor/stringLiteral.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";


export default class StringLiteral extends Visitor {
export default class StringLiteral implements Visitor {
visitNode(node: ASTNode) {
return node.value;
}
Expand Down
22 changes: 13 additions & 9 deletions packages/interpreter/src/components/visitor/variableDeclaration.ts
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { ASTNode } from "bhai-lang-parser";

import InterpreterModule from "../../module/interpreterModule";


export default class VariableStatement extends Visitor {
export default class VariableStatement implements Visitor {
visitNode(node: ASTNode) {
node.declarations?.forEach(declaration => {
InterpreterModule.getVisitor(declaration.type)?.visitNode(declaration);
})
node.declarations?.forEach((declaration) => {
InterpreterModule.getVisitor(declaration.type).visitNode(declaration);
});
}
}
7 changes: 7 additions & 0 deletions packages/interpreter/src/exceptions/invalidStateException.ts
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;
}
}
7 changes: 7 additions & 0 deletions packages/interpreter/src/exceptions/runtimeException.ts
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}`;
}
}
Loading

0 comments on commit 49dcca1

Please sign in to comment.