Пример #1
0
        public override void GenerateCode(ILGenerator generator)
        {
            Label loopCondition = generator.DefineLabel();
            Label loopEnd       = generator.DefineLabel();
            var   from          = ProgramNode.Program.DefineField(VariableInfo.StaticName, typeof(int),
                                                                  FieldAttributes.Public | FieldAttributes.Static);
            var to = generator.DeclareLocal(typeof(int));

            End = loopEnd;
            FromExpr ession.GenerateCode(generator);     //genera el codigo de la variable from

            generator.Emit(OpCodes.Stsfld, from);
            VariableInfo.FieldBuilder = from;
            ToExpression.GenerateCode(generator);       //genera el codigo de la variable to
            generator.Emit(OpCodes.Stloc, to);
            generator.MarkLabel(loopCondition);         //comienza el for
            generator.Emit(OpCodes.Ldsfld, from);       //guarda from y to en la pila
            generator.Emit(OpCodes.Ldloc, to);
            generator.Emit(OpCodes.Bgt, loopEnd);       //si from > to sal del for
            DoInstruction.GenerateCode(generator);      //si no genera la instruccion
            generator.Emit(OpCodes.Ldsfld, from);
            generator.Emit(OpCodes.Ldc_I4_1);
            generator.Emit(OpCodes.Add);
            generator.Emit(OpCodes.Stsfld, from);       //sumale 1 a from
            generator.Emit(OpCodes.Br, loopCondition);  //vuelve a iterar
            generator.MarkLabel(loopEnd);               //fin del ciclo
        }
Пример #2
0
        public override void CheckSemantics(TigerScope scope, Report report)
        {
            ContainingScope = scope;

            //Check children
            IdNode.CheckSemantics(scope, report);
            FromExpression.CheckSemantics(scope, report);
            ToExpression.CheckSemantics(scope, report);

            if (!IdNode.IsOK || !FromExpression.IsOK || !ToExpression.IsOK)
            {
                return;
            }

            //Check loop bounds type
            if (!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int) || !TigerType.AreCompatible(ToExpression.TigerType, TigerType.Int))
            {
                report.AddError(!TigerType.AreCompatible(FromExpression.TigerType, TigerType.Int)
                    ? SemanticErrors.InvalidForBoundType(FromExpression, FromExpression.TigerType)
                    : SemanticErrors.InvalidForBoundType(ToExpression, ToExpression.TigerType));
                return;
            }

            IsOK = true;

            //Define new scope
            TigerScope childScope = new TigerScope(scope, "For");

            VariableInfo = childScope.DefineVariable(IdNode.Name, TigerType.Int, ContainingScope, false);

            DoInstruction.CheckSemantics(childScope, report);

            if (!DoInstruction.IsOK)
            {
                return;
            }

            if (!TigerType.AreCompatible(DoInstruction.TigerType, TigerType.Void))
            {
                report.AddError(SemanticErrors.InvalidForBodyType(DoInstruction));
                IsOK = false;
            }
        }
 AttributeArgument(ToExpression(arg.Value))
 .Select(static arg => AttributeArgument(ToExpression(arg))).Concat(