Example #1
0
        public void AssertFirst_2()
        {
            ArrayList       p  = new ArrayList();
            HaltInstruction hi = new HaltInstruction();

            p.Add(hi);

            AMProgram program = new AMProgram();

            program.Initialize(p);

            program.AssertFirst("male", 1, p);


            ProgramClause oldFirst = program["male/1"];

            program.AssertFirst("male", 1, p);

            ProgramClause newFirst = program["male/1"];

            Assert.AreEqual(newFirst.Name, "male");
            Assert.AreEqual(newFirst.Arity, 1);
            Assert.AreEqual(newFirst.Instruction.Name(), "try_me_else");

            Assert.AreEqual(oldFirst.Name, "male%1/1");
            Assert.AreEqual(oldFirst.Arity, 1);
            Assert.AreEqual(oldFirst.Instruction.Name(), "trust_me");
        }
Example #2
0
        public void AddLabel_2()
        {
            ArrayList       p  = new ArrayList();
            HaltInstruction hi = new HaltInstruction();

            p.Add(hi);

            AMProgram program = new AMProgram();

            program.Initialize(p);

            program.AddLabel("male/1", new ProgramClause("male", 1));
            program.AddLabel("male/1", new ProgramClause("male", 1));

            ProgramClause male1 = program["male/1"];
            ProgramClause male2 = male1.NextPredicate;

            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "try_me_else");

            Assert.AreEqual(male2.Name, "male%1/1");
            Assert.AreEqual(male2.Arity, 1);
            Assert.AreEqual(male2.Instruction.Name(), "trust_me");
        }
        public static Machine getSampleMachine()
        {
            Machine machine = new Machine(6);

            Instruction i0 = new AddInstruction();
            List <int>  p  = new List <int>(); p.Add(2); p.Add(3); p.Add(0); // M2 = 0 + M0

            i0.setParameters(p);
            machine.addInstruction(i0);

            Instruction i1 = new AssignValueInstruction();

            p.Clear(); p.Add(4); p.Add(1); //M4 = 1
            i1.setParameters(p);
            machine.addInstruction(i1);

            Instruction i2 = new GotoIfInstruction();

            p.Clear(); p.Add(5); p.Add(2); // goto 5 if M2 > 0
            i2.setParameters(p);
            machine.addInstruction(i2);

            Instruction i3 = new AddInstruction();

            p.Clear(); p.Add(0); p.Add(3); p.Add(5); // M0 = M3 + 0
            i3.setParameters(p);
            machine.addInstruction(i3);

            Instruction i4 = new HaltInstruction();

            machine.addInstruction(i4); //halt

            Instruction i5 = new AddInstruction();

            p.Clear(); p.Add(3); p.Add(3); p.Add(1); // M3 = M3 + M1
            i5.setParameters(p);
            machine.addInstruction(i5);

            Instruction i6 = new SubstractInstruction();

            p.Clear(); p.Add(2); p.Add(2); p.Add(4); // M2 = M2 - 1
            i6.setParameters(p);
            machine.addInstruction(i6);

            Instruction i7 = new GotoIfInstruction();

            p.Clear(); p.Add(2); p.Add(4); // goto 2
            i7.setParameters(p);
            machine.addInstruction(i7);

            int[] input = { 5, 6 };
            machine.insertInput(input);

            // when machine halts result will be in M0
            return(machine);
        }
Example #4
0
        public void Program()
        {
            ArrayList       p  = new ArrayList();
            HaltInstruction hi = new HaltInstruction();

            p.Add(hi);

            AMProgram program = new AMProgram();

            program.Initialize(p);

            Assert.AreSame(program.P.Instruction, program.Program.Instruction);
        }
Example #5
0
        public void AssertFirst_1()
        {
            ArrayList       p  = new ArrayList();
            HaltInstruction hi = new HaltInstruction();

            p.Add(hi);

            AMProgram program = new AMProgram();

            program.Initialize(p);

            program.AssertFirst("male", 1, p);

            ProgramClause male1 = program["male/1"];


            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "nop");
        }
Example #6
0
 public AMInstructionSet()
 {
     _instructions["allocate"]          = new AllocateInstruction();
     _instructions["bcall"]             = new BCallInstruction();
     _instructions["call"]              = new CallInstruction();
     _instructions["cut"]               = new CutInstruction();
     _instructions["deallocate"]        = new DeallocateInstruction();
     _instructions["execute"]           = new ExecuteInstruction();
     _instructions["fcall"]             = new FCallInstruction();
     _instructions["fail"]              = new FailInstruction();
     _instructions["get_constant"]      = new GetConstantInstruction();
     _instructions["get_list"]          = new GetListInstruction();
     _instructions["get_structure"]     = new GetStructureInstruction();
     _instructions["get_value"]         = new GetValueInstruction();
     _instructions["get_variable"]      = new GetVariableInstruction();
     _instructions["halt"]              = new HaltInstruction();
     _instructions["nop"]               = new NopInstruction();
     _instructions["proceed"]           = new ProceedInstruction();
     _instructions["put_constant"]      = new PutConstantInstruction();
     _instructions["put_list"]          = new PutListInstruction();
     _instructions["put_structure"]     = new PutStructureInstruction();
     _instructions["put_unsafe_value"]  = new PutUnsafeValueInstruction();
     _instructions["put_variable"]      = new PutVariableInstruction();
     _instructions["put_value"]         = new PutValueInstruction();
     _instructions["retry_me_else"]     = new RetryMeElseInstruction();
     _instructions["set_constant"]      = new SetConstantInstruction();
     _instructions["set_local_value"]   = new SetLocalValueInstruction();
     _instructions["set_value"]         = new SetValueInstruction();
     _instructions["set_variable"]      = new SetVariableInstruction();
     _instructions["set_void"]          = new SetVoidInstruction();
     _instructions["trust_me"]          = new TrustMeInstruction();
     _instructions["try_me_else"]       = new TryMeElseInstruction();
     _instructions["unify_constant"]    = new UnifyConstantInstruction();
     _instructions["unify_local_value"] = new UnifyLocalValueInstruction();
     _instructions["unify_variable"]    = new UnifyVariableInstruction();
     _instructions["unify_value"]       = new UnifyValueInstruction();
     _instructions["unify_void"]        = new UnifyVoidInstruction();
     _instructions["procedure"]         = new ProcedureInstruction();
 }
Example #7
0
        public void AssertFirst_3()
        {
            ArrayList       p  = new ArrayList();
            HaltInstruction hi = new HaltInstruction();

            p.Add(hi);

            AMProgram program = new AMProgram();

            program.Initialize(p);

            program.AssertFirst("male", 1, p);


            ProgramClause male3 = program["male/1"];

            program.AssertFirst("male", 1, p);

            ProgramClause male2 = program["male/1"];

            program.AssertFirst("male", 1, p);

            ProgramClause male1 = program["male/1"];

            Assert.AreEqual(male1.Name, "male");
            Assert.AreEqual(male1.Arity, 1);
            Assert.AreEqual(male1.Instruction.Name(), "try_me_else");

            Assert.AreEqual(male2.Name, "male%1/1");
            Assert.AreEqual(male2.Arity, 1);
            Assert.AreEqual(male2.Instruction.Name(), "retry_me_else");

            Assert.AreEqual(male3.Name, "male%2/1");
            Assert.AreEqual(male3.Arity, 1);
            Assert.AreEqual(male3.Instruction.Name(), "trust_me");
        }
        public static Machine GetSampleMachine()
        {
            var machine = new Machine(6);

            Instruction ix = new AssignValueInstruction();

            ix.SetParameters(new List <int> {
                0, 5
            });
            machine.AddInstruction(ix);

            Instruction iy = new AssignValueInstruction();

            iy.SetParameters(new List <int> {
                1, 6
            });
            machine.AddInstruction(iy);

            Instruction i0 = new AddInstruction();

            i0.SetParameters(new List <int> {
                2, 3, 0
            });                                          // M2 = 0 + M0
            machine.AddInstruction(i0);

            Instruction i1 = new AssignValueInstruction();

            i1.SetParameters(new List <int> {
                4, 1
            });                                       //M4 = 1
            machine.AddInstruction(i1);

            Instruction i2 = new GotoIfInstruction();

            i2.SetParameters(new List <int> {
                7, 2
            });                                       // goto 7 if M2 > 0
            machine.AddInstruction(i2);

            Instruction i3 = new AddInstruction();

            i3.SetParameters(new List <int> {
                0, 3, 5
            });                                          // M0 = M3 + 5
            machine.AddInstruction(i3);

            Instruction i4 = new HaltInstruction();

            machine.AddInstruction(i4); //halt

            Instruction i5 = new AddInstruction();

            i5.SetParameters(new List <int> {
                3, 3, 1
            });                                          // M3 = M3 + M1
            machine.AddInstruction(i5);

            Instruction i6 = new SubstractInstruction();

            i6.SetParameters(new List <int> {
                2, 2, 4
            });                                          // M2 = M2 - 1
            machine.AddInstruction(i6);

            Instruction i7 = new GotoIfInstruction();

            i7.SetParameters(new List <int> {
                4, 4
            });                                       // goto 4 if M4 > 0
            machine.AddInstruction(i7);

            // when machine halts result will be in M0
            return(machine);
        }
 public void Visit(HaltInstruction instruction)
 {
     lines.Add($"{label}\tHALT");
 }
        public Instruction Translate()
        {
            Instruction instruction;

            switch (Type)
            {
            case CommandType.AssignValue:
                instruction = new AssignValueInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.Add:
                instruction = new AddInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value, Arg3.Value
                });
                break;

            case CommandType.Substract:
                instruction = new SubstractInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value, Arg3.Value
                });
                break;

            case CommandType.Divide:
                instruction = new DivideByTwoInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value
                });
                break;

            case CommandType.CopyValue:
                instruction = new CopyValueInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.CopyValue2:
                instruction = new CopyValue2Instruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.GotoIf:
                instruction = new GotoIfInstruction();
                instruction.SetParameters(new List <int> {
                    Arg1.Value, Arg2.Value
                });
                break;

            case CommandType.Halt:
                instruction = new HaltInstruction();
                instruction.SetParameters(new List <int>());
                break;

            default:
                throw new InvalidEnumArgumentException();
            }
            return(instruction);
        }
Example #11
0
 public virtual void VisitHaltInstruction(HaltInstruction instruction)
 {
     DefaultVisitInstruction(instruction);
 }