arith2js – Calculator Transpiler with Scope Analysis
raw JSON → 1.0.0 verified Fri May 01 auth: no javascript
A transpiler that converts calculator expressions (supporting complex numbers, min/max operators, variables, and print statements) into executable JavaScript code. Built on jison and extended from a basic arithmetic calculator, version 1.0.0 adds scope analysis to enforce variable declaration before use, reference semantics for complex objects, and comma-separated expression sequences. Key differentiators: full complex arithmetic transpilation, meticulous error reporting with line/column, and avoidance of primitive method calls via explicit Complex() wrapping.
Common errors
error SyntaxError: Unexpected token, expected ";" ↓
cause Missing semicolon between consecutive expressions.
fix
Add semicolons: e.g., 'a=1; b=2;' instead of 'a=1 b=2;'.
error ReferenceError: $x is not defined ↓
cause Using a variable that was not declared (no prior assignment).
fix
Assign a value to the variable before use: e.g., '$x = 5; print($x);'.
error TypeError: Complex(...).sub is not a function ↓
cause Using an older version (<1.0.0) that did not wrap primitives in Complex().
fix
Upgrade to arith2js-calculator@1.0.0 or later.
error Error: Line 1, col 10: Variable 'y' used before declaration ↓
cause Referencing a variable that has not been assigned earlier in the input.
fix
Declare the variable before its first use: e.g., 'y=1; print(y);'.
Warnings
gotcha Package is ESM-only; using require() will throw ERR_REQUIRE_ESM. ↓
fix Use dynamic import() or set "type": "module" in package.json.
deprecated The direct use of Complex methods on primitives (e.g., (4-2).sub()) is no longer supported; always wrap in Complex(). ↓
fix Upgrade to >=1.0.0 to avoid primitive method call errors.
gotcha Variables use reference semantics for complex numbers; mutations through aliases affect the original. ↓
fix Use explicit cloning (e.g., Complex($a).add(0)) if copy semantics are needed.
gotcha Variable names must be prefixed with $ in the generated code, but the parser accepts unprefixed declarations. ↓
fix Declare variables without $ in the input; the transpiler adds the prefix automatically.
breaking The transpile function now throws on undeclared variable usage; previously it would silently generate possibly broken code. ↓
fix Ensure all variables are declared with an assignment before use.
Install
npm install arith2js-calculator yarn add arith2js-calculator pnpm add arith2js-calculator Imports
- transpile wrong
const { transpile } = require('arith2js-calculator')correctimport { transpile } from 'arith2js-calculator' - arith2js wrong
import { arith2js } from 'arith2js-calculator'correctimport arith2js from 'arith2js-calculator' - Complex wrong
import Complex from 'arith2js-calculator'correctimport { Complex } from 'arith2js-calculator'
Quickstart
import arith2js from 'arith2js-calculator';
const expr = 'a=2+3i; b=4-1i; print(a*b);';
try {
const jsCode = arith2js(expr);
console.log('Generated JS:');
console.log(jsCode);
// Evaluate the generated code (requires Complex class in scope)
const { Complex } = await import('arith2js-calculator');
const evalResult = eval(jsCode);
console.log('Result:', evalResult);
} catch (err) {
console.error('Transpilation error:', err.message);
}