示例#1
0
        /// <summary> Creates a new {@link se.sics.prologbeans.PBTerm} instance representing a
        /// list with the characters, as integer values, in the string argument as
        /// its elements.
        /// </summary>
        /// <param name="value">the non-null value to represent
        /// </param>
        /// <returns> a term representing the argument
        /// </returns>
        static public PBTerm makeTerm(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // [PM] 4.3.2
            {
                PBTerm tail = PBTerm.NIL;
                for (int i = value.Length - 1; i >= 0; i--)
                {
                    tail = PBTerm.makeTerm(PBTerm.makeTerm(value [i]), tail);
                }
                return(tail);
            }
        }
示例#2
0
 /// <summary> Adds the specified variable binding. The variable name must start
 /// with an upper case letter or '_'.
 /// </summary>
 /// <param name="name">a prolog variable name
 /// </param>
 /// <param name="value">the value to bind to the variable
 /// </param>
 /// <returns> a reference to this <c>Bindings</c> object
 /// </returns>
 /// <exception cref="System.ArgumentException"> if the name is not a
 /// valid prolog variable name
 /// </exception>
 public virtual Bindings bind(string name, float value)
 {
     return(bind(name, PBTerm.makeTerm(value)));
 }
示例#3
0
        /*
         * [PM] 4.1.3 Manage an explicit stack to avoid running out of Java stack
         * (SPRM 11909)
         */
        PBTerm parseTerm(System.IO.Stream stream)
        {
            Work outer = new Work(new PBTerm[1], null);
            Work stack = outer;

            do
            {
                int chr = stream.ReadByte();

                PBTerm term;

                if (FastParser.logging)
                {
                    Console.Out.WriteLine("parseTerm() switch on " + chr);
                }

                switch (chr)
                {
                case INTEGER:
                {
                    string val = getString(stream);
                    // return new PBInteger(val);
                    try {
                        term = new PBInteger(Convert.ToInt64(val, 10), val);
                    } catch (FormatException /* nfe */) {
                        // FIXME: Perhaps not catch FormatException? If malformed then it is no bignum either.
                        term = new PBBignum(BigInteger.Parse(val), val);
                    } catch (OverflowException) {
                        term = new PBBignum(BigInteger.Parse(val), val);
                    }
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() INTEGER " + val);
                    }
                    stack = stack.bump(term);
                }
                break;

                case FLOAT:
                {
                    string val = getString(stream);
                    term = new PBFloat(Double.Parse(val), val);
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() FLOAT " + val);
                    }
                    stack = stack.bump(term);
                }
                break;

                case ATOM:
                {
                    string val = getString(stream);
                    term = PBTerm.makeAtom(val);
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() ATOM " + val);
                    }
                    stack = stack.bump(term);
                }
                break;

                case VARIABLE:
                {
                    string val = '_' + getString(stream);
                    if (variableTable == null)
                    {
                        variableTable = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
                    }
                    PBVariable var = (PBVariable)variableTable [val];
                    if (var == null)
                    {
                        var = new PBVariable(val);
                        variableTable [val] = var;
                    }
                    term = var;
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() VARIABLE " + val);
                    }
                    stack = stack.bump(term);
                }
                break;

                case STRING:
                {
                    byte[] val = getBytes(stream);
                    PBTerm t   = parseTerm(stream);
                    // Note that this builds the list from the end.
                    for (int i = val.Length - 1; i >= 0; i--)
                    {
                        t = PBTerm.makeTerm(new PBInteger(val [i]), t);
                    }
                    term = t;
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() STRING " + val);
                    }
                    stack = stack.bump(term);
                }
                break;

                case LIST:
                {
                    const int noTerms = 2;
                    PBTerm[]  terms   = new PBTerm[noTerms];
                    term = new PBListCell(terms);
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() LIST ./2");
                    }
                    stack = stack.bump(term, terms);
                }
                break;

                case NIL:
                {
                    term = PBTerm.NIL;
                    if (logging)
                    {
                        Console.Out.WriteLine("bump() NIL");
                    }
                    stack = stack.bump(term);
                }
                break;

                case COMPOUND:
                {
                    string   val     = getString(stream);
                    int      noTerms = stream.ReadByte();
                    PBTerm[] terms   = new PBTerm[noTerms];

                    term = PBTerm.makeTerm(val, terms);

                    if (logging)
                    {
                        Console.Out.WriteLine("bump() COMPOUND " + val + "/" + noTerms);
                    }
                    stack = stack.bump(term, terms);
                }
                break;

                default:
                    throw new System.IO.IOException("Parse error: illegal character " + (char)chr);
                }
            } while (stack != null);
            // assert outer != null;
            // assert outer.args != null && outer.args.length == 1;
            // assert validTerm(outer.args[0]);
            if (logging)
            {
                Console.Out.WriteLine("parseTermWithStack returning " + outer.args [0]);
            }
            return(outer.args [0]);
        }