// File: xzebra[P]function-plotter/mathParser/mathParser.cpp
// Function: Parser::Solve(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
double Parser::Solve(const std::string postfix) {
    Stack<double> operandStack = Stack<double>();
    std::stringstream ss(postfix);
    std::string token;
    
    while(ss >> token) { // iterate through each token in string    
        if(isNum(token[0]) || token.length() > 1 && token[0] == '-') { //starts with -  
            // if number push to stack
            operandStack.push(std::stod(token));
        } else { // is operator -> solve that operation
            double y = operandStack.pop();
            // push result to stack      
            switch (token[0]) {
                case '+':    
                    operandStack.push(operandStack.pop() + y);
                    continue;
                case '-':
                    operandStack.push(operandStack.pop() - y);
                    continue;
                case '*':
                    operandStack.push(operandStack.pop() * y);
                    continue;
                case '/':
                    operandStack.push(operandStack.pop() / y);
                    continue;
                case '^':
                    operandStack.push(std::pow(operandStack.pop(), y));
                    continue;
            }
            if(token == "sin") {               
                operandStack.push(std::sin(y));
            } else if(token == "cos") {
                operandStack.push(std::cos(y));
            } else if(token == "tan") {
                operandStack.push(std::tan(y));
            }         
        }
    }

    // pop result from stack
    return operandStack.pop();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x280, %rsp            # imm = 0x280
movq %rsi, -0x200(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x30(%rbp)
movq $0x0, -0x20(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x54f0
movl $0x10, %edi
movl $0x8, %esi
callq 0x5510
movl %eax, -0x1f4(%rbp)
jmp 0x4c69
movl -0x1f4(%rbp), %edx
movq -0x200(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x22e0
jmp 0x4c84
leaq -0x1e8(%rbp), %rdi
callq 0x22a0
leaq -0x1b8(%rbp), %rdi
leaq -0x1e8(%rbp), %rsi
callq 0x2240
movq %rax, -0x208(%rbp)
jmp 0x4cac
movq -0x208(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2230
movb %al, -0x209(%rbp)
jmp 0x4cca
movb -0x209(%rbp), %al
testb $0x1, %al
jne 0x4cd9
jmp 0x5036
xorl %eax, %eax
movl %eax, %esi
leaq -0x1e8(%rbp), %rdi
callq 0x2380
movq %rax, -0x218(%rbp)
jmp 0x4cf2
movq -0x218(%rbp), %rax
movsbl (%rax), %edi
callq 0x3e80
testb $0x1, %al
jne 0x4d43
leaq -0x1e8(%rbp), %rdi
callq 0x22c0
cmpq $0x1, %rax
jbe 0x4dbb
xorl %eax, %eax
movl %eax, %esi
leaq -0x1e8(%rbp), %rdi
callq 0x2380
movq %rax, -0x220(%rbp)
jmp 0x4d34
movq -0x220(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
jne 0x4dbb
xorl %eax, %eax
movl %eax, %esi
leaq -0x1e8(%rbp), %rdi
callq 0x2ac0
movsd %xmm0, -0x228(%rbp)
jmp 0x4d5d
movsd -0x228(%rbp), %xmm0
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4d70
jmp 0x5031
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1c0(%rbp)
movl %eax, -0x1c4(%rbp)
jmp 0x507b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x1c0(%rbp)
movl %eax, -0x1c4(%rbp)
leaq -0x1e8(%rbp), %rdi
callq 0x2130
leaq -0x1b8(%rbp), %rdi
callq 0x2170
jmp 0x507b
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x230(%rbp)
jmp 0x4dce
movsd -0x230(%rbp), %xmm0
movsd %xmm0, -0x1f0(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x1e8(%rbp), %rdi
callq 0x2380
movq %rax, -0x238(%rbp)
jmp 0x4df7
movq -0x238(%rbp), %rax
movsbl (%rax), %eax
movl %eax, -0x23c(%rbp)
subl $0x2a, %eax
je 0x4ebf
jmp 0x4e12
movl -0x23c(%rbp), %eax
subl $0x2b, %eax
je 0x4e51
jmp 0x4e1f
movl -0x23c(%rbp), %eax
subl $0x2d, %eax
je 0x4e88
jmp 0x4e2c
movl -0x23c(%rbp), %eax
subl $0x2f, %eax
je 0x4ef6
jmp 0x4e3d
movl -0x23c(%rbp), %eax
subl $0x5e, %eax
je 0x4f2d
jmp 0x4f65
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x248(%rbp)
jmp 0x4e64
movsd -0x248(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
addsd %xmm1, %xmm0
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4e83
jmp 0x4c90
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x250(%rbp)
jmp 0x4e9b
movsd -0x250(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
subsd %xmm1, %xmm0
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4eba
jmp 0x4c90
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x258(%rbp)
jmp 0x4ed2
movsd -0x258(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
mulsd %xmm1, %xmm0
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4ef1
jmp 0x4c90
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x260(%rbp)
jmp 0x4f09
movsd -0x260(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
divsd %xmm1, %xmm0
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4f28
jmp 0x4c90
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x268(%rbp)
jmp 0x4f40
movsd -0x268(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
callq 0x2310
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4f60
jmp 0x4c90
leaq 0x3145(%rip), %rsi      # 0x80b1
leaq -0x1e8(%rbp), %rdi
callq 0x2a90
movb %al, -0x269(%rbp)
jmp 0x4f80
movb -0x269(%rbp), %al
testb $0x1, %al
jne 0x4f8c
jmp 0x4fa9
movsd -0x1f0(%rbp), %xmm0
callq 0x2070
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4fa4
jmp 0x502f
leaq 0x3105(%rip), %rsi      # 0x80b5
leaq -0x1e8(%rbp), %rdi
callq 0x2a90
movb %al, -0x26a(%rbp)
jmp 0x4fc4
movb -0x26a(%rbp), %al
testb $0x1, %al
jne 0x4fd0
jmp 0x4fea
movsd -0x1f0(%rbp), %xmm0
callq 0x2340
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x4fe8
jmp 0x502d
leaq 0x30c8(%rip), %rsi      # 0x80b9
leaq -0x1e8(%rbp), %rdi
callq 0x2a90
movb %al, -0x26b(%rbp)
jmp 0x5005
movb -0x26b(%rbp), %al
testb $0x1, %al
jne 0x5011
jmp 0x502b
movsd -0x1f0(%rbp), %xmm0
callq 0x2100
leaq -0x30(%rbp), %rdi
callq 0x5530
jmp 0x5029
jmp 0x502b
jmp 0x502d
jmp 0x502f
jmp 0x5031
jmp 0x4c90
leaq -0x30(%rbp), %rdi
callq 0x5610
movsd %xmm0, -0x278(%rbp)
jmp 0x5049
leaq -0x1e8(%rbp), %rdi
callq 0x2130
leaq -0x1b8(%rbp), %rdi
callq 0x2170
leaq -0x30(%rbp), %rdi
callq 0x5650
movsd -0x278(%rbp), %xmm0
addq $0x280, %rsp            # imm = 0x280
popq %rbp
retq
leaq -0x30(%rbp), %rdi
callq 0x5650
movq -0x1c0(%rbp), %rdi
callq 0x2320