Use a simple IR that should also produce better code
This commit is contained in:
parent
62f93875c1
commit
e510661d9c
182
sf2xed.py
182
sf2xed.py
|
@ -2,10 +2,10 @@
|
||||||
import enum
|
import enum
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
class writes(enum.Enum):
|
class actions(enum.Enum):
|
||||||
zero, one, same = range(3)
|
change, left, right, nothing = range(4)
|
||||||
|
|
||||||
State = namedtuple('State', ['write', 'move', 'transitions'])
|
State = namedtuple('State', ['action', 'transitions', 'reachable'])
|
||||||
|
|
||||||
class SFParseError(Exception): pass
|
class SFParseError(Exception): pass
|
||||||
|
|
||||||
|
@ -41,138 +41,108 @@ def parse_smallfuck(s):
|
||||||
parsed, index = parse(s)
|
parsed, index = parse(s)
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
def chunk(parsed):
|
def turingify(parsed):
|
||||||
def append_change_move():
|
states = []
|
||||||
nonlocal change, move, chunked
|
def add_state(action, transitions):
|
||||||
while change or move != 0:
|
nonlocal states
|
||||||
chunk = '*' if change else ''
|
assert action in actions
|
||||||
change = False
|
assert len(transitions) == 2
|
||||||
|
|
||||||
if move < 0:
|
index = len(states)
|
||||||
chunk += '<'
|
states.append(State(action, transitions, [False, False]))
|
||||||
move += 1
|
|
||||||
elif move > 0:
|
|
||||||
chunk += '>'
|
|
||||||
move -= 1
|
|
||||||
|
|
||||||
if chunk != '':
|
return index
|
||||||
chunked.append(chunk)
|
|
||||||
|
def worker(parsed, end = None):
|
||||||
|
nonlocal states
|
||||||
|
|
||||||
|
while len(parsed) > 0:
|
||||||
|
c = parsed[-1]
|
||||||
|
parsed = parsed[:-1]
|
||||||
|
|
||||||
chunked = []
|
|
||||||
change = False
|
|
||||||
move = 0
|
|
||||||
for c in parsed:
|
|
||||||
if type(c) == list:
|
if type(c) == list:
|
||||||
append_change_move()
|
loop_test = add_state(actions.nothing, [end, None])
|
||||||
chunked.append(chunk(c))
|
loop_body = worker(c, loop_test)
|
||||||
|
# We want the loop to repeat if the cell is set
|
||||||
|
states[loop_test].transitions[1] = loop_body
|
||||||
|
end = loop_test
|
||||||
|
|
||||||
elif c == '*':
|
elif c == '*':
|
||||||
if move == 0:
|
end = add_state(actions.change, [end, end])
|
||||||
change = not change
|
|
||||||
else:
|
|
||||||
append_change_move()
|
|
||||||
change = True
|
|
||||||
|
|
||||||
elif c == '<':
|
elif c == '<':
|
||||||
move -= 1
|
end = add_state(actions.left, [end, end])
|
||||||
|
|
||||||
elif c == '>':
|
elif c == '>':
|
||||||
move += 1
|
end = add_state(actions.right, [end, end])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
assert not "Unreachable"
|
assert not "Unreachable"
|
||||||
|
|
||||||
append_change_move()
|
return end
|
||||||
|
|
||||||
return chunked
|
return worker(parsed), states
|
||||||
|
|
||||||
def turingify(chunked):
|
|
||||||
states = []
|
|
||||||
def add_state(write, move, transitions):
|
|
||||||
nonlocal states
|
|
||||||
assert write in writes
|
|
||||||
assert -1 <= move <= 1
|
|
||||||
assert len(transitions) == 2
|
|
||||||
|
|
||||||
index = len(states)
|
|
||||||
states.append(State(write, move, transitions))
|
|
||||||
|
|
||||||
return index
|
|
||||||
|
|
||||||
def worker(chunked, end = None, in_loop = False):
|
|
||||||
nonlocal states
|
|
||||||
|
|
||||||
ifunset = ifset = end
|
|
||||||
while len(chunked) > 0:
|
|
||||||
c = chunked[-1]
|
|
||||||
chunked = chunked[:-1]
|
|
||||||
|
|
||||||
if type(c) == list:
|
|
||||||
loop_test = add_state(writes.same, 0, [ifunset, None])
|
|
||||||
loop_body = worker(c, loop_test, True)
|
|
||||||
# We want the loop to repeat if the cell is set
|
|
||||||
states[loop_test].transitions[1] = loop_body
|
|
||||||
ifunset = ifset = loop_test
|
|
||||||
|
|
||||||
else:
|
|
||||||
change = '*' in c
|
|
||||||
move = (1 if '>' in c else 0) - (1 if '<' in c else 0)
|
|
||||||
|
|
||||||
assert change or move != 0
|
|
||||||
|
|
||||||
if change:
|
|
||||||
# Create a pair of states
|
|
||||||
new_ifunset = add_state(writes.one, move, [ifunset, ifset])
|
|
||||||
new_ifset = add_state(writes.zero, move, [ifunset, ifset])
|
|
||||||
|
|
||||||
ifunset, ifset = new_ifunset, new_ifset
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Create only one state
|
|
||||||
ifunset = ifset = add_state(writes.same, move, [ifunset, ifset])
|
|
||||||
|
|
||||||
if in_loop:
|
|
||||||
# At the start of the loop body, the cell will always be set
|
|
||||||
return ifset
|
|
||||||
else:
|
|
||||||
# At the start of the program, the cell will always be unset
|
|
||||||
return ifunset
|
|
||||||
|
|
||||||
return worker(chunked), states
|
|
||||||
|
|
||||||
def prettyprint_states(states):
|
def prettyprint_states(states):
|
||||||
for i in range(len(states)):
|
for i in range(len(states)):
|
||||||
state = states[i]
|
state = states[i]
|
||||||
write = {writes.zero: '0 ', writes.one: '1 ', writes.same: ''}[state.write]
|
action = {actions.change: '* ', actions.left: '< ', actions.right: '> ', actions.nothing: ''}[state.action]
|
||||||
move = {-1: '< ', 0: '', 1: '> '}[state.move]
|
if state.reachable == [True, True]:
|
||||||
print('%i: %s%s%s' % (i, write, move, state.transitions))
|
reachable = '(*)'
|
||||||
|
elif state.reachable == [True, False]:
|
||||||
|
reachable = '(0)'
|
||||||
|
elif state.reachable == [False, True]:
|
||||||
|
reachable = '(1)'
|
||||||
|
elif state.reachable == [False, False]:
|
||||||
|
reachable = '(!)'
|
||||||
|
else:
|
||||||
|
reachable = ''
|
||||||
|
print('%4i%s: %s%s' % (i, reachable, action, state.transitions))
|
||||||
|
|
||||||
def tree_shake(start, states):
|
def reachability(start, states):
|
||||||
# Since we always end up with start at index 0, no need to create shaken_start
|
def copy_state(index):
|
||||||
shaken = [states[start]]
|
state = states[index]
|
||||||
translations = {start: 0, None: None}
|
action = state.action
|
||||||
|
transitions = state.transitions[:]
|
||||||
|
reachable = [False if i is None else i for i in state.reachable]
|
||||||
|
return State(action, transitions, reachable)
|
||||||
|
|
||||||
shaken_sweep = 0
|
# Since we always end up with start at index 0, no need to create processed_start
|
||||||
while shaken_sweep < len(shaken):
|
processed = [copy_state(start)]
|
||||||
for i in range(len(shaken[shaken_sweep].transitions)):
|
processed[0].reachable[0] = True
|
||||||
index_states = shaken[shaken_sweep].transitions[i]
|
processed[0].reachable[1] = True
|
||||||
|
translations = {start: 0}
|
||||||
|
|
||||||
|
processed_sweep = 0
|
||||||
|
while processed_sweep < len(processed):
|
||||||
|
for i in range(len(processed[processed_sweep].transitions)):
|
||||||
|
index_states = processed[processed_sweep].transitions[i]
|
||||||
|
|
||||||
|
# Nothing to be done if we point to halt state
|
||||||
|
if index_states is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Copy the state to processed if it isn't already there
|
||||||
if index_states not in translations:
|
if index_states not in translations:
|
||||||
index_shaken = len(shaken)
|
index_processed = len(processed)
|
||||||
shaken.append(states[index_states])
|
processed.append(copy_state(index_states))
|
||||||
translations[index_states] = index_shaken
|
translations[index_states] = index_processed
|
||||||
|
|
||||||
shaken[shaken_sweep].transitions[i] = translations[index_states]
|
# Fix transition to the state
|
||||||
|
processed[processed_sweep].transitions[i] = translations[index_states]
|
||||||
|
|
||||||
shaken_sweep += 1
|
# Mark how we got to the state
|
||||||
|
processed[translations[index_states]].reachable[i] = True
|
||||||
|
|
||||||
return shaken
|
processed_sweep += 1
|
||||||
|
|
||||||
|
return processed
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
program = input('program: ')
|
program = input('program: ')
|
||||||
start_nonshaken, states_nonshaken = turingify(chunk(parse_smallfuck(program)))
|
start_noreachability, states_noreachability = turingify(parse_smallfuck(program))
|
||||||
shaken = tree_shake(start_nonshaken, states_nonshaken)
|
processed = reachability(start_noreachability, states_noreachability)
|
||||||
prettyprint_states(shaken)
|
prettyprint_states(processed)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue