Beispiel #1
0
        /// <summary>
        /// Creates the entry point for an assembly if it is supposed to be saved.
        /// The entry point method basically calls the Run method and discards the result.
        /// </summary>
        private void createEntryPoint()
        {
            var ep = MainType.CreateMethod(EntityNames.EntryPointMethodName, "Void", args: null, isStatic: true);

            ep.Kind = TypeContentsKind.AutoGenerated;
            ep.Body = Expr.Block(
                Expr.Invoke(Expr.New(EntityNames.MainTypeName), "Run"),
                Expr.Unit()
                );
        }
Beispiel #2
0
        /// <summary>
        /// Declares a new function.
        /// </summary>
        private void declareFunction(FunctionNode node)
        {
            if (node.Name == "_")
            {
                Error(CompilerMessages.UnderscoreName);
            }

            var isVariadic = false;

            // validation
            if (node.Arguments.Count > 0)
            {
                for (var idx = 0; idx < node.Arguments.Count; idx++)
                {
                    var curr = node.Arguments[idx];
                    if (curr.Name == "_")
                    {
                        curr.Name = Unique.AnonymousArgName();
                    }

                    if (curr.Type == typeof(UnspecifiedType))
                    {
                        Error(CompilerMessages.LambdaArgTypeUnknown);
                    }

                    if (curr.IsVariadic)
                    {
                        if (idx < node.Arguments.Count - 1)
                        {
                            Error(CompilerMessages.VariadicArgumentNotLast);
                        }

                        isVariadic = true;
                    }
                }
            }
            else
            {
                if (node.Name == EntityNames.RunMethodName || node.Name == EntityNames.EntryPointMethodName)
                {
                    Error(CompilerMessages.ReservedFunctionRedefinition, node.Name);
                }
            }

            var method = MainType.CreateMethod(node.Name, node.ReturnTypeSignature, node.Arguments, true, prepare: false);

            method.Kind       = TypeContentsKind.UserDefined;
            method.IsPure     = node.IsPure;
            method.IsVariadic = isVariadic;
            method.Body       = node.Body;
        }
Beispiel #3
0
        /// <summary>
        /// Declares a new type.
        /// </summary>
        private void declareType(TypeDefinitionNode node)
        {
            if (node.Name == "_")
            {
                Error(CompilerMessages.UnderscoreName);
            }

            var mainType = CreateType(node.Name, prepare: false);

            mainType.Kind = TypeEntityKind.Type;

            foreach (var curr in node.Entries)
            {
                var tagName   = curr.Name;
                var labelType = CreateType(tagName, node.Name, isSealed: true, defaultCtor: false, prepare: false);
                labelType.Kind = TypeEntityKind.TypeLabel;

                var ctor = labelType.CreateConstructor(prepare: false);
                ctor.Kind = TypeContentsKind.AutoGenerated;
                if (curr.IsTagged)
                {
                    var tagField = labelType.CreateField("Tag", curr.TagType, prepare: false);
                    tagField.Kind = TypeContentsKind.UserDefined;

                    var args = new HashList <FunctionArgument> {
                        { "value", new FunctionArgument("value", curr.TagType) }
                    };

                    var staticCtor = MainType.CreateMethod(tagName, tagName, new string[0], isStatic: true, prepare: false);
                    staticCtor.Kind = TypeContentsKind.AutoGenerated;

                    ctor.Arguments = staticCtor.Arguments = args;
                    ctor.Body.Add(
                        Expr.SetMember(Expr.This(), "Tag", Expr.Get("value"))
                        );

                    staticCtor.Body.Add(
                        Expr.New(tagName, Expr.Get("value"))
                        );
                }
            }
        }
Beispiel #4
0
        public Context(LensCompilerOptions options = null)
        {
            Options = options ?? new LensCompilerOptions();

            _definedTypes      = new Dictionary <string, TypeEntity>();
            _definedProperties = new Dictionary <string, GlobalPropertyInfo>();

            Unique = new UniqueNameGenerator();

            if (Options.UseDefaultNamespaces)
            {
                Namespaces.Add("System", true);
                Namespaces.Add("System.Linq", true);
                Namespaces.Add("System.Text.RegularExpressions", true);
            }

            AssemblyCache      = new ReferencedAssemblyCache(Options.UseDefaultAssemblies);
            _extensionResolver = new ExtensionMethodResolver(Namespaces, AssemblyCache);
            _typeResolver      = new TypeResolver(Namespaces, AssemblyCache)
            {
                ExternalLookup = name =>
                {
                    _definedTypes.TryGetValue(name, out var ent);
                    return(ent?.TypeBuilder);
                }
            };

            AssemblyName an;

            lock (typeof(Context))
                an = new AssemblyName(Unique.AssemblyName());

#if NET_CLASSIC
            if (Options.AllowSave)
            {
                if (string.IsNullOrEmpty(Options.FileName))
                {
                    Options.FileName = an.Name + (Options.SaveAsExe ? ".exe" : ".dll");
                }

                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave);
                MainModule   = MainAssembly.DefineDynamicModule(an.Name, Options.FileName);
            }
            else
            {
                MainAssembly = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
                MainModule   = MainAssembly.DefineDynamicModule(an.Name);
            }
#else
            MainAssembly = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
            MainModule   = MainAssembly.DefineDynamicModule(an.Name);
#endif

            ContextId = GlobalPropertyHelper.RegisterContext();

            MainType            = CreateType(EntityNames.MainTypeName, prepare: false);
            MainType.Kind       = TypeEntityKind.Main;
            MainType.Interfaces = new[] { typeof(IScript) };
            MainMethod          = MainType.CreateMethod(EntityNames.RunMethodName, typeof(object), Type.EmptyTypes, false, true, false);

            if (Options.LoadStandardLibrary)
            {
                InitStdlib();
            }

            InitSafeMode();
        }