/System.Xronos/Builtins/SpecialForms.FnForm.cs
C# | 151 lines | 105 code | 23 blank | 23 comment | 13 complexity | 7ba44007aa176269a3074322ce6ead9c MD5 | raw file
- /* ****************************************************************************
- *
- * Copyright (c) 2008 Stefan Rusek and Benjamin Pollack
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * ***************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xronos.Language;
- using Microsoft.Linq.Expressions;
- using System.Reflection;
- using System.Reflection.Emit;
- using System.Collections;
- using System.Xronos.Scripting.MetaObjects;
-
- namespace System.Xronos.Builtins
- {
- public static partial class SpecialForms
- {
- internal partial class FnForm : SpecialFormBase
- {
- class RestFunction { }
-
- static Dictionary<string, Type> cache = new Dictionary<string, Type>();
-
- protected override Expression Compile(Compiler compiler, ISequence rest, Scope scope)
- {
- var name = rest.first() as Symbol;
- if (name == null)
- name = Symbol.Create("__UnnamedMethod");
- else
- rest = rest.rest();
-
- int c = -1;
-
- var list = new List<LambdaExpression>();
- if (rest.first() is IPersistentVector)
- {
- list.Add(ParseFunction(compiler, name, ref c, RT.seq(rest), scope));
- }
- else
- {
- while (rest != null)
- {
- list.Add(ParseFunction(compiler, name, ref c, RT.seq(rest.first()), scope));
- rest = rest.rest();
- }
- }
-
- return GetDelegateFunction(name, list);
- }
-
- private Expression GetDelegateFunction(Symbol name, List<LambdaExpression> list)
- {
- var map = new List<Expression>();
- foreach (var item in list)
- {
- var arity = item.Parameters.Count - 1;
-
- while (map.Count < arity)
- map.Add(Expression.Constant(null, typeof(FunctionMapEntry)));
-
- var temp = Expression.Parameter(typeof(FunctionMapEntry));
- map.Add(Expression.Block(new[] { temp },
- Expression.Assign(temp, Expression.New(typeof(FunctionMapEntry))),
- Expression.Assign(Expression.Field(temp, "Arity"), Expression.Constant(arity)),
- Expression.Assign(Expression.Field(temp, "Delegate"), item),
- Expression.Assign(Expression.Field(temp, "IsRest"), Expression.Constant(item.Annotations().Contains<RestFunction>())),
- temp));
- }
-
- return Expression.New(typeof(DelegateFunction).GetConstructors()[0], Expression.NewArrayInit(typeof(FunctionMapEntry), map));
- }
-
- private LambdaExpression ParseFunction(Compiler compiler, Symbol self, ref int c, ISequence cons, Scope scope)
- {
- var paramNames = (IList)cons.first();
- if (c >= paramNames.Count)
- throw new InvalidProgramException("Functions must be written in order of increasing numbers of parameters");
-
- c = paramNames.Count;
-
- var annotations = Annotations.Empty;
- var paramExprs = new List<ParameterExpression>();
-
- scope = new Scope(scope);
-
- {
- var param = Expression.Parameter(typeof(object), self.ToString());
- paramExprs.Add(param);
- scope[self] = param;
- }
-
- foreach (var item in paramNames)
- {
- var name = (Symbol)item;
-
- if (name.Namespace == null && name.Name == "&")
- {
- annotations = annotations.Add(new RestFunction());
- continue;
- }
-
- var param = Expression.Parameter(annotations.Contains<RestFunction>() ? typeof(ISequence) : typeof(object), name.ToString());
- paramExprs.Add(param);
- scope[name] = param;
- }
-
- if (annotations.Contains<RestFunction>() && !paramNames[paramNames.Count - 2].Equals(Symbol.Create("&")))
- throw new ReaderException("& cannot appear before any but the last parameter");
-
- var label = Expression.Label();
- var vars = new List<Expression>(from p in paramExprs select (Expression)p);
- vars.RemoveAt(0);
-
- scope.LoopLabel = label;
- scope.LoopVariables = vars;
-
- return Lambda(
- Expression.Convert(
- LoopForm.BuildOuterLoop(compiler, null, label, DoForm.CompileDo(compiler, null, cons.rest(), null, scope), scope),
- typeof(object)),
- self.ToString(), annotations, paramExprs.ToArray());
- }
-
- public LambdaExpression Lambda(Expression body, string name, Annotations annotations, ParameterExpression[] parameters)
- {
- return Expression.Lambda(body, name, parameters).Annotate(annotations);
- }
- }
- }
- }