Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Member Functions | Package Functions | Static Package Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
org.javia.arity.Symbols Class Reference

The collection of names (symbols) used for parsing an expression (the context in which the parsing takes place). More...

Collaboration diagram for org.javia.arity.Symbols:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Symbols ()
 Constructs a Symbols containing the built-in symbols (such as sin, log).
synchronized double eval (String expression) throws SyntaxException
 Evaluates a simple expression (such as "1+1") and returns its value.
synchronized FunctionAndName compile (String source) throws SyntaxException
 Compiles an expression in the context of this Symbols.
synchronized void define (String name, Function function)
 Adds a new function symbol to the top-most frame of this Symbols.
synchronized void define (FunctionAndName funAndName)
 Adds a new function symbol to the top-most frame of this Symbols.
synchronized void addConstant (String name, double value)
 Adds a new constant symbol to the top-most frame of this Symbols.
synchronized void pushFrame ()
 Pushes a new top frame.
synchronized void popFrame ()
 Pops the top frame.
 Symbols ()
 Constructs a Symbols containing the built-in symbols (such as sin, log).
synchronized double eval (String expression) throws SyntaxException
 Evaluates a simple expression (such as "1+1") and returns its value.
synchronized FunctionAndName compile (String source) throws SyntaxException
 Compiles an expression in the context of this Symbols.
synchronized void define (String name, Function function)
 Adds a new function symbol to the top-most frame of this Symbols.
synchronized void define (FunctionAndName funAndName)
 Adds a new function symbol to the top-most frame of this Symbols.
synchronized void addConstant (String name, double value)
 Adds a new constant symbol to the top-most frame of this Symbols.
synchronized void pushFrame ()
 Pushes a new top frame.
synchronized void popFrame ()
 Pops the top frame.

Static Public Member Functions

static boolean isDefinition (String source)
static boolean isDefinition (String source)

Package Functions

void addArguments (String args[])
void add (Symbol s)
synchronized Symbol lookup (String name, int arity)
void addArguments (String args[])
void add (Symbol s)
synchronized Symbol lookup (String name, int arity)

Static Package Functions

 [static initializer]
 [static initializer]

Static Private Member Functions

static Compiler getCompiler ()
static Compiler getCompiler ()

Private Attributes

Hashtable symbols = new Hashtable()
Vector delta = null
Stack frames = new Stack()

Static Private Attributes

static final Symbol builtin []
static Symbol shell = new Symbol(null, 0)
static ThreadLocal compilers = new ThreadLocal()
static final String defines []

Detailed Description

The collection of names (symbols) used for parsing an expression (the context in which the parsing takes place).

Each symbol maps to either a Function or a constant.

A symbol is identified by the pair (name, arity). So a constant and a function with the same name, or two function with the same name but with different arity are distinct symbols.

Symbols functions as a stack of frames: when you pop the topmost frame, all the symbols added in that frame dissapear (i.e. all the modifications done between the push and the pop are lost).

Example:

   
   Symbols symbols = new Symbols();
   symbols.eval("1+1"); //doesn't update symbols
   symbols.define(symbols.compile("f(x)=x^2")); //updates symbols
   
   

Thread safety

The Symbols class is thread-safe (the same Symbols instance can be used by multiple threads without locking).

The compile() methods are synchronized, so parallel compile() calls on the same Symbols instance are serialized.

Definition at line 56 of file Symbols.java.


Constructor & Destructor Documentation

Constructs a Symbols containing the built-in symbols (such as sin, log).

Definition at line 60 of file Symbols.java.

                     {
        for (int i = 0; i < builtin.length; ++i) {
            Symbol s = builtin[i];
            symbols.put(s, s);
        }
        try {
            for (int i = 0; i < defines.length; ++i) {
                define(compile(defines[i]));
            }
        } catch (SyntaxException e) {
            throw new Error(""+e); //never
        }
    }

Here is the call graph for this function:

Constructs a Symbols containing the built-in symbols (such as sin, log).

Definition at line 60 of file Symbols.java.

                     {
        for (int i = 0; i < builtin.length; ++i) {
            Symbol s = builtin[i];
            symbols.put(s, s);
        }
        try {
            for (int i = 0; i < defines.length; ++i) {
                define(compile(defines[i]));
            }
        } catch (SyntaxException e) {
            throw new Error(""+e); //never
        }
    }

Here is the call graph for this function:


Member Function Documentation

org.javia.arity.Symbols.[static initializer] ( ) [inline, static, package]
org.javia.arity.Symbols.[static initializer] ( ) [inline, static, package]
void org.javia.arity.Symbols.add ( Symbol  s) [inline, package]

Definition at line 253 of file Symbols.java.

                       {
        Object previous = symbols.put(s, s);
        if (delta == null) {
            delta = new Vector();
        }
        delta.addElement(previous != null ? previous : Symbol.newEmpty(s));
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void org.javia.arity.Symbols.add ( Symbol  s) [inline, package]

Definition at line 253 of file Symbols.java.

                       {
        Object previous = symbols.put(s, s);
        if (delta == null) {
            delta = new Vector();
        }
        delta.addElement(previous != null ? previous : Symbol.newEmpty(s));
    }

Here is the call graph for this function:

void org.javia.arity.Symbols.addArguments ( String  args[]) [inline, package]

Definition at line 247 of file Symbols.java.

                                     {
        for (int i = 0; i < args.length; ++i) {
            add(new Symbol(args[i], Symbol.CONST_ARITY, (byte)(VM.LOAD0 + i)));
        }
    }

Here is the call graph for this function:

void org.javia.arity.Symbols.addArguments ( String  args[]) [inline, package]

Definition at line 247 of file Symbols.java.

                                     {
        for (int i = 0; i < args.length; ++i) {
            add(new Symbol(args[i], Symbol.CONST_ARITY, (byte)(VM.LOAD0 + i)));
        }
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.addConstant ( String  name,
double  value 
) [inline]

Adds a new constant symbol to the top-most frame of this Symbols.

Parameters:
namethe name of the constant (e.g. "pi")
valuethe value of the constant

Definition at line 156 of file Symbols.java.

                                                                    {
        add(new Symbol(name, value));
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.addConstant ( String  name,
double  value 
) [inline]

Adds a new constant symbol to the top-most frame of this Symbols.

Parameters:
namethe name of the constant (e.g. "pi")
valuethe value of the constant

Definition at line 156 of file Symbols.java.

                                                                    {
        add(new Symbol(name, value));
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized FunctionAndName org.javia.arity.Symbols.compile ( String  source) throws SyntaxException [inline]

Compiles an expression in the context of this Symbols.

Does not modify the symbols.

An expression is one of these cases (

See also:
Symbols.isDefinition()):
  • constant value: 1+1
  • implicit function: x+1
  • constant definition: a=1+1
  • function definition with explicit arguments: f(a)=a+1
  • function definition with implicit arguments: f=x+1
Parameters:
sourcethe expression; may contain '=' to denote a definition (with a name).
Returns:
the function together with its eventual name.

If this is not a definition (e.g. "1+1", "x^2"), the name is null.

If the expression is a constant (e.g. "1+1", "a=2"), the returned Function is an instance of Constant.

Exceptions:
SyntaxExceptionif there are errors compiling the expression.

Definition at line 123 of file Symbols.java.

                                                                                      {
        return getCompiler().compile(this, source);
    }

Here is the call graph for this function:

synchronized FunctionAndName org.javia.arity.Symbols.compile ( String  source) throws SyntaxException [inline]

Compiles an expression in the context of this Symbols.

Does not modify the symbols.

An expression is one of these cases (

See also:
Symbols.isDefinition()):
  • constant value: 1+1
  • implicit function: x+1
  • constant definition: a=1+1
  • function definition with explicit arguments: f(a)=a+1
  • function definition with implicit arguments: f=x+1
Parameters:
sourcethe expression; may contain '=' to denote a definition (with a name).
Returns:
the function together with its eventual name.

If this is not a definition (e.g. "1+1", "x^2"), the name is null.

If the expression is a constant (e.g. "1+1", "a=2"), the returned Function is an instance of Constant.

Exceptions:
SyntaxExceptionif there are errors compiling the expression.

Definition at line 123 of file Symbols.java.

                                                                                      {
        return getCompiler().compile(this, source);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized void org.javia.arity.Symbols.define ( String  name,
Function  function 
) [inline]

Adds a new function symbol to the top-most frame of this Symbols.

Parameters:
namethe name of the function (e.g. "sin")
functionthe function to which the name maps

Definition at line 132 of file Symbols.java.

                                                                    {
        if (function instanceof Constant) {            
            addConstant(name, ((Constant) function).eval());
        } else {
            add(new Symbol(name, function));
        }
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.define ( String  name,
Function  function 
) [inline]

Adds a new function symbol to the top-most frame of this Symbols.

Parameters:
namethe name of the function (e.g. "sin")
functionthe function to which the name maps

Definition at line 132 of file Symbols.java.

                                                                    {
        if (function instanceof Constant) {            
            addConstant(name, ((Constant) function).eval());
        } else {
            add(new Symbol(name, function));
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized void org.javia.arity.Symbols.define ( FunctionAndName  funAndName) [inline]

Adds a new function symbol to the top-most frame of this Symbols.

Parameters:
funAndNamestructure containing the function and its name

Definition at line 144 of file Symbols.java.

                                                                {
        if (funAndName.name != null) {
            define(funAndName.name, funAndName.function);
        }
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.define ( FunctionAndName  funAndName) [inline]

Adds a new function symbol to the top-most frame of this Symbols.

Parameters:
funAndNamestructure containing the function and its name

Definition at line 144 of file Symbols.java.

                                                                {
        if (funAndName.name != null) {
            define(funAndName.name, funAndName.function);
        }
    }

Here is the call graph for this function:

synchronized double org.javia.arity.Symbols.eval ( String  expression) throws SyntaxException [inline]

Evaluates a simple expression (such as "1+1") and returns its value.

Exceptions:
SyntaxExceptionin these cases:
  • the expression is not well-formed
  • the expression is a definition (such as "a=1+1")
  • the expression is an implicit function (such as "x+1")

Definition at line 93 of file Symbols.java.

                                                                              {
        return getCompiler().eval(this, expression);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized double org.javia.arity.Symbols.eval ( String  expression) throws SyntaxException [inline]

Evaluates a simple expression (such as "1+1") and returns its value.

Exceptions:
SyntaxExceptionin these cases:
  • the expression is not well-formed
  • the expression is a definition (such as "a=1+1")
  • the expression is an implicit function (such as "x+1")

Definition at line 93 of file Symbols.java.

                                                                              {
        return getCompiler().eval(this, expression);
    }

Here is the call graph for this function:

static Compiler org.javia.arity.Symbols.getCompiler ( ) [inline, static, private]

Definition at line 199 of file Symbols.java.

                                          {
        Compiler compiler;
        if ((compiler = (Compiler) compilers.get()) == null) {
            compilers.set(compiler = new Compiler());
        }
        return compiler;
    }

Here is the caller graph for this function:

static Compiler org.javia.arity.Symbols.getCompiler ( ) [inline, static, private]

Definition at line 199 of file Symbols.java.

                                          {
        Compiler compiler;
        if ((compiler = (Compiler) compilers.get()) == null) {
            compilers.set(compiler = new Compiler());
        }
        return compiler;
    }
static boolean org.javia.arity.Symbols.isDefinition ( String  source) [inline, static]
Parameters:
sourcethe expression
Returns:
true if the expression is a definition (i.e. contains a '=').

These are definitions: "a=1+1"; "f(k)=2^k"

These are not definitions: "1+1"; "x+1"

Definition at line 80 of file Symbols.java.

                                                      {
        return source.indexOf('=') != -1;
    }

Here is the caller graph for this function:

static boolean org.javia.arity.Symbols.isDefinition ( String  source) [inline, static]
Parameters:
sourcethe expression
Returns:
true if the expression is a definition (i.e. contains a '=').

These are definitions: "a=1+1"; "f(k)=2^k"

These are not definitions: "1+1"; "x+1"

Definition at line 80 of file Symbols.java.

                                                      {
        return source.indexOf('=') != -1;
    }
synchronized Symbol org.javia.arity.Symbols.lookup ( String  name,
int  arity 
) [inline, package]

Definition at line 261 of file Symbols.java.

                                                       {
        return (Symbol) symbols.get(shell.setKey(name, arity));
    }

Here is the call graph for this function:

Here is the caller graph for this function:

synchronized Symbol org.javia.arity.Symbols.lookup ( String  name,
int  arity 
) [inline, package]

Definition at line 261 of file Symbols.java.

                                                       {
        return (Symbol) symbols.get(shell.setKey(name, arity));
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.popFrame ( ) [inline]

Pops the top frame.

   All the modifications done since this frame was pushed are reverted.
   @throws EmptyStackException if there were fewer <code>pushFrame</code> than <code>popFrame</code>.

Definition at line 177 of file Symbols.java.

                                        {
        if (delta != null) {
            for (int i = delta.size() - 1; i >= 0; --i) {
                Symbol previous = (Symbol) delta.elementAt(i);
                if (previous.isEmpty()) {
                    symbols.remove(previous);
                } else {
                    symbols.put(previous, previous);
                }
            }
        }
        delta = (Vector) frames.pop();
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.popFrame ( ) [inline]

Pops the top frame.

   All the modifications done since this frame was pushed are reverted.
   @throws EmptyStackException if there were fewer <code>pushFrame</code> than <code>popFrame</code>.

Definition at line 177 of file Symbols.java.

                                        {
        if (delta != null) {
            for (int i = delta.size() - 1; i >= 0; --i) {
                Symbol previous = (Symbol) delta.elementAt(i);
                if (previous.isEmpty()) {
                    symbols.remove(previous);
                } else {
                    symbols.put(previous, previous);
                }
            }
        }
        delta = (Vector) frames.pop();
    }

Here is the call graph for this function:

synchronized void org.javia.arity.Symbols.pushFrame ( ) [inline]

Pushes a new top frame.

   All modifications (defining new symbols) happen in the top-most frame.
   When the frame is pop-ed the modifications that happened in it are reverted.

Definition at line 166 of file Symbols.java.

                                         {
        frames.push(delta);
        delta = null;
    }
synchronized void org.javia.arity.Symbols.pushFrame ( ) [inline]

Pushes a new top frame.

   All modifications (defining new symbols) happen in the top-most frame.
   When the frame is pop-ed the modifications that happened in it are reverted.

Definition at line 166 of file Symbols.java.

                                         {
        frames.push(delta);
        delta = null;
    }

Member Data Documentation

static final Symbol org.javia.arity.Symbols.builtin [static, private]

Definition at line 195 of file Symbols.java.

static ThreadLocal org.javia.arity.Symbols.compilers = new ThreadLocal() [static, private]

Definition at line 197 of file Symbols.java.

static final String org.javia.arity.Symbols.defines [static, private]
Initial value:
 {
        "ln(x)    = log(x)",
        "log2(x)  = log(x) * 1.4426950408889634074",  
        "log10(x) = log(x) * 0.43429448190325182765", 
        "lg(x)    = log10(x)",
        "lb(x)    = log2(x)",
        "log(x, base) = log(x) / log(base)"
    }

Definition at line 238 of file Symbols.java.

Vector org.javia.arity.Symbols.delta = null [private]

Definition at line 208 of file Symbols.java.

Stack org.javia.arity.Symbols.frames = new Stack() [private]

Definition at line 209 of file Symbols.java.

static Symbol org.javia.arity.Symbols.shell = new Symbol(null, 0) [static, private]

Definition at line 196 of file Symbols.java.

Hashtable org.javia.arity.Symbols.symbols = new Hashtable() [private]

Definition at line 207 of file Symbols.java.


The documentation for this class was generated from the following files: