UNPKG

@adamsy/bhai-lang

Version:

<h1 align="center">Bhai Lang</h1> <p align="center"> <a href="https://lgtm.com/projects/g/DulLabs/bhai-lang/alerts/"><img alt="Total alerts" src="https://img.shields.io/lgtm/alerts/g/DulLabs/bhai-lang.svg?logo=lgtm&logoWidth=18"/></a> <a href="https://lgt

30 lines (22 loc) 899 B
import Visitor from "."; import { ASTNode, NodeType } from "bhai-lang-parser"; import InvalidStateException from "../../exceptions/invalidStateException"; import InterpreterModule from "../../module/interpreterModule"; export default class VariableDeclaration implements Visitor { visitNode(node: ASTNode) { if (!node.id || !node.init) { throw new InvalidStateException(`id or init not found for ${node.type}`); } const identifier = node.id.name; let value; if (node.init.type === NodeType.NullLiteral) value = null; else if (node.init.type === NodeType.BooleanLiteral) value = node.init.value === "sahi" ? true : false; else value = InterpreterModule.getVisitor(node.init.type).visitNode(node.init); const currentScope = InterpreterModule.getCurrentScope(); if (identifier) { currentScope.declare(identifier, value); } } }