本文整理汇总了Python中SymbolTable.SymbolTable.kindOf方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.kindOf方法的具体用法?Python SymbolTable.kindOf怎么用?Python SymbolTable.kindOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.kindOf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSymbolTable
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
class TestSymbolTable(unittest.TestCase):
def setUp(self):
self.symbol_table = SymbolTable()
def tearDown(self):
pass
# def assertion(self, actual, expected):
# asserted = actual == expected
# if not asserted:
# print 'FAILED: assert {} == {}'.format(actual, expected)
# assert asserted
def test_constructor(self):
assert self.symbol_table.__class__.__name__ == 'SymbolTable'
def test_define_var(self):
self.symbol_table.define('foo', 'int', 'STATIC')
assert self.symbol_table.counts['STATIC'] == 1
assert self.symbol_table.class_scope['foo'] == ('int', 'STATIC', 0)
def test_define_field(self):
self.symbol_table.define('bar', 'Snake', 'VAR')
self.symbol_table.define('foo', 'int', 'FIELD')
assert self.symbol_table.counts['VAR'] == 1
assert self.symbol_table.subroutine_scope['bar'] == ('Snake', 'VAR', 0)
def test_varCount(self):
self.symbol_table.define('foo', 'int', 'FIELD')
assert self.symbol_table.varCount('FIELD') == 1
assert self.symbol_table.varCount('ARG') == 0
assert self.symbol_table.varCount('STATIC') == 2
assert self.symbol_table.varCount('VAR') == 1
def test_kindOf(self):
self.symbol_table.define('foo', 'int', 'FIELD')
assert self.symbol_table.kindOf('foo') == 'FIELD'
assert self.symbol_table.kindOf('bar') == 'NONE'
def test_typeOf(self):
self.symbol_table.define('foo', 'Square', 'FIELD')
assert self.symbol_table.typeOf('foo') == 'Square'
assert self.symbol_table.typeOf('bar') == 'NONE'
def test_indexOf(self):
self.symbol_table.define('foo', 'boolean', 'FIELD')
self.symbol_table.define('bar', 'char', 'FIELD')
self.symbol_table.define('qaz', 'char', 'STATIC')
assert self.symbol_table.indexOf('foo') == 0
assert self.symbol_table.indexOf('bar') == 1
assert self.symbol_table.indexOf('qaz') == 1
assert self.symbol_table.indexOf('quz') == 'NONE'
示例2: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
#.........这里部分代码省略.........
self.tokenizer.advance()
def compileStatements(self):
while self.tokenizer.tokenType() == 0:
if self.tokenizer.keyWord() == "let":
self.compileLet()
elif self.tokenizer.keyWord() == "if":
self.compileIf()
elif self.tokenizer.keyWord() == "while":
self.compileWhile()
elif self.tokenizer.keyWord() == "do":
self.compileDo()
elif self.tokenizer.keyWord() == "return":
self.compileReturn()
def compileDo(self):
self.tokenizer.advance()
self.compileSubRoutineCall()
self.vmWriter.writePop("temp", "0")
# ignore ;
self.tokenizer.advance()
def compileLet(self):
# let
self.tokenizer.advance()
# varName
varname = self.tokenizer.identifier()
varkind = self.symbolTable.kindOf(varname)
self.tokenizer.advance()
# ([ expression ])?
if self.tokenizer.symbol() == "[":
self.tokenizer.advance()
self.CompileExpression()
if varkind == "field":
self.vmWriter.writePush("this", self.symbolTable.indexOf(varname))
elif varkind == "var":
self.vmWriter.writePush("local", self.symbolTable.indexOf(varname))
elif varkind == "arg":
self.vmWriter.writePush("argument", self.symbolTable.indexOf(varname))
elif varkind == "static":
self.vmWriter.writePush("static", self.symbolTable.indexOf(varname))
self.vmWriter.writeArithmetic("add")
#ignore ]
self.tokenizer.advance()
#ignore =
self.tokenizer.advance()
self.CompileExpression()
self.vmWriter.writePop("temp", "0")
# that
self.vmWriter.writePop("pointer", "1")
self.vmWriter.writePush("temp", "0")
self.vmWriter.writePop("that", "0")
self.tokenizer.advance()
else:
示例3: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
#.........这里部分代码省略.........
self.compileSubroutineCall()
self.vmWriter.writePop("temp", 0) #This pops and ignores the returned value
self.printToken() #Print ';'
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.indentLevel -= 1
self.writeFormatted("</doStatement>")
def compileLet(self):
from SymbolTable import SymbolTable
from VMWriter import VMWriter
self.writeFormatted("<letStatement>")
self.indentLevel += 1
isArray = False
if self.tokenizer.keyWord() != "let":
raise Exception("Let keyword expected")
self.printToken() #Should print "let"
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.printToken() #Should print varname
varName = self.tokenizer.identifier()
self.writeVarInfo(self.tokenizer.identifier(), True)
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.printToken() #Should print '[' or '='
print("compileLet - [ or = " + self.tokenizer.currentToken)
if self.tokenizer.currentToken == "[":
isArray = True
self.tokenizer.advance()
if self.symbolTable.isDefined(varName):
varKind = self.symbolTable.kindOf(varName)
self.vmWriter.writePush(varKind, self.symbolTable.indexOf(varName))
self.compileExpression()
self.printToken() #Should print ']'
self.vmWriter.writeArithmetic("add")
segment = "that"
index = 0
else:
raise Exception("Symbol " + varName + " is not defined")
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.printToken() #Should print '='
else:
#If it goes down this path this is just a regular variable not an array
varKind = self.symbolTable.kindOf(varName)
segment = varKind
index = self.symbolTable.indexOf(varName)
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
print("compileLet - after equals " + self.tokenizer.currentToken)
self.compileExpression()
if isArray:
self.vmWriter.writePop("temp", 0)
self.vmWriter.writePop("pointer", VMWriter.THAT_POINTER)
self.vmWriter.writePush("temp", 0)
self.vmWriter.writePop(segment, index)
self.printToken() #print ";"
示例4: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
#.........这里部分代码省略.........
while self.peek() != ';': # (',' varName)*
self.get_token() # ','
name = self.get_token() # varName
self.symbol_table.define(name, type, 'VAR')
self.get_token() # ';'
# statement*
# letStatement | ifStatement | whileStatement | doStatement | returnStatement
def compileStatements(self):
while self.is_statement():
token = self.get_token()
if 'let' == token:
self.compileLet()
elif 'if' == token:
self.compileIf()
elif 'while' == token:
self.compileWhile()
elif 'do' == token:
self.compileDo()
elif 'return' == token:
self.compileReturn()
# 'do' subroutineCall ';'
def compileDo(self):
self.compile_subroutine_call()
self.vm_writer.writePop('TEMP', 0)
self.get_token() # ';'
# 'let' varName ('[' expression ']')? '=' expression ';'
def compileLet(self):
var_name = self.get_token() # varName
var_kind = self.CONVERT_KIND[self.symbol_table.kindOf(var_name)]
var_index = self.symbol_table.indexOf(var_name)
if '[' == self.peek(): # array assignment
self.get_token() # '['
self.compileExpression() # expression
self.get_token() # ']'
self.vm_writer.writePush(var_kind, var_index)
self.vm_writer.writeArithmetic('ADD')
self.vm_writer.writePop('TEMP', 0)
self.get_token() # '='
self.compileExpression() # expression
self.get_token() # ';'
self.vm_writer.writePush('TEMP', 0)
self.vm_writer.writePop('POINTER', 1)
self.vm_writer.writePop('THAT', 0)
else: # regular assignment
self.get_token() # '='
self.compileExpression() # expression
self.get_token() # ';'
self.vm_writer.writePop(var_kind, var_index)
# 'while' '(' expression ')' '{' statements '}'
def compileWhile(self):
self.while_index += 1
while_index = self.while_index
self.vm_writer.writeLabel('WHILE{}\n'.format(while_index))
示例5: CompliationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
#.........这里部分代码省略.........
str(self._tokenizer.next_token()) # '{'
token = str(self._tokenizer.next_token())
while token != "}":
token = self.CompileStatements(token)
self._vm_writer.write_goto(label) # 'goto label'
self._vm_writer.write_label(if_label) # label for next statement
token = str(self._tokenizer.next_token())
return token
def CompileDo(self, token):
identifier = str(self._tokenizer.next_token()) # identifer or class name
token = str(self._tokenizer.next_token())
class_name = identifier
no_of_arguments = 0
if token == ".":
method_or_function = str(self._tokenizer.next_token())
str(self._tokenizer.next_token()) # '('
id_type = self._symbol_table.typeOf(identifier)
else:
class_name = self._class_name
method_or_function = identifier
no_of_arguments += 1
self._vm_writer.write_push("pointer", "0")
id_type = None
token = str(self._tokenizer.next_token())
if id_type != None:
segment = self._symbol_table.kindOf(identifier)
index = self._symbol_table.indexOf(identifier)
self._vm_writer.write_push(segment, index)
no_of_arguments += 1
class_name = id_type
no_arguments = 0
if token != ")":
token, no_arguments = self.CompilerExpressionList(token) # return value is ')'
no_of_arguments += no_arguments
self._vm_writer.write_call(class_name, method_or_function, no_of_arguments)
str(self._tokenizer.next_token()) # ';'
# 'void functions will return constant 0 which should be discarded'
self._vm_writer.write_pop("temp", "0")
token = str(self._tokenizer.next_token())
return token
def CompileLet(self, token):
"""
Function receiver 'let' and return ';'
"""
identifier = str(self._tokenizer.next_token()) # left hand side identifier
segment = self._symbol_table.kindOf(identifier)
index = str(self._symbol_table.indexOf(identifier))
token = str(self._tokenizer.next_token()) # = or [
if_array = False
if token == "[":
if_array = True
token = str(self._tokenizer.next_token())
示例6: CompilationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import kindOf [as 别名]
#.........这里部分代码省略.........
self._acceptNextToken(',')
argc += 1
self.symbolTable.define(self._acceptNextToken('identifier'), type, 'local')
self._acceptNextToken(';')
return argc
def compileStatements(self):
#statement*
#letStatement|ifStatement|whileStatement|doStatement|returnStatement
while self._tryNextToken(STATEMENT):
if self._tryNextToken('let'):
self.compileLet()
elif self._tryNextToken('if'):
self.compileIf()
elif self._tryNextToken('while'):
self.compileWhile()
elif self._tryNextToken('do'):
self.compileDo()
elif self._tryNextToken('return'):
self.compileReturn()
def compileDo(self):
#'do' subroutineCall ';'
#subroutineName '(' expressionList ')' | (className | varName) '.' subroutineName '(' expressionList ')'
self._acceptNextToken('do')
funcname = self._acceptNextToken('identifier')
argc = 0
if self._tryNextToken('.'):
self._acceptNextToken('.')
type = self.symbolTable.typeOf(funcname)
if type != None:
argc += 1
self.writer.writePush(self.symbolTable.kindOf(funcname), self.symbolTable.indexOf(funcname))
funcname = type + '.' + self._acceptNextToken('identifier') #game.run()
else:
funcname = funcname + '.' + self._acceptNextToken('identifier') #Game.run()
else:
argc += 1
funcname = self.classname + '.' + funcname #run()
self.writer.writePush('pointer', 0)
self._acceptNextToken('(')
argc += self.compileExpressionList()
self._acceptNextToken(')')
self._acceptNextToken(';')
self.writer.writeCall(funcname, argc)
self.writer.writePop('temp', 0)
def compileLet(self):
#'let' varName ('[' expression ']')? '=' expression ';'
self._acceptNextToken('let')
varName = self._acceptNextToken('identifier')
if self._tryNextToken('['):
self.writer.writePush(self.symbolTable.kindOf(varName), self.symbolTable.indexOf(varName))
self._acceptNextToken('[')
self.compileExpression()
self._acceptNextToken(']')
self.writer.writeArithmetic('add')
self._acceptNextToken('=')
self.compileExpression()
self._acceptNextToken(';')
self.writer.writePop('temp', 0)
self.writer.writePop('pointer', 1)
self.writer.writePush('temp', 0)