Building Parsers with Java

sjm.engine
Class ArithmeticOperator

java.lang.Object
  |
  +--sjm.engine.Structure
        |
        +--sjm.engine.ArithmeticOperator

public class ArithmeticOperator
extends Structure
implements ArithmeticTerm

An ArithmeticOperator represents an arithmetic operation that will perform itself as part of a proof.

An ArithmeticOperator has an operator and two terms. The operator must be '+', '-', '/', '*' or '%', or else the eval() value of this object will always be 0. The terms may be other arithmetic operators, variables, or number structures.

For example, an ArithmeticOperator might be appear in a comparison, as follows:

     >(+(X, 3), 42)
 
The arithmetic operator will have a valid value if X is instantiated to a NumberStructure object. If X is instantiated to, say, 40, then the arithmetic operator's reply to eval() will be 47, and the comparison will succeed.


Field Summary
protected  char operator
           
protected  ArithmeticTerm term0
           
protected  ArithmeticTerm term1
           
 
Fields inherited from class sjm.engine.Structure
emptyList, functor, terms
 
Constructor Summary
ArithmeticOperator(char operator, ArithmeticTerm term0, ArithmeticTerm term1)
          Constructs an arithmetic operator with the indicated operator and terms.
 
Method Summary
protected  java.lang.Object arithmeticValue(double d0, double d1)
           
 Term copyForProof(AxiomSource ignored, Scope scope)
          Create a copy using the supplied scope for variables.
 java.lang.Object eval()
          Returns the result of applying this object's operator against the arithmetic values of its two terms.
protected  double eval(ArithmeticTerm t)
           
 
Methods inherited from class sjm.engine.Structure
arity, canFindNextProof, equals, functorAndArityEquals, headAndTail, isList, list, list, list, listTailString, listTermsToString, terms, toString, unify, unify, unify, variables
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

operator

protected char operator

term0

protected ArithmeticTerm term0

term1

protected ArithmeticTerm term1
Constructor Detail

ArithmeticOperator

public ArithmeticOperator(char operator,
                          ArithmeticTerm term0,
                          ArithmeticTerm term1)
Constructs an arithmetic operator with the indicated operator and terms.

The operator must be '+', '-', '/', '*' or '%', or else the eval() value of this object will always be 0. The terms must be other arithmetic operators, variables, or number structures. If either term is invalid, this object will throw an EvaluationException during a proof.

Parameters:
char - the operator
ArithmeticTerm - the first term
ArithmeticTerm - the second term
Method Detail

arithmeticValue

protected java.lang.Object arithmeticValue(double d0,
                                           double d1)

copyForProof

public Term copyForProof(AxiomSource ignored,
                         Scope scope)
Create a copy using the supplied scope for variables.
Overrides:
copyForProof in class Structure
Parameters:
AxiomSource - ignored
Scope - the scope to use for variables
Returns:
a copy with variables from the supplied scope

eval

public java.lang.Object eval()
Returns the result of applying this object's operator against the arithmetic values of its two terms. For example,
 
     NumberStructure two = new NumberStructure(2);
     ArithmeticOperator x, y;
     x = new ArithmeticOperator('*', two, two);
     y = new ArithmeticOperator('+', x, two);
     System.out.println(y + " = " + y.eval());
 
prints out:
 
     +(*(2, 2), 2) = 6.0
 
Overrides:
eval in class Structure
Returns:
the result of applying this object's operator to the arithmetic value of its two terms
Throws:
EvaluationException - if either term is not a valid arithmetic value

eval

protected double eval(ArithmeticTerm t)

by Steve Metsker