From d5424b9c4b1d63928496fb8274936672aa43ec54 Mon Sep 17 00:00:00 2001 From: nisstyre56 Date: Mon, 10 Mar 2014 14:58:49 -0400 Subject: [PATCH] closure stuff, desugar to curried functions --- closure_conversion.js | 18 +++++++++++++----- desugar.js | 17 ++++++++++++++--- representation.js | 11 ++++++++++- tools.js | 12 +++++++++++- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/closure_conversion.js b/closure_conversion.js index 44c3a06..55a8b25 100644 --- a/closure_conversion.js +++ b/closure_conversion.js @@ -79,7 +79,8 @@ function fvs(stx) { return a+" "+b; }, "")); } - var variables; + var variables, free_variables; + switch (stx.exprType) { case "Let": var bound_vars = stx.pairs.map( @@ -90,13 +91,20 @@ function fvs(stx) { var body_fvs = fvs_helper(stx.body); variables = $.flatten(let_fvs); $.extend(variables, $.flatten(body_fvs)); - } - return $.difference($.unique($.flatten(variables)), bound_vars); + free_variables = $.difference($.unique($.flatten(variables)), bound_vars); + break; + case "Function": + + } } //var ast = parser.parse("let { c = trtr a = let {tttt = (rtertret^yyyy) } let { dfsdf = let { asdsd = 3434 } gdfgdfg } (45+(asdddy*uyuy)) q = ((lambda x y -> (x+y)) 4 ui) } (^ wat (a+(ar*b*c^twerp+\"sdfdsfsdfsdfsdf\")*rt))")[0]; //var ast = parser.parse("let { a = let { b = let {dsdfgf = sddd } fdgfg } gggggg } t")[0]; //console.log(pprint.pprint(ast)); -var ast = parser.parse("let { a = 12 b = (a + t) } (a + b * d)")[0]; -console.log(fvs(ast)); +//var ast = parser.parse("let { a = 12 b = (a + t) } (a + b * d)")[0]; +//console.log(fvs(ast)); +//var ast = parser.parse("((lambda a b c -> (+ a b c)) 2 3.0 4)"); +var ast = parser.parse("def (f a b c) 12")[0]; +//console.log(JSON.stringify(ast, null, 4)); +console.log(pprint.pprint(ast)); diff --git a/desugar.js b/desugar.js index daef9eb..e9e1feb 100644 --- a/desugar.js +++ b/desugar.js @@ -5,6 +5,7 @@ */ var typ = require("./representation.js"); +var $ = require("./tools.js"); // Lists get desugared to nested function calls // i.e. (cons (cons (cons ...))) @@ -21,8 +22,18 @@ function desugarList(lst) { function desugarDefFunc(def) { return new typ.Def(def.ident, - new typ.FuncT(desugar(def.params), - desugar(def.body))); + curryFunc(def.params, + def.body)); +} + +function curryFunc(ps, body) { + if ($.empty(ps)) { + return desugar(body); + } + else { + return new typ.FuncT(desugar($.fst(ps)), + curryFunc($.rst(ps), body)); + } } @@ -53,7 +64,7 @@ function desugar(stx) { return new typ.App(desugar(stx.func), desugar(stx.p)); return new typ.App(stx.func); case "Function": - return new typ.FuncT(stx.p, desugar(stx.body)); + return curryFunc(stx.p, stx.body); case "List": return desugarList(stx); case "Bool": diff --git a/representation.js b/representation.js index 9e23498..e357c9e 100644 --- a/representation.js +++ b/representation.js @@ -20,6 +20,14 @@ var Expression = { } }; +function Closure(bound_vars, free_vars, body, env) { + this.bound_vars = bound_vars; + this.free_vars = free_vars; + this.body = body; + this.env = env; + return this; +} + function LetExp(pairs, body) { if (!pairs.every(function(x) { return (x.exprType === "Definition" || @@ -241,5 +249,6 @@ module.exports = LetExp : LetExp, gensym : gensym, TypeVar : TypeVar, - TypeOp : TypeOp + TypeOp : TypeOp, + Closure : Closure }; diff --git a/tools.js b/tools.js index 8be3018..a2b2084 100644 --- a/tools.js +++ b/tools.js @@ -1,3 +1,7 @@ +function empty(xs) { + return xs.length === 0; +} + function identity(a) { return a; } @@ -89,6 +93,10 @@ function fst(xs) { return xs[0]; } +function rst(xs) { + return xs.slice(1,xs.length); +} + function equal(a) { return function(b) { return a === b; @@ -197,7 +205,9 @@ module.exports = {compose : compose, dict: dict, unique : unique, fst : fst, + rst : rst, eq: eq, extend : extend, flatten : flatten, - difference : difference}; + difference : difference, + empty : empty };