Skip to content
This repository
Browse code

Type declarations in ast

  • Loading branch information...
commit f943b2388f6c6265887f6ab70d4f0484738f2c2f 1 parent 301d982
Mikael Bung phyrex1an authored

Showing 1 changed file with 69 additions and 0 deletions. Show diff stats Hide diff stats

  1. +69 0 haskell.ast.js
69 haskell.ast.js
@@ -42,12 +42,18 @@
42 42 this.name = name;
43 43 };
44 44 ast.TypeVariable.prototype = new ast.Type();
  45 + ast.TypeVariable.prototype.stringify = function() {
  46 + return this.name;
  47 + };
45 48
46 49 ast.TypeConstructor = function(name) {
47 50 expectTypeOf(name, "string");
48 51 this.name = name;
49 52 };
50 53 ast.TypeConstructor.prototype = new ast.Type();
  54 + ast.TypeConstructor.prototype.stringify = function() {
  55 + return this.name;
  56 + };
51 57
52 58 ast.TypeApplication = function(t1, t2) {
53 59 expectType(t1, ast.Type);
@@ -56,12 +62,49 @@
56 62 this.t2 = t2;
57 63 };
58 64 ast.TypeApplication.prototype = new ast.Type();
  65 + ast.TypeApplication.prototype.stringify = function() {
  66 + return this.t1.stringify() + " " + this.t2.stringify();
  67 + };
59 68
60 69 ast.TypeTupple = function(size) {
61 70 expectTypeOf(size, "number");
62 71 this.size = size;
63 72 };
64 73 ast.TypeTupple.prototype = new ast.Type();
  74 + ast.TypeTupple.prototype.stringify = function() {
  75 + return "(" + (new Array(this.size)).join(",") + ")";
  76 + };
  77 +
  78 + /*
  79 + data Constraint = Constraint Identifier Identifier
  80 + */
  81 +
  82 + ast.Constraint = function(typeclass, typevar) {
  83 + expectTypeOf(typeclass, "string");
  84 + expectTypeOf(typevar, "string");
  85 + this.typeclass = typeclass;
  86 + this.typevar = typevar;
  87 + };
  88 + ast.Constraint.prototype.stringify = function() {
  89 + return this.typeclass + " " + this.typevar;
  90 + };
  91 +
  92 + /*
  93 + data TypeConstraint = TypeConstraint [Constraint] Type
  94 + */
  95 + ast.TypeConstraint = function(constraints, type) {
  96 + expectTypeArray(constraints, ast.Constraint);
  97 + expectType(type, ast.Type);
  98 + this.constraints = constraints;
  99 + this.type = type;
  100 + };
  101 + ast.Constraint.prototype.stringify = function() {
  102 + var constraints = this.constraints.map(function(p) { return p.stringify(); }).join(", ");
  103 + if (constraints.length > 0) {
  104 + constraints = "(" + constraints + ") => ";
  105 + }
  106 + return predsString + this.type.stringify();
  107 + };
65 108
66 109
67 110 /*
@@ -71,6 +114,7 @@
71 114 | Let Pattern Expression Expression
72 115 | Case Expression [(Pattern, Expression)]
73 116 | VariableLookup Identifier
  117 + | ExpressionTypeConstraint Expression TypeConstraint
74 118 | If Expression Expression Expression
75 119 | Do [DoNotation]
76 120 | List [Expression]
@@ -189,6 +233,21 @@
189 233 return this.identifier;
190 234 };
191 235 };
  236 + ast.ExpressionTypeConstraint = function(expr, type) {
  237 + expectType(expr, ast.Expression);
  238 + expectType(type, ast.TypeConstraint);
  239 + this.expr = expr;
  240 + this.type = type;
  241 + this.eval = function(env) {
  242 + return this.expr;
  243 + };
  244 +
  245 + this.stringify = function() {
  246 + return this.expr.stringify() + " :: " + this.type.stringify();
  247 + };
  248 + };
  249 + ast.ExpressionTypeConstraint.prototype = new ast.Expression();
  250 +
192 251 ast.If = function(ifExpr, thenExpr, elseExpr) {
193 252 expectType(ifExpr, ast.Expression);
194 253 expectType(thenExpr, ast.Expression);
@@ -215,6 +274,7 @@
215 274 };
216 275 };
217 276 ast.If.prototype = new ast.Expression();
  277 +
218 278 ast.Do = function(notations) {
219 279 expectTypeArray(notations, ast.DoNotation);
220 280 this.type="Do";
@@ -459,6 +519,7 @@
459 519 data Declaration = Variable Pattern Expression
460 520 | Function Identifier [Pattern] [(Guard, Expression)]|Expression
461 521 | Data Identifier [TVar] [Constructor]
  522 + | TypeConstraintDeclaration [Identifier] TypeConstraint
462 523 */
463 524
464 525 ast.Declaration = function(){};
@@ -519,6 +580,14 @@
519 580 };
520 581 };
521 582
  583 + ast.TypeConstraintDeclaration = function(funs, type) {
  584 + // expectTypeArrayOf(funs, "string");
  585 + expectType(type, ast.TypeConstraint);
  586 + this.funs = funs;
  587 + this.type = type;
  588 + };
  589 + ast.TypeConstraintDeclaration.prototype = new ast.Declaration();
  590 +
522 591 ast.Variable.prototype = new ast.Declaration();
523 592 ast.Data.prototype = new ast.Declaration();
524 593 ast.Function.prototype = new ast.Declaration();

0 comments on commit f943b23

Please sign in to comment.
Something went wrong with that request. Please try again.