Example #1
0
        public void ImportApi(Type module, Env env)
        {
            foreach (var method in module.GetMethods())
            {
                var attributes = method.GetCustomAttributes(typeof(LispApiAttribute), true);
                foreach (var attr in attributes)
                {
                    var api  = (LispApiAttribute)attr;
                    var name = api.Name;
                    if (name == null)
                    {
                        name = method.Name.Replace('_', '-');
                    }
                    var      param = method.GetParameters();
                    Delegate del;
                    switch (param.Length)
                    {
                    case 1:
                        del = method.CreateDelegate(typeof(LispApi.Func0));
                        break;

                    case 2:
                        if (param[1].ParameterType == typeof(Value))
                        {
                            del = method.CreateDelegate(typeof(LispApi.Func1));
                        }
                        else
                        {
                            del = method.CreateDelegate(typeof(LispApi.FuncVararg));
                        }
                        break;

                    case 3:
                        del = method.CreateDelegate(typeof(LispApi.Func2));
                        break;

                    case 4:
                        del = method.CreateDelegate(typeof(LispApi.Func3));
                        break;

                    case 5:
                        del = method.CreateDelegate(typeof(LispApi.Func4));
                        break;

                    case 6:
                        del = method.CreateDelegate(typeof(LispApi.Func5));
                        break;

                    default:
                        throw new LispException("Invalid parameter size");
                    }
                    var nameSymbol = Symbol.Intern(name);
                    var func       = new LispApi(del, nameSymbol);
                    env.Define(nameSymbol, new Value(func));
                }
            }
        }
Example #2
0
 public Value(LispApi v)
 {
     if (v == null)
     {
         val_ = NilMark;
         obj_ = null;
     }
     else
     {
         val_ = LispApiMark;
         obj_ = v;
     }
 }