Browse Source

Merge pull request #6 from oftn/master

merge OFTN version
pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
02af6f71d8
  1. 6
      closure_conversion.js
  2. 5
      desugar.js
  3. 139
      representation.js
  4. 20
      treegen.js

6
closure_conversion.js

@ -106,7 +106,7 @@ function closure_convert(stx) {
return new rep.Closure(bound_vars, free_variables, stx, []); return new rep.Closure(bound_vars, free_variables, stx, []);
} }
function closure_convert_all(stx, env) { function closure_convert_all(stx) {
var closure; var closure;
switch (stx.exprType) { switch (stx.exprType) {
case "Let": case "Let":
@ -151,8 +151,8 @@ function test(src) {
console.log(JSON.stringify(closure_convert_all(ast), null, 4)); console.log(JSON.stringify(closure_convert_all(ast), null, 4));
} }
console.log(pprint.pprint(parser.parse(pprint.pprint(parser.parse("if something then if a then if b then c else d else rtrrt else some_other_thing")[0]))[0])); //console.log(test(pprint.pprint(parser.parse(pprint.pprint(parser.parse("if something then if a then if b then c else d else rtrrt else some_other_thing")[0]))[0])));
console.log(pprint.pprint(parser.parse("def main (print let { a = def {f = (lambda a b -> (a+b))} f} (a 2 3))")[0])); console.log(pprint.pprint(parser.parse("defop 1 Right (a $# b) (a - b) def main (4 $# b $# c)")[1]));
module.export = { module.export = {
test : test, test : test,
closureConvert : closure_convert_all closureConvert : closure_convert_all

5
desugar.js

@ -26,6 +26,7 @@ function desugarDefFunc(def) {
def.body)); def.body));
} }
function curryFunc(ps, body) { function curryFunc(ps, body) {
if (_.isEmpty(ps)) { if (_.isEmpty(ps)) {
return desugar(body); return desugar(body);
@ -55,6 +56,10 @@ function desugar(stx) {
case "Name": case "Name":
return stx; return stx;
case "Application": case "Application":
if (stx.func.ident === "::") {
//It's a type binding
return desugarTypeBinding(stx);
}
if ((stx.func.ident === "-" || if ((stx.func.ident === "-" ||
stx.func.ident === "+") && stx.func.ident === "+") &&
stx.p) { stx.p) {

139
representation.js

@ -1,3 +1,6 @@
var errors = require("./errors.js");
var _ = require("underscore");
var Expression = { var Expression = {
display : display :
function() { function() {
@ -6,18 +9,24 @@ var Expression = {
type : type :
function () { function () {
return this.exprType; return this.exprType;
}, }
unify : };
var TypeExpression = {
unify :
function (t) { function (t) {
if (this.exprType === t.exprType) { if (this.expr === t.expr) {
return t.exprType; return t.expr;
} }
else { else {
console.log("Could not unify " + this.exprType + " with " + t.exprType); console.log("Could not unify " + this.expr + " with " + t.expr);
} }
} },
isTypeExpr : true
}; };
var isTypeExpr = _.property("isTypeExpr");
function Closure(bound_vars, free_vars, body, env) { function Closure(bound_vars, free_vars, body, env) {
this.bound_vars = bound_vars; this.bound_vars = bound_vars;
this.free_vars = free_vars; this.free_vars = free_vars;
@ -163,25 +172,37 @@ function If(condition, thenexp, elseexp) {
} }
function TypeVar(name) { function TypeVar(name) {
this.exprtype = "TypeVar";
this.name = name; this.name = name;
this.exprType = "TypeVariable";
return this; return this;
} }
function TypeOp(name) { TypeVar.prototype = TypeExpression;
function TypeOp(name, params, body) {
if (!_.every(params, _.compose(
_.partial(_.isEqual, "TypeVar"),
_.property("exprtype")))) {
throw errors.JInternalError(
"Parameters to a type operator must be type variables"
);
}
this.name = name; this.name = name;
this.val = name; this.params = params;
this.exprType = "TypeOperator" this.body = body;
return this; return this;
} }
//convenience function to construct binary operators TypeOp.prototype = TypeExpression;
//assumes that the identifier refers to the name of a primitive
//operation function TypeBinding(expression, type) {
function makeBin(ident) { this.expr = expression;
return new OpT(new FuncT (new Name("a"), new FuncT(new Name("b"), new App(new App(ident, "a"), "b")))); this.type = type;
return this;
} }
TypeBinding.prototype = TypeExpression;
//Applies the function ``name'' to the list of parameters //Applies the function ``name'' to the list of parameters
function makeApp(name, parameters) { function makeApp(name, parameters) {
if (parameters) { if (parameters) {
@ -192,7 +213,6 @@ function makeApp(name, parameters) {
else { else {
return new App(name); return new App(name);
} }
} }
function makeGensym() { function makeGensym() {
@ -206,49 +226,50 @@ function makeGensym() {
var gensym = makeGensym(); var gensym = makeGensym();
OPInfo = {"+" : [4, "Left"], OPInfo = {"+" : [3, "Left"],
"-" : [4, "Left"], "-" : [3, "Left"],
"*" : [5, "Left"], "*" : [4, "Left"],
"/" : [5, "Left"], "/" : [4, "Left"],
"^" : [6, "Right"], "^" : [5, "Right"],
"++" : [4, "Left"], "++" : [3, "Left"],
"==" : [3, "Left"], "==" : [2, "Left"],
">" : [3, "Left"], ">" : [2, "Left"],
">=" : [3, "Left"], ">=" : [2, "Left"],
"<" : [3, "Left"], "<" : [2, "Left"],
"<=" : [3, "Left"], "<=" : [2, "Left"],
"&&" : [3, "Left"], "&&" : [2, "Left"],
"||" : [3, "Left"], "||" : [2, "Left"],
"::" : [1, "Left"], "::" : [2, "Left"],
":" : [2, "Left"], ":" : [1, "Left"],
"$" : [2, "Left"], "$" : [1, "Left"],
">>" : [2, "Left"], ">>" : [1, "Left"],
">>=" : [2, "Left"], ">>=" : [1, "Left"],
"<$>" : [2, "Left"], "<$>" : [1, "Left"],
"." : [2, "Left"], "." : [1, "Left"],
"," : [2, "Left"], "," : [1, "Left"]};
"->" : [2, "Right"]}
module.exports = module.exports =
{ IntT : IntT, {
FloatT : FloatT, IntT : IntT,
StrT : StrT, FloatT : FloatT,
BoolT : BoolT, StrT : StrT,
ListT : ListT, BoolT : BoolT,
FuncT : FuncT, ListT : ListT,
App : App, FuncT : FuncT,
Name : Name, App : App,
Def : Def, Name : Name,
OpT : OpT, Def : Def,
OPInfo : OPInfo, OpT : OpT,
makeApp : makeApp, OPInfo : OPInfo,
If : If, makeApp : makeApp,
DefFunc : DefFunc, If : If,
UnaryOp : UnaryOp, DefFunc : DefFunc,
Nil : Nil, UnaryOp : UnaryOp,
LetExp : LetExp, Nil : Nil,
gensym : gensym, LetExp : LetExp,
TypeVar : TypeVar, gensym : gensym,
TypeOp : TypeOp, TypeVar : TypeVar,
Closure : Closure TypeOp : TypeOp,
TypeBinding : TypeBinding,
Closure : Closure
}; };

20
treegen.js

@ -1,20 +0,0 @@
#! /usr/bin/node
var parser = require("./parse.js");
var pprint = require("./pprint.js");
var repr = require("./representation.js");
var lex = require("./tokenize.js");
var qc = require("quickcheck");
function arbIdentifier() {
var st = qc.arbString()
if (lex.isIdentifier(st)) {
return new repr.Name(st);
}
else {
return arbIdentifier();
}
}
console.log(arbIdentifier());
Loading…
Cancel
Save