If you thought the programs for recipe 578366 were too difficult to understand, you have just found an upgrade to the COW language: mnemonics humans can understand. This recipe makes a slight alteration to the interpreter so as to take the decoded instructions instead of the COW (moo) instructions.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | INS = ['moo', 'mOo', 'moO', 'mOO', 'Moo', 'MOo',
'MoO', 'MOO', 'OOO', 'MMM', 'OOM', 'oom']
HLP = dict((s, i) for i, s in enumerate(INS))
import sys
import msvcrt
################################################################################
class COD:
def __init__(self, code):
for ins in code:
assert 0 <= ins < len(INS)
self.cod = code
self.pos = 0
def get(self):
return self.cod[self.pos]
def inc(self):
self.pos += 1
if self.pos == len(self.cod):
raise SystemExit
def dec(self):
self.pos -= 1
assert self.pos >= 0
################################################################################
class MEM:
def __init__(self):
self.mem = {}
self.pos = 0
def get(self):
if self.pos in self.mem:
return self.mem[self.pos]
return 0
def set(self, value):
if value:
self.mem[self.pos] = value
elif self.pos in self.mem:
del self.mem[self.pos]
def inc(self):
self.pos += 1
def dec (self):
self.pos -= 1
################################################################################
def parse(string):
index = 0
code = []
ins = string[index:index+3]
while len(ins) == 3:
if ins in INS:
code.append(INS.index(ins))
index += 3
else:
index += 1
ins = string[index:index+3]
return tuple(code)
DECODE = ['END', 'P--', 'P++', 'EXE', 'I/O', 'M--',
'M++', 'LOP', 'M=0', 'REG', 'OUT', 'INP']
def decode(code):
for ins in code:
print INS[ins] + '\t' + DECODE[ins]
print
def engine(code):
global REG
REG = None
mem = MEM()
cod = COD(code)
ins = cod.get()
while True:
ins = run(mem, cod, ins)
def run(mem, cod, ins):
global REG
# INS - 0 - moo
if ins == 0:
cod.dec()
cod.dec()
# INIT
level = 1
temp = cod.get()
if temp == HLP['moo']:
level += 1
elif temp == HLP['MOO']:
level -= 1
while level:
# LOOP
cod.dec()
temp = cod.get()
if temp == HLP['moo']:
level += 1
elif temp == HLP['MOO']:
level -= 1
return HLP['MOO']
# INS - 1 - mOo
elif ins == 1:
mem.dec()
cod.inc()
return cod.get()
# INS - 2 - moO
elif ins == 2:
mem.inc()
cod.inc()
return cod.get()
# INS - 3 - mOO
elif ins == 3:
temp = mem.get()
assert temp != 3
if 0 <= temp < len(INS):
return run(mem, cod, temp)
else:
raise SystemExit
# INS - 4 - Moo
elif ins == 4:
temp = mem.get()
if temp:
sys.stdout.write(chr(temp & 0x7F))
else:
char = get_char()
mem.set(ord(char) & 0x7F)
cod.inc()
return cod.get()
# INS - 5 - MOo
elif ins == 5:
mem.set(mem.get() - 1)
cod.inc()
return cod.get()
# INS - 6 - MoO
elif ins == 6:
mem.set(mem.get() + 1)
cod.inc()
return cod.get()
# INS - 7 - MOO
elif ins == 7:
temp = mem.get()
if temp:
cod.inc()
return cod.get()
else:
cod.inc()
cod.inc()
# INIT
level = 1
temp = cod.get()
if temp == HLP['moo']:
level -= 1
elif temp == HLP['MOO']:
level += 1
while level:
# LOOP
cod.inc()
temp = cod.get()
if temp == HLP['moo']:
level -= 1
elif temp == HLP['MOO']:
level += 1
cod.inc()
return cod.get()
# INS - 8 - OOO
elif ins == 8:
mem.set(0)
cod.inc()
return cod.get()
# INS - 9 - MMM
elif ins == 9:
if REG is None:
REG = mem.get()
else:
mem.set(REG)
REG = None
cod.inc()
return cod.get()
# INS - 10 - OOM
elif ins == 10:
sys.stdout.write(str(mem.get()) + '\n')
cod.inc()
return cod.get()
# INS - 11 - oom
elif ins == 11:
mem.set(get_int())
cod.inc()
return cod.get()
# ERROR
else:
raise Exception
def get_char():
while msvcrt.kbhit():
msvcrt.getch()
func = False
char = msvcrt.getch()
if char in ('\x00', '\xE0'):
func = True
while func:
msvcrt.getch()
func = False
char = msvcrt.getch()
if char in ('\x00', '\xE0'):
func = True
return char.replace('\r', '\n')
def get_int():
while msvcrt.kbhit():
msvcrt.getch()
buff = ''
char = msvcrt.getch()
while char != '\r' or not buff:
if '0' <= char <= '9':
sys.stdout.write(char)
buff += char
char = msvcrt.getch()
sys.stdout.write('\n')
return int(buff)
################################################################################
def main():
path = raw_input('Run File: ') + '.cow'
code = parse(file(path).read())
decode(code)
engine(code)
################################################################################
if __name__ == '__main__':
INS, DECODE = DECODE, INS
try:
main()
except SystemExit:
raw_input()
except:
import traceback
raw_input(traceback.format_exc())
|
The following version of the interpreter also has some minimal debugging facilities built into it.
INS = ['moo', 'mOo', 'moO', 'mOO', 'Moo', 'MOo',
'MoO', 'MOO', 'OOO', 'MMM', 'OOM', 'oom',
'DBG']
HLP = dict((s, i) for i, s in enumerate(INS))
import sys
import msvcrt
################################################################################
class COD:
def __init__(self, code):
for ins in code:
assert 0 <= ins < len(INS)
self.cod = code
self.pos = 0
def get(self):
return self.cod[self.pos]
def inc(self):
self.pos += 1
if self.pos == len(self.cod):
raise SystemExit
def dec(self):
self.pos -= 1
assert self.pos >= 0
################################################################################
class MEM:
def __init__(self):
self.mem = {}
self.pos = 0
def get(self):
if self.pos in self.mem:
return self.mem[self.pos]
return 0
def set(self, value):
if value:
self.mem[self.pos] = value
elif self.pos in self.mem:
del self.mem[self.pos]
def inc(self):
self.pos += 1
def dec (self):
self.pos -= 1
################################################################################
def parse(string):
index = 0
code = []
ins = string[index:index+3]
while len(ins) == 3:
if ins in INS:
code.append(INS.index(ins))
index += 3
else:
index += 1
ins = string[index:index+3]
return tuple(code)
DECODE = ['END', 'P--', 'P++', 'EXE', 'I/O', 'M--',
'M++', 'LOP', 'M=0', 'REG', 'OUT', 'INP',
'DBG']
def decode(code):
for ins in code:
print INS[ins] + '\t' + DECODE[ins]
print
def engine(code):
global REG
REG = None
mem = MEM()
cod = COD(code)
ins = cod.get()
while True:
ins = run(mem, cod, ins)
def run(mem, cod, ins):
global REG
# INS - 0 - moo
if ins == 0:
cod.dec()
cod.dec()
# INIT
level = 1
temp = cod.get()
if temp == HLP['moo']:
level += 1
elif temp == HLP['MOO']:
level -= 1
while level:
# LOOP
cod.dec()
temp = cod.get()
if temp == HLP['moo']:
level += 1
elif temp == HLP['MOO']:
level -= 1
return HLP['MOO']
# INS - 1 - mOo
elif ins == 1:
mem.dec()
cod.inc()
return cod.get()
# INS - 2 - moO
elif ins == 2:
mem.inc()
cod.inc()
return cod.get()
# INS - 3 - mOO
elif ins == 3:
temp = mem.get()
assert temp != 3
if 0 <= temp < len(INS):
return run(mem, cod, temp)
else:
raise SystemExit
# INS - 4 - Moo
elif ins == 4:
temp = mem.get()
if temp:
sys.stdout.write(chr(temp & 0x7F))
else:
char = get_char()
mem.set(ord(char) & 0x7F)
cod.inc()
return cod.get()
# INS - 5 - MOo
elif ins == 5:
mem.set(mem.get() - 1)
cod.inc()
return cod.get()
# INS - 6 - MoO
elif ins == 6:
mem.set(mem.get() + 1)
cod.inc()
return cod.get()
# INS - 7 - MOO
elif ins == 7:
temp = mem.get()
if temp:
cod.inc()
return cod.get()
else:
cod.inc()
cod.inc()
# INIT
level = 1
temp = cod.get()
if temp == HLP['moo']:
level -= 1
elif temp == HLP['MOO']:
level += 1
while level:
# LOOP
cod.inc()
temp = cod.get()
if temp == HLP['moo']:
level -= 1
elif temp == HLP['MOO']:
level += 1
cod.inc()
return cod.get()
# INS - 8 - OOO
elif ins == 8:
mem.set(0)
cod.inc()
return cod.get()
# INS - 9 - MMM
elif ins == 9:
if REG is None:
REG = mem.get()
else:
mem.set(REG)
REG = None
cod.inc()
return cod.get()
# INS - 10 - OOM
elif ins == 10:
sys.stdout.write(str(mem.get()) + '\n')
cod.inc()
return cod.get()
# INS - 11 - oom
elif ins == 11:
mem.set(get_int())
cod.inc()
return cod.get()
# INS - 12 - DBG
elif ins == 12:
debug(mem)
cod.inc()
return cod.get()
# ERROR
else:
raise Exception
def get_char():
while msvcrt.kbhit():
msvcrt.getch()
func = False
char = msvcrt.getch()
if char in ('\x00', '\xE0'):
func = True
while func:
msvcrt.getch()
func = False
char = msvcrt.getch()
if char in ('\x00', '\xE0'):
func = True
return char.replace('\r', '\n')
def get_int():
while msvcrt.kbhit():
msvcrt.getch()
buff = ''
char = msvcrt.getch()
while char != '\r' or not buff:
if '0' <= char <= '9':
sys.stdout.write(char)
buff += char
char = msvcrt.getch()
sys.stdout.write('\n')
return int(buff)
def debug(mem):
cache = 'DEBUG\n=====\n[POS] = %s\n' % mem.pos
for index in range(min(mem.mem), max(mem.mem) + 1):
cache += '[%03i] = %i\n' % (index, mem.mem.get(index, 0))
sys.stdout.write(cache + '=====\n')
################################################################################
def main():
path = raw_input('Run File: ') + '.cow'
code = parse(file(path).read())
decode(code)
engine(code)
################################################################################
if __name__ == '__main__':
INS, DECODE = DECODE, INS
try:
main()
except SystemExit:
raw_input()
except:
import traceback
raw_input(traceback.format_exc())
Of course, you will want some programs that your new easy cow can run for you.
Adder
INP
P++
INP
LOP
M--
P--
M++
P++
END
P--
OUT
Conditional Test
M++M++ a = 2
REGP++REG b = a
P++M=0
M++M++M++ c = 3
LOP
M--P-- c = 0
M--P++ b -= c
END
P--LOP if b: # if a != 3:
P--OUT print a
P--M--OUTP++ print -1
P++M=0
LOPEND else:
[[ ERROR - ELSE cannot be executed ]]
P--OUT print a
P--M--M--OUTP++ print -2
P++M=0LOP
ENDENDP-- end_if
OUT print a
P--M=0M--M--M--OUT print -3
Hello, World!
# This is the string.
H = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
e = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
l = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
l = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
o = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
, = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
= M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
W = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
o = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
r = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
l = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
d = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++P++
! = M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++M++
# Rewind the pointer.
P--P--P--P--P--P--P--P--P--P--P--P--
# Print the string.
LOP
I/O
P++
END
Multiplier
INP a = get_num()
P++
INP b = get_num()
P--
LOP while a:
M-- a -= 1
P++
REG temp = b
LOP while b: #
M-- b -= 1 #
P++ #
M++ c += 1 #
P-- #
END # c += b
REG b = temp
P--
END
P++
P++
OUT print c