Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Expressions-only ECMAScript
// This gist plays around with a subset of ECMAScript6 (ES2015) that only uses expressions (except for the 'Program' const declaration).
/* Some rules:
* Arrow functions that evaluate an expression are the only kinds of functions allowed.
* All variables are declared as function parameters.
* If you want to scope variables, use the following format of an arrow function in parentheses:
* No return statement - the last expression in a list of expressions is what is returned.
* No Generators :( Or async/await :(
* Loops are done with recursive functions!
* Class expressions aren't allowed - as they use curly braces, they have to contain statements.
* Where are the semicolons and curly braces? Well, use commas within a set of parentheses.
* First parameter to Program is a standard library of sorts, usually window or global with FOR, WHILE and DOWHILE.
const Program = ({FOR,WHILE,UNTIL,DOWHILE,DOUNTIL,SWITCH,IF,...stdlib}, vars, to, declare, at, run) => (console.log(stdlib));
// This declares expression-only loops/conditionals!
const FOR = (init,test,step,body)=>((init?init:step)(),(test()?FOR(null,test,step,body):null)),
WHILE = (test,body)=>(test()?(body(),WHILE(test,body)):null),
UNTIL = (test,body)=>(!test()?(body(),UNTIL(test,body)):null),
DOWHILE = (test,body)=>(body(),(test()?DOWHILE(test,body):null)),
DOUNTIL = (test,body)=>(body(),(!test()?DOUNTIL(test,body):null)),
SWITCH = (disc, tests)=>(tests[disc]?tests[disc]:tests[void 0])(),
IF = (...tests)=>(tests.length>0?(tests[0][0]?test[0][1]():IF(...tests.slice(1))):null);
// This actually runs the program :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment