Browse Source

handle unary arithmetic operations correctly

pull/21/head
Wesley Kerfoot 11 years ago
parent
commit
e8d74ef3a0
  1. 5
      desugar.js
  2. 21
      fib.jl
  3. 11
      optimize.js
  4. 2
      pprint.js
  5. 9
      representation.js
  6. 14
      test.jl

5
desugar.js

@ -39,6 +39,11 @@ function desugar(stx) {
case "Name":
return stx;
case "Application":
if ((stx.func.ident === "-" ||
stx.func.ident === "+") &&
stx.p) {
return new typ.UnaryOp(desugar(stx.func), desugar(stx.p));
}
if (stx.p)
return new typ.App(desugar(stx.func), desugar(stx.p));
return new typ.App(stx.func);

21
fib.jl

@ -0,0 +1,21 @@
def (fib n)
if (n == 0)
then 0
else if (n == 1)
then 1
else
((fib (n-1)) +
(fib (n-2)))
def (fact n)
if (n == 0)
then 1
else
((fact (n-1)) * n)
def ns [1,2,3,4,5]
def main
((print (fib 19)) >>
(print (ns >>= (return . fact))))

11
optimize.js

@ -0,0 +1,11 @@
var typ = require("./representation.js");
/*function simplify(stx) {
switch (stx.exprType) {
case "Application":
}
}*/
//function simplifyUnary(stx) {

2
pprint.js

@ -53,6 +53,8 @@ function pprint(expr) {
return pprintFunc(expr);
else if (expr.exprType === "Nil")
return "[]";
else if (expr.exprType === "Unary")
return "("+expr.op.ident+" "+pprint(expr.val)+")";
}
module.exports = {pprint : pprint};

9
representation.js

@ -18,6 +18,12 @@ var Expression = {
}
};
function UnaryOp(op, v) {
this.exprType = "Unary";
this.val = v;
this.op = op;
return this;
}
function IntT(v) {
this.exprType = "Integer";
@ -186,4 +192,5 @@ module.exports =
makeApp : makeApp,
If : If,
DefFunc : DefFunc,
Nil : Nil}
UnaryOp : UnaryOp,
Nil : Nil }

14
test.jl

@ -5,7 +5,9 @@ def (add a b)
(a + b)
def (catstrs strs)
(foldr f (head strs) (tail strs))
(foldr f
(head strs)
(tail strs))
def strs ["aa", "bb"]
@ -14,16 +16,20 @@ def (mymap f xs)
then
xs
else
(: (f (head xs))
(mymap f (tail xs)))
((f (head xs))
: (mymap f (tail xs)))
def empty []
def getFile
(readFile "./parse.js")
def (testUnary n)
((-n) + n)
def main
((print (testUnary 6)) >>
if False
then
undefined
else getFile
else getFile)

Loading…
Cancel
Save