-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.js
More file actions
35 lines (30 loc) · 833 Bytes
/
Copy pathexpr.js
File metadata and controls
35 lines (30 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var adt = require('adt'), data = adt.data, only = adt.only;
var Expr = data(function () {
return { Var : {name: only(String)}
, Lambda: {name: only(String), body: only(this)}
, App : {fun: only(this), arg: only(this)} }
});
Expr.prototype.pretty = function() {
if (this.isVar) {
return this.name;
} else if (this.isLambda) {
var body = this.body.pretty();
if (!this.body.isVar) {
body = '(' + body + ')';
}
return '/' + this.name + '. ' + body;
} else if (this.isApp) {
var lhs = this.fun.pretty();
var rhs = this.arg.pretty();
if (!this.fun.isVar) {
lhs = '(' + lhs + ')';
}
if (!this.arg.isVar) {
rhs = '(' + rhs + ')';
}
return lhs + ' ' + rhs;
}
};
module.exports = Expr;
Expr.parse = require('./parser');
require('./eval');