author | Alan Dipert
<alan@dipert.org> 2019-11-01 04:20:20 UTC |
committer | Alan Dipert
<alan@dipert.org> 2019-11-01 04:20:20 UTC |
parent | 912c558d5b6579aa05f5b06ecdba77bb39a74082 |
jacl.js | +23 | -8 |
diff --git a/jacl.js b/jacl.js index 378a49f..e36c048 100644 --- a/jacl.js +++ b/jacl.js @@ -1418,6 +1418,21 @@ const emitNode = (print, node) => { print('}'); if (context === 'sval') print(')()'); break; + case 'lambda': + print('(function(){\n'); + // TODO enhance arg length check + print(`if(arguments.length!==${node.lambdaList.required.length})throw new Error("Invalid number of arguments: "+arguments.length);\n`); + let argidx = 0; + if (node.lambdaList.required.length) print('var '); + for (let i = 0; i < node.lambdaList.required.length; i++) { + print(mungeSym(node.lambdaList.required[i], 'local')); + print(`=arguments[${argidx++}]`); + if (i < node.lambdaList.required.length-1) print(','); + } + if (node.lambdaList.required.length) print(';'); + emitBlock(print, node.statements, node.ret); + print('})'); + break; case 'call': if (context === 'return') print('return '); emitNode(print, node.f); @@ -1439,9 +1454,9 @@ const emitNode = (print, node) => { emitNode(print, node.elseNode); print(')'); } else { - print('if('); + print('if(truth('); emitNode(print, node.testNode); - print(')\n\t'); + print('))\n\t'); emitNode(print, node.thenNode); print('else\n\t'); emitNode(print, node.elseNode); @@ -1490,12 +1505,12 @@ var buf = new BufferedStream(), console.log('read:', obj); const node = analyze(emptyEnv, null, obj); console.log('analyzed:', node); - //const sb = new StringBuffer(); - //emitNode(sb.append.bind(sb), node); - //const code = sb.toString(); - //console.log('generated:', code); - //const evald = eval(code); - //console.log('evaled:', evald); + const sb = new StringBuffer(); + emitNode(sb.append.bind(sb), node); + const code = sb.toString(); + console.log('generated:', code); + const evald = eval(code); + console.log('evaled:', evald); //console.log('parsed', parseLambdaList(evald)); } })()