コード例 #1
0
ファイル: StackOperations.cs プロジェクト: AndrewNeo/muftec
        public static void StackDepth(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Result = new MuftecStackItem();
            Result.Type = MuftecType.Integer;
            Result.Item = RuntimeStack.Count;

            RuntimeStack.Push(Result);
        }
コード例 #2
0
ファイル: Conversion.cs プロジェクト: AndrewNeo/muftec
        public static void FloatToString(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.Float);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.String;
            Result.Item = Item1.Item.ToString();

            RuntimeStack.Push(Result);
        }
コード例 #3
0
ファイル: Conversion.cs プロジェクト: AndrewNeo/muftec
        public static void FloatToIntTruncate(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.Float);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Integer;
            Result.Item = (int)Item1.Item;

            RuntimeStack.Push(Result);
        }
コード例 #4
0
ファイル: Conversion.cs プロジェクト: AndrewNeo/muftec
        public static void FloatToIntRound(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.Float);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Integer;
            Result.Item = (int)System.Math.Round((double)Item1.Item,0);

            RuntimeStack.Push(Result);
        }
コード例 #5
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void InStr(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Integer;
            Result.Item = ((string)Item1.Item).IndexOf((string)Item2.Item);
            RuntimeStack.Push(Result);
        }
コード例 #6
0
ファイル: Conversion.cs プロジェクト: AndrewNeo/muftec
        public static void IntToFloat(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.Integer);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Float;
            Result.Item = (double)Item1.Item;

            RuntimeStack.Push(Result);
        }
コード例 #7
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void Concatenate(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.String;
            Result.Item = String.Concat((string)Item1.Item,(string)Item2.Item);

            RuntimeStack.Push(Result);
        }
コード例 #8
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void Explode(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();
            string[] Exploded = ((string)Item1.Item).Split((char)Item2.Item);

            Result.Type = MuftecType.Integer;
            Result.Item = Exploded.Length;
            RuntimeStack.Push(Result);

            foreach (string s in Exploded)
            {
                Result.Type = MuftecType.String;
                Result.Item = s;
                RuntimeStack.Push(Result);
            }
        }
コード例 #9
0
ファイル: TestShared.cs プロジェクト: AndrewNeo/muftec
        private static void CompareArrays(MuftecStackItem[] actualArr, MuftecStackItem[] expectedArr)
        {
            Assert.AreEqual(expectedArr.Length, actualArr.Length);

            for (var i = 0; i < expectedArr.Length; i++)
            {
                Assert.AreEqual(expectedArr[i].Type, actualArr[i].Type);

                // Handle specific AreEqual cases
                switch (expectedArr[i].Type)
                {
                    case MuftecType.Float:
                        var expectedFloat = expectedArr[i].AsDouble();
                        var actualFloat = actualArr[i].AsDouble();
                        Assert.AreEqual(expectedFloat, actualFloat, 0.00000001); // 8 digits of precision
                        break;
                    case MuftecType.String:
                        var expectedStr = (string)expectedArr[i].Item;
                        var actualStr = (string)actualArr[i].Item;
                        Assert.AreEqual(expectedStr, actualStr);
                        break;
                    case MuftecType.List:
                        var expectedList = (MuftecList) expectedArr[i].Item;
                        var actualList = (MuftecList) actualArr[i].Item;
                        Assert.IsTrue(actualList.SequenceEqual(expectedList));
                        break;
                    case MuftecType.Dictionary:
                        var expectedArr3 = MashDict((MuftecDict) expectedArr[i].Item);
                        var actualArr3 = MashDict((MuftecDict) expectedArr[i].Item);
                        CompareArrays(actualArr3, expectedArr3);
                        break;
                    default:
                        Assert.AreEqual(expectedArr[i].Item, actualArr[i].Item);
                        break;
                }
            }

            // Test against expected comperator last since it's useless for test debugging
            Assert.IsTrue(expectedArr.SequenceEqual(actualArr));
        }
コード例 #10
0
ファイル: Math.cs プロジェクト: AndrewNeo/muftec
        public static void AbsoluteVal(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Result = new MuftecStackItem();

            if (Item1.Type == MuftecType.Float)
            {
                Result.Type = MuftecType.Float;
                Result.Item = (double)System.Math.Abs((double)Item1.Item);
            }
            else if (Item1.Type == MuftecType.Integer)
            {
                Result.Type = MuftecType.Integer;
                Result.Item = (int)System.Math.Abs((int)Item1.Item);
            }
            else
            {
                throw new MuftecInvalidStackItemTypeException(ref RuntimeStack);
            }

            RuntimeStack.Push(Result);
        }
コード例 #11
0
ファイル: Logic.cs プロジェクト: AndrewNeo/muftec
        public static void GreaterThan(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Result = new MuftecStackItem();

            if ((Item1.Type == MuftecType.Float) || (Item2.Type == MuftecType.Float))
            {
                Result.Type = MuftecType.Float;
                Result.Item = (double)Item1.Item > (double)Item2.Item;
            }
            else if ((Item1.Type == MuftecType.Integer) || (Item2.Type == MuftecType.Integer))
            {
                Result.Type = MuftecType.Integer;
                Result.Item = (int)Item1.Item > (int)Item2.Item;
            }
            else
            {
                throw new MuftecInvalidStackItemTypeException(ref RuntimeStack);
            }

            RuntimeStack.Push(Result);
        }
コード例 #12
0
ファイル: Logic.cs プロジェクト: AndrewNeo/muftec
        public static void IsString(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Integer;
            if (Item1.Type == MuftecType.String)
            {
                Result.Item = 1;
            }
            else
            {
                Result.Item = 0;
            }

            RuntimeStack.Push(Result);
        }
コード例 #13
0
ファイル: Fabricator.cs プロジェクト: AndrewNeo/muftec
        private void GenerateFunctionInner(Emit<MuftecFunction> funcDef, Queue<MuftecStackItem> execStack, Local runtimeStack, MuftecStackItem lastItem = default(MuftecStackItem))
        {
            DebugMsg("- Call stack -> {0}", String.Join(", ", execStack.ToArray()));

            var stackPush = typeof (Stack<MuftecStackItem>).GetMethod("Push");

            while (execStack.Count > 0)
            {
                var currStackItem = execStack.Dequeue();
                DebugMsg("- Popping stack item: " + currStackItem.ToDebugString());

                switch (currStackItem.Type)
                {
                    // Run a user defined function
                    case MuftecType.Function:
                        // Find the function and create an IL call
                        var funcName = currStackItem.Item.ToString();
                        DebugMsg(" -- Call function {0}", funcName);
                        var func = _funcCache[funcName];
                        funcDef.LoadArgument(0); // Load OpCodeData into stack
                        funcDef.Call(func);
                        break;

                    // Execute a library opcode
                    case MuftecType.OpCode:
                        // Translate opcode into direct call
                        var opCodeName = currStackItem.Item.ToString();
                        DebugMsg(" >> Call opcode {0}", opCodeName);
                        var opCode = _system.FindOpCode(opCodeName);

                        // If this is an internal opcode, we need to handle it at this level
                        if (opCode.Attribute.Extern)
                        {
                            switch (opCode.Attribute.OpCodeName)
                            {
                                case "!":
                                case "@":
                                    throw new MuftecCompilerException("Variables not supported in the fabricator at line " + currStackItem.LineNumber);
                                case "loadlibdll":
                                    funcDef.LoadLocal(runtimeStack);
                                    funcDef.Call(typeof (Shared).GetMethod("PopStr"));
                                    _system.AddLibrary(lastItem.ToString());
                                    break;
                            }
                        }
                        else
                        {
                            funcDef.LoadArgument(0); // Load OpCodeData into the stack
                            funcDef.Call(opCode.Pointer.Method); // Call OpCode function
                        }

                        // Handle post-execution magic
                        var magic = opCode.Attribute.Magic;
                        switch (magic)
                        {
                            case MagicOpcodes.Abort:
                                // End exeuction
                                DebugMsg(" ---- Abort");
                                funcDef.LoadConstant(0);
                                funcDef.Call(typeof (Environment).GetMethod("Exit"));
                                return;
                            case MagicOpcodes.Exit:
                                DebugMsg(" ---- Exit");
                                // Exit out of this loop
                                return;
                        }
                        break;

                    // Handle a conditional container
                    case MuftecType.Conditional:
                        var container = currStackItem.Item as ConditionalContainer;
                        if (container == null)
                            throw new MuftecCompilerException("Unable to process conditional statement at line " + currStackItem.LineNumber);

                        DebugMsg(" -- Container");
                        var ltLabel = funcDef.DefineLabel();
                        var endLabel = funcDef.DefineLabel();
                        funcDef.LoadLocal(runtimeStack); // Get the RuntimeStack
                        funcDef.Call(typeof (Shared).GetMethod("PopInt")); // Call PopInt on RuntimeStack
                        funcDef.BranchIfFalse(ltLabel);

                        // GT operations
                        DebugMsg(" -- Starting true condition");
                        GenerateFunctionInner(funcDef, container.TrueQueue, runtimeStack, lastItem);
                        funcDef.Branch(endLabel);

                        // LT operations
                        funcDef.MarkLabel(ltLabel);
                        DebugMsg(" -- Starting false condition");
                        GenerateFunctionInner(funcDef, container.FalseQueue, runtimeStack, lastItem);

                        funcDef.MarkLabel(endLabel);
                        DebugMsg(" -- Conditions done");
                        break;

                    // Add item to the runtime stack
                    case MuftecType.Integer:
                        DebugMsg(" -- Pushing int {0} to RS", currStackItem.ToString());
                        funcDef.LoadLocal(runtimeStack); // Get the RuntimeStack
                        funcDef.LoadConstant((int)currStackItem.Item);
                        funcDef.LoadConstant(currStackItem.LineNumber);
                        funcDef.NewObject<MuftecStackItem, int, int>();
                        funcDef.Call(stackPush);
                        break;
                    case MuftecType.Float:
                        DebugMsg(" -- Pushing float {0} to RS", currStackItem.ToString());
                        funcDef.LoadLocal(runtimeStack); // Get the RuntimeStack
                        funcDef.LoadConstant((double)currStackItem.Item);
                        funcDef.LoadConstant(currStackItem.LineNumber);
                        funcDef.NewObject<MuftecStackItem, double, int>();
                        funcDef.Call(stackPush);
                        break;
                    case MuftecType.String:
                        DebugMsg(" -- Pushing string {0} to RS", currStackItem.ToString());
                        funcDef.LoadLocal(runtimeStack); // Get the RuntimeStackp =
                        funcDef.LoadConstant((string)currStackItem.Item);
                        funcDef.LoadConstant(currStackItem.LineNumber);
                        funcDef.NewObject<MuftecStackItem, string, int>();
                        funcDef.Call(stackPush);
                        break;
                    case MuftecType.ArrayMarker:
                        DebugMsg(" -- Pushing array marker to RS");
                        funcDef.LoadLocal(runtimeStack); // Get the RuntimeStack
                        funcDef.LoadConstant((int)currStackItem.Item);
                        funcDef.Call(typeof(MuftecStackItem).GetMethod("CreateArrayMarker"));
                        funcDef.Call(stackPush);
                        break;
                }

                lastItem = currStackItem;
            }
        }
コード例 #14
0
ファイル: Math.cs プロジェクト: AndrewNeo/muftec
        public static void Ceiling(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.Float);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Float;
            Result.Item = (double)System.Math.Ceiling((double)Item1.Item);

            RuntimeStack.Push(Result);
        }
コード例 #15
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void StrRight(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.Integer);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();
            string str1;
            int n1;

            Result.Type = MuftecType.String;
            str1 = (string)Item1.Item;
            n1 = (int)Item2.Item;
            Result.Item = str1.Substring(str1.Length - n1, n1);

            RuntimeStack.Push(Result);
        }
コード例 #16
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void StrToUpper(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.String;
            Result.Item = ((string)Item1.Item).ToUpper();
            RuntimeStack.Push(Result);
        }
コード例 #17
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void StrMid(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item3 = Shared.Pop(ref RuntimeStack, MuftecType.Integer);
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.Integer);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.String;
            Result.Item = ((string)Item1.Item).Substring((int)Item2.Item, (int)Item3.Item);

            RuntimeStack.Push(Result);
        }
コード例 #18
0
ファイル: Conversion.cs プロジェクト: AndrewNeo/muftec
        public static void StringToInt(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Type = MuftecType.Integer;
            try
            {
                Result.Item = int.Parse((string)Item1.Item);
            }
            #pragma warning disable 168
            catch (FormatException ex)
            {
                throw new MuftecInvalidConversionException(ref RuntimeStack, "Could not convert from type String to Integer.");
            }

            RuntimeStack.Push(Result);
        }
コード例 #19
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void StringCompare(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();

            Result.Item = ((string)Item1.Item == (string)Item2.Item);
            RuntimeStack.Push(Result);
        }
コード例 #20
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void StrCut(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.Integer);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();
            string str1 = (string)Item1.Item;
            int splitPos = (int)Item2.Item;

            Result.Type = MuftecType.String;
            Result.Item = str1.Substring(0, splitPos);
            RuntimeStack.Push(Result);

            Result.Type = MuftecType.String;
            Result.Item = str1.Substring(splitPos);
            RuntimeStack.Push(Result);
        }
コード例 #21
0
ファイル: Logic.cs プロジェクト: AndrewNeo/muftec
        public static void LogicalXor(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Result = new MuftecStackItem();

            if (((Item1.Type == MuftecType.Integer) || (Item1.Type == MuftecType.Float)) &&
                ((Item2.Type == MuftecType.Integer) || (Item2.Type == MuftecType.Float)))
            {
                Result.Type = MuftecType.Integer;
                Result.Item = (((bool)Item1.Item && !(bool)Item2.Item) || ((bool)Item2.Item && !(bool)Item1.Item));
            }
            else
            {
                throw new MuftecInvalidStackItemTypeException(ref RuntimeStack);
            }

            RuntimeStack.Push(Result);
        }
コード例 #22
0
ファイル: Strings.cs プロジェクト: AndrewNeo/muftec
        public static void SplitReverse(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack, MuftecType.String);
            MuftecStackItem Result = new MuftecStackItem();
            string str1 = (string)Item1.Item;
            string str2 = (string)Item2.Item;
            int splitPos = str1.LastIndexOf(str2);

            Result.Type = MuftecType.String;
            Result.Item = str1.Substring(0, splitPos);
            RuntimeStack.Push(Result);

            Result.Type = MuftecType.String;
            Result.Item = str1.Substring(splitPos + 1);
            RuntimeStack.Push(Result);
        }
コード例 #23
0
ファイル: Math.cs プロジェクト: AndrewNeo/muftec
        public static void Exponent(ref Stack<MuftecStackItem> RuntimeStack)
        {
            MuftecStackItem Item2 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Item1 = Shared.Pop(ref RuntimeStack);
            MuftecStackItem Result = new MuftecStackItem();

            if ((Item1.Type == MuftecType.Float) || (Item2.Type == MuftecType.Float))
            {
                Result.Type = MuftecType.Float;
                Result.Item = System.Math.Pow((double)Item1.Item,(double)Item2.Item);
            }
            else if ((Item1.Type == MuftecType.Integer) || (Item2.Type == MuftecType.Integer))
            {
                Result.Type = MuftecType.Integer;
                Result.Item = System.Math.Pow((int)Item1.Item,(int)Item2.Item);
            }
            else
            {
                throw new MuftecInvalidStackItemTypeException(ref RuntimeStack);
            }

            RuntimeStack.Push(Result);
        }