@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
51 lines (40 loc) • 1.56 kB
text/typescript
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";
import InvalidStateException from "../../exceptions/invalidStateException";
import NallaPointerException from "../../exceptions/nallaPointerException";
import RuntimeException from "../../exceptions/runtimeException";
import { getOperationValue } from "../../helpers";
import InterpreterModule from "../../module/interpreterModule";
export default class AssignmentExpression implements Visitor {
visitNode(node: ASTNode) {
if (!node.left)
throw new InvalidStateException(
`left node not present while executing: ${node.type}`
);
let identifier = node.left.name;
let value: unknown;
const currentScope = InterpreterModule.getCurrentScope();
if (node.right) {
value = InterpreterModule.getVisitor(node.right.type).visitNode(
node.right
);
}
if (identifier && node.operator) {
const left = currentScope.get(identifier);
if (left === null && node.operator !== "=")
throw new NallaPointerException(
`Nalla operand ni jamta "${node.operator}" ke sath`
);
if ((left === true || left === false) && node.operator !== "=")
throw new RuntimeException(
`Boolean operand ni jamta "${node.operator}" ke sath`
);
const newValue = getOperationValue(
{ left: currentScope.get(identifier), right: value },
node.operator
);
currentScope.assign(identifier, newValue);
return currentScope.get(identifier);
}
}
}