Beispiel #1
0
            public static void Setup()
            {
                var invo = new InvokeableItem();

                invo.AddInvokeable(AddNumbers());
                invo.AddInvokeable(AddStrings());

                globalSpace.Bind("+", invo);
            }
Beispiel #2
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Something,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(
                            ItemType.Space,
                            ItemType.List),
                        args => args.Count == 2),

                    Function = (space, args) =>
                    {
                        var symbolSpace = args[0] as SymbolSpaceItem;
                        var expression  = args[1] as ListItem;

                        var spaceParent = symbolSpace.Space.GetParent();
                        symbolSpace.Space.SetParent(space);
                        space.SetParent(spaceParent);

                        var result = expression.Evaluate(symbolSpace.Space);

                        symbolSpace.Space.SetParent(spaceParent);

                        return(result);
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("in", invo);
            }
Beispiel #3
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Any,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(
                            ItemType.Bool,
                            ItemType.Something),
                        args => args.Count == 2),

                    Function = (space, args) =>
                    {
                        var condition = args[0] as ValueItem;
                        var ifBody    = args[1];

                        if (!(bool)condition.Value)
                        {
                            return(ifBody is ListItem
                                                                ? (ifBody as ListItem).Evaluate(space)
                                                                : ifBody);
                        }

                        return(null);
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("unless", invo);
            }
Beispiel #4
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Space,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandType(0, ItemType.List),
                        args => (args[0] as ListItem).Expression.HasEvenLength(),
                        args => (args[0] as ListItem).Expression
                        .GroupingSelect(2)
                        .All(pair => pair[0].ItemType == ItemType.Symbol)),

                    Function = (space, args) =>
                    {
                        var newSpace         = new SymbolSpace(space);
                        var symbolValuePairs = (args[0] as ListItem).Expression.GroupingSelect(2);

                        foreach (var pair in symbolValuePairs)
                        {
                            var symbol = pair[0] as SymbolItem;
                            newSpace.Bind(symbol.Name, pair[1]);
                        }

                        return(new SymbolSpaceItem(newSpace));
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("make-space", invo);
            }
Beispiel #5
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Symbol,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(ItemType.Symbol, ItemType.Space),
                        args => args.Count == 2),

                    Function = (space, args) =>
                    {
                        var symbol      = args[0] as SymbolItem;
                        var symbolSpace = args[1] as SymbolSpaceItem;

                        symbol.BoundSpace = symbolSpace.Space;

                        return(symbol.Quote());
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("bind", invo);
            }
Beispiel #6
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Something,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandType(0, ItemType.Symbol),
                        args => args.Count == 1),

                    Function = (space, args) =>
                    {
                        var symbol = args[0] as SymbolItem;

                        var reference = space.Lookup(symbol.Name);

                        return(reference is EvaluateableItem
                                                        ? (reference as EvaluateableItem).Quote()
                                                        : reference);
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("deref-symbol", invo);
            }
Beispiel #7
0
            public static void Setup()
            {
                var invo = new InvokeableItem();

                invo.AddInvokeable(CreateInvokeable());
                globalSpace.Bind("=", invo);
            }
Beispiel #8
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Something,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandOfAnyType(0,
                                                        ItemType.Symbol,
                                                        ItemType.List),
                        args => args.Count == 1),

                    Function = (space, args) =>
                    {
                        var evaluateable = args[0] as EvaluateableItem;

                        var item = evaluateable is SymbolItem
                                                        ? (EvaluateableItem)(Deref(space, evaluateable as SymbolItem) as EvaluateableItem).Unquote()
                                                        : evaluateable;

                        return(item.Evaluate(space));
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind(";", invo);
            }
Beispiel #9
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.None,

                    Demands = InvokeableUtils.MakeDemands(args => args.Count == 1),

                    Function = (space, args) =>
                    {
                        Console.WriteLine(args[0].Print());
                        return(null);
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("print", invo);
            }
Beispiel #10
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Symbol,

                    Demands = InvokeableUtils.MakeDemands(InvokeableUtils.DemandOfAnyType(0, ItemType.List, ItemType.Symbol)),

                    Function = (space, args) =>
                    {
                        var symbol = args[0] as EvaluateableItem;
                        return(symbol.Quote());
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("`", invo);
                globalSpace.Bind("quote", invo);
            }
Beispiel #11
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Number,

                    Demands = InvokeableUtils.MakeDemands(
                        args => args.Count == 1,
                        InvokeableUtils.DemandType(0, ItemType.Number)),

                    Function = (space, args) =>
                    {
                        var item = args[0] as ValueItem;

                        return(new ValueItem(ItemType.Number, -(double)item.Value));
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("_", invo);
            }
Beispiel #12
0
        public void BindMethod(string symbol, MethodInfo mi, object boundObject)
        {
            var invokeable = new InvokeableItem();

            var fn = new Invokeable()
            {
                ReturnType = this.GetReturnTypeFromMethodInfo(mi),

                Demands = this.GetParameterDemandsFromMethodInfo(mi),

                Function = (space, args) =>
                {
                    if (!args.All(i => ValueItem.IsValueType(i.ItemType)))
                    {
                        throw new Exception("Tried calling bound method with non-value arguments.");
                    }

                    var arguments = args
                                    .Select(i => i as ValueItem)
                                    .Select(i => i.Value)
                                    .ToArray();

                    var result = mi.Invoke(boundObject, arguments);

                    if (result != null)
                    {
                        var resultType = this.GetTypeFromClrType(result.GetType());
                        var resultItem = new ValueItem(resultType, result);
                        return(resultItem);
                    }

                    return(null);
                }
            };

            invokeable.AddInvokeable(fn);
            this.globalSpace.Bind(symbol, invokeable);
        }
Beispiel #13
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Symbol,

                    Demands = InvokeableUtils.MakeDemands(InvokeableUtils.DemandType(0, ItemType.Symbol)),

                    Function = (space, args) =>
                    {
                        var symbol       = args[0] as SymbolItem;
                        var value        = args[1];
                        var bindingSpace = symbol.BoundSpace ?? space;

                        bindingSpace.Bind(symbol.Name, value);

                        return(symbol);
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("def", invo);
            }
Beispiel #14
0
            public static void Setup()
            {
                var invo = new InvokeableItem();
                var fn   = new Invokeable
                {
                    ReturnType = ItemType.Symbol,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(
                            ItemType.Space,
                            ItemType.Symbol)),

                    Function = (space, args) =>
                    {
                        var sourceSpace = args[0] as SymbolSpaceItem;
                        var symbol      = args[1] as SymbolItem;

                        return(new SymbolItem(symbol.Name, sourceSpace.Space));
                    }
                };

                invo.AddInvokeable(fn);
                globalSpace.Bind("get", invo);
            }
Beispiel #15
0
            public static void Setup()
            {
                const int typeIndex   = 0;
                const int paramsIndex = 1;
                const int bodyIndex   = 2;

                Func <Item, List <Tuple <Item, Item> > > groupArguments = l =>
                                                                          (l as ListItem).Expression
                                                                          .GroupingSelect(2, xs => new Tuple <Item, Item>(xs[0], xs[1]));

                Func <List <Item>, ListItem> getParams = args => (args[paramsIndex] as ListItem);

                // now to the meat of things

                var invo = new InvokeableItem();

                var fn = new Invokeable
                {
                    ReturnType = ItemType.Invokeable,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(ItemType.Type, ItemType.List, ItemType.List),
                        args => args.Count == 3,
                        args => getParams(args).Expression.HasEvenLength(),
                        args => groupArguments(getParams(args)).All(pair =>
                                                                    pair.Item1.ItemType == ItemType.Symbol &&
                                                                    pair.Item2.ItemType == ItemType.Type)
                        ),

                    Function = (space, args) =>
                    {
                        var returnType   = args[typeIndex] as TypeItem;
                        var argumentList = groupArguments(args[paramsIndex]);
                        var body         = args[bodyIndex] as ListItem;

                        var argumentTypes =
                            argumentList
                            .Select(argument => (argument.Item2 as TypeItem).Type)
                            .ToArray();

                        var resultingInvokeable = new InvokeableItem();

                        resultingInvokeable.AddInvokeable(new Invokeable()
                        {
                            ReturnType = returnType.Type,

                            Demands = InvokeableUtils.MakeDemands(InvokeableUtils.DemandTypes(argumentTypes)),

                            // create symbol space,
                            // bind fnargs,
                            // evaluate body with new symbol space
                            Function = (fnspace, fnargs) => {
                                var newSpace = new SymbolSpace(space);

                                for (var index = 0; index < fnargs.Count; index++)
                                {
                                    newSpace.Bind(
                                        (argumentList[index].Item1 as SymbolItem).Name,
                                        fnargs[index]);
                                }

                                body = body.Unquote() as ListItem;
                                return(body.Evaluate(newSpace));
                            }
                        });

                        return(resultingInvokeable);
                    }
                };

                invo.AddInvokeable(fn);

                globalSpace.Bind("=>", invo);
            }