-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkont.js
134 lines (114 loc) · 3.9 KB
/
kont.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Store } from './store';
import { State } from './state';
import { Environment } from './env';
import { print, error, unimplemented } from './utils';
import { CESKDone } from './ast';
// continuations
class Kont {
constructor(next) {
this._next = next;
}
get next() { return this._next; }
// used by HandlerKont + throw
handle(thrown, store) {
return this.next.handle(thrown, store);
}
// used by AssignKont + return
apply(returnValue, store) {
return this.next.apply(returnValue, store);
}
// used by LeaveScopeKont + falling off a lexical scope
leaveScope(store) {
return this.next.leaveScope(store);
}
// used by LeaveHandlerKont + falling off a the end of a try block
leaveHandler(store) {
return this.next.leaveHandler(store);
}
}
// this continuation is matched with 'return' statements inside called
// functions. function calls register this continuation before
// returning the State corresponding to the function's body, and when
// the function returns, it apply's the current kont. This bubbles up
// the stack until we find the topmost AssignKont.
export class AssignKont extends Kont {
constructor(name, stmt, fp, kont) {
super(kont);
this._name = name;
this._stmt = stmt;
this._fp = fp;
}
get stmt() { return this._stmt; }
get fp() { return this._fp; }
toString() { return 'AssignKont'; }
apply(returnValue, store) {
let store_;
if (this._name)
store_ = Store.extend(store, this._fp.offset(this._name), returnValue);
else
store_ = store;
return new State(this._stmt, this._fp, store_, this.next);
}
}
export class LeaveScopeKont extends Kont {
constructor(stmt, fp, kont) {
super(kont);
this._stmt = stmt;
this._fp = fp;
}
get stmt() { return this._stmt; }
get fp() { return this._fp; }
toString() { return 'LeaveScopeKont'; }
leaveScope(store) {
return new State(this._stmt, this._fp, store, this.next);
}
}
export class HandlerKont extends Kont {
constructor(catchClause, fp, kont) {
super(kont);
this._fp = fp;
this._catchClause = catchClause;
}
get fp() { return this._fp; }
get catchClause() { return this._catchClause; }
toString() { return 'HandlerKont'; }
handle(thrown, store) {
let catch_body = this._catchClause.body;
let fp_ = this._fp;
let store_ = store;
let kont_ = this.next;
if (catch_body._leave_scope_ins) {
// if the catch block had a body at all we've already
// inserted the %leaveScope() instruction. in that case,
// install a new environment with a single binding, and
// start stepping through the inside of the catch clause's
// block directly (note .body.body[0] instead of .body)
fp_ = new Environment();
store_ = Store.extend(store, fp_.offset(this._catchClause.param.name), thrown);
fp_._parent = this._fp;
kont_ = new LeaveScopeKont(catch_body._leave_scope_ins.nextStmt, this._fp, kont_);
return new State(this._catchClause.body.body[0], fp_, store_, kont_);
}
return new State(this._catchClause.body, fp_, store_, kont_);
}
leaveHandler(store) {
return new State(this._stmt, this._fp, store, this.next);
}
}
export class HaltKont extends Kont {
constructor() { super(null); }
toString() { return 'HaltKont'; }
apply(returnValue, store) {
unimplemented('HaltKont.apply');
}
handle(thrown, store) {
print('unhandled exception!');
return new State(new CESKDone(), null, null, null);
}
leaveScope(store) {
unimplemented('HaltKont.leaveScope');
}
leaveHandler(store) {
unimplemented('HaltKont.leaveHandler');
}
}