2017-02-23 11 views
-3

呼び出すことはできませんこれは私のコードです:はTypeError: 'strの' オブジェクトが

class Parser(object): 

    def __init__(self, inputFile): # initalizer/constructor 
     #open input file and gets ready to parse it 
     f = open(inputFile, "r") 
     self.commands = list(f) 
     f.close() 
     print(self.commands) 
     self.currentCommand = 0 
     self.index = 0 

    def hasMoreCommands(self): 
     #are there any more commands in the input 
     #returns boolean 
     if (self.commands[self.currentCommand][self.index] == "\\") and (self.commands[self.currentCommand][self.index+1] == "n"): # checks for "/n", alluding that the command has ended and we can advance to the next command 
      return True 
     else: 
      return False 

    def advance(self): 
     #reads next command and makes it current command 
     #called only if hasMoreCommands is true 
     if self.hasMoreCommands(): 
      self.currentCommand += 1 

    def commandType(self): 
     #returns type of current command A_COMMAND, C_COMMAND, L_COMMAND 
     #C A or L(psuedo command for (XxX)) 
     #dest=comp; jmp, @,() 
     self.type = self.commands[self.currentCommand][0] 
     if self.type == "@": 
      return "A_COMMAND" 
     elif self.type == "(": 
      return "L_COMMAND" 
     else: 
      return "C_COMMAND" 

    def dest(self): 
     #returns dest mnemoic of current C instruction - 8 Poss 
     #called when command type is C 
     #return string 
     if (self.commandType() == "C_COMMAND") and ("=" in self.commands[self.currentCommand]): 
       return self.commands[self.currentCommand][0:(self.commands[self.currentCommand].index("="))] 

def main(inputFile): 
    d = Parser(inputFile) 
    d.commandType = "C_COMMAND" 
    d.commands = ["D=A+2\\n", "AMD=A+5\\n"] 
    d.currentCommand = 0 
    print(d.dest()) 

main("/Users/user1/Desktop/filelocation/projects/06/add/add.asm") 

問題のファイル:

// This file is part of www.nand2tetris.org 
// and the book "The Elements of Computing Systems" 
// by Nisan and Schocken, MIT Press. 
// File name: projects/06/add/Add.asm 

// Computes R0 = 2 + 3 

@2 
D=A 
@3 
D=D+A 
@0 
M=D 

エラーが返さ:

['// This file is part of www.nand2tetris.org\n', '// and the book "The Elements of Computing Systems"\n', '// by Nisan and Schocken, MIT Press.\n', '// File name: projects/06/add/Add.asm\n', '\n', '// Computes R0 = 2 + 3\n', '\n', '@2\n', 'D=A\n', '@3\n', 'D=D+A\n', '@0\n', 'M=D\n'] 

Traceback (most recent call last): 
    File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 104, in <module> 
    main("/Users/user1Desktop/filelocation/projects/06/add/add.asm") 
    File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 99, in main 
    print(d.dest()) 
    File "/Users/user1/Desktop/Python/filelocation/assembler.py", line 50, in dest 
    if (self.commandType() == "C_COMMAND") and ("=" in self.commands[self.currentCommand]): 
TypeError: 'str' object is not callable 
[Finished in 0.1s with exit code 1] 

私がしようとしていましたテストの宛先。
これは、NAND 2テトリスの一部です/あなたのメインで

+0

どのようにあなたがそれを呼び出している、"C_COMMAND()"によって "と呼ばれる" ことができないということでしょうか? –

+0

'd.commandType =" C_COMMAND "' ...あなたは関数を上書きしました。 –

答えて

1

第6章ではコンピューティングシステムのカリキュラムの要素、あなたはそのためstrとしている方法d.commandType = "C_COMMAND"def commandType(self)を、交換することは方法のようにコールすることはできません。 。あなたのクラスのために

+0

ありがとうございました!修正しました。 – apklip

+0

これで問題が解決すれば、その答えを受け入れることは素晴らしいことです。 – languitar

+0

私は8分待つ必要があると言います。私はalloted期間の後にそうするでしょう。 – apklip

0

....

class Parser(object): 

    def __init__(self, inputFile): # initalizer/constructor 
     #open input file and gets ready to parse it 
     f = open(inputFile, "r") 
     self.commands = list(f) 
     f.close() 

あなたは既にファイルから

self.commandsを設定し、注意してください:indexcurrentCommand

まったく同じ目的を果たすように見えます関数はそのリストを使用します。

def commandType(self): 
    #returns type of current command A_COMMAND, C_COMMAND, L_COMMAND 
    #C A or L(psuedo command for (XxX)) 
    #dest=comp; jmp, @,() 
    self.type = self.commands[self.currentCommand][0] 

したがって、あなたはあなたが唯一の入力ファイルが正しいと仮定すると、このmain必要があるこれらの行

def main(inputFile): 
    d = Parser(inputFile) 
    # d.commandType = "C_COMMAND" 
    # d.commands = ["D=A+2\\n", "AMD=A+5\\n"] 
    # d.currentCommand = 0 

を必要としません。

def main(inputFile): 
    d = Parser(inputFile) 
    print(d.dest()) 

あなたのエラーがd.commandType = "C_COMMAND"が(すなわちd.commandType()

関連する問題