示例#1
0
 static LNode MaybeQuoteList(RVList <LNode> list, bool substitutions)
 {
     if (list.IsEmpty)
     {
         return(null);
     }
     else if (substitutions && list.Any(a => VarArgExpr(a) != null))
     {
         if (list.Count == 1)
         {
             return(F.Call(S.New, F.Call(F.Of(F.Id("RVList"), F.Id("LNode")), VarArgExpr(list[0]))));
         }
         // If you write something like quote(Foo($x, $(..y), $z)), a special
         // output style is used to accommodate the variable argument list.
         LNode argList = F.Call(S.New, F.Call(F.Of(F.Id("RVList"), F.Id("LNode"))));
         foreach (LNode arg in list)
         {
             var vae = VarArgExpr(arg);
             if (vae != null)
             {
                 argList = F.Call(F.Dot(argList, F.Id("AddRange")), vae);
             }
             else
             {
                 argList = F.Call(F.Dot(argList, F.Id("Add")), QuoteOne(arg, substitutions));
             }
         }
         return(argList);
     }
     else
     {
         return(F.Call(LNode_List, list.Select(item => QuoteOne(item, substitutions))));
     }
 }
            public void GenerateOutput(ref RVList <LNode> list)
            {
                bool isAbstract = _typeAttrs.Any(a => a.IsIdNamed(S.Abstract));
                var  baseParts  = new List <AdtParam>();

                for (var type = ParentType; type != null; type = type.ParentType)
                {
                    baseParts.InsertRange(0, type.Parts);
                }
                var allParts       = baseParts.Concat(Parts);
                var initialization = Parts.Select(p => LNode.Call(CodeSymbols.Assign, LNode.List(LNode.Call(CodeSymbols.Dot, LNode.List(LNode.Id(CodeSymbols.This), p.NameId)), p.NameId)).SetStyle(NodeStyle.Operator)).ToList();

                if (baseParts.Count > 0)
                {
                    initialization.Insert(0, F.Call(S.Base, baseParts.Select(p => p.NameId)));
                }
                var args = new RVList <LNode>(allParts.Select(p => p.OriginalDecl));

                if (!_constructorAttrs.Any(a => a.IsIdNamed(S.Public)))
                {
                    _constructorAttrs.Add(F.Id(S.Public));
                }
                LNode constructor = LNode.Call(new RVList <LNode>(_constructorAttrs), CodeSymbols.Cons, LNode.List(LNode.Missing, _typeNameStem, LNode.Call(CodeSymbols.AltList, new RVList <LNode>(args)), LNode.Call(CodeSymbols.Braces, new RVList <LNode>().AddRange(initialization).AddRange(_extraConstrLogic)).SetStyle(NodeStyle.Statement)));
                var   outBody     = new RVList <LNode>();

                outBody.Add(constructor);
                outBody.AddRange(Parts.Select(p => p.GetFieldDecl()));
                outBody.AddRange(baseParts.Select(p => GetWithFn(p, isAbstract, S.Override, allParts)));
                outBody.AddRange(Parts.Select(p => GetWithFn(p, isAbstract, _children.Count > 0 ? S.Virtual : null, allParts)));
                outBody.AddRange(Parts.WithIndexes().Where(kvp => kvp.Value.NameId.Name.Name != "Item" + (baseParts.Count + kvp.Key + 1)).Select(kvp => kvp.Value.GetItemDecl(baseParts.Count + kvp.Key + 1)));
                outBody.AddRange(_classBody);
                list.Add(LNode.Call(new RVList <LNode>(_typeAttrs), CodeSymbols.Class, LNode.List(TypeName, LNode.Call(CodeSymbols.AltList, new RVList <LNode>(BaseTypes)), LNode.Call(CodeSymbols.Braces, new RVList <LNode>(outBody)).SetStyle(NodeStyle.Statement))));
                if (_genericArgs.Count > 0 && Parts.Count > 0)
                {
                    var argNames = allParts.Select(p => p.NameId);
                    list.Add(LNode.Call(new RVList <LNode>().AddRange(_typeAttrs).Add(LNode.Id(CodeSymbols.Static)).Add(LNode.Id(LNode.List(LNode.Id(CodeSymbols.TriviaWordAttribute)), CodeSymbols.Partial)), CodeSymbols.Class, LNode.List(_typeNameStem, LNode.Call(CodeSymbols.AltList), LNode.Call(CodeSymbols.Braces, LNode.List(LNode.Call(LNode.List(LNode.Id(CodeSymbols.Public), LNode.Id(CodeSymbols.Static)), CodeSymbols.Fn, LNode.List(TypeName, LNode.Call(CodeSymbols.Of, new RVList <LNode>().Add(LNode.Id((Symbol)"New")).AddRange(_genericArgs)), LNode.Call(CodeSymbols.AltList, new RVList <LNode>(args)), LNode.Call(CodeSymbols.Braces, LNode.List(LNode.Call(CodeSymbols.Return, LNode.List(LNode.Call(CodeSymbols.New, LNode.List(LNode.Call(TypeName, new RVList <LNode>(argNames)))))))).SetStyle(NodeStyle.Statement))))).SetStyle(NodeStyle.Statement))));
                }
                foreach (var child in _children)
                {
                    child.GenerateOutput(ref list);
                }
            }