forked from gb3h/ocaml-type-inference-in-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.ts
74 lines (63 loc) · 2.56 KB
/
nodes.ts
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* tslint:disable:max-classes-per-file */
export interface AstNode {
}
export enum basicType {
integer_literal,
bool_literal,
string_literal,
reference,
default,
}
export class Id implements AstNode {
constructor(public name: string, public type: basicType) { }
toString() { return this.name }
}
export class Lambda implements AstNode {
constructor(public args: Id[], public body: AstNode) { }
toString() { return `(fn ${this.args} => ${this.body})` }
}
export class Apply implements AstNode {
constructor(public func: AstNode, public arg: AstNode) { }
toString() { return `(${this.func} ${this.arg})` }
}
export class Let implements AstNode {
constructor(public variable: Id, public value: AstNode, public body: AstNode) { }
toString() { return `(let ${this.variable} = ${this.value} in ${this.body})` }
}
export class Letrec implements AstNode {
constructor(public variable: Id, public value: AstNode, public body: AstNode) { }
toString() { return `(let rec ${this.variable} = ${this.value} in ${this.body})` }
}
export class GlobalLet implements AstNode {
constructor(public variable: Id, public value: AstNode) { }
toString() { return `(let ${this.variable} = ${this.value})` }
}
export class GlobalLetrec implements AstNode {
constructor(public variable: Id, public value: AstNode) { }
toString() { return `(let rec ${this.variable} = ${this.value})` }
}
export class BinaryOp implements AstNode {
constructor(public operator: string, public left: AstNode, public right: AstNode) { }
toString() { return `(${this.left} ${this.operator} ${this.right})` }
}
export class Sequence implements AstNode {
constructor(public sequence: AstNode[]) { }
toString() { return `(sequence: ${this.sequence})`}
}
export class Conditional implements AstNode {
constructor(public condition: AstNode, public consequent: AstNode, public alternative?: AstNode) { }
toString() {
if (this.alternative !== undefined) {
return `(if ${this.condition} then ${this.consequent} else ${this.alternative})`
}
return `(if ${this.condition} then ${this.consequent})`
}
}
export class While implements AstNode {
constructor(public condition: AstNode, public body: AstNode) { }
toString() { return `(while ${this.condition} do ${this.body} done)` }
}
export class For implements AstNode {
constructor(public name: Id, public binding: AstNode, public end: AstNode, public body: AstNode) { }
toString() { return `(for ${this.name} = ${this.binding} to ${this.end} do ${this.body})` }
}