Exemple #1
0
        public static StructBody TypeBodyDefinition(SourceCodeBody src, StructNode sn)
        {
            var body = new StructBody(src, sn.Name.Name);

            sn.Generics.Each(x => body.Generics.Add(new TypeGenericsParameter(x.Name)));
            FunctionBodyDefinition(body, sn.Statements);
            sn.Statements.Each(let =>
            {
                switch (let)
                {
                case LetNode x:
                    body.Members.Add(x.Var.Name, body.LexicalScope[x.Var.Name]);
                    break;

                case LetTypeNode x:
                    body.Members.Add(x.Var.Name, body.LexicalScope[x.Var.Name]);
                    break;

                default:
                    throw new Exception();
                }
            });
            if (sn.Generics.Count == 0)
            {
                body.SpecializationMapper[new GenericsMapper()] = new TypeMapper();
                src.Functions.Add(new EmbeddedFunction(sn.Name.Name, sn.Name.Name)
                {
                    OpCode = (args) => $"newobj instance void {sn.Name.Name}::.ctor()"
                });
            }
            return(body);
        }
 public ReadOnlyStructPropertyCollection(StructBody body)
 {
     properties                  = body.Properties.ToArray(property => new ReadOnlyStructProperty(property));
     autoProperties              = body.AutoProperties.ToArray(property => new ReadOnlyStructAutoProperty(property));
     lambdaProperties            = body.LambdaProperties.ToArray(property => new ReadOnlyStructLambdaProperty(property));
     explicitInterfaceProperties = body.ExplicitInterfaceProperties.ToArray(property => new ReadOnlyExplicitInterfaceProperty(property));
 }
Exemple #3
0
 public ClassNestedStruct(string name)
 {
     Name = name;
     Attributes = new Collection<AttributeGroup>();
     Body = new StructBody();
     GenericParameters = new Collection<GenericParameter>();
     Interfaces = new Collection<InterfaceReference>();
 }
        public ReadOnlyStructEventCollection(StructBody structBody)
        {
            List <ReadOnlyStructEvent> events          = new List <ReadOnlyStructEvent>();
            List <ReadOnlyStructEvent> eventProperties = new List <ReadOnlyStructEvent>();

            this.events          = events;
            this.eventProperties = eventProperties;
            InitializeEvents(structBody, events, eventProperties);
            explicitInterfaceEvents = structBody.ExplicitInterfaceEvents.ToArray(@event => new ReadOnlyExplicitInterfaceEvent(@event));
        }
Exemple #5
0
        public static bool StructBodyInference(StructBody body)
        {
            var resolved = false;
            var keys     = body.SpecializationMapper.Keys.ToArray();

            for (var i = 0; i < keys.Length; i++)
            {
                var value = body.SpecializationMapper[keys[i]];
                body.Body.Each(x => resolved = OperandTypeInference(body.Namespace, value, x) || resolved);
            }
            return(resolved);
        }
Exemple #6
0
        public static void AssemblyStructEmit(ILWriter il, StructBody body, List <FunctionSpecialization> fss)
        {
            var cache = new HashSet <string>();

            body.SpecializationMapper.Each(sp =>
            {
                var g      = sp.Key;
                var mapper = sp.Value;

                var name = GetStructName(body.Name, body, g);
                if (cache.Contains(name))
                {
                    return;
                }
                cache.Add(name);

                il.WriteLine($".class public {name}");
                il.WriteLine("{");
                il.Indent++;
                body.Members.Each(x => il.WriteLine($".field public {GetTypeName(mapper, x.Value, g)} {EscapeILName(x.Key)}"));
                il.WriteLine("");

                il.WriteLine($".method public void .ctor()");
                il.WriteLine("{");
                il.Indent++;
                var local_vals = mapper.Values.Where(x => x.Type == VariableType.LocalVariable && !(x.Struct is NamespaceBody)).Sort((a, b) => a.Index - b.Index).ToList();
                local_vals.Each((x, i) => x.Index = i);
                if (local_vals.Count > 0)
                {
                    il.WriteLine(".locals(");
                    il.Indent++;
                    il.WriteLine(local_vals.Map(x => $"[{x.Index}] {GetTypeName(x, g)} {x.Name}").Join(",\n"));
                    il.Indent--;
                    il.WriteLine(")");
                }
                var labels = Lookup.AllLabels(body.Body).Zip(Lists.Sequence(1)).ToDictionary(x => x.First, x => $"_{x.First.Name}{x.Second}");
                body.Body.Each(x =>
                {
                    if (x is Call call)
                    {
                        CallToAddEmitFunctionList(mapper, call, fss);
                    }
                    AssemblyOperandEmit(il, x, body.Namespace, mapper, labels, g);
                });
                il.WriteLine("ret");
                il.Indent--;
                il.WriteLine("}");

                il.Indent--;
                il.WriteLine("}");
            });
        }
 private void InitializeEvents(StructBody structBody, List <ReadOnlyStructEvent> events, List <ReadOnlyStructEvent> eventProperties)
 {
     foreach (StructEvent structEvent in structBody.Events)
     {
         ReadOnlyStructEvent @event = new ReadOnlyStructEvent(structEvent);
         if (structEvent.IsEventProperty)
         {
             eventProperties.Add(@event);
         }
         else
         {
             events.Add(@event);
         }
     }
 }
Exemple #8
0
 public ReadOnlyStructBody(StructBody structBody)
 {
     Classes             = new ReadOnlyStructNestedClassCollection(structBody);
     Constructors        = new ConstructorNotSupported[0];
     ConversionOperators = structBody.ConversionOperators
                           .ToArray(conversionOperator => new ReadOnlyConversionOperator(conversionOperator));
     Delegates         = structBody.NestedDelegates.ToArray(nestedDelegate => new ReadOnlyStructNestedDelegate(nestedDelegate));
     Enums             = structBody.NestedEnums.ToArray(nestedEnum => new ReadOnlyStructNestedEnum(nestedEnum));
     Events            = new ReadOnlyStructEventCollection(structBody);
     Fields            = new ReadOnlyStructFieldCollection(structBody);
     Indexers          = new ReadOnlyStructIndexerCollection(structBody);
     Interfaces        = structBody.NestedInterfaces.ToArray(@interface => new ReadOnlyStructNestedInterface(@interface));
     Methods           = new ReadOnlyStructMethodCollection(structBody);
     OperatorOverloads =
         structBody.BinaryOperators.Select(@operator => new ReadOnlyOperatorOverload(@operator))
         .Concat(structBody.UnaryOperators.Select(@operator => new ReadOnlyOperatorOverload(@operator)))
         .ToArray();
     Properties = new ReadOnlyStructPropertyCollection(structBody);
     Structs    = new ReadOnlyStructNestedStructCollection(structBody);
 }
Exemple #9
0
        public static StructBody TupleDefinition(RootNamespace root, int count)
        {
            var name   = GetTupleName(count);
            var exists = root.Structs.FindFirstOrNull(x => x.Name == name);

            if (exists is StructBody sb)
            {
                return(sb);
            }

            var body = new StructBody(root, name);

            Lists.RangeTo(1, count).Each(i =>
            {
                var gp = new TypeGenericsParameter($"a{i}");
                body.Generics.Add(gp);
                var member = new VariableValue($"{i}");
                body.LexicalScope.Add(member.Name, member);
                body.Body.Add(new TypeBind(member, gp));
                body.Members.Add(member.Name, member);
            });
            root.Structs.Add(body);
            return(body);
        }
Exemple #10
0
        public static bool TypeInference(StructBody body)
        {
            var resolved = StructBodyInference(body);

            SpecializationNumericDecide(body);
            _ = StructBodyInference(body);

            VariableValue self;

            if (body.LexicalScope.ContainsKey("$self"))
            {
                self = body.LexicalScope["$self"].Cast <VariableValue>();
            }
            else
            {
                self = new VariableValue("$self");
                body.LexicalScope.Add(self.Name, self);
            }

            foreach (var mapper in body.SpecializationMapper.Values)
            {
                if (!mapper.ContainsKey(self))
                {
                    mapper[self] = CreateVariableDetail(self.Name, new StructSpecialization(body, Lookup.TypeMapperToGenericsMapper(mapper)), VariableType.Argument, 0);
                }

                body.Members.Values.Each(x =>
                {
                    var d      = mapper[x];
                    d.Type     = VariableType.Property;
                    d.Reciever = self;
                    d.Index    = 0;
                });
            }
            return(resolved);
        }
Exemple #11
0
 public ReadOnlyStructIndexerCollection(StructBody body)
 {
     indexers = body.Indexers.ToArray(indexer => new ReadOnlyStructIndexer(indexer));
     explicitInterfaceIndexers = body.ExplicitInterfaceIndexers.ToArray(indexer => new ReadOnlyExplicitInterfaceIndexer(indexer));
 }
Exemple #12
0
 public ReadOnlyStructFieldCollection(StructBody body)
 {
     fields = body.Fields.ToArray(field => new ReadOnlyStructField(field));
 }
 public ReadOnlyStructNestedClassCollection(StructBody structBody)
 {
     classes = structBody.NestedClasses.ToArray(@class => new ReadOnlyStructNestedClass(@class));
 }
 public ReadOnlyStructMethodCollection(StructBody body)
 {
     methods = body.Methods.ToArray(method => new ReadOnlyStructMethod(method));
     explicitInterfaceMethods = body.ExplicitInterfaceMethods.ToArray(method => new ReadOnlyExplicitInterfaceMethod(method));
 }
Exemple #15
0
 public ReadOnlyStructNestedStructCollection(StructBody structBody)
 {
     structs = structBody.NestedStructs.ToArray(@struct => new ReadOnlyStructNestedStruct(@struct));
 }