// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
xorpd %xmm0, %xmm0
movapd %xmm0, -0x40(%rbp)
andq $0x0, -0x30(%rbp)
leaq -0x1f0(%rbp), %rdi
pushq $0x18
popq %rdx
callq 0x2240
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x1f0(%rbp), %rbx
leaq -0x68(%rbp), %r14
leaq -0x40(%rbp), %r15
leaq 0x17f7(%rip), %r12      # 0x50f2
leaq 0x17ba(%rip), %r13      # 0x50bc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x3a5d
movq -0x68(%rbp), %rdx
movb (%rdx), %al
leal -0x30(%rax), %ecx
cmpb $0xa, %cl
jb 0x3938
cmpb $0x2d, %al
jne 0x394c
cmpq $0x2, -0x60(%rbp)
jb 0x394c
movq %r14, %rdi
xorl %esi, %esi
callq 0x276d
movq %r15, %rdi
callq 0x3c1a
jmp 0x3902
movq -0x40(%rbp), %rax
movsd (%rax), %xmm1
movq 0x8(%rax), %rcx
movq %rcx, -0x40(%rbp)
movl -0x30(%rbp), %eax
leal -0x1(%rax), %esi
movl %esi, -0x30(%rbp)
movzbl (%rdx), %edx
leal -0x2a(%rdx), %esi
cmpl $0x5, %esi
ja 0x3980
movslq (%r13,%rsi,4), %rdx
addq %r13, %rdx
jmpq *%rdx
mulsd (%rcx), %xmm1
jmp 0x39f8
cmpl $0x5e, %edx
jne 0x39a1
movsd (%rcx), %xmm0
movq 0x8(%rcx), %rcx
movq %rcx, -0x40(%rbp)
addl $-0x2, %eax
movl %eax, -0x30(%rbp)
callq 0x2280
jmp 0x3a48
movsd %xmm1, -0x48(%rbp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x275d
testb %al, %al
je 0x3a08
movsd -0x48(%rbp), %xmm0
callq 0x2070
jmp 0x3a48
movsd (%rcx), %xmm0
movq 0x8(%rcx), %rcx
movq %rcx, -0x40(%rbp)
addl $-0x2, %eax
movl %eax, -0x30(%rbp)
subsd %xmm1, %xmm0
jmp 0x3a48
movsd (%rcx), %xmm0
movq 0x8(%rcx), %rcx
movq %rcx, -0x40(%rbp)
addl $-0x2, %eax
movl %eax, -0x30(%rbp)
divsd %xmm1, %xmm0
jmp 0x3a48
addsd (%rcx), %xmm1
movq 0x8(%rcx), %rcx
movq %rcx, -0x40(%rbp)
addl $-0x2, %eax
movl %eax, -0x30(%rbp)
jmp 0x3a4c
movq %r14, %rdi
leaq 0x16e4(%rip), %rsi      # 0x50f6
callq 0x275d
testb %al, %al
je 0x3a27
movsd -0x48(%rbp), %xmm0
callq 0x22a0
jmp 0x3a48
movq %r14, %rdi
leaq 0x16c9(%rip), %rsi      # 0x50fa
callq 0x275d
testb %al, %al
je 0x3902
movsd -0x48(%rbp), %xmm0
callq 0x20f0
movapd %xmm0, %xmm1
movq %r15, %rdi
movapd %xmm1, %xmm0
callq 0x3c1a
jmp 0x3902
leaq -0x40(%rbp), %rbx
movq (%rbx), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq 0x8(%rax), %rax
movq %rax, (%rbx)
decl 0x10(%rbx)
leaq -0x68(%rbp), %rdi
callq 0x20c0
leaq -0x1f0(%rbp), %rdi
callq 0x2120
movq %rbx, %rdi
callq 0x40ea
movsd -0x48(%rbp), %xmm0
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x3aca
jmp 0x3ab2
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20c0
leaq -0x1f0(%rbp), %rdi
callq 0x2120
leaq -0x40(%rbp), %rdi
callq 0x40ea
movq %rbx, %rdi
callq 0x2290
nop