示例#1
0
        static void SerializeNam(Variable v, StringBuilder sb,
                                 List <object> refs)
        {
            P6any o = v.Fetch();

            if (o is BoxObject <int> ) /* includes bool */
            {
                sb.Append(Kernel.UnboxAny <int>(o));
            }
            else if (o is BoxObject <double> )
            {
                sb.Append(Utils.N2S(Kernel.UnboxAny <double>(o)));
            }
            else if (o is BoxObject <string> )
            {
                string s = Kernel.UnboxAny <string>(o);
                sb.Append('"');
                foreach (char c in s)
                {
                    if (c >= ' ' && c <= '~' && c != '\\' && c != '"')
                    {
                        sb.Append(c);
                    }
                    else
                    {
                        sb.Append("\\u");
                        sb.AppendFormat("{0:X4}", (int)c);
                    }
                }
                sb.Append('"');
            }
            else if (!o.IsDefined())
            {
                sb.Append("null");
            }
            else if (o.Isa(Kernel.ListMO))
            {
                VarDeque d     = o.mo.mro_raw_iterator.Get(v);
                bool     comma = false;
                sb.Append('[');
                while (Kernel.IterHasFlat(d, true))
                {
                    if (comma)
                    {
                        sb.Append(',');
                    }
                    SerializeNam(d.Shift(), sb, refs);
                    comma = true;
                }
                sb.Append(']');
            }
            else if (o is BoxObject <object> )
            {
                sb.Append('!');
                sb.Append(refs.Count);
                refs.Add(Kernel.UnboxAny <object>(o));
            }
            else
            {
                throw new NieczaException("weird object in sub_finish " + o.mo.name);
            }
        }