Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added if to ast

  • Loading branch information...
commit 967a1150e895291b217da06f85cfe6e80221c85e 1 parent d8cef11
Mikael Bung authored
Showing with 28 additions and 1 deletion.
  1. +27 −0 haskell.ast.js
  2. +1 −1  haskell.parser.js
View
27 haskell.ast.js
@@ -35,6 +35,7 @@
| Let Pattern Expression Expression
| Case Expression [(Pattern, Expression)]
| VariableLookup Identifier
+ | If Expression Expression Expression
| Do [DoNotation]
| List [Expression]
| ArithmeticSequence Expression (Maybe Expression) (Maybe Expression)
@@ -152,6 +153,32 @@
return this.identifier;
};
};
+ ast.If = function(ifExpr, thenExpr, elseExpr) {
+ expectType(ifExpr, ast.Expression);
+ expectType(thenExpr, ast.Expression);
+ expectType(elseExpr, ast.Expression);
+ this.ifExpr = ifExpr;
+ this.thenExpr = thenExpr;
+ this.elseExpr = elseExpr;
+ this.eval = function(env) {
+
+ };
+
+ this.stringify = function() {
+ return "if " + this.ifExpr.stringify() + " then " + this.thenExpr.stringify() + " else " + this.elseExpr.stringify();
+ };
+
+ this.eval = function(env) {
+ var expr = new interpreter.HeapPtr(new interpreter.Closure(env, this.ifExpr));
+ var res = expr.dereference();
+ if (new ast.PatternConstructor("True", []).match(env, expr)) {
+ return this.thenExpr;
+ } else {
+ return this.elseExpr;
+ }
+ };
+ };
+ ast.If.prototype = new ast.Expression();
ast.Do = function(notations) {
expectTypeArray(notations, ast.DoNotation);
this.type="Do";
View
2  haskell.parser.js
@@ -501,7 +501,7 @@
};
var if_action = function(p) {
- return action(p, function(ast) {
+ return action(p, function(ast) {
return new haskell.ast.If(ast[0], ast[1], ast[2]);
});
};
Please sign in to comment.
Something went wrong with that request. Please try again.