本文整理汇总了Python中SymbolTable.SymbolTable.typeOf方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.typeOf方法的具体用法?Python SymbolTable.typeOf怎么用?Python SymbolTable.typeOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.typeOf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSymbolTable
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import typeOf [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 typeOf [as 别名]
#.........这里部分代码省略.........
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.tokenizer.advance()
elif self.tokenizer.tokenType() == 1 and self.tokenizer.symbol() == '(':
# ( expression )
self.tokenizer.advance()
self.CompileExpression()
self.tokenizer.advance()
else:
#unary!!!
op = self.tokenizer.symbol()
self.tokenizer.advance()
self.CompileTerm()
if op == "-":
self.vmWriter.writeArithmetic("neg")
elif op == "~":
self.vmWriter.writeArithmetic("not")
def compileSubRoutineCall(self):
# subroutineName | (className | varName)
identifier = self.tokenizer.identifier()
self.tokenizer.advance()
#no "." only name
if self.tokenizer.symbol() == '(':
# ( expressionList ) -- subroutine of type method
self.tokenizer.advance()
self.vmWriter.writePush("pointer", "0")
argnum = self.CompileExpressionList()
self.vmWriter.writeCall(self.classname + "." + identifier, str(argnum +1))
self.tokenizer.advance()
else:
# . -- class.function or var.method
self.tokenizer.advance()
# subroutineName
subname = self.tokenizer.identifier()
self.tokenizer.advance()
self.tokenizer.advance()
if identifier in self.symbolTable.classtable or identifier in self.symbolTable.subroutinetable:
# varname!!!
if identifier in self.symbolTable.subroutinetable:
if self.symbolTable.kindOf(identifier) == "var":
self.vmWriter.writePush("local", self.symbolTable.indexOf(identifier))
else:
self.vmWriter.writePush("argument", self.symbolTable.indexOf(identifier))
else:
if self.symbolTable.kindOf(identifier) == "static":
self.vmWriter.writePush("static", self.symbolTable.indexOf(identifier))
else:
self.vmWriter.writePush("this", self.symbolTable.indexOf(identifier))
argnum = self.CompileExpressionList()
identifierclass = self.symbolTable.typeOf(identifier)
self.vmWriter.writeCall(identifierclass + "." + subname, str(argnum +1))
else:
argnum = self.CompileExpressionList()
self.vmWriter.writeCall(identifier + "." + subname, str(argnum))
self.tokenizer.advance()
def CompileExpressionList(self):
# (expression
i = 0
if self.isTerm():
i += 1
# (, expression)
self.CompileExpression()
while self.tokenizer.symbol() == ',':
i+= 1
self.tokenizer.advance()
self.CompileExpression()
return i
def isTerm(self):
if self.tokenizer.tokenType() == 3 or self.tokenizer.tokenType() == 4:
return True
if self.tokenizer.tokenType() == 0 and self.tokenizer.keyWord() in keyword_const:
return True
if self.tokenizer.tokenType() == 1 and self.tokenizer.symbol() == '(' :
return True
if self.tokenizer.tokenType() == 1 and (self.tokenizer.symbol() == '-' or self.tokenizer.symbol() == '~'):
return True
if self.tokenizer.tokenType() == 2:
return True
return False
def compileType(self):
if self.tokenizer.tokenType() == 0:
typen = self.tokenizer.keyWord()
else:
typen = self.tokenizer.identifier()
self.tokenizer.advance()
return typen
示例3: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import typeOf [as 别名]
#.........这里部分代码省略.........
self.indentLevel += 1
self.printToken()
if self.tokenizer.tokenType == JackTokenizer.IDENTIFIER:
name = self.tokenizer.identifier()
self.tokenizer.advance()
print("second token in IDENTIFIER " + self.tokenizer.currentToken)
if self.tokenizer.tokenType == JackTokenizer.SYMBOL:
if self.tokenizer.symbol() == ".":
if self.symbolTable.isDefined(name):
self.writeVarInfo(name, True)
else:
self.writeClassOrSubInfo("class", True)
self.printToken() #Should print '.'
self.tokenizer.advance()
self.printToken() #Should print subroutine name
subName = self.tokenizer.identifier()
self.tokenizer.advance()
self.printToken() #Should print '('
#If the subroutine is a method call we must first push the object before
#pushing the rest of the arguments
if self.symbolTable.isDefined(name):
self.vmWriter.writePush(self.symbolTable.kindOf(name), self.symbolTable.indexOf(name))
self.numExpressions = 0
self.tokenizer.advance()
self.compileExpressionList()
self.printToken() #Should print ')'
if self.symbolTable.isDefined(name):
#Must add 1 to the number of arguments since we pushed the object that the method is operating on
self.vmWriter.writeCall(self.symbolTable.typeOf(name) + "." + subName, self.numExpressions + 1)
else:
self.vmWriter.writeCall(name + "." + subName, self.numExpressions)
self.tokenizer.advance()
elif self.tokenizer.symbol() == "(":
self.printToken()
self.writeClassOrSubInfo("subroutine", True)
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.vmWriter.writePush("pointer", VMWriter.THIS_POINTER)
self.compileExpressionList()
self.numExpressions += 1
self.printToken() #Print ')'
self.vmWriter.writeCall(self.className + "." + name, self.numExpressions)
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
elif self.tokenizer.symbol() == "[":
self.writeVarInfo(name, True)
self.printToken()
self.vmWriter.writePush(self.symbolTable.kindOf(name), self.symbolTable.indexOf(name))
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
self.compileExpression()
self.printToken() #Should print ']'
self.vmWriter.writeArithmetic("add")
self.vmWriter.writePop("pointer", VMWriter.THAT_POINTER)
self.vmWriter.writePush("that", 0)
if self.tokenizer.hasMoreTokens():
示例4: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import typeOf [as 别名]
#.........这里部分代码省略.........
if ')' != self.peek():
number_args += 1
self.compileExpression()
while ')' != self.peek():
number_args += 1
self.get_token() # ','
self.compileExpression()
return number_args
# private
def compile_keyword(self):
keyword = self.get_token() # keywordConstant
if keyword == 'this':
self.vm_writer.writePush('POINTER', 0)
else:
self.vm_writer.writePush('CONST', 0)
if keyword == 'true':
self.vm_writer.writeArithmetic('NOT')
# subroutineCall: subroutineName '(' expressionList ')' | ( className | varName) '.' subroutineName '(' expressionList ')'
def compile_subroutine_call(self):
identifier = self.get_token() # (subroutineName | className | varName)
function_name = identifier
number_args = 0
if '.' == self.peek():
self.get_token() # '.'
subroutine_name = self.get_token() # subroutineName
type = self.symbol_table.typeOf(identifier)
if type != 'NONE': # it's an instance
instance_kind = self.symbol_table.kindOf(identifier)
instance_index = self.symbol_table.indexOf(identifier)
self.vm_writer.writePush(self.CONVERT_KIND[instance_kind], instance_index)
function_name = '{}.{}'.format(type, subroutine_name)
number_args += 1
else: # it's a class
class_name = identifier
function_name = '{}.{}'.format(class_name, subroutine_name)
elif '(' == self.peek():
subroutine_name = identifier
function_name = '{}.{}'.format(self.class_name, subroutine_name)
number_args += 1
self.vm_writer.writePush('POINTER', 0)
self.get_token() # '('
number_args += self.compileExpressionList() # expressionList
self.get_token() # ')'
self.vm_writer.writeCall(function_name, number_args)
def compile_string(self):
string = self.get_token() # stringConstant
self.vm_writer.writePush('CONST', len(string))
self.vm_writer.writeCall('String.new', 1)
for char in string:
示例5: CompliationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import typeOf [as 别名]
#.........这里部分代码省略.........
self._vm_writer.write_label(label)
str(self._tokenizer.next_token()) # '('
token = str(self._tokenizer.next_token())
token = self.CompileExpression(token) # 'returns ')'
self._vm_writer.write_arithmatic("~") # ~cond
if_label = self._class_name + "." + "while." + str(self._counter) + ".L2"
self._vm_writer.write_if_goto(if_label)
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):
示例6: CompilationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import typeOf [as 别名]
#.........这里部分代码省略.........
self.symbolTable.define(self._acceptNextToken('identifier'), type, 'local')
while self._tryNextToken(','):
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(';')