Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/geometryzen/js-to-mathscript
- Owner: geometryzen
- License: mit
- Created: 2022-02-12T01:53:51.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-05T20:43:39.000Z (almost 2 years ago)
- Last Synced: 2024-04-24T19:33:29.488Z (7 months ago)
- Language: TypeScript
- Size: 657 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
}
}
});
```