Browse Source

update

pull/21/head
Wesley Kerfoot 12 years ago
parent
commit
888b2d698a
  1. 56
      desugar.js
  2. 7
      parse.js
  3. 12
      representation.js
  4. 43
      test.jl
  5. 2
      tokenize.js

56
desugar.js

@ -9,3 +9,59 @@ var typ = require("./representation.js");
// Lists get desugared to nested function calls // Lists get desugared to nested function calls
// i.e. (cons (cons (cons ...))) // i.e. (cons (cons (cons ...)))
function desugarList(lst) { function desugarList(lst) {
if (lst.xs.length <= 0) {
return new typ.Nil();
}
else {
var x = desugar(lst.xs[0]);
var rest = lst.xs.slice(1);
return new typ.App(new typ.App(new typ.Name("(:)"), x), desugarList(new typ.ListT(rest)));
}
}
function desugarDefFunc(def) {
return new typ.Def(def.ident, new typ.FuncT(desugar(def.params), desugar(def.body)));
}
//function desugarString(str) {
function desugar(stx) {
switch (stx.exprType) {
case "If":
if (stx.elseexp)
return new typ.If(desugar(stx.condition), desugar(stx.thenexp), desugar(stx.elseexp));
return new typ.If(desugar(stx.condition), desugar(stx.thenexp));
case "FunctionDefinition":
return desugarDefFunc(stx);
case "Definition":
return new typ.Def(stx.ident, desugar(stx.val));
case "Name":
return stx;
case "Application":
if (stx.p)
return new typ.App(stx.func, desugar(stx.p));
return new typ.App(stx.func);
case "Function":
return new typ.FuncT(stx.p, desugar(stx.body));
case "List":
return desugarList(stx);
case "Bool":
return stx;
case "String":
return stx;
case "Float":
return stx;
case "Integer":
return stx;
default:
return stx;
}
}
module.exports = { desugar : desugar };
//var test = typ.ListT([1,2,3]);
//console.log(desugarList(test));

7
parse.js

@ -3,6 +3,7 @@
var typ = require("./representation.js"); var typ = require("./representation.js");
var tool = require("./tools.js"); var tool = require("./tools.js");
var tokenizer = require("./tokenize.js"); var tokenizer = require("./tokenize.js");
var desugarer = require("./desugar.js");
var fs = require("fs"); var fs = require("fs");
function fst(ts) { function fst(ts) {
@ -322,7 +323,7 @@ function pprintFunc(func) {
function pprintApp(app) { function pprintApp(app) {
if (!app.p || app.p === undefined) if (!app.p || app.p === undefined)
return "(" + pprint(app.func) + ")"; return pprint(app.func);
return "((" + pprint(app.func) + ") " + pprint(app.p) + ")"; return "((" + pprint(app.func) + ") " + pprint(app.p) + ")";
} }
@ -361,13 +362,15 @@ function pprint(expr) {
return pprintIf(expr); return pprintIf(expr);
else if (expr.exprType === "Function") else if (expr.exprType === "Function")
return pprintFunc(expr); return pprintFunc(expr);
else if (expr.exprType === "Nil")
return "[]";
} }
var input = fs.readFileSync('/dev/stdin').toString(); var input = fs.readFileSync('/dev/stdin').toString();
var tokenized = tokenizer.tokenize(input).reverse().filter(function(x) { return x[0] !== "whitespace";}); var tokenized = tokenizer.tokenize(input).reverse().filter(function(x) { return x[0] !== "whitespace";});
//console.log(tokenized); //console.log(tokenized);
while (tokenized !== []) { while (tokenized !== []) {
console.log(parse(tokenized)); console.log(pprint(desugarer.desugar(parse(tokenized))));
if (!tokenized) if (!tokenized)
break; break;
} }

12
representation.js

@ -62,6 +62,12 @@ function ListT(xs) {
return this; return this;
} }
function Nil() {
this.exprType = "Nil";
return this;
}
Nil.prototype = Expression;
ListT.prototype = Expression; ListT.prototype = Expression;
function FuncT(p, body) { function FuncT(p, body) {
@ -120,7 +126,7 @@ function DefFunc(ident, params, body) {
function If(condition, thenexp, elseexp) { function If(condition, thenexp, elseexp) {
this.condition = condition; this.condition = condition;
this.thenexp = thenexp; this.thenexp = thenexp;
if (elseexp) if (elseexp)
this.elseexp = elseexp; this.elseexp = elseexp;
this.exprType = "If"; this.exprType = "If";
return this; return this;
@ -156,6 +162,7 @@ OPInfo = {"+" : [3, "Left"],
"==" : [2, "Left"], "==" : [2, "Left"],
">" : [2, "Left"], ">" : [2, "Left"],
"<" : [2, "Left"], "<" : [2, "Left"],
":" : [2, "Left"],
"$" : [1, "Left"]} "$" : [1, "Left"]}
module.exports = module.exports =
@ -172,4 +179,5 @@ module.exports =
OPInfo : OPInfo, OPInfo : OPInfo,
makeApp : makeApp, makeApp : makeApp,
If : If, If : If,
DefFunc : DefFunc} DefFunc : DefFunc,
Nil : Nil}

43
test.jl

@ -1,31 +1,24 @@
def square def (f a b)
(lambda a -> (a * a)) (a ++ b)
def pow def (add a b)
(lambda base exp -> (a + b)
(base ^ exp))
def powed (pow (2 + 3 * 5) 2) def (catstrs strs)
(foldr f (head strs) (tail strs))
def squared (square powed) def strs ["aa", "bb"]
def fact def (mymap f xs)
(lambda n -> if ((length xs) == 0)
if (n == 0) then
then 1 xs
else else
(n * (fact (n - 1)))) (: (f (head xs))
(mymap f (tail xs)))
def fib def main
(lambda n -> if (2 < 3)
if (n == 0) then
then 0 (print (mymap add [1,2,3,4 , 5]))
else else (print "")
if (n == 1)
then 1
else
(+
(fib (n - 1))
(fib (n - 2))))
def main (print (fib 15))

2
tokenize.js

@ -67,7 +67,7 @@ function tokenizeNum(tokstream) {
function tokenizeIdent(tokstream) { function tokenizeIdent(tokstream) {
var identifier = []; var identifier = [];
var n = 0; var n = 0;
while ((!isWhitespace(tokstream[0])) && (!isDigit(tokstream[0])) && isIdentifier(tokstream[0])) { while ((!isWhitespace(tokstream[0])) && isIdentifier(tokstream[0])) {
identifier.push(tokstream[0]); identifier.push(tokstream[0]);
tokstream = tokstream.substr(1); tokstream = tokstream.substr(1);
n++; n++;

Loading…
Cancel
Save