Exemple #1
0
        public string ReorderPost(string code)
        {
            var d0 = code.ToStack();
            var d1 = ReorderInterpreter.RunReorderPost(d0);

            return(interpreter.StackToString(d1));
        }
Exemple #2
0
 public void TestReorderInstruction()
 {
     interpreter = reorderInterpreter;
     Assert.Equal("[[] R<int>[2 R<int>[3 5 +] +]]", Run("[[2 3 5 + +]]"));
     Assert.Equal("[[2 3 5 + +]]", ReorderInterpreter.ReorderPost(lastRun).ToRepr());
     Assert.Equal("[[] a R<int>[2 R<int>[3 5 +] +]]", Run("[[2 3 a 5 + +]]"));
     Assert.Equal("[[2 3 5 + + a]]", ReorderInterpreter.RunReorderPost(lastRun).ToRepr());
     Assert.Equal("[[] R<int>[3 5 +]]", Run("[[3 5 + +]]"));
     Assert.Equal("[[3 5 +]]", ReorderInterpreter.RunReorderPost(lastRun).ToRepr());
 }
        public void testLotsOfReordering()
        {
            Assert.Equal("[[] d c b a 10]", Run("[[2 a 3 b c 5 + d +]]"));

            Stack s1, s2;

            // Assert.Equal("[[] d c b a R[2 R[3 5 +] +]]", (s1 = interpreter.RunReorderPre("[[2 a 3 b c 5 + d +]]".ToStack())).ToRepr());
            Assert.Equal("[[] d c b a R<int>[2 R<int>[3 5 +] +]]", (s1 = reorderInterpreter.Run("[[2 a 3 b c 5 + d +]]".ToStack())).ToRepr());

            Assert.Equal("[[2 3 5 + + a b c d]]", (s2 = ReorderInterpreter.RunReorderPost(s1)).ToRepr());
            Assert.Equal("[[] d c b a 10]", strictInterpreter.Run(s2).ToRepr());
        }
        public void TestReorderWithAssignment()
        {
            Assert.Equal("[[] 3]", Run("[[1 x 2 ! x +]]"));
            var s = new Stack(new object[] { new Defer("[x 2 !]".ToStack(), typeof(void)),
                                             new Defer("[1 x +]".ToStack(), typeof(int)), new Stack() });

            Assert.Equal("[[] R<int>[1 x +] R<Void>[x 2 !]]", s.ToRepr());
            Assert.Equal("[[x 2 ! 1 x +]]", ReorderInterpreter.RunReorderPost(s).ToRepr());
            Assert.Equal("[[] R<int>[1 R<int>[x] +] R<Void>[x 2 !]]", ReorderPre("[[1 x 2 ! x +]]"));
            reorderInterpreter.instructions.Remove("x");
            Assert.Equal("[[x 2 ! 1 x +]]", Reorder("[[1 x 2 ! x +]]"));
            interpreter.instructions.Remove("x");
            Assert.Equal("[[] x 1]", Run("[[1 x + x 2 !]]"));
            interpreter.instructions.Remove("x");
            Assert.Equal("[[] 2 1]", Run("[[1 x 2 ! x]]"));
        }
        public void TestReorderBugFoundInWild()
        {
            interpreter = reorderInterpreter;
            Assert.Equal("[[]]", Run("[[dup]]"));
            Assert.Equal("[[]]", Run("[[pop]]"));
            // Assert.Equal("[[] R[-3 pop]]", Run("[[-3 pop]]"));
            Assert.Equal("[[] R<Void>[-3 pop]]", Run("[[-3 pop]]"));
            Assert.Equal("[[] R<int>[1 dup] R<int>[1 dup] R<Void>[-3 pop]]", Run("[[1 -3 pop dup]]"));

            Assert.Equal("[[] R<Void>[-3 pop]]", Run("[[-3 pop]]"));
            Assert.Equal("[[] R<Void>[-3 pop]]", Run("[[-3 pop dup]]"));
            Assert.Equal("[[] R<Void>[-3 pop]]", Run("[[swap -3 pop dup]]"));
            Assert.Equal("[[] R<Void>[-3 pop]]", Run("[[dup / pop < swap -3 pop dup dup]]"));
            Assert.Equal("[]", ReorderInterpreter.RunReorderPost("[]".ToStack()).ToRepr());
            Assert.Equal("[[]]", ReorderInterpreter.RunReorderPost("[[]]".ToStack()).ToRepr());
            Assert.Equal("[[-3 pop]]", Reorder("[[dup / pop < swap -3 pop dup dup]]"));
        }
Exemple #6
0
        public void TestReorder()
        {
            interpreter = reorderInterpreter;
            var d0 = "[[2 a 1 +]]".ToStack();
            var d1 = interpreter.Eval(d0);

            Assert.Equal("[[a 1 +] 2]".ToStack(), d1);
            var d2 = interpreter.Eval(d1);

            Assert.Equal("[[1 +] a 2]".ToStack(), d2);
            var d3 = interpreter.Eval(d2);

            Assert.Equal("[[+] 1 a 2]".ToStack(), d3);
            var d4 = interpreter.Eval(d3);

            // Assert.Equal(interpreter.ParseWithResolution("[[+ a ] 1 2]"), d4);
            Assert.Equal("[[+ a] 1 2]", interpreter.StackToString(d4));
            // var d4b = "[[' reorder run] [[+ a ] 1 2] []]".ToStack();
            // var d4b = "[[reorder] [[+ a ] 1 2] []]".ToStack();
            // var d5 = interpreter.Eval(d4b);
            Assert.Equal("[[a] R<int>[2 1 +]]",
                         Eval("[[+ a ] 1 2]"));
            var e1 = interpreter.Eval("[[+ a ] 1 2]".ToStack());

            Assert.Equal("[[a] R<int>[2 1 +]]",
                         e1.ToRepr());
            var s = e1;

            Assert.Equal("[[a] R<int>[2 1 +]]",
                         s.ToRepr());
            var e2 = ReorderInterpreter.ReorderPost(s);

            Assert.Equal("[[2 1 + a]]",
                         e2.ToRepr());

            // R<T>[1,2,3] is not parsing properly.
            // Assert.Equal("[[] [[a] R<System.Int32> [2 1 +]]]",
            //              Eval("[[reorder-post] [[] a R<System.Int32> [2 1 +]]]"));

            // Assert.Equal("[[] [[a] R<System.Int32> [2 1 +]]]",
            //              Eval("[[reorder-post] [[a] R<System.Int32> [2 1 +]]]"));
            // var d6 = interpreter.Eval(d5);
            // Assert.Equal("[[2 1 +] [[] a]]".ToStack(), d6);
        }
Exemple #7
0
        public InterpreterTestUtil()
        {
            // Maybe I should do these lazily.
            nonstrictInterpreter = new NonstrictInterpreter();
            strictInterpreter    = new StrictInterpreter();
            reorderInterpreter   = new ReorderInterpreter();
            cleanInterpreter     = new Interpreter();
            typeInterpreter      = new TypeInterpreter();
            cleanInterpreter.instructions.Clear();
            interpreter = nonstrictInterpreter;

            // With InstructionFunc you have to do all your own error handling.
            interpreter.instructions["add"]
                  = strictInterpreter.instructions["add"]
                  = new InstructionFunc(stack => {
                if (stack.Count < 2)
                {
                    return(stack);
                }
                object a, b;
                a = stack.Pop();
                if (!(a is int))
                {
                    var code = new Stack();
                    code.Push(a);
                    code.Push(new Symbol("add"));
                    stack.Push(new Continuation(code));
                    return(stack);
                }
                b = stack.Pop();
                if (!(b is int))
                {
                    var code = new Stack();
                    code.Push(b);
                    code.Push(new Symbol("add"));
                    stack.Push(a);
                    stack.Push(new Continuation(code));
                    return(stack);
                }
                stack.Push((int)a + (int)b);
                return(stack);
            });
        }