Пример #1
0
        /// <remarks>
        /// See also CodeValue.Create.
        /// </remarks>
        public static WamValue Create(CodeValue value)
        {
            if (value == null)
            {
                return(WamValueObject.Create(null));
            }

            if (value is CodeValueBoolean)
            {
                return(WamValueBoolean.Create(((CodeValueBoolean)value).Value));
            }
            if (value is CodeValueDateTime)
            {
                return(WamValueDateTime.Create(((CodeValueDateTime)value).Value));
            }
            if (value is CodeValueDouble)
            {
                return(WamValueDouble.Create(((CodeValueDouble)value).Value));
            }
            if (value is CodeValueException)
            {
                return(WamValueException.Create(((CodeValueException)value).Value));
            }
            if (value is CodeValueInteger)
            {
                return(WamValueInteger.Create(((CodeValueInteger)value).Value));
            }
            if (value is CodeValueString)
            {
                return(WamValueString.Create(((CodeValueString)value).Value));
            }
            if (value is CodeValueType)
            {
                return(WamValueType.Create(((CodeValueType)value).Value));
            }

            return(WamValueObject.Create(value.Object));
        }
Пример #2
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);
        }