Example #1
0
        public override WamReferenceTarget Dereference()
        {
            if (m_target == null)
            {
                return(this);
            }

            return(m_target.Dereference());
        }
Example #2
0
        internal WamReferenceTarget Evaluate(WamReferenceTarget wamReferenceTarget)
        {
            if (wamReferenceTarget == null)
            {
                throw new ArgumentNullException("wamReferenceTarget");
            }

            WamCompoundTerm wamCompoundTerm = wamReferenceTarget.Dereference() as WamCompoundTerm;
            if (wamCompoundTerm == null)
            {
                return wamReferenceTarget;
            }

            if (!Program.Libraries.Contains(wamCompoundTerm.Functor))
            {
                return wamReferenceTarget;
            }

            LibraryMethod method = Program.Libraries[wamCompoundTerm.Functor];
            if (method == null
                || method.CanEvaluate == false)
            {
                return wamReferenceTarget;
            }

            WamReferenceTarget[] arguments = new WamReferenceTarget[method.Functor.Arity];
            for (int index = 0; index < method.Functor.Arity; ++index)
            {
                arguments[index] = Evaluate(wamCompoundTerm.Children[index]);
            }

            Function function = method as Function;
            if (function != null)
            {
                CodeTerm[] codeTerms = new CodeTerm[method.Functor.Arity];
                for (int index = 0; index < method.Functor.Arity; ++index)
                {
                    codeTerms[index] = arguments[index].GetCodeTerm();
                }

                CodeTerm result;
                try
                {
                    result = function.FunctionDelegate(codeTerms);
                    if (result == null)
                    {
                        result = new CodeValueObject(null);
                    }
                }
                catch (Exception ex)
                {
                    result = new CodeValueException(ex);
                }

                return WamValue.Create(result);
            }

            Predicate predicate = method as Predicate;
            if (predicate != null)
            {
                bool result;
                try
                {
                    result = predicate.PredicateDelegate(this, arguments);
                }
                catch
                {
                    result = false;
                }

                return WamValueBoolean.Create(result);
            }

            return wamReferenceTarget;
        }
Example #3
0
        internal WamReferenceTarget Evaluate(WamReferenceTarget wamReferenceTarget)
        {
            if (wamReferenceTarget == null)
            {
                throw new ArgumentNullException("wamReferenceTarget");
            }

            var wamCompoundTerm = wamReferenceTarget.Dereference() as WamCompoundTerm;

            if (wamCompoundTerm == null)
            {
                return(wamReferenceTarget);
            }

            if (!Program.Libraries.Contains(wamCompoundTerm.Functor))
            {
                return(wamReferenceTarget);
            }

            var method = Program.Libraries[wamCompoundTerm.Functor];

            if (method == null ||
                method.CanEvaluate == false)
            {
                return(wamReferenceTarget);
            }

            var arguments = new WamReferenceTarget[method.Functor.Arity];

            for (var index = 0; index < method.Functor.Arity; ++index)
            {
                arguments[index] = Evaluate(wamCompoundTerm.Children[index]);
            }

            var function = method as Function;

            if (function != null)
            {
                var codeTerms = new CodeTerm[method.Functor.Arity];
                for (var index = 0; index < method.Functor.Arity; ++index)
                {
                    codeTerms[index] = arguments[index].GetCodeTerm();
                }

                CodeTerm result;
                try
                {
                    result = function.FunctionDelegate(codeTerms) ?? new CodeValueObject(null);
                }
                catch (Exception ex)
                {
                    result = new CodeValueException(ex);
                }

                return(WamValue.Create(result));
            }

            var predicate = method as Predicate;

            if (predicate != null)
            {
                bool result;
                try
                {
                    result = predicate.PredicateDelegate(this, arguments);
                }
                catch
                {
                    result = false;
                }

                return(WamValueBoolean.Create(result));
            }
            return(wamReferenceTarget);
        }
Example #4
0
        private bool Unify(WamReferenceTarget lhs, WamReferenceTarget rhs, bool testOnly)
        {
            if (lhs == null)
            {
                throw new ArgumentNullException("lhs");
            }
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs");
            }

            lhs = lhs.Dereference();
            rhs = rhs.Dereference();

            WamVariable lhsVariable = lhs as WamVariable;
            WamVariable rhsVariable = rhs as WamVariable;

            WamCompoundTerm lhsCompoundTerm = lhs as WamCompoundTerm;
            WamCompoundTerm rhsCompoundTerm = rhs as WamCompoundTerm;

            WamValue lhsValue = lhs as WamValue;
            WamValue rhsValue = rhs as WamValue;

            // Ensure that each term is either a compound term or variable.
            //
            Debug.Assert(lhsVariable != null || lhsCompoundTerm != null || lhsValue != null);
            Debug.Assert(rhsVariable != null || rhsCompoundTerm != null || rhsValue != null);

            // Ensure if that we've dereferenced to a variable that it is unbound.
            //
            Debug.Assert(lhsVariable == null || lhsVariable.Target == null);
            Debug.Assert(rhsVariable == null || rhsVariable.Target == null);

            if (lhsVariable != null)
            {
                if (rhsVariable != null)
                {
                    if (lhsVariable.Generation < rhsVariable.Generation)
                    {
                        if (!testOnly) Bind(rhsVariable, lhs);
                    }
                    else
                    {
                        if (!testOnly) Bind(lhsVariable, rhs);
                    }
                }
                else
                {
                    if (!testOnly) Bind(lhsVariable, rhs);
                }
            }
            else
            {
                if (rhsVariable != null)
                {
                    if (!testOnly) Bind(rhsVariable, lhs);
                }
                else
                {
                    if (lhsCompoundTerm != null && rhsCompoundTerm != null)
                    {
                        if (lhsCompoundTerm.Functor != rhsCompoundTerm.Functor)
                        {
                            return false;
                        }

                        for (int index = 0; index < lhsCompoundTerm.Functor.Arity; ++index)
                        {
                            if (!Unify(lhsCompoundTerm.Children[index], rhsCompoundTerm.Children[index], testOnly))
                            {
                                return false;
                            }
                        }
                    }
                    else if (lhsValue != null && rhsValue != null)
                    {
                        if (!lhsValue.Object.Equals(rhsValue.Object))
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }

            return true;
        }
Example #5
0
        bool Unify(WamReferenceTarget lhs, WamReferenceTarget rhs, bool testOnly)
        {
            if (lhs == null)
            {
                throw new ArgumentNullException("lhs");
            }
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs");
            }

            lhs = lhs.Dereference();
            rhs = rhs.Dereference();

            var lhsVariable = lhs as WamVariable;
            var rhsVariable = rhs as WamVariable;

            var lhsCompoundTerm = lhs as WamCompoundTerm;
            var rhsCompoundTerm = rhs as WamCompoundTerm;

            var lhsValue = lhs as WamValue;
            var rhsValue = rhs as WamValue;

            // Ensure that each term is either a compound term or variable.
            //
            Debug.Assert(lhsVariable != null || lhsCompoundTerm != null || lhsValue != null);
            Debug.Assert(rhsVariable != null || rhsCompoundTerm != null || rhsValue != null);

            // Ensure if that we've dereferenced to a variable that it is unbound.
            //
            Debug.Assert(lhsVariable == null || lhsVariable.Target == null);
            Debug.Assert(rhsVariable == null || rhsVariable.Target == null);

            if (lhsVariable != null)
            {
                if (rhsVariable != null)
                {
                    if (lhsVariable.Generation < rhsVariable.Generation)
                    {
                        if (!testOnly)
                        {
                            Bind(rhsVariable, lhs);
                        }
                    }
                    else
                    {
                        if (!testOnly)
                        {
                            Bind(lhsVariable, rhs);
                        }
                    }
                }
                else
                {
                    if (!testOnly)
                    {
                        Bind(lhsVariable, rhs);
                    }
                }
            }
            else
            {
                if (rhsVariable != null)
                {
                    if (!testOnly)
                    {
                        Bind(rhsVariable, lhs);
                    }
                }
                else
                {
                    if (lhsCompoundTerm != null && rhsCompoundTerm != null)
                    {
                        if (lhsCompoundTerm.Functor != rhsCompoundTerm.Functor)
                        {
                            return(false);
                        }

                        for (int index = 0; index < lhsCompoundTerm.Functor.Arity; ++index)
                        {
                            if (!Unify(lhsCompoundTerm.Children[index], rhsCompoundTerm.Children[index], testOnly))
                            {
                                return(false);
                            }
                        }
                    }
                    else if (lhsValue != null && rhsValue != null)
                    {
                        if (!lhsValue.Object.Equals(rhsValue.Object))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }