Exemple #1
0
        public LuryObject GetMember(string name, LuryContext context)
        {
            if (members.ContainsKey(name))
            {
                return(members[name]);
            }

            if (LuryTypeName != null && context.HasMember(LuryTypeName))
            {
                return(context[LuryTypeName].GetMemberNoRecursion(name));
            }

            //throw new LuryException(LuryExceptionType.NameIsNotFound);
            throw new InvalidOperationException();
        }
Exemple #2
0
 public LuryContext(LuryContext parent)
 {
     Parent = parent;
 }
Exemple #3
0
        public static LuryContext CreateGlobalContext()
        {
            var context = new LuryContext();

            #region Intrinsic Classes

            Action <string, string, IEnumerable <Tuple <string, MethodInfo> > > setFunctionMember = (t, n, f) =>
            {
                var type = new LuryObject(t, null);

                foreach (var item in f)
                {
                    type.SetMember(item.Item1, new LuryObject(LuryFunction.FullName, item.Item2, true));
                }

                type.Freeze();
                context[n] = type;
                context[type.LuryTypeName] = type;
            };

            var intrinsicTypes = Assembly
                                 .GetExecutingAssembly()
                                 .GetTypes()
                                 .Where(t =>
                                        t.IsClass &&
                                        Attribute.GetCustomAttribute(t, typeof(IntrinsicClassAttribute)) != null);

            foreach (var type in intrinsicTypes)
            {
                var attr      = type.GetCustomAttribute <IntrinsicClassAttribute>();
                var methods   = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
                var funcnames = methods
                                .Select(
                    method =>
                    new
                {
                    method,
                    attributes = method.GetCustomAttributes <IntrinsicAttribute>().Select(a => a.TargetFunction)
                })
                                .SelectMany(_ => _.attributes, (_, funcName) => Tuple.Create(funcName, _.method)).ToList();

                if (funcnames.Count > 0)
                {
                    setFunctionMember(attr.FullName, attr.TypeName, funcnames);
                }
            }

            #endregion

            #region BuiltIn Functions

            var builtInFunctions = Assembly
                                   .GetExecutingAssembly()
                                   .GetTypes()
                                   .Where(t =>
                                          t.IsClass &&
                                          Attribute.GetCustomAttribute(t, typeof(BuiltInClass)) != null);

            foreach (var type in builtInFunctions)
            {
                var attr      = type.GetCustomAttribute <BuiltInClass>();
                var methods   = type.GetMethods(BindingFlags.Static | BindingFlags.Public);
                var funcnames = methods
                                .Select(
                    method =>
                    new
                {
                    method,
                    attributes = method.GetCustomAttributes <BuiltInAttribute>().Select(a => a.FunctionName)
                })
                                .SelectMany(_ => _.attributes, (_, funcName) => Tuple.Create(funcName, _.method)).ToList();

                foreach (var func in funcnames)
                {
                    context[func.Item1] = LuryFunction.GetObject(func.Item2);
                }
            }

            #endregion

            return(context);
        }
Exemple #4
0
 public LuryEngine()
 {
     Context = LuryContext.CreateGlobalContext();
 }