Пример #1
0
        public static LuryObject PrintLine(LuryObject value)
        {
            var text = value + "\n";

            Console.Write(text);
            return(LuryString.GetObject(text));
        }
Пример #2
0
        public static LuryObject Print(LuryObject value)
        {
            var text = value.ToString();

            Console.Write(text);
            return(LuryString.GetObject(text));
        }
Пример #3
0
        public void SetMember(string name, LuryObject obj)
        {
            if (IsFrozen)
            {
                throw new InvalidOperationException();
            }

            if (members.ContainsKey(name))
            {
                members[name] = obj;
            }
            else
            {
                members.Add(name, obj);
            }
        }
Пример #4
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);
        }
Пример #5
0
 public void SetMemberNoRecursion(string name, LuryObject value)
 {
     members.Add(name, value);
 }