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

48 lines (34 loc) 1.46 kB
import Visitor from "."; import { ASTNode } from "bhai-lang-parser"; import RuntimeException from "../../exceptions/runtimeException"; import InterpreterModule from "../../module/interpreterModule"; import Scope from "../scope"; export default class WhileStatement implements Visitor { visitNode(node: ASTNode) { const test = node.test; if (test) { const getConditionValue = ()=> InterpreterModule.getVisitor(test.type).visitNode(test); const start = new Date(); const parentScope = InterpreterModule.getCurrentScope(); InterpreterModule.setCurrentScope(new Scope(parentScope)); InterpreterModule.getCurrentScope().setLoop(true); for (let testResult = getConditionValue(); testResult === true || testResult === "sahi"; testResult = getConditionValue()) { if (InterpreterModule.getCurrentScope().isBreakStatement()) { break; } if (Date.now() - start.getTime() > 1000) { throw new RuntimeException("Bohot jyada hi chale jaa rha hai loop"); } if(InterpreterModule.getCurrentScope().isContinueStatement()){ InterpreterModule.getCurrentScope().setContinueStatement(false); continue; } const body = node.body; if (body && !Array.isArray(body)) { InterpreterModule.getVisitor(body.type).visitNode(body); } } InterpreterModule.setCurrentScope(parentScope); } } }