예제 #1
0
        public static void EndCompoundStatement(List <CCompoundStatmentItemType> itemTypes)
        {
            CIdentifier.ExitBlockScope();

            List <CStatment> blockBody = new List <CStatment>();

            List <int> localVarSizes = new List <int>();

            //the item types come in sequential order (top to bottom), but they are sitting on the stack in reverse order (botom to top)
            //revers the list to deal with this
            itemTypes.Reverse();
            foreach (var itemType in itemTypes)
            {
                if (itemType == CCompoundStatmentItemType.Statment)
                {
                    CStatment stat = PopStatement();
                    blockBody.Add(stat);
                }
                else
                {
                    CDeclaration decl = CDeclaration.PopDecl();

                    localVarSizes.Add(decl.Size);

                    //get a statments that initilizes the local variable (or does nothing if it is not a definition)
                    blockBody.Add(decl.GetDefinitionStatment());
                }
            }

            //put the statments back in top to bottom order
            blockBody.Reverse();

            PushStatement(new CStatment(blockBody, localVarSizes));
        }
예제 #2
0
 public override void ExitInitDeclarator(CParser.InitDeclaratorContext context)
 {
     if (context.initializer() != null)
     {
         CDeclaration.DeclaratorWithInitilizer();
     }
 }
예제 #3
0
 public override void ExitPointer(CParser.PointerContext context)
 {
     if (context.GetText().StartsWith("^"))
     {
         SematicError(context, "blocks extention not supported");
     }
     else
     {
         CDeclaration.PushPointerModifier(GetTypeQualifierListCount(context.typeQualifierList()), context.pointer() != null);
     }
 }
예제 #4
0
        public override void ExitTypeSpecifier(CParser.TypeSpecifierContext context)
        {
            //atomicTypeSpecifier
            if (context.atomicTypeSpecifier() != null)
            {
                SematicError(context, "atomic types not supported");
            }
            //structOrUnionSpecifier
            else if (context.structOrUnionSpecifier() != null)
            {
                if (context.structOrUnionSpecifier().structOrUnion().GetText() == "union")
                {
                    SematicError(context, "unions not supported");
                }
                else if (context.structOrUnionSpecifier().structDeclarationList() != null)
                {
                    string idstring = context.structOrUnionSpecifier().Identifier() != null?context.structOrUnionSpecifier().Identifier().GetText() : null;

                    CDeclaration.EndStructDefinition(idstring);
                }
                else
                {
                    CDeclaration.PushStructDeclaration(context.structOrUnionSpecifier().Identifier().GetText());
                }
            }
            //enumSpecifier
            else if (context.enumSpecifier() != null)
            {
                if (context.enumSpecifier().enumeratorList() != null)
                {
                    string idString = context.enumSpecifier().Identifier() != null?context.enumSpecifier().Identifier().GetText() : null;

                    CDeclaration.EnumDefinition(idString, GetEnumeratorListCount(context.enumSpecifier().enumeratorList()));
                }
                else
                {
                    CDeclaration.PushEnumDeclaration(context.enumSpecifier().Identifier().GetText());
                }
            }
            //'__typeof__' '(' constantExpression ')' --- GCC extension not supported
            else if (context.constantExpression() != null)
            {
                SematicError(context, "gcc typeof not supported");
            }
            //else, a fundamental type
            else if (context.typedefName() != null)
            {
                CDeclaration.PushTypeDefName(context.GetText());
            }
            else
            {
                CDeclaration.PushTypeSpecifier(context.GetText());
            }
        }
예제 #5
0
 public override void EnterTypeSpecifier(CParser.TypeSpecifierContext context)
 {
     //beginstructdef
     if (context.structOrUnionSpecifier() != null)
     {
         if (context.structOrUnionSpecifier().structDeclarationList() != null)
         {
             CDeclaration.BeginStructDefinition();
         }
     }
 }
예제 #6
0
 public override void ExitInitializer(CParser.InitializerContext context)
 {
     if (context.assignmentExpression() != null)
     {
         CDeclaration.Initilizer();
     }
     else
     {
         CDeclaration.InitilizerList(GetInitializerListCount(context.initializerList()));
     }
 }
예제 #7
0
 public override void ExitParameterDeclaration(CParser.ParameterDeclarationContext context)
 {
     if (context.declarationSpecifiers2() != null)
     {
         SematicError(context, "abstract parameter declaration not supported (???????)");
     }
     else
     {
         int numSpecifiers = context.declarationSpecifiers().declarationSpecifier().Count;
         CDeclaration.ParamaterDeclaration(numSpecifiers);
     }
 }
예제 #8
0
        public override void ExitDeclarator(CParser.DeclaratorContext context)
        {
            if (context.gccDeclaratorExtension().Count > 0)
            {
                throw new SemanticException("gcc Declarator Extensions not supported");
            }

            bool isPointer = context.pointer() != null;

            CParser.DirectDeclaratorContext directDecltor = context.directDeclarator();

            if (directDecltor.Identifier() != null)
            {
                CDeclaration.PushIdentifierDeclarator(isPointer, directDecltor.GetText());
            }
            else if (directDecltor.declarator() != null)
            {
                CDeclaration.NestedDeclarator(isPointer);
            }
            else if (directDecltor.GetText().EndsWith("*]"))
            {
                SematicError(context, "VLA not supported");
            }
            else
            {
                if (directDecltor.directDeclarator().Identifier() != null)
                {
                    CDeclaration.PushIdentifierDeclarator(false, directDecltor.directDeclarator().GetText());
                }

                if (directDecltor.GetText().EndsWith(")"))
                {
                    if (directDecltor.identifierList() != null)
                    {
                        SematicError(context, "old style (K&R) style functions not supported");
                    }
                    else if (directDecltor.parameterTypeList() != null)
                    {
                        CDeclaration.EndFunctionDeclarator(isPointer, GetParameterListCount(directDecltor.parameterTypeList().parameterList()));
                    }
                    else
                    {
                        CDeclaration.EndFunctionDeclarator(isPointer, 0);
                    }
                }
                else
                {
                    bool hasAssgnExpr      = directDecltor.assignmentExpression() != null;
                    int  numTypeQualifiers = GetTypeQualifierListCount(directDecltor.typeQualifierList());
                    CDeclaration.ArrayDeclarator(isPointer, numTypeQualifiers, hasAssgnExpr);
                }
            }
        }
예제 #9
0
        //declarators

        public override void EnterDeclarator(CParser.DeclaratorContext context)
        {
            //beginFunctionDeclarator
            CParser.DirectDeclaratorContext directDecltor = context.directDeclarator();

            if (directDecltor.GetText().EndsWith(")"))
            {
                if (directDecltor.identifierList() == null)
                {
                    CDeclaration.BeginFunctionDeclarator();
                }
            }
        }
예제 #10
0
 public override void ExitDeclaration(CParser.DeclarationContext context)
 {
     //declarationSpecifiers initDeclaratorList? ';'
     if (context.declarationSpecifiers() != null)
     {
         CDeclaration.Declaration(GetSpecifierCount(context.declarationSpecifiers()), GetDeclaratorListCount(context.initDeclaratorList()));
     }
     //staticAssertDeclaration
     else
     {
         SematicError(context, "staticAssert not supported");
     }
 }
예제 #11
0
 public override void ExitTypeQualifier(CParser.TypeQualifierContext context)
 {
     CDeclaration.PushTypeQualifier(context.GetText());
 }
예제 #12
0
        public static void EndForLoopStatement(CForInitType initType, bool hasConditionExpression, bool hasIterationExpression)
        {
            List <CCompoundStatmentItemType> compondItems = new List <CCompoundStatmentItemType> {
                CCompoundStatmentItemType.Statment, CCompoundStatmentItemType.Statment
            };

            CStatment loopBody = PopStatement();

            CStatment iterationStatement = hasIterationExpression ? new CStatment(CExpression.PopExpression()) : new CStatment();

            CExpression condition;

            if (hasConditionExpression)
            {
                condition = CExpression.PopExpression();
            }
            else
            {
                CExpression.PushConstant("1");
                condition = CExpression.PopExpression();
            }

            //init_claus

            CStatment initClaus;

            if (initType == CForInitType.Decl)
            {
                initClaus = CDeclaration.PopDecl().GetDefinitionStatment();
            }
            else if (initType == CForInitType.Expression)
            {
                initClaus = new CStatment(CExpression.PopExpression());
            }
            else
            {
                initClaus = new CStatment();
            }

            // contruct loop body
            BeginCompoundStatement();
            PushStatement(loopBody);
            PushStatement(iterationStatement);
            EndCompoundStatement(compondItems);

            CStatment loopbody = PopStatement();

            // while(cond_expression) loopbody
            CExpression.PushExpression(condition);
            PushStatement(loopBody);
            BeginWhileLoopStatement();
            EndWhileLoopStatement();

            CStatment whilestat = PopStatement();

            PushStatement(initClaus);
            PushStatement(whilestat);

            EndCompoundStatement(compondItems);

            PushStatement(PopStatement());
        }
예제 #13
0
        //declarationSpecifiers

        public override void ExitStorageClassSpecifier(CParser.StorageClassSpecifierContext context)
        {
            CDeclaration.PushStorageClassSpecifier(context.GetText());
        }
예제 #14
0
 public override void ExitFunctionDefinition(CParser.FunctionDefinitionContext context)
 {
     //declarationSpecifiers? declarator declarationList? compoundStatement
     CDeclaration.EndFunctionDefinition(GetSpecifierCount(context.declarationSpecifiers()));
 }
예제 #15
0
        //DECLARATIONS

        public override void EnterFunctionDefinition(CParser.FunctionDefinitionContext context)
        {
            CDeclaration.BeginFunctionDefinition();
        }
예제 #16
0
 private static void PushDecl(CDeclaration decl)
 {
     decls.Push(decl);
 }
예제 #17
0
 public override void ExitEnumerator(CParser.EnumeratorContext context)
 {
     CDeclaration.PushEnumerator(context.enumerationConstant().Identifier().GetText(), context.constantExpression() != null);
 }