Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/geometryzen/js-to-mathscript

A transpiler from JavaScript to some syntax.
https://github.com/geometryzen/js-to-mathscript

Last synced: 9 days ago
JSON representation

A transpiler from JavaScript to some syntax.

Awesome Lists containing this project

README

        

# js-to-mathscript
A transpiler and validator from JavaScript syntax to some similar syntax.

Example of replacing the name of a Binary operator:

```typescript
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:

```typescript
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:

```typescript
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();
}
}
});
```