当前位置: 首页>>代码示例>>Python>>正文


Python SymbolTable.startSubroutine方法代码示例

本文整理汇总了Python中SymbolTable.SymbolTable.startSubroutine方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.startSubroutine方法的具体用法?Python SymbolTable.startSubroutine怎么用?Python SymbolTable.startSubroutine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SymbolTable.SymbolTable的用法示例。


在下文中一共展示了SymbolTable.startSubroutine方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [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,
#.........这里部分代码省略.........
开发者ID:nonocodebox,项目名称:from-nand-to-tetris,代码行数:103,代码来源:CompilationEngine.py

示例2: __init__

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [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):
#.........这里部分代码省略.........
开发者ID:idan0610,项目名称:nand-project11,代码行数:103,代码来源:CompilationEngine.py

示例3: __init__

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [as 别名]

#.........这里部分代码省略.........
            varNames.append(self.tokenizer.currentToken)
            if self.tokenizer.hasMoreTokens(): 
                self.tokenizer.advance()

        while self.tokenizer.symbol() != ";" and self.tokenizer.hasMoreTokens():
            if self.tokenizer.symbol() != ",":
                raise Exception("Invalid variable list")
            self.printToken() #Should print ','
            self.tokenizer.advance()
            self.printToken() #Should print variable name
            varNames.append(self.tokenizer.currentToken)
            if kind == SymbolTable.FIELD:
                self.fieldCount += 1
            if not self.tokenizer.hasMoreTokens():
                raise Exception("More tokens expected")
            self.tokenizer.advance()
        self.printToken()
    

        for name in varNames:
            self.symbolTable.define(name, identifierType, kind)
            self.writeVarInfo(name, False)


        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance()
        self.indentLevel -= 1
        self.writeFormatted("</classVarDec>")

    def compileSubroutine(self):
        from JackTokenizer import JackTokenizer
        from SymbolTable import SymbolTable
        self.writeFormatted("<subroutineDec>")
        self.symbolTable.startSubroutine()
        self.indentLevel += 1
        NUM_OPENING_STATEMENTS = 4
        
        self.printToken() #Should print 'constructor', 'function', or 'method'
        self.isConstructor = False
        self.isMethod = False
        if self.tokenizer.keyWord() == "constructor":
            self.isConstructor = True
        elif self.tokenizer.keyWord() == "method":
            self.isMethod = True
            self.symbolTable.define("this", self.className, SymbolTable.ARG)

        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance() 
            self.printToken() #Should print the type or 'void'

        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance() 
            self.printToken() #Should print the subroutine name
            self.subName = self.tokenizer.identifier()

        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance() 
            self.printToken() #Should print opening '(' before parameter list

        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance() 
        self.compileParameterList()
        self.printToken() #Should print closing ")" after parameter list
        if self.tokenizer.hasMoreTokens():
            self.tokenizer.advance()
开发者ID:itzhak-razi,项目名称:Elements-of-Computing-Systems-Assignments,代码行数:69,代码来源:CompilationEngine.py

示例4: CompilationEngine

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [as 别名]

#.........这里部分代码省略.........
        skip('symbol', '}')

        closeTag()

    def compileClassVarDec(self):
        openTag('classVarDec')

        kind = tokenValue()
        skipToken()

        typ = tokenValue()
        skipType()

        if kind == 'field': self.nClassFields += 1
        name = tokenValue()
        skipIdentifier()

        self.classSymbols.Define(name, typ, kind)

        while tokenIs('symbol', ','):
            skipToken()

            if kind == 'field': self.nClassFields += 1
            name = tokenValue()
            skipIdentifier() # varName
            self.classSymbols.Define(name, typ, kind)

        skip('symbol', ';')

        closeTag()

    def compileSubroutine(self):

        self.subroutineSymbols.startSubroutine()
        self.labelCounter = Counter()

        openTag('subroutineDec')

        subroutineType = tokenValue()
        skipToken()

        returnType = tokenValue()

        if tokenIsKeyword('void'):
            skipToken()
        else:
            skipType()

        functionName = '{}.{}'.format(self.currentClass, tokenValue())
        skipIdentifier()

        self.subroutineTypes[functionName] = Function(subroutineType,
                returnType)

        if subroutineType == 'method':
            self.subroutineSymbols.Define('this', self.currentClass, 'argument')

        self.compileParameterList()


        openTag('subroutineBody')
        skip('symbol', '{')

        nLocals = 0

        while tokenIsKeyword('var'):
开发者ID:Jsearle01,项目名称:the_elements_of_computing_systems,代码行数:70,代码来源:CompilationEngine.py

示例5: __init__

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [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
#.........这里部分代码省略.........
开发者ID:kmanzana,项目名称:nand2tetris,代码行数:103,代码来源:CompilationEngine.py

示例6: Parser

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [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)
#.........这里部分代码省略.........
开发者ID:hubbazoot,项目名称:ecs10,代码行数:103,代码来源:Parser.py

示例7: CompilationEngine

# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import startSubroutine [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
#.........这里部分代码省略.........
开发者ID:TaoZang,项目名称:FromNandToTetris,代码行数:103,代码来源:CompilationEngine.py


注:本文中的SymbolTable.SymbolTable.startSubroutine方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。