UNPKG

tcl-js

Version:

tcl-js is a tcl intepreter written completely in Typescript. It is meant to replicate the tcl-sh interpreter as closely as possible.

31 lines (29 loc) 1.13 kB
import { Instruction, IOP1, IOP2, IOP3, IVAR, IEXPR, ternaryInstruction, binaryInstruction, unaryInstruction } from './instruction'; export default function substitute(tokens, variable, expr) { var newexpression = []; for (var i = 0; i < tokens.length; i++) { var item = tokens[i]; var type = item.type; if (type === IVAR && item.value === variable) { for (var j = 0; j < expr.tokens.length; j++) { var expritem = expr.tokens[j]; var replitem; if (expritem.type === IOP1) { replitem = unaryInstruction(expritem.value); } else if (expritem.type === IOP2) { replitem = binaryInstruction(expritem.value); } else if (expritem.type === IOP3) { replitem = ternaryInstruction(expritem.value); } else { replitem = new Instruction(expritem.type, expritem.value); } newexpression.push(replitem); } } else if (type === IEXPR) { newexpression.push(new Instruction(IEXPR, substitute(item.value, variable, expr))); } else { newexpression.push(item); } } return newexpression; }