Browse Source

closure stuff, desugar to curried functions

pull/1/head
nisstyre56 11 years ago
parent
commit
d5424b9c4b
  1. 18
      closure_conversion.js
  2. 17
      desugar.js
  3. 11
      representation.js
  4. 12
      tools.js

18
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));

17
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":

11
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
};

12
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 };

Loading…
Cancel
Save