https://github.com/lagunoff/jexp
Simple functional language embedded in JSON
https://github.com/lagunoff/jexp
functional-language json-rpc
Last synced: 2 months ago
JSON representation
Simple functional language embedded in JSON
- Host: GitHub
- URL: https://github.com/lagunoff/jexp
- Owner: lagunoff
- Created: 2019-08-27T12:42:37.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T00:11:05.000Z (over 2 years ago)
- Last Synced: 2023-03-08T14:40:35.524Z (about 2 years ago)
- Topics: functional-language, json-rpc
- Language: TypeScript
- Size: 99.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://shields.io/)
## Installation
```sh
$ yarn add jexp```
## API reference
#### jeval`function jeval(expr: unknown, ctx?: Cons>>): unknown;`
Evaluate `JExp`
```ts
const ex01: JExp = {
let: {
one: 1,
two: 2,
},
in: {
plus: ['one', 'two'],
}
};
assert.deepEqual(jexp.jeval(ex01), 3);
``````ts
const ex02: JExp = {
let: {
times10: {lambda: 'a', body: {mult:['a', 10]} },
two: 2,
twoTimesTen: {times10: 'two'},
},
in: ['two', 'twoTimesTen'],
};
assert.deepEqual(jexp.jeval(ex02), [2, 20]);
``````ts
const ex03: JExp = {
let: {
one: 1,
two: 2,
onePlusTwo: {plus: ['one', 'two']},
quotedOnePlusTwo: {$: {plus: ['one', 'two']}},
},
in: ['one', 'two', 'onePlusTwo', 'quotedOnePlusTwo'],
};
assert.deepEqual(jexp.jeval(ex03), [1, 2, 3, {plus: ['one', 'two']}]);
``````ts
const ex04: JExp = {
let: {
someThings: {$: [
{name: 'banana', type: 'fruits'},
{name: 'teapot', type: 'dishes'},
{name: 'Sun', type: 'stars'},
]},
expressions: {_$: {
'1 + 1': {$_: {plus: [1, 1]}},
'(1 + 2) * 3': {$_: {mult: [{plus: [1, 2]}, 3]}},
'10 % 3': {$_: {mod: [10, 3]}},
}},
},
in: ['someThings', 'expressions'],
};
assert.deepEqual(jexp.jeval(ex04), [
[{name: 'banana', type: 'fruits'},
{name: 'teapot', type: 'dishes'},
{name: 'Sun', type: 'stars'},
], {
'1 + 1': 2,
'(1 + 2) * 3': 9,
'10 % 3': 1,
},
]);
```