Browse Source

return type alias bindings along with ast

pull/22/head
nisstyre56 11 years ago
parent
commit
252798b134
  1. 22
      desugar.js
  2. 6
      free_vars.js
  3. 11
      parse.js
  4. 1
      typecheck.js

22
desugar.js

@ -57,30 +57,34 @@ function sugarTypeDecl(stx) {
return new typ.TypeDecl(expression, type); return new typ.TypeDecl(expression, type);
} }
function desugarDefType(stx) { function desugarDefType(stx, typeEnv) {
var result; var result;
result = new typ.DefType(desugar(stx.lhs), desugar(stx.rhs)); var rhs = desugar(stx.rhs);
var name = stx.lhs.name;
typeEnv[name] = rhs;
result = new typ.DefType(stx.lhs, desugar(stx.rhs));
result.linenum = stx.linenum; result.linenum = stx.linenum;
result.charnum = stx.charnum; result.charnum = stx.charnum;
return result; return result;
} }
function desugar(stx) { function desugar(stx, typeAliases, typeEnv) {
var typeExpTest; var typeExpTest;
switch (stx.exprType) { switch (stx.exprType) {
case "If": case "If":
if (stx.elseexp) { if (stx.elseexp) {
return new typ.If(desugar(stx.condition), desugar(stx.thenexp), desugar(stx.elseexp)); return new typ.If(desugar(stx.condition, typeEnv), desugar(stx.thenexp, typeEnv), desugar(stx.elseexp, typeEnv));
} }
return new typ.If(desugar(stx.condition), desugar(stx.thenexp)); return new typ.If(desugar(stx.condition, typeEnv), desugar(stx.thenexp, typeEnv));
case "FunctionDefinition": case "FunctionDefinition":
return desugarDefFunc(stx); return desugarDefFunc(stx);
case "Definition": case "Definition":
return new typ.Def(stx.ident, desugar(stx.val)); return new typ.Def(stx.ident, desugar(stx.val, typeEnv));
case "TypeDefinition": case "TypeDefinition":
return desugarDefType(stx); return desugarDefType(stx, typeEnv);
case "Name": case "Name":
return stx; return stx;
case "Application": case "Application":
@ -104,10 +108,10 @@ function desugar(stx) {
if ((stx.func.ident === "-" || if ((stx.func.ident === "-" ||
stx.func.ident === "+") && stx.func.ident === "+") &&
stx.p) { stx.p) {
return new typ.UnaryOp(desugar(stx.func), desugar(stx.p)); return new typ.UnaryOp(desugar(stx.func, typeEnv), desugar(stx.p, typeEnv));
} }
if (stx.p) { if (stx.p) {
return new typ.App(desugar(stx.func), desugar(stx.p)); return new typ.App(desugar(stx.func, typeEnv), desugar(stx.p, typeEnv));
} }
return new typ.App(stx.func); return new typ.App(stx.func);
case "Function": case "Function":

6
free_vars.js

@ -149,11 +149,11 @@ function annotate_fvs_all(stx) {
function test(src) { function test(src) {
var ast = parser.parse(src)[0]; var ast = parser.parse(src);
console.log(JSON.stringify(annotate_fvs_all(ast), null, 4)); console.log(JSON.stringify(ast.map(annotate_fvs_all), null, 4));
} }
//console.log(test("if something then if a then if b then c else d else rtrrt else some_other_thing")); console.log(test("if something then if a then if b then c else d else rtrrt else some_other_thing"));
module.export = { module.export = {
test : test, test : test,
annotate_fvs: annotate_fvs_all annotate_fvs: annotate_fvs_all

11
parse.js

@ -807,11 +807,12 @@ function parse(tokens) {
function parseFull(tokenized) { function parseFull(tokenized) {
var ast = []; var ast = [];
var typeBindings = {};
try { try {
while (tokenized.length > 0) { while (tokenized.length > 0) {
ast.push(desugarer.desugar(parse(tokenized))); ast.push(desugarer.desugar(parse(tokenized), typeBindings));
} }
return ast; return [ast, typeBindings];
} catch (e) { } catch (e) {
if (e.stxerror !== undefined) { if (e.stxerror !== undefined) {
e.stxerror(); e.stxerror();
@ -829,5 +830,9 @@ module.exports = { parse : function(str) {
}, },
tokenize : tokenizer.tokenize tokenize : tokenizer.tokenize
}; };
/*
var istr = fs.readFileSync('/dev/stdin').toString(); var istr = fs.readFileSync('/dev/stdin').toString();
console.log(parseFull(tokenizer.tokenize(istr)).map(pprint.pprint)); var testParse = parseFull(tokenizer.tokenize(istr));
console.log(testParse[1]);
console.log(testParse[0].map(pprint.pprint));
*/

1
typecheck.js

@ -16,4 +16,3 @@ var env = require("./environments.js");
/* /*
* Map all bindings with explicit type annotations in the environment * Map all bindings with explicit type annotations in the environment
*/ */
function gather_annotations(stx) {

Loading…
Cancel
Save