genish.js
Version:
72 lines (56 loc) • 1.87 kB
JavaScript
//test
gen = require('./dist/index.js'); accum = gen.accum; add = gen.add; param =gen.param; mul = gen.mul;test=acc = accum( .1, param() );add( mul( acc, 2 ), acc );b = gen.gen.createCallback( test );b()
gen = require('./dist/index.js');
accum = gen.accum; add = gen.add; param =gen.param;
mul = gen.mul; acc= accum(.1,param());
test=add( mul( acc, 2 ), acc );
b = gen.gen.createCallback( test );
b()
gen = require('./dist/index.js');
accum = gen.accum; add = gen.add; param =gen.param;
mul = gen.mul; p = param();
test= add( mul( p, 2 ), p );
b = gen.gen.createCallback( test );
b()
// convert
acc = accum( .1, param() );
test = add( mul( acc, 2 ), acc )
out = gen.createCallback( test )
// to:
function( accum1, p0 ) {
'use strict';
let accum1_incr = 0.1, accum1_reset = p0, accum1_out;
accum1.value += accum1_incr
if (accum1_reset >= 1) {
accum1.value = accum1.min
} else {
if (accum1.value > accum1.max) accum1.value = accum1.min
}
accum1_out = accum1.value
return accum1_out * 2 + accum1_out
}
// convert accum(.1, param() )
1. add p0 to callback arguments
2. create function body
3. process value .1: convert all instances of _incr to .1 in code
4. process value param(): convert all instances of _reset to p0
5. memoize: gen.memo.accum1 = 'accum1.value'
// convert mul( acc,2 )
1. lookup acc in memo: 'accum1.value'
2. combine with binop: 'accum1.value * 2'
3. surround in parenthesis: '(accum1.value * 2)'
// process add( mul, acc )
1. get value from mul: '(accum1.value * 2)'
2. add acc: '( accum1.value * 2 ) + acc'
// add return to last line
1. 'return ( accum1.value * 2 ) + acc'
// final function
function( accum1, p0 ) {
accum1.value += .1
if (p0 >= 1) {
accum1.value = accum1.min
} else {
if (accum1.value > accum1.max) accum1.value = accum1.min
}
return (accum1.value * 2) + acc
}