js-to-mathscript
A transpiler and validator from JavaScript syntax to some similar syntax.
Example of replacing the name of a Binary operator:
it('Exponentiation', function () {
const src = 'x**2';
const { code } = jsToMathScript(src,
{
namespace: '',
binOp: {
'**': { kind: 'rename', value: '^' }
},
unaryOp: {}
},
{
format: {
semicolons: false
}
}
);
assert.strictEqual(code, 'x ^ 2');
});
Example of converting a Binary operator to a function call:
it('Bitwise XOR (^) should be turned into a function.', function () {
const src = 'x ^ y';
const { code } = jsToMathScript(src, {
namespace: 'MathScript',
binOp: {
'**': { kind: 'rename', value: '^' },
'^': { kind: 'function', value: 'wedge' }
},
unaryOp: {}
},
{
format: {
semicolons: false
}
});
assert.strictEqual(code, 'MathScript.wedge(x, y)');
});
Example of rejecting a Unary operator:
it("error", function () {
try {
const src = "const a = +b";
jsToMathScript(src, {
unaryOp: {
'+': { kind: 'error', value: "Unexpected unary plus (+)" }
}
});
assert.fail();
} catch (e) {
if (e instanceof Error) {
assert.strictEqual(e.name, "Error");
assert.strictEqual(e.message, "Unexpected unary plus (+)");
} else {
assert.fail();
}
}
});