from buffaloparse import parse, parses
from IPython.display import Image
from IPython.core.display import HTML
from programmachine import Register, Instructions
from buffalotoprogrammachine import interpret
> whoami
Chigozie Nri
Scientific Software Developer at Enthought
Bovine Esoteric Computational Syntactician
— Dmitri Borgmann
— William Rapaport (independently)
— Annie Senghas (independently)
HTML("<img src='https://images-na.ssl-images-amazon.com/images/I/31c3Zzep0xL._SX331_BO1,204,203,200_.jpg', width=200>")
grammar_string = """
S -> V | VP
NP -> NP NP V | A N | N
VP -> NP V NP | NP V
V -> 'buffalo'
N -> 'buffalo'
A -> 'Buffalo'
"""
parsed = parse('Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo')
for tree in parsed:
tree.pretty_print()
S | VP ___________|_____________________________ NP | | _____________|___________________ | | NP NP | | NP _____|_____ _____|_____ | | _____|_____ A N A N V V A N | | | | | | | | Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
# The Program Machine program we are aiming for
src = """
buffalo 2
Buffalo 0
JZ buffalo 4
DEC buffalo
INC Buffalo
JZ buffalo 0
"""
How do we represent this with only buffaloes?
Each sentence will represent one line
The first two lines are special, as they represent the initial state of the registers.
# The register "buffalo" contains the value 2:
Buffalo buffalo buffalo buffalo Buffalo buffalo buffalo.
xxxxxxxxxxxxxxx ------- xxxxxxx ------- xxxxxxx -------
variable variable variable
1s place 2s place 4s place
1 x 0 + 2 x 1 + 4 x 0 = 2
# The register "Buffalo" contains the value 0:
Buffalo buffalo buffalo buffalo.
xxxxxxxxxxxxxxx ------- xxxxxxx
variable
1s place
1 x 0 = 0
Notice: Little-endian. What is 4 x 0 doing at the end of value 2? Why are we bothering with fixed values?
After the initial state of the registers comes the instructions. Remember, there are 3 possible instructions: JZ, DEC and INC
# DEC buffalo
Buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo.
xxxxxxxxxxxxxxx ------- xxxxxxx ------- xxxxxxx ------- xxxxxxx
variable variable variable
DEC/INC DEC buffalo
# INC Buffalo
Buffalo buffalo buffalo buffalo Buffalo buffalo Buffalo buffalo.
xxxxxxxxxxxxxxx ------- xxxxxxx ------- xxxxxxx ------- xxxxxxx
variable variable variable
DEC/INC INC Buffalo
# JZ buffalo 0
Buffalo buffalo Buffalo buffalo buffalo buffalo buffalo buffalo.
xxxxxxxxxxxxxxx ------- xxxxxxx ------- xxxxxxx ------- xxxxxxx
variable variable variable
JZ buffalo 1 x 0
Will every possible buffaloscript instruction be valid English? Eh, probably.
I checked the first 10000, every one worked with a sufficient number of trailing buffaloes. At most, sentence 8191 needed 14 trailing buffaloes to parse.
program = "Buffalo buffalo buffalo buffalo Buffalo buffalo buffalo. 🐃Swamp buffalo enjoy wallowing in mudholes they make with their horns🐃 Buffalo buffalo buffalo buffalo. Buffalo buffalo Buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo Buffalo buffalo. Buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo. Buffalo buffalo buffalo buffalo Buffalo buffalo Buffalo buffalo buffalo buffalo 🐃It's true!🐃. Buffalo buffalo Buffalo buffalo buffalo buffalo buffalo buffalo."
Convert to Minsky Program Machine
src = interpret(program)
instructions = Instructions(src)
print(f'Initial Register Values\n-----------------------')
registers = instructions.registers
for key, val in zip(registers.keys(),
[str(register.value) for register in registers.values()]):
print(f'{key}: {val}')
print()
instructions_with_linenumbers = [f'{i+1}: {" ".join(instructions.instruction_list[i])}' for i in range(len((instructions.instruction_list)))]
instruction_print = '\n'.join(instructions_with_linenumbers)
print(f"""Instructions
------------
{instruction_print}
""")
print("Run program\n-----------")
instructions.interpret()
Initial Register Values ----------------------- buffalo: 2 Buffalo: 0 Instructions ------------ 1: JZ buffalo 4 2: DEC buffalo 3: INC Buffalo 4: JZ buffalo 0 Run program ----------- line 1: register buffalo is not empty, doing nothing line 2: decreasing register buffalo from 2 to 1 line 3: increasing register Buffalo from 0 to 1 line 4: register buffalo is not empty, doing nothing line number out of range, halting Final Register Values on Halt ----------------------------- buffalo: 1 Buffalo: 1