Exemplo n.º 1
0
        public static FunctionBody TupleBodyDefinition(RootNamespace root, TupleNode tuple)
        {
            var name   = GetName(tuple);
            var exists = root.Structs.FindFirstOrNull(x => x.Name == name);

            if (exists is null)
            {
                _ = TupleDefinition(root, tuple.Values.Count);
            }

            var fbody = MakeFunction(root, $"{name}#{root.TupleUniqueCount++}");
            var fret  = new TypeSpecialization(new VariableValue(name));
            var fcall = new TypeSpecialization(new VariableValue(name));
            var self  = new VariableValue("$self");

            fbody.LexicalScope.Add(self.Name, self);
            fbody.Body.Add(new Call(new FunctionCallValue(fcall))
            {
                Return = self
            });

            tuple.Values.Each((x, i) =>
            {
                var gp       = new TypeGenericsParameter($"t{i + 1}");
                var farg_var = new VariableValue($"x{i + 1}");
                fbody.Generics.Add(gp);
                fbody.Arguments.Add((farg_var, gp));
                fbody.LexicalScope.Add(farg_var.Name, farg_var);
                fbody.Body.Add(new Code {
                    Operator = Operator.Bind, Return = new PropertyValue(self, $"{i + 1}"), Left = NormalizationExpression(fbody, x, true)
                });
                fret.Generics.Add(gp);
                fcall.Generics.Add(gp);
            });
            fbody.Body.Add(new Call(new FunctionCallValue(new VariableValue("return")).Return(x => x.Arguments.Add(self))));
            fbody.Return = fret;
            return(fbody);
        }
Exemplo n.º 2
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);
        }