2013-09-29 41 views
-3

嘿傢伙我一直在試圖運行這段代碼,但我不斷收到這個錯誤,說「期望一個縮進塊」 我是新來的python,我不明白是什麼問題是。根據我的知識,我已經正確縮進了代碼。預計在Python中的縮進塊

import sys, string 
wordsize = 24 # everything is a word 
numregbits = 3 # actually +1, msb is indirect bit 
opcodesize = 5 
memloadsize = 1024 # change this for larger programs 
numregs = 2**numregbits 
opcposition = wordsize - (opcodesize + 1) # shift value to position opcode 
reg1position = opcposition - (numregbits + 1) # first register position 
reg2position = reg1position - (numregbits + 1) 
memaddrimmedposition = reg2position # mem address or immediate same place as reg2 
startexecptr ==0 
def regval (rstr): # help with reg or indirect addressing 
    if rstr.isdigit(): 
     return (int(rstr)) 
    elif rstr[0] == '*': 
     return int (rstr[1:] + (1<<numregbits)) 
    else: 
     return 0 # should not happen 
     mem = [0] * memloadsize # this is the memory load executable 
# instruction mnemonic, type: (1 reg, 2 reg, reg+addr, immed, pseudoop), opcode 
     opcodes = {'add': (2, 1),'sub': (2, 2), 
      'dec': (1, 3), 'inc': (1, 4), 
      'ld': (3, 7), 'st': (3, 8), 'ldi': (3, 9), 
      'bnz': (3, 12), 'brl': (3, 13), 
      'ret': (1, 14), 
      'int': (3, 16), 'sys': (3, 16), 
      'dw': (4, 0), 'go':(3, 0), 'end': (0, 0) }  
     curaddr = 0 # start assembling to location 0 
#for line in open(sys.argv[1], 'r').readlines(): # command line 
     infile = open("in.asm", 'r') 
# Build Symbol Table 
     symboltable = {} 
     for line in infile.readlines(): # read our asm code 
       tokens = string.split(string.lower(line)) # tokens on each line 
       firsttoken = tokens[0] 
    if firsttoken.isdigit(): # if line starts with an address 
     curaddr = int(tokens[0]) # assemble to here 
     tokens = tokens[1:] 
    if firsttoken == ';': # skip comments 
    if firsttoken == 'go': # start execution here 
     startexecptr = (int(tokens[ 1 ]) & ((2**wordsize)-1)) # data 
    if firsttoken[0] == '.': 
     symboltable[firsttoken] = curaddr 
     curaddr = curaddr + 1 
     print symboltable 
     infile.close() 
infile = open("in.asm", 'r') 
    for line in infile.readlines(): 
     tokens = string.split(string.lower(line)) # tokens on each line 
     firsttoken = tokens[0] 
    if firsttoken.isdigit(): # if line starts with an address 
     curaddr = int(tokens[0]) # assemble to here 
     tokens = tokens[1:] 
    if firsttoken == 'go': # start execution here 
     startexecptr = (int(tokens[ 1 ]) & ((2**wordsize)-1)) 
    if firsttoken[0] == '.': 
     symaddr = symboltable[firsttoken] 
     tokens = tokens[1:] 
     memdata = 0 
     instype = opcodes[ tokens[0] ] [0] 
     memdata = (opcodes[ tokens[0] ] [1]) << opcposition 
    if instype == 4: # dw type 
     memdata = (int(tokens[ 1 ]) & ((2**wordsize)-1)) 
    elif instype == 0: # end type 
     memdata = memdata 
    elif instype == 1: # dec,inc type, one reg 
     memdata = memdata + (regval(tokens[1]) << reg1position) 
    elif instype == 2: # add, sub type, two regs 
     memdata = memdata + (regval(tokens[1]) << reg1position) + (regval(tokens[2]) << reg2position) 
    elif instype == 3: # ld,st type 
     token2 = tokens[2] 
    if token2.isdigit(): 
     memaddr = int(tokens[2]) 
    else: 
     memaddr = symboltable[ token2 ] 
     memdata = memdata + (regval(tokens[1]) << reg1position) + memaddr 
     mem[ curaddr ] = memdata # memory image at the current location 
     curaddr = curaddr + 1 
     outfile = open("a.out", 'w') # done, write it out 
     outfile.write('go ' + '%d' % startexecptr) # start execution here 
     outfile.write("\n") 
    for i in range(memloadsize): # write memory image 
     outfile.write(hex(mem[ i ]) + " " + '%d'%i) 
     outfile.write("\n") 
     outfile.close() 
+0

請發佈完整的追溯。我們看不到你的神奇 – TerryA

+0

你的意思是我應該發佈與此相關的其他代碼呢? – Nitz

+0

Haidro意味着* full *錯誤信息,這可能表明事情開始出錯的行號。 – Edward

回答

1

你有兩個if行縮進到同一級別:

if firsttoken == ';': # skip comments 
if firsttoken == 'go': # start execution here 

您的意思是縮進第一後的一切嗎?

如果您自己的編輯器將兩行顯示爲不同的縮進級別,則需要停止混合製表符和空格; Python正在翻譯與您的編輯器不同的標籤。

運行你的腳本python -tt <scriptname>讓Python告訴你你在哪裏濫用標籤。

+0

我想他想'如果firsttoken ==';':''通過' –

+0

我會說'continue'而不是'pass'在這種情況下。 –

+0

但坦率地說,在這個問題上的縮進絕對是一團糟;程序流程與發佈無關。 –