diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..dc369e9a --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require("config/eslint-preset"); diff --git a/packages/interpreter/src/components/interpreter.ts b/packages/interpreter/src/components/interpreter.ts index 0ca583db..4bb5c472 100644 --- a/packages/interpreter/src/components/interpreter.ts +++ b/packages/interpreter/src/components/interpreter.ts @@ -4,7 +4,6 @@ import InterpreterModule from "../module/interpreterModule"; import Scope from "./scope"; - export default class Interpreter { _parser: typeof parser; _scope: Scope; diff --git a/packages/interpreter/src/components/scope.ts b/packages/interpreter/src/components/scope.ts index 1633573d..7f73118b 100644 --- a/packages/interpreter/src/components/scope.ts +++ b/packages/interpreter/src/components/scope.ts @@ -1,3 +1,5 @@ +import RuntimeException from "../exceptions/runtimeException"; + export default class Scope { _variables: Map = new Map(); _parentScope: Scope | null; @@ -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) { @@ -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); } } diff --git a/packages/interpreter/src/components/visitor/assignmentExpression.ts b/packages/interpreter/src/components/visitor/assignmentExpression.ts index 41ab3512..36b024ca 100644 --- a/packages/interpreter/src/components/visitor/assignmentExpression.ts +++ b/packages/interpreter/src/components/visitor/assignmentExpression.ts @@ -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; - } - } } diff --git a/packages/interpreter/src/components/visitor/binaryExpression.ts b/packages/interpreter/src/components/visitor/binaryExpression.ts index 43180036..a04111ca 100644 --- a/packages/interpreter/src/components/visitor/binaryExpression.ts +++ b/packages/interpreter/src/components/visitor/binaryExpression.ts @@ -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); } } diff --git a/packages/interpreter/src/components/visitor/blockStatement.ts b/packages/interpreter/src/components/visitor/blockStatement.ts index 8c1d7e23..2517a891 100644 --- a/packages/interpreter/src/components/visitor/blockStatement.ts +++ b/packages/interpreter/src/components/visitor/blockStatement.ts @@ -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); }); } diff --git a/packages/interpreter/src/components/visitor/booleanLiteral.ts b/packages/interpreter/src/components/visitor/booleanLiteral.ts index eb4a1d80..a41a5bbd 100644 --- a/packages/interpreter/src/components/visitor/booleanLiteral.ts +++ b/packages/interpreter/src/components/visitor/booleanLiteral.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/emptyStatement.ts b/packages/interpreter/src/components/visitor/emptyStatement.ts index 5d66432e..f9b43eb9 100644 --- a/packages/interpreter/src/components/visitor/emptyStatement.ts +++ b/packages/interpreter/src/components/visitor/emptyStatement.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/expressionStatement.ts b/packages/interpreter/src/components/visitor/expressionStatement.ts index f0fce409..2757ce4d 100644 --- a/packages/interpreter/src/components/visitor/expressionStatement.ts +++ b/packages/interpreter/src/components/visitor/expressionStatement.ts @@ -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 + ); } } } diff --git a/packages/interpreter/src/components/visitor/identifierExpression.ts b/packages/interpreter/src/components/visitor/identifierExpression.ts index ac0118b0..a8d29755 100644 --- a/packages/interpreter/src/components/visitor/identifierExpression.ts +++ b/packages/interpreter/src/components/visitor/identifierExpression.ts @@ -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); } } diff --git a/packages/interpreter/src/components/visitor/index.ts b/packages/interpreter/src/components/visitor/index.ts index 2e0bccec..cc6b9e33 100644 --- a/packages/interpreter/src/components/visitor/index.ts +++ b/packages/interpreter/src/components/visitor/index.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/initStatement.ts b/packages/interpreter/src/components/visitor/initStatement.ts index 11911e6b..d8a09284 100644 --- a/packages/interpreter/src/components/visitor/initStatement.ts +++ b/packages/interpreter/src/components/visitor/initStatement.ts @@ -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); }); } } diff --git a/packages/interpreter/src/components/visitor/nullLiteral.ts b/packages/interpreter/src/components/visitor/nullLiteral.ts index 5e7a60c3..eda6d55d 100644 --- a/packages/interpreter/src/components/visitor/nullLiteral.ts +++ b/packages/interpreter/src/components/visitor/nullLiteral.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/numericLiteral.ts b/packages/interpreter/src/components/visitor/numericLiteral.ts index e8d74d89..ca022029 100644 --- a/packages/interpreter/src/components/visitor/numericLiteral.ts +++ b/packages/interpreter/src/components/visitor/numericLiteral.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/printStatement.ts b/packages/interpreter/src/components/visitor/printStatement.ts index c9fa985e..e509be69 100644 --- a/packages/interpreter/src/components/visitor/printStatement.ts +++ b/packages/interpreter/src/components/visitor/printStatement.ts @@ -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); diff --git a/packages/interpreter/src/components/visitor/program.ts b/packages/interpreter/src/components/visitor/program.ts index 665be434..f1cee73b 100644 --- a/packages/interpreter/src/components/visitor/program.ts +++ b/packages/interpreter/src/components/visitor/program.ts @@ -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); } } } diff --git a/packages/interpreter/src/components/visitor/stringLiteral.ts b/packages/interpreter/src/components/visitor/stringLiteral.ts index bd70fdb3..be354200 100644 --- a/packages/interpreter/src/components/visitor/stringLiteral.ts +++ b/packages/interpreter/src/components/visitor/stringLiteral.ts @@ -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; } diff --git a/packages/interpreter/src/components/visitor/variableDeclaration.ts b/packages/interpreter/src/components/visitor/variableDeclaration.ts index 26591848..a97bce12 100644 --- a/packages/interpreter/src/components/visitor/variableDeclaration.ts +++ b/packages/interpreter/src/components/visitor/variableDeclaration.ts @@ -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); } } } diff --git a/packages/interpreter/src/components/visitor/variableStatement.ts b/packages/interpreter/src/components/visitor/variableStatement.ts index 7c90c51b..b66af39e 100644 --- a/packages/interpreter/src/components/visitor/variableStatement.ts +++ b/packages/interpreter/src/components/visitor/variableStatement.ts @@ -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); + }); } } diff --git a/packages/interpreter/src/exceptions/invalidStateException.ts b/packages/interpreter/src/exceptions/invalidStateException.ts new file mode 100644 index 00000000..23b730c3 --- /dev/null +++ b/packages/interpreter/src/exceptions/invalidStateException.ts @@ -0,0 +1,7 @@ +export default class InvalidStateException extends Error { + constructor(errorMessage: string) { + super(errorMessage); + this.name = this.constructor.name; + this.message = errorMessage; + } +} diff --git a/packages/interpreter/src/exceptions/runtimeException.ts b/packages/interpreter/src/exceptions/runtimeException.ts new file mode 100644 index 00000000..3c2bd1d2 --- /dev/null +++ b/packages/interpreter/src/exceptions/runtimeException.ts @@ -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}`; + } +} diff --git a/packages/interpreter/src/helpers/index.ts b/packages/interpreter/src/helpers/index.ts index 820827ad..619b05fd 100644 --- a/packages/interpreter/src/helpers/index.ts +++ b/packages/interpreter/src/helpers/index.ts @@ -1,7 +1,73 @@ -export function checkNumberOperands(operands:{ left: unknown, right: unknown }): operands is { left: number, right: number } { - return (typeof operands.left === "number" && typeof operands.right === "number"); +import InvalidStateException from "../exceptions/invalidStateException"; +import RuntimeException from "../exceptions/runtimeException"; + +export function checkNumberOperands(operands: { + left: unknown; + right: unknown; +}): operands is { left: number; right: number } { + return ( + typeof operands.left === "number" && typeof operands.right === "number" + ); +} + +export function checkStringOperands(operands: { + left: unknown; + right: unknown; +}): operands is { left: string; right: string } { + return ( + typeof operands.left === "string" && typeof operands.right === "string" + ); } -export function checkStringOperands(operands:{ left: unknown, right: unknown }): operands is { left: string, right: string } { - return (typeof operands.left === "string" && typeof operands.right === "string"); +export function getOperationValue( + operands: { left: unknown; right: unknown }, + operator: string +) { + const exception = new RuntimeException( + `Ye kya kar raha hai: ${operator} ke sath ${operands.left} aur ${operands.right} nahi jamte.` + ); + + switch (operator) { + case "=": + return operands.right; + + case "+=": + case "+": + if (checkNumberOperands(operands)) { + return operands.left + operands.right; + } + + if (checkStringOperands(operands)) { + return operands.left + operands.right; + } + + throw exception; + + case "-=": + case "-": + if (checkNumberOperands(operands)) { + return operands.left - operands.right; + } + + throw exception; + + case "*=": + case "*": + if (checkNumberOperands(operands)) { + return operands.left * operands.right; + } + + throw exception; + + case "/=": + case "/": + if (checkNumberOperands(operands)) { + return operands.left / operands.right; + } + + throw exception; + + default: + throw new InvalidStateException(`Unsupported operator: ${operator}`); + } } diff --git a/packages/interpreter/src/index.ts b/packages/interpreter/src/index.ts index a83c3d4e..f7896b36 100644 --- a/packages/interpreter/src/index.ts +++ b/packages/interpreter/src/index.ts @@ -3,7 +3,6 @@ import parser from "bhai-lang-parser"; import Interpreter from "./components/interpreter"; import Scope from "./components/scope"; - const scope = new Scope(null); const interpreter = new Interpreter(parser, scope); diff --git a/packages/interpreter/src/module/interpreterModule.ts b/packages/interpreter/src/module/interpreterModule.ts index ae8bd41e..80ecc556 100644 --- a/packages/interpreter/src/module/interpreterModule.ts +++ b/packages/interpreter/src/module/interpreterModule.ts @@ -17,7 +17,7 @@ import Program from "../components/visitor/program"; import StringLiteral from "../components/visitor/stringLiteral"; import VariableDeclaration from "../components/visitor/variableDeclaration"; import VariableStatement from "../components/visitor/variableStatement"; - +import InvalidStateException from "../exceptions/invalidStateException"; export default class InterpreterModule { private static _visitorMap = { @@ -42,9 +42,13 @@ export default class InterpreterModule { static getVisitor(nodeType: string) { const visitor = InterpreterModule._visitorMap[nodeType]; - if (visitor) { - return visitor; - } + + if (!visitor) + throw new InvalidStateException( + `Couldn't find any visitor object for nodeType: ${nodeType}` + ); + + return visitor; } static getCurrentScope() { diff --git a/packages/parser/src/components/parser/program.ts b/packages/parser/src/components/parser/program.ts index 40390f0e..59540ef6 100644 --- a/packages/parser/src/components/parser/program.ts +++ b/packages/parser/src/components/parser/program.ts @@ -3,7 +3,6 @@ import { NodeType } from "../../constants/constants"; import StatementList from "./statementList"; import { ASTNode } from "./types/nodeTypes"; - export default class Program { private _statementList: StatementList; diff --git a/packages/parser/src/components/parser/statement/blockStatement.ts b/packages/parser/src/components/parser/statement/blockStatement.ts index 0adb01bb..7eafeab4 100644 --- a/packages/parser/src/components/parser/statement/blockStatement.ts +++ b/packages/parser/src/components/parser/statement/blockStatement.ts @@ -6,7 +6,6 @@ import StatementList from "../statementList"; import TokenExecutor from "../tokenExecutor"; import { ASTNode } from "../types/nodeTypes"; - export default class BlockStatement extends Statement { _statementList: StatementList; diff --git a/packages/parser/src/components/parser/statement/emptyStatement.ts b/packages/parser/src/components/parser/statement/emptyStatement.ts index 7b450fe9..e9138bff 100644 --- a/packages/parser/src/components/parser/statement/emptyStatement.ts +++ b/packages/parser/src/components/parser/statement/emptyStatement.ts @@ -4,7 +4,6 @@ import { TokenTypes } from "../../../constants/bhaiLangSpec"; import { NodeType } from "../../../constants/constants"; import { ASTNode } from "../types/nodeTypes"; - export default class EmptyStatement extends Statement { getStatement(): ASTNode { this._tokenExecutor.eatTokenAndForwardLookahead(TokenTypes.SEMI_COLON_TYPE); diff --git a/packages/parser/src/components/parser/statement/expression/identifierExpression.ts b/packages/parser/src/components/parser/statement/expression/identifierExpression.ts index 2006f7a5..830b3e7e 100644 --- a/packages/parser/src/components/parser/statement/expression/identifierExpression.ts +++ b/packages/parser/src/components/parser/statement/expression/identifierExpression.ts @@ -4,9 +4,9 @@ import { TokenTypes } from "../../../../constants/bhaiLangSpec"; import { NodeType } from "../../../../constants/constants"; import { ASTNode } from "../../types/nodeTypes"; - export default class IdentifierExpression extends Expression { - getExpression(): ASTNode{ + getExpression(): ASTNode { + const name = this._tokenExecutor.eatTokenAndForwardLookahead( TokenTypes.IDENTIFIER_TYPE ).value; diff --git a/packages/parser/src/components/parser/statement/expression/index.ts b/packages/parser/src/components/parser/statement/expression/index.ts index 2e415eeb..52c6bc39 100644 --- a/packages/parser/src/components/parser/statement/expression/index.ts +++ b/packages/parser/src/components/parser/statement/expression/index.ts @@ -3,7 +3,6 @@ import BhaiLangModule from "../../../../module/bhaiLangModule"; import TokenExecutor from "../../tokenExecutor"; import { ASTNode } from "../../types/nodeTypes"; - export default abstract class Expression { protected _tokenExecutor: TokenExecutor; @@ -39,8 +38,7 @@ export default abstract class Expression { expressionType: keyof typeof NodeType, operatorToken: string ) { - let left = - Expression.getExpressionImpl(expressionType).getExpression(); + let left = Expression.getExpressionImpl(expressionType).getExpression(); while (this._tokenExecutor.getLookahead()?.type === operatorToken) { const operator = diff --git a/packages/parser/src/components/parser/statement/expression/literals/index.ts b/packages/parser/src/components/parser/statement/expression/literals/index.ts index 76fa74da..0a131ba2 100644 --- a/packages/parser/src/components/parser/statement/expression/literals/index.ts +++ b/packages/parser/src/components/parser/statement/expression/literals/index.ts @@ -3,7 +3,6 @@ import BhaiLangModule from "../../../../../module/bhaiLangModule"; import TokenExecutor from "../../../tokenExecutor"; import { ASTNode } from "../../../types/nodeTypes"; - export default abstract class Literal { protected _tokenExecutor: TokenExecutor; diff --git a/packages/parser/src/components/parser/statement/expression/literals/nullLiteral.ts b/packages/parser/src/components/parser/statement/expression/literals/nullLiteral.ts index d3bfe8d0..61333662 100644 --- a/packages/parser/src/components/parser/statement/expression/literals/nullLiteral.ts +++ b/packages/parser/src/components/parser/statement/expression/literals/nullLiteral.ts @@ -3,12 +3,12 @@ import Literal from "."; import { NodeType } from "../../../../../constants/constants"; import { ASTNode } from "../../../types/nodeTypes"; - export default class NullLiteral extends Literal { - getLiteral(): ASTNode{ + getLiteral(): ASTNode { return { type: NodeType.NullLiteral, - value: null + value: null, + }; } } diff --git a/packages/parser/src/components/parser/statement/expressionStatement.ts b/packages/parser/src/components/parser/statement/expressionStatement.ts index e4e6bbae..73b63fdf 100644 --- a/packages/parser/src/components/parser/statement/expressionStatement.ts +++ b/packages/parser/src/components/parser/statement/expressionStatement.ts @@ -6,7 +6,6 @@ import { ASTNode } from "../types/nodeTypes"; import Expression from "./expression"; - export default class ExpressionStatement extends Statement { getStatement(): ASTNode { const expression = Expression.getExpressionImpl( diff --git a/packages/parser/src/components/parser/statement/index.ts b/packages/parser/src/components/parser/statement/index.ts index 39a27398..a17630c5 100644 --- a/packages/parser/src/components/parser/statement/index.ts +++ b/packages/parser/src/components/parser/statement/index.ts @@ -4,7 +4,6 @@ import { Token } from "../../tokenizer/types"; import TokenExecutor from "../tokenExecutor"; import { ASTNode } from "../types/nodeTypes"; - export default abstract class Statement { protected _tokenExecutor: TokenExecutor; @@ -14,7 +13,7 @@ export default abstract class Statement { abstract getStatement(): ASTNode; - static getStatementImpl(lookahead: Token): Statement{ + static getStatementImpl(lookahead: Token): Statement { switch (lookahead.type) { case TokenTypes.BOL_BHAI_TYPE: return BhaiLangModule.getPrintStatement(); diff --git a/packages/parser/src/components/parser/statement/initStatement.ts b/packages/parser/src/components/parser/statement/initStatement.ts index 90a15799..c256e8d7 100644 --- a/packages/parser/src/components/parser/statement/initStatement.ts +++ b/packages/parser/src/components/parser/statement/initStatement.ts @@ -6,7 +6,6 @@ import StatementList from "../statementList"; import TokenExecutor from "../tokenExecutor"; import { ASTNode } from "../types/nodeTypes"; - export default class InitStatement extends Statement { _statementList: StatementList; diff --git a/packages/parser/src/components/parser/statement/printStatement.ts b/packages/parser/src/components/parser/statement/printStatement.ts index 4640584a..bbac93f0 100644 --- a/packages/parser/src/components/parser/statement/printStatement.ts +++ b/packages/parser/src/components/parser/statement/printStatement.ts @@ -6,7 +6,6 @@ import { ASTNode } from "../types/nodeTypes"; import Expression from "./expression"; - export default class PrintStatement extends Statement { getStatement(): ASTNode { this._tokenExecutor.eatTokenAndForwardLookahead(TokenTypes.BOL_BHAI_TYPE); diff --git a/packages/parser/src/components/parser/statement/variableStatement.ts b/packages/parser/src/components/parser/statement/variableStatement.ts index dbe733d3..e3d8d1af 100644 --- a/packages/parser/src/components/parser/statement/variableStatement.ts +++ b/packages/parser/src/components/parser/statement/variableStatement.ts @@ -45,7 +45,7 @@ export default class VariableStatement extends Statement { return declarations; } - private _getVariableDeclaration():ASTNode { + private _getVariableDeclaration(): ASTNode { const id = Expression.getExpressionImpl( NodeType.IdentifierExpression ).getExpression(); diff --git a/packages/parser/src/components/parser/types/nodeTypes.ts b/packages/parser/src/components/parser/types/nodeTypes.ts index 67fdc522..2cb7dd1c 100644 --- a/packages/parser/src/components/parser/types/nodeTypes.ts +++ b/packages/parser/src/components/parser/types/nodeTypes.ts @@ -1,6 +1,6 @@ export type ASTNode = { type: string; - body?: ASTNode | ASTNode[] + body?: ASTNode | ASTNode[]; expressions?: ASTNode[]; expression?: ASTNode; operator?: string; @@ -11,4 +11,4 @@ export type ASTNode = { id?: ASTNode; init?: ASTNode | null; declarations?: ASTNode[]; -} +}; diff --git a/packages/parser/src/index.ts b/packages/parser/src/index.ts index 54b85f9f..96882851 100644 --- a/packages/parser/src/index.ts +++ b/packages/parser/src/index.ts @@ -1,6 +1,5 @@ import BhaiLangModule from "./module/bhaiLangModule"; - export { NodeType } from "./constants/constants"; export type { ASTNode } from "./components/parser/types/nodeTypes"; export default BhaiLangModule.getParser(); diff --git a/packages/parser/src/module/bhaiLangModule.ts b/packages/parser/src/module/bhaiLangModule.ts index 4a4b6902..623cbd8d 100644 --- a/packages/parser/src/module/bhaiLangModule.ts +++ b/packages/parser/src/module/bhaiLangModule.ts @@ -2,39 +2,26 @@ import { Parser } from "../components/parser"; import Program from "../components/parser/program"; import BlockStatement from "../components/parser/statement/blockStatement"; import EmptyStatement from "../components/parser/statement/emptyStatement"; -import AdditiveExpression - from "../components/parser/statement/expression/addititveExpression"; -import AssignmentExpression - from "../components/parser/statement/expression/assignmentExpression"; -import IdentifierExpression - from "../components/parser/statement/expression/identifierExpression"; -import BooleanLiteral - from "../components/parser/statement/expression/literals/booleanLiteral"; -import NullLiteral - from "../components/parser/statement/expression/literals/nullLiteral"; -import NumericLiteral - from "../components/parser/statement/expression/literals/numericLiteral"; -import StringLiteral - from "../components/parser/statement/expression/literals/stringLiteral"; -import MultiplicativeExpression - from "../components/parser/statement/expression/multiplicativeExpression"; -import ParanthesizedExpression - from "../components/parser/statement/expression/paranthesizedExpression"; -import PrimaryExpression - from "../components/parser/statement/expression/primaryExpression"; -import ExpressionStatement - from "../components/parser/statement/expressionStatement"; +import AdditiveExpression from "../components/parser/statement/expression/addititveExpression"; +import AssignmentExpression from "../components/parser/statement/expression/assignmentExpression"; +import IdentifierExpression from "../components/parser/statement/expression/identifierExpression"; +import BooleanLiteral from "../components/parser/statement/expression/literals/booleanLiteral"; +import NullLiteral from "../components/parser/statement/expression/literals/nullLiteral"; +import NumericLiteral from "../components/parser/statement/expression/literals/numericLiteral"; +import StringLiteral from "../components/parser/statement/expression/literals/stringLiteral"; +import MultiplicativeExpression from "../components/parser/statement/expression/multiplicativeExpression"; +import ParanthesizedExpression from "../components/parser/statement/expression/paranthesizedExpression"; +import PrimaryExpression from "../components/parser/statement/expression/primaryExpression"; +import ExpressionStatement from "../components/parser/statement/expressionStatement"; import InitStatement from "../components/parser/statement/initStatement"; import PrintStatement from "../components/parser/statement/printStatement"; -import VariableStatement - from "../components/parser/statement/variableStatement"; +import VariableStatement from "../components/parser/statement/variableStatement"; import StatementList from "../components/parser/statementList"; import TokenExecutor from "../components/parser/tokenExecutor"; import TokenizerImpl from "../components/tokenizer"; import { Tokenizer } from "../components/tokenizer/types"; import { SPEC } from "../constants/bhaiLangSpec"; - export default class BhaiLangModule { private static _tokenizer?: Tokenizer; private static _initStatement?: InitStatement; @@ -129,7 +116,8 @@ export default class BhaiLangModule { if (!this._variableStatement) this._variableStatement = new VariableStatement( this.getTokenExecutor(), - this.getNullLiteral()); + this.getNullLiteral() + ); return this._variableStatement; }