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.

38 lines (34 loc) 1.08 kB
import { IVAR, IMEMBER, IEXPR } from './instruction'; import contains from './contains'; export default function getSymbols(tokens, symbols, options) { options = options || {}; var withMembers = !!options.withMembers; var prevVar = null; for (var i = 0; i < tokens.length; i++) { var item = tokens[i]; if (item.type === IVAR && !contains(symbols, item.value)) { if (!withMembers) { symbols.push(item.value); } else if (prevVar !== null) { if (!contains(symbols, prevVar)) { symbols.push(prevVar); } prevVar = item.value; } else { prevVar = item.value; } } else if (item.type === IMEMBER && withMembers && prevVar !== null) { prevVar += '.' + item.value; } else if (item.type === IEXPR) { getSymbols(item.value, symbols, options); } else if (prevVar !== null) { if (!contains(symbols, prevVar)) { symbols.push(prevVar); } prevVar = null; } } if (prevVar !== null && !contains(symbols, prevVar)) { symbols.push(prevVar); } }