def insert(self, variable, value, env_list): """ Inserts a new variable with the specified value into the specified environment. Parameters: variable -- A VARIABLE type Lexeme object with the variable's name as its value. value -- A NUMBER, BOOLEAN, or STRING type Lexeme object with the variable's value as its value. env_list -- The Environment list into which to insert the variable. """ if self._debug: print( "Inserting variable [" + str(variable) + "]") if self._debug: print( " with value [" + str(value) + "].") var_glue = Lexeme(token_type="GLUE", left=variable) val_glue = Lexeme(token_type="GLUE", left=value) head = env_list.left var_glue.right = head.left val_glue.right = head.right head.left = var_glue head.right = val_glue
def booleanExpression(self): tree = Lexeme(token_type="BOOLEAN_EXPRESSION") tmp = Lexeme(token_type="GLUE") tmp.left = self.expression() self.match("IS") tree.left = self.boolOp() tmp.right = self.expression() tree.right = tmp return tree
def ifStatement(self): if self._debug: print(" in ifStatement") self.match("IF") tree = Lexeme(token_type="IF_STATEMENT") tmp = Lexeme(token_type="GLUE") tree.left = self.booleanExpression() self.match("COMMA") tmp.left = self.block() tmp.right = self.optOtherwise() tree.right = tmp return tree
def statements(self): if self._debug: print(" in statements") tree = Lexeme(token_type="STATEMENTS") tree.left = self.statement() tree.right = self.optStatements() return tree
def commaChain(self): if self._debug: print(" in commaChain") self.match("COMMA") tree = Lexeme(token_type="GLUE") tree.left = self.expression() tree.right = self.optCommaChain() return tree
def whileStatement(self): if self._debug: print(" in whileStatement") self.match("WHILE") tree = Lexeme(token_type="WHILE_STATEMENT") tree.left = self.booleanExpression() self.match("COMMA") tree.right = self.block() return tree
def assignment(self): if self._debug: print(" in assignment") self.match("SET") tree = Lexeme(token_type="ASSIGNMENT") tree.left = self.match("VARIABLE") self.match("TO") tree.right = self.expression() self.match("PERIOD") return tree
def sequence(self): if self._debug: print(" in sequence") tree = Lexeme(token_type="GLUE") # this will be attached to the "head" in my environment tree.left = self.expression() if self.check("AND"): self.match("AND") tmp = Lexeme(token_type="GLUE") tmp.left = self.expression() tree.right = tmp elif self.commaChainPending(): tmp = self.commaChain() self.match("AND") tracer = tmp while tracer.right: tracer = tracer.right tracer.left = self.expression() tree.right = tmp return tree
def optCommaChain(self): if self._debug: print(" in optCommaChain") tree = None if self.check("COMMA"): tree = Lexeme(token_type="GLUE") self.match("COMMA") if self.expressionPending(): tree.left = self.expression() tree.right = self.optCommaChain() return tree
def sequence(self): if self._debug: print(" in sequence") tree = Lexeme(token_type="LIST") tree.left = self.expression() if self.check("AND"): self.match("AND") tmp = Lexeme(token_type="GLUE") tmp.left = self.expression() tree.right = tmp elif self.commaChainPending(): tmp = self.commaChain() self.match("AND") tracer = tmp while tracer.right: tracer = tracer.right tmp2 = Lexeme(token_type="GLUE") tmp2.left = self.expression() tracer.right = tmp2 tree.right = tmp return tree
def program(self): if self._debug: print(" in program") tree = Lexeme(token_type="PROGRAM") sections = [] while self.functionDefPending() or self.statementsPending(): if self.functionDefPending(): sections.append(self.functionDef()) if self.statementsPending(): sections.append(self.statements()) self.match("EOF") tmp = Lexeme(token_type="GLUE") tracer = tmp for section in sections: tracer.left = section tracer.right = Lexeme(token_type="GLUE") tracer = tracer.right tree.right = tmp return tree