/*
 * The MIT License
 *
 * Copyright 2016 Maxim Krivchun.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cexpr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

/**
 *
 * @author Maxim Krivchun
 */
public class Parser {

    private Lexical lex;
    private Stack<ExprToken> stk;
    private List<ExprToken> out;
    private static final HashMap<Character, Integer> OPERATOR_PRIORITY // for stack
            = new HashMap<Character, Integer>() {
        {
            put('f', 5); // function
            put('p', 4); // unary plus
            put('m', 4); // unary minus
            put('(', 0); // for stack
            put(')', 0);
            put('^', 3);
            put('*', 2);
            put('%', 2);
            put('/', 2);
            put('+', 1);
            put('-', 1);
        }
    };

    public Parser(String str, Functions functions) {
        lex = new Lexical(str, functions);
        stk = new Stack<>();
        out = new ArrayList<>();
    }

    public List<ExprToken> get() throws CompileError {
        ExprToken tok;
        boolean unary = true;
        do {
            tok = lex.next_token();
            switch (tok.type) {
                case NUM_RE:
                case NUM_IM:
                case IDENTIFIER:
                    out.add(tok);
                    unary = false;
                    break;
                case FUNCTION:
                case OPERATOR:
                    char operator = (tok.type == ExprToken.TYPE.FUNCTION
                            ? 'f' : (Character) tok.val);
                    int priority = OPERATOR_PRIORITY.get(operator);
                    if (unary && operator == '-')
                        tok = new ExprToken(ExprToken.TYPE.OPERATOR, 'm'); // unary minus
                    if (unary && operator == '+')
                        tok = new ExprToken(ExprToken.TYPE.OPERATOR, 'p'); // unary plus
                    ExprToken top;
                    unary = true;
                    if (operator == ')') {
                        unary = false;
                        while (!stk.empty()) {
                            top = stk.pop();
                            if (top.type == ExprToken.TYPE.FUNCTION
                                    || (char) top.val != '(') {
                                out.add(top);
                            } else {
                                break;
                            }
                        };
                    } else if (operator == '(') {
                        stk.add(tok);
                    } else if (stk.empty()) {
                        stk.add(tok);
                    } else {
                        int top_priority;
                        while (!stk.empty()) {
                            top = stk.peek();
                            top_priority = OPERATOR_PRIORITY
                                    .get(top.type == ExprToken.TYPE.FUNCTION
                                            ? 'f' : (char) top.val);
                            if (top.type == ExprToken.TYPE.FUNCTION
                                    || top_priority >= priority) {
                                out.add(top);
                                stk.pop();
                            } else {
                                break;
                            }
                        }
                        stk.add(tok);
                    }
                    break;
            }
        } while (tok.type != ExprToken.TYPE.END);
        while (!stk.empty()) {
            out.add(stk.pop());
        }

        // empty input
        if (out.size() == 0){
            out.add(new ExprToken(ExprToken.TYPE.NUM_RE, 0.0));
        }
        // single number/variable
        if (out.size() == 1 && (out.get(0).type == ExprToken.TYPE.NUM_IM
                || out.get(0).type == ExprToken.TYPE.NUM_RE
                || out.get(0).type == ExprToken.TYPE.IDENTIFIER)) {
            out.add(new ExprToken(ExprToken.TYPE.NUM_RE, 0.0));
            out.add(new ExprToken(ExprToken.TYPE.OPERATOR, '+'));
        }

        return out;
    }
}

@@ -1603,29 +1603,28 @@
 ut('
-p', 4
+(', 0
 ); // 
-unary plus
+for stack
 %0A   
@@ -1641,30 +1641,15 @@
 ut('
-m', 4
+)', 0
 );
- // unary minus
 %0A   
@@ -1662,36 +1662,23 @@
    put('
-(', 0
+%5E', 4
 );
- // for stack
 %0A       
@@ -1679,39 +1679,53 @@
            put('
-)', 0
+p', 3
 );
+ // unary plus
 %0A            put
@@ -1730,15 +1730,30 @@
 ut('
-%5E
+m
 ', 3);
+ // unary minus
 %0A   

update priority for unary operators