de.dante.extex.interpreter.primitives.register.count
Class Numexpr

java.lang.Object
  extended byde.dante.extex.interpreter.type.AbstractCode
      extended byde.dante.extex.interpreter.primitives.register.count.Numexpr
All Implemented Interfaces:
Code, CountConvertible, Localizable, java.io.Serializable, Theable

public class Numexpr
extends AbstractCode
implements CountConvertible, Theable

This class provides an implementation for the primitive \numexpr.

The Primitive \numexpr

The primitive \numexpr provides a means to use a inline way of writing mathematical expressions to be evaluated. Mathematical expressions can be evaluated in ExTeX using \advance, \multiply, and \divide. Nevertheless those primitives result in an assignment. This is not the case for \numexpr. Here the intermediate results are not stored in count registers but kept internally. Also the application of \afterassignment and \tracingassigns is suppressed.

The mathematical expression to be evaluated can be made up of the basic operations addition (+), subtraction (-), multiplication (*), and division(/). The unary minus can be used. Parentheses can be used for grouping. Anything which looks like a number can be used as argument. White-space can be used freely without any harm.

The expression is terminated at the first token which can not be part of an expression. For instance a letter may signal the end of the expression. If the expression should terminate without a proper token following it, the token \relax can be used to signal the end of the expression. This \relax token is silently consumed by \numexpr.

The primitive \numexpr can be used in any place where a number is required. This includes assignments to count registers and comparisons.

Syntax

The formal description of this primitive is the following:
    ⟨numexpr⟩
      → \numexpr ⟨expr⟩ \relax
      |   \numexpr ⟨expr⟩

    ⟨expr⟩
      → ⟨number⟩
      |   ⟨operand⟩
      |   ⟨expr⟩ + ⟨expr⟩
      |   ⟨expr⟩ - ⟨expr⟩
      |   ⟨expr⟩ * ⟨expr⟩
      |   ⟨expr⟩ / ⟨expr⟩

    ⟨operand⟩
      → ⟨number⟩
      |   - ⟨expr⟩
      |   ( ⟨expr⟩ )   

Examples

   \count1=\numexpr 23 \relax 
   \count1=\numexpr 2 * 3 \relax 
   \count1=\numexpr 2*\count2  
   \count1=\numexpr 2*(1+3)  
   \count1=\numexpr 2*-\count0  

Version:
$Revision: 1.7 $
Author:
Gerd Neugebauer
See Also:
Serialized Form

Field Summary
protected static long serialVersionUID
          The constant serialVersionUID contains the id for serialization.
 
Constructor Summary
Numexpr(java.lang.String name)
          Creates a new object.
 
Method Summary
 long convertCount(Context context, TokenSource source, Typesetter typesetter)
          This method converts a register into a count.
 long evalOperand(Context context, TokenSource source, Typesetter typesetter)
          Evaluate an operand.
 Tokens the(Context context, TokenSource source, Typesetter typesetter)
          This method is the getter for the description of the primitive.
 
Methods inherited from class de.dante.extex.interpreter.type.AbstractCode
enableLocalization, execute, getLocalizer, getName, isIf, isOuter, printable, printableControlSequence, readResolve, setName, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

protected static final long serialVersionUID
The constant serialVersionUID contains the id for serialization.

See Also:
Constant Field Values
Constructor Detail

Numexpr

public Numexpr(java.lang.String name)
Creates a new object.

Parameters:
name - the name for debugging
Method Detail

convertCount

public long convertCount(Context context,
                         TokenSource source,
                         Typesetter typesetter)
                  throws InterpreterException
Description copied from interface: CountConvertible
This method converts a register into a count. It might be necessary to read further tokens to determine which value to use. For instance an additional register number might be required. In this case the additional arguments Context and TokenSource can be used.

Specified by:
convertCount in interface CountConvertible
Parameters:
context - the interpreter context
source - the source for new tokens
typesetter - the typesetter to use for conversion
Returns:
the converted value
Throws:
InterpreterException - in case of an error
See Also:
CountConvertible.convertCount( de.dante.extex.interpreter.context.Context, de.dante.extex.interpreter.TokenSource, de.dante.extex.typesetter.Typesetter)

evalOperand

public long evalOperand(Context context,
                        TokenSource source,
                        Typesetter typesetter)
                 throws InterpreterException
Evaluate an operand.

Parameters:
context - the interpreter context
source - the source for new tokens
typesetter - the typesetter
Returns:
the result
Throws:
InterpreterException - in case of an error

the

public Tokens the(Context context,
                  TokenSource source,
                  Typesetter typesetter)
           throws InterpreterException
Description copied from interface: Theable
This method is the getter for the description of the primitive.

Specified by:
the in interface Theable
Parameters:
context - the interpreter context
source - the source for further tokens to qualify the request
typesetter - the typesetter to use
Returns:
the description of the primitive as list of Tokens
Throws:
InterpreterException - in case of an error
See Also:
Theable.the( de.dante.extex.interpreter.context.Context, de.dante.extex.interpreter.TokenSource, de.dante.extex.typesetter.Typesetter)