def dump_dict(): """display named items""" html = "<table>" l = '' i = 0 for w in parable.dictionary_names(): wx = '<pre>' + w.replace('<', '<').replace('>', '>') + '</pre>' slice = parable.dictionary_slices()[i] cell, type = parable.fetch(slice, 0) size = parable.memory_size[slice] if type == parable.TYPE_REMARK: comment = parable.slice_to_string(cell) l = l + '<tr>' l = l + '<td width="10%">' + str(slice) + '</td>' + '<td>' + wx cell, type = parable.fetch(slice, size) if type == parable.TYPE_REMARK: l = l + '<br>' + parable.slice_to_string(cell) l = l + '</td>' l = l + '<td width="20%"><pre>' + comment + '</pre></td></tr>\n' else: l = l + '<tr><td width-"10%">' + str(slice) + '</td>' l = l + '<td colspan="3">' + wx + '</td></tr>\n' i = i + 1 html = html + l html = html + "</table>" return html
def dump_stack(): """display the stack""" i = 0 while i < len(parable.stack): tos = parable.stack_value_for(i) type = parable.stack_type_for(i) sys.stdout.write("\t" + str(i)) if type == parable.TYPE_NUMBER: display_item('#', tos) elif type == parable.TYPE_CHARACTER: display_item('$', chr(tos)) elif type == parable.TYPE_STRING: display_item('\'', parable.slice_to_string(tos) + '\'') elif type == parable.TYPE_POINTER: display_item('&', tos) elif type == parable.TYPE_REMARK: display_item('"', parable.slice_to_string(tos) + '"') elif type == parable.TYPE_FLAG: if tos == -1: display_item("", "true") elif tos == 0: display_item("", "false") else: display_item("", "malformed flag") else: display_item("", "unmatched type on the stack") sys.stdout.write("\n") i += 1
def display_stack(): """display the stack contents. returns the number of lines rendered""" i = 0 l = 1 while i < len(parable.stack): tos = parable.stack_value_for(i) type = parable.stack_type_for(i) # display the stack item number if i == len(parable.stack) - 1: write("TOS\t" + str(i), COLOR_STACK_LINE) else: write("\t" + str(i), COLOR_STACK_LINE) # display the stack item if type == parable.TYPE_NUMBER: write("\t#" + str(tos), COLOR_STACK_N) elif type == parable.TYPE_CHARACTER: write("\t$" + str(chr(tos)), COLOR_STACK_C) elif type == parable.TYPE_STRING: write("\t'" + parable.slice_to_string(tos) + "'", COLOR_STACK_S) write("\n\t\tstored at: " + str(tos), 'normal') l += 1 elif type == parable.TYPE_POINTER: write("\t&" + str(tos), COLOR_STACK_F) if parable.pointer_to_name(tos) != "": write("\n\t\tpointer to: ", 'normal') write(parable.pointer_to_name(tos), 'normal') l += 1 elif type == parable.TYPE_FLAG: if tos == -1: write("\ttrue", COLOR_STACK_FLAG) elif tos == 0: write("\tfalse", COLOR_STACK_FLAG) else: write("\tmalformed flag", COLOR_STACK_FLAG) elif type == parable.TYPE_BYTECODE: write("\t`" + str(tos), COLOR_STACK_BYTECODE) elif type == parable.TYPE_REMARK: write("\t\"" + parable.slice_to_string(tos) + "\"", COLOR_STACK_COMMENT) write("\n\t\tstored at: " + str(tos), 'normal') l += 1 elif type == parable.TYPE_FUNCALL: write("\t|" + str(tos), COLOR_STACK_FUN_CALL) else: write("\tUNKNOWN\t" + str(tos) + "\t" + str(type), COLOR_ERROR) sys.stdout.write("\n") # increase "l" so we know how many lines have been displayed so far i += 1 l += 1 return l
def dump_stack(): """display the stack""" i = 0 s = "" depth = len(parable.stack) html = "<table>" while i < depth: tos = parable.stack_value_for(i) type = parable.stack_type_for(i) s = "" if type == parable.TYPE_NUMBER: s = "#" + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_CHARACTER: s = "$" + str(chr(tos)) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_STRING: s = "'" + parable.slice_to_string(tos) + "'" s = s + "<br>Store at: " + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_POINTER: s = "&" + str(tos) if parable.pointer_to_name(tos) != "": s = s + "<br>Pointer to: " + parable.pointer_to_name(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_FLAG: if tos == -1: s = "true" elif tos == 0: s = "false" else: s = "malformed flag" s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_BYTECODE: s = "`" + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_REMARK: s = "\"" + parable.slice_to_string(tos) + "\"" s = s + "<br>Stored at: " + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_FUNCALL: s = "|" + str(tos) if parable.pointer_to_name(tos) != "": s = s + "<br>Pointer to: " + parable.pointer_to_name(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) else: s = stack_item( i, "unmatched type on stack!<br>(" + str(tos) + "," + str(type) + ")") s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) html = html + stack_item(i, s) i += 1 html = html + "</table>" return html
def dump_stack(): """display the stack""" i = 0 s = "" depth = len(parable.stack) html = "<table>" while i < depth: tos = parable.stack_value_for(i) type = parable.stack_type_for(i) s = "" if type == parable.TYPE_NUMBER: s = "#" + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_CHARACTER: s = "$" + str(chr(tos)) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_STRING: s = "'" + parable.slice_to_string(tos) + "'" s = s + "<br>Store at: " + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_POINTER: s = "&" + str(tos) if parable.pointer_to_name(tos) != "": s = s + "<br>Pointer to: " + parable.pointer_to_name(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_FLAG: if tos == -1: s = "true" elif tos == 0: s = "false" else: s = "malformed flag" s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_BYTECODE: s = "`" + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_REMARK: s = "\"" + parable.slice_to_string(tos) + "\"" s = s + "<br>Stored at: " + str(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) elif type == parable.TYPE_FUNCALL: s = "|" + str(tos) if parable.pointer_to_name(tos) != "": s = s + "<br>Pointer to: " + parable.pointer_to_name(tos) s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) else: s = stack_item(i, "unmatched type on stack!<br>(" + str(tos) + "," + str(type) + ")") s = s + "<br>Type: {0}, Raw Value: {1}".format(type, tos) html = html + stack_item(i, s) i += 1 html = html + "</table>" return html
def opcodes(slice, offset, opcode): if opcode == 9000: dump_stack() elif opcode == 9001: exit() elif opcode == 9002: dump_dict() elif opcode == 9003: name = parable.slice_to_string(parable.stack_pop()) if os.path.exists(name): lines = parable.condense_lines(open(name).readlines()) for l in lines: slice = parable.request_slice() parable.interpret(parable.compile(l, slice), opcodes) return offset
def display_value(): i = len(parable.stack) - 1 if parable.types[i] == parable.TYPE_NUMBER: sys.stdout.write(str(parable.stack[i])) elif parable.types[i] == parable.TYPE_CHARACTER: sys.stdout.write(str(chr(parable.stack[i]))) elif parable.types[i] == parable.TYPE_STRING: sys.stdout.write(parable.slice_to_string(parable.stack[i])) elif parable.types[i] == parable.TYPE_POINTER: sys.stdout.write('&' + str(parable.stack[i])) elif parable.types[i] == parable.TYPE_FLAG: if parable.stack[i] == -1: sys.stdout.write("true") elif parable.stack[i] == 0: sys.stdout.write("false") else: sys.stdout.write("malformed flag")
def opcode_include_file(): import os name = parable.slice_to_string(parable.stack_pop()) if os.path.exists(name): source = open(name).readlines() parable.parse_bootstrap(source)
def opcodes(slice, offset, opcode): if opcode == 9000: display_value() parable.stack_pop() elif opcode == 9010: dump_stack() elif opcode == 9020: exit() elif opcode == 9030: dump_dict() elif opcode == 9040: s = parable.request_slice() i = 0 for word in parable.dictionary_names(): value = parable.string_to_slice(word) parable.store(value, s, i, parable.TYPE_STRING) i = i + 1 parable.stack_push(s, parable.TYPE_POINTER) elif opcode == 3000: slot = 0 i = 1 while i < 8: if files[i] == 0: slot = i i = i + 1 mode = parable.slice_to_string(parable.stack_pop()) name = parable.slice_to_string(parable.stack_pop()) if slot != 0: files[int(slot)] = open(name, mode) stack_push(slot, TYPE_NUMBER) elif opcode == 3001: slot = int(parable.stack_pop()) files[slot].close() files[slot] = 0 elif opcode == 3002: slot = int(parable.stack_pop()) stack_push(ord(files[slot].read(1)), TYPE_NUMBER) elif opcode == 3003: slot = int(parable.stack_pop()) files[slot].write(unichr(int(parable.stack_pop()))) elif opcode == 3004: slot = int(parable.stack_pop()) parable.stack_push(files[slot].tell(), TYPE_NUMBER) elif opcode == 3005: slot = int(parable.stack_pop()) pos = int(parable.stack_pop()) parable.stack_push(files[slot].seek(pos, 0), TYPE_NUMBER) elif opcode == 3006: slot = int(parable.stack_pop()) at = files[slot].tell() files[slot].seek(0, 2) # SEEK_END parable.stack_push(files[slot].tell(), TYPE_NUMBER) files[slot].seek(at, 0) # SEEK_SET elif opcode == 3007: name = parable.slice_to_string(parable.stack_pop()) if os.path.exists(name): os.remove(name) elif opcode == 3008: name = parable.slice_to_string(parable.stack_pop()) if os.path.exists(name): stack_push(-1, TYPE_FLAG) else: stack_push(0, TYPE_FLAG) elif opcode == 4000: name = parable.slice_to_string(parable.stack_pop()) print name if os.path.exists(name): lines = parable.condense_lines(open(name).readlines()) for l in lines: s = rewrite(l) print s slice = parable.request_slice() parable.interpret(parable.compile(s, slice), opcodes) return offset