コード例 #1
0
ファイル: tokenizer.py プロジェクト: arshnooramin/Nand2Tetris
 def _firstParse(self):
     for line in self._f:
         # remove the newline char '\n'
         line = line.strip()
         # check if there is parseable code in line
         if not fh.isCommentOrEmpty(line):
             line = fh.removeInlineComments(line)
             # update the clean code string
             self._code += line
コード例 #2
0
 def _secPass(self, line):
     line = fh.removeInlineComments(line)
     # check if it's a instruction
     if fh.isAInst(line):
         # handle a-instruction
         self._handleAInst(line[1:])
     # else must be a c-instruction
     elif not fh.isLoopMarker(line):
         # handle c-instruction
         self._handleCInst(line)
コード例 #3
0
 def parse(self):
     for line in self._f:
         # remove the newline char '\n'
         line = line.strip()
         # check if there is parseable code in line
         if not fh.isCommentOrEmpty(line):
             line = fh.removeInlineComments(line)
             # split commands from the line
             commands = line.split(" ")
             # get the type of command
             cmdtype = cf.commandType(commands[0])
             # if it's a push or pop command handle it
             if cmdtype == PUSH or cmdtype == POP:
                 self._handlePushPop(commands, cmdtype)
             # else if it's a arithmetic command handle it
             elif cmdtype == ARITHMETIC:
                 self._handleArithmetic(commands)
コード例 #4
0
 def parse(self):
     for line in self._f:
         # remove the newline char '\n'
         line = line.strip()
         # check if there is parseable code in line
         if not fh.isCommentOrEmpty(line):
             writeStr = ""
             # remove in-line comments from each line
             line = fh.removeInlineComments(line)
             # split commands from the line
             commands = line.split(" ")
             # get the type of command
             cmdtype = self._cf.commandType(commands[0])
             # if it's a push or pop command handle it
             if cmdtype == PUSH or cmdtype == POP:
                 writeStr = self._handlePushPop(commands, cmdtype)
             # else if it's a arithmetic command handle it
             elif cmdtype == ARITHMETIC:
                 writeStr = self._handleArithmetic(commands)
             # else if it's a label command handle it
             elif cmdtype == LABEL:
                 writeStr = self._cf.writeLabelAsm(commands[1])
             # else if it's a goto command handle it
             elif cmdtype == GOTO:
                 writeStr = self._cf.writeGotoAsm(commands[1])
             # else if it's a if-goto command handle it
             elif cmdtype == IF:
                 writeStr = self._cf.writeIfGotoAsm(commands[1])
             # else if it's a call command handle it
             elif cmdtype == CALL:
                 writeStr = self._cf.writeCallAsm(commands[1], int(commands[2]))
             # else if it's a return command handle it
             elif cmdtype == RETURN:
                 writeStr = self._cf.writeReturnAsm()
             # else if it's a function command handle it
             elif cmdtype == FUNCTION:
                 writeStr = self._cf.writeFunctionAsm(commands[1], int(commands[2]))
             # add it to the code list
             self._codeList.append(writeStr)
             print(line)
             print(self._codeList)