本文整理汇总了Python中SymbolTable.SymbolTable.varCount方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.varCount方法的具体用法?Python SymbolTable.varCount怎么用?Python SymbolTable.varCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.varCount方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSymbolTable
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [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 varCount [as 别名]
class CompilationEngine:
def __init__(self, inputFile, outputFile):
self.tokenizer = JackTokenizer(inputFile)
self.vmWriter = VMWriter(outputFile)
self.symbolTable = SymbolTable()
self.classname = ""
self.CompileClass()
self.whilecounter = 0
self.ifcounter = 0
def CompileClass(self):
#classname
self.tokenizer.advance()
self.classname = self.tokenizer.identifier()
self.tokenizer.advance()
# ignore {
self.tokenizer.advance()
while self.tokenizer.keyWord() == "static" or self.tokenizer.keyWord() == "field":
self.CompileClassVarDec()
while self.tokenizer.keyWord() == "constructor" or self.tokenizer.keyWord() == "function" or self.tokenizer.keyWord() == "method":
self.CompileSubroutine()
#ignore }
self.tokenizer.advance()
def CompileClassVarDec(self):
kind = self.tokenizer.keyWord()
self.tokenizer.advance()
type = self.compileType()
name = self.tokenizer.identifier()
self.symbolTable.define(name, type, kind)
self.tokenizer.advance()
# add the rest of var names, if there are
while self.tokenizer.symbol() == ",":
self.tokenizer.advance()
name = self.tokenizer.identifier()
self.symbolTable.define(name, type, kind)
self.tokenizer.advance()
# ignore ;
self.tokenizer.advance()
def CompileSubroutine(self):
self.symbolTable.startSubroutine()
self.ifcounter = 0
self.whilecounter = 0
# constructor | function | method
functype = self.tokenizer.keyWord()
self.tokenizer.advance()
if functype == "method":
self.symbolTable.define("this", self.classname, "arg")
self.tokenizer.advance()
subrotineName = self.classname + "." + self.tokenizer.identifier()
self.tokenizer.advance()
# ( parameterList )
self.tokenizer.advance()
self.compileParameterList()
self.tokenizer.advance()
# subrotineBody
# ignore {
self.tokenizer.advance()
# varDec*
while self.tokenizer.keyWord() == "var":
self.compileVarDec()
self.vmWriter.writeFunction(subrotineName, self.symbolTable.varCount("var"))
# allocate memory for constructor
# if functype == "constructor":
# self.vmWriter.writePush("constant" , self.symbolTable.varCount("field"))
# self.vmWriter.writeCall("Memory.alloc", "1")
if functype == "constructor" or functype == "method":
if functype == "constructor":
self.vmWriter.writePush("constant" , self.symbolTable.varCount("field"))
self.vmWriter.writeCall("Memory.alloc", "1")
else:
self.vmWriter.writePush("argument", "0")
self.vmWriter.writePop("pointer", "0")
# statements
self.compileStatements()
# ignore }
self.tokenizer.advance()
def compileParameterList(self):
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [as 别名]
class CompilationEngine:
"""Recursive top-down parser"""
def __init__(self, inFile, outFile):
"""Creates a new compilation engine with the given input and output.
The next routine called must be compileClass()"""
self.tokenizer = JackTokenizer(inFile)
self.targetFile = open(outFile, 'w')
self.getNext()
self.classTable = None
self.className = ''
self.writer = VMWriter(outFile)
self.labelWhile = 1
self.labelIf = 1
def getNext(self):
if self.tokenizer.hasMoreTokens():
self.tokenizer.advance()
def compileClass(self):
"""Compiles a complete class"""
self.classTable = SymbolTable()
# 'class' className '{' classVarDec* subroutineDec* '}'
# class
self.getNext()
# className
self.className = self.tokenizer.getToken()
self.getNext()
# {
self.getNext()
token = self.tokenizer.getToken()
while token in ["static", "field"]:
self.compileDec()
token = self.tokenizer.getToken()
token = self.tokenizer.getToken()
while token in ["constructor", "function", "method"]:
self.compileSubroutine()
token = self.tokenizer.getToken()
# }
self.getNext()
def compileSubroutine(self):
"""Compiles a complete method, function, or constructor."""
# subroutine dec
self.classTable.startSubroutine()
# ('constructor' | 'function' | 'method') ('void' | type) subroutineName '(' parameterList ')' subroutineBody
# ('constructor' | 'function' | 'method')
subroutineType = self.tokenizer.getToken()
self.getNext()
# ('void' | type)
self.getNext()
# subroutineName
name = self.tokenizer.getToken()
self.getNext()
# (
self.getNext()
# parameterList
self.compileParameterList(subroutineType == 'method')
# )
self.getNext()
# subroutine body
# '{' varDec* statements '}'
# {
self.getNext()
# varDec*
while self.tokenizer.getToken() == 'var':
self.compileDec()
numOfVars = self.classTable.varCount(Toolbox.VAR)
if subroutineType == 'function':
self.writer.writeFunction(self.className + "." + name, numOfVars)
elif subroutineType == 'constructor':
self.writer.writeFunction(self.className + "." + name, numOfVars)
# push constant (num of fields)
# call Memory.alloc 1
# pop pointer 0
fields = self.classTable.varCount(Toolbox.FIELD)
self.writer.writePush(Toolbox.CONST, fields)
self.writer.writeCall('Memory.alloc', 1)
self.writer.writePop(Toolbox.POINTER, 0)
else: # method
self.writer.writeFunction(self.className + "." + name, numOfVars)
# push argument 0
# pop pointer 0
self.writer.writePush(Toolbox.SEG_ARG, 0)
self.writer.writePop(Toolbox.POINTER, 0)
# statements
self.compileStatements()
# }
self.getNext()
def compileParameterList(self, method=False):
"""Compiles a (possibly empty) parameter list,
#.........这里部分代码省略.........
示例4: __init__
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [as 别名]
class CompilationEngine:
CONVERT_KIND = {
'ARG': 'ARG',
'STATIC': 'STATIC',
'VAR': 'LOCAL',
'FIELD': 'THIS'
}
ARITHMETIC = {
'+': 'ADD',
'-': 'SUB',
'=': 'EQ',
'>': 'GT',
'<': 'LT',
'&': 'AND',
'|': 'OR'
}
ARITHMETIC_UNARY = {
'-': 'NEG',
'~': 'NOT'
}
if_index = -1
while_index = -1
def __init__(self, vm_writer, tokenizer):
self.vm_writer = vm_writer
self.tokenizer = tokenizer
self.symbol_table = SymbolTable()
self.buffer = []
# 'class' className '{' classVarDec* subroutineDec* '}'
def compileClass(self):
self.get_token() # 'class'
self.class_name = self.get_token() # className
self.get_token() # '{'
while self.is_class_var_dec():
self.compileClassVarDec() # classVarDec*
while self.is_subroutine_dec():
self.compileSubroutine() # subroutineDec*
self.vm_writer.close()
# ('static' | 'field' ) type varName (',' varName)* ';'
def compileClassVarDec(self):
kind = self.get_token() # ('static' | 'field' )
type = self.get_token() # type
name = self.get_token() # varName
self.symbol_table.define(name, type, kind.upper())
while self.peek() != ';': # (',' varName)*
self.get_token() # ','
name = self.get_token() # varName
self.symbol_table.define(name, type, kind.upper())
self.get_token() # ';'
# subroutineDec: ('constructor' | 'function' | 'method') ('void' | type) subroutineName '(' parameterList ')' subroutineBody
# subroutineBody: '{' varDec* statements '}'
def compileSubroutine(self):
subroutine_kind = self.get_token() # ('constructor' | 'function' | 'method')
self.get_token() # ('void' | type)
subroutine_name = self.get_token() # subroutineName
self.symbol_table.startSubroutine()
if subroutine_kind == 'method':
self.symbol_table.define('instance', self.class_name, 'ARG')
self.get_token() # '('
self.compileParameterList() # parameterList
self.get_token() # ')'
self.get_token() # '{'
while 'var' == self.peek():
self.compileVarDec() # varDec*
function_name = '{}.{}'.format(self.class_name, subroutine_name)
num_locals = self.symbol_table.varCount('VAR')
self.vm_writer.writeFunction(function_name, num_locals)
if subroutine_kind == 'constructor':
num_fields = self.symbol_table.varCount('FIELD')
self.vm_writer.writePush('CONST', num_fields)
self.vm_writer.writeCall('Memory.alloc', 1)
self.vm_writer.writePop('POINTER', 0)
elif subroutine_kind == 'method':
self.vm_writer.writePush('ARG', 0)
self.vm_writer.writePop('POINTER', 0)
self.compileStatements() # statements
self.get_token() # '}'
# ( (type varName) (',' type varName)*)?
def compileParameterList(self):
if ')' != self.peek():
type = self.get_token() # type
#.........这里部分代码省略.........
示例5: CompliationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [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
#.........这里部分代码省略.........
示例6: Parser
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [as 别名]
class Parser():
def __init__(self,tokens,vmwriter):
try:
tokens[0].value
tokens[0].type
except:
sys.exit("Parser did not take in a list of tokens!")
self.tokens=tokens
self.vmwriter=vmwriter
self.symTable=SymbolTable()
def parse(self):
self.index = 0
self.compileClass()
def compileClass(self):
self.createChild()
self.className=self.createChild().value
self.createChild()
while(self.lookChild().value in ["static","field"]):
self.varDec()
while(self.lookChild().value in ["constructor","function","method"]):
self.subroutineDec()
self.createChild() #"}"
def lookChild(self): #Helper method. Returns current token.
return self.tokens[self.index]
def createChild(self): #Helper method. Returns and increments child
#Note: This function is designed to input the NEXT element, as it will iterate the counter by one.
child = self.tokens[self.index]
self.index+=1
return child
def varDec(self): #Adds all the vars
varKind = self.createChild().value
varType = self.createChild().value
varName = self.createChild().value
self.symDef(varType,varKind,varName)
while(self.createChild().value==","): #Adds all additional variables, will also increment semicolon
varName = self.createChild().value
self.symDef(varType,varKind,varName)
def subroutineDec(self):
self.whileLoops=0
self.ifStatements=0
self.symTable.startSubroutine()
if(self.lookChild().value=="method"):
self.symDef(self.className,"arg","this")
subroutineKind="method"
elif(self.lookChild().value=="constructor"):
subroutineKind="constructor"
elif(self.lookChild().value=="function"):
subroutineKind="function"
self.createChild() #Pass over the type of subroutine
self.createChild() #Pass over the kind of subroutine
subroutineName=self.createChild().value #name of function/method/construct
self.createChild() #Skip "("
while(self.lookChild().value!=")"):
varType = self.createChild().value
varName = self.createChild().value
self.symDef(varType,"arg",varName)
if(self.lookChild().value==","):
self.createChild()
self.createChild() #Passes up to and over ")"
self.subRoutineBody(subroutineKind,subroutineName)
def subRoutineBody(self,subKind,subName): #builds method body
self.createChild() #{
while(self.lookChild().value)=="var":
self.varDec()
self.vmwriter.writeFunction(self.className+"."+subName,self.symTable.varCount("var"))
if subKind=="constructor":
self.vmwriter.writePush("constant",self.symTable.varCount("field"))
self.vmwriter.writeCall("Memory.alloc",1)
self.vmwriter.writePop("pointer",0)
elif subKind=="method":
self.vmwriter.writePush("argument",0)
self.vmwriter.writePop("pointer",0)
self.statements()
self.createChild()#"}"
def subRoutineCall(self):
nArgs=0
beforeDot=self.createChild().value
label=beforeDot
if self.lookChild().value ==".":
self.createChild() #.
isObject=self.symTable.getItem(beforeDot)
if(isObject):
self.vmwriter.writePush(self.tranKind(isObject.kind),isObject.index)
nArgs=1
label=isObject.type
label+="."+self.createChild().value #identifier
else:
self.vmwriter.writePush("pointer",0)
nArgs=1
label=self.className+"."+label
self.createChild() #(
nArgs+=self.expressionList() #expression
self.createChild() #)
self.vmwriter.writeCall(label, nArgs)
#.........这里部分代码省略.........
示例7: CompilationEngine
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import varCount [as 别名]
class CompilationEngine(object):
def __init__(self, src, output):
self.tokenizer = JackTokenizer(src)
self.writer = VMWriter(output)
self.symbolTable = SymbolTable()
self.labelIndex = 0
def _acceptNextToken(self, token):
if self.tokenizer.hasMoreToken():
self.tokenizer.advance()
typ = self.tokenizer.tokenType()
tok = self.tokenizer.tokenValue()
if type(token) != list:
token = [token]
if typ in token or tok in token:
return tok
raise SyntaxError('Parse Error')
def _tryNextToken(self, token):
if self.tokenizer.hasMoreToken():
typ, tok = self.tokenizer.next()
if type(token) != list:
token = [token]
if typ in token or tok in token:
return True
return False
def compileClass(self):
#'class' className '{' classVarDec* subroutineDec* '}'
self._acceptNextToken('class')
self.classname = self._acceptNextToken('identifier')
self._acceptNextToken('{')
while self._tryNextToken(['static', 'field']):
self.compileClassVarDec()
while self._tryNextToken(['constructor', 'function', 'method']):
self.compileSubroutine()
self._acceptNextToken('}')
self.writer.close()
def compileClassVarDec(self):
#('static'|'field') type varName (','varName)* ';'
kind = self._acceptNextToken(['static', 'field'])
type = self._acceptNextToken(['int', 'char', 'boolean', 'identifier'])
self.symbolTable.define(self._acceptNextToken('identifier'), type, kind)
while self._tryNextToken(','):
self._acceptNextToken(',')
self.symbolTable.define(self._acceptNextToken('identifier'), type, kind)
self._acceptNextToken(';')
def compileSubroutine(self):
#('constructor'|'function'|'method')
#('void'|type)subroutineName'('parameterList')'
#subroutineBody
self.labelIndex = 0
self.symbolTable.startSubroutine()
subroutine = self._acceptNextToken(['constructor', 'function', 'method'])
self._acceptNextToken(['void', 'int', 'char', 'boolean', 'identifier'])
functionname = self._acceptNextToken('identifier')
if subroutine == 'method':
self.symbolTable.define('this', self.classname, 'argument')
self._acceptNextToken('(')
self.compileParameterList()
self._acceptNextToken(')')
self._acceptNextToken('{')
argc = 0
while self._tryNextToken('var'):
argc += self.compileVarDec()
self.writer.writeFunction(self.classname + '.' + functionname, argc)
if subroutine == 'constructor':
self.writer.writePush('constant', self.symbolTable.varCount('field'))
self.writer.writeCall('Memory.alloc', 1)
self.writer.writePop('pointer', 0)
elif subroutine == 'method':
self.writer.writePush('argument', 0)
self.writer.writePop('pointer', 0)
while self._tryNextToken(STATEMENT):
self.compileStatements()
self._acceptNextToken('}')
def compileParameterList(self):
#((type varName)(','type varName)*)?
if self._tryNextToken(TYPE):
type = self._acceptNextToken(TYPE)
self.symbolTable.define(self._acceptNextToken('identifier'), type, 'argument')
while self._tryNextToken(','):
self._acceptNextToken(',')
type = self._acceptNextToken(TYPE)
self.symbolTable.define(self._acceptNextToken('identifier'), type, 'argument')
def compileVarDec(self):
#'var' type varName (',' varName)*';'
argc = 1
#.........这里部分代码省略.........