public static string FormatString(Statement s, Dictionary <string, int> map)
        {
            string n = s.Name;

            if (s is Atomic)
            {
                Atomic a = s as Atomic;
                if (a.IsConstant)
                {
                    return(n);
                }
                if (!map.ContainsKey(n))
                {
                    map[n] = map.Count;
                }
                return("{" + map[n] + "}");
            }
            return(string.Format("({0} {1})", s.Name, string.Join(" ", s.Args.Select(a => FormatString(a, map)).ToArray())));
        }
Exemple #2
0
        //Attempts to parse an entire lisp string using the available types of statements. Returns null on failure.
        protected static Statement Parse(string lisp)
        {
            Statement s = null;

            lisp = lisp.Trim();
            int e = lisp.IndexOf(')');

            if (e >= 0) //There is a claim in parentheses to deal with (any non-atomic)
            {
                while (e >= 0)
                {
                    string part = lisp.Substring(0, e + 1);
                    int    st   = part.LastIndexOf('(');
                    if (st < 0)
                    {
                        return(null);
                    }
                    part = part.Substring(st);


                    //Try and parse the enclosed portion (into s), and return null on failure
                    if (!Negation.TryParse(part, out s) &&   //Check for negations
                        !Binary.TryParse(part, out s) &&     //Check for binary claims
                        !Function.TryParse(part, out s))     //Check for Functions
                    {
                        return(null);
                    }

                    //Add the new statement to the buffer and leave a reference to it in its place in the string
                    int x = buffer.Count;
                    buffer.Add(s);
                    lisp = string.Format("{0}[{1}]{2}", lisp.Substring(0, st), x, lisp.Substring(e + 1));
                    e    = lisp.IndexOf(')');
                }
                return(s);
            } //The claim is atomic or an integer (an index of an already parsed statement in the buffer)
            else if (FromBuffer(lisp, out s) || Atomic.TryParse(lisp, out s))
            {
                return(s);
            }

            return(null);
        }
        public static string RegexString(Statement s, Dictionary <string, int> map)
        {
            string n = s.Name;

            if (s is Atomic)
            {
                Atomic a = s as Atomic;
                if (a.IsConstant)
                {
                    return(n);
                }
                if (map.ContainsKey(n))
                {
                    return(string.Format(@"\{0}", map[n]));
                }
                map[n] = map.Count + 1;
                return(@"([A-Z]|\(.+\))");
            }
            return(string.Format(@"\({0} {1}\)", s.Name, string.Join(" ", s.Args.Select(a => RegexString(a, map)).ToArray())));
        }