AddConstant() public method

Adds a constant to the context.
public AddConstant ( String name, IConstants constant ) : ParseContext
name String /// The name of the constant. ///
constant IConstants /// The class instance of the constant. ///
return ParseContext
Example #1
0
        /// <summary>
        /// Installs the plugin.
        /// </summary>
        public void Install()
        {
            var types    = _assembly.GetTypes();
            var elements = _context.Elements;

            foreach (var type in types)
            {
                if (!type.IsAbstract)
                {
                    if (type.Name.EndsWith("Value", StringComparison.Ordinal))
                    {
                        var value = Instantiate <IRegisterElement>(elements, type);
                        value.RegisterElement(elements);
                        _valueTypes.Add(type.Name.RemoveValueConvention());
                    }
                    else
                    {
                        var interfaces = type.GetInterfaces();
                        var element    = Instantiate <IRegisterElement>(elements, type, interfaces);

                        if (element != null)
                        {
                            element.RegisterElement(elements);
                        }

                        var function = Instantiate <IFunction>(elements, type, interfaces);

                        if (function != null)
                        {
                            bool isNested;
                            var  name = FunctionName(type, out isNested);
                            _functions.Add(name);
                            _context.AddFunction(name, function);
                        }

                        var constant = Instantiate <IConstants>(elements, type, interfaces);

                        if (constant != null)
                        {
                            var name = constant.Name;
                            _constants.Add(name);
                            _context.AddConstant(name, constant);
                        }

                        var loader = Instantiate <IFunctionLoader>(elements, type, interfaces);

                        if (loader != null)
                        {
                            _loaders.Add(loader);
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Registers the IFunction, IConstant and IRegisterToken token classes at the specified context.
        /// </summary>
        /// <param name="context">
        /// The context where the IFunction and IConstant instances will be placed.
        /// </param>
        /// <param name="assembly">
        /// The assembly to load.
        /// </param>
        /// <returns>The ID for the assembly.</returns>
        public int RegisterAssembly(ParseContext context, Assembly assembly)
        {
            var plugin = new Plugin(context, assembly.FullName);
            var types  = assembly.GetTypes();
            var ir     = typeof(IRegisterElement).Name;
            var fu     = typeof(IFunction).Name;
            var ct     = typeof(IConstants).Name;

            foreach (var type in types)
            {
                if (type.IsAbstract)
                {
                    continue;
                }

                if (type.Name.EndsWith("Value"))
                {
                    var ctor = type.GetConstructor(Value.EmptyTypes);

                    if (ctor != null)
                    {
                        (ctor.Invoke(null) as IRegisterElement).RegisterElement();
                        plugin.ValueTypes.Add(type.Name.RemoveValueConvention());
                    }

                    continue;
                }

                var interfaces = type.GetInterfaces();

                if (interfaces.Any(iface => iface.Name.Equals(ir)))
                {
                    var ctor = type.GetConstructor(Value.EmptyTypes);

                    if (ctor != null)
                    {
                        (ctor.Invoke(null) as IRegisterElement).RegisterElement();
                    }
                }

                if (interfaces.Any(iface => iface.Name.Equals(fu)))
                {
                    var ctor = type.GetConstructor(Value.EmptyTypes);

                    if (ctor != null)
                    {
                        var name   = type.Name.RemoveFunctionConvention().ToLower();
                        var method = ctor.Invoke(null) as IFunction;
                        plugin.Functions.Add(name);
                        context.AddFunction(name, method);
                    }
                }

                if (interfaces.Any(iface => iface.Name.Equals(ct)))
                {
                    var ctor = type.GetConstructor(Value.EmptyTypes);

                    if (ctor != null)
                    {
                        var instc = ctor.Invoke(null) as IConstants;
                        plugin.Functions.Add(instc.Name);
                        context.AddConstant(instc.Name, instc);
                    }
                }
            }

            plugins.Add(plugin.Id, plugin);
            return(plugin.Id);
        }
Example #3
0
 /// <summary>
 /// Adds a custom constant to the parser using a specific context.
 /// </summary>
 /// <param name="context">
 /// The context where this constant should be made available.
 /// </param>
 /// <param name="name">
 /// The name of the symbol corresponding to the constant.
 /// </param>
 /// <param name="constant">
 /// The value of the constant.
 /// </param>
 /// <returns>The given context.</returns>
 public static ParseContext AddCustomConstant(ParseContext context, string name, Value constant)
 {
     context.AddConstant(name, new ContainerConstant(name, constant));
     return(context);
 }