Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Half finished ast.toString implementation

  • Loading branch information...
commit a3693a9d925217347b28070833760dd496fd60c2 1 parent 18dec16
Mikael Bung authored
Showing with 60 additions and 7 deletions.
  1. +60 −7 ast.js
View
67 ast.js
@@ -58,24 +58,44 @@ haskell.ast.Value = function() {
haskell.ast.Char = function(c) {
this.c = c;
+
+ this.toString = function() {
+ return "'" + this.c + "'";
+ };
};
haskell.ast.Num = function(num){
-
this.num = num;
+ this.toString = function() {
+ return "" + this.num;
+ };
};
haskell.ast.ConstantConstructor = function(identifier){
this.identifier = identifier;
+ this.toString = function() {
+ return this.identifier;
+ };
};
haskell.ast.Fraction = function(fraction) {
this.fraction = fraction;
+ this.toString = function() {
+ return this.fraction;
+ };
};
haskell.ast.Lambda = function(patterns, expr){
this.patterns = patterns;
this.expr = expr;
+
+ this.patternsToString() {
+ return this.patterns.map(function(p) {return p.toString()}).join(" ");
+ }
+
+ this.toString = function() {
+ return "(\ " + this.patternsToString() + " -> " + this.expr.toString() + " )"
+ };
};
haskell.ast.Char.prototype = haskell.ast.Value;
@@ -87,6 +107,9 @@ haskell.ast.Lambda.prototype = haskell.ast.Value;
haskell.ast.Module = function(declarations) {
this.declarations = declarations;
+ this.toString = function() {
+ return this.declerations.map(function(d) {return d.toString()}).join("\n");
+ };
};
haskell.ast.Expression = function() {
@@ -95,27 +118,43 @@ haskell.ast.Expression = function() {
haskell.ast.ConstantExpression = function(value) {
this.value = value; // :: a
+ this.toString = function() {
+ return this.value.toString();
+ };
};
haskell.ast.Application = function(func,expr) {
this.func = func; // :: a -> b
this.expr = expr; // :: a
+ this.toString = function() {
+ return "(" + this.func.toString() + " " + this.expr.toString() + ")";
+ };
};
haskell.ast.IfExpression = function(boolexpr,trueexpr,falseexpr) {
this.boolexpr = boolexpr; // :: Bool
this.trueexpr = trueexpr; // :: a
this.falseexpr = falseexpr; // :: a
+ this.toString = function() {
+ return "if " + this.boolexpr.toString() + " then " + this.trueexpr.toString() + " else " + this.falseexpr.toString();
+ };
};
haskell.ast.CaseExpression = function(testexpr,cases) {
this.testexpr = testexpr; // :: a
this.cases = cases; // :: [("Pattern" a, b)]
+ this.toString = function() {
+ return "case " + this.testexpr.toString() + "of\n" +
+ this.cases.map(function(c) {return c[0].toString() + " -> " + c[1].toString()}).join("\n\t");
+ };
};
haskell.ast.LetDeclaration = function(decleration,expression) {
this.decleration = decleration; // no type
this.expression = expression; // :: a
+ this.toString = function() {
+ return "let " + this.decleration.toString() + " in " + this.expression.toString();
+ };
};
haskell.ast.ConstantExpression.prototype = haskell.ast.Expression;
@@ -131,23 +170,37 @@ haskell.ast.Pattern = function() {
haskell.ast.PatternConstructor = function(constructor,patterns) {
this.constructor = constructor;
this.patterns = patterns;
+ this.toString = function() {
+ return "(" + this.constructor + " " + this.patterns.map(function(p) {return p.toString()}).join(" ") + ")";
+ };
};
haskell.ast.PatternVariableBinding = function(identifier) {
this.identifier = identifier;
+ this.toString = function() {
+ return this.identifier;
+ }
};
haskell.ast.PatternIgnored = function() {
-
+ this.toString = function() {
+ return "_";
+ };
};
haskell.ast.PatternCombined = function(identifier,pattern) {
this.identifier = identifier;
this.pattern = pattern;
+ this.toString = function() {
+ return this.identifier + "@" + this.pattern.toString();
+ };
};
haskell.ast.PatternConstant = function(value) {
this.value = value;
+ this.toString = function() {
+ return this.value.toString();
+ };
};
haskell.ast.PatternConstructor.prototype = haskell.ast.Pattern;
@@ -161,13 +214,12 @@ haskell.ast.Declaration = function() {
};
haskell.ast.VarDef = function(pattern, expression, wheres) {
- if (wheres==null)
- {
- wheres=[];
- }
this.pattern = pattern;
this.expression = expression;
- this.wheres = wheres;
+ this.wheres = wheres == null ? [] : wheres;
+ this.toString = function() {
+ return this.pattern.toString() + " = " + this.expression.toString() + "\n\twhere\n\t\t" + this.wheres.map(function(w){return w.toString()}).join("\n\t");
+ };
};
haskell.ast.FunDef = function(identifier, patterns, expression, wheres) {
@@ -175,6 +227,7 @@ haskell.ast.FunDef = function(identifier, patterns, expression, wheres) {
this.patterns = patterns;
this.expression = expression;
this.wheres = wheres==null ? [] : wheres;
+
};
Please sign in to comment.
Something went wrong with that request. Please try again.