Пример #1
0
        public override void Execute(AbstractMachineState state)
        {
            AMProgram prog = (AMProgram)state.Program;

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

            if (!X0.IsConstant)
            {
                Console.WriteLine("Invalid class type of object/2");
                state.Backtrack();
            }

            if (X1.IsConstant)
            {
                Console.WriteLine("object/2: object instantiation error.");
                state.Backtrack();
            }

            if (CreateObject(X0.Data() as string, X1, state))
            {
                prog.Next();
            }
            else
            {
                state.Backtrack();
            }
        }
        public void Call()
        {
            AbstractMachineState state = new AbstractMachineState(new AMFactory());
            ArrayList prog = new ArrayList();
            prog.Add(new PutConstantInstruction());
            prog.Add(new HaltInstruction());

            state.Initialize(prog);

            AMProgram program = (AMProgram)state.Program;

            ProgramClause clause = new ProgramClause("male", 2);

            program.AddLabel("male/2", clause);

            CallInstruction i = new CallInstruction();

            object[] args = { "male", "2" };

            i.Process(args);

            i.Execute(state);
            Assert.AreEqual("call", i.Name());
            Assert.AreEqual(2, i.NumberOfArguments());
            Assert.AreSame(clause, program.P);
            Assert.AreEqual("halt", program.CP.Instruction.Name());
        }
Пример #3
0
        public void Call()
        {
            ArrayList prog = new ArrayList();
            prog.Add(new NopInstruction());
            prog.Add(new HaltInstruction());

            AbstractMachineState state = new AbstractMachineState(new AMFactory());
            state.Initialize(prog);

            AMProgram program = (AMProgram)state.Program;

            ArrayList predicateCode = new ArrayList();

            AMInstructionSet iset = new AMInstructionSet();

            // say_hello(X) :- write(X).

            predicateCode.Add(iset.CreateInstruction("bcall", "write/1"));
            predicateCode.Add(iset.CreateInstruction("proceed"));

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

            X0.Assign(new ConstantTerm("Hello, World!"));

            program.AssertFirst("say_hello", 1, predicateCode);

            Assert.IsTrue(state.Call("say_hello", 1, new object[] { "Hello man" }));
        }
Пример #4
0
 public void UnsaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm Xi = (AbstractTerm)state["X" + i.ToString()];
         Xi.Assign((AbstractTerm)_savedVariables[i]);
     }
 }
Пример #5
0
 public void SaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm toSave = new AbstractTerm();
         toSave.Copy((AbstractTerm)state["X" + i.ToString()]);
         _savedVariables.Add(toSave);
     }
 }
Пример #6
0
        public AbstractMachineState SetupMachine()
        {
            AbstractMachineState state = new AbstractMachineState(new AMFactory());
            ArrayList prog = new ArrayList();
            prog.Add(new HaltInstruction());

            state.Initialize(prog);

            return state;
        }
Пример #7
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());
        }
Пример #8
0
        public void Stop()
        {
            ArrayList prog = new ArrayList();
            prog.Add(new NopInstruction());
            prog.Add(new HaltInstruction());

            AbstractMachineState state = new AbstractMachineState(new AMFactory());

            state.Initialize(prog);

            Assert.IsFalse(state.Stop());
        }
Пример #9
0
        public void Initialize()
        {
            ArrayList prog = new ArrayList();
            prog.Add(new NopInstruction());
            prog.Add(new HaltInstruction());

            AbstractMachineState state = new AbstractMachineState(new AMFactory());

            state.Initialize(prog);

            Assert.IsNotNull(state.Program);
            Assert.IsNotNull(state.DataArea);
        }
Пример #10
0
        private bool CreateObject(string classType, AbstractTerm netObject, AbstractMachineState state)
        {
            Assembly asm = GetRequiredAssembly(classType, state);
            Type[] types = asm.GetTypes();
            Type type = null;

            type = asm.GetType(fullTypeName);
            if (type == null)
            {
                return false;
            }
            netObject.Assign(new ObjectTerm(Activator.CreateInstance(type)));
            return true;
        }
Пример #11
0
        public InteractiveCompiler(string filename)
        {
            PrologCodeProvider provider = new PrologCodeProvider();
            IPrologCompiler compiler = provider.CreateCompiler();
            PrologCompilerParameters parameters = new PrologCompilerParameters();
            PrologCompilerResults results = compiler.CompileAbstractCodeFromFile(parameters, "boot.pro");

            /* Run */
            AbstractMachineState runtime = new AbstractMachineState(new AMFactory());
            //runtime.Init(results.AbstractInstructions, results.ForeignMethods, results.Namespaces, results.AssemblyFiles);
            runtime.Initialize(results.AbstractInstructions);

            runtime.Transition();
        }
Пример #12
0
        public void Backtrack()
        {
            AbstractMachineState state = new AbstractMachineState(new AMFactory());
            AMProgram program = (AMProgram)state.Program;

            state.Backtrack();
            Assert.IsNotNull(program.P);

            ProgramClause nextClause = new ProgramClause();

            state.B = new Choicepoint(2, null, null, null, nextClause, 3, null);

            state.Backtrack();

            Assert.AreSame(program.P, nextClause);
        }
Пример #13
0
        public override void Execute(AbstractMachineState state)
        {
            AMProgram prog = (AMProgram)state.Program;

            runtime = state;
            // invoke(+ClassObj,+meth(+x..),-Return).
            AbstractTerm X0 = (state["X0"] as AbstractTerm).Dereference();
            AbstractTerm X1 = (state["X1"] as AbstractTerm).Dereference();
            AbstractTerm X2 = (state["X2"] as AbstractTerm).Dereference();

            if (Getproperty(X0, X1, X2))
            {
                prog.Next();
            }
            else
            {
                state.Backtrack();
            }
        }
Пример #14
0
        private Assembly GetRequiredAssembly(string classType, AbstractMachineState state)
        {
            AMAssemblyCache cache = (AMAssemblyCache)state.AssemblyCache;
            string assemblyFile = "";

            // if it exists in the global assembly cache, then get it from there...
            foreach (string ns in cache.Namespaces)
            {
                string fullName = ns + "." + classType;
                if (cache.LocalAssemblyCache.Contains(fullName))
                {
                    assemblyFile = (string)cache.LocalAssemblyCache[fullName];
                    fullTypeName = fullName;
                    return Assembly.LoadWithPartialName(assemblyFile);
                }
            }

            foreach (string loadedAssembly in cache.AssemblyFiles)
            {
                Assembly a = Assembly.LoadFrom(loadedAssembly);

                Type[] types = a.GetTypes();
                Type type = null;
                foreach (Type t in types)
                {
                    if (t.Name == classType)
                    {
                        type = t;
                        break;
                    }
                }

                fullTypeName = type.FullName;
                return a;
            }
            return null;
        }
Пример #15
0
 public override void Initialize(AbstractMachineState state)
 {
     _state = state;
 }
 public override void Execute(AbstractMachineState state)
 {
     state.B = null;
 }
Пример #17
0
 public override void Initialize(AbstractMachineState state)
 {
     _state = state;
 }
 abstract public void Execute(AbstractMachineState state);
Пример #19
0
 public abstract void Initialize(AbstractMachineState state);
Пример #20
0
 public abstract void Execute(AbstractMachineState state);
Пример #21
0
 public void UnsaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm Xi = (AbstractTerm)state["X" + i.ToString()];
         Xi.Assign((AbstractTerm)_savedVariables[i]);
     }
 }
Пример #22
0
 public void SaveRegisters(AbstractMachineState state, int count)
 {
     for (int i = 0; i < count; i++)
     {
         AbstractTerm toSave = new AbstractTerm();
         toSave.Copy((AbstractTerm)state["X" + i.ToString()]);
         _savedVariables.Add(toSave);
     }
 }
Пример #23
0
        private bool InvokeMethod(AbstractTerm obj, AbstractTerm method, AbstractTerm returnObject, AbstractMachineState state)
        {
            if (obj.IsConstant)
            {
                // invoke a static method
                Assembly asm = GetRequiredAssembly(obj.Data() as string, state);
                Type type = asm.GetType(obj.Data() as string);
                if (type == null)
                {
                    return false;
                }
                ArrayList paramArray = new ArrayList();
                GetTypes(type.GetMethod(method.Data() as string), method, ref paramArray, obj);

                object[] arguments = paramArray.ToArray();
                object res = type.InvokeMember(method.Data() as string, BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, arguments);
                switch (res.GetType().ToString())
                {
                    case "System.Char":
                    case "System.String":
                    case "System.Int32":
                    case "System.Boolean":
                        returnObject.Assign(new ConstantTerm(res.ToString()));
                        break;
                    default:
                        returnObject.Assign(new ObjectTerm(res));
                        break;
                }
                SetTypes(method, arguments);
            }
            else
            {
                // invoke an instance method
                ArrayList paramArray = new ArrayList();

                GetTypes(obj.Data().GetType().GetMethod(method.Data() as string), method, ref paramArray, obj);
                object classObject = obj.Data();
                MethodInfo omi = classObject.GetType().GetMethod(method.Data() as string);

                object[] arguments = paramArray.ToArray();
                object res = obj.Data().GetType().InvokeMember(method.Data() as string, BindingFlags.Default | BindingFlags.ExactBinding | BindingFlags.InvokeMethod, null, obj.Data(), arguments);
                if (res != null)
                {
                    switch (res.GetType().ToString())
                    {

                        case "System.String":
                            ConstantTerm rC = new ConstantTerm(res.ToString());
                            if (!returnObject.Unify(rC))
                            {
                                return false;
                            }
                            break;
                        case "System.Char":
                        case "System.Int32":
                        case "System.Boolean":
                            ConstantTerm returnConstant = new ConstantTerm(res.ToString());
                             if (!returnObject.Unify(returnConstant))
                            {
                                return false;
                            }
                            break;
                        default:
                            returnObject.Assign(new ObjectTerm(res));
                            break;
                    }
                }
                SetTypes(method, arguments);
            }
            return true;
        }
Пример #24
0
        public void SaveRegisters()
        {
            AbstractMachineState state = new AbstractMachineState(new AMFactory());
            ArrayList prog = new ArrayList();
            prog.Add(new HaltInstruction());
            state.Initialize(prog);

            AbstractTerm X0 = (AbstractTerm)state["X0"];
            AbstractTerm X1 = (AbstractTerm)state["X1"];
            AbstractTerm X2 = (AbstractTerm)state["X2"];

            X0.Assign(new ConstantTerm("ali"));
            X1.Assign(new ConstantTerm("samir"));
            X2.Assign(new ConstantTerm("moe"));

            Choicepoint c = new Choicepoint();

            c.SaveRegisters(state, 3);

            Assert.AreEqual("ali", c["X0"].Data());
            Assert.AreEqual("samir", c["X1"].Data());
            Assert.AreEqual("moe", c["X2"].Data());
        }
Пример #25
0
 abstract public void Initialize(AbstractMachineState state);
Пример #26
0
 public void X_Registers()
 {
     AbstractMachineState state = new AbstractMachineState(new AMFactory());
 }