Example #1
0
            LNode GetOutputAsLNode()
            {
                WList <LNode> finalOutput = _handler.ToWList();

                for (int end = _output.Count - 1; end >= 0; end--)
                {
                    var   tmp_10 = _output[end];
                    Mode  mode   = tmp_10.Item1;
                    LNode code   = tmp_10.Item2;

                    if (mode == Mode.Condition)
                    {
                        // Merge adjacent conditions into the same if-statement
                        int start = end;
                        for (; start > 0 && _output[start - 1].A == mode; start--)
                        {
                        }
                        LNode cond = _output[start].B;
                        for (int i = start + 1; i <= end; i++)
                        {
                            cond = LNode.Call(CodeSymbols.And, LNode.List(cond, _output[i].B)).SetStyle(NodeStyle.Operator);
                        }
                        end = start;

                        finalOutput = new WList <LNode> {
                            LNode.Call(CodeSymbols.If, LNode.List(cond, finalOutput.ToLNodeList().AsLNode(S.Braces)))
                        };
                    }
                    else
                    {
                        finalOutput.Insert(0, code);
                    }
                }
                return(finalOutput.ToLNodeList().AsLNode(S.Braces));
            }
Example #2
0
            public AltType(LNodeList classAttrs, LNode typeName, LNodeList baseTypes, AltType parentType)
            {
                _classAttrs = classAttrs;
                TypeName    = typeName;
                BaseTypes   = baseTypes;
                ParentType  = parentType;
                //matchCode (TypeName) {
                //	case $stem<$(..a)>, $stem:
                //		_typeNameStem = stem;
                //		_genericArgs = a;
                //  default:
                //		_genericArgs = new WList<LNode>();
                //}
                {                       // Above matchCode expanded:
                    LNode     stem;
                    LNodeList a = default(LNodeList);
                    if (TypeName.CallsMin(CodeSymbols.Of, 1) && (stem = TypeName.Args[0]) != null && (a = new LNodeList(TypeName.Args.Slice(1))).IsEmpty | true || (stem = TypeName) != null)
                    {
                        _typeNameStem = stem;
                        _genericArgs  = a.ToWList();
                    }
                    else
                    {
                        _genericArgs = new WList <LNode>();
                    }
                }
                if (ParentType != null)
                {
                    BaseTypes.Insert(0, ParentType.TypeNameWithoutAttrs);

                    // Search for all 'where' clauses on the ParentType and make sure OUR generic args have them too.
                    bool changed = false;
                    for (int i = 0; i < _genericArgs.Count; i++)
                    {
                        var arg       = _genericArgs[i];
                        var parentArg = ParentType._genericArgs.FirstOrDefault(a => a.IsIdNamed(arg.Name));
                        if (parentArg != null)
                        {
                            var wheres       = new HashSet <LNode>(WhereTypes(arg));
                            int oldCount     = wheres.Count;
                            var parentWheres = WhereTypes(parentArg);
                            foreach (var where in parentWheres)
                            {
                                wheres.Add(where);
                            }
                            if (wheres.Count > oldCount)
                            {
                                arg = arg.WithAttrs(arg.Attrs.SmartWhere(a => !a.Calls(S.WhereClause))
                                                    .Add(LNode.Call(S.WhereClause, LNode.List(wheres))));
                                _genericArgs[i] = arg;
                                changed         = true;
                            }
                        }
                    }
                    if (changed)
                    {
                        TypeName = LNode.Call(CodeSymbols.Of, LNode.List().Add(_typeNameStem).AddRange(_genericArgs)).SetStyle(NodeStyle.Operator);
                    }
                }
                TypeNameWithoutAttrs = TypeName.Select(n => n.WithoutAttrs());
            }