Exemplo n.º 1
0
            /// <summary>
            /// Compiles the term formats for the given term.
            /// </summary>
            /// <param name="code"></param>
            /// <param name="terms"></param>
            private static void Compile(Case code, TermElement[] terms)
            {
                if (terms.Length == 1)
                {
                    // single term
                    Compile(code, terms.Single());
                }
                else
                {
                    // fallback by format
                    var dic = new Dictionary <TermFormat, TermElement>();
                    dic.Add(TermFormat.Long, terms.SingleOrDefault(x => x.Format == TermFormat.Long));
                    dic.Add(TermFormat.Short, terms.SingleOrDefault(x => x.Format == TermFormat.Short) ?? dic[TermFormat.Long]);
                    dic.Add(TermFormat.Verb, terms.SingleOrDefault(x => x.Format == TermFormat.Verb) ?? dic[TermFormat.Long]);
                    dic.Add(TermFormat.Symbol, terms.SingleOrDefault(x => x.Format == TermFormat.Symbol) ?? dic[TermFormat.Short]);
                    dic.Add(TermFormat.VerbShort, terms.SingleOrDefault(x => x.Format == TermFormat.VerbShort) ?? dic[TermFormat.Verb]);

                    // switch
                    using (var sw = code.AppendSwitch("format"))
                    {
                        // cases
                        foreach (var term in terms)
                        {
                            // find used
                            var uses = dic
                                       .Where(x => x.Value == term)
                                       .Select(x => x.Key)
                                       .ToArray();
                            if (uses.Length > 0)
                            {
                                using (var block = sw.AppendCases(uses.Cast <object>().ToArray()))
                                {
                                    Compile(block, term);
                                }
                            }
                            else
                            {
                                throw new NotSupportedException();
                            }
                        }

                        // default
                        using (var block = sw.AppendDefault())
                        {
                            block.AppendLine("throw new NotSupportedException();");
                        }
                    }
                }
            }