def hanoi(self, n, x, y, z): n = n.clone() x = x.clone() y = y.clone() z = z.clone() Driver.simulate_function_call('hanoi', 3, locals()) Result = None Driver.simulate_statement(5) Driver.simulate_statement(6) if n == Integer(1): Driver.simulate_statement(7) pascal.move(x, Integer(1), z) else: Driver.simulate_statement(10) _hanoi = self.hanoi(n - Integer(1), x, z, y) Driver.simulate_function_return() Driver.simulate_statement(10) _hanoi Driver.simulate_statement(11) pascal.move(x, n, z) Driver.simulate_statement(13) _hanoi = self.hanoi(n - Integer(1), y, x, z) Driver.simulate_function_return() Driver.simulate_statement(13) _hanoi Driver.simulate_statement(15) return Result
def hanoi(self, n, x, y, z): Driver.simulate_function_call("hanoi", 1, locals()) # BEGIN Driver.simulate_statement(3) Driver.simulate_statement(4) if n == Integer(1): Driver.simulate_statement(5) self.move(x, Integer(1), z) Driver.simulate_function_return() else: # ELSE BEGIN Driver.simulate_statement(6) Driver.simulate_statement(8) self.hanoi(n - Integer(1), x, z, y) Driver.simulate_function_return() Driver.simulate_statement(9) self.move(x, n, z) Driver.simulate_function_return() # 模拟函数调用 Driver.simulate_statement(11) self.hanoi(n - Integer(1), y, x, z) Driver.simulate_function_return() # 模拟函数返回 Driver.simulate_statement(13)
def setlength(var, newlength): if not issubclass(type(var), (Array, Pointer)): raise AlgorithmError(1, "Pointer", type(var).__name__) if issubclass(type(var), Pointer): pcls = var.pcls value = Array(cls=pcls) value.frame = sys._getframe(1) value.name = "<%s>" % var.name for i in range(newlength): obj = pcls.__new__(pcls) pcls.__init__(obj) value[Integer(i)] = obj value[Integer(i)].frame = sys._getframe(1) var.assign(value) value.new() elif issubclass(type(var), Array): pcls = var.pcls for i in range(newlength): obj = pcls.__new__(pcls) pcls.__init__(obj) var[Integer(i)] = obj var[Integer(i)].frame = sys._getframe(1) var.resize() else: raise AlgorithmError(1, "Pointer", type(var).__name__)
def ord(x): assert(issubclass(type(x), (Char, Enum, String, Integer))) v = x.value if isinstance(v, basestring): result = Integer(ord(v[0])) else: result = Integer(v) result.frame = sys._getframe(1) return result
def low(x): assert(issubclass(type(x), BaseType)) assert(not issubclass(type(x), (Pointer, Record))) v = x.value if isinstance(v, dict): result = Integer(min(*v.keys())) else: result = Integer(0) result.frame = sys._getframe(1) return result
def move(self, x, n, y): Driver.simulate_function_call("move", 15, locals()) i = Integer(3) Driver.simulate_statement(16) Driver.simulate_statement(17) i.assign(Integer(5)) Driver.simulate_statement(18)
def high(x): assert issubclass(type(x), BaseType), type(x) assert(not issubclass(type(x), (Pointer, Record))) v = x.value if isinstance(v,dict): if len(v) == 0: result = Integer(-1) else: result = Integer(max(*v.keys())) elif isinstance(v, basestring): result = Integer(len(v)) else: result = Integer(0) result.frame = sys._getframe(1) return result
def move_node(self, x, n, y): DRIVER.simulate_function_call('move_node', 21, sys._getframe()) x.frame = sys._getframe() n = n.clone() n.declare() y.frame = sys._getframe() result = None DRIVER.simulate_statement(22, sys._getframe()) DRIVER.simulate_statement(23, sys._getframe()) x[n - Integer(1)].assign(None) DRIVER.simulate_statement(24, sys._getframe()) y[n - Integer(1)].assign(n) DRIVER.simulate_statement(25, sys._getframe()) x.frame = sys._getframe(1) n.destroy() y.frame = sys._getframe(1) return result
def hanoi(self, n, x, y, z): DRIVER.simulate_function_call('hanoi', 10, sys._getframe()) n = n.clone() n.declare() x.frame = sys._getframe() y.frame = sys._getframe() z.frame = sys._getframe() result = None DRIVER.simulate_statement(11, sys._getframe()) DRIVER.simulate_statement(12, sys._getframe()) if n == Integer(1): DRIVER.simulate_statement(13, sys._getframe()) _move_node = self.move_node(x, Integer(1), z) DRIVER.simulate_function_return() DRIVER.simulate_statement(13, sys._getframe()) _move_node else: DRIVER.simulate_statement(15, sys._getframe()) _hanoi = self.hanoi(n - Integer(1), x, z, y) DRIVER.simulate_function_return() DRIVER.simulate_statement(15, sys._getframe()) _hanoi DRIVER.simulate_statement(16, sys._getframe()) _move_node = self.move_node(x, n, z) DRIVER.simulate_function_return() DRIVER.simulate_statement(16, sys._getframe()) _move_node DRIVER.simulate_statement(17, sys._getframe()) _hanoi = self.hanoi(n - Integer(1), y, x, z) DRIVER.simulate_function_return() DRIVER.simulate_statement(17, sys._getframe()) _hanoi DRIVER.simulate_statement(19, sys._getframe()) n.destroy() x.frame = sys._getframe(1) y.frame = sys._getframe(1) z.frame = sys._getframe(1) return result
def mergesort(self, r, r2, s, t): s.clone() t.clone() Driver.simulate_function_call('mergesort', 1, locals()) Result = None class Integer5(Array): def __init__(self, value={}): Array.__init__(self, value=value, cls=Integer) self.frame = sys._getframe(1) r1 = Integer5() Driver.simulate_declare_var(r1) Driver.simulate_statement(6) Driver.simulate_statement(7) pascal.setlength(r1, t + Integer(1)) Driver.simulate_statement(8) if s == t: Driver.simulate_statement(9) r1[s].assign(r[s]) else: Driver.simulate_statement(11) _mergesort = self.mergesort(r, r2, s, (s + t) / Integer(2)) Driver.simulate_function_return() Driver.simulate_statement(11) _mergesort Driver.simulate_statement(12) _mergesort = self.mergesort(r, r2, (s + t) / Integer(2) + Integer(1), t) Driver.simulate_function_return() Driver.simulate_statement(12) _mergesort Driver.simulate_statement(13) _merge = self.merge(r2, s, (s + t) / Integer(2), t, r1) Driver.simulate_function_return() Driver.simulate_statement(13) _merge Driver.simulate_statement(15) return Result
def qksort(self, r, s, t): DRIVER.simulate_function_call('qksort', 8, sys._getframe()) r.frame = sys._getframe() s = s.clone() s.declare() t = t.clone() t.declare() result = None k = Indexpointer() k.declare() DRIVER.simulate_statement(11, sys._getframe()) DRIVER.simulate_statement(12, sys._getframe()) k.assign(Pointer(r)) DRIVER.simulate_statement(13, sys._getframe()) if s < t: DRIVER.simulate_statement(15, sys._getframe()) _qkpass = self.qkpass(r, s, t, k) DRIVER.simulate_function_return() DRIVER.simulate_statement(15, sys._getframe()) _qkpass DRIVER.simulate_statement(16, sys._getframe()) _qksort = self.qksort(r, s, k - Integer(1)) DRIVER.simulate_function_return() DRIVER.simulate_statement(16, sys._getframe()) _qksort DRIVER.simulate_statement(17, sys._getframe()) _qksort = self.qksort(r, k + Integer(1), t) DRIVER.simulate_function_return() DRIVER.simulate_statement(17, sys._getframe()) _qksort DRIVER.simulate_statement(19, sys._getframe()) r.frame = sys._getframe(1) s.destroy() t.destroy() k.destroy() return result
def __init__(self, driver, configure): global Driver Driver = driver n = Integer() x = Char() y = Char() z = Char() n.build(configure['paras']['n']) x.build(configure['paras']['x']) y.build(configure['paras']['y']) z.build(configure['paras']['z']) global Paras Paras = {'n': n, 'x': x, 'y': y, 'z': z} Driver.vision_initialize(configure['options']) for (name, args) in configure['images'].iteritems(): Driver.vision_append_node(name, **args) # 添加盘子 for i in range(n.get()): Driver.append_node( "A", 'node', 'plate_%d' % (i + 1), title=str(i + 1), padx=(i + 1) * 5, tanchor='center', options={'fill': '#1E90FF'}, ) Driver.vision_enable_refresh() Driver.simulate_main_call("hanoi", 1, self.__paras)
def run(self): DRIVER.simulate_program_entry('mergesort', 54) DRIVER.simulate_statement(55, sys._getframe()) first.assign(Pointer(meglist)) DRIVER.simulate_statement(55, sys._getframe()) last.assign(Pointer(meglist)) DRIVER.simulate_statement(56, sys._getframe()) first.assign(pascal.low(meglist)) DRIVER.simulate_statement(56, sys._getframe()) last.assign(pascal.high(meglist)) DRIVER.simulate_statement(57, sys._getframe()) pascal.setlength(buflist, last - first + Integer(1)) DRIVER.simulate_statement(58, sys._getframe()) _mergesort = self.mergesort(meglist, buflist.get(), first, last) DRIVER.simulate_function_return() DRIVER.simulate_statement(58, sys._getframe()) _mergesort DRIVER.simulate_statement(59, sys._getframe())
def move(self, x, n, y): Driver.simulate_function_call("move", 15, locals()) i = Integer(3) Driver.simulate_declare_var(i.name, i) Driver.simulate_statement(16) Driver.simulate_statement(17) # print x, " ", n, " ", y _name = "plate_%s" % (Paras['n'] - n + Integer(1)) Driver.simulateVisionMove(str(_name), str(x), str(y)) i.assign(Integer(5)) Driver.simulate_statement(18)
def move(self, x1, x2): x1 = x1.clone() x2 = x2.clone() Driver.simulate_function_call('move', 17, locals()) Result = None v0 = Integer() Driver.simulate_declare_var(v0) v1 = Char() Driver.simulate_declare_var(v1) v1 = Char() Driver.simulate_declare_var(v1) v1 = Char() Driver.simulate_declare_var(v1) v4 = Integer() Driver.simulate_declare_var(v4) Driver.simulate_statement(22) Driver.simulate_statement(23) v0.assign(Integer(1)) v0.assign(Integer(1) - Integer(1)) _for_end = Integer(100) while True: Driver.simulate_statement(24) v0.assign(v0 + Integer(1)) if v0 > _for_end: break Driver.simulate_statement(26) v1.assign(xxx) Driver.simulate_statement(30) v2.assign(yyy) Driver.simulate_statement(29) while True: Driver.simulate_statement(31) if v2 == Integer(0): break
def merge(self, rs, s, m, n, rn): DRIVER.simulate_function_call('merge', 25, sys._getframe()) rs = rs.clone() rs.declare() s = s.clone() s.declare() m = m.clone() m.declare() n = n.clone() n.declare() rn.frame = sys._getframe() result = None i = Indexpointer() i.declare() j = Indexpointer() j.declare() k = Indexpointer() k.declare() ki = Integer() ki.declare() DRIVER.simulate_statement(29, sys._getframe()) DRIVER.simulate_statement(30, sys._getframe()) i.assign(Pointer(rs)) DRIVER.simulate_statement(30, sys._getframe()) j.assign(Pointer(rs)) DRIVER.simulate_statement(30, sys._getframe()) k.assign(Pointer(rs)) DRIVER.simulate_statement(31, sys._getframe()) i.assign(s) DRIVER.simulate_statement(31, sys._getframe()) j.assign(m + Integer(1)) DRIVER.simulate_statement(31, sys._getframe()) k.assign(s - Integer(1)) DRIVER.simulate_statement(32, sys._getframe()) while (i <= m) and (j <= n): DRIVER.simulate_statement(34, sys._getframe()) k.assign(k + Integer(1)) DRIVER.simulate_statement(35, sys._getframe()) if rs[i] <= rs[j]: DRIVER.simulate_statement(37, sys._getframe()) rn[k].assign(rs[i]) DRIVER.simulate_statement(38, sys._getframe()) i.assign(i + Integer(1)) else: DRIVER.simulate_statement(42, sys._getframe()) rn[k].assign(rs[j]) DRIVER.simulate_statement(43, sys._getframe()) j.assign(j + Integer(1)) DRIVER.simulate_statement(46, sys._getframe()) if i <= m: ki.assign(k + Integer(1) - Integer(1)) _for_end = n while True: DRIVER.simulate_statement(47, sys._getframe()) ki.assign(ki + Integer(1)) if ki > _for_end: break DRIVER.simulate_statement(48, sys._getframe()) rn[ki].assign(rs[i + ki - k - Integer(1)]) DRIVER.simulate_statement(49, sys._getframe()) if j <= n: ki.assign(k + Integer(1) - Integer(1)) _for_end = n while True: DRIVER.simulate_statement(50, sys._getframe()) ki.assign(ki + Integer(1)) if ki > _for_end: break DRIVER.simulate_statement(51, sys._getframe()) rn[ki].assign(rs[j + ki - k - Integer(1)]) DRIVER.simulate_statement(52, sys._getframe()) rs.destroy() s.destroy() m.destroy() n.destroy() rn.frame = sys._getframe(1) i.destroy() j.destroy() k.destroy() ki.destroy() return result
def mergesort(self, r, r1, s, t): DRIVER.simulate_function_call('mergesort', 8, sys._getframe()) r.frame = sys._getframe() r1.frame = sys._getframe() s = s.clone() s.declare() t = t.clone() t.declare() result = None r2 = Queue() r2.declare() m = Integer() m.declare() DRIVER.simulate_statement(12, sys._getframe()) DRIVER.simulate_statement(13, sys._getframe()) pascal.setlength(r2, t + Integer(1)) DRIVER.simulate_statement(14, sys._getframe()) if s == t: DRIVER.simulate_statement(15, sys._getframe()) r1[s].assign(r[s]) else: DRIVER.simulate_statement(17, sys._getframe()) m.assign(pascal.trunc((s + t) / Integer(2))) DRIVER.simulate_statement(18, sys._getframe()) _mergesort = self.mergesort(r, r2, s, m) DRIVER.simulate_function_return() DRIVER.simulate_statement(18, sys._getframe()) _mergesort DRIVER.simulate_statement(19, sys._getframe()) _mergesort = self.mergesort(r, r2, m + Integer(1), t) DRIVER.simulate_function_return() DRIVER.simulate_statement(19, sys._getframe()) _mergesort DRIVER.simulate_statement(20, sys._getframe()) _merge = self.merge(r2, s, m, t, r1) DRIVER.simulate_function_return() DRIVER.simulate_statement(20, sys._getframe()) _merge DRIVER.simulate_statement(22, sys._getframe()) pascal.dispose(r2) DRIVER.simulate_statement(23, sys._getframe()) r.frame = sys._getframe(1) r1.frame = sys._getframe(1) s.destroy() t.destroy() r2.destroy() m.destroy() return result
def length(var): assert(issubclass(type(var), BaseType)) assert(not issubclass(type(index), Pointer)) result = Integer(len(var.value)) result.frame = sys._getframe(1) return result
def merge(self, rs, s, m, n, rn): rs.clone() s.clone() m.clone() n.clone() Driver.simulate_function_call('merge', 17, locals()) Result = None i = Integer() Driver.simulate_declare_var(i) j = Integer() Driver.simulate_declare_var(j) k = Integer() Driver.simulate_declare_var(k) ki = Integer() Driver.simulate_declare_var(ki) Driver.simulate_statement(23) i.show() j.show() k.show() Driver.simulate_statement(25) i.assign(s) Driver.simulate_statement(25) j.assign(m + Integer(1)) Driver.simulate_statement(25) k.assign(s - Integer(1)) Driver.simulate_statement(1) while (i <= m) and (j <= n): Driver.simulate_statement(28) k.assign(k + Integer(1)) Driver.simulate_statement(29) if rs[i] <= rs[j]: Driver.simulate_statement(31) rn[k].assign(rs[i]) Driver.simulate_statement(32) i.assign(i + Integer(1)) else: Driver.simulate_statement(36) rn[k].assign(rs[j]) Driver.simulate_statement(37) j.assign(j + Integer(1)) Driver.simulate_statement(39) if i <= m: ki = k + Integer(1) - Integer(1) _for_end = n while True: Driver.simulate_statement(40) ki = ki + Integer(1) if ki > _for_end: break Driver.simulate_statement(41) rn[ki].assign(rs[i + ki - k - Integer(1)]) Driver.simulate_statement(43) if j <= n: ki = k + Integer(1) - Integer(1) _for_end = n while True: Driver.simulate_statement(44) ki = ki + Integer(1) if ki > _for_end: break Driver.simulate_statement(45) rn[ki].assign(rs[j + ki - k - Integer(2)]) i.hide() j.hide() k.hide() Driver.simulate_statement(48) return Result
def addr(var): result = Integer(id(var)) result.frame = sys._getframe(1) return result
def qkpass(self, r, s, t, i): DRIVER.simulate_function_call('qkpass', 21, sys._getframe()) r.frame = sys._getframe() s = s.clone() s.declare() t = t.clone() t.declare() i.frame = sys._getframe() result = None j = Integer() j.declare() x = Integer() x.declare() rp = Integer() rp.declare() DRIVER.simulate_statement(26, sys._getframe()) DRIVER.simulate_statement(27, sys._getframe()) i.assign(s) DRIVER.simulate_statement(28, sys._getframe()) j.assign(t) DRIVER.simulate_statement(29, sys._getframe()) rp.assign(r[s]) DRIVER.simulate_statement(30, sys._getframe()) x.assign(r[s]) DRIVER.simulate_statement(31, sys._getframe()) while i < j: DRIVER.simulate_statement(33, sys._getframe()) while i < j and r[j] >= x: DRIVER.simulate_statement(34, sys._getframe()) j.assign(j - Integer(1)) DRIVER.simulate_statement(35, sys._getframe()) r[i].assign(r[j]) DRIVER.simulate_statement(36, sys._getframe()) while i < j and r[i] <= x: DRIVER.simulate_statement(37, sys._getframe()) i.assign(i + Integer(1)) DRIVER.simulate_statement(38, sys._getframe()) r[j].assign(r[i]) DRIVER.simulate_statement(40, sys._getframe()) r[i].assign(rp) DRIVER.simulate_statement(41, sys._getframe()) r.frame = sys._getframe(1) s.destroy() t.destroy() i.frame = sys._getframe(1) j.destroy() x.destroy() rp.destroy() return result
def straightsort(self, r): DRIVER.simulate_function_call('straightsort', 5, sys._getframe()) r.frame = sys._getframe() result = None n = Integer() n.declare() i = Indexpointer() i.declare() DRIVER.simulate_statement(9, sys._getframe()) DRIVER.simulate_statement(10, sys._getframe()) n.assign(pascal.high(r)) DRIVER.simulate_statement(11, sys._getframe()) i.assign(Pointer(r)) i.assign(Integer(2) - Integer(1)) _for_end = n while True: DRIVER.simulate_statement(12, sys._getframe()) i.assign(i + Integer(1)) if i > _for_end: break DRIVER.simulate_statement(13, sys._getframe()) _straipass = self.straipass(r, i) DRIVER.simulate_function_return() DRIVER.simulate_statement(13, sys._getframe()) _straipass DRIVER.simulate_statement(14, sys._getframe()) r.frame = sys._getframe(1) n.destroy() i.destroy() return result
def pos(substr, s): result = Integer(s.value.find(substr.value) + 1) result.frame = sys._getframe(1) return result
def int(x): assert(issubclass(type(x), (Integer, Real))) result = Integer(math.floor(x)) result.frame = sys._getframe(1) return result
def write(f, *args): s = f.value for p in args: s = "{0} {1}".format(s, p.value) print s, def writeln(f, *args): write(f, *args) print if __name__ == '__main__': p1 = Pointer(Integer) print p1.name new(p1) p1.get().assign(Integer(123)) print p1.get() s = String(String("123")) p1.assign(s) p1.get().assign(String("456")) print p1.get() write("123") writeln("123456:", 6, 7) write("aaa") write("bbb") a = Array() setlength(a, 3) print a[0]
def straipass(self, r, i): DRIVER.simulate_function_call('straipass', 16, sys._getframe()) r.frame = sys._getframe() i = i.clone() i.declare() result = None x = Integer() x.declare() j = Indexpointer() j.declare() DRIVER.simulate_statement(20, sys._getframe()) DRIVER.simulate_statement(21, sys._getframe()) j.assign(Pointer(r)) DRIVER.simulate_statement(22, sys._getframe()) r[Integer(0)].assign(r[i]) DRIVER.simulate_statement(23, sys._getframe()) j.assign(i - Integer(1)) DRIVER.simulate_statement(24, sys._getframe()) x.assign(r[i]) DRIVER.simulate_statement(25, sys._getframe()) while x < r[j]: DRIVER.simulate_statement(27, sys._getframe()) r[j + Integer(1)].assign(r[j]) DRIVER.simulate_statement(28, sys._getframe()) j.assign(j - Integer(1)) DRIVER.simulate_statement(30, sys._getframe()) r[j + Integer(1)].assign(r[Integer(0)]) DRIVER.simulate_statement(31, sys._getframe()) r.frame = sys._getframe(1) i.destroy() x.destroy() j.destroy() return result
def trunc(x): result = Integer(math.modf(x.value)[1]) result.frame = sys._getframe(1) return result