-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodes.py
48 lines (40 loc) · 1.31 KB
/
modes.py
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
"modes.py -- mode-switching logic"
import collections
Link = collections.namedtuple('Link', 'latch state')
class ModeState:
def __init__(self):
assert self.graph
self.stack = [self.initial_state]
def state(self):
return self.stack[-1]
def tick(self):
"if link was a shift instead of a latch, return to base"
if len(self.stack) > 1:
popped = self.stack.pop()
def command(self, codepoint):
link = self.graph[codepoint]
if link.latch:
self.stack[-1] = link.state
else:
self.stack.append(link.state)
class MainState(ModeState):
graph = {
900: Link(True, 'text'),
901: Link(True, 'byte'),
902: Link(True, 'num'),
913: Link(False, 'byte'), # todo: illegal from num mode
924: Link(True, 'byte'), # something special about 924 but I haven't read the manual
}
initial_state = 'text'
class TextState(ModeState):
graph = {
# note: don't really need a table, there's a pattern
# todo: some transitions are illegal
'll': Link(True, 'lower'),
'ps': Link(False, 'punc'),
'ml': Link(True, 'mixed'),
'as': Link(False, 'alpha'),
'al': Link(True, 'alpha'),
'pl': Link(True, 'punc'),
}
initial_state = 'alpha'