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)); }
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()); }