Example #1
0
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            //Save the previous variables
            var currentVariables = new List<string>();
            foreach (var item in symbols.Variables)
            {
                currentVariables.Add(item.Key);
                generator.Emit(OpCodes.Ldsfld, symbols.Variables[item.Key]);
            }

            DeclarationBlock.Generate(generator, symbols);
            InstructionsBlock.Generate(generator, symbols);

            LocalBuilder returnValue = null;
            if (InstructionsBlock.ExpressionType.Type != TypesEnumeration.Void)
            {
                returnValue = generator.DeclareLocal(symbols.GetRealType(InstructionsBlock.ExpressionType.ILName));
                generator.Emit(OpCodes.Stloc, returnValue);
            }

            currentVariables.Reverse();
            //load in the variables it's previous values
            currentVariables.ForEach(x => generator.Emit(OpCodes.Stsfld, symbols.Variables[x]));
            if (InstructionsBlock.ExpressionType.Type != TypesEnumeration.Void)
                generator.Emit(OpCodes.Ldloc, returnValue);
        }
Example #2
0
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            Index.Generate(generator, symbols);
            generator.Emit(OpCodes.Ldelem, symbols.GetRealType(Index.ILName));

            if (NextNested != null)
                NextNested.Generate(generator, symbols);
        }
Example #3
0
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            var varVariable = symbols.Variables[ArrayIdentifier.ILName];
            generator.Emit(OpCodes.Ldsfld, varVariable);
            NextNested.Index.Generate(generator, symbols);
            generator.Emit(OpCodes.Ldelem, symbols.GetRealType(NextNested.Index.ILName));

            if (NextNested.NextNested != null)
                NextNested.NextNested.Generate(generator, symbols);
        }
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            var recordBuilder = symbols.ModuleBuilder.DefineType(Identifier.ILName, TypeAttributes.Public);
            symbols.Records.Add(Identifier.ILName, recordBuilder);

            foreach (var item in FieldDeclarationList)
            {
                item.ILName = Identifier.ILName + item.Field.ILName;
                Type _type = symbols.GetRealType(item.TypeName.ILName);
                FieldBuilder fieldBuilder = recordBuilder.DefineField(item.ILName,
                    _type,
                    FieldAttributes.Public);
                symbols.Variables.Add(item.ILName, fieldBuilder);
            }
            recordBuilder.CreateType();
        }
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            var paramsTypes = Arguments != null ? new Type[Arguments.Count] : System.Type.EmptyTypes;

            for (int i = 0; i < paramsTypes.Length; i++)
            {
                var paramType = symbols.GetRealType(Arguments[i].TypeName.ILName);
                var parameter = symbols.ProgramType.DefineField(
                                            Arguments[i].ILName, paramType,
                                            FieldAttributes.Public |
                                            FieldAttributes.Static);

                symbols.Variables.Add(Arguments[i].ILName, parameter);
                paramsTypes[i] = paramType;
            }

            var returnType = symbols.GetRealType(ReturnType != null ? ReturnType.ILName : "voidScope0");
            var routin = symbols.ProgramType.DefineMethod(Identifier.ILName,
                                                          MethodAttributes.Public | MethodAttributes.Static, returnType,
                                                          paramsTypes);
            var routinIL = routin.GetILGenerator();
            symbols.Routines.Add(Identifier.ILName, routin);

            for (int i = 0; i < paramsTypes.Length; i++) //l_0000
                routinIL.Emit(OpCodes.Ldsfld, symbols.Variables[Arguments[i].ILName]);

            //var currentVariables = new List<string>();
            //foreach (var item in symbols.Variables)
            //{
            //    currentVariables.Add(item.Key);
            //    routinIL.Emit(OpCodes.Ldsfld, symbols.Variables[item.Key]);
            //}

            for (int i = 0; i < paramsTypes.Length; i++)
            {
                routinIL.Emit(OpCodes.Ldarg, i); //ldarg A_0
                routinIL.Emit(OpCodes.Stsfld, symbols.Variables[Arguments[i].ILName]); //l_000b
            }

            if (this is FunctionDeclarationNode)
                ((FunctionDeclarationNode)this).Body.Generate(routinIL, symbols);
            else
                ((ProcedureDeclarationNode)this).Body.Generate(routinIL, symbols);

            LocalBuilder returnVariable = null;
            if (ReturnType != null)
            {
                returnVariable = routinIL.DeclareLocal(returnType);
                routinIL.Emit(OpCodes.Stloc, returnVariable);
            }

            for (int i = paramsTypes.Length - 1; i >= 0; i--)
                routinIL.Emit(OpCodes.Stsfld, symbols.Variables[Arguments[i].ILName]);

            //currentVariables.Reverse();
            //currentVariables.ForEach(x => routinIL.Emit(OpCodes.Stsfld, symbols.Variables[x]));

            if (ReturnType != null)
                routinIL.Emit(OpCodes.Ldloc, returnVariable);

            routinIL.Emit(OpCodes.Ret);

            if (Arguments != null)
                foreach (var argument in Arguments)
                    symbols.Variables.Remove(argument.Field.ILName);
        }
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            Type varType = symbols.GetRealType(Value.ExpressionType.ILName);
            FieldBuilder varVariable = symbols.ProgramType.DefineField(Identifier.ILName, varType,
                                                                       FieldAttributes.Public | FieldAttributes.Static);

            Value.Generate(generator, symbols);
            generator.Emit(OpCodes.Stsfld, varVariable);
            symbols.Variables.Add(Identifier.ILName, varVariable);
        }
        public override void Generate(ILGenerator generator, Symbols symbols)
        {
            var array = generator.DeclareLocal(symbols.ArraysTypes[ArrayIdentifier.ILName]);
            var itemsType = symbols.GetRealType(DefaultValue.ILName);
            var index = generator.DeclareLocal(typeof(int));
            var lenght = generator.DeclareLocal(typeof (int));
            var item = generator.DeclareLocal(itemsType);

            var loop = generator.DefineLabel();
            var end = generator.DefineLabel();

            generator.Emit(OpCodes.Ldc_I4_0);
            generator.Emit(OpCodes.Stloc, index);

            Length.Generate(generator, symbols);
            generator.Emit(OpCodes.Stloc, lenght);

            generator.Emit(OpCodes.Ldloc, lenght);
            generator.Emit(OpCodes.Newarr, itemsType);
            generator.Emit(OpCodes.Stloc, array);

            generator.MarkLabel(loop);
            generator.Emit(OpCodes.Ldloc, index);
            generator.Emit(OpCodes.Ldloc, lenght);
            generator.Emit(OpCodes.Bge, end);

            DefaultValue.Generate(generator, symbols);
            generator.Emit(OpCodes.Stloc, item);

            generator.Emit(OpCodes.Ldloc, array);
            generator.Emit(OpCodes.Ldloc, index);
            generator.Emit(OpCodes.Ldloc, item);
            generator.Emit(OpCodes.Stelem, itemsType);

            generator.Emit(OpCodes.Ldc_I4_1);
            generator.Emit(OpCodes.Ldloc, index);
            generator.Emit(OpCodes.Add);
            generator.Emit(OpCodes.Stloc, index);
            generator.Emit(OpCodes.Br, loop);
            generator.MarkLabel(end);

            generator.Emit(OpCodes.Ldloc, array);
        }
Example #8
0
 private void GenerateIndexAccess(IndexNestedNode current, ILGenerator generator, Symbols symbols)
 {
     if (current.NextNested != null)
     {
         current.Index.Generate(generator, symbols);
         generator.Emit(OpCodes.Ldelem, symbols.GetRealType(current.ILName));
         current = (IndexNestedNode)current.NextNested;
         if (current.NextNested is FieldNestedNode)
             GenerateFieldAccess((FieldNestedNode)current.NextNested, generator, symbols);
         else
             GenerateIndexAccess((IndexNestedNode)current.NextNested, generator, symbols);
     }
     else
     {
         current.Index.Generate(generator, symbols);
         Expr.Generate(generator, symbols);
         generator.Emit(OpCodes.Stelem, symbols.GetRealType(Identifier.ExpressionType.ILName));
     }
 }
Example #9
0
 public override void Generate(ILGenerator generator, Symbols symbols)
 {
     var arrayType = symbols.GetRealType(DefinedType.ILName);
     symbols.ArraysTypes.Add(Identifier.ILName, arrayType);
 }