示例#1
0
        public void TestDetermineInputAndOutput3()
        {
            UniqueVariable.Clear();
            interpreter = typeInterpreter;
            var s = "[[if typeof(int) + typeof(char)]]".ToStack();
            var _if
                = new DetermineTypesInstruction("[bool 'a 'a]",
                                                "['a]")
                {
                getType = o => o is Type t ? t : o.GetType()
                };
            var add
                = new DetermineTypesInstruction("[int int]",
                                                "[int]")
                {
                getType = o => o is Type t ? t : o.GetType()
                };

            interpreter.instructions["if"] = _if;
            interpreter.instructions["+"]  = add;

            var s2 = interpreter.Run(s);

            Assert.Equal("[[] char int { a0 -> int } ['a0 'a0 bool]]", s2.ToRepr());
            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("[bool int int]", consumes.ToRepr());
            Assert.Equal("[int char]", produces.ToRepr());
        }
示例#2
0
        public void TestNoSwap()
        {
            interpreter = typeInterpreter;
            UniqueVariable.Clear();
            var s = "[[typeof(int) typeof(char) ]]";

            Assert.Equal("[[] char int]", Run(s));
            var s2 = lastRun;

            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("[]", consumes.ToRepr());
            Assert.Equal("[int char]", produces.ToRepr());
        }
示例#3
0
        public void TestSwap2()
        {
            interpreter = typeInterpreter;
            UniqueVariable.Clear();
            var s = "[[swap]]";

            Assert.Equal("[[] 'a0 'b1 ['b1 'a0]]", Run(s));
            var s2 = lastRun;

            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("['a0 'b1]", consumes.ToRepr());
            Assert.Equal("['b1 'a0]", produces.ToRepr());
        }
示例#4
0
        public void TestTypeCheckDupDupWithVars3()
        {
            interpreter = typeInterpreter;
            UniqueVariable.Clear();
            var s = "[[dup typeof(char) dup]]";

            Assert.Equal("[[] 'a1 'a1 { a1 -> char } 'a0 'a0 ['a0]]", Run(s));
            var s2 = lastRun;

            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("['a0]", consumes.ToRepr());
            Assert.Equal("['a0 'a0 char char]", produces.ToRepr());
        }
示例#5
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);
            });
        }
示例#6
0
        public void TestTypeCheckDupInterp()
        {
            interpreter = typeInterpreter;
            UniqueVariable.Clear();
            var s = "[[typeof(int) dup]]";

            // var dup
            //   = new DetermineTypesInstruction("['a]",
            //                                   "['a 'a]")
            //   { getType = o => o is Type t ? t : o.GetType() };
            // interpreter.instructions["dup"] = dup;
            Assert.Equal("[[] 'a0 'a0 { a0 -> int }]", Run(s));
            var s2 = lastRun;

            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("[]", consumes.ToRepr());
            Assert.Equal("[int int]", produces.ToRepr());
        }
示例#7
0
        public void TestTypeCheckDupDupWithVars2()
        {
            interpreter = cleanInterpreter;
            UniqueVariable.Clear();
            var s = "[[dup typeof(char) dup]]";
            var dup
                = new DetermineTypesInstruction("['a]",
                                                "['a 'a]")
                {
                getType = o => o is Type t ? t : o.GetType()
                };

            interpreter.instructions["dup"] = dup;
            Assert.Equal("[[] 'a1 'a1 { a1 -> char } 'a0 'a0 ['a0]]", Run(s));
            var s2 = lastRun;

            s2.Pop();
            var(consumes, produces) = TypeInterpreter.ConsumesAndProduces(s2);
            Assert.Equal("['a0]", consumes.ToRepr());
            Assert.Equal("['a0 'a0 char char]", produces.ToRepr());
        }