#!/usr/bin/python from string import * from random import * from sys import * ops = [] stack = [] pc = 0 scrabble_scores = { 'A': 1, 'B': 3, 'C': 3, 'D': 2, 'E': 1, 'F': 4, 'G': 2, 'H': 4, 'I': 1, 'J': 8, 'K': 5, 'L': 1, 'M': 3, 'N': 1, 'O': 1, 'P': 3, 'Q': 10, 'R': 1, 'S': 1, 'T': 1, 'U': 1, 'V': 4, 'W': 4, 'X': 8, 'Y': 4, 'Z': 10 } ### Beatnik operation functions def beat_push (): # print "OP: beat_push" global pc pc = pc + 1 stack.append(ops[pc]) def beat_pop (): # print "OP: beat_pop" stack.pop() def beat_add (): # print "OP: beat_add" x = stack.pop() y = stack.pop() stack.append(x+y) def beat_input (): # print "OP: beat_input" c = stdin.read(1) # c = get_keypress() stack.append(ord(c)) def beat_output (): # print "OP: beat_output" c = stack.pop() stdout.write(chr(c)) def beat_subtract (): # print "OP: beat_subtract" x = stack.pop() y = stack.pop() stack.append(y-x) def beat_swap (): # print "OP: beat_swap" x = stack.pop() y = stack.pop() stack.append(x) stack.append(y) def beat_duplicate (): # print "OP: beat_duplicate" x = stack.pop() stack.append(x) stack.append(x) def beat_jump_forward_if_zero (): # print "OP: beat_jump_forward_if_zero" x = stack.pop() if x == 0: global pc j = ops[pc+1] pc = pc + j + 1 def beat_jump_forward_if_not_zero (): # print "OP: beat_jump_forward_if_not_zero" x = stack.pop() if x != 0: global pc j = ops[pc+1] pc = pc + j + 1 def beat_jump_back_if_zero (): # print "OP: beat_jump_back_if_zero" x = stack.pop() if x == 0: global pc j = ops[pc+1] pc = pc - j def beat_jump_back_if_not_zero (): # print "OP: beat_jump_back_if_not_zero" x = stack.pop() if x == 0: global pc j = ops[pc+1] pc = pc - j def beat_halt (): # print "OP: beat_halt" global pc pc = len(ops) beatnik_opcode = { 5: beat_push, 6: beat_pop, 7: beat_add, 8: beat_input, 9: beat_output, 10: beat_subtract, 11: beat_swap, 12: beat_duplicate, 13: beat_jump_forward_if_zero, 14: beat_jump_forward_if_not_zero, 15: beat_jump_back_if_zero, 16: beat_jump_back_if_not_zero, 17: beat_halt } ### Beatnik helper functions def get_score (word): """Get Scrabble(tm) word score of a word.""" score = 0 for letter in word: key = upper(letter) if scrabble_scores.has_key(key): score = score + scrabble_scores[key] return score def parse_beatnik (howl): """Calculate operation list values for a Beatnik program.""" for word in split(howl): ops.append(get_score(word)) def run_beatnik (): """Run Beatnik program loaded into current oplist.""" global pc while pc < len(ops): op = ops[pc] if op < 5: mock() elif op > 23: snap() else: if beatnik_opcode.has_key(op): try: beatnik_opcode[op]() # print "Stack:",stack except: print "***ERR*** Beatnik exiting at PC:", pc import sys print "Error:",sys.exc_value print "Ops:", ops print "Stack:", stack break pc = pc + 1 def mock (): hep = random() if hep < .2: stderr.write("real square, man.\n") elif hep < .3: stderr.write("you're bringin' me down, daddy.\n") elif hep < .4: stderr.write("gimme a break, hippy.\n") def snap (): if random() < .4: stderr.write("*snap*snap*snap*snap*\n") #def get_keypress (): # import termios, sys, os # fd = sys.stdin.fileno() # old = termios.tcgetattr(fd) # new = termios.tcgetattr(fd) # new[3] = new[3] & ~termios.ICANON & ~termios.ECHO # new[6][termios.VMIN] = 1 # new[6][termios.VTIME] = 0 # termios.tcsetattr(fd, termios.TCSANOW, new) # s = '' # We'll save the characters typed and add them to the pool. # try: # while 1: # c = os.read(fd, 1) # print "Got character", `c` # s = s+c # finally: # termios.tcsetattr(fd, termios.TCSAFLUSH, old) #### Make it go, man, go! try: parse_beatnik(open(argv[1]).read()) except: parse_beatnik(stdin.read()) run_beatnik()