Permalink
Browse files

Merge branch 'master' of git@github.com:johang88/haskellinjavascript

  • Loading branch information...
2 parents d0b05d1 + c5a6d7a commit 831dacf3dafb41ff9c3317d83d73e85e671ede0c Mikael Bung committed May 20, 2010
View
@@ -108,7 +108,7 @@
this.expr = expr;
this.eval = function(env) {
var newEnv = interpreter.loadDeclarations(this.declr, env.derive());
- return this.expr.eval(newEnv);
+ return new interpreter.Closure(newEnv, this.expr);
};
this.stringify = function() {
return "let {" + this.declr.map(function (d) {
@@ -127,7 +127,7 @@
for (var i in this.cases) {
var newEnv = env.derive();
if (this.cases[i][0].match(newEnv, expr)) {
- return this.cases[i][1].eval(newEnv);
+ return new interpreter.Closure(newEnv, this.cases[i][1]);
};
};
throw new Error("No matching clause");
@@ -202,7 +202,7 @@
var funname = 'enumFrom';
if (this.e2) funname = funname + 'Then';
if (this.e3) funname = funname + 'To';
- var application = new ast.Application(ast.VariableLookup(funname), this.e1);
+ var application = new ast.Application(new ast.VariableLookup(funname), this.e1);
if (this.e2) application = new ast.Application(application, this.e2);
if (this.e3) application = new ast.Application(application, this.e3);
return application;
View
@@ -115,12 +115,12 @@ commands[":help"] = "HELP";
for (var i = 0; i < result.ptrs.length; i++) {
if (str.length == 0 && first) {
str = showResult(result.ptrs[i].dereference());
- if (str.str)
+ if (typeof str.str != "undefined")
str = str.str;
first = false;
} else {
var res = showResult(result.ptrs[i].dereference());
- if (res.str)
+ if (typeof res.str != "undefined")
res = res.str;
str = str + op + res;
}
@@ -142,7 +142,7 @@ commands[":help"] = "HELP";
result = result.str;
result = result.substring(0, result.length - 3);
result = "[" + result + "]";
- } else if (result.str) {
+ } else if (typeof result.str != "undefined") {
result = result.str;
}
View
@@ -228,8 +228,8 @@
this.pattern = pattern;
this.expression = expression;
this.apply = function(argument) {
- var substitution = this.env.substitute(this.pattern, argument);
- return this.expression.eval(substitution);
+ var newEnv = this.env.substitute(this.pattern, argument);
+ return new interpreter.Closure(newEnv, this.expression);
};
this.stringify = function() {
@@ -260,11 +260,11 @@
var expression = matchedFunc.expression[j][1];
var guardResult = guard.eval(newEnv);
if (guardResult.identifier == "True") {
- return expression.eval(newEnv);
+ return new interpreter.Closure(newEnv, expression);
}
}
} else {
- return matchedFunc.expression.eval(newEnv);
+ return new interpreter.Closure(newEnv, matchedFunc.expression);
}
}
}
View
@@ -406,7 +406,10 @@
var aexp_arithmetic_action = function(p) {
return action(p, function(ast) {
- return ast;
+ if (ast[1]) {
+ ast[1] = ast[1][0];
+ }
+ return new haskell.ast.ArithmeticSequence(ast[0], ast[1], ast[2]);
});
}
@@ -431,7 +434,7 @@
list_action(sequence(expect(ws('[')), optional(wlist(exp, ',')), expect(ws(']')))), // list constructor
left_section_action(sequence(expect(ws('(')), ws(infixexp), ws(qop), expect(ws(')')))), // left section
right_section_action(sequence(expect(ws('(')), ws(qop), ws(infixexp), expect(ws(')')))), // right section, todo: look into resolution of infixexp in this case, see Haskell Report Chapter 3
- aexp_arithmetic_action(sequence(expectws('['), exp, repeat0(exp), expectws('..'), optional(ws(exp)), expectws(']'))), // arithmetic sequence
+ aexp_arithmetic_action(sequence(expectws('['), ws(exp), optional(sequence(expectws(','), ws(exp))), expectws('..'), optional(ws(exp)), expectws(']'))), // arithmetic sequence
aexp_list_comp_action(sequence(expectws('['), ws(exp), expectws('|'), list(qual, ws(',')), expectws(']'))) // list comprehension
// Todo:
// Labeled construction
View
@@ -47,8 +47,11 @@ foldr1 f xs = case xs of
filter _ [] = []
filter f (x:xs ) | f x = x : filter f xs
| otherwise = filter f xs
+
+iterate f x = f x : iterate f (f x)
-iterate f x = f x : iterate f x
+zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
+zipWith _ _ _ = []
head xs = case xs of
(x:_) -> x
@@ -73,6 +76,16 @@ data Int = I# Int#
(==) (I# i1) (I# i2) = i1 ==# i2
+(>) (I# i1) (I# i2) = i1 ># i2
+
+(<) (I# i1) (I# i2) = i1 <# i2
+
+(<=) (I# i1) (I# i2) = i1 <=# i2
+
+(>=) (I# i1) (I# i2) = i1 >=# i2
+
+(%) (I# i1) (I# i2) = I# (remInt# i1 i2)
+
stepDebug = stepDebug#
data Maybe a = Just a | Nothing
@@ -108,3 +121,27 @@ double m = do
let doubleFunc = (*2)
x <- m
return (doubleFunc x)
+
+
+take 0 _ = []
+take n (x:xs) = x : take (n-1) xs
+take _ [] = []
+
+length [] = 0
+length (_:xs) = 1 + length xs
+
+
+const r _ = r
+
+-- Enum functions only for int so far, awaiting type classes
+enumHelper i p n = case p n of
+ True -> []
+ False -> n : enumHelper i p (n+i)
+
+enumFrom e1 = enumHelper 1 (const False) e1
+
+enumFromThen e1 e2 = enumHelper (e2-e1) (const False) e1
+
+enumFromTo e1 e3 = enumHelper 1 (>e3) e1
+
+enumFromThenTo e1 e2 e3 = enumHelper (e2-e1) (>e3) e1
Oops, something went wrong.

0 comments on commit 831dacf

Please sign in to comment.