-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.js
42 lines (36 loc) · 896 Bytes
/
fsm.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
class FSM {
constructor() {
this.state = "start";
}
responds_to(action) {
let fsm = this;
return fsm.states()[fsm.state][action] !== undefined;
}
take_action(action) {
let fsm = this;
// TODO: Check if state exists
return fsm.states()[fsm.state][action]();
}
update_state(new_state) {
let fsm = this;
this.state = new_state;
return;
}
process_epsilons() {
let fsm = this;
// TODO: Don't do this indefinitely
console.log("processing epsilons for ", fsm.state);
if (fsm.responds_to("epsilon")) {
return fsm.dispatch("epsilon");
}
}
dispatch(action) {
let fsm = this;
console.log("dispatching: " + fsm.state + ", action: " + action);
return fsm
.take_action(action)
.then(new_state => fsm.update_state(new_state))
.then(() => fsm.process_epsilons());
}
}
module.exports = FSM;