Skip to content
/ jexp Public

Simple functional language embedded in JSON

Notifications You must be signed in to change notification settings

lagunoff/jexp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generic badge

Installation

$ yarn add jexp

API reference

jeval

function jeval(expr: unknown, ctx?: Cons<Readonly<Record<string, unknown>>>): unknown;

Evaluate JExp

 const ex01: JExp = {
   let: {
     one: 1,
     two: 2,
   },
   in: {
     plus: ['one', 'two'],
   }
 };
 assert.deepEqual(jexp.jeval(ex01), 3);
 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]);
 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']}]);
 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,
   },
 ]);

About

Simple functional language embedded in JSON

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published