示例#1
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);
            }
示例#2
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);
            }
示例#3
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);
            }
示例#4
0
            private static Invokeable setup()
            {
                Func <List <Item>, List <List <Item> > > getPairs = a =>
                                                                    (a[0] as ListItem).Expression
                                                                    .GroupingSelect(2);

                Func <List <Item>, Tuple <SymbolItem, Item> > pairUp = pair =>
                                                                       new Tuple <SymbolItem, Item>(pair[0] as SymbolItem, pair[1]);

                return(new Invokeable
                {
                    ReturnType = ItemType.Something,

                    Demands = InvokeableUtils.MakeDemands(
                        InvokeableUtils.DemandTypes(
                            ItemType.List,
                            ItemType.List),
                        args => getPairs(args).All(pair => pair[0] is SymbolItem),
                        args => args.Count == 2),

                    Function = (space, args) =>
                               (args[1] as ListItem).Evaluate(new SymbolSpace(space, getPairs(args).Select(pairUp)))
                });
            }
示例#5
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);
            }
示例#6
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);
            }