public override void Execute(AbstractMachineState state)
        {
            AMHeap    heap    = (AMHeap)state.DataArea;
            AMProgram program = (AMProgram)state.Program;


            if (_vn[0] == 'Y')
            {
                if (state.E[_vn] == null)
                {
                    throw new Exception("INTERNAL ERROR: Yn variable is null in E");
                }
                AbstractTerm Ai = (AbstractTerm)state[_ai];
                AbstractTerm Vn = (AbstractTerm)state.E[_vn];
                Ai.Assign(Vn);

                program.Next();
            }
            else
            {
                heap.Push(new AbstractTerm());

                AbstractTerm x = (AbstractTerm)state[_vn];
                AbstractTerm a = (AbstractTerm)state[_ai];

                x.Assign((AbstractTerm)heap.Top());
                a.Assign((AbstractTerm)heap.Top());

                program.Next();
            }
        }
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Ai = ((AbstractTerm)state[_ai]).Dereference();

            if (Ai.IsReference)
            {
                heap.Push(new ListTerm());
                Ai.Bind((AbstractTerm)heap.Top());
                state.IsWriteMode = true;
            }
            else if (Ai.IsList)
            {
                state.S          = Ai.Next;
                state.IsReadMode = true;
            }
            else
            {
                state.Fail = true;
            }

            if (state.Fail)
            {
                state.Backtrack();
            }
            else
            {
                program.Next();
            }
        }
Exemplo n.º 3
0
        public void Push_ConstantTerm()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con = new ConstantTerm("Hello, World!");

            heap.Push(con);

            Assert.AreSame(con, heap.Top());
        }
Exemplo n.º 4
0
        public void Push_StructureTerm()
        {
            AMHeap heap = new AMHeap();

            StructureTerm con = new StructureTerm("Hello, World!", 2);

            heap.Push(con);

            Assert.AreSame(con, heap.Top());
        }
Exemplo n.º 5
0
        public void Push_ListTerm()
        {
            AMHeap heap = new AMHeap();

            ListTerm con = new ListTerm();

            heap.Push(con);

            Assert.AreSame(con, heap.Top());
        }
Exemplo n.º 6
0
        public void Push_ObjectTerm()
        {
            AMHeap heap = new AMHeap();

            ObjectTerm con = new ObjectTerm();

            heap.Push(con);

            Assert.AreSame(con, heap.Top());
        }
Exemplo n.º 7
0
        public void Push_AbstractTerm()
        {
            AMHeap heap = new AMHeap();

            AbstractTerm term = new AbstractTerm();

            heap.Push(term);

            Assert.AreSame(heap.Top(), term);
        }
Exemplo n.º 8
0
        public void Initialize()
        {
            AMHeap heap = new AMHeap();
            AbstractMachineState state = new AbstractMachineState(new AMFactory());

            heap.Initialize(state);

            Assert.IsNull(heap.H);
            Assert.IsNull(heap.Top());
        }
Exemplo n.º 9
0
        public void Pop_one_item()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con = new ConstantTerm("ali");

            heap.Push(con);

            heap.Pop();

            Assert.IsNull(heap.Top());
        }
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Ai = (AbstractTerm)state[_ai];

            heap.Push(new ListTerm());

            Ai.Assign((AbstractTerm)heap.Top());

            program.Next();
        }
        public override void Execute(AbstractMachineState state)
        {
            AMHeap    heap    = (AMHeap)state.DataArea;
            AMProgram program = (AMProgram)state.Program;

            heap.Push(new StructureTerm(structureName, arity));

            AbstractTerm Ai = (AbstractTerm)state[ai];

            Ai.Assign((AbstractTerm)heap.Top());

            program.Next();
        }
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            EnvironmentFrame env = new EnvironmentFrame(state.E, program.CP, state.B0, 2);

            // Is this really needed?
            heap.Push(env);

            state.E = (EnvironmentFrame)heap.Top();

            program.Next();
        }
Exemplo n.º 13
0
        public void Pop_two_items()
        {
            AMHeap heap = new AMHeap();

            ConstantTerm con   = new ConstantTerm("ali");
            ConstantTerm first = new ConstantTerm("foo");

            heap.Push(first);
            heap.Push(con);

            heap.Pop();

            Assert.AreSame(first, heap.Top());
        }
Exemplo n.º 14
0
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Vn = (AbstractTerm)state[_vn];

            if (state.IsReadMode)
            {
                state.Fail = !Vn.Unify((AbstractTerm)state.S);
            }
            else
            {
                AbstractTerm addr = Vn.Dereference();

                if (IsEnvironmentVariable(addr, state))
                {
                    // Push a new variable on the heap
                    AbstractTerm newVariable = new AbstractTerm();
                    heap.Push(newVariable);
                    addr.Bind((AbstractTerm)heap.Top());
                }
                else
                {
                    AbstractTerm newHeapItem = new AbstractTerm();
                    newHeapItem.Assign(addr);
                    heap.Push(newHeapItem);
                }
            }
            if (state.S != null)
            {
                state.S = state.S.Next;
            }

            if (state.Fail)
            {
                state.Backtrack();
            }
            else
            {
                program.Next();
            }
        }
Exemplo n.º 15
0
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Ai = ((AbstractTerm)state[_ai]).Dereference();

            if (Ai.IsReference)
            {
                heap.Push(new StructureTerm(_structureName, _arity));
                Ai.Bind((AbstractTerm)heap.Top());
                state.IsWriteMode = true;
            }
            else if (Ai.IsStructure)
            {
                if (Ai.Name == _structureName)
                {
                    // set S to next item on heap
                    state.S = Ai.Next;

                    // set read mode
                    state.IsReadMode = true;
                }
                else
                {
                    state.Fail = true;
                }
            }
            else
            {
                state.Fail = true;
            }

            if (state.Fail)
            {
                state.Backtrack();
            }
            else
            {
                program.Next();
            }
        }
Exemplo n.º 16
0
        public void SetVariable()
        {
            AbstractMachineState state = SetupMachine();

            SetVariableInstruction i = new SetVariableInstruction();

            object[] args = { "X0" };

            i.Process(args);

            i.Execute(state);

            Assert.AreEqual("set_variable", i.Name());
            Assert.AreEqual(1, i.NumberOfArguments());

            AbstractTerm X0 = ((AbstractTerm)state["X0"]).Dereference();

            AMHeap heap = (AMHeap)state.DataArea;

            Assert.AreSame(X0, heap.Top());
        }
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Vn = (AbstractTerm)state[_vn];

            if (state.IsReadMode)
            {
                Vn.Assign((AbstractTerm)state.S);
            }
            else
            {
                heap.Push(new AbstractTerm());
                Vn.Assign((AbstractTerm)heap.Top());
            }
            if (state.S != null)
            {
                state.S = state.S.Next;
            }
            program.Next();
        }
Exemplo n.º 18
0
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;


            AbstractTerm Yn = state.E[_yn].Dereference();
            AbstractTerm Ai = (AbstractTerm)state[_ai];

            if (!IsEnvironmentVariable(Yn, state))
            {
                Ai.Assign(Yn);
            }
            else
            {
                heap.Push(new AbstractTerm());
                Yn.Bind((AbstractTerm)heap.Top());
                Ai.Assign(Yn);
            }

            program.Next();
        }
Exemplo n.º 19
0
        public void Allocate()
        {
            AbstractMachineState state = SetupMachine();

            AMHeap heap = (AMHeap)state.DataArea;

            AllocateInstruction i = new AllocateInstruction();

            i.Process(null);
            i.Execute(state);


            EnvironmentFrame env = (EnvironmentFrame)heap.Top();

            Assert.AreEqual("allocate", i.Name());
            Assert.AreEqual(0, i.NumberOfArguments());
            Assert.AreSame(env, state.E);
            for (int r = 0; r < 20; r++)
            {
                Assert.IsNotNull(env["Y" + r.ToString()]);
            }
        }
        public override void Execute(AbstractMachineState state)
        {
            AMProgram program = (AMProgram)state.Program;
            AMHeap    heap    = (AMHeap)state.DataArea;

            AbstractTerm Vn = ((AbstractTerm)state[_vn]).Dereference();

            if (IsEnvironmentVariable(Vn, state))
            {
                // Push a new variable on the heap
                AbstractTerm newVariable = new AbstractTerm();
                heap.Push(newVariable);
                Vn.Bind((AbstractTerm)heap.Top());
            }
            else
            {
                AbstractTerm newHeapItem = new AbstractTerm();
                newHeapItem.Assign(Vn);
                heap.Push(newHeapItem);
            }

            program.Next();
        }