Esempio n. 1
0
        public static void PushEnumerator(string idString, bool hasConstant)
        {
            CEnumerator enumer = new CEnumerator();

            enumer.Name     = idString;
            enumer.ConstVal = hasConstant ? CExpression.PopExpression() : null;

            PushEnumerator(enumer);
        }
Esempio n. 2
0
        public static void EndWhileLoopStatement()
        {
            CExpression expr = CExpression.PopExpression();
            CStatment   stat = PopStatement();

            //loopCheck:  if(expression) goto loopBody;
            if (!expr.IsScalar)
            {
                throw new SemanticException("condition expression in loop must be scalar");
            }

            CStatment gotoBody = new CStatment(loopBodyLabels.Peek());

            CExpression.PushExpression(expr);
            PushStatement(gotoBody);
            IfStatement(false);

            CStatment ifCheckGotoBody = PopStatement();

            ifCheckGotoBody.AddLabel(loopChechLabels.Peek().Name);

            //goto endloop:
            CStatment gotoEnd = new CStatment(endLoopLabels.Peek());

            //loopBody:   statment;
            CStatment loopBody = stat;

            loopBody.AddLabel(loopBodyLabels.Peek().Name);

            //goto loopCheck;
            CStatment gotoCheck = new CStatment(loopChechLabels.Peek());

            //endLoop:    {};
            CStatment end = new CStatment();

            end.AddLabel(endLoopLabels.Peek().Name);

            PushStatement(ifCheckGotoBody);
            PushStatement(gotoEnd);
            PushStatement(loopBody);
            PushStatement(gotoCheck);
            PushStatement(end);

            ExitLoop();
            EndCompoundStatement(new List <CCompoundStatmentItemType>
            {
                CCompoundStatmentItemType.Statment,
                CCompoundStatmentItemType.Statment,
                CCompoundStatmentItemType.Statment,
                CCompoundStatmentItemType.Statment,
                CCompoundStatmentItemType.Statment
            }
                                 );

            PushStatement(PopStatement());
        }
Esempio n. 3
0
        //TODO clean this up
        //genertic ident
        private CIdentifier(CType type, string name, bool isMember, bool isLocal, int stackOffset, int structOffset)
        {
            this.type               = type;
            this.name               = name;
            this.isMember           = isMember;
            this.islocal            = isLocal;
            this.stackPointerOffset = stackOffset;
            this.structOffset       = structOffset;

            if (!type.IsStruct && !type.IsArray && type.TypeClass != CTypeClass.CFunction && type.TypeClass != CTypeClass.CVoid)
            {
                CExpression.PushConstant("0");
                m_init = new CInitilizer(CExpression.PopExpression());
            }
        }
Esempio n. 4
0
        //http://en.cppreference.com/w/c/language/if
        public static void IfStatement(bool hasElse)
        {
            //expression must be an expression of any scalar type.
            //If expression compares not equal to the integer zero, statement_true is executed.
            //In the else form, if expression compares equal to the integer zero, statement_false is executed.

            //if ( expression ) statement_true
            if (!hasElse)
            {
                PushStatement(new CStatment(CExpression.PopExpression(), PopStatement()));
            }
            //if ( expression ) statement_true else statement_false
            else
            {
                CStatment els = PopStatement();
                CStatment fi  = PopStatement();

                PushStatement(new CStatment(CExpression.PopExpression(), fi, els));
            }
        }
Esempio n. 5
0
        public static void ArrayDeclarator(bool isPointer, int numTypeQualifiers, bool hasAssgnExpr)
        {
            CDeclarator old = PopDeclarator();

            CExpression assgn = hasAssgnExpr ? CExpression.PopExpression() : null;

            List <CDeclarationSpecifier> qualifiers = new List <CDeclarationSpecifier>();

            for (int i = 0; i < numTypeQualifiers; i++)
            {
                qualifiers.Add(PopDeclSpec());
            }

            CDeclarator result = new CDeclarator();

            result.Identifer    = old.Identifer;
            result.TypeModifier = CTypeModifier.ArrayModifier(qualifiers, assgn).ModifyModifier(old.TypeModifier);
            if (isPointer)
            {
                result.TypeModifier = PopPointerModifier().ModifyModifier(result.TypeModifier);
            }

            PushDeclarator(result);
        }
Esempio n. 6
0
        //initilizers

        //scalar init
        public static void Initilizer()
        {
            inits.Push(new CInitilizer(CExpression.PopExpression()));
        }
Esempio n. 7
0
        internal CStatment GetDefinitionStatment()
        {
            if (Init == null)
            {
                return(new CStatment());
            }

            if (type.IsStruct)
            {
                throw new SemanticException("local variables of type struct are not implimentd");
            }
            else if (type.IsArray || Init.IsList)
            {
                throw new SemanticException("local variables of type array are not implimentd");
            }
            else if (type.TypeClass == CTypeClass.CFunction)
            {
                throw new InvalidOperationException("wtf how did you declare a local function varable");
            }
            else if (type.TypeClass == CTypeClass.CVoid)
            {
                throw new InvalidOperationException("wtf how did you declare a local void varable");
            }
            else
            {
                OpCode PushOp;

                if (type.Size == 1)
                {
                    PushOp = OpCode.PUSHB;
                }
                else if (type.Size == 2)
                {
                    PushOp = OpCode.PUSHH;
                }
                else if (type.Size == 4)
                {
                    PushOp = OpCode.PUSHW;
                }
                else
                {
                    throw new InvalidOperationException("local variable of unknown size");
                }

                CStatment initilize = new CStatment();
                initilize.Add(new OpCodeEmitter(PushOp, "0"));

                CExpression localAddr = new CExpression(type, ValueCatagory.LValue);

                localAddr.Add(new OpCodeEmitter(OpCode.PUSHBP));
                localAddr.Add(new OpCodeEmitter(OpCode.PUSHW, stackPointerOffset.ToString()));
                localAddr.Add(new OpCodeEmitter(OpCode.ADD));

                CExpression.PushExpression(localAddr);
                CExpression.PushExpression(Init.Expression);

                CExpression.BasicAssignmentOperator();

                initilize.Add(new CStatment(CExpression.PopExpression()));

                return(initilize);
            }
        }
Esempio n. 8
0
 //http://en.cppreference.com/w/c/language/return
 //Terminates current function and returns specified value to the caller function.
 //If the type of the expression is different from the return type of the function, its value is converted as if by assignment to an object whose type is the return type of the function (must be possible to convert)
 //Reaching the end of a function other than main is equivalent to return;
 //Reaching the end of the main function is equivalent to return 0;
 // see http://en.cppreference.com/w/c/language/main_function
 public static void ReturnStatement(bool retVal)
 {
     PushStatement(new CStatment(retVal ? CExpression.PopExpression() : null, true));
 }
Esempio n. 9
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());
        }
Esempio n. 10
0
 //An expression followed by a semicolon is a statement.
 public static void ExpressionStatement()
 {
     PushStatement(new CStatment(CExpression.PopExpression()));
 }