Last active
July 17, 2017 17:55
-
-
Save halcy/3e254964b8d945b68cb88378593ffe31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compile_tm(source_code): | |
# First pass: Parse code | |
lines = source_code.splitlines() | |
states = {} | |
initial_state = "" | |
state_name = "" | |
symbol_count = 0 | |
for line in lines: | |
# Skip comments, whitespace | |
if len(line) == 0 or line[0] == '#': | |
continue | |
# First line is symbol count | |
if symbol_count == 0: | |
symbol_count = int(line) | |
continue | |
if line[0] == "+": | |
state_name = line[1:] | |
if state_name[-1] == '!': | |
state_name = state_name[:-1] | |
initial_state = state_name | |
states[state_name] = {} | |
else: | |
symbol_in, movement, next_state, symbol_out = line.split(' ') | |
# Special symbol x: "all unspecified inputs" | |
if symbol_in == 'x': | |
for s in range(symbol_count): | |
if str(s) in states[state_name]: | |
continue | |
# Can also use special symbol x for output: Same as input | |
if symbol_out == 'x': | |
states[state_name][str(s)] = (next_state, str(s), movement) | |
else: | |
states[state_name][str(s)] = (next_state, symbol_out, movement) | |
else: | |
states[state_name][symbol_in] = (next_state, symbol_out, movement) | |
# Enumerate states | |
state_count = 0 | |
state_numbers = {} | |
states_ordered = [] | |
# Store initial | |
state_numbers[initial_state] = 0 | |
states_ordered.append(initial_state) | |
state_count += 1 | |
# Store rest | |
for state in states.keys(): | |
if state == initial_state: | |
continue | |
state_numbers[state] = state_count | |
states_ordered.append(state) | |
state_count += 1 | |
# Movements as numbers | |
movement_dict = { | |
'l': 1, | |
'r': 0, | |
'u': 2, | |
'd': 3 | |
} | |
# Generate string | |
tm_commands = [str(state_count), str(symbol_count)] | |
for symbol_in in range(symbol_count): | |
for state in states_ordered: | |
next_state, symbol_out, movement = states[state][str(symbol_in)] | |
tm_commands.append(str(state_numbers[next_state])) | |
tm_commands.append(str(symbol_out)) | |
tm_commands.append(str(movement_dict[movement[0]])) | |
# Done | |
return(",".join(tm_commands)) | |
# Code | |
source_code = """ | |
# Symbol count | |
4 | |
## | |
# PART I: Init | |
## | |
# Starting off: Fill left line | |
+fill_line_left! | |
x down fill_line_left 3 | |
3 right fill_line 3 | |
# Part 2: Fill first line | |
+fill_line | |
x right fill_line 1 | |
3 down third1_d1 3 | |
# Then, go 3 down 1 right until you hit the line again | |
+third1_d1 | |
x down third1_d2 x | |
1 down third2_d1 2 | |
+third1_d2 | |
x down third1_d3 x | |
1 down third2_d1 2 | |
+third1_d3 | |
x down third1_r x | |
1 down third2_d1 2 | |
+third1_r | |
x right third1_d1 x | |
1 down third2_d1 2 | |
# Same thing again | |
+third2_d1 | |
x down third2_d2 x | |
1 right fill_to_2 2 | |
+third2_d2 | |
x down third2_d3 x | |
1 right fill_to_2 2 | |
+third2_d3 | |
x down third2_r x | |
1 right fill_to_2 2 | |
+third2_r | |
x right third2_d1 x | |
1 right fill_to_2 2 | |
# Now fill until 2 is encountered, skipping 3 | |
+fill_to_2 | |
x right fill_to_2 2 | |
3 right fill_to_2 3 | |
2 right goto_3 2 | |
# Go to first 3 (start of line) | |
+goto_3 | |
x right goto_3 x | |
3 down nextline 3 | |
## | |
# PART 2: Copy-down | |
## | |
# Having come from above, go to the first "real" pixel | |
+nextline | |
x right nextline2 x | |
+nextline2 | |
x right upleft_1 x | |
# Go up-left | |
+upleft_1 | |
x up upleft_2 x | |
+upleft_2 | |
x left check_col x | |
# Check color and branch | |
+check_col | |
1 down downright_c1_1 1 | |
2 down downright_c2_1 2 | |
3 down shiftthrough 3 | |
# Should not happen | |
x down fill_line_left x | |
+downright_c1_1 | |
x right downright_c1_2 x | |
+downright_c2_1 | |
x right downright_c2_2 x | |
+downright_c1_2 | |
x right upleft_1 1 | |
+downright_c2_2 | |
x right upleft_1 2 | |
# Shift colour from the left of this cell right through the line of 3s | |
+shiftthrough | |
x left shiftthrough_check x | |
+shiftthrough_check | |
1 right shiftthrough_check_c1_1 1 | |
2 right shiftthrough_check_c2_1 2 | |
# Should not happen | |
x right fill_line_left x | |
+shiftthrough_check_c1_1 | |
x right shiftthrough_check_c1_2 x | |
+shiftthrough_check_c2_1 | |
x right shiftthrough_check_c2_2 x | |
+shiftthrough_check_c1_2 | |
x left incline 1 | |
+shiftthrough_check_c2_2 | |
x left incline 2 | |
# Increment line | |
+incline | |
x down nextline 3 | |
""" | |
# Make url and print | |
tm_string = compile_tm(source_code) | |
base_url = "https://maximecb.github.io/Turing-Drawings/#" | |
print(base_url + tm_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment