Brainfuck解析器(Python)

來源:互聯網
上載者:User

標籤:char   ++   val   span   logs   opcode   nbsp   pre   pytho   

global csglobal ipglobal ss#global spglobal dsglobal bpglobal tabglobal outcs=‘++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.‘ip = 0ss = []#sp = 0ds = []bp = 0tab = 0out = []def tab_():    i = tab    stab = ‘‘    while i > 0:        stab += ‘\t‘        i -= 1    return stabdef push(var):    global ss    ss.append(var)def pop():    global ss    return ss.pop()def op_inc_val():    global ip    count = 0    while True:        op = cs[ip]                    ip = ip + 1        if op == ‘+‘:            count = count + 1        else:            break    l = len(ds)    if l <= bp:        ds.append(0)    old = ds[bp]    old += count    ds[bp] = old    print tab_()+‘ds[%d] += %d                  (%d)‘%(bp, count, old)def op_dec_val():    global ip        count = 0    while True:        op = cs[ip]                    ip = ip + 1        if op == ‘-‘:            count = count + 1        else:            break    old = ds[bp]    old -= count    ds[bp] = old    print tab_()+‘ds[%d] -= %d                  (%d)‘%(bp, count, old)def op_inc_dp():    global bp    bp = bp + 1def op_dec_dp():    global bp    bp = bp - 1def op_jmp_fwd():    global tab    global ip    print tab_()+‘while ds[%d]=%d:‘%(bp, ds[bp])    tab=tab + 1    if ds[bp] != 0:        curip = ip - 1        push(curip)    else:        c = 1;         while c > 0:            op = cs[ip]            if op == ‘[‘:                c += 1            elif op == ‘]‘:                c -= 1            ip += 1def op_jmp_bck():    global tab    global ip    tab = tab - 1    if ds[bp] != 0:        ip = pop()def op_out():    print tab_()+‘putchar(ds[%d])                  (%d)‘%(bp, ds[bp])    out.append(ds[bp])def op_in():    print tab_()+‘getchar‘end = len(cs)while ip < end:    op = cs[ip]    ip = ip + 1    if op == ‘+‘:        ip = ip - 1        op_inc_val()        ip = ip - 1    elif op == ‘-‘:        ip = ip - 1        op_dec_val()        ip = ip - 1    elif op == ‘>‘:        op_inc_dp()    elif op == ‘<‘:        op_dec_dp()    elif op == ‘[‘:        op_jmp_fwd()    elif op == ‘]‘:        op_jmp_bck()    elif op == ‘.‘:        op_out()    elif op == ‘,‘:        op_in()    else:        print ‘invalid opcode‘        break    print outstr = ‘‘for c in out:    str += ‘%c‘%(c)print str

 

Brainfuck解析器(Python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.