Esempio n. 1
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent  = values.CommandAt(entry);
            bool         debug = cent.DBMode <= DebugMode.FULL;
            MapTag       mt    = MapTag.For(cent.Arguments[0].ToString());

            if (mt.Internal.Count == 0)
            {
                throw new ErrorInducedException("Empty map input to require!");
            }
            foreach (string varn in mt.Internal.Keys)
            {
                values.LoadQueue();
                values.LoadEntry(entry);
                values.LoadLocalVariable(cent.VarLoc(varn));
                values.ILGen.Emit(OpCodes.Ldstr, varn);
                values.ILGen.Emit(OpCodes.Call, REQUIRECOMMAND_CHECKFORVALIDITY);
            }
            if (debug)
            {
                values.LoadQueue();
                values.LoadEntry(entry);
                values.ILGen.Emit(OpCodes.Call, REQUIRECOMMAND_OUTPUTSUCCESS);
            }
        }
Esempio n. 2
0
        // NOTE: Intentionally no meta!

        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            // TODO: Type verification? (Can this type be modified in the way being attempted?)
            values.MarkCommand(entry);
            CommandEntry cent  = values.Entry.Entries[entry];
            bool         debug = cent.DBMode.ShouldShow(DebugMode.FULL);
            string       vn    = cent.Arguments[0].ToString().ToLowerFast();

            string[] split   = vn.Split('.');
            string   mainVar = split[0];

            if (!cent.VarLookup.TryGetValue(mainVar, out SingleCILVariable locVar))
            {
                throw new ErrorInducedException("Unknown variable name '" + mainVar + "' - cannot set its value.");
            }
            TagReturnType varType       = locVar.Type;
            string        mode          = cent.Arguments[1].ToString();
            var           operationType = mode switch
            {
                "=" => ObjectOperation.SET,
                "+=" => ObjectOperation.ADD,
                "-=" => ObjectOperation.SUBTRACT,
                "*=" => ObjectOperation.MULTIPLY,
                "/=" => ObjectOperation.DIVIDE,
                _ => throw new ErrorInducedException("That setter mode (" + mode + ") does not exist!"),
            };

            if (split.Length > 1)
            {
                values.LoadLocalVariable(locVar.Index);
                if (locVar.Type.IsRaw)
                {                                                                               // TODO: Work directly with raws
                    values.ILGen.Emit(OpCodes.Newobj, locVar.Type.Type.RawInternalConstructor); // Handle raw translation if needed.
                }
                if (split.Length == 2)
                {
                    values.ILGen.Emit(OpCodes.Dup);
                }
                values.ILGen.Emit(OpCodes.Ldstr, split[1]);
                if (varType.Type.Operation_GetSubSettable.Method.GetParameters().Length == 3)
                {
                    values.LoadQueue();
                    values.LoadEntry(entry);
                    values.ILGen.Emit(OpCodes.Call, Method_GetOES);
                }
                values.ILGen.Emit(OpCodes.Call, varType.Type.Operation_GetSubSettable.Method);
                for (int i = 2; i < split.Length; i++)
                {
                    if (i + 1 == split.Length)
                    {
                        values.ILGen.Emit(OpCodes.Dup);
                    }
                    values.LoadEntry(entry);
                    values.LoadQueue();
                    values.ILGen.Emit(OpCodes.Ldstr, split[i]);
                    values.ILGen.Emit(OpCodes.Call, Method_GetSubObject);
                }
                values.LoadArgumentObject(entry, 2);
                values.ILGen.Emit(OpCodes.Ldstr, split[^ 1]);
Esempio n. 3
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.Entry.Entries[entry];

            if (cent.IsCallback)
            {
                values.MarkCommand(entry);
                // TODO: Debug?
                int dodgepoint = cent.BlockEnd + 2;
                while (dodgepoint < values.Entry.Entries.Length)
                {
                    CommandEntry tent = values.Entry.Entries[dodgepoint];
                    if (tent.Command is ElseCommand)
                    {
                        dodgepoint = tent.BlockEnd + 2;
                    }
                    else
                    {
                        break;
                    }
                }
                values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[dodgepoint]);
                return;
            }
            if (cent.BlockEnd <= 0)
            {
                throw new Exception("Incorrectly defined IF command: no block follows!");
            }
            values.MarkCommand(entry);
            values.LoadQueue();
            values.LoadEntry(entry);
            values.ILGen.Emit(OpCodes.Call, TryIfCILMethod);
            values.ILGen.Emit(OpCodes.Brfalse, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]);
        }
Esempio n. 4
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The relevant entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.CommandAt(entry);

            if (cent.DBMode == DebugMode.FULL)
            {
                values.MarkCommand(entry);
                values.LoadQueue();
                values.LoadEntry(entry);
                values.ILGen.Emit(OpCodes.Call, Method_DebugOutput);
            }
        }
Esempio n. 5
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.CommandAt(entry);
            bool         db   = cent.DBMode <= DebugMode.FULL;

            if (db)
            {
                values.LoadQueue();
                values.LoadEntry(entry);
                values.ILGen.Emit(OpCodes.Call, DebugStopMethod);
            }
            values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[^ 1]);
Esempio n. 6
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The relevant entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent  = values.CommandAt(entry);
            bool         debug = cent.DBMode.ShouldShow(DebugMode.FULL);

            values.MarkCommand(entry);
            TagReturnType     returnType = ArgumentCompiler.ReturnType(cent.Arguments[2], values);
            string            lvarname   = cent.Arguments[0].ToString().ToLowerFast();
            SingleCILVariable locVar     = cent.VarLookup[lvarname];

            // This method:
            // runnable.Var = TYPE.CREATE_FOR(tagdata, runnable.entry.arg2());
            // or:
            // runnable.Var = runnable.entry.arg2();
            values.LoadRunnable();
            values.LoadArgumentObject(entry, 2);
            if (cent.Arguments.Length > 4)
            {
                string        type_name           = cent.Arguments[4].ToString().ToLowerFast();
                TagType       specifiedType       = cent.System.TagSystem.Types.RegisteredTypes[type_name];
                TagReturnType specifiedReturnType = new(specifiedType, specifiedType.Meta.RawInternal);
                values.EnsureType(returnType, specifiedReturnType); // Ensure the correct object type.
                returnType = specifiedReturnType;
            }
            else
            {
                TagReturnType specifiedReturnType = new(returnType.Type, returnType.Type.Meta.RawInternal);
                values.EnsureType(returnType, specifiedReturnType); // Ensure the correct object type.
                returnType = specifiedReturnType;
            }
            values.ILGen.Emit(OpCodes.Stfld, locVar.Field); // Push the result into the local var
            if (debug)                                      // If in debug mode...
            {
                values.LoadLocalVariable(locVar.Index);     // Load variable.
                if (returnType.IsRaw)
                {
                    values.ILGen.Emit(OpCodes.Newobj, returnType.Type.RawInternalConstructor); // Handle raw translation if needed.
                }
                values.ILGen.Emit(OpCodes.Ldstr, lvarname);                                    // Load the variable name as a string.
                values.ILGen.Emit(OpCodes.Ldstr, returnType.Type.TypeName);                    // Load the variable type name as a string.
                values.LoadQueue();                                                            // Load the queue
                values.LoadEntry(entry);                                                       // Load the entry
                values.ILGen.Emit(OpCodes.Call, Method_DebugHelper);                           // Call the debug method
            }
        }
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.CommandAt(entry);
            bool         db   = cent.DBMode <= DebugMode.FULL;

            if (cent.IsCallback)
            {
                int lvar_ind_loc = GetSaveLoc(values, entry);
                values.LoadQueue();
                if (db)
                {
                    values.LoadEntry(entry);
                }
                else
                {
                    values.ILGen.Emit(OpCodes.Ldc_I4, cent.BlockStart - 1);
                }
                values.LoadLocalVariable(lvar_ind_loc);
                values.ILGen.Emit(OpCodes.Call, db ? TryForeachCILMethod : TryForeachCILMethodNoDebug);
                values.ILGen.Emit(OpCodes.Brtrue, values.Entry.AdaptedILPoints[cent.BlockStart]);
                return;
            }
            string arg = cent.Arguments[0].ToString();

            if (arg == "stop")
            {
                for (int i = entry - 1; i >= 0; i--)
                {
                    CommandEntry nextEntry = values.Entry.Entries[i];
                    if (nextEntry.Command is not ForeachCommand || nextEntry.IsCallback)
                    {
                        continue;
                    }
                    string a0 = nextEntry.Arguments[0].ToString();
                    if (a0 == "start" && nextEntry.InnerCommandBlock != null)
                    {
                        if (db)
                        {
                            values.LoadQueue();
                            values.LoadEntry(entry);
                            values.ILGen.Emit(OpCodes.Call, DebugStopMethod);
                        }
                        values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 2]);
                        return;
                    }
                }
                throw new ErrorInducedException("Invalid 'foreach stop' command: not inside a foreach block!");
            }
            else if (arg == "next")
            {
                for (int i = entry - 1; i >= 0; i--)
                {
                    CommandEntry nextEntry = values.Entry.Entries[i];
                    if (nextEntry.Command is not ForeachCommand || nextEntry.IsCallback)
                    {
                        continue;
                    }
                    string a0 = nextEntry.Arguments[0].ToString();
                    if (a0 == "start" && nextEntry.InnerCommandBlock != null)
                    {
                        if (db)
                        {
                            values.LoadQueue();
                            values.LoadEntry(entry);
                            values.ILGen.Emit(OpCodes.Call, DebugNextMethod);
                        }
                        values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 1]);
                        return;
                    }
                }
                throw new ErrorInducedException("Invalid 'foreach next' command: not inside a foreach block!");
            }
            else if (arg == "start")
            {
                SingleCILVariable locVar = cent.VarLookup[cent.GetSaveNameNoParse("foreach_value")];
                values.LoadQueue();
                values.LoadEntry(entry);
                values.LoadRunnable();
                values.ILGen.Emit(OpCodes.Call, CreateListItemMethod);
                values.ILGen.Emit(OpCodes.Stfld, locVar.Field);
                values.LoadLocalVariable(locVar.Index);
                values.ILGen.Emit(OpCodes.Call, db ? TryForeachNumberedCILMethod : TryForeachNumberedCIL_NoDebugMethod);
                values.ILGen.Emit(OpCodes.Brfalse, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]);
            }
            else
            {
                throw new ErrorInducedException("Invalid 'foreach' command: unknown argument: " + arg);
            }
        }
Esempio n. 8
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The present entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.CommandAt(entry);
            bool         db   = cent.DBMode <= DebugMode.FULL;

            if (cent.IsCallback)
            {
                string sn           = values.Entry.Entries[cent.BlockStart - 1].GetSaveNameNoParse("while_index");
                int    lvar_ind_loc = cent.VarLoc(sn);
                values.LoadQueue();
                if (db)
                {
                    values.LoadEntry(entry);
                }
                else
                {
                    values.ILGen.Emit(OpCodes.Ldc_I4, cent.BlockStart - 1);
                }
                values.LoadLocalVariable(lvar_ind_loc);
                values.ILGen.Emit(OpCodes.Call, db ? TryWhileCILMethod : TryWhileCILMethodNoDebug);
                values.ILGen.Emit(OpCodes.Brtrue, values.Entry.AdaptedILPoints[cent.BlockStart]);
                return;
            }
            string arg = cent.Arguments[0].ToString();

            if (arg == "stop")
            {
                for (int i = entry - 1; i >= 0; i--)
                {
                    CommandEntry nextEntry = values.Entry.Entries[i];
                    if (nextEntry.Command is not WhileCommand || nextEntry.IsCallback)
                    {
                        continue;
                    }
                    string a0 = nextEntry.Arguments[0].ToString();
                    if (a0 != "stop" && a0 != "next" && nextEntry.InnerCommandBlock != null)
                    {
                        if (db)
                        {
                            values.LoadQueue();
                            values.LoadEntry(entry);
                            values.ILGen.Emit(OpCodes.Call, DebugStopMethod);
                        }
                        values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 2]);
                        return;
                    }
                }
                throw new ErrorInducedException("Invalid 'while stop' command: not inside a while block!");
            }
            else if (arg == "next")
            {
                for (int i = entry - 1; i >= 0; i--)
                {
                    CommandEntry nextEntry = values.Entry.Entries[i];
                    if (nextEntry.Command is not WhileCommand || nextEntry.IsCallback)
                    {
                        continue;
                    }
                    string a0 = nextEntry.ToString();
                    if (a0 != "stop" && a0 != "next" && nextEntry.InnerCommandBlock != null)
                    {
                        if (db)
                        {
                            values.LoadQueue();
                            values.LoadEntry(entry);
                            values.ILGen.Emit(OpCodes.Call, DebugNextMethod);
                        }
                        values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[nextEntry.BlockEnd + 1]);
                        return;
                    }
                }
                throw new ErrorInducedException("Invalid 'while next' command: not inside a while block!");
            }
            else
            {
                values.LoadQueue();
                values.LoadEntry(entry);
                values.ILGen.Emit(OpCodes.Call, CreateIndexObjectMethod);
                values.ILGen.Emit(OpCodes.Stfld, cent.VarLookup[cent.GetSaveNameNoParse("while_index")].Field);
                values.ILGen.Emit(OpCodes.Call, db ? TryWhileNumberedCILMethod : TryWhileNumberedCIL_NoDebugMethod);
                values.ILGen.Emit(OpCodes.Brfalse, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]);
            }
        }
Esempio n. 9
0
        /// <summary>Adapts a command entry to CIL.</summary>
        /// <param name="values">The adaptation-relevant values.</param>
        /// <param name="entry">The relevant entry ID.</param>
        public override void AdaptToCIL(CILAdaptationValues values, int entry)
        {
            CommandEntry cent = values.Entry.Entries[entry];
            bool         db   = cent.DBMode <= DebugMode.FULL;

            if (cent.IsCallback)
            {
                if (db)
                {
                    values.LoadQueue();
                    values.LoadEntry(entry);
                    values.ILGen.Emit(OpCodes.Call, DebugCallbackMethod);
                }
                return;
            }
            string arg0 = cent.Arguments[0].ToString();

            if (arg0 == "stop")
            {
                for (int i = entry - 1; i >= 0; i--)
                {
                    CommandEntry nextEntry = values.Entry.Entries[i];
                    if (nextEntry.Command is not FunctionCommand)
                    {
                        continue;
                    }
                    if (nextEntry.IsCallback)
                    {
                        if (db)
                        {
                            values.LoadQueue();
                            values.LoadEntry(entry);
                            values.ILGen.Emit(OpCodes.Call, DebugStopMethod);
                        }
                        values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[i + 2]);
                        return;
                    }
                }
                throw new ErrorInducedException("Invalid 'function stop' command: not inside a function block!");
            }
            else if (arg0 == "undefine")
            {
                if (cent.Arguments.Length < 2)
                {
                    throw new ErrorInducedException("Invalid 'function undefine' command: must label what function to undefine (as an additional argument)!");
                }
                values.LoadEntry(entry);
                values.LoadQueue();
                values.ILGen.Emit(OpCodes.Call, UndefineMethod);
            }
            else // "define"
            {
                if (cent.Arguments.Length < 2)
                {
                    throw new ErrorInducedException("Invalid 'function define' command: must name the function to define (as an additional argument)!");
                }
                if (cent.InnerCommandBlock == null)
                {
                    throw new ErrorInducedException("Invalid 'function define' command: must be followed by a block-function to define!");
                }
                values.LoadEntry(entry);
                values.LoadQueue();
                values.ILGen.Emit(OpCodes.Call, DefineMethod);
                values.ILGen.Emit(OpCodes.Br, values.Entry.AdaptedILPoints[cent.BlockEnd + 2]);
            }
        }