예제 #1
0
        public override Value Evaluate()
        {
            var stack   = State.Stack;
            var target  = stack.Pop(true, LOCATION, false);
            var subject = stack.Pop(false, LOCATION);

            switch (target.Type)
            {
            case ValueType.Object:
            case ValueType.Class:
            case ValueType.Tuple:
            case ValueType.MessagePath:
                return(SendMessage(target, "apply", subject));
            }

            var arguments = new Arguments();

            if (subject.IsVariable)
            {
                var variable = (Variable)subject;
                arguments.ApplyVariable = variable;
                arguments.ApplyValue    = variable.Value;
            }
            else
            {
                arguments.ApplyVariable = null;
                arguments.ApplyValue    = subject;
            }
            return(MessagingState.SendMessage(target, "apply", arguments));
        }
예제 #2
0
        Value evaluate(Value x, Value y)
        {
            if (y.Type == Value.ValueType.Complex && x.Type != Value.ValueType.Complex)
            {
                x = new Complex(x.Number);
            }
            if (y.Type == Value.ValueType.Big && x.Type != Value.ValueType.Big)
            {
                x = new Big(x.Number);
            }
            if (x.Type == Value.ValueType.Case && y.Type != Value.ValueType.Case)
            {
                var _case = (Case)x;
                y = new Case(_case.Value, y, false, _case.Required, _case.Condition);
            }
            switch (x.Type)
            {
            case Value.ValueType.Date:
            case Value.ValueType.Rational:
            case Value.ValueType.Complex:
            case Value.ValueType.Big:
            case Value.ValueType.Case:
            case Value.ValueType.Object:
            case Value.ValueType.Set:
                return(MessagingState.SendMessage(x, Message, new Arguments(y)));

            default:
                return((x.IsArray || y.IsArray) && UseArrayVersion?evaluateArray(x, y) : Evaluate(x, y));
            }
        }
예제 #3
0
        protected Value evaluate(Value x, Value y)
        {
            x = y.Type switch
            {
                Value.ValueType.Complex when x.Type != Value.ValueType.Complex => new Complex(x.Number),
                Value.ValueType.Big when x.Type != Value.ValueType.Big => new Big(x.Number),
                _ => x
            };

            if (x.Type == Value.ValueType.Case && y.Type != Value.ValueType.Case)
            {
                var _case = (Case)x;
                y = new Case(_case.Value, y, false, _case.Required, _case.Condition);
            }

            return(x.Type switch
            {
                Value.ValueType.Date => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Rational => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Complex => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Big => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Case => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Object => MessagingState.SendMessage(x, Message, new Arguments(y)),
                Value.ValueType.Set => MessagingState.SendMessage(x, Message, new Arguments(y)),
                _ => (x.IsArray || y.IsArray) && UseArrayVersion?evaluateArray(x, y) : Evaluate(x, y)
            });
예제 #4
0
        public override Value Evaluate()
        {
            var stack     = State.Stack;
            var value     = stack.Pop(true, LOCATION);
            var target    = stack.Pop(true, LOCATION);
            var arguments = FromValue(value, false);

            return(MessagingState.SendMessage(target, "skip", arguments));
        }
예제 #5
0
        public override Value Evaluate()
        {
            var stack      = State.Stack;
            var executable = stack.Pop(true, LOCATION);
            var target     = stack.Pop(true, LOCATION);
            var arguments  = GuaranteedExecutable(executable);

            return(MessagingState.SendMessage(target, "group", arguments));
        }
예제 #6
0
        public override Value Evaluate()
        {
            var stack     = State.Stack;
            var value     = stack.Pop(true, LOCATION);
            var target    = stack.Pop(true, LOCATION);
            var arguments = PipelineSource(value);

            return(MessagingState.SendMessage(target, Message, arguments));
        }
예제 #7
0
        public override Value Evaluate()
        {
            var stack     = State.Stack;
            var y         = stack.Pop(true, LOCATION, false);
            var x         = stack.Pop(true, LOCATION);
            var arguments = PipelineSource(y);

            return(MessagingState.SendMessage(x, "if", arguments));
        }
예제 #8
0
        public override Value Evaluate()
        {
            var stack     = State.Stack;
            var source    = stack.Pop(true, LOCATION, false);
            var target    = stack.Pop(false, LOCATION);
            var arguments = GuaranteedExecutable(source);

            return(MessagingState.SendMessage(target, "map", arguments));
        }
예제 #9
0
        Value evaluate(Variable variable, Value value)
        {
            switch (variable.Type)
            {
            case Value.ValueType.Big:
            case Value.ValueType.Object:
                return(MessagingState.SendMessage(variable.Value, Message, new Arguments(value)));

            default:
                return(Execute(variable, value));
            }
        }
예제 #10
0
        public override Value Evaluate(Value x, Value y)
        {
            var generator = x.PossibleIndexGenerator();
            var arguments = new Arguments(y);

            return(generator.FlatMap(g =>
            {
                var list = new GeneratorList();
                list.Add(g);
                return MessagingState.SendMessage(list, "concat", arguments);
            }, () => MessagingState.SendMessage(x, "concat", arguments)));
        }
예제 #11
0
        public override Value Evaluate()
        {
            var stack         = State.Stack;
            var operatorValue = stack.Pop(true, LOCATION);
            var value         = stack.Pop(true, LOCATION).Self;

            if (operatorValue is Lambda lambda)
            {
                return(MessagingState.SendMessage(value, "foldr", FromValue(lambda)));
            }

            throw LOCATION.ThrowsWithLocation(() => "Expected lambda");
        }
예제 #12
0
파일: FoldR.cs 프로젝트: toddcoder/Orange
        public override Value Evaluate()
        {
            var stack         = State.Stack;
            var operatorValue = stack.Pop(true, LOCATION);
            var value         = stack.Pop(true, LOCATION).Self;

            if (operatorValue is Lambda lambda)
            {
                return(MessagingState.SendMessage(value, "foldr", FromValue(lambda)));
            }

            Throw(LOCATION, "Expected lambda");
            return(null);
        }
예제 #13
0
파일: FlatMap.cs 프로젝트: toddcoder/Orange
        public override Value Evaluate()
        {
            var stack      = State.Stack;
            var code       = stack.Pop(true, LOCATION, false);
            var arrayValue = stack.Pop(true, LOCATION);

            if (arrayValue.IsArray)
            {
                var       array = (Array)arrayValue.SourceArray;
                Arguments arguments;
                switch (code)
                {
                case Lambda lambda:
                    arguments = new Arguments(new NullBlock(), lambda.Block, lambda.Parameters);
                    return(MessagingState.SendMessage(array, MESSAGE_NAME, arguments));

                case Block block:
                    arguments = new Arguments(new NullBlock(), block, new NullParameters());
                    return(MessagingState.SendMessage(array, MESSAGE_NAME, arguments));

                case IStringify _:
                    var stringifyBlock = new Block {
                        new Push(code)
                    };
                    arguments = new Arguments(new NullBlock(), stringifyBlock, new NullParameters());
                    return(MessagingState.SendMessage(array, MESSAGE_NAME, arguments));

                case Values.Format format:
                    var formatBlock = new Block {
                        new Push(format.Stringify.String)
                    };
                    arguments = new Arguments(new NullBlock(), formatBlock, format.Parameters);
                    return(MessagingState.SendMessage(array, MESSAGE_NAME, arguments));

                default:
                    return(arrayValue);
                }
            }

            return(arrayValue);
        }
예제 #14
0
        public override Value Evaluate()
        {
            var stack  = State.Stack;
            var format = stack.Pop(true, LOCATION);
            var value  = stack.Pop(true, LOCATION, false);

            switch (value.Type)
            {
            case ValueType.Object:
            case ValueType.Rational:
                return(SendMessage(value, "fmt", format));
            }

            if (format.IsArray)
            {
                return(MessagingState.SendMessage(value, "format", new Arguments(format.SourceArray)));
            }

            var matcher = new Matcher();
            var input   = format.Text;

            if (matcher.IsMatch(input, REGEX_FORMAT))
            {
                var formatter = new Formatter(matcher.FirstGroup, matcher.SecondGroup, matcher.ThirdGroup);
                return(formatter.Format(value.Text));
            }

            switch (value.Type)
            {
            case ValueType.Number:
                return(value.Number.ToString(input));

            case ValueType.Date:
                return(((Date)value).DateTime.ToString(input));
            }

            var formatString = "{0:\"" + input + "\"}";

            return(string.Format(formatString, ConvertToObject(value.Text, input.EndsWith("x}"))));
        }
예제 #15
0
        public override Value Evaluate()
        {
            var value = State.Stack.Pop(true, "Sequence");

            return(value is Object obj ? new ObjectSequence(obj) : MessagingState.SendMessage(value, "seq", new Arguments()));
        }
예제 #16
0
        public Value Random()
        {
            var array = AlternateValue("rand");

            return(MessagingState.SendMessage(array, "sample", new Arguments()));
        }
예제 #17
0
        public static Value DefaultSendMessage(Value value, string messageName, Arguments arguments, out bool handled)
        {
            handled = true;
            InterpolatedString interpolated;
            double             number;

            switch (messageName)
            {
            case "print":
                print(value.Text, arguments);
                return(null);

            case "put":
                put(value.Text, arguments);
                return(null);

            case "date":
                return(new Date(value));

            case "but":
                return(elseMessage(value, arguments));

            case "kind":
                return(new TypeName(value.Type.ToString()));

            case "match":
                var match = new Match(value);
                return(match.Evaluate(arguments.Executable));

            case "str":
            {
                return(value is Object obj && obj.RespondsNoDefault("str") ? Runtime.SendMessage(obj, "str", arguments) : value.Text);
            }

            case "num":
                return(value.Number);

            case "id":
                return(value.ID);

            case "dup":
                return(value.Clone());

            case "write":
                write(value.Text, arguments);
                return(null);

            case "newMessage":
                value.RegisterMessages();
                if (arguments[0] is Values.Message message)
                {
                    value.RegisterUserMessage(message);
                    return(value);
                }

                return(new Nil());

            case "cmp":
            {
                return(value is Object obj && obj.RespondsNoDefault("cmp") ? Runtime.SendMessage(obj, "cmp", arguments) :
                       value.Compare(arguments[0]));
            }

            case "isResp":
                return(MessagingState.RespondsTo(value, arguments[0].Text));

            case "isEmpty":
                return(value.IsEmpty);

            case "fmt":
                var source    = arguments[0].Text;
                var formatter = Formatter.Parse(source);
                return(formatter.Format(value.Text));

            case "return":
                value        = value.Resolve();
                interpolated = value as InterpolatedString;
                if (interpolated != null)
                {
                    value = interpolated.String;
                }
                State.ReturnValue  = value;
                State.ReturnSignal = true;
                return(State.ReturnValue);

            case "result":
                value        = value.Resolve();
                interpolated = value as InterpolatedString;
                if (interpolated != null)
                {
                    value = interpolated.String;
                }
                State.ResultValue = value;
                return(value);

            case "squote":
                return(value.Text.SingleQuotify("`'"));

            case "dquote":
                return(value.Text.Quotify("`\""));

            case "rep":
                return(value.Resolve().ToString());

            case "isNum":
                return(value.IsNumeric() ? value.Number : value);

            case "exit":
                if (value.IsTrue)
                {
                    State.ExitSignal = true;
                }
                return(null);

            case "after":
                State.LateBlock = arguments.Executable;
                return(value);

            case "give":
                State.Give(value);
                return(value);

            case "with":
                return(with(value, arguments));

            case "alt":
                return(value.AlternateValue("alt"));

            case "for":
                handled = false;
                return(forLoop(value, arguments, out handled));

            case "json":
                return(value.Text);

            case "send":
                var argClone = arguments.Clone();
                messageName = argClone.Shift().Text;
                return(MessagingState.SendMessage(value, messageName, argClone));

            case "assert":
                if (!value.IsTrue)
                {
                    var errorMessage = arguments[0].Text;
                    if (errorMessage.IsEmpty())
                    {
                        errorMessage = "Assertion failed";
                    }
                    throw new ApplicationException(errorMessage);
                }

                return(null);

            case "freeze":
                freeze(value, arguments);
                return(value);

            case "thaw":
                return(thaw(value));

            case "isVal":
                return(arguments.IsArray() ? arguments.AsArray().ContainsValue(value) : arguments[0].Text.Has(value.Text,
                                                                                                              true));

            case "isKey":
                return(arguments.IsArray() && arguments.AsArray().ContainsKey(value.Text));

            case "can":
                return(can(value, arguments));

            case "isInt":
                number = value.Number;
                return(number == Math.Truncate(number));

            case "isFloat":
                number = value.Number;
                return(number != Math.Truncate(number));

            case "isArr":
                return(value.IsArray);

            case "isInv":
                return(invokeable(value));

            case "isTrue":
                return(value.IsTrue);

            case "isIter":
                return(value.ProvidesGenerator);

            case "tap":
                return(tap(value, arguments));

            case "isFailure":
                return(value.Type == Value.ValueType.Failure);
            }

            handled = false;

            return(null);
        }
예제 #18
0
 public static Value GetSuccessor(Value value) => MessagingState.SendMessage(value, "succ", new Arguments());
예제 #19
0
        public override Value Evaluate()
        {
            var stack = State.Stack;
            var value = stack.Pop(false, LOCATION);

            arguments.FromSelf = arguments.FromSelf;
            if (value is Variable variable && variable.Name == "super" && variable.Value is Class super)
            {
                return(SendSuperMessage(super, message, arguments));
            }

            if (registerCall)
            {
                MessagingState.RegisterMessageCall(message);
            }
            value = value.Resolve();
            var responds = MessagingState.RespondsTo(value, message);

            if (optional && !responds)
            {
                return(new Nil());
            }

            if (!optional && !responds)
            {
                message  = GetterName(message);
                responds = MessagingState.RespondsTo(value, message);
                if (optional && !responds)
                {
                    return(new Nil());
                }
            }

            Assert(optional || responds, LOCATION, () => $"{value} doesn't understand {Unmangle(message)} message");
            var result = MessagingState.SendMessage(value, message, arguments);

            if (result is ObjectVariable objectVariable && objectVariable.Value is Class cls)
            {
                return(SendMessage(cls, "invoke", arguments));
            }

            if (optional)
            {
                if (result is ObjectVariable innerValue)
                {
                    if (innerValue.Value is Some some)
                    {
                        return(some.Value());
                    }

                    if (innerValue.Value is None)
                    {
                        return(None.NoneValue);
                    }

                    return(innerValue.Value);
                }
            }

            if (value is Variable variable1 && inPlace)
            {
                Reject(variable1.Name.StartsWith(VAR_ANONYMOUS), LOCATION, "Can't reassign to an anonymous variable");
                variable1.Value = result;
            }
            return(result);
        }