2017-02-23 14 views
-3

Dies ist mein Code:Typeerror: 'str' Objekt ist nicht aufrufbar

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") 

Die betreffende Datei:

// 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 

Fehler zurückgegeben:

['// 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] 

ich versuchte zu Test Ziel Diese
ist ein Teil des Nand 2 Tetris/Elements of Computing Systems Lehrplan in Kapitel 6.

+0

Wie nennst du es? –

+0

'd.commandType =" C_COMMAND "' ... Sie haben die Funktion überschrieben. –

Antwort

1

In Ihrem Haupt, Sie ersetzen die Methode def commandType(self) mit d.commandType = "C_COMMAND", die eine str ist und kann daher nicht wie ein Verfahren genannt werden, .

+0

Danke! Behoben. – apklip

+0

Wenn dies Ihr Problem löst, wäre die Annahme der Antwort großartig. – languitar

+0

Es heißt, ich muss 8 Minuten warten. Ich werde dies nach Ablauf der Zeit tun. – apklip

0

Für Ihre Klasse ....

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() 

Sie setzen bereits self.commandsaus der Datei

Und Anmerkung: index und currentCommand erscheinen exakt den gleichen Zweck

dienen Ihre Funktion verwendet diese Liste.

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] 

Daher brauchen Sie nicht diese Zeilen

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

So brauchen Sie nur diese main vorausgesetzt, die Eingabedatei korrekt ist.

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

Ihr Fehler ist, dass d.commandType = "C_COMMAND" nicht durch "C_COMMAND()" "genannt werden" (das heißt d.commandType())

Verwandte Themen