[ create a new paste ] login | about

Link: http://codepad.org/O149HeF1    [ raw code | fork ]

Python, pasted on Dec 24:
from sys import argv, stdout

prog = open(argv[1], 'r')
cmds = prog.readlines()
prog.close()

colors = ['red', 'ylw', 'grn', 'cyn', 'blu', 'mgn']

hue = 4
dark = 1

stack = []
codels = 0

for cmd in cmds:
  terms = cmd.split()
  if len(terms) == 0: continue
  if terms[0] == 'add':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      stack.append(b+a)
    codels += 1
    hue = (hue+1)%6
  elif terms[0] == 'sub':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      stack.append(b-a)
    codels += 1
    hue = (hue+1)%6
    dark = (dark+1)%3
  elif terms[0] == 'mul':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      stack.append(b*a)
    codels += 1
    hue = (hue+1)%6
    dark = (dark+2)%3
  elif terms[0] == 'div':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      stack.append(b/a)
    codels += 1
    hue = (hue+2)%6
  elif terms[0] == 'mod':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      stack.append(b%a)
    codels += 1
    hue = (hue+2)%6
    dark = (dark+1)%3
  elif terms[0] == 'dup':
    if stack:
      stack.append(stack[-1])
    codels += 1
    hue = (hue+4)%6
  elif terms[0] == 'push':
    stack.append(int(terms[1]))
    codels += int(terms[1])
    dark = (dark+1)%3
  elif terms[0] == 'pop':
    if stack:
      stack.pop()
    codels += 1
    dark = (dark+2)%3
  elif terms[0] == 'putc':
    if stack:
      a = stack.pop()
    stdout.write(chr(a))
    codels += 1
    hue = (hue+5)%6
    dark = (dark+2)%3
  elif terms[0] == 'roll':
    if len(stack) > 1:
      a, b = stack.pop(), stack.pop()
      while a:
        t = stack.pop(-b)
        stack += [t]
        a -= 1
    codels += 1
    hue = (hue+4)%6
    dark = (dark+1)%3
  elif terms[0] == 'rot':
    if stack:
      stack.pop()
    codels += 1
    hue = (hue+3)%6
    dark = (dark+1)%3
  elif terms[0] == 'swit':
    if stack:
      stack.pop()
    codels += 1
    hue = (hue+3)%6
    dark = (dark+2)%3
  elif terms[0] == 'nop':
    codels += 1
  elif terms[0] == 'end':
    break
  else:
    continue
  print "\t", "%3d %-6s"%(codels, cmd.strip()), "\t", "%s%d"%(colors[hue], dark), stack


Create a new paste based on this one


Comments: