From: Nick Downing Date: Mon, 28 Jan 2019 03:19:31 +0000 (+1100) Subject: Use ansi_c.t, get rid of old AST generator and stripper X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?p=c_to_python.git;a=commitdiff_plain;h=484465e7f1356e8c9455b0dd7abd53e2f1add82f Use ansi_c.t, get rid of old AST generator and stripper --- diff --git a/.gitignore b/.gitignore index 21fcc18..b525ad6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,9 +3,8 @@ a.c a.i a.xml b.xml -ansi_c.l.xml -ansi_c.y.xml lex_yy.py +t_def.py tests/*.l.xml tests/*.l.new.xml tests/*.l.new diff --git a/Makefile b/Makefile index 5316128..fe4da19 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,13 @@ -all: lex_yy.py y_tab.py +all: lex_yy.py t_def.py y_tab.py lex_yy.py: ansi_c.l - ../bootstrap_flex.git/src/flex -o /dev/null $< 2>$<.xml - ../pilex.git/pilex.py --element --python $<.xml + ../pilex.git/pilex.py --element --python $< + +t_def.py: ansi_c.t + ../pitree.git/pitree.py --python $< y_tab.py: ansi_c.y - ../bootstrap_bison.git/src/bison -o /dev/null $< 2>$<.xml - ../piyacc.git/piyacc.py --element --python $<.xml + ../piyacc.git/piyacc.py --element --python $< clean: - rm -f lex_yy.py y_tab.py *.xml + rm -f lex_yy.py t_def.py y_tab.py diff --git a/ansi_c.l b/ansi_c.l index 55d36cd..099f1b1 100644 --- a/ansi_c.l +++ b/ansi_c.l @@ -22,7 +22,7 @@ ES (\\(['"\?\\abfnrtv]|[0-7]{1,3}|x[a-fA-F0-9]+)) WS [ \t\v\n\f] %{ -import ast +import t_def import y_tab %} @@ -86,28 +86,28 @@ import y_tab } /* hack for yacc/bison specification */ -(?E{ast.AST.Identifier}{L}{A}*) | -(?E{ast.AST.Identifier}[$@]("$"|{D}+)) { +(?E{t_def.AST.Identifier}{L}{A}*) | +(?E{t_def.AST.Identifier}[$@]("$"|{D}+)) { return y_tab.IDENTIFIER } -(?E{ast.AST.ExpressionIntLiteral}{HP}{H}+{IS}?) | -(?E{ast.AST.ExpressionIntLiteral}{NZ}{D}*{IS}?) | -(?E{ast.AST.ExpressionIntLiteral}"0"{O}*{IS}?) | -(?E{ast.AST.ExpressionCharConstant}{CP}?"'"(?E{ast.AST.Text}([^'\\\n]|{ES})+)"'") { +(?E{t_def.AST.ExpressionIntLiteral}{HP}{H}+{IS}?) | +(?E{t_def.AST.ExpressionIntLiteral}{NZ}{D}*{IS}?) | +(?E{t_def.AST.ExpressionIntLiteral}"0"{O}*{IS}?) | +(?E{t_def.AST.ExpressionCharConstant}{CP}?"'"(?E{t_def.AST.Text}([^'\\\n]|{ES})+)"'") { return y_tab.I_CONSTANT } -(?E{ast.AST.ExpressionFloatLiteral}{D}+{E}{FS}?) | -(?E{ast.AST.ExpressionFloatLiteral}{D}*"."{D}+{E}?{FS}?) | -(?E{ast.AST.ExpressionFloatLiteral}{D}+"."{E}?{FS}?) | -(?E{ast.AST.ExpressionFloatLiteral}{HP}{H}+{P}{FS}?) | -(?E{ast.AST.ExpressionFloatLiteral}{HP}{H}*"."{H}+{P}{FS}?) | -(?E{ast.AST.ExpressionFloatLiteral}{HP}{H}+"."{P}{FS}?) { +(?E{t_def.AST.ExpressionFloatLiteral}{D}+{E}{FS}?) | +(?E{t_def.AST.ExpressionFloatLiteral}{D}*"."{D}+{E}?{FS}?) | +(?E{t_def.AST.ExpressionFloatLiteral}{D}+"."{E}?{FS}?) | +(?E{t_def.AST.ExpressionFloatLiteral}{HP}{H}+{P}{FS}?) | +(?E{t_def.AST.ExpressionFloatLiteral}{HP}{H}*"."{H}+{P}{FS}?) | +(?E{t_def.AST.ExpressionFloatLiteral}{HP}{H}+"."{P}{FS}?) { return y_tab.F_CONSTANT } -(?E{ast.AST.ExpressionStringLiteral}({SP}?\"(?E{ast.AST.Text}([^"\\\n]|{ES})*)\"{WS}*)+) { +(?E{t_def.AST.ExpressionStringLiteral}({SP}?\"(?E{t_def.AST.Text}([^"\\\n]|{ES})*)\"{WS}*)+) { return y_tab.STRING_LITERAL } diff --git a/ansi_c.py b/ansi_c.py index bda910b..172b8eb 100755 --- a/ansi_c.py +++ b/ansi_c.py @@ -16,19 +16,19 @@ # this program; if not, write to the Free Software Foundation, Inc., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import ast +import t_def import element import sys import xml.etree.ElementTree import y_tab -root = y_tab.yyparse(ast.AST.TranslationUnit) +root = y_tab.yyparse(t_def.AST.TranslationUnit) element.serialize(root, 'a.xml', 'utf-8') -root = element.deserialize('a.xml', ast.factory, 'utf-8') +root = element.deserialize('a.xml', t_def.factory, 'utf-8') xml.etree.ElementTree.dump(root) for i in root: - if isinstance(i, ast.AST.Declaration): + if isinstance(i, t_def.AST.Declaration): base_type = i[0].get_type() for j in i[1]: type, name = j[0].get_type_and_name(base_type) diff --git a/ansi_c.t b/ansi_c.t new file mode 100644 index 0000000..5c0ab28 --- /dev/null +++ b/ansi_c.t @@ -0,0 +1,921 @@ +/* + * Copyright (C) 2019 Nick Downing + * SPDX-License-Identifier: GPL-2.0-only + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +%{ + import element + #import xml.etree.ElementTree +%} + +%% + +class AST { + /* internal classes */ + class Text; + class DeclarationOrStatement; + class Statement: DeclarationOrStatement; + class Declarator; + class InitializerOrExpression; + class Expression: InitializerOrExpression; + class ExpressionUnary: Expression { + str unary_operator = ''; + bool postfix = False; + }; + class ExpressionBinary: Expression { + str binary_operator = ''; + int precedence = -1; + bool right_to_left = False; + }; + /* type analysis */ + class Type; + class TypeVoid: Type; + class TypeInt: Type { + bool signed = False; + int bits = -1; + }; + class TypeFloat: Type { + int complex = -1; + int bits = -1; + }; + class TypeBool: Type; + class TypePointer: Type { + ref target_type = None; + }; + class TypeArray: Type { + ref element_type = None; + int element_count = -1; + }; + class TypeFunction: Type { + class Argument { + ref type = None; + str name = ''; + }; + ref return_type = None; + bool varargs = False; + }; + /* syntax classes */ + class AlignAsExpression; + class AlignAsType; + class ArgumentExpressionList; + class BlockItemList; + class Declaration: DeclarationOrStatement; + class DeclarationList; + class DeclarationSpecifierList; + class DeclaratorAbstract: Declarator; + class DeclaratorArray: Declarator; + class DeclaratorEmpty: Declarator; + class DeclaratorFunction: Declarator { + bool varargs = False; + }; + class DeclaratorFunctionOldStyle: Declarator; + class DeclaratorIdentifier: Declarator; + class DeclaratorPointer: Declarator; + class DefaultTypeName; + class DesignatorField; + class DesignatorIndex; + class DesignatorInitializer: InitializerOrExpression; + class DesignatorInitializerList: InitializerOrExpression; + class DesignatorListEqualsEmpty; + class DesignatorList; + class EnumSpecifier; + class Enumerator; + class EnumeratorList; + class EqualsInitializerEmpty; + class ExpressionAdd: ExpressionBinary; + class ExpressionAddAssignment: ExpressionBinary; + class ExpressionAddressOf: ExpressionUnary; + class ExpressionAlignOfType: ExpressionUnary; + class ExpressionArray: Expression; + class ExpressionAssignment: ExpressionBinary; + class ExpressionAsterisk: Expression; + class ExpressionBitwiseAnd: ExpressionBinary; + class ExpressionBitwiseAndAssignment: ExpressionBinary; + class ExpressionBitwiseNot: ExpressionUnary; + class ExpressionBitwiseOr: ExpressionBinary; + class ExpressionBitwiseOrAssignment: ExpressionBinary; + class ExpressionCall: Expression; + class ExpressionCast: Expression; + class ExpressionCharConstant: Expression; + class ExpressionComma: ExpressionBinary; + class ExpressionConditional: Expression; + class ExpressionDereference: ExpressionUnary; + class ExpressionDivide: ExpressionBinary; + class ExpressionDivideAssignment: ExpressionBinary; + class ExpressionEmpty: Expression; + class ExpressionEqual: ExpressionBinary; + class ExpressionExclusiveOr: ExpressionBinary; + class ExpressionExclusiveOrAssignment: ExpressionBinary; + class ExpressionField: Expression; + class ExpressionFieldDereference: Expression; + class ExpressionFloatLiteral: Expression; + class ExpressionFunctionName: Expression; + class ExpressionGreaterThan: ExpressionBinary; + class ExpressionGreaterThanOrEqual: ExpressionBinary; + class ExpressionIdentifier: Expression; + class ExpressionIndex: Expression; + class ExpressionIntLiteral: Expression; + class ExpressionLeftShiftAssignment: ExpressionBinary; + class ExpressionLessThan: ExpressionBinary; + class ExpressionLessThanOrEqual: ExpressionBinary; + class ExpressionLogicalAnd: ExpressionBinary; + class ExpressionLogicalNot: ExpressionUnary; + class ExpressionLogicalOr: ExpressionBinary; + class ExpressionMinus: ExpressionUnary; + class ExpressionModulo: ExpressionBinary; + class ExpressionModuloAssignment: ExpressionBinary; + class ExpressionMultiply: ExpressionBinary; + class ExpressionMultiplyAssignment: ExpressionBinary; + class ExpressionNotEqual: ExpressionBinary; + class ExpressionPlus: ExpressionUnary; + class ExpressionPostDecrement: ExpressionUnary; + class ExpressionPostIncrement: ExpressionUnary; + class ExpressionPreDecrement: ExpressionUnary; + class ExpressionPreIncrement: ExpressionUnary; + class ExpressionRightShiftAssignment: ExpressionBinary; + class ExpressionShiftLeft: ExpressionBinary; + class ExpressionShiftRight: ExpressionBinary; + class ExpressionSizeOfExpression: ExpressionUnary; + class ExpressionSizeOfType: ExpressionUnary; + class ExpressionStringLiteral: Expression; + class ExpressionSubtract: ExpressionBinary; + class ExpressionSubtractAssignment: ExpressionBinary; + class FunctionDefinition: DeclarationOrStatement; + class FunctionSpecifier { + int n = -1; + }; + class GenericAssociation; + class GenericAssociationList; + class GenericSelection; + class Identifier; + class IdentifierEmpty; + class IdentifierList; + class InitDeclarator; + class InitDeclaratorList; + class ParameterDeclaration; + class ParameterDeclarationList; + class SpecifierQualifierList; + class StatementBlock: Statement; + class StatementBreak: Statement; + class StatementCase: Statement; + class StatementContinue: Statement; + class StatementDefault: Statement; + class StatementDoWhile: Statement; + class StatementExpression: Statement; + class StatementFor: Statement; + class StatementGoto: Statement; + class StatementIf: Statement; + class StatementIfElse: Statement; + class StatementLabel: Statement; + class StatementReturn: Statement; + class StatementSwitch: Statement; + class StatementWhile: Statement; + class StaticAssertDeclaration: DeclarationOrStatement; + class StorageClassSpecifier { + int n = -1; + }; + class StructDeclaration; + class StructDeclarationList; + class StructDeclarator; + class StructDeclaratorList; + class StructSpecifier; + class TypeName; + class TypeQualifier { + int n = -1; + }; + class TypeQualifierList; + class TypeQualifierOrStaticList; + class TypeSpecifier { + int n = -1; + }; + class UnionSpecifier; + class TranslationUnit; +}; + +%% + +class Context: + def __init__( + self, + indent = '', + lines = [], + top_level = True, + enclosing_loop = None, + translate_identifier = { + 'NULL': 'None', + 'false': 'False', + 'strlen': 'len', + 'true': 'True' + } + ): + self.indent = indent + self.lines = lines + self.top_level = top_level + self.enclosing_loop = enclosing_loop + self.translate_identifier = translate_identifier + +# void char short int long float double signed unsigned bool complex imaginary +type_specifiers_to_type = { + (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeVoid(), + (0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 8), + (0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 8), + (0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 8), + (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), + (0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), + (0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 16), + (0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), + (0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), + (0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 16), + (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), + (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), + (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), + (0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), + (0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), + (0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), + (0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), + (0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 64), + (0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), + (0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), + (0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 64), + (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0): AST.TypeFloat(complex = 0, bits = 32), + (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0): AST.TypeFloat(complex = 2, bits = 32), + (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1): AST.TypeFloat(complex = 1, bits = 32), + (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0): AST.TypeFloat(complex = 0, bits = 64), + (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0): AST.TypeFloat(complex = 1, bits = 64), + (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1): AST.TypeFloat(complex = 2, bits = 64), + (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0): AST.TypeBool() +} +octal_prefix = set( + ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'] +) + +def factory(tag, attrib = {}, *args, **kwargs): + return tag_to_class.get(tag, element.Element)(tag, attrib, *args, **kwargs) + +@method(AST.DeclarationOrStatement) +def translate_declaration_or_statement(self, context): + #text = element.to_text(self).strip() + #assert text[-1] == ';' + #context.lines.append( + # '{0:s}{1:s}\n'.format(context.indent, text[:-1]) + #) + print(self) + raise NotImplementedError +@method(AST.Declaration) +def translate_declaration_or_statement(self, context): + base_type = self[0].get_type() + for i in self[1]: + type, name = i[0].get_type_and_name(base_type) + if not isinstance(type, AST.TypeFunction): + context.lines.append( + '{0:s}{1:s} = {2:s}\n'.format( + context.indent, + name, + ( + type.translate_zero(context) + if isinstance(i[1], AST.EqualsInitializerEmpty) else + i[1].translate_initializer_or_expression(context) + ) + ) + ) +@method(AST.FunctionDefinition) +def translate_declaration_or_statement(self, context): + type, name = self[1].get_type_and_name(self[0].get_type()) + assert isinstance(type, AST.TypeFunction) + if len(context.lines): + context.lines.append('\n') + context.lines.append( + '{0:s}def {1:s}({2:s}):\n'.format( + context.indent, + name, + ', '.join([i.name for i in type]) + ) + ) + indent_save = context.indent + context.indent += ' ' + assert context.top_level + context.top_level = False + self[3].translate_block_item_list(context) + context.top_level = True + context.indent = indent_save +@method(AST.StatementBlock) +def translate_declaration_or_statement(self, context): + if len(self[0]): + self[0].translate_block_item_list(context) + else: + context.lines.append('{0:s}pass\n'.format(context.indent)) +@method(AST.StatementBreak) +def translate_declaration_or_statement(self, context): + context.lines.append('{0:s}break\n'.format(context.indent)) +@method(AST.StatementContinue) +def translate_declaration_or_statement(self, context): + enclosing_loop_save = context.enclosing_loop + context.enclosing_loop = None + if isinstance(enclosing_loop_save, AST.StatementDoWhile): + AST.StatementIfElse( + children = [ + enclosing_loop_save[1], + AST.StatementContinue(), + AST.StatementBreak() + ] + ).translate_declaration_or_statement(context) + elif isinstance(enclosing_loop_save, AST.StatementFor): + context.lines.append( + '{0:s}{1:s}\n{2:s}continue\n'.format( + context.indent, + enclosing_loop_save[2].translate_statement_expression(context), + context.indent + ) + ) + else: + context.lines.append('{0:s}continue\n'.format(context.indent)) + context.enclosing_loop = enclosing_loop_save +@method(AST.StatementDoWhile) +def translate_declaration_or_statement(self, context): + if ( + isinstance(self[1], AST.ExpressionIntLiteral) and + element.get_text(self[1], 0) == '0' + ): + self[0].translate_declaration_or_statement(context) + return + context.lines.append('{0:s}while True:\n'.format(context.indent)) + indent_save = context.indent + context.indent += ' ' + enclosing_loop_save = context.enclosing_loop + context.enclosing_loop = self + self[0].translate_declaration_or_statement(context) + context.enclosing_loop = enclosing_loop_save + AST.StatementIf( + children = [ + AST.ExpressionLogicalNot( + children = [ + self[1] + ], + unary_operator = 'not ' + ), + AST.StatementBreak() + ] + ).translate_declaration_or_statement(context) + context.indent = indent_save +@method(AST.StatementExpression) +def translate_declaration_or_statement(self, context): + context.lines.append( + '{0:s}{1:s}\n'.format( + context.indent, + self[0].translate_statement_expression(context) + ) + ) +@method(AST.StatementFor) +def translate_declaration_or_statement(self, context): + self[0].translate_declaration_or_statement(context) + context.lines.append( + '{0:s}while {1:s}:\n'.format( + context.indent, + self[1].translate_expression(context, 0) + ) + ) + indent_save = context.indent + context.indent += ' ' + enclosing_loop_save = context.enclosing_loop + context.enclosing_loop = self + self[3].translate_declaration_or_statement(context) + context.enclosing_loop = enclosing_loop_save + context.lines.append( + '{0:s}{1:s}\n'.format( + context.indent, + self[2].translate_statement_expression(context) + ) + ) + context.indent = indent_save +@method(AST.StatementIf) +def translate_declaration_or_statement(self, context): + context.lines.append( + '{0:s}if {1:s}:\n'.format( + context.indent, + self[0].translate_expression(context, 0) + ) + ) + indent_save = context.indent + context.indent += ' ' + self[1].translate_declaration_or_statement(context) + context.indent = indent_save +@method(AST.StatementIfElse) +def translate_declaration_or_statement(self, context): + context.lines.append( + '{0:s}if {1:s}:\n'.format( + context.indent, + self[0].translate_expression(context, 0) + ) + ) + indent_save = context.indent + context.indent += ' ' + self[1].translate_declaration_or_statement(context) + context.lines.append('{0:s}else:\n'.format(indent_save)) + self[2].translate_declaration_or_statement(context) + context.indent = indent_save +@method(AST.StatementReturn) +def translate_declaration_or_statement(self, context): + context.lines.append( + '{0:s}return {1:s}\n'.format( + context.indent, + self[0].translate_expression(context, 0) + ) + ) +@method(AST.StatementSwitch) +def translate_declaration_or_statement(self, context): + assert isinstance(self[1], AST.StatementBlock) + indent_save = context.indent + context.indent += ' ' + if_text = 'if' + for i in self[1][0]: + if ( + isinstance(i, AST.StatementCase) or + isinstance(i, AST.StatementDefault) + ): + if isinstance(i, AST.StatementCase): + cond_expr = AST.ExpressionEqual( + children = [ + self[0], + i[0] + ], + binary_operator = ' == ', + precedence = 8 + ) + j = i[1] + else: + cond_expr = None + j = i[0] + if ( + isinstance(j, AST.StatementCase) or + isinstance(j, AST.StatementDefault) + ): + if isinstance(j, AST.StatementCase): + if cond_expr is not None: + cond_expr = AST.ExpressionLogicalOr( + children = [ + cond_expr, + AST.ExpressionEqual( + children = [ + self[0], + j[0] + ], + binary_operator = ' == ', + precedence = 8 + ) + ], + binary_operator = ' or ', + precedence = 3 + ) + j = j[1] + else: + cond_expr = None + j = j[0] + if cond_expr is not None: + assert if_text is not None + context.lines.append( + '{0:s}{1:s} {2:s}:\n'.format( + indent_save, + if_text, + cond_expr.translate_expression(context, 0) + ) + ) + if_text = 'elif' + else: + context.lines.append( + '{0:s}else:\n'.format(indent_save) + ) + if_text = None + j.translate_declaration_or_statement(context) + else: + i.translate_declaration_or_statement(context) + context.indent = indent_save +@method(AST.StatementWhile) +def translate_declaration_or_statement(self, context): + context.lines.append( + '{0:s}while {1:s}:\n'.format( + context.indent, + self[0].translate_expression(context, 0) + ) + ) + indent_save = context.indent + context.indent += ' ' + enclosing_loop_save = context.enclosing_loop + context.enclosing_loop = self + self[1].translate_declaration_or_statement(context) + context.enclosing_loop = enclosing_loop_save + context.indent = indent_save +@method(AST.StaticAssertDeclaration) +def translate_declaration_or_statement(self, context): + pass +del translate_declaration_or_statement + +@method(AST.Declarator) +def get_type_and_name(self, base_type): + print(self) + raise NotImplementedError +@method(AST.DeclaratorAbstract) +def get_type_and_name(self, base_type): + return base_type, '' +@method(AST.DeclaratorArray) +def get_type_and_name(self, base_type): + return self[0].get_type_and_name( + AST.TypeArray( + element_type = base_type, + element_count = ( + -1 + if isinstance(self[2], AST.ExpressionEmpty) else + # kludgey way, assuming not calculated size + int( + element.get_text(self[2], 0), + 8 if element.get_text(self[2], 0)[:2] in octal_prefix else 0 + ) + ) + ) + ) +@method(AST.DeclaratorFunction) +def get_type_and_name(self, base_type): + children = [] + for i in self[1]: + type, name = i[1].get_type_and_name(i[0].get_type()) + children.append(AST.TypeFunction.Argument(type = type, name = name)) + return self[0].get_type_and_name( + AST.TypeFunction( + children = children, + return_type = base_type, + varargs = self.varargs, + ) + ) +@method(AST.DeclaratorIdentifier) +def get_type_and_name(self, base_type): + return base_type, element.get_text(self[0], 0) +@method(AST.DeclaratorPointer) +def get_type_and_name(self, base_type): + return self[1].get_type_and_name( + AST.TypePointer(target_type = base_type) + ) +del get_type_and_name + +@method(AST.InitializerOrExpression) +def translate_initializer_or_expression(self, context): + print(self) + raise NotImplementedError +@method(AST.Expression) +def translate_initializer_or_expression(self, context): + return self.translate_expression(context, 0) +@method(AST.DesignatorInitializer) +def translate_initializer_or_expression(self, context): + assert isinstance(self[0], AST.DesignatorListEqualsEmpty) + return self[1].translate_initializer_or_expression(context) +@method(AST.DesignatorInitializerList) +def translate_initializer_or_expression(self, context): + return '[{0:s}]'.format( + ', '.join( + [ + i.translate_initializer_or_expression(context) + for i in self + ] + ) + ) +del translate_initializer_or_expression + +@method(AST.Expression) +def translate_expression(self, context, precedence): + #return element.to_text(self).strip() + print(self) + raise NotImplementedError +@method(AST.ExpressionUnary) +def translate_expression(self, context, precedence): + if self.postfix: + text = '{0:s}{1:s}'.format( + self[0].translate_expression(context, 14), + self.unary_operator + ) + if 14 < precedence: + text = '({0:s})'.format(text) + else: + text = '{0:s}{1:s}'.format( + self.unary_operator, + self[0].translate_expression(context, 13) + ) + if 13 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionBinary) +def translate_expression(self, context, precedence): + text = '{0:s}{1:s}{2:s}'.format( + self[0].translate_expression( + context, + self.precedence + int(self.right_to_left) + ), + self.binary_operator, + self[1].translate_expression( + context, + self.precedence + int(not self.right_to_left) + ) + ) + if self.precedence < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionCall) +def translate_expression(self, context, precedence): + text = '{0:s}({1:s})'.format( + self[0].translate_expression(context, 14), + ', '.join([i.translate_expression(context, 1) for i in self[1]]) + ) + if 14 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionCast) +def translate_expression(self, context, precedence): + type, _ = self[0][1].get_type_and_name(self[0][0].get_type()) + text = '{0:s}({1:s})'.format( + type.translate_type(context), + self[1].translate_expression(context, 0) + ) + if 14 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionCharConstant) +def translate_expression(self, context, precedence): + return 'ord(\'{0:s}\')'.format(element.get_text(self[0], 0)) +@method(AST.ExpressionConditional) +def translate_expression(self, context, precedence): + text = '{0:s} if {1:s} else {2:s}'.format( + self[1].translate_expression(context, 3), + self[0].translate_expression(context, 0), + self[2].translate_expression(context, 2) + ) + if 2 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionEmpty) +def translate_expression(self, context, precedence): + return 'True' +@method(AST.ExpressionField) +def translate_expression(self, context, precedence): + text = '{0:s}.{1:s}'.format( + self[0].translate_expression(context, 14), + self[1].translate_identifier(context) + ) + if 14 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionFieldDereference) +def translate_expression(self, context, precedence): + text = '{0:s}->{1:s}'.format( + self[0].translate_expression(context, 14), + self[1].translate_identifier(context) + ) + if 14 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionIdentifier) +def translate_expression(self, context, precedence): + return self[0].translate_identifier(context) +@method(AST.ExpressionIndex) +def translate_expression(self, context, precedence): + text = '{0:s}[{1:s}]'.format( + self[0].translate_expression(context, 14), + self[1].translate_expression(context, 0) + ) + if 14 < precedence: + text = '({0:s})'.format(text) + return text +@method(AST.ExpressionIntLiteral) +def translate_expression(self, context, precedence): + text = element.get_text(self, 0) + if text[:2] in octal_prefix: + text = '0o' + text[1:] + return text +@method(AST.ExpressionSizeOfType) +def translate_expression(self, context, precedence): + type, _ = self[0][1].get_type_and_name(self[0][0].get_type()) + return str(type.translate_size(context)) +@method(AST.ExpressionStringLiteral) +def translate_expression(self, context, precedence): + return ' '.join( + [ + '\'{0:s}\''.format( + element.get_text(i, 0). + replace('\\"', '"'). + replace('\'', '\\\'') + ) + for i in self + ] + ) +del translate_expression + +@method(AST.Expression) +def translate_statement_expression(self, context): + return self.translate_expression(context, 0) +@method(AST.ExpressionEmpty) +def translate_statement_expression(self, context): + return 'pass' +@method(AST.ExpressionPostDecrement) +def translate_statement_expression(self, context): + return '{0:s} -= 1'.format(self[0].translate_expression(context, 0)) +@method(AST.ExpressionPostIncrement) +def translate_statement_expression(self, context): + return '{0:s} += 1'.format(self[0].translate_expression(context, 0)) +@method(AST.ExpressionPreDecrement) +def translate_statement_expression(self, context): + return '{0:s} -= 1'.format(self[0].translate_expression(context, 0)) +@method(AST.ExpressionPreIncrement) +def translate_statement_expression(self, context): + return '{0:s} += 1'.format(self[0].translate_expression(context, 0)) +del translate_statement_expression + +@method(AST.Type) +def translate_size(self, context): + print(self) + raise NotImplementedError +@method(AST.TypeVoid) +def translate_size(self, context): + return 0 +@method(AST.TypeInt) +def translate_size(self, context): + return (self.bits + 7) // 8 +@method(AST.TypeFloat) +def translate_size(self, context): + return (self.bits + 7) // 8 +@method(AST.TypeBool) +def translate_size(self, context): + return 1 +@method(AST.TypePointer) +def translate_size(self, context): + return 4 +@method(AST.TypeArray) +def translate_size(self, context): + return self.element_type.translate_type(context) * self.element_count +del translate_size + +@method(AST.Type) +def translate_type(self, context): + print(self) + raise NotImplementedError +@method(AST.TypeVoid) +def translate_type(self, context): + return 'NoneType' +@method(AST.TypeInt) +def translate_type(self, context): + return 'int' +@method(AST.TypeFloat) +def translate_type(self, context): + return 'float' +@method(AST.TypeBool) +def translate_type(self, context): + return 'bool' +@method(AST.TypePointer) +def translate_type(self, context): + return ( + 'str' + if ( + isinstance(self.target_type, AST.TypeInt) and + self.target_type.bits == 8 + ) else + 'list' + ) +del translate_type + +@method(AST.Type) +def translate_zero(self, context): + print(self) + raise NotImplementedError +@method(AST.TypeVoid) +def translate_zero(self, context): + return 'None' +@method(AST.TypeInt) +def translate_zero(self, context): + return '0' if context.top_level else 'None' +@method(AST.TypeFloat) +def translate_zero(self, context): + return '0.' if context.top_level else 'None' +@method(AST.TypeBool) +def translate_zero(self, context): + return 'False' if context.top_level else 'None' +@method(AST.TypePointer) +def translate_zero(self, context): + return ( + ( + '\'\'' + if ( + isinstance(self.target_type, AST.TypeInt) and + self.target_type.bits == 8 + ) else + '[]' + ) + if context.top_level else + 'None' + ) +@method(AST.TypeArray) +def translate_zero(self, context): + return '[{0:s}]'.format( + ', '.join( + [self.element_type.translate_zero(context)] * self.element_count + ) + ) +del translate_zero + +@method(AST.TypeVoid) +def __str__(self): + return 'void' +@method(AST.TypeInt) +def __str__(self): + return '{0:s}int{1:d}'.format(['u', ''][int(self.signed)], self.bits) +@method(AST.TypeFloat) +def __str__(self): + return '{0:s}float{0:d}'.format( + ['', 'i', 'c'][int(self.complex)], + self.bits + ) +@method(AST.TypeBool) +def __str__(self): + return 'bool' +@method(AST.TypePointer) +def __str__(self): + return 'pointer<{0:s}>'.format(str(self.target_type)) +@method(AST.TypeArray) +def __str__(self): + return 'array<{0:s}: {1:s}>'.format( + str(self.element_type), + ( + 'UNSPECIFIED' + if self.element_count == -1 else + 'C99_FLEXIBLE' + if self.element_count == -2 else + str(self.element_count) + ) + ) +@method(AST.TypeFunction.Argument) +def __str__(self): + return '{0:s} {1:s}'.format( + str(self.type), + 'ABSTRACT' if self.name == '' else self.name + ) +@method(AST.TypeFunction) +def __str__(self): + return 'function<{0:s} -> {1:s}>'.format( + ( + 'void' + if len(self) == 0 else + ', '.join( + [str(i) for i in self] + (['...'] if self.varargs else []) + ) + ), + str(self.return_type) + ) +del __str__ + +@method(AST.BlockItemList) +def translate_block_item_list(self, context): + for i in self: + i.translate_declaration_or_statement(context) +del translate_block_item_list + +@method(AST.DeclarationSpecifierList) +def get_type(self): + type_specifiers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + for i in self: + if isinstance(i, AST.TypeSpecifier): + type_specifiers[i.n] += 1 + return type_specifiers_to_type[tuple(type_specifiers)] +@method(AST.SpecifierQualifierList) +def get_type(self): + type_specifiers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + for i in self: + if isinstance(i, AST.TypeSpecifier): + type_specifiers[i.n] += 1 + return type_specifiers_to_type[tuple(type_specifiers)] +del get_type + +@method(AST.Identifier) +def translate_identifier(self, context): + text = element.get_text(self, 0) + return context.translate_identifier.get(text, text) +del translate_identifier + +@method(AST.TranslationUnit) +def translate_translation_unit(self, context): + for i in self: + i.translate_declaration_or_statement(context) +del translate_translation_unit diff --git a/ansi_c.y b/ansi_c.y index 734aa81..11dd954 100644 --- a/ansi_c.y +++ b/ansi_c.y @@ -20,13 +20,13 @@ %expect 2 %{ -import ast +import t_def %} %% primary_expression - : (?E{ast.AST.ExpressionIdentifier}IDENTIFIER) + : (?E{t_def.AST.ExpressionIdentifier}IDENTIFIER) | constant | string | '(' expression ')' @@ -45,11 +45,11 @@ enumeration_constant string : STRING_LITERAL - | (?E{ast.AST.ExpressionFunctionName}FUNC_NAME) + | (?E{t_def.AST.ExpressionFunctionName}FUNC_NAME) ; generic_selection - : (?E{ast.AST.GenericSelection}GENERIC '(' assignment_expression ',' (?E{ast.AST.GenericAssociationList}generic_association_list) ')') + : (?E{t_def.AST.GenericSelection}GENERIC '(' assignment_expression ',' (?E{t_def.AST.GenericAssociationList}generic_association_list) ')') ; generic_association_list @@ -58,23 +58,23 @@ generic_association_list ; generic_association - : (?E{ast.AST.GenericAssociation}type_name_or_default ':' assignment_expression) + : (?E{t_def.AST.GenericAssociation}type_name_or_default ':' assignment_expression) ; type_name_or_default : type_name - | (?E{ast.AST.DefaultTypeName}DEFAULT) + | (?E{t_def.AST.DefaultTypeName}DEFAULT) ; postfix_expression : primary_expression - | (?E{ast.AST.ExpressionIndex}postfix_expression '[' expression ']') - | (?E{ast.AST.ExpressionCall}postfix_expression '(' (?E{ast.AST.ArgumentExpressionList}argument_expression_list_opt) ')') - | (?E{ast.AST.ExpressionField}postfix_expression '.' IDENTIFIER) - | (?E{ast.AST.ExpressionFieldDereference}postfix_expression PTR_OP IDENTIFIER) - | (?E{ast.AST.ExpressionPostIncrement, unary_operator = '++', postfix = True}postfix_expression INC_OP) - | (?E{ast.AST.ExpressionPostDecrement, unary_operator = '--', postfix = True}postfix_expression DEC_OP) - | (?E{ast.AST.ExpressionArray}'(' type_name ')' '{' (?E{ast.AST.DesignatorInitializerList}designator_initializer_list_comma_opt) '}') + | (?E{t_def.AST.ExpressionIndex}postfix_expression '[' expression ']') + | (?E{t_def.AST.ExpressionCall}postfix_expression '(' (?E{t_def.AST.ArgumentExpressionList}argument_expression_list_opt) ')') + | (?E{t_def.AST.ExpressionField}postfix_expression '.' IDENTIFIER) + | (?E{t_def.AST.ExpressionFieldDereference}postfix_expression PTR_OP IDENTIFIER) + | (?E{t_def.AST.ExpressionPostIncrement, unary_operator = '++', postfix = True}postfix_expression INC_OP) + | (?E{t_def.AST.ExpressionPostDecrement, unary_operator = '--', postfix = True}postfix_expression DEC_OP) + | (?E{t_def.AST.ExpressionArray}'(' type_name ')' '{' (?E{t_def.AST.DesignatorInitializerList}designator_initializer_list_comma_opt) '}') ; argument_expression_list_opt @@ -89,120 +89,120 @@ argument_expression_list unary_expression : postfix_expression - | (?E{ast.AST.ExpressionPreIncrement, unary_operator = '++'}INC_OP unary_expression) - | (?E{ast.AST.ExpressionPreDecrement, unary_operator = '--'}DEC_OP unary_expression) - | (?E{ast.AST.ExpressionAddressOf, unary_operator = '&'}'&' cast_expression) - | (?E{ast.AST.ExpressionDereference, unary_operator = '*'}'*' cast_expression) - | (?E{ast.AST.ExpressionPlus, unary_operator = '+'}'+' cast_expression) - | (?E{ast.AST.ExpressionMinus, unary_operator = '-'}'-' cast_expression) - | (?E{ast.AST.ExpressionBitwiseNot, unary_operator = '~'}'~' cast_expression) - | (?E{ast.AST.ExpressionLogicalNot, unary_operator = 'not '}'!' cast_expression) - | (?E{ast.AST.ExpressionSizeOfExpression, unary_operator = 'sizeof '}SIZEOF unary_expression) - | (?E{ast.AST.ExpressionSizeOfType}SIZEOF '(' type_name ')') - | (?E{ast.AST.ExpressionAlignOfType}ALIGNOF '(' type_name ')') + | (?E{t_def.AST.ExpressionPreIncrement, unary_operator = '++'}INC_OP unary_expression) + | (?E{t_def.AST.ExpressionPreDecrement, unary_operator = '--'}DEC_OP unary_expression) + | (?E{t_def.AST.ExpressionAddressOf, unary_operator = '&'}'&' cast_expression) + | (?E{t_def.AST.ExpressionDereference, unary_operator = '*'}'*' cast_expression) + | (?E{t_def.AST.ExpressionPlus, unary_operator = '+'}'+' cast_expression) + | (?E{t_def.AST.ExpressionMinus, unary_operator = '-'}'-' cast_expression) + | (?E{t_def.AST.ExpressionBitwiseNot, unary_operator = '~'}'~' cast_expression) + | (?E{t_def.AST.ExpressionLogicalNot, unary_operator = 'not '}'!' cast_expression) + | (?E{t_def.AST.ExpressionSizeOfExpression, unary_operator = 'sizeof '}SIZEOF unary_expression) + | (?E{t_def.AST.ExpressionSizeOfType}SIZEOF '(' type_name ')') + | (?E{t_def.AST.ExpressionAlignOfType}ALIGNOF '(' type_name ')') ; cast_expression : unary_expression - | (?E{ast.AST.ExpressionCast}'(' type_name ')' cast_expression) + | (?E{t_def.AST.ExpressionCast}'(' type_name ')' cast_expression) ; multiplicative_expression : cast_expression - | (?E{ast.AST.ExpressionMultiply, binary_operator = ' * ', precedence = 12}multiplicative_expression '*' cast_expression) - | (?E{ast.AST.ExpressionDivide, binary_operator = ' / ', precedence = 12}multiplicative_expression '/' cast_expression) - | (?E{ast.AST.ExpressionModulo, binary_operator = ' % ', precedence = 12}multiplicative_expression '%' cast_expression) + | (?E{t_def.AST.ExpressionMultiply, binary_operator = ' * ', precedence = 12}multiplicative_expression '*' cast_expression) + | (?E{t_def.AST.ExpressionDivide, binary_operator = ' / ', precedence = 12}multiplicative_expression '/' cast_expression) + | (?E{t_def.AST.ExpressionModulo, binary_operator = ' % ', precedence = 12}multiplicative_expression '%' cast_expression) ; additive_expression : multiplicative_expression - | (?E{ast.AST.ExpressionAdd, binary_operator = ' + ', precedence = 11}additive_expression '+' multiplicative_expression) - | (?E{ast.AST.ExpressionSubtract, binary_operator = ' - ', precedence = 11}additive_expression '-' multiplicative_expression) + | (?E{t_def.AST.ExpressionAdd, binary_operator = ' + ', precedence = 11}additive_expression '+' multiplicative_expression) + | (?E{t_def.AST.ExpressionSubtract, binary_operator = ' - ', precedence = 11}additive_expression '-' multiplicative_expression) ; shift_expression : additive_expression - | (?E{ast.AST.ExpressionShiftLeft, binary_operator = ' << ', precedence = 10}shift_expression LEFT_OP additive_expression) - | (?E{ast.AST.ExpressionShiftRight, binary_operator = ' >> ', precedence = 10}shift_expression RIGHT_OP additive_expression) + | (?E{t_def.AST.ExpressionShiftLeft, binary_operator = ' << ', precedence = 10}shift_expression LEFT_OP additive_expression) + | (?E{t_def.AST.ExpressionShiftRight, binary_operator = ' >> ', precedence = 10}shift_expression RIGHT_OP additive_expression) ; relational_expression : shift_expression - | (?E{ast.AST.ExpressionLessThan, binary_operator = ' < ', precedence = 9}relational_expression '<' shift_expression) - | (?E{ast.AST.ExpressionGreaterThan, binary_operator = ' > ', precedence = 9}relational_expression '>' shift_expression) - | (?E{ast.AST.ExpressionLessThanOrEqual, binary_operator = ' <= ', precedence = 9}relational_expression LE_OP shift_expression) - | (?E{ast.AST.ExpressionGreaterThanOrEqual, binary_operator = ' >= ', precedence = 9}relational_expression GE_OP shift_expression) + | (?E{t_def.AST.ExpressionLessThan, binary_operator = ' < ', precedence = 9}relational_expression '<' shift_expression) + | (?E{t_def.AST.ExpressionGreaterThan, binary_operator = ' > ', precedence = 9}relational_expression '>' shift_expression) + | (?E{t_def.AST.ExpressionLessThanOrEqual, binary_operator = ' <= ', precedence = 9}relational_expression LE_OP shift_expression) + | (?E{t_def.AST.ExpressionGreaterThanOrEqual, binary_operator = ' >= ', precedence = 9}relational_expression GE_OP shift_expression) ; equality_expression : relational_expression - | (?E{ast.AST.ExpressionEqual, binary_operator = ' == ', precedence = 8}equality_expression EQ_OP relational_expression) - | (?E{ast.AST.ExpressionNotEqual, binary_operator = ' != ', precedence = 8}equality_expression NE_OP relational_expression) + | (?E{t_def.AST.ExpressionEqual, binary_operator = ' == ', precedence = 8}equality_expression EQ_OP relational_expression) + | (?E{t_def.AST.ExpressionNotEqual, binary_operator = ' != ', precedence = 8}equality_expression NE_OP relational_expression) ; and_expression : equality_expression - | (?E{ast.AST.ExpressionBitwiseAnd, binary_operator = ' & ', precedence = 7}and_expression '&' equality_expression) + | (?E{t_def.AST.ExpressionBitwiseAnd, binary_operator = ' & ', precedence = 7}and_expression '&' equality_expression) ; exclusive_or_expression : and_expression - | (?E{ast.AST.ExpressionExclusiveOr, binary_operator = ' ^ ', precedence = 6}exclusive_or_expression '^' and_expression) + | (?E{t_def.AST.ExpressionExclusiveOr, binary_operator = ' ^ ', precedence = 6}exclusive_or_expression '^' and_expression) ; inclusive_or_expression : exclusive_or_expression - | (?E{ast.AST.ExpressionBitwiseOr, binary_operator = ' | ', precedence = 5}inclusive_or_expression '|' exclusive_or_expression) + | (?E{t_def.AST.ExpressionBitwiseOr, binary_operator = ' | ', precedence = 5}inclusive_or_expression '|' exclusive_or_expression) ; logical_and_expression : inclusive_or_expression - | (?E{ast.AST.ExpressionLogicalAnd, binary_operator = ' and ', precedence = 4}logical_and_expression AND_OP inclusive_or_expression) + | (?E{t_def.AST.ExpressionLogicalAnd, binary_operator = ' and ', precedence = 4}logical_and_expression AND_OP inclusive_or_expression) ; logical_or_expression : logical_and_expression - | (?E{ast.AST.ExpressionLogicalOr, binary_operator = ' or ', precedence = 3}logical_or_expression OR_OP logical_and_expression) + | (?E{t_def.AST.ExpressionLogicalOr, binary_operator = ' or ', precedence = 3}logical_or_expression OR_OP logical_and_expression) ; conditional_expression : logical_or_expression - | (?E{ast.AST.ExpressionConditional}logical_or_expression '?' expression ':' conditional_expression) + | (?E{t_def.AST.ExpressionConditional}logical_or_expression '?' expression ':' conditional_expression) ; assignment_expression_or_asterisk_opt - : (?E{ast.AST.ExpressionEmpty}) - | (?E{ast.AST.ExpressionAsterisk}'*') + : (?E{t_def.AST.ExpressionEmpty}) + | (?E{t_def.AST.ExpressionAsterisk}'*') | assignment_expression ; assignment_expression : conditional_expression - | (?E{ast.AST.ExpressionAssignment, binary_operator = ' = ', precedence = 1, right_to_left = True}unary_expression '=' assignment_expression) - | (?E{ast.AST.ExpressionMultiplyAssignment, binary_operator = ' *= ', precedence = 1, right_to_left = True}unary_expression MUL_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionDivideAssignment, binary_operator = ' /= ', precedence = 1, right_to_left = True}unary_expression DIV_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionModuloAssignment, binary_operator = ' %= ', precedence = 1, right_to_left = True}unary_expression MOD_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionAddAssignment, binary_operator = ' += ', precedence = 1, right_to_left = True}unary_expression ADD_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionSubtractAssignment, binary_operator = ' -= ', precedence = 1, right_to_left = True}unary_expression SUB_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionLeftShiftAssignment, binary_operator = ' <<= ', precedence = 1, right_to_left = True}unary_expression LEFT_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionRightShiftAssignment, binary_operator = ' >>= ', precedence = 1, right_to_left = True}unary_expression RIGHT_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionBitwiseAndAssignment, binary_operator = ' &= ', precedence = 1, right_to_left = True}unary_expression AND_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionExclusiveOrAssignment, binary_operator = ' ^= ', precedence = 1, right_to_left = True}unary_expression XOR_ASSIGN assignment_expression) - | (?E{ast.AST.ExpressionBitwiseOrAssignment, binary_operator = ' |= ', precedence = 1, right_to_left = True}unary_expression OR_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionAssignment, binary_operator = ' = ', precedence = 1, right_to_left = True}unary_expression '=' assignment_expression) + | (?E{t_def.AST.ExpressionMultiplyAssignment, binary_operator = ' *= ', precedence = 1, right_to_left = True}unary_expression MUL_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionDivideAssignment, binary_operator = ' /= ', precedence = 1, right_to_left = True}unary_expression DIV_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionModuloAssignment, binary_operator = ' %= ', precedence = 1, right_to_left = True}unary_expression MOD_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionAddAssignment, binary_operator = ' += ', precedence = 1, right_to_left = True}unary_expression ADD_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionSubtractAssignment, binary_operator = ' -= ', precedence = 1, right_to_left = True}unary_expression SUB_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionLeftShiftAssignment, binary_operator = ' <<= ', precedence = 1, right_to_left = True}unary_expression LEFT_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionRightShiftAssignment, binary_operator = ' >>= ', precedence = 1, right_to_left = True}unary_expression RIGHT_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionBitwiseAndAssignment, binary_operator = ' &= ', precedence = 1, right_to_left = True}unary_expression AND_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionExclusiveOrAssignment, binary_operator = ' ^= ', precedence = 1, right_to_left = True}unary_expression XOR_ASSIGN assignment_expression) + | (?E{t_def.AST.ExpressionBitwiseOrAssignment, binary_operator = ' |= ', precedence = 1, right_to_left = True}unary_expression OR_ASSIGN assignment_expression) ; expression_opt - : (?E{ast.AST.ExpressionEmpty}) + : (?E{t_def.AST.ExpressionEmpty}) | expression ; expression : assignment_expression - | (?E{ast.AST.ExpressionComma, binary_operator = ', ', precedence = 0}expression ',' assignment_expression) + | (?E{t_def.AST.ExpressionComma, binary_operator = ', ', precedence = 0}expression ',' assignment_expression) ; equals_constant_expression_opt - : (?E{ast.AST.ExpressionEmpty}) + : (?E{t_def.AST.ExpressionEmpty}) | '=' constant_expression ; @@ -211,7 +211,7 @@ constant_expression ; declaration - : (?E{ast.AST.Declaration}(?E{ast.AST.DeclarationSpecifierList}declaration_specifier_list) (?E{ast.AST.InitDeclaratorList}init_declarator_list_opt) ';') + : (?E{t_def.AST.Declaration}(?E{t_def.AST.DeclarationSpecifierList}declaration_specifier_list) (?E{t_def.AST.InitDeclaratorList}init_declarator_list_opt) ';') | static_assert_declaration ; @@ -239,31 +239,31 @@ init_declarator_list ; init_declarator - : (?E{ast.AST.InitDeclarator}declarator equals_initializer_opt) + : (?E{t_def.AST.InitDeclarator}declarator equals_initializer_opt) ; storage_class_specifier - : (?E{ast.AST.StorageClassSpecifier, n = 0}TYPEDEF) - | (?E{ast.AST.StorageClassSpecifier, n = 1}EXTERN) - | (?E{ast.AST.StorageClassSpecifier, n = 2}STATIC) - | (?E{ast.AST.StorageClassSpecifier, n = 3}THREAD_LOCAL) - | (?E{ast.AST.StorageClassSpecifier, n = 4}AUTO) - | (?E{ast.AST.StorageClassSpecifier, n = 5}REGISTER) + : (?E{t_def.AST.StorageClassSpecifier, n = 0}TYPEDEF) + | (?E{t_def.AST.StorageClassSpecifier, n = 1}EXTERN) + | (?E{t_def.AST.StorageClassSpecifier, n = 2}STATIC) + | (?E{t_def.AST.StorageClassSpecifier, n = 3}THREAD_LOCAL) + | (?E{t_def.AST.StorageClassSpecifier, n = 4}AUTO) + | (?E{t_def.AST.StorageClassSpecifier, n = 5}REGISTER) ; type_specifier - : (?E{ast.AST.TypeSpecifier, n = 0}VOID) - | (?E{ast.AST.TypeSpecifier, n = 1}CHAR) - | (?E{ast.AST.TypeSpecifier, n = 2}SHORT) - | (?E{ast.AST.TypeSpecifier, n = 3}INT) - | (?E{ast.AST.TypeSpecifier, n = 4}LONG) - | (?E{ast.AST.TypeSpecifier, n = 5}FLOAT) - | (?E{ast.AST.TypeSpecifier, n = 6}DOUBLE) - | (?E{ast.AST.TypeSpecifier, n = 7}SIGNED) - | (?E{ast.AST.TypeSpecifier, n = 8}UNSIGNED) - | (?E{ast.AST.TypeSpecifier, n = 9}BOOL) - | (?E{ast.AST.TypeSpecifier, n = 10}COMPLEX) - | (?E{ast.AST.TypeSpecifier, n = 11}IMAGINARY) + : (?E{t_def.AST.TypeSpecifier, n = 0}VOID) + | (?E{t_def.AST.TypeSpecifier, n = 1}CHAR) + | (?E{t_def.AST.TypeSpecifier, n = 2}SHORT) + | (?E{t_def.AST.TypeSpecifier, n = 3}INT) + | (?E{t_def.AST.TypeSpecifier, n = 4}LONG) + | (?E{t_def.AST.TypeSpecifier, n = 5}FLOAT) + | (?E{t_def.AST.TypeSpecifier, n = 6}DOUBLE) + | (?E{t_def.AST.TypeSpecifier, n = 7}SIGNED) + | (?E{t_def.AST.TypeSpecifier, n = 8}UNSIGNED) + | (?E{t_def.AST.TypeSpecifier, n = 9}BOOL) + | (?E{t_def.AST.TypeSpecifier, n = 10}COMPLEX) + | (?E{t_def.AST.TypeSpecifier, n = 11}IMAGINARY) | atomic_type_specifier | struct_specifier | union_specifier @@ -272,13 +272,13 @@ type_specifier ; struct_specifier - : (?E{ast.AST.StructSpecifier}STRUCT identifier_opt '{' (?E{ast.AST.StructDeclarationList}struct_declaration_list_opt) '}') - | (?E{ast.AST.StructSpecifier}STRUCT IDENTIFIER) + : (?E{t_def.AST.StructSpecifier}STRUCT identifier_opt '{' (?E{t_def.AST.StructDeclarationList}struct_declaration_list_opt) '}') + | (?E{t_def.AST.StructSpecifier}STRUCT IDENTIFIER) ; union_specifier - : (?E{ast.AST.UnionSpecifier}UNION identifier_opt '{' (?E{ast.AST.StructDeclarationList}struct_declaration_list_opt) '}') - | (?E{ast.AST.UnionSpecifier}UNION IDENTIFIER) + : (?E{t_def.AST.UnionSpecifier}UNION identifier_opt '{' (?E{t_def.AST.StructDeclarationList}struct_declaration_list_opt) '}') + | (?E{t_def.AST.UnionSpecifier}UNION IDENTIFIER) ; struct_declaration_list_opt @@ -292,7 +292,7 @@ struct_declaration_list ; struct_declaration - : (?E{ast.AST.StructDeclaration}(?E{ast.AST.SpecifierQualifierList}specifier_qualifier_list) (?E{ast.AST.StructDeclaratorList}struct_declarator_list_opt) ';') + : (?E{t_def.AST.StructDeclaration}(?E{t_def.AST.SpecifierQualifierList}specifier_qualifier_list) (?E{t_def.AST.StructDeclaratorList}struct_declarator_list_opt) ';') | static_assert_declaration ; @@ -317,13 +317,13 @@ struct_declarator_list ; struct_declarator - : (?E{ast.AST.StructDeclarator}declarator_opt ':' constant_expression) + : (?E{t_def.AST.StructDeclarator}declarator_opt ':' constant_expression) | declarator ; enum_specifier - : (?E{ast.AST.EnumSpecifier}ENUM identifier_opt '{' (?E{ast.AST.EnumeratorList}enumerator_list_comma_opt) '}') - | (?E{ast.AST.EnumSpecifier}ENUM IDENTIFIER) + : (?E{t_def.AST.EnumSpecifier}ENUM identifier_opt '{' (?E{t_def.AST.EnumeratorList}enumerator_list_comma_opt) '}') + | (?E{t_def.AST.EnumSpecifier}ENUM IDENTIFIER) ; enumerator_list_comma_opt @@ -338,7 +338,7 @@ enumerator_list ; enumerator - : (?E{ast.AST.Enumerator}enumeration_constant equals_constant_expression_opt) + : (?E{t_def.AST.Enumerator}enumeration_constant equals_constant_expression_opt) ; atomic_type_specifier @@ -357,43 +357,43 @@ type_qualifier_or_static_list type_qualifier_or_static : type_qualifier - | (?E{ast.AST.StorageClassSpecifier, n = 2}STATIC) + | (?E{t_def.AST.StorageClassSpecifier, n = 2}STATIC) ; type_qualifier - : (?E{ast.AST.TypeQualifier, n = 0}CONST) - | (?E{ast.AST.TypeQualifier, n = 1}RESTRICT) - | (?E{ast.AST.TypeQualifier, n = 2}VOLATILE) - | (?E{ast.AST.TypeQualifier, n = 3}ATOMIC) + : (?E{t_def.AST.TypeQualifier, n = 0}CONST) + | (?E{t_def.AST.TypeQualifier, n = 1}RESTRICT) + | (?E{t_def.AST.TypeQualifier, n = 2}VOLATILE) + | (?E{t_def.AST.TypeQualifier, n = 3}ATOMIC) ; function_specifier - : (?E{ast.AST.FunctionSpecifier, n = 0}INLINE) - | (?E{ast.AST.FunctionSpecifier, n = 1}NORETURN) + : (?E{t_def.AST.FunctionSpecifier, n = 0}INLINE) + | (?E{t_def.AST.FunctionSpecifier, n = 1}NORETURN) ; alignment_specifier - : (?E{ast.AST.AlignAsType}ALIGNAS '(' type_name ')') - | (?E{ast.AST.AlignAsExpression}ALIGNAS '(' constant_expression ')') + : (?E{t_def.AST.AlignAsType}ALIGNAS '(' type_name ')') + | (?E{t_def.AST.AlignAsExpression}ALIGNAS '(' constant_expression ')') ; declarator_opt - : (?E{ast.AST.DeclaratorEmpty}) + : (?E{t_def.AST.DeclaratorEmpty}) | declarator ; declarator : direct_declarator - | (?E{ast.AST.DeclaratorPointer}'*' (?E{ast.AST.TypeQualifierList}type_qualifier_list_opt) declarator) + | (?E{t_def.AST.DeclaratorPointer}'*' (?E{t_def.AST.TypeQualifierList}type_qualifier_list_opt) declarator) ; direct_declarator - : (?E{ast.AST.DeclaratorIdentifier}IDENTIFIER) + : (?E{t_def.AST.DeclaratorIdentifier}IDENTIFIER) | '(' declarator ')' - | (?E{ast.AST.DeclaratorArray}direct_declarator '[' (?E{ast.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') - | (?E{ast.AST.DeclaratorFunctionOldStyle}direct_declarator '(' (?E{ast.AST.IdentifierList}identifier_list_opt) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = False}direct_declarator '(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = True}direct_declarator '(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') + | (?E{t_def.AST.DeclaratorArray}direct_declarator '[' (?E{t_def.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') + | (?E{t_def.AST.DeclaratorFunctionOldStyle}direct_declarator '(' (?E{t_def.AST.IdentifierList}identifier_list_opt) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = False}direct_declarator '(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = True}direct_declarator '(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') ; type_qualifier_list_opt @@ -412,8 +412,8 @@ parameter_declaration_list ; parameter_declaration - : (?E{ast.AST.ParameterDeclaration}(?E{ast.AST.DeclarationSpecifierList}declaration_specifier_list) declarator) - | (?E{ast.AST.ParameterDeclaration}(?E{ast.AST.DeclarationSpecifierList}declaration_specifier_list) abstract_declarator) + : (?E{t_def.AST.ParameterDeclaration}(?E{t_def.AST.DeclarationSpecifierList}declaration_specifier_list) declarator) + | (?E{t_def.AST.ParameterDeclaration}(?E{t_def.AST.DeclarationSpecifierList}declaration_specifier_list) abstract_declarator) ; identifier_list_opt @@ -427,40 +427,40 @@ identifier_list ; type_name - : (?E{ast.AST.TypeName}(?E{ast.AST.SpecifierQualifierList}specifier_qualifier_list) abstract_declarator) + : (?E{t_def.AST.TypeName}(?E{t_def.AST.SpecifierQualifierList}specifier_qualifier_list) abstract_declarator) ; abstract_declarator : direct_abstract_declarator_opt - | (?E{ast.AST.DeclaratorPointer}'*' (?E{ast.AST.TypeQualifierList}type_qualifier_list_opt) abstract_declarator) + | (?E{t_def.AST.DeclaratorPointer}'*' (?E{t_def.AST.TypeQualifierList}type_qualifier_list_opt) abstract_declarator) ; direct_abstract_declarator_opt - : (?E{ast.AST.DeclaratorAbstract}) + : (?E{t_def.AST.DeclaratorAbstract}) | direct_abstract_declarator ; /* in the below, ") (" should be ")(", inserted space for now */ direct_abstract_declarator : '(' direct_abstract_declarator ')' - | '(' (?E{ast.AST.DeclaratorPointer}'*' (?E{ast.AST.TypeQualifierList}type_qualifier_list_opt) abstract_declarator) ')' - | (?E{ast.AST.DeclaratorArray}(?E{ast.AST.DeclaratorAbstract})'[' (?E{ast.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') - | (?E{ast.AST.DeclaratorFunction, varargs = False}(?E{ast.AST.DeclaratorAbstract})'('(?E{ast.AST.ParameterDeclarationList}) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = False}(?E{ast.AST.DeclaratorAbstract})'(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = True}(?E{ast.AST.DeclaratorAbstract})'(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') - | (?E{ast.AST.DeclaratorArray}direct_abstract_declarator '[' (?E{ast.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') - | (?E{ast.AST.DeclaratorFunction, varargs = False}direct_abstract_declarator '('(?E{ast.AST.ParameterDeclarationList}) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = False}direct_abstract_declarator '(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ')') - | (?E{ast.AST.DeclaratorFunction, varargs = True}direct_abstract_declarator '(' (?E{ast.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') + | '(' (?E{t_def.AST.DeclaratorPointer}'*' (?E{t_def.AST.TypeQualifierList}type_qualifier_list_opt) abstract_declarator) ')' + | (?E{t_def.AST.DeclaratorArray}(?E{t_def.AST.DeclaratorAbstract})'[' (?E{t_def.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') + | (?E{t_def.AST.DeclaratorFunction, varargs = False}(?E{t_def.AST.DeclaratorAbstract})'('(?E{t_def.AST.ParameterDeclarationList}) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = False}(?E{t_def.AST.DeclaratorAbstract})'(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = True}(?E{t_def.AST.DeclaratorAbstract})'(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') + | (?E{t_def.AST.DeclaratorArray}direct_abstract_declarator '[' (?E{t_def.AST.TypeQualifierOrStaticList}type_qualifier_or_static_list_opt) assignment_expression_or_asterisk_opt ']') + | (?E{t_def.AST.DeclaratorFunction, varargs = False}direct_abstract_declarator '('(?E{t_def.AST.ParameterDeclarationList}) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = False}direct_abstract_declarator '(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ')') + | (?E{t_def.AST.DeclaratorFunction, varargs = True}direct_abstract_declarator '(' (?E{t_def.AST.ParameterDeclarationList}parameter_declaration_list) ',' ELLIPSIS ')') ; equals_initializer_opt - : (?E{ast.AST.EqualsInitializerEmpty}) + : (?E{t_def.AST.EqualsInitializerEmpty}) | '=' initializer ; initializer - : '{' (?E{ast.AST.DesignatorInitializerList}designator_initializer_list_comma_opt) '}' + : '{' (?E{t_def.AST.DesignatorInitializerList}designator_initializer_list_comma_opt) '}' | assignment_expression ; @@ -476,12 +476,12 @@ designator_initializer_list ; designator_initializer - : (?E{ast.AST.DesignatorInitializer}designator_list_equals_opt initializer) + : (?E{t_def.AST.DesignatorInitializer}designator_list_equals_opt initializer) ; designator_list_equals_opt - : (?E{ast.AST.DesignatorListEqualsEmpty}) - | (?E{ast.AST.DesignatorList}designator_list) '=' + : (?E{t_def.AST.DesignatorListEqualsEmpty}) + | (?E{t_def.AST.DesignatorList}designator_list) '=' ; designator_list @@ -490,31 +490,31 @@ designator_list ; designator - : (?E{ast.AST.DesignatorIndex}'[' constant_expression ']') - | (?E{ast.AST.DesignatorField}'.' IDENTIFIER) + : (?E{t_def.AST.DesignatorIndex}'[' constant_expression ']') + | (?E{t_def.AST.DesignatorField}'.' IDENTIFIER) ; static_assert_declaration - : (?E{ast.AST.StaticAssertDeclaration}STATIC_ASSERT '(' constant_expression ',' STRING_LITERAL ')' ';') + : (?E{t_def.AST.StaticAssertDeclaration}STATIC_ASSERT '(' constant_expression ',' STRING_LITERAL ')' ';') ; statement - : (?E{ast.AST.StatementLabel}IDENTIFIER ':' statement) - | (?E{ast.AST.StatementCase}CASE constant_expression ':' statement) - | (?E{ast.AST.StatementDefault}DEFAULT ':' statement) - | (?E{ast.AST.StatementBlock}'{' (?E{ast.AST.BlockItemList}block_item_list_opt) '}') - | (?E{ast.AST.StatementExpression}expression_opt ';') - | (?E{ast.AST.StatementIfElse}IF '(' expression ')' statement ELSE statement) - | (?E{ast.AST.StatementIf}IF '(' expression ')' statement) - | (?E{ast.AST.StatementSwitch}SWITCH '(' expression ')' statement) - | (?E{ast.AST.StatementWhile}WHILE '(' expression ')' statement) - | (?E{ast.AST.StatementDoWhile}DO statement WHILE '(' expression ')' ';') - | (?E{ast.AST.StatementFor}FOR '(' (?E{ast.AST.StatementExpression}expression_opt) ';' expression_opt ';' expression_opt ')' statement) - | (?E{ast.AST.StatementFor}FOR '(' declaration expression_opt ';' expression_opt ')' statement) - | (?E{ast.AST.StatementGoto}GOTO IDENTIFIER ';') - | (?E{ast.AST.StatementContinue}CONTINUE ';') - | (?E{ast.AST.StatementBreak}BREAK ';') - | (?E{ast.AST.StatementReturn}RETURN expression_opt ';') + : (?E{t_def.AST.StatementLabel}IDENTIFIER ':' statement) + | (?E{t_def.AST.StatementCase}CASE constant_expression ':' statement) + | (?E{t_def.AST.StatementDefault}DEFAULT ':' statement) + | (?E{t_def.AST.StatementBlock}'{' (?E{t_def.AST.BlockItemList}block_item_list_opt) '}') + | (?E{t_def.AST.StatementExpression}expression_opt ';') + | (?E{t_def.AST.StatementIfElse}IF '(' expression ')' statement ELSE statement) + | (?E{t_def.AST.StatementIf}IF '(' expression ')' statement) + | (?E{t_def.AST.StatementSwitch}SWITCH '(' expression ')' statement) + | (?E{t_def.AST.StatementWhile}WHILE '(' expression ')' statement) + | (?E{t_def.AST.StatementDoWhile}DO statement WHILE '(' expression ')' ';') + | (?E{t_def.AST.StatementFor}FOR '(' (?E{t_def.AST.StatementExpression}expression_opt) ';' expression_opt ';' expression_opt ')' statement) + | (?E{t_def.AST.StatementFor}FOR '(' declaration expression_opt ';' expression_opt ')' statement) + | (?E{t_def.AST.StatementGoto}GOTO IDENTIFIER ';') + | (?E{t_def.AST.StatementContinue}CONTINUE ';') + | (?E{t_def.AST.StatementBreak}BREAK ';') + | (?E{t_def.AST.StatementReturn}RETURN expression_opt ';') ; block_item_list_opt @@ -548,7 +548,7 @@ external_declaration ; function_definition - : (?E{ast.AST.FunctionDefinition}(?E{ast.AST.DeclarationSpecifierList}declaration_specifier_list) declarator (?E{ast.AST.DeclarationList}declaration_list_opt) '{' (?E{ast.AST.BlockItemList}block_item_list_opt) '}') + : (?E{t_def.AST.FunctionDefinition}(?E{t_def.AST.DeclarationSpecifierList}declaration_specifier_list) declarator (?E{t_def.AST.DeclarationList}declaration_list_opt) '{' (?E{t_def.AST.BlockItemList}block_item_list_opt) '}') ; declaration_list_opt @@ -562,6 +562,6 @@ declaration_list ; identifier_opt - : (?E{ast.AST.IdentifierEmpty}) + : (?E{t_def.AST.IdentifierEmpty}) | IDENTIFIER ; diff --git a/ast.py b/ast.py deleted file mode 100644 index f7d4935..0000000 --- a/ast.py +++ /dev/null @@ -1,4644 +0,0 @@ -import element -#import xml.etree.ElementTree - -class Context: - def __init__( - self, - indent = '', - lines = [], - top_level = True, - enclosing_loop = None, - translate_identifier = { - 'NULL': 'None', - 'false': 'False', - 'strlen': 'len', - 'true': 'True' - } - ): - self.indent = indent - self.lines = lines - self.top_level = top_level - self.enclosing_loop = enclosing_loop - self.translate_identifier = translate_identifier - -class AST(element.Element): - # internal classes - class Text(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Text', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - Text if factory is None else factory - ) - return result - # GENERATE END - - class DeclarationOrStatement(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclarationOrStatement', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DeclarationOrStatement if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - #text = element.to_text(self).strip() - #assert text[-1] == ';' - #context.lines.append( - # '{0:s}{1:s}\n'.format(context.indent, text[:-1]) - #) - print(self) - raise NotImplementedError - - class Statement(DeclarationOrStatement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Statement', - attrib = {}, - text = '', - children = [] - ): - AST.DeclarationOrStatement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.DeclarationOrStatement.copy( - self, - Statement if factory is None else factory - ) - return result - # GENERATE END - - class Declarator(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Declarator', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - Declarator if factory is None else factory - ) - return result - # GENERATE END - def get_type_and_name(self, base_type): - print(self) - raise NotImplementedError - - class InitializerOrExpression(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_InitializerOrExpression', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - InitializerOrExpression if factory is None else factory - ) - return result - # GENERATE END - def translate_initializer_or_expression(self, context): - print(self) - raise NotImplementedError - - class Expression(InitializerOrExpression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Expression', - attrib = {}, - text = '', - children = [] - ): - AST.InitializerOrExpression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.InitializerOrExpression.copy( - self, - Expression if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - #return element.to_text(self).strip() - print(self) - raise NotImplementedError - def translate_statement_expression(self, context): - return self.translate_expression(context, 0) - def translate_initializer_or_expression(self, context): - return self.translate_expression(context, 0) - - class ExpressionUnary(Expression): - # GENERATE ELEMENT(str unary_operator, bool postfix) BEGIN - def __init__( - self, - tag = 'AST_ExpressionUnary', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - self.unary_operator = unary_operator - self.postfix = postfix - def serialize(self, ref_list): - AST.Expression.serialize(self, ref_list) - self.set( - 'unary_operator', - element.serialize_str(self.unary_operator) - ) - self.set( - 'postfix', - element.serialize_bool(self.postfix) - ) - def deserialize(self, ref_list): - AST.Expression.deserialize(self, ref_list) - self.unary_operator = element.deserialize_str(self.get('unary_operator', '')) - self.postfix = element.deserialize_bool(self.get('postfix', 'false')) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionUnary if factory is None else factory - ) - result.unary_operator = self.unary_operator - result.postfix = self.postfix - return result - # GENERATE END - def translate_expression(self, context, precedence): - if self.postfix: - text = '{0:s}{1:s}'.format( - self[0].translate_expression(context, 14), - self.unary_operator - ) - if 14 < precedence: - text = '({0:s})'.format(text) - else: - text = '{0:s}{1:s}'.format( - self.unary_operator, - self[0].translate_expression(context, 13) - ) - if 13 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionBinary(Expression): - # GENERATE ELEMENT(str binary_operator, int precedence, bool right_to_left) BEGIN - def __init__( - self, - tag = 'AST_ExpressionBinary', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - self.binary_operator = binary_operator - self.precedence = precedence - self.right_to_left = right_to_left - def serialize(self, ref_list): - AST.Expression.serialize(self, ref_list) - self.set( - 'binary_operator', - element.serialize_str(self.binary_operator) - ) - self.set( - 'precedence', - element.serialize_int(self.precedence) - ) - self.set( - 'right_to_left', - element.serialize_bool(self.right_to_left) - ) - def deserialize(self, ref_list): - AST.Expression.deserialize(self, ref_list) - self.binary_operator = element.deserialize_str(self.get('binary_operator', '')) - self.precedence = element.deserialize_int(self.get('precedence', '-1')) - self.right_to_left = element.deserialize_bool(self.get('right_to_left', 'false')) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionBinary if factory is None else factory - ) - result.binary_operator = self.binary_operator - result.precedence = self.precedence - result.right_to_left = self.right_to_left - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s}{1:s}{2:s}'.format( - self[0].translate_expression( - context, - self.precedence + int(self.right_to_left) - ), - self.binary_operator, - self[1].translate_expression( - context, - self.precedence + int(not self.right_to_left) - ) - ) - if self.precedence < precedence: - text = '({0:s})'.format(text) - return text - - # type analysis - class Type(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Type', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - Type if factory is None else factory - ) - return result - # GENERATE END - def translate_size(self, context): - print(self) - raise NotImplementedError - def translate_type(self, context): - print(self) - raise NotImplementedError - def translate_zero(self, context): - print(self) - raise NotImplementedError - - class TypeVoid(Type): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TypeVoid', - attrib = {}, - text = '', - children = [] - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeVoid if factory is None else factory - ) - return result - # GENERATE END - def __str__(self): - return 'void' - def translate_size(self, context): - return 0 - def translate_type(self, context): - return 'NoneType' - def translate_zero(self, context): - return 'None' - - class TypeInt(Type): - # GENERATE ELEMENT(bool signed, int bits) BEGIN - def __init__( - self, - tag = 'AST_TypeInt', - attrib = {}, - text = '', - children = [], - signed = False, - bits = -1 - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - self.signed = signed - self.bits = bits - def serialize(self, ref_list): - AST.Type.serialize(self, ref_list) - self.set( - 'signed', - element.serialize_bool(self.signed) - ) - self.set( - 'bits', - element.serialize_int(self.bits) - ) - def deserialize(self, ref_list): - AST.Type.deserialize(self, ref_list) - self.signed = element.deserialize_bool(self.get('signed', 'false')) - self.bits = element.deserialize_int(self.get('bits', '-1')) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeInt if factory is None else factory - ) - result.signed = self.signed - result.bits = self.bits - return result - # GENERATE END - def __str__(self): - return '{0:s}int{1:d}'.format(['u', ''][int(self.signed)], self.bits) - def translate_size(self, context): - return (self.bits + 7) // 8 - def translate_type(self, context): - return 'int' - def translate_zero(self, context): - return '0' if context.top_level else 'None' - - class TypeFloat(Type): - # GENERATE ELEMENT(int complex, int bits) BEGIN - def __init__( - self, - tag = 'AST_TypeFloat', - attrib = {}, - text = '', - children = [], - complex = -1, - bits = -1 - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - self.complex = complex - self.bits = bits - def serialize(self, ref_list): - AST.Type.serialize(self, ref_list) - self.set( - 'complex', - element.serialize_int(self.complex) - ) - self.set( - 'bits', - element.serialize_int(self.bits) - ) - def deserialize(self, ref_list): - AST.Type.deserialize(self, ref_list) - self.complex = element.deserialize_int(self.get('complex', '-1')) - self.bits = element.deserialize_int(self.get('bits', '-1')) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeFloat if factory is None else factory - ) - result.complex = self.complex - result.bits = self.bits - return result - # GENERATE END - def __str__(self): - return '{0:s}float{0:d}'.format( - ['', 'i', 'c'][int(self.complex)], - self.bits - ) - def translate_size(self, context): - return (self.bits + 7) // 8 - def translate_type(self, context): - return 'float' - def translate_zero(self, context): - return '0.' if context.top_level else 'None' - - class TypeBool(Type): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TypeBool', - attrib = {}, - text = '', - children = [] - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeBool if factory is None else factory - ) - return result - # GENERATE END - def __str__(self): - return 'bool' - def translate_size(self, context): - return 1 - def translate_type(self, context): - return 'bool' - def translate_zero(self, context): - return 'False' if context.top_level else 'None' - - class TypePointer(Type): - # GENERATE ELEMENT(ref target_type) BEGIN - def __init__( - self, - tag = 'AST_TypePointer', - attrib = {}, - text = '', - children = [], - target_type = None - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - self.target_type = target_type - def serialize(self, ref_list): - AST.Type.serialize(self, ref_list) - self.set( - 'target_type', - element.serialize_ref(self.target_type, ref_list) - ) - def deserialize(self, ref_list): - AST.Type.deserialize(self, ref_list) - self.target_type = element.deserialize_ref(self.get('target_type', '-1'), ref_list) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypePointer if factory is None else factory - ) - result.target_type = self.target_type - return result - # GENERATE END - def __str__(self): - return 'pointer<{0:s}>'.format(str(self.target_type)) - def translate_size(self, context): - return 4 - def translate_type(self, context): - return ( - 'str' - if ( - isinstance(self.target_type, AST.TypeInt) and - self.target_type.bits == 8 - ) else - 'list' - ) - def translate_zero(self, context): - return ( - ( - '\'\'' - if ( - isinstance(self.target_type, AST.TypeInt) and - self.target_type.bits == 8 - ) else - '[]' - ) - if context.top_level else - 'None' - ) - - class TypeArray(Type): - # GENERATE ELEMENT(ref element_type, int element_count) BEGIN - def __init__( - self, - tag = 'AST_TypeArray', - attrib = {}, - text = '', - children = [], - element_type = None, - element_count = -1 - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - self.element_type = element_type - self.element_count = element_count - def serialize(self, ref_list): - AST.Type.serialize(self, ref_list) - self.set( - 'element_type', - element.serialize_ref(self.element_type, ref_list) - ) - self.set( - 'element_count', - element.serialize_int(self.element_count) - ) - def deserialize(self, ref_list): - AST.Type.deserialize(self, ref_list) - self.element_type = element.deserialize_ref(self.get('element_type', '-1'), ref_list) - self.element_count = element.deserialize_int(self.get('element_count', '-1')) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeArray if factory is None else factory - ) - result.element_type = self.element_type - result.element_count = self.element_count - return result - # GENERATE END - def __str__(self): - return 'array<{0:s}: {1:s}>'.format( - str(self.element_type), - ( - 'UNSPECIFIED' - if self.element_count == -1 else - 'C99_FLEXIBLE' - if self.element_count == -2 else - str(self.element_count) - ) - ) - def translate_size(self, context): - return self.element_type.translate_type(context) * self.element_count - def translate_zero(self, context): - return '[{0:s}]'.format( - ', '.join( - [self.element_type.translate_zero(context)] * self.element_count - ) - ) - - class TypeFunction(Type): - class Argument(element.Element): - # GENERATE ELEMENT(ref type, str name) BEGIN - def __init__( - self, - tag = 'AST_TypeFunction_Argument', - attrib = {}, - text = '', - children = [], - type = None, - name = '' - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - self.type = type - self.name = name - def serialize(self, ref_list): - element.Element.serialize(self, ref_list) - self.set( - 'type', - element.serialize_ref(self.type, ref_list) - ) - self.set( - 'name', - element.serialize_str(self.name) - ) - def deserialize(self, ref_list): - element.Element.deserialize(self, ref_list) - self.type = element.deserialize_ref(self.get('type', '-1'), ref_list) - self.name = element.deserialize_str(self.get('name', '')) - def copy(self, factory = None): - result = element.Element.copy( - self, - Argument if factory is None else factory - ) - result.type = self.type - result.name = self.name - return result - # GENERATE END - def __str__(self): - return '{0:s} {1:s}'.format( - str(self.type), - 'ABSTRACT' if self.name == '' else self.name - ) - - # GENERATE ELEMENT(ref return_type, bool varargs) BEGIN - def __init__( - self, - tag = 'AST_TypeFunction', - attrib = {}, - text = '', - children = [], - return_type = None, - varargs = False - ): - AST.Type.__init__( - self, - tag, - attrib, - text, - children - ) - self.return_type = return_type - self.varargs = varargs - def serialize(self, ref_list): - AST.Type.serialize(self, ref_list) - self.set( - 'return_type', - element.serialize_ref(self.return_type, ref_list) - ) - self.set( - 'varargs', - element.serialize_bool(self.varargs) - ) - def deserialize(self, ref_list): - AST.Type.deserialize(self, ref_list) - self.return_type = element.deserialize_ref(self.get('return_type', '-1'), ref_list) - self.varargs = element.deserialize_bool(self.get('varargs', 'false')) - def copy(self, factory = None): - result = AST.Type.copy( - self, - TypeFunction if factory is None else factory - ) - result.return_type = self.return_type - result.varargs = self.varargs - return result - # GENERATE END - def __str__(self): - return 'function<{0:s} -> {1:s}>'.format( - ( - 'void' - if len(self) == 0 else - ', '.join( - [str(i) for i in self] + (['...'] if self.varargs else []) - ) - ), - str(self.return_type) - ) - - # syntax classes - class AlignAsExpression(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_AlignAsExpression', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - AlignAsExpression if factory is None else factory - ) - return result - # GENERATE END - - class AlignAsType(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_AlignAsType', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - AlignAsType if factory is None else factory - ) - return result - # GENERATE END - - class ArgumentExpressionList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ArgumentExpressionList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - ArgumentExpressionList if factory is None else factory - ) - return result - # GENERATE END - - class BlockItemList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_BlockItemList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - BlockItemList if factory is None else factory - ) - return result - # GENERATE END - def translate_block_item_list(self, context): - for i in self: - i.translate_declaration_or_statement(context) - - class Declaration(DeclarationOrStatement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Declaration', - attrib = {}, - text = '', - children = [] - ): - AST.DeclarationOrStatement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.DeclarationOrStatement.copy( - self, - Declaration if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - base_type = self[0].get_type() - for i in self[1]: - type, name = i[0].get_type_and_name(base_type) - if not isinstance(type, AST.TypeFunction): - context.lines.append( - '{0:s}{1:s} = {2:s}\n'.format( - context.indent, - name, - ( - type.translate_zero(context) - if isinstance(i[1], AST.EqualsInitializerEmpty) else - i[1].translate_initializer_or_expression(context) - ) - ) - ) - - class DeclarationList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclarationList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DeclarationList if factory is None else factory - ) - return result - # GENERATE END - - class DeclarationSpecifierList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclarationSpecifierList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DeclarationSpecifierList if factory is None else factory - ) - return result - # GENERATE END - def get_type(self): - type_specifiers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - for i in self: - if isinstance(i, AST.TypeSpecifier): - type_specifiers[i.n] += 1 - return type_specifiers_to_type[tuple(type_specifiers)] - - class DeclaratorAbstract(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorAbstract', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorAbstract if factory is None else factory - ) - return result - # GENERATE END - def get_type_and_name(self, base_type): - return base_type, '' - - class DeclaratorArray(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorArray', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorArray if factory is None else factory - ) - return result - # GENERATE END - def get_type_and_name(self, base_type): - return self[0].get_type_and_name( - AST.TypeArray( - element_type = base_type, - element_count = ( - -1 - if isinstance(self[2], AST.ExpressionEmpty) else - # kludgey way, assuming not calculated size - int( - element.get_text(self[2], 0), - 8 if element.get_text(self[2], 0)[:2] in octal_prefix else 0 - ) - ) - ) - ) - - class DeclaratorEmpty(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorEmpty', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorEmpty if factory is None else factory - ) - return result - # GENERATE END - - class DeclaratorFunction(Declarator): - # GENERATE ELEMENT(bool varargs) BEGIN - def __init__( - self, - tag = 'AST_DeclaratorFunction', - attrib = {}, - text = '', - children = [], - varargs = False - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - self.varargs = varargs - def serialize(self, ref_list): - AST.Declarator.serialize(self, ref_list) - self.set( - 'varargs', - element.serialize_bool(self.varargs) - ) - def deserialize(self, ref_list): - AST.Declarator.deserialize(self, ref_list) - self.varargs = element.deserialize_bool(self.get('varargs', 'false')) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorFunction if factory is None else factory - ) - result.varargs = self.varargs - return result - # GENERATE END - def get_type_and_name(self, base_type): - children = [] - for i in self[1]: - type, name = i[1].get_type_and_name(i[0].get_type()) - children.append(AST.TypeFunction.Argument(type = type, name = name)) - return self[0].get_type_and_name( - AST.TypeFunction( - children = children, - return_type = base_type, - varargs = self.varargs, - ) - ) - - class DeclaratorFunctionOldStyle(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorFunctionOldStyle', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorFunctionOldStyle if factory is None else factory - ) - return result - # GENERATE END - - class DeclaratorIdentifier(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorIdentifier', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorIdentifier if factory is None else factory - ) - return result - # GENERATE END - def get_type_and_name(self, base_type): - return base_type, element.get_text(self[0], 0) - - class DeclaratorPointer(Declarator): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DeclaratorPointer', - attrib = {}, - text = '', - children = [] - ): - AST.Declarator.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Declarator.copy( - self, - DeclaratorPointer if factory is None else factory - ) - return result - # GENERATE END - def get_type_and_name(self, base_type): - return self[1].get_type_and_name( - AST.TypePointer(target_type = base_type) - ) - - class DefaultTypeName(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DefaultTypeName', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DefaultTypeName if factory is None else factory - ) - return result - # GENERATE END - - class DesignatorField(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorField', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DesignatorField if factory is None else factory - ) - return result - # GENERATE END - - class DesignatorIndex(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorIndex', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DesignatorIndex if factory is None else factory - ) - return result - # GENERATE END - - class DesignatorInitializer(InitializerOrExpression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorInitializer', - attrib = {}, - text = '', - children = [] - ): - AST.InitializerOrExpression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.InitializerOrExpression.copy( - self, - DesignatorInitializer if factory is None else factory - ) - return result - # GENERATE END - def translate_initializer_or_expression(self, context): - assert isinstance(self[0], AST.DesignatorListEqualsEmpty) - return self[1].translate_initializer_or_expression(context) - - class DesignatorInitializerList(InitializerOrExpression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorInitializerList', - attrib = {}, - text = '', - children = [] - ): - AST.InitializerOrExpression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.InitializerOrExpression.copy( - self, - DesignatorInitializerList if factory is None else factory - ) - return result - # GENERATE END - def translate_initializer_or_expression(self, context): - return '[{0:s}]'.format( - ', '.join( - [ - i.translate_initializer_or_expression(context) - for i in self - ] - ) - ) - - class DesignatorListEqualsEmpty(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorListEqualsEmpty', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DesignatorListEqualsEmpty if factory is None else factory - ) - return result - # GENERATE END - - class DesignatorList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_DesignatorList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - DesignatorList if factory is None else factory - ) - return result - # GENERATE END - - class EnumSpecifier(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_EnumSpecifier', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - EnumSpecifier if factory is None else factory - ) - return result - # GENERATE END - - class Enumerator(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Enumerator', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - Enumerator if factory is None else factory - ) - return result - # GENERATE END - - class EnumeratorList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_EnumeratorList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - EnumeratorList if factory is None else factory - ) - return result - # GENERATE END - - class EqualsInitializerEmpty(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_EqualsInitializerEmpty', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - EqualsInitializerEmpty if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAdd(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAdd', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionAdd if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAddAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAddAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionAddAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAddressOf(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAddressOf', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionAddressOf if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAlignOfType(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAlignOfType', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionAlignOfType if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionArray(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionArray', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionArray if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionAsterisk(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionAsterisk', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionAsterisk if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionBitwiseAnd(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionBitwiseAnd', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionBitwiseAnd if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionBitwiseAndAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionBitwiseAndAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionBitwiseAndAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionBitwiseNot(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionBitwiseNot', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionBitwiseNot if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionBitwiseOr(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionBitwiseOr', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionBitwiseOr if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionBitwiseOrAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionBitwiseOrAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionBitwiseOrAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionCall(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionCall', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionCall if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s}({1:s})'.format( - self[0].translate_expression(context, 14), - ', '.join([i.translate_expression(context, 1) for i in self[1]]) - ) - if 14 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionCast(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionCast', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionCast if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - type, _ = self[0][1].get_type_and_name(self[0][0].get_type()) - text = '{0:s}({1:s})'.format( - type.translate_type(context), - self[1].translate_expression(context, 0) - ) - if 14 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionCharConstant(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionCharConstant', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionCharConstant if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - return 'ord(\'{0:s}\')'.format(element.get_text(self[0], 0)) - - class ExpressionComma(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionComma', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionComma if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionConditional(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionConditional', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionConditional if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s} if {1:s} else {2:s}'.format( - self[1].translate_expression(context, 3), - self[0].translate_expression(context, 0), - self[2].translate_expression(context, 2) - ) - if 2 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionDereference(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionDereference', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionDereference if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionDivide(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionDivide', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionDivide if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionDivideAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionDivideAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionDivideAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionEmpty(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionEmpty', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionEmpty if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - return 'True' - def translate_statement_expression(self, context): - return 'pass' - - class ExpressionEqual(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionEqual', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionEqual if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionExclusiveOr(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionExclusiveOr', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionExclusiveOr if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionExclusiveOrAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionExclusiveOrAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionExclusiveOrAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionField(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionField', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionField if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s}.{1:s}'.format( - self[0].translate_expression(context, 14), - self[1].translate_identifier(context) - ) - if 14 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionFieldDereference(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionFieldDereference', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionFieldDereference if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s}->{1:s}'.format( - self[0].translate_expression(context, 14), - self[1].translate_identifier(context) - ) - if 14 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionFloatLiteral(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionFloatLiteral', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionFloatLiteral if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionFunctionName(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionFunctionName', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionFunctionName if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionGreaterThan(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionGreaterThan', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionGreaterThan if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionGreaterThanOrEqual(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionGreaterThanOrEqual', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionGreaterThanOrEqual if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionIdentifier(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionIdentifier', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionIdentifier if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - return self[0].translate_identifier(context) - - class ExpressionIndex(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionIndex', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionIndex if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = '{0:s}[{1:s}]'.format( - self[0].translate_expression(context, 14), - self[1].translate_expression(context, 0) - ) - if 14 < precedence: - text = '({0:s})'.format(text) - return text - - class ExpressionIntLiteral(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionIntLiteral', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionIntLiteral if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - text = element.get_text(self, 0) - if text[:2] in octal_prefix: - text = '0o' + text[1:] - return text - - class ExpressionLeftShiftAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLeftShiftAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionLeftShiftAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionLessThan(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLessThan', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionLessThan if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionLessThanOrEqual(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLessThanOrEqual', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionLessThanOrEqual if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionLogicalAnd(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLogicalAnd', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionLogicalAnd if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionLogicalNot(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLogicalNot', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionLogicalNot if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionLogicalOr(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionLogicalOr', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionLogicalOr if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionMinus(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionMinus', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionMinus if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionModulo(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionModulo', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionModulo if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionModuloAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionModuloAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionModuloAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionMultiply(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionMultiply', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionMultiply if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionMultiplyAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionMultiplyAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionMultiplyAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionNotEqual(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionNotEqual', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionNotEqual if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionPlus(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionPlus', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionPlus if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionPostDecrement(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionPostDecrement', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionPostDecrement if factory is None else factory - ) - return result - # GENERATE END - def translate_statement_expression(self, context): - return '{0:s} -= 1'.format(self[0].translate_expression(context, 0)) - - class ExpressionPostIncrement(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionPostIncrement', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionPostIncrement if factory is None else factory - ) - return result - # GENERATE END - def translate_statement_expression(self, context): - return '{0:s} += 1'.format(self[0].translate_expression(context, 0)) - - class ExpressionPreDecrement(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionPreDecrement', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionPreDecrement if factory is None else factory - ) - return result - # GENERATE END - def translate_statement_expression(self, context): - return '{0:s} -= 1'.format(self[0].translate_expression(context, 0)) - - class ExpressionPreIncrement(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionPreIncrement', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionPreIncrement if factory is None else factory - ) - return result - # GENERATE END - def translate_statement_expression(self, context): - return '{0:s} += 1'.format(self[0].translate_expression(context, 0)) - - class ExpressionRightShiftAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionRightShiftAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionRightShiftAssignment if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionShiftLeft(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionShiftLeft', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionShiftLeft if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionShiftRight(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionShiftRight', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionShiftRight if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionSizeOfExpression(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionSizeOfExpression', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionSizeOfExpression if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionSizeOfType(ExpressionUnary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionSizeOfType', - attrib = {}, - text = '', - children = [], - unary_operator = '', - postfix = False - ): - AST.ExpressionUnary.__init__( - self, - tag, - attrib, - text, - children, - unary_operator, - postfix - ) - def copy(self, factory = None): - result = AST.ExpressionUnary.copy( - self, - ExpressionSizeOfType if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - type, _ = self[0][1].get_type_and_name(self[0][0].get_type()) - return str(type.translate_size(context)) - - class ExpressionStringLiteral(Expression): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionStringLiteral', - attrib = {}, - text = '', - children = [] - ): - AST.Expression.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Expression.copy( - self, - ExpressionStringLiteral if factory is None else factory - ) - return result - # GENERATE END - def translate_expression(self, context, precedence): - return ' '.join( - [ - '\'{0:s}\''.format( - element.get_text(i, 0). - replace('\\"', '"'). - replace('\'', '\\\'') - ) - for i in self - ] - ) - - class ExpressionSubtract(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionSubtract', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionSubtract if factory is None else factory - ) - return result - # GENERATE END - - class ExpressionSubtractAssignment(ExpressionBinary): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ExpressionSubtractAssignment', - attrib = {}, - text = '', - children = [], - binary_operator = '', - precedence = -1, - right_to_left = False - ): - AST.ExpressionBinary.__init__( - self, - tag, - attrib, - text, - children, - binary_operator, - precedence, - right_to_left - ) - def copy(self, factory = None): - result = AST.ExpressionBinary.copy( - self, - ExpressionSubtractAssignment if factory is None else factory - ) - return result - # GENERATE END - - class FunctionDefinition(DeclarationOrStatement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_FunctionDefinition', - attrib = {}, - text = '', - children = [] - ): - AST.DeclarationOrStatement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.DeclarationOrStatement.copy( - self, - FunctionDefinition if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - type, name = self[1].get_type_and_name(self[0].get_type()) - assert isinstance(type, AST.TypeFunction) - if len(context.lines): - context.lines.append('\n') - context.lines.append( - '{0:s}def {1:s}({2:s}):\n'.format( - context.indent, - name, - ', '.join([i.name for i in type]) - ) - ) - indent_save = context.indent - context.indent += ' ' - assert context.top_level - context.top_level = False - self[3].translate_block_item_list(context) - context.top_level = True - context.indent = indent_save - - class FunctionSpecifier(element.Element): - # GENERATE ELEMENT(int n) BEGIN - def __init__( - self, - tag = 'AST_FunctionSpecifier', - attrib = {}, - text = '', - children = [], - n = -1 - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - self.n = n - def serialize(self, ref_list): - element.Element.serialize(self, ref_list) - self.set( - 'n', - element.serialize_int(self.n) - ) - def deserialize(self, ref_list): - element.Element.deserialize(self, ref_list) - self.n = element.deserialize_int(self.get('n', '-1')) - def copy(self, factory = None): - result = element.Element.copy( - self, - FunctionSpecifier if factory is None else factory - ) - result.n = self.n - return result - # GENERATE END - - class GenericAssociation(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_GenericAssociation', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - GenericAssociation if factory is None else factory - ) - return result - # GENERATE END - - class GenericAssociationList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_GenericAssociationList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - GenericAssociationList if factory is None else factory - ) - return result - # GENERATE END - - class GenericSelection(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_GenericSelection', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - GenericSelection if factory is None else factory - ) - return result - # GENERATE END - - class Identifier(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_Identifier', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - Identifier if factory is None else factory - ) - return result - # GENERATE END - def translate_identifier(self, context): - text = element.get_text(self, 0) - return context.translate_identifier.get(text, text) - - class IdentifierEmpty(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_IdentifierEmpty', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - IdentifierEmpty if factory is None else factory - ) - return result - # GENERATE END - - class IdentifierList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_IdentifierList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - IdentifierList if factory is None else factory - ) - return result - # GENERATE END - - class InitDeclarator(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_InitDeclarator', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - InitDeclarator if factory is None else factory - ) - return result - # GENERATE END - - class InitDeclaratorList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_InitDeclaratorList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - InitDeclaratorList if factory is None else factory - ) - return result - # GENERATE END - - class ParameterDeclaration(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ParameterDeclaration', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - ParameterDeclaration if factory is None else factory - ) - return result - # GENERATE END - - class ParameterDeclarationList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_ParameterDeclarationList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - ParameterDeclarationList if factory is None else factory - ) - return result - # GENERATE END - - class SpecifierQualifierList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_SpecifierQualifierList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - SpecifierQualifierList if factory is None else factory - ) - return result - # GENERATE END - def get_type(self): - type_specifiers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - for i in self: - if isinstance(i, AST.TypeSpecifier): - type_specifiers[i.n] += 1 - return type_specifiers_to_type[tuple(type_specifiers)] - - class StatementBlock(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementBlock', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementBlock if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - if len(self[0]): - self[0].translate_block_item_list(context) - else: - context.lines.append('{0:s}pass\n'.format(context.indent)) - - class StatementBreak(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementBreak', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementBreak if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append('{0:s}break\n'.format(context.indent)) - - class StatementCase(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementCase', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementCase if factory is None else factory - ) - return result - # GENERATE END - - class StatementContinue(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementContinue', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementContinue if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - enclosing_loop_save = context.enclosing_loop - context.enclosing_loop = None - if isinstance(enclosing_loop_save, AST.StatementDoWhile): - AST.StatementIfElse( - children = [ - enclosing_loop_save[1], - AST.StatementContinue(), - AST.StatementBreak() - ] - ).translate_declaration_or_statement(context) - elif isinstance(enclosing_loop_save, AST.StatementFor): - context.lines.append( - '{0:s}{1:s}\n{2:s}continue\n'.format( - context.indent, - enclosing_loop_save[2].translate_statement_expression(context), - context.indent - ) - ) - else: - context.lines.append('{0:s}continue\n'.format(context.indent)) - context.enclosing_loop = enclosing_loop_save - - class StatementDefault(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementDefault', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementDefault if factory is None else factory - ) - return result - # GENERATE END - - class StatementDoWhile(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementDoWhile', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementDoWhile if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - if ( - isinstance(self[1], AST.ExpressionIntLiteral) and - element.get_text(self[1], 0) == '0' - ): - self[0].translate_declaration_or_statement(context) - return - context.lines.append('{0:s}while True:\n'.format(context.indent)) - indent_save = context.indent - context.indent += ' ' - enclosing_loop_save = context.enclosing_loop - context.enclosing_loop = self - self[0].translate_declaration_or_statement(context) - context.enclosing_loop = enclosing_loop_save - AST.StatementIf( - children = [ - AST.ExpressionLogicalNot( - children = [ - self[1] - ], - unary_operator = 'not ' - ), - AST.StatementBreak() - ] - ).translate_declaration_or_statement(context) - context.indent = indent_save - - class StatementExpression(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementExpression', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementExpression if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append( - '{0:s}{1:s}\n'.format( - context.indent, - self[0].translate_statement_expression(context) - ) - ) - - class StatementFor(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementFor', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementFor if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - self[0].translate_declaration_or_statement(context) - context.lines.append( - '{0:s}while {1:s}:\n'.format( - context.indent, - self[1].translate_expression(context, 0) - ) - ) - indent_save = context.indent - context.indent += ' ' - enclosing_loop_save = context.enclosing_loop - context.enclosing_loop = self - self[3].translate_declaration_or_statement(context) - context.enclosing_loop = enclosing_loop_save - context.lines.append( - '{0:s}{1:s}\n'.format( - context.indent, - self[2].translate_statement_expression(context) - ) - ) - context.indent = indent_save - - class StatementGoto(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementGoto', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementGoto if factory is None else factory - ) - return result - # GENERATE END - - class StatementIf(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementIf', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementIf if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append( - '{0:s}if {1:s}:\n'.format( - context.indent, - self[0].translate_expression(context, 0) - ) - ) - indent_save = context.indent - context.indent += ' ' - self[1].translate_declaration_or_statement(context) - context.indent = indent_save - - class StatementIfElse(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementIfElse', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementIfElse if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append( - '{0:s}if {1:s}:\n'.format( - context.indent, - self[0].translate_expression(context, 0) - ) - ) - indent_save = context.indent - context.indent += ' ' - self[1].translate_declaration_or_statement(context) - context.lines.append('{0:s}else:\n'.format(indent_save)) - self[2].translate_declaration_or_statement(context) - context.indent = indent_save - - class StatementLabel(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementLabel', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementLabel if factory is None else factory - ) - return result - # GENERATE END - - class StatementReturn(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementReturn', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementReturn if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append( - '{0:s}return {1:s}\n'.format( - context.indent, - self[0].translate_expression(context, 0) - ) - ) - - class StatementSwitch(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementSwitch', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementSwitch if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - assert isinstance(self[1], AST.StatementBlock) - indent_save = context.indent - context.indent += ' ' - if_text = 'if' - for i in self[1][0]: - if ( - isinstance(i, AST.StatementCase) or - isinstance(i, AST.StatementDefault) - ): - if isinstance(i, AST.StatementCase): - cond_expr = AST.ExpressionEqual( - children = [ - self[0], - i[0] - ], - binary_operator = ' == ', - precedence = 8 - ) - j = i[1] - else: - cond_expr = None - j = i[0] - if ( - isinstance(j, AST.StatementCase) or - isinstance(j, AST.StatementDefault) - ): - if isinstance(j, AST.StatementCase): - if cond_expr is not None: - cond_expr = AST.ExpressionLogicalOr( - children = [ - cond_expr, - AST.ExpressionEqual( - children = [ - self[0], - j[0] - ], - binary_operator = ' == ', - precedence = 8 - ) - ], - binary_operator = ' or ', - precedence = 3 - ) - j = j[1] - else: - cond_expr = None - j = j[0] - if cond_expr is not None: - assert if_text is not None - context.lines.append( - '{0:s}{1:s} {2:s}:\n'.format( - indent_save, - if_text, - cond_expr.translate_expression(context, 0) - ) - ) - if_text = 'elif' - else: - context.lines.append( - '{0:s}else:\n'.format(indent_save) - ) - if_text = None - j.translate_declaration_or_statement(context) - else: - i.translate_declaration_or_statement(context) - context.indent = indent_save - - class StatementWhile(Statement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StatementWhile', - attrib = {}, - text = '', - children = [] - ): - AST.Statement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.Statement.copy( - self, - StatementWhile if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - context.lines.append( - '{0:s}while {1:s}:\n'.format( - context.indent, - self[0].translate_expression(context, 0) - ) - ) - indent_save = context.indent - context.indent += ' ' - enclosing_loop_save = context.enclosing_loop - context.enclosing_loop = self - self[1].translate_declaration_or_statement(context) - context.enclosing_loop = enclosing_loop_save - context.indent = indent_save - - class StaticAssertDeclaration(DeclarationOrStatement): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StaticAssertDeclaration', - attrib = {}, - text = '', - children = [] - ): - AST.DeclarationOrStatement.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = AST.DeclarationOrStatement.copy( - self, - StaticAssertDeclaration if factory is None else factory - ) - return result - # GENERATE END - def translate_declaration_or_statement(self, context): - pass - - class StorageClassSpecifier(element.Element): - # GENERATE ELEMENT(int n) BEGIN - def __init__( - self, - tag = 'AST_StorageClassSpecifier', - attrib = {}, - text = '', - children = [], - n = -1 - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - self.n = n - def serialize(self, ref_list): - element.Element.serialize(self, ref_list) - self.set( - 'n', - element.serialize_int(self.n) - ) - def deserialize(self, ref_list): - element.Element.deserialize(self, ref_list) - self.n = element.deserialize_int(self.get('n', '-1')) - def copy(self, factory = None): - result = element.Element.copy( - self, - StorageClassSpecifier if factory is None else factory - ) - result.n = self.n - return result - # GENERATE END - - class StructDeclaration(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StructDeclaration', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - StructDeclaration if factory is None else factory - ) - return result - # GENERATE END - - class StructDeclarationList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StructDeclarationList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - StructDeclarationList if factory is None else factory - ) - return result - # GENERATE END - - class StructDeclarator(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StructDeclarator', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - StructDeclarator if factory is None else factory - ) - return result - # GENERATE END - - class StructDeclaratorList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StructDeclaratorList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - StructDeclaratorList if factory is None else factory - ) - return result - # GENERATE END - - class StructSpecifier(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_StructSpecifier', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - StructSpecifier if factory is None else factory - ) - return result - # GENERATE END - - class TypeName(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TypeName', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - TypeName if factory is None else factory - ) - return result - # GENERATE END - - class TypeQualifier(element.Element): - # GENERATE ELEMENT(int n) BEGIN - def __init__( - self, - tag = 'AST_TypeQualifier', - attrib = {}, - text = '', - children = [], - n = -1 - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - self.n = n - def serialize(self, ref_list): - element.Element.serialize(self, ref_list) - self.set( - 'n', - element.serialize_int(self.n) - ) - def deserialize(self, ref_list): - element.Element.deserialize(self, ref_list) - self.n = element.deserialize_int(self.get('n', '-1')) - def copy(self, factory = None): - result = element.Element.copy( - self, - TypeQualifier if factory is None else factory - ) - result.n = self.n - return result - # GENERATE END - - class TypeQualifierList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TypeQualifierList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - TypeQualifierList if factory is None else factory - ) - return result - # GENERATE END - - class TypeQualifierOrStaticList(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TypeQualifierOrStaticList', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - TypeQualifierOrStaticList if factory is None else factory - ) - return result - # GENERATE END - - class TypeSpecifier(element.Element): - # GENERATE ELEMENT(int n) BEGIN - def __init__( - self, - tag = 'AST_TypeSpecifier', - attrib = {}, - text = '', - children = [], - n = -1 - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - self.n = n - def serialize(self, ref_list): - element.Element.serialize(self, ref_list) - self.set( - 'n', - element.serialize_int(self.n) - ) - def deserialize(self, ref_list): - element.Element.deserialize(self, ref_list) - self.n = element.deserialize_int(self.get('n', '-1')) - def copy(self, factory = None): - result = element.Element.copy( - self, - TypeSpecifier if factory is None else factory - ) - result.n = self.n - return result - # GENERATE END - - class UnionSpecifier(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_UnionSpecifier', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - UnionSpecifier if factory is None else factory - ) - return result - # GENERATE END - - class TranslationUnit(element.Element): - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST_TranslationUnit', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - TranslationUnit if factory is None else factory - ) - return result - # GENERATE END - def translate_translation_unit(self, context): - for i in self: - i.translate_declaration_or_statement(context) - - # GENERATE ELEMENT() BEGIN - def __init__( - self, - tag = 'AST', - attrib = {}, - text = '', - children = [] - ): - element.Element.__init__( - self, - tag, - attrib, - text, - children - ) - def copy(self, factory = None): - result = element.Element.copy( - self, - AST if factory is None else factory - ) - return result - # GENERATE END - -# void char short int long float double signed unsigned bool complex imaginary -type_specifiers_to_type = { - (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeVoid(), - (0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 8), - (0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 8), - (0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 8), - (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), - (0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), - (0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 16), - (0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), - (0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 16), - (0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 16), - (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), - (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), - (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), - (0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 32), - (0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 32), - (0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), - (0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), - (0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 64), - (0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), - (0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 0, 0): AST.TypeInt(signed = True, bits = 64), - (0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0): AST.TypeInt(signed = False, bits = 64), - (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0): AST.TypeFloat(complex = 0, bits = 32), - (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0): AST.TypeFloat(complex = 2, bits = 32), - (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1): AST.TypeFloat(complex = 1, bits = 32), - (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0): AST.TypeFloat(complex = 0, bits = 64), - (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0): AST.TypeFloat(complex = 1, bits = 64), - (0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1): AST.TypeFloat(complex = 2, bits = 64), - (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0): AST.TypeBool() -} -octal_prefix = set( - ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'] -) - -# GENERATE FACTORY(element.Element) BEGIN -tag_to_class = { - 'AST': AST, - 'AST_Text': AST.Text, - 'AST_DeclarationOrStatement': AST.DeclarationOrStatement, - 'AST_Statement': AST.Statement, - 'AST_Declarator': AST.Declarator, - 'AST_InitializerOrExpression': AST.InitializerOrExpression, - 'AST_Expression': AST.Expression, - 'AST_ExpressionUnary': AST.ExpressionUnary, - 'AST_ExpressionBinary': AST.ExpressionBinary, - 'AST_Type': AST.Type, - 'AST_TypeVoid': AST.TypeVoid, - 'AST_TypeInt': AST.TypeInt, - 'AST_TypeFloat': AST.TypeFloat, - 'AST_TypeBool': AST.TypeBool, - 'AST_TypePointer': AST.TypePointer, - 'AST_TypeArray': AST.TypeArray, - 'AST_TypeFunction': AST.TypeFunction, - 'AST_TypeFunction_Argument': AST.TypeFunction.Argument, - 'AST_AlignAsExpression': AST.AlignAsExpression, - 'AST_AlignAsType': AST.AlignAsType, - 'AST_ArgumentExpressionList': AST.ArgumentExpressionList, - 'AST_BlockItemList': AST.BlockItemList, - 'AST_Declaration': AST.Declaration, - 'AST_DeclarationList': AST.DeclarationList, - 'AST_DeclarationSpecifierList': AST.DeclarationSpecifierList, - 'AST_DeclaratorAbstract': AST.DeclaratorAbstract, - 'AST_DeclaratorArray': AST.DeclaratorArray, - 'AST_DeclaratorEmpty': AST.DeclaratorEmpty, - 'AST_DeclaratorFunction': AST.DeclaratorFunction, - 'AST_DeclaratorFunctionOldStyle': AST.DeclaratorFunctionOldStyle, - 'AST_DeclaratorIdentifier': AST.DeclaratorIdentifier, - 'AST_DeclaratorPointer': AST.DeclaratorPointer, - 'AST_DefaultTypeName': AST.DefaultTypeName, - 'AST_DesignatorField': AST.DesignatorField, - 'AST_DesignatorIndex': AST.DesignatorIndex, - 'AST_DesignatorInitializer': AST.DesignatorInitializer, - 'AST_DesignatorInitializerList': AST.DesignatorInitializerList, - 'AST_DesignatorListEqualsEmpty': AST.DesignatorListEqualsEmpty, - 'AST_DesignatorList': AST.DesignatorList, - 'AST_EnumSpecifier': AST.EnumSpecifier, - 'AST_Enumerator': AST.Enumerator, - 'AST_EnumeratorList': AST.EnumeratorList, - 'AST_EqualsInitializerEmpty': AST.EqualsInitializerEmpty, - 'AST_ExpressionAdd': AST.ExpressionAdd, - 'AST_ExpressionAddAssignment': AST.ExpressionAddAssignment, - 'AST_ExpressionAddressOf': AST.ExpressionAddressOf, - 'AST_ExpressionAlignOfType': AST.ExpressionAlignOfType, - 'AST_ExpressionArray': AST.ExpressionArray, - 'AST_ExpressionAssignment': AST.ExpressionAssignment, - 'AST_ExpressionAsterisk': AST.ExpressionAsterisk, - 'AST_ExpressionBitwiseAnd': AST.ExpressionBitwiseAnd, - 'AST_ExpressionBitwiseAndAssignment': AST.ExpressionBitwiseAndAssignment, - 'AST_ExpressionBitwiseNot': AST.ExpressionBitwiseNot, - 'AST_ExpressionBitwiseOr': AST.ExpressionBitwiseOr, - 'AST_ExpressionBitwiseOrAssignment': AST.ExpressionBitwiseOrAssignment, - 'AST_ExpressionCall': AST.ExpressionCall, - 'AST_ExpressionCast': AST.ExpressionCast, - 'AST_ExpressionCharConstant': AST.ExpressionCharConstant, - 'AST_ExpressionComma': AST.ExpressionComma, - 'AST_ExpressionConditional': AST.ExpressionConditional, - 'AST_ExpressionDereference': AST.ExpressionDereference, - 'AST_ExpressionDivide': AST.ExpressionDivide, - 'AST_ExpressionDivideAssignment': AST.ExpressionDivideAssignment, - 'AST_ExpressionEmpty': AST.ExpressionEmpty, - 'AST_ExpressionEqual': AST.ExpressionEqual, - 'AST_ExpressionExclusiveOr': AST.ExpressionExclusiveOr, - 'AST_ExpressionExclusiveOrAssignment': AST.ExpressionExclusiveOrAssignment, - 'AST_ExpressionField': AST.ExpressionField, - 'AST_ExpressionFieldDereference': AST.ExpressionFieldDereference, - 'AST_ExpressionFloatLiteral': AST.ExpressionFloatLiteral, - 'AST_ExpressionFunctionName': AST.ExpressionFunctionName, - 'AST_ExpressionGreaterThan': AST.ExpressionGreaterThan, - 'AST_ExpressionGreaterThanOrEqual': AST.ExpressionGreaterThanOrEqual, - 'AST_ExpressionIdentifier': AST.ExpressionIdentifier, - 'AST_ExpressionIndex': AST.ExpressionIndex, - 'AST_ExpressionIntLiteral': AST.ExpressionIntLiteral, - 'AST_ExpressionLeftShiftAssignment': AST.ExpressionLeftShiftAssignment, - 'AST_ExpressionLessThan': AST.ExpressionLessThan, - 'AST_ExpressionLessThanOrEqual': AST.ExpressionLessThanOrEqual, - 'AST_ExpressionLogicalAnd': AST.ExpressionLogicalAnd, - 'AST_ExpressionLogicalNot': AST.ExpressionLogicalNot, - 'AST_ExpressionLogicalOr': AST.ExpressionLogicalOr, - 'AST_ExpressionMinus': AST.ExpressionMinus, - 'AST_ExpressionModulo': AST.ExpressionModulo, - 'AST_ExpressionModuloAssignment': AST.ExpressionModuloAssignment, - 'AST_ExpressionMultiply': AST.ExpressionMultiply, - 'AST_ExpressionMultiplyAssignment': AST.ExpressionMultiplyAssignment, - 'AST_ExpressionNotEqual': AST.ExpressionNotEqual, - 'AST_ExpressionPlus': AST.ExpressionPlus, - 'AST_ExpressionPostDecrement': AST.ExpressionPostDecrement, - 'AST_ExpressionPostIncrement': AST.ExpressionPostIncrement, - 'AST_ExpressionPreDecrement': AST.ExpressionPreDecrement, - 'AST_ExpressionPreIncrement': AST.ExpressionPreIncrement, - 'AST_ExpressionRightShiftAssignment': AST.ExpressionRightShiftAssignment, - 'AST_ExpressionShiftLeft': AST.ExpressionShiftLeft, - 'AST_ExpressionShiftRight': AST.ExpressionShiftRight, - 'AST_ExpressionSizeOfExpression': AST.ExpressionSizeOfExpression, - 'AST_ExpressionSizeOfType': AST.ExpressionSizeOfType, - 'AST_ExpressionStringLiteral': AST.ExpressionStringLiteral, - 'AST_ExpressionSubtract': AST.ExpressionSubtract, - 'AST_ExpressionSubtractAssignment': AST.ExpressionSubtractAssignment, - 'AST_FunctionDefinition': AST.FunctionDefinition, - 'AST_FunctionSpecifier': AST.FunctionSpecifier, - 'AST_GenericAssociation': AST.GenericAssociation, - 'AST_GenericAssociationList': AST.GenericAssociationList, - 'AST_GenericSelection': AST.GenericSelection, - 'AST_Identifier': AST.Identifier, - 'AST_IdentifierEmpty': AST.IdentifierEmpty, - 'AST_IdentifierList': AST.IdentifierList, - 'AST_InitDeclarator': AST.InitDeclarator, - 'AST_InitDeclaratorList': AST.InitDeclaratorList, - 'AST_ParameterDeclaration': AST.ParameterDeclaration, - 'AST_ParameterDeclarationList': AST.ParameterDeclarationList, - 'AST_SpecifierQualifierList': AST.SpecifierQualifierList, - 'AST_StatementBlock': AST.StatementBlock, - 'AST_StatementBreak': AST.StatementBreak, - 'AST_StatementCase': AST.StatementCase, - 'AST_StatementContinue': AST.StatementContinue, - 'AST_StatementDefault': AST.StatementDefault, - 'AST_StatementDoWhile': AST.StatementDoWhile, - 'AST_StatementExpression': AST.StatementExpression, - 'AST_StatementFor': AST.StatementFor, - 'AST_StatementGoto': AST.StatementGoto, - 'AST_StatementIf': AST.StatementIf, - 'AST_StatementIfElse': AST.StatementIfElse, - 'AST_StatementLabel': AST.StatementLabel, - 'AST_StatementReturn': AST.StatementReturn, - 'AST_StatementSwitch': AST.StatementSwitch, - 'AST_StatementWhile': AST.StatementWhile, - 'AST_StaticAssertDeclaration': AST.StaticAssertDeclaration, - 'AST_StorageClassSpecifier': AST.StorageClassSpecifier, - 'AST_StructDeclaration': AST.StructDeclaration, - 'AST_StructDeclarationList': AST.StructDeclarationList, - 'AST_StructDeclarator': AST.StructDeclarator, - 'AST_StructDeclaratorList': AST.StructDeclaratorList, - 'AST_StructSpecifier': AST.StructSpecifier, - 'AST_TypeName': AST.TypeName, - 'AST_TypeQualifier': AST.TypeQualifier, - 'AST_TypeQualifierList': AST.TypeQualifierList, - 'AST_TypeQualifierOrStaticList': AST.TypeQualifierOrStaticList, - 'AST_TypeSpecifier': AST.TypeSpecifier, - 'AST_UnionSpecifier': AST.UnionSpecifier, - 'AST_TranslationUnit': AST.TranslationUnit -} -def factory(tag, attrib = {}, *args, **kwargs): - return tag_to_class.get(tag, element.Element)(tag, attrib, *args, **kwargs) -# GENERATE END diff --git a/ast.sh b/ast.sh deleted file mode 100755 index 2ba2388..0000000 --- a/ast.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -if ./generate_ast.py ast ast.py.new && ! diff -q ast.py ast.py.new -then - mv ast.py.new ast.py -else - rm -f ast.py.new -fi diff --git a/c_to_python.py b/c_to_python.py index 0f2ceb3..32caec8 100755 --- a/c_to_python.py +++ b/c_to_python.py @@ -16,16 +16,16 @@ # this program; if not, write to the Free Software Foundation, Inc., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import ast +import t_def import element import sys import xml.etree.ElementTree import y_tab -root = y_tab.yyparse(ast.AST.TranslationUnit) +root = y_tab.yyparse(t_def.AST.TranslationUnit) element.serialize(root, 'a.xml', 'utf-8') -root = element.deserialize('a.xml', ast.factory, 'utf-8') +root = element.deserialize('a.xml', t_def.factory, 'utf-8') -context = ast.Context() +context = t_def.Context() root.translate_translation_unit(context) sys.stdout.write(''.join(context.lines)) diff --git a/degenerate.py b/degenerate.py deleted file mode 100755 index 96d362a..0000000 --- a/degenerate.py +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python3 - -import re -import sys - -re_begin = re.compile( - '([\t ]*# GENERATE .*) BEGIN' -) -re_end = re.compile( - '([\t ]*# GENERATE END)' -) - -line = sys.stdin.readline() -while len(line): - match = re_begin.match(line) - if match is not None: - sys.stdout.write(match.group(1)) - line = sys.stdin.readline() - while len(line): - match = re_end.match(line) - if match is not None: - sys.stdout.write(line[len(match.group(1)):]) - break - line = sys.stdin.readline() - else: - sys.stdout.write(line) - line = sys.stdin.readline() diff --git a/generate_ast.py b/generate_ast.py deleted file mode 100755 index f65d4f3..0000000 --- a/generate_ast.py +++ /dev/null @@ -1,396 +0,0 @@ -#!/usr/bin/env python3 - -import re -import sys - -if len(sys.argv) >= 2: - package_name = '{0:s}.'.format(sys.argv[1]) -else: - package_name = '' - -default_value = { - 'bool': 'False', - 'int': '-1', - 'ref': 'None', - 'str': '\'\'', - 'list(bool)': '[]', - 'list(int)': '[]', - 'list(ref)': '[]', - 'list(str)': '[]', - 'set(bool)': 'set()', - 'set(int)': 'set()', - 'set(ref)': 'set()', - 'set(str)': 'set()' -} -default_value_str = { - 'bool': 'false', - 'int': '-1', - 'ref': '-1', - 'str': '' -} - -re_class = re.compile( - '([\t ]*)class ([A-Za-z_][A-Za-z0-9_]*)\(([A-Za-z_][A-Za-z0-9_.]*)' -) -re_element = re.compile( - '([\t ]*)# GENERATE ELEMENT\((([^()]|\([^()]*\))*)\)( BEGIN)?' -) -re_factory = re.compile( - '([\t ]*)# GENERATE FACTORY\(([^()]*)\)( BEGIN)?' -) -stack = [] -classes = [] -base_classes = [{'element.Element': []}] # params - -line = sys.stdin.readline() -while len(line): - match = re_class.match(line) - if match is not None: - sys.stdout.write(line) - indent = match.group(1) - class_name = match.group(2) - base_class = match.group(3) - while len(stack) and stack[-1][0][:len(indent)] == indent: - _, temp_class_name, _, _ = stack.pop() - for temp_base_class, temp_fields in base_classes.pop().items(): - base_classes[-1][ - '{0:s}.{1:s}'.format(temp_class_name, temp_base_class) - ] = temp_fields - for i in range(len(base_classes) - 1, -1, -1): - if base_class in base_classes[i]: - classes.append( - '.'.join([j for _, j, _, _ in stack] + [class_name]) - ) - full_base_class = '.'.join( - [j for _, j, _, _ in stack[:i]] + [base_class] - ) - base_classes[-1][class_name] = list(base_classes[i][base_class]) - break - else: - full_base_class = base_class - stack.append((indent, class_name, base_class, full_base_class)) - base_classes.append({}) - else: - match = re_element.match(line) - if match is not None: - indent = match.group(1) - params = match.group(2) - begin = match.group(4) - - while len(stack) and stack[-1][0][:len(indent)] == indent: - _, temp_class_name, _, _ = stack.pop() - for temp_base_class, temp_fields in base_classes.pop().items(): - base_classes[-1][ - '{0:s}.{1:s}'.format(temp_class_name, temp_base_class) - ] = temp_fields - _, class_name, base_class, full_base_class = stack[-1] - - fields = params.split(',') - if fields[-1] == '': - del fields[-1:] - fields = [i.split() for i in fields] - fields = [(type, name) for [type, name] in fields] - i = len(base_classes[-2][class_name]) - base_classes[-2][class_name].extend(fields) - - sys.stdout.write( - '''{0:s}# GENERATE ELEMENT({1:s}) BEGIN -{2:s}def __init__( -{3:s} self, -{4:s} tag = '{5:s}', -{6:s} attrib = {{}}, -{7:s} text = '', -{8:s} children = []{9:s} -{10:s}): -{11:s} {12:s}.__init__( -{13:s} self, -{14:s} tag, -{15:s} attrib, -{16:s} text, -{17:s} children{18:s} -{19:s} ) -{20:s}'''.format( - indent, - params, - indent, - indent, - indent, - '_'.join([i for _, i, _, _ in stack]), - indent, - indent, - indent, - ''.join( - [ - ',\n{0:s} {1:s} = {2:s}'.format( - indent, - name, - default_value[type] - ) - for type, name in base_classes[-2][class_name] - ] - ), - indent, - indent, - full_base_class, - indent, - indent, - indent, - indent, - indent, - ''.join( - [ - ',\n{0:s} {1:s}'.format( - indent, - name - ) - for _, name in base_classes[-2][class_name][:i] - ] - ), - indent, - ''.join( - [ - '{0:s} self.{1:s} = {2:s}\n'.format( - indent, - name, - name - ) - for _, name in fields - ] - ) - ) - ) - if len(fields): - sys.stdout.write( - '''{0:s}def serialize(self, ref_list): -{1:s} {2:s}.serialize(self, ref_list) -'''.format( - indent, - indent, - full_base_class - ) - ) - for type, name in fields: - if type[:5] == 'list(' and type[-1:] == ')': - subtype = type[5:-1] - sys.stdout.write( - '''{0:s} self.set( -{1:s} '{2:s}', -{3:s} ' '.join( -{4:s} [ -{5:s} element.serialize_{6:s}(i{7:s}) -{8:s} for i in self.{9:s} -{10:s} ] -{11:s} ) -{12:s} ) -'''.format( - indent, - indent, - name, - indent, - indent, - indent, - subtype, - ', ref_list' if subtype == 'ref' else '', - indent, - name, - indent, - indent, - indent - ) - ) - elif type[:4] == 'set(' and type[-1:] == ')': - subtype = type[4:-1] - sys.stdout.write( - '''{0:s} self.set( -{1:s} '{2:s}', -{3:s} ' '.join( -{4:s} [ -{5:s} element.serialize_{6:s}(i{7:s}) -{8:s} for i in sorted(self.{9:s}) -{10:s} ] -{11:s} ) -{12:s} ) -'''.format( - indent, - indent, - name, - indent, - indent, - indent, - subtype, - ', ref_list' if subtype == 'ref' else '', - indent, - name, - indent, - indent, - indent - ) - ) - else: - sys.stdout.write( - '''{0:s} self.set( -{1:s} '{2:s}', -{3:s} element.serialize_{4:s}(self.{5:s}{6:s}) -{7:s} ) -'''.format( - indent, - indent, - name, - indent, - type, - name, - ', ref_list' if type == 'ref' else '', - indent - ) - ) - sys.stdout.write( - '''{0:s}def deserialize(self, ref_list): -{1:s} {2:s}.deserialize(self, ref_list) -'''.format( - indent, - indent, - full_base_class - ) - ) - for type, name in fields: - if type[:5] == 'list(' and type[-1:] == ')': - subtype = type[5:-1] - sys.stdout.write( - '''{0:s} self.{1:s} = [ -{2:s} element.deserialize_{3:s}(i{4:s}) -{5:s} for i in self.get('{6:s}', '').split() -{7:s} ] -'''.format( - indent, - name, - indent, - subtype, - ', ref_list' if subtype == 'ref' else '', - indent, - name, - indent - ) - ) - elif type[:4] == 'set(' and type[-1:] == ')': - subtype = type[4:-1] - sys.stdout.write( - '''{0:s} self.{1:s} = set( -{2:s} [ -{3:s} element.deserialize_{4:s}(i{5:s}) -{6:s} for i in self.get('{7:s}', '').split() -{8:s} ] -{9:s} ) -'''.format( - indent, - name, - indent, - indent, - subtype, - ', ref_list' if subtype == 'ref' else '', - indent, - name, - indent, - indent - ) - ) - else: - sys.stdout.write( - '''{0:s} self.{1:s} = element.deserialize_{2:s}(self.get('{3:s}', '{4:s}'){5:s}) -'''.format( - indent, - name, - type, - name, - default_value_str[type], - ', ref_list' if type == 'ref' else '' - ) - ) - sys.stdout.write( - '''{0:s}def copy(self, factory = None): -{1:s} result = {2:s}.copy( -{3:s} self, -{4:s} {5:s} if factory is None else factory -{6:s} ){7:s} -{8:s} return result -'''.format( - indent, - indent, - full_base_class, - indent, - indent, - class_name, - indent, - ''.join( - [ - '\n{0:s} result.{1:s} = self.{2:s}'.format( - indent, - name, - name - ) - for _, name in fields - ] - ), - indent - ) - ) - sys.stdout.write( - '''{0:s}# GENERATE END -'''.format( - indent - ) - ) - if begin is not None: - line = sys.stdin.readline() - while len(line): - if line.strip() == '# GENERATE END': - break - line = sys.stdin.readline() - else: - assert False - else: - match = re_factory.match(line) - if match is not None: - indent = match.group(1) - param = match.group(2) - begin = match.group(3) - - sys.stdout.write( - '''{0:s}# GENERATE FACTORY({1:s}) BEGIN -{2:s}tag_to_class = {{{3:s} -{4:s}}} -{5:s}def factory(tag, attrib = {{}}, *args, **kwargs): -{6:s} return tag_to_class.get(tag, {7:s})(tag, attrib, *args, **kwargs) -{8:s}# GENERATE END -'''.format( - indent, - param, - indent, - ','.join( - [ - '\n{0:s} \'{1:s}\': {2:s}'.format( - indent, - i.replace('.', '_'), - i - ) - for i in classes - ] - ), - indent, - indent, - indent, - param, - indent - ) - ) - - if begin is not None: - line = sys.stdin.readline() - while len(line): - if line.strip() == '# GENERATE END': - break - line = sys.stdin.readline() - else: - assert False - else: - sys.stdout.write(line) - line = sys.stdin.readline() diff --git a/l_to_python.py b/l_to_python.py index 8dd8e44..7d3528b 100755 --- a/l_to_python.py +++ b/l_to_python.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import ast +import t_def import element import lex_yy import os @@ -24,7 +24,7 @@ def c_to_python(context, text): lex_yy.yy_buffer_stack = [lex_yy.YYBufferState()] lex_yy.yytext_len = 0 lex_yy.unput(text) - root = y_tab.yyparse(ast.AST.TranslationUnit) + root = y_tab.yyparse(t_def.AST.TranslationUnit) context.lines = [] root.translate_translation_unit(context) return ''.join(context.lines) @@ -37,7 +37,7 @@ root = xml.etree.ElementTree.parse( ) ).getroot() -context = ast.Context() +context = t_def.Context() actions = [] with open('a.c', 'w') as fout: diff --git a/regex.sh b/regex.sh deleted file mode 100755 index 8cc339a..0000000 --- a/regex.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -if ./generate.py regex regex.py.new && ! diff -q regex.py regex.py.new -then - mv regex.py.new regex.py -else - rm -f regex.py.new -fi diff --git a/y_to_python.py b/y_to_python.py index 8f93615..4e0baee 100755 --- a/y_to_python.py +++ b/y_to_python.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import ast +import t_def import element import lex_yy import os @@ -13,7 +13,7 @@ def c_to_python(context, text): lex_yy.yy_buffer_stack = [lex_yy.YYBufferState()] lex_yy.yytext_len = 0 lex_yy.unput(text) - root = y_tab.yyparse(ast.AST.TranslationUnit) + root = y_tab.yyparse(t_def.AST.TranslationUnit) context.lines = [] root.translate_translation_unit(context) return ''.join(context.lines) @@ -26,7 +26,7 @@ root = xml.etree.ElementTree.parse( ) ).getroot() -context = ast.Context() +context = t_def.Context() actions = [] with open('a.c', 'w') as fout: