Esempio n. 1
0
 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')
Esempio n. 2
0
 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
Esempio n. 3
0
 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
Esempio n. 4
0
 def build_parameter_declarator(self, tree: grammerParser.RContext):
     if tree.getChildCount() != 1:
         pass  # TODO 函数参数出错
     else:
         return tree.getText()
Esempio n. 5
0
 def build_function_name(self, tree: grammerParser.RContext):
     if tree.getPayload().type == grammerLexer.IDENTIFIER:
         return tree.getText()
     else:
         raise
Esempio n. 6
0
 def build_declaration_specifiers(self, tree: grammerParser.RContext):
     return tree.getText()
Esempio n. 7
0
 def build_unary_operator(self, tree: grammerParser.RContext):
     return tree.getText()
Esempio n. 8
0
 def build_assignment_operator(self, tree: grammerParser.RContext):
     return tree.getText()