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); }
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); }
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); }
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); }
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); }
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); }
private static Invokeable AddStrings() { return(new Invokeable { ReturnType = ItemType.String, Demands = InvokeableUtils.MakeDemands( args => args.All(arg => arg.ItemType == ItemType.String)), Function = (space, args) => new ValueItem( ItemType.String, string.Join("", args .Select(arg => arg as ValueItem) .Select(arg => (string)arg.Value))) }); }
private static Invokeable AddNumbers() { return(new Invokeable { ReturnType = ItemType.Number, Demands = InvokeableUtils.MakeDemands( args => args.All(arg => arg.ItemType == ItemType.Number)), Function = (space, args) => new ValueItem( ItemType.Number, args .Select(arg => arg as ValueItem) .Select(arg => (double)arg.Value) .Sum()) }); }
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); }
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); }
private List <Func <List <Item>, bool> > GetParameterDemandsFromMethodInfo(MethodInfo mi) { var parameters = mi.GetParameters(); Func <ParameterInfo, int, Func <List <Item>, bool> > getDemandForParameter = (p, i) => InvokeableUtils.DemandType(i, this.GetTypeFromClrType(p.ParameterType)); var countDemand = InvokeableUtils.DemandCount(parameters.Count()); var typeDemands = parameters .Select((p, i) => getDemandForParameter(p, i)); var demands = new List <Func <List <Item>, bool> > { countDemand }; demands.AddRange(typeDemands); return(InvokeableUtils.MakeDemands(demands.ToArray())); }
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); }
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); }
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))) }); }
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); }
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); }