Registry / serialization / arith2js-calculator

arith2js-calculator

JSON →
library1.0.0jsnpmunverified

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.

npm install arith2js-calculator
INSTALL
IMPORT
SIG · ARITH2JS-CALCULATO
A
arith2js-calculator
serializationjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transpile
import { transpile } from 'arith2js-calculator'
const { transpile } = require('arith2js-calculator')
Package is ESM-only; CommonJS require will fail with ERR_REQUIRE_ESM.
arith2js
import arith2js from 'arith2js-calculator'
import { arith2js } from 'arith2js-calculator'
Default export is the transpiler function; named export does not exist.
Complex
import { Complex } from 'arith2js-calculator'
import Complex from 'arith2js-calculator'
Complex is a named export, not default.

Transpile a complex expression with variables and print, then evaluate the generated JS.

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); }
arith2js --version
Debug
Known issues
gotchaPackage is ESM-only; using require() will throw ERR_REQUIRE_ESM.
fix
Use dynamic import() or set "type": "module" in package.json.
affects: >=1.0.0
deprecatedThe 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.
affects: <1.0.0
gotchaVariables 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.
affects: >=1.0.0
gotchaVariable 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.
affects: >=1.0.0
breakingThe 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.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token, expected ";"
Missing semicolon between consecutive expressions.
fix
Add semicolons: e.g., 'a=1; b=2;' instead of 'a=1 b=2;'.
ReferenceError: $x is not defined
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);'.
TypeError: Complex(...).sub is not a function
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: Line 1, col 10: Variable 'y' used before declaration
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);'.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
jisonrequiredParser generator used to define the calculator grammar and produce the AST
Agent activity
28 hits · last 30 days
node
26
OpenAI (training)
1
Resources
arith2js-calculator — npm install arith2js-calculator · libregistry