/// <summary> [PM] 4.1.3 Traverse the term and verify that no null arguments remain /// (for assertions). /// </summary> static bool validTerm(PBTerm pbTerm) { if (pbTerm == null) { return(false); } // FIXME: Traverse all args[] arrays (without running out of Java // stack!) return(true); }
/* * bump the term onto the stack and then push terms on the resulting * stack. Never null. */ public Work bump(PBTerm term, PBTerm[] terms) { // Note that bump(term) may return null. This is why we do not use // something like bump(term).push(terms). Work tmp = new Work(terms, bump(term)); if (FastParser.logging) { Console.Out.WriteLine("bump() returning new: " + tmp); } return(tmp); }
internal virtual void fastWrite(FastWriter writer) { foreach (string key in bindings.Keys) { PBTerm value = (PBTerm)bindings [key]; writer.writeList(); writer.writeCompound("=", 2); // Arg 1 writer.writeAtom(key); // Arg 2 writer.writeTerm(value); } writer.writeNIL(); }
public override string ToString() { System.Text.StringBuilder sb = new System.Text.StringBuilder().Append('['); sb.Append(arguments [0].ToString()); PBTerm t = arguments [1]; while (!t.EmptyList) { sb.Append(','); sb.Append(t.head().ToString()); t = t.tail(); } sb.Append(']'); return(sb.ToString()); }
/// <summary> For debugging. /// </summary> public override string ToString() { System.Text.StringBuilder buffer = new System.Text.StringBuilder(); buffer.Append('['); string prefix = ""; foreach (string key in bindings.Keys) { buffer.Append(prefix); prefix = ","; PBTerm value = (PBTerm)bindings [key]; buffer.Append(key).Append('=').Append(value.toPrologString()); } buffer.Append(']'); return(buffer.ToString()); }
// [PD] 4.0.0 Non-recursive version System.Text.StringBuilder getString(System.Text.StringBuilder sbuf) { PBListCell pbt = this; PBTerm h; do { h = pbt.arguments [0]; if (h.Atom && h.Name.Length == 1) { // Is head a one char // atom? sbuf.Append(h.Name); } else if (h.Integer) { // Is head an integer? // *** FIXME: Perhaps check if this cast truncates. int c = (int)h.intValue(); if (!isUnicode(c)) { throw new InvalidOperationException("not a list of characters"); } appendCodePoint(sbuf, c); } else { throw new InvalidOperationException("not a list of characters"); } PBTerm t = pbt.arguments [1]; // tail() if (t == PBTerm.NIL) { return(sbuf); } else { if (t.ListCell) { pbt = (PBListCell)t; } else { throw new InvalidOperationException("not a list of characters"); } } } while (true); }
/// <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); } }
/// <summary> Update top of stack. Return the resulting stack, i.e. either 'this' /// or this.next. /// </summary> internal Work bump(PBTerm term) { // assert i < args.length; // assert term != null; args [i] = term; i++; if (i < args.Length) { if (FastParser.logging) { Console.Out.WriteLine("bump() returning this: " + this); } return(this); } if (FastParser.logging) { Console.Out.WriteLine("bump() returning next: " + next); } return(next); }
internal virtual void writeTerm(PBTerm term) { WriteWork stack = new WriteWork(new [] { term }, null); do { PBTerm t = stack.bump(); if (t != null) { PBTerm[] args = t.fastWritePrefix(this); if (args.Length > 0) { stack = stack.push(args); } } else { // top-most entry exhausted, pop it. stack = stack.prune(); } } while (stack != null); }
/// <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))); }
/// <summary> Adds the specified variable binding. The variable name must start /// with an upper case letter or '_'. The value will be bound as an /// atom. /// </summary> /// <param name="name">a prolog variable name /// </param> /// <param name="value">the value to bind to the variable as an atom /// </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 bindAtom(string name, string value) { return(bind(name, PBTerm.makeAtom(value))); }
/// <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, PBTerm value) { checkVar(name); bindings [name] = value; return(this); }
/* * [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]); }
/// <summary> Creates a new {@link se.sics.prologbeans.PBTerm} instance representing a /// list cell. /// </summary> /// <param name="head">the first argument of the list cell /// </param> /// <param name="tail">the second argument of the list cell /// </param> /// <returns> a term representing the argument /// </returns> static public PBTerm makeTerm(PBTerm head, PBTerm tail) { return(new PBListCell(head, tail)); }
internal PBListCell(PBTerm head, PBTerm tail) : this(new [] { head, tail }) { }