本文整理汇总了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 = {"<": "<", ">": ">", '"': """, "&": "&"}
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
#.........这里部分代码省略.........