コード例 #1
0
ファイル: ByteCode.cs プロジェクト: Logicalshift/Reason
        public void AllocatePreservesArgumentsByMovingUp()
        {
            var executor = new ByteCodeExecutor(new ByteCodePoint[0], new ILiteral[0], 2);

            var someValue = Literal.NewAtom();
            executor.Register(0).SetTo(new SimpleReference(someValue, new SimpleReference()));
            Assert.AreEqual(someValue, executor.Register(0).Term, "Term is initially set");

            executor.Dispatch(new ByteCodePoint(Operation.Allocate, 1, 1));
            Assert.AreEqual(someValue, executor.Register(1).Term, "Argument is preserved by allocate");
        }
コード例 #2
0
ファイル: ByteCode.cs プロジェクト: Logicalshift/Reason
        public void AllocateReplacesVariables()
        {
            var executor = new ByteCodeExecutor(new ByteCodePoint[0], new ILiteral[0], 2);

            var someValue = Literal.NewAtom();
            executor.Register(0).SetTo(new SimpleReference(someValue, new SimpleReference()));
            Assert.AreEqual(someValue, executor.Register(0).Term, "Term is initially set");

            executor.Dispatch(new ByteCodePoint(Operation.Allocate, 1, 0));
            Assert.AreNotEqual(someValue, executor.Register(0).Term, "Term is replaced by allocate");
        }
コード例 #3
0
        public Func <bool> Call(ILiteral predicate, params IReferenceLiteral[] arguments)
        {
            // Create the execution environment
            var executor = new ByteCodeExecutor(GetProgram(), GetLiterals(), _maxVariableIndex + 1);

            // Load the arguments
            for (int argIndex = 0; argIndex < arguments.Length; ++argIndex)
            {
                executor.Register(argIndex).SetTo(arguments[argIndex]);
            }

            // Run the program
            executor.Run(_predicateLocation[predicate]);

            // TODO: implement backtracking
            return(() => true);
        }
コード例 #4
0
ファイル: ByteCode.cs プロジェクト: Logicalshift/Reason
        public void PermanentVariablesAreRestored()
        {
            var executor = new ByteCodeExecutor(new ByteCodePoint[0], new ILiteral[0], 2);

            // Allocate a permanent variable
            executor.Dispatch(new ByteCodePoint(Operation.Allocate, 1, 0));

            // Set it to something
            var someValue = Literal.NewAtom();
            executor.Register(0).SetTo(new SimpleReference(someValue, new SimpleReference()));
            Assert.AreEqual(someValue, executor.Register(0).Term, "Term is initially set");

            // Allocate another frame
            executor.Dispatch(new ByteCodePoint(Operation.Allocate, 1, 0));
            Assert.AreNotEqual(someValue, executor.Register(0).Term, "Term is replaced by allocate");

            // Restore the original value using deallocate
            executor.Dispatch(new ByteCodePoint(Operation.Deallocate));
            Assert.AreEqual(someValue, executor.Register(0).Term);
        }
コード例 #5
0
ファイル: ByteCodeSolver.cs プロジェクト: Logicalshift/Reason
        public Func<bool> Call(ILiteral predicate, params IReferenceLiteral[] arguments)
        {
            // Create the execution environment
            var executor = new ByteCodeExecutor(GetProgram(), GetLiterals(), _maxVariableIndex+1);

            // Load the arguments
            for (int argIndex = 0; argIndex < arguments.Length; ++argIndex)
            {
                executor.Register(argIndex).SetTo(arguments[argIndex]);
            }
            
            // Run the program
            executor.Run(_predicateLocation[predicate]);

            // TODO: implement backtracking
            return () => true;
        }