def build_primary_expression(self, tree: grammerParser.RContext): if tree.getChildCount() == 3: return self.build_assignment_expression(tree.getChild(1)) elif tree.getChild(0).getChildCount() != 0: identifier, expression_list = self.build_variable_expression( tree.getChild(0)) if self.symbol_table.is_function_existed(identifier) and len( expression_list) == 0: return identifier if self.symbol_table.is_variable_existed(identifier): symbol = self.symbol_table.get_variable(identifier) if len(expression_list) > 0: variable_expression = VariableExpression( symbol, expression_list) else: variable_expression = VariableExpression(symbol) return variable_expression else: raise CompilerError('variable is not defined') elif tree.getChild(0).getPayload().type == grammerLexer.INT_CONSTANT: return ConstantExpression(tree.getText(), 'int') elif tree.getChild(0).getPayload().type == grammerLexer.BOOL_CONSTANT: return ConstantExpression(tree.getText(), 'bool') elif tree.getChild(0).getPayload().type == grammerLexer.REAL_CONSTANT: return ConstantExpression(tree.getText(), 'real')
def build_variable_expression(self, tree: grammerParser.RContext): if tree.getChildCount() == 1: return tree.getText(), [] else: identifier, expression_list = self.build_variable_expression( tree.getChild(0)) expression_list.append( self.build_assignment_expression(tree.getChild(2))) return identifier, expression_list
def build_declarator(self, tree: grammerParser.RContext): if tree.getChildCount() == 1: identifier = tree.getText() return identifier, [] elif tree.getChildCount() == 4 and tree.getChild( 0).getRuleIndex() == grammerParser.RULE_declarator: identifier, size_list = self.build_declarator(tree.getChild(0)) expression = self.build_logical_or_expression(tree.getChild(2)) if isinstance( expression, ConstantExpression) and expression.get_type() == 'int': size_list.append(expression.get_value()) return identifier, size_list
def build_parameter_declarator(self, tree: grammerParser.RContext): if tree.getChildCount() != 1: pass # TODO 函数参数出错 else: return tree.getText()
def build_function_name(self, tree: grammerParser.RContext): if tree.getPayload().type == grammerLexer.IDENTIFIER: return tree.getText() else: raise
def build_declaration_specifiers(self, tree: grammerParser.RContext): return tree.getText()
def build_unary_operator(self, tree: grammerParser.RContext): return tree.getText()
def build_assignment_operator(self, tree: grammerParser.RContext): return tree.getText()