Ejemplo n.º 1
0
        public override Type Execute(CompilingContext c, Statement statement)
        {
            T s = (T)statement;

            _action(c, s);
            return(typeof(Null));
        }
Ejemplo n.º 2
0
        private static void MakeReturn(CompilingContext c)
        {
            c.IL.MarkLabel(c.ReturnLabel);

            if (c.ReturnLabelsCount == 0)
            {
                return;
            }

            // check if stack is empty
            Label okLabel = c.IL.DefineLabel();

            c.IL.Emit(OpCodes.Ldloc, c.GosubStack);
            c.IL.Emit(OpCodes.Callvirt, typeof(Stack <int>).GetMethod("get_Count"));
            c.IL.Emit(OpCodes.Brtrue, okLabel);
            c.IL.Emit(OpCodes.Ldstr, "RETURN without GOSUB");
            c.IL.Emit(OpCodes.Newobj, typeof(Exception).GetConstructor(new[] { typeof(string) }));
            c.IL.Emit(OpCodes.Throw);

            c.IL.MarkLabel(okLabel);

            LocalBuilder popValue = c.IL.DeclareLocal(typeof(int));

            c.IL.Emit(OpCodes.Ldloc, c.GosubStack);
            c.IL.Emit(OpCodes.Call, typeof(Stack <int>).GetMethod("Pop"));
            c.IL.Emit(OpCodes.Stloc, popValue);

            foreach (int label in c.ReturnLabels())
            {
                Label returnLabel = c.GetLabel(label);
                c.IL.Emit(OpCodes.Ldloc, popValue);
                c.IL.Emit(OpCodes.Ldc_I4, label);
                c.IL.Emit(OpCodes.Beq, returnLabel);
            }
        }
Ejemplo n.º 3
0
        public void Prepare(CompilingContext c, Statement statement)
        {
            c.IL.Emit(OpCodes.Ldarg_0);
            c.IL.Emit(OpCodes.Ldfld, GetField(c, statement));

            VariableArray v = (VariableArray)statement;

            foreach (NumStatement n in v.Dimensions)
            {
                c.Factory.Execute(c, n);
                c.IL.Emit(OpCodes.Conv_I4);
            }
        }
Ejemplo n.º 4
0
        public override Type Execute(CompilingContext c, Statement statement)
        {
            Prepare(c, statement);

            VariableArray arr       = (VariableArray)statement;
            var           callTypes = new Type[arr.Dimensions.Count];

            for (int i = 0; i < callTypes.Length; ++i)
            {
                callTypes[i] = typeof(int);
            }

            c.IL.Emit(OpCodes.Call, GetField(c, statement).FieldType.GetMethod("Get", callTypes));
            return(typeof(VarType));
        }
Ejemplo n.º 5
0
        public void SetValue(CompilingContext c, Statement statement)
        {
            Type          fieldType = GetField(c, statement).FieldType;
            VariableArray arr       = (VariableArray)statement;
            var           callTypes = new Type[arr.Dimensions.Count + 1];

            for (int i = 0; i < callTypes.Length - 1; ++i)
            {
                callTypes[i] = typeof(int);
            }

            callTypes[callTypes.Length - 1] = typeof(VarType);
            MethodInfo set = fieldType.GetMethod("Set", callTypes);

            c.IL.Emit(OpCodes.Call, set);
        }
Ejemplo n.º 6
0
        private static void CheckJumps(CompilingContext context, TokenizerOutput program)
        {
            for (int i = 0; i < program.Program.Count; ++i)
            {
                Statement s = program.Program[i].Statement;
                if (s is KwrJump)
                {
                    if (s is KwrGosub)
                    {
                        context.MakeReturnLabel(((KwrGosub)s).ReturnAddress);
                    }

                    KwrJump jmp = s as KwrJump;
                    context.MakeLabel(jmp.JumpPos);
                }
            }
        }
Ejemplo n.º 7
0
        private static void DoCompile(CompilerContext outerContext, TokenizerOutput to)
        {
            var c = new CompilingContext {
                IL      = outerContext.ProgramIL,
                Out     = outerContext.DefaultOutput,
                Program = outerContext.ProgramType
            };

            CheckJumps(c, to);

            // string conversion stuff
            c.DoubleConversionVar = c.IL.DeclareLocal(typeof(Double));
            c.CharConversionVar   = c.IL.DeclareLocal(typeof(char));

            // data / read
            c.DataList         = outerContext.ProgramDataList;
            c.DataPointer      = c.Program.DefineField("DataPointer", typeof(int), FieldAttributes.Public);
            c.DataTempVariable = c.IL.DeclareLocal(typeof(string));
            c.IL.Emit(OpCodes.Ldarg_0);
            c.IL.Emit(OpCodes.Ldc_I4, 0);
            c.IL.Emit(OpCodes.Stfld, c.DataPointer);

            // gosub/return stack
            c.GosubStack = c.IL.DeclareLocal(typeof(Stack <int>));
            c.IL.Emit(OpCodes.Newobj, typeof(Stack <int>).GetConstructor(Type.EmptyTypes));
            c.IL.Emit(OpCodes.Stloc, c.GosubStack);

            // rnd
            c.Rnd = c.IL.DeclareLocal(typeof(Random));
            c.IL.Emit(OpCodes.Newobj, typeof(Random).GetConstructor(Type.EmptyTypes));
            c.IL.Emit(OpCodes.Stloc, c.Rnd);

            c.EndLabel    = c.IL.DefineLabel();
            c.ReturnLabel = c.IL.DefineLabel();

            // switch off the cursor
            //c.IL.Emit( OpCodes.Ldc_I4_0 );
            //c.IL.Emit( OpCodes.Call, typeof( Console ).GetMethod( "set_CursorVisible" ) );

            Factory f = GetFactory();

            c.Factory = f;
            for (int i = 0; i < to.Program.Count; ++i)
            {
                TLine line = to.Program[i];
                c.MarkLabelIfNeeded(i);
                try {
                    f.Execute(c, line.Statement);
                }
                catch (Exception e) {
                    Console.WriteLine("Error : " + e.Message);
                    Console.WriteLine("Line: " + (line.OriginalLine.LineNum ?? -1).ToString());
                    Console.WriteLine(line.OriginalLine.OriginalLine);
                }
            }

            // switch the cursor on
            //c.IL.Emit( OpCodes.Ldc_I4_1 );
            //c.IL.Emit( OpCodes.Call, typeof( Console ).GetMethod( "set_CursorVisible" ) );

            MakeReturn(c);

            // return
            c.IL.MarkLabel(c.EndLabel);
            c.IL.Emit(OpCodes.Ldc_I4_1);
            c.IL.Emit(OpCodes.Ret);
        }
Ejemplo n.º 8
0
        private static FieldBuilder GetField(CompilingContext c, Statement statement)
        {
            Variable v = (Variable)statement;

            return(c.GetArray(v.Name));
        }
Ejemplo n.º 9
0
 public void SetValue(CompilingContext c, Statement statement)
 {
     c.IL.Emit(OpCodes.Stfld, GetField(c, statement));
 }
Ejemplo n.º 10
0
 public void Prepare(CompilingContext c, Statement statement)
 {
     c.IL.Emit(OpCodes.Ldarg_0);
 }
Ejemplo n.º 11
0
 public override Type Execute(CompilingContext c, Statement statement)
 {
     Prepare(c, statement);
     c.IL.Emit(OpCodes.Ldfld, GetField(c, statement));
     return(typeof(VarType));
 }
Ejemplo n.º 12
0
        private static FieldBuilder GetField(CompilingContext c, Statement statement)
        {
            Variable v = (Variable)statement;

            return(c.GetVariable(v.Name, typeof(VarType)));
        }
Ejemplo n.º 13
0
 public override Type Execute(CompilingContext c, Statement statement)
 {
     base.Execute(c, statement);
     return(typeof(RetType));
 }
Ejemplo n.º 14
0
 public abstract Type Execute(CompilingContext c, Statement statement);
Ejemplo n.º 15
0
        public Type Execute(CompilingContext c, Statement s)
        {
            CItem ret = Make(s.GetType().Name);

            return(ret.Execute(c, s));
        }