#!/usr/bin/python # TOS is first item in list = 0. class Manipulation: def __init__(self, name, delve, effect): self.delve = delve self.effect = effect self.name = name def change(self, stack): if len(stack) < self.delve or len(stack) == 0: return ( ) else: result = ( ) for to in range(0, len(self.effect)): x = self.effect[to] if x == 5: result += (5,) else: result += (stack[x-1],) for to in range(self.delve, len(stack)): result += (stack[to],) return result #Stack manipulation operators. copy1 = Manipulation("copy1", 1, (1,1)) drop1 = Manipulation("drop1", 1,( )) swap = Manipulation("swap", 2,(2,1)) copy2 = Manipulation("copy2", 2,(2,1,2)) tuck2 = Manipulation("tuck2", 2,(1,2,1)) drop2 = Manipulation("drop2", 2,(1,)) rotu3 = Manipulation("rotu3", 3,(2,3,1)) rotd3 = Manipulation("rotd3", 3,(3,1,2)) copy3 = Manipulation("copy3", 3,(3,1,2,3)) tuck3 = Manipulation("tuck3", 3,(1,2,3,1)) drop3 = Manipulation("drop3", 3,(1,2)) rotu4 = Manipulation("rotu4", 4,(2,3,4,1)) rotd4 = Manipulation("rotd4", 4,(4,1,2,3)) copy4 = Manipulation("copy4", 4,(4,1,2,3,4)) tuck4 = Manipulation("tuck4", 4,(1,2,3,4,1)) drop4 = Manipulation("drop4", 4,(1,2,3)) lit = Manipulation("%s", 0, (5,)) DEPTH = 4 TABLE_SIZE = 5 ** 3 manipulation = [copy1, drop1, swap, copy2, tuck2, drop2, rotu3, rotd3, copy3, tuck3, drop3, rotu4, copy4, tuck4, drop4] def preamble(): pass def postamble(): print "char*** lookup_table = { NULL, lookup_table_m3, lookup_table_m2, lookup_table_m1, lookup_table_0, lookup_table_1, lookup_table_2 };" def createTable(depth, width): if depth: table = [] for i in range(0, width): table.append(createTable(depth - 1, width)) return table else: return 0 def fillTable(table, sequence, ops, stack, has_lit): if ops: if sequence != "" and not has_lit: fillTable(table, sequence + "%s\\n", ops - 1, lit.change(stack), 1) index = 0 for op in manipulation: fillTable(table, sequence + op.name + "\\n", ops - 1, op.change(stack), has_lit) index += 1 if sequence == "" and not has_lit: fillTable(table, sequence + "%s\\n", ops - 1, lit.change(stack), 1) else: if len(stack) <= DEPTH + 2: if not table.has_key(stack): table[stack] = sequence def table_index(stack): result = 0 for i in range(0, len(stack)): result *= 5 result += stack[i] - 1 return result def printTable(table): output_m3 = [] output_m2 = [] output_m1 = [] output_0 = [] output_1 = [] output_2 = [] for i in xrange(0,5): output_m3.append("NULL,") for i in range(0,5): output_m2.append("NULL,") for j in range(0,5): output_m1.append("NULL,") for k in range(0,5): output_0.append("NULL,") for l in range(0,5): output_1.append("NULL,") for m in range(0,5): output_2.append("NULL,") for stack in table.keys(): if len(stack) == DEPTH-3: output_m3[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) elif len(stack) == DEPTH-2: if table_index(stack) >= 25: print table_index(stack), stack, table[stack] output_m2[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) elif len(stack) == DEPTH-1: output_m1[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) elif len(stack) == DEPTH: output_0[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) elif len(stack) == DEPTH+1: output_1[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) elif len(stack) == DEPTH+2: output_2[table_index(stack)] = "\"%s\", // %s" % (table[stack], str(stack)) print "char** lookup_table_m3 = {" for i in xrange(0, 5): print output_m3[i] print "};" print "char** lookup_table_m2 = {" for i in xrange(0, 25): print output_m2[i] print "};" print "char** lookup_table_m1 = {" for i in xrange(0, TABLE_SIZE): print output_m1[i] print "};" print "char** lookup_table_0 = {" for i in xrange(0, TABLE_SIZE * 5): print output_0[i] print "};" print "char** lookup_table_1 = {" for i in xrange(0, TABLE_SIZE * 25): print output_1[i] print "};" print "char** lookup_table_2 = {" for i in xrange(0, TABLE_SIZE * 125): print output_2[i] print "};" table = {} preamble() #ALU first index = 0 for n in range(1,6): fillTable(table, "", n, (1,2,3,4), 0) printTable(table) postamble()