Browse Source

update

pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
888b2d698a
  1. 56
      desugar.js
  2. 7
      parse.js
  3. 12
      representation.js
  4. 45
      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
// i.e. (cons (cons (cons ...)))
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 tool = require("./tools.js");
var tokenizer = require("./tokenize.js");
var desugarer = require("./desugar.js");
var fs = require("fs");
function fst(ts) {
@ -322,7 +323,7 @@ function pprintFunc(func) {
function pprintApp(app) {
if (!app.p || app.p === undefined)
return "(" + pprint(app.func) + ")";
return pprint(app.func);
return "((" + pprint(app.func) + ") " + pprint(app.p) + ")";
}
@ -361,13 +362,15 @@ function pprint(expr) {
return pprintIf(expr);
else if (expr.exprType === "Function")
return pprintFunc(expr);
else if (expr.exprType === "Nil")
return "[]";
}
var input = fs.readFileSync('/dev/stdin').toString();
var tokenized = tokenizer.tokenize(input).reverse().filter(function(x) { return x[0] !== "whitespace";});
//console.log(tokenized);
while (tokenized !== []) {
console.log(parse(tokenized));
console.log(pprint(desugarer.desugar(parse(tokenized))));
if (!tokenized)
break;
}

12
representation.js

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

45
test.jl

@ -1,31 +1,24 @@
def square
(lambda a -> (a * a))
def (f a b)
(a ++ b)
def pow
(lambda base exp ->
(base ^ exp))
def (add a b)
(a + b)
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
(lambda n ->
if (n == 0)
then 1
else
(n * (fact (n - 1))))
def (mymap f xs)
if ((length xs) == 0)
then
xs
else
(: (f (head xs))
(mymap f (tail xs)))
def fib
(lambda n ->
if (n == 0)
then 0
else
if (n == 1)
then 1
else
(+
(fib (n - 1))
(fib (n - 2))))
def main (print (fib 15))
def main
if (2 < 3)
then
(print (mymap add [1,2,3,4 , 5]))
else (print "")

2
tokenize.js

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

Loading…
Cancel
Save