Back to index

wims  3.65+svn20090927
Package Functions | Static Package Functions | Package Attributes | Private Member Functions
org.javia.arity.RPN Class Reference
Inheritance diagram for org.javia.arity.RPN:
Inheritance graph
[legend]
Collaboration diagram for org.javia.arity.RPN:
Collaboration graph
[legend]

List of all members.

Package Functions

 RPN (SyntaxException exception)
void setConsumer (TokenConsumer consumer)
void start ()
void push (Token token) throws SyntaxException
 RPN (SyntaxException exception)
void setConsumer (TokenConsumer consumer)
void start ()
void push (Token token) throws SyntaxException

Static Package Functions

static final boolean isOperand (int id)
static final boolean isOperand (int id)

Package Attributes

Stack stack = new Stack()
int prevTokenId = 0
TokenConsumer consumer
SyntaxException exception

Private Member Functions

Token top ()
void popHigher (int priority) throws SyntaxException
Token top ()
void popHigher (int priority) throws SyntaxException

Detailed Description

Definition at line 28 of file RPN.java.


Constructor & Destructor Documentation

org.javia.arity.RPN.RPN ( SyntaxException  exception) [inline, package]

Definition at line 34 of file RPN.java.

                                   {
        this.exception = exception;
    }
org.javia.arity.RPN.RPN ( SyntaxException  exception) [inline, package]

Definition at line 34 of file RPN.java.

                                   {
        this.exception = exception;
    }

Member Function Documentation

static final boolean org.javia.arity.RPN.isOperand ( int  id) [inline, static, package]

Definition at line 63 of file RPN.java.

                                           {
        return 
            id == Lexer.FACT || 
            id == Lexer.RPAREN ||
            id == Lexer.NUMBER ||
            id == Lexer.CONST;
    }

Here is the caller graph for this function:

static final boolean org.javia.arity.RPN.isOperand ( int  id) [inline, static, package]

Definition at line 63 of file RPN.java.

                                           {
        return 
            id == Lexer.FACT || 
            id == Lexer.RPAREN ||
            id == Lexer.NUMBER ||
            id == Lexer.CONST;
    }
void org.javia.arity.RPN.popHigher ( int  priority) throws SyntaxException [inline, private]

Definition at line 53 of file RPN.java.

                                                                {
        Token t = top();
        while (t != null && t.priority >= priority) {
            consumer.push(t);
            //code.push(t);
            stack.pop();
            t = top();
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void org.javia.arity.RPN.popHigher ( int  priority) throws SyntaxException [inline, private]

Definition at line 53 of file RPN.java.

                                                                {
        Token t = top();
        while (t != null && t.priority >= priority) {
            consumer.push(t);
            //code.push(t);
            stack.pop();
            t = top();
        }
    }

Here is the call graph for this function:

void org.javia.arity.RPN.push ( Token  token) throws SyntaxException [inline, package, virtual]

Implements org.javia.arity.TokenConsumer.

Definition at line 71 of file RPN.java.

                                                  {
        int priority = token.priority;
        int id = token.id;
        switch (id) {
        case Lexer.NUMBER:
        case Lexer.CONST:
            if (isOperand(prevTokenId)) {
                push(Lexer.TOK_MUL);
            }
            consumer.push(token);
            break;
            
            /*
        case Lexer.CALL:
        case Lexer.LPAREN:
            if (isOperand(prevTokenId)) {
                push(Lexer.TOK_MUL);
            }
            stack.push(token);
            break;
            */
            
        case Lexer.RPAREN: {
            if (prevTokenId == Lexer.CALL) {
                top().arity--;
            } else if (!isOperand(prevTokenId)) {
                throw exception.set("unexpected ) or END", token.position);
            }

            popHigher(priority);
            Token t = top();
            if (t != null) {
                if (t.id == Lexer.CALL) {
                    consumer.push(t);
                } else if (t != Lexer.TOK_LPAREN) {
                    throw exception.set("expected LPAREN or CALL", token.position);
                }
                stack.pop();
            }
            break;
        }
        
        case Lexer.COMMA: {            
            if (!isOperand(prevTokenId)) {
                throw exception.set("misplaced COMMA", token.position);
            }
            popHigher(priority);
            Token t = top();
            if (t==null || t.id != Lexer.CALL) {
                throw exception.set("COMMA not inside CALL", token.position);
            }
            t.arity++;
            //code.push(stack.pop());
            break;
        }
        
        case Lexer.END: {
            Token t = Lexer.TOK_RPAREN;
            t.position = token.position;
            do {
                push(t);
            } while (top() != null);
            break;
        }
            
        default: //operators, CALL, LPAREN
            if (token.assoc == Token.PREFIX) {
                if (isOperand(prevTokenId)) {
                    push(Lexer.TOK_MUL);
                }
                stack.push(token);
                break;
            }
            if (!isOperand(prevTokenId)) {
                if (id == Lexer.SUB) {
                    //change SUB to unary minus
                    token = Lexer.TOK_UMIN;
                    stack.push(token);
                    break;
                } else if (id == Lexer.ADD) {
                    // ignore, keep prevTokenId unchanged
                    return;
                }
                throw exception.set("operator without operand", token.position);
            }
            popHigher(priority + (token.assoc == Token.RIGHT ? 1 : 0));
            stack.push(token);
        }
        prevTokenId = token.id;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void org.javia.arity.RPN.push ( Token  token) throws SyntaxException [inline, package, virtual]

Implements org.javia.arity.TokenConsumer.

Definition at line 71 of file RPN.java.

                                                  {
        int priority = token.priority;
        int id = token.id;
        switch (id) {
        case Lexer.NUMBER:
        case Lexer.CONST:
            if (isOperand(prevTokenId)) {
                push(Lexer.TOK_MUL);
            }
            consumer.push(token);
            break;
            
            /*
        case Lexer.CALL:
        case Lexer.LPAREN:
            if (isOperand(prevTokenId)) {
                push(Lexer.TOK_MUL);
            }
            stack.push(token);
            break;
            */
            
        case Lexer.RPAREN: {
            if (prevTokenId == Lexer.CALL) {
                top().arity--;
            } else if (!isOperand(prevTokenId)) {
                throw exception.set("unexpected ) or END", token.position);
            }

            popHigher(priority);
            Token t = top();
            if (t != null) {
                if (t.id == Lexer.CALL) {
                    consumer.push(t);
                } else if (t != Lexer.TOK_LPAREN) {
                    throw exception.set("expected LPAREN or CALL", token.position);
                }
                stack.pop();
            }
            break;
        }
        
        case Lexer.COMMA: {            
            if (!isOperand(prevTokenId)) {
                throw exception.set("misplaced COMMA", token.position);
            }
            popHigher(priority);
            Token t = top();
            if (t==null || t.id != Lexer.CALL) {
                throw exception.set("COMMA not inside CALL", token.position);
            }
            t.arity++;
            //code.push(stack.pop());
            break;
        }
        
        case Lexer.END: {
            Token t = Lexer.TOK_RPAREN;
            t.position = token.position;
            do {
                push(t);
            } while (top() != null);
            break;
        }
            
        default: //operators, CALL, LPAREN
            if (token.assoc == Token.PREFIX) {
                if (isOperand(prevTokenId)) {
                    push(Lexer.TOK_MUL);
                }
                stack.push(token);
                break;
            }
            if (!isOperand(prevTokenId)) {
                if (id == Lexer.SUB) {
                    //change SUB to unary minus
                    token = Lexer.TOK_UMIN;
                    stack.push(token);
                    break;
                } else if (id == Lexer.ADD) {
                    // ignore, keep prevTokenId unchanged
                    return;
                }
                throw exception.set("operator without operand", token.position);
            }
            popHigher(priority + (token.assoc == Token.RIGHT ? 1 : 0));
            stack.push(token);
        }
        prevTokenId = token.id;
    }

Here is the call graph for this function:

void org.javia.arity.RPN.setConsumer ( TokenConsumer  consumer) [inline, package]

Definition at line 38 of file RPN.java.

                                             {
        this.consumer = consumer;
    }

Here is the caller graph for this function:

void org.javia.arity.RPN.setConsumer ( TokenConsumer  consumer) [inline, package]

Definition at line 38 of file RPN.java.

                                             {
        this.consumer = consumer;
    }
void org.javia.arity.RPN.start ( ) [inline, package]

Reimplemented from org.javia.arity.TokenConsumer.

Definition at line 43 of file RPN.java.

                 {
        stack.removeAllElements();
        prevTokenId = 0;
        consumer.start();
    }

Here is the call graph for this function:

void org.javia.arity.RPN.start ( ) [inline, package]

Reimplemented from org.javia.arity.TokenConsumer.

Definition at line 43 of file RPN.java.

                 {
        stack.removeAllElements();
        prevTokenId = 0;
        consumer.start();
    }

Here is the call graph for this function:

Token org.javia.arity.RPN.top ( ) [inline, private]

Definition at line 49 of file RPN.java.

                        {
        return stack.empty() ? null : (Token) stack.peek();
    }
Token org.javia.arity.RPN.top ( ) [inline, private]

Definition at line 49 of file RPN.java.

                        {
        return stack.empty() ? null : (Token) stack.peek();
    }

Here is the caller graph for this function:


Member Data Documentation

Definition at line 31 of file RPN.java.

Definition at line 32 of file RPN.java.

Definition at line 30 of file RPN.java.

Stack org.javia.arity.RPN.stack = new Stack() [package]

Definition at line 29 of file RPN.java.


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