tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

expression.js (965B)


      1 import { sourceText } from './compiler.js';
      2 // Use the shipped definition compiler, not a second expression parser. Preserve
      3 // relative layout and isolate the expression in a single, collision-free binding.
      4 export function expressionUnit(input, names) {
      5     const expression = sourceText(input), occupied = new Set(names);
      6     let i = 0, name;
      7     do {
      8         name = `browserExpression${i++}`;
      9     } while (occupied.has(name) || expression.includes(name));
     10     return { name, source: sourceText(`${name} = (\n${expression.split('\n').map(line => '  ' + line).join('\n')}\n)`) };
     11 }
     12 export function expressionDiagnostic(error) {
     13     if (!(error instanceof Error) || !/Compiler diagnostic \d+ at \d+:\d+/.test(error.message))
     14         return error;
     15     return new Error(error.message.replace(/(Compiler diagnostic \d+) at (\d+):(\d+)/, (_match, label, line, column) => `${label} at expression ${Math.max(1, Number(line) - 1)}:${Math.max(1, Number(column) - 2)}`));
     16 }