當前位置: 首頁>>代碼示例>>Python>>正文


Python SymbolTable.startSubRoutine方法代碼示例

本文整理匯總了Python中SymbolTable.SymbolTable.startSubRoutine方法的典型用法代碼示例。如果您正苦於以下問題:Python SymbolTable.startSubRoutine方法的具體用法?Python SymbolTable.startSubRoutine怎麽用?Python SymbolTable.startSubRoutine使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在SymbolTable.SymbolTable的用法示例。


在下文中一共展示了SymbolTable.startSubRoutine方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: CompliationEngine

# 需要導入模塊: from SymbolTable import SymbolTable [as 別名]
# 或者: from SymbolTable.SymbolTable import startSubRoutine [as 別名]
class CompliationEngine(object):
    """
    Effects the actual compilation output. Gets its input from a
    JackTokenizer and emits its parsed structure into an output file/stream
    """

    MAP = {"<": "&lt;", ">": "&gt;", '"': "&quot;", "&": "&amp;"}

    def __init__(self, tokenizer, out_file_name):
        """
        Constructor
        """
        self._tokenizer = tokenizer
        self._vm_writer = VMWriter(out_file_name)
        self._class_name = None
        self._symbol_table = SymbolTable()
        self._counter = 0
        self._subroutine_name = None

    def Compile(self):
        token = str(self._tokenizer.next_token())
        if token == "class":
            self.CompileClass(token)

    def CompileClass(self, token):
        """
        takes 'class' as token
        and end the compilation
        """
        self._class_name = self._tokenizer.next_token()  # got the class name
        str(self._tokenizer.next_token())  # '{'
        token = self._tokenizer.next_token()  # field declarations

        # For declaring Class Level Variable

        while token in ["field", "static"]:
            token = self.CompileClassVarDec(token)

        # Class Methods
        while token in ["function", "method", "constructor"]:
            token = self.CompileSubroutine(token)

        self._vm_writer.writer_close()
        self._symbol_table.printSymbolTables()

    def CompileSubroutine(self, token):
        """
        Takes any among 'function', 'method', 'constructor'
        and return token after end of subroutine '}' 
        or simple next subroutine token
        """
        function_modifier = token

        str(self._tokenizer.next_token())  # return type
        function_name = str(self._tokenizer.next_token())  # name of function

        self._subroutine_name = function_name

        self._symbol_table.startSubRoutine(function_name)
        if function_modifier == "method":
            self._symbol_table.define(["this", self._class_name, "argument"])

        str(self._tokenizer.next_token())  # '('

        token = str(self._tokenizer.next_token())  # 'arguments'

        while token != ")":
            token = self.CompileParamList(token)

        str(self._tokenizer.next_token())  # '{'
        token = str(self._tokenizer.next_token())  # Statements or '}'

        while token == "var":
            token = self.CompileVarDec(token)

        local_variables = self._symbol_table.varCount("local")

        # Writing Function VM
        self._vm_writer.write_subroutine(self._class_name, function_name, local_variables)
        if function_name == "new":
            no_of_fields = self._symbol_table.varCount("field")
            self._vm_writer.write_push("constant", no_of_fields)
            self._vm_writer.write_call("Memory", "alloc", 1)
            self._vm_writer.write_pop("pointer", 0)
        if function_modifier == "method":
            self._vm_writer.write_push("argument", 0)
            self._vm_writer.write_pop("pointer", 0)
        """temp_buffer = ""
        while local_variables > 0:
            temp_buffer += 'push constant 0\n'
            local_variables -= 1
        
        self._out_file_object.write(temp_buffer)
        self._out_file_object.flush()"""

        while token != "}":
            token = self.CompileStatements(token)

        token = str(self._tokenizer.next_token())  # next subroutine
        return token
#.........這裏部分代碼省略.........
開發者ID:saikumarm4,項目名稱:Nand2Tetris,代碼行數:103,代碼來源:Compilation.py


注:本文中的SymbolTable.SymbolTable.startSubRoutine方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。