// File: havluj[P]spreadsheet/src/CExpressionParser.cpp
// Function: CExpressionParser::InfixToRPN(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>&)
bool CExpressionParser::InfixToRPN(std::vector<std::string>& inputs) {
    std::list<std::string> infix;
    Tokenize(infix, " ");

    typedef std::list<std::string>::const_iterator tok_iter;
    std::stack<std::string> stack;
    std::queue<std::string> outputQueue;

    bool success = true;

    // For each token
    for (tok_iter it = infix.begin(); it != infix.end(); it++) {
	  // Read a token.
	  std::string token = *it;

	  // If the token is a number, then add it to the output queue.
	  if (IsNumber(token) || IsFloat(token) || IsPi(token) || IsE(token)) {
		outputQueue.push(token);
	  }// If the token is a function token, then push it onto the stack.
	  else if (IsFunction(token)) {
		stack.push(token);
	  } else if (IsComma(token)) {
		// Until token at the top of stack is left parenthesis, pop operators 
		// off the stack onto the output queue.             
		std::string stackToken = stack.top();

		while (stackToken != "(") {
		    outputQueue.push(stackToken);
		    stack.pop();
		    stackToken = stack.top();
		}

		if (stackToken == "(") {
		    success = true;
		} else {
		    success = false;
		}
	  }// If the token (o1) is an operator, then:
	  else if (IsOperator(token)) {
		// While there is operator token, o2, at the top of the stack, and:
		// either o1 is left-associative and its precedence is equal to that of o2,
		// or o1 has precedence less than that of o2
		while (!stack.empty() && IsOperator(stack.top()) &&
			  ((OpLeftAssoc(token) && OpPrecedence(token) == OpPrecedence(stack.top())) ||
			  (OpPrecedence(token) < OpPrecedence(stack.top())))) {
		    // Pop o2 off the stack, onto the output queue
		    std::string stackToken = stack.top();
		    stack.pop();
		    outputQueue.push(stackToken);
		}

		// Push o1 onto the stack
		stack.push(token);

	  }// If the token is a left parenthesis, then push it onto the stack.
	  else if (token == "(") {
		// Push token to top of the stack  
		stack.push(token);
	  }// If the token is a right parenthesis:
	  else if (token == ")") {
		// Until the token at the top of the stack is a left parenthesis, pop operators 
		// off the stack onto the output queue.
		while (!stack.empty() && stack.top() != "(") {
		    // Add to end of list  
		    outputQueue.push(stack.top());
		    stack.pop();
		}

		// If stack runs out without finding a left parenthesis, 
		// there are mismatched parentheses
		if (!stack.empty()) {
		    std::string stackToken = stack.top();

		    // Mismatched parentheses
		    if (stackToken != "(") {
			  success = false;
		    }
		} else {
		    return false;
		}

		// Pop left parenthesis from the stack, but not onto output queue.
		stack.pop();

		// If token at top of stack is function token, pop it onto output queue
		if (!stack.empty()) {
		    std::string stackToken = stack.top();
		    if (IsFunction(stackToken)) {
			  outputQueue.push(stackToken);
			  stack.pop();
		    }
		}
	  }
    }

    // While there are still operator tokens in the stack:
    while (!stack.empty()) {
	  // Pop the operator onto the output queue
	  outputQueue.push(stack.top());
	  stack.pop();
    }

    while (!outputQueue.empty()) {
	  std::string token = outputQueue.front();
	  inputs.push_back(token);
	  outputQueue.pop();
    }

    return success;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x210(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x18500
leaq -0x51(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x5740
movq -0x208(%rbp), %rdx
leaq 0x572e(%rip), %rsi      # 0x1c833
leaq -0x50(%rbp), %rdi
callq 0x7360
jmp 0x17110
movq -0x210(%rbp), %rdi
leaq -0x30(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x14fd0
jmp 0x17126
leaq -0x50(%rbp), %rdi
callq 0x52a0
leaq -0x51(%rbp), %rdi
callq 0x54a0
leaq -0xb8(%rbp), %rdi
callq 0x181e0
jmp 0x17146
leaq -0x108(%rbp), %rdi
callq 0x18520
jmp 0x17154
movb $0x1, -0x109(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x17e40
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x18090
leaq -0x30(%rbp), %rdi
callq 0x17ed0
movq %rax, -0x130(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x18090
leaq -0x118(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x18550
testb $0x1, %al
jne 0x171bd
jmp 0x17958
leaq -0x118(%rbp), %rdi
callq 0x18580
movq %rax, %rsi
leaq -0x150(%rbp), %rdi
callq 0x51b0
jmp 0x171da
leaq -0x150(%rbp), %rdi
callq 0x14d50
movb %al, -0x211(%rbp)
jmp 0x171ee
movb -0x211(%rbp), %al
testb $0x1, %al
jne 0x1725d
jmp 0x171fa
leaq -0x150(%rbp), %rdi
callq 0x14c30
movb %al, -0x212(%rbp)
jmp 0x1720e
movb -0x212(%rbp), %al
testb $0x1, %al
jne 0x1725d
jmp 0x1721a
leaq -0x150(%rbp), %rdi
callq 0x14990
movb %al, -0x213(%rbp)
jmp 0x1722e
movb -0x213(%rbp), %al
testb $0x1, %al
jne 0x1725d
jmp 0x1723a
leaq -0x150(%rbp), %rdi
callq 0x14a30
movb %al, -0x214(%rbp)
jmp 0x1724e
movb -0x214(%rbp), %al
testb $0x1, %al
jne 0x1725d
jmp 0x172ec
leaq -0x108(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x185b0
jmp 0x17272
jmp 0x17905
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1729a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x52a0
leaq -0x51(%rbp), %rdi
callq 0x54a0
jmp 0x17aa8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x17aa8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x17a9c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x17a90
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x17947
leaq -0x150(%rbp), %rdi
callq 0x14ad0
movb %al, -0x215(%rbp)
jmp 0x17300
movb -0x215(%rbp), %al
testb $0x1, %al
jne 0x1730c
jmp 0x17326
leaq -0xb8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x182d0
jmp 0x17321
jmp 0x17903
leaq -0x150(%rbp), %rdi
callq 0x148c0
movb %al, -0x216(%rbp)
jmp 0x1733a
movb -0x216(%rbp), %al
testb $0x1, %al
jne 0x17349
jmp 0x17450
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x220(%rbp)
jmp 0x1735e
movq -0x220(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x51b0
jmp 0x17373
jmp 0x17375
leaq 0x4c88(%rip), %rsi      # 0x1c004
leaq -0x170(%rbp), %rdi
callq 0x185e0
movb %al, -0x221(%rbp)
jmp 0x17390
movb -0x221(%rbp), %al
testb $0x1, %al
jne 0x1739c
jmp 0x17408
leaq -0x108(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x185b0
jmp 0x173b1
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x173bf
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x230(%rbp)
jmp 0x173d4
movq -0x230(%rbp), %rsi
leaq -0x170(%rbp), %rdi
callq 0x5040
jmp 0x173e9
jmp 0x17375
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x52a0
jmp 0x17947
leaq 0x4bf5(%rip), %rsi      # 0x1c004
leaq -0x170(%rbp), %rdi
callq 0x17ac0
movb %al, -0x231(%rbp)
jmp 0x17423
movb -0x231(%rbp), %al
testb $0x1, %al
jne 0x1742f
jmp 0x17438
movb $0x1, -0x109(%rbp)
jmp 0x1743f
movb $0x0, -0x109(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x52a0
jmp 0x17901
leaq -0x150(%rbp), %rdi
callq 0x147e0
movb %al, -0x232(%rbp)
jmp 0x17464
movb -0x232(%rbp), %al
testb $0x1, %al
jne 0x17473
jmp 0x17659
jmp 0x17475
leaq -0xb8(%rbp), %rdi
callq 0x18610
movb %al, -0x233(%rbp)
jmp 0x17489
movb -0x233(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x234(%rbp)
jne 0x175b8
jmp 0x174a2
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x240(%rbp)
jmp 0x174b7
movq -0x240(%rbp), %rdi
callq 0x147e0
movb %al, -0x241(%rbp)
jmp 0x174cb
movb -0x241(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x234(%rbp)
jne 0x174e3
jmp 0x175b8
leaq -0x150(%rbp), %rdi
callq 0x14720
movb %al, -0x242(%rbp)
jmp 0x174f7
movb -0x242(%rbp), %al
testb $0x1, %al
jne 0x17503
jmp 0x17558
leaq -0x150(%rbp), %rdi
callq 0x14640
movl %eax, -0x248(%rbp)
jmp 0x17517
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x250(%rbp)
jmp 0x1752c
movq -0x250(%rbp), %rdi
callq 0x14640
movl %eax, -0x254(%rbp)
jmp 0x17540
movl -0x248(%rbp), %ecx
movl -0x254(%rbp), %edx
movb $0x1, %al
cmpl %edx, %ecx
movb %al, -0x255(%rbp)
je 0x175ac
leaq -0x150(%rbp), %rdi
callq 0x14640
movl %eax, -0x25c(%rbp)
jmp 0x1756c
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x268(%rbp)
jmp 0x17581
movq -0x268(%rbp), %rdi
callq 0x14640
movl %eax, -0x26c(%rbp)
jmp 0x17595
movl -0x25c(%rbp), %eax
movl -0x26c(%rbp), %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x255(%rbp)
movb -0x255(%rbp), %al
movb %al, -0x234(%rbp)
movb -0x234(%rbp), %al
testb $0x1, %al
jne 0x175c4
jmp 0x1763f
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x278(%rbp)
jmp 0x175d9
movq -0x278(%rbp), %rsi
leaq -0x190(%rbp), %rdi
callq 0x51b0
jmp 0x175ee
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x175fc
leaq -0x108(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x185b0
jmp 0x17611
leaq -0x190(%rbp), %rdi
callq 0x52a0
jmp 0x17475
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x52a0
jmp 0x17947
leaq -0xb8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x182d0
jmp 0x17654
jmp 0x178ff
leaq 0x49a4(%rip), %rsi      # 0x1c004
leaq -0x150(%rbp), %rdi
callq 0x17ac0
movb %al, -0x279(%rbp)
jmp 0x17674
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0x17680
jmp 0x1769a
leaq -0xb8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x182d0
jmp 0x17695
jmp 0x178fd
leaq 0x5235(%rip), %rsi      # 0x1c8d6
leaq -0x150(%rbp), %rdi
callq 0x17ac0
movb %al, -0x27a(%rbp)
jmp 0x176b5
movb -0x27a(%rbp), %al
testb $0x1, %al
jne 0x176c4
jmp 0x178fb
jmp 0x176c6
leaq -0xb8(%rbp), %rdi
callq 0x18610
movb %al, -0x27b(%rbp)
jmp 0x176da
movb -0x27b(%rbp), %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x27c(%rbp)
jne 0x1772d
jmp 0x176ef
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x288(%rbp)
jmp 0x17704
movq -0x288(%rbp), %rdi
leaq 0x48f2(%rip), %rsi      # 0x1c004
callq 0x185e0
movb %al, -0x289(%rbp)
jmp 0x1771f
movb -0x289(%rbp), %al
movb %al, -0x27c(%rbp)
jmp 0x1772d
movb -0x27c(%rbp), %al
testb $0x1, %al
jne 0x17739
jmp 0x17776
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x298(%rbp)
jmp 0x1774e
movq -0x298(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x185b0
jmp 0x17763
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x17771
jmp 0x176c6
leaq -0xb8(%rbp), %rdi
callq 0x18610
movb %al, -0x299(%rbp)
jmp 0x1778a
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x1781f
jmp 0x1779a
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x2a8(%rbp)
jmp 0x177af
movq -0x2a8(%rbp), %rsi
leaq -0x1b0(%rbp), %rdi
callq 0x51b0
jmp 0x177c4
leaq 0x4839(%rip), %rsi      # 0x1c004
leaq -0x1b0(%rbp), %rdi
callq 0x185e0
movb %al, -0x2a9(%rbp)
jmp 0x177df
movb -0x2a9(%rbp), %al
testb $0x1, %al
jne 0x177eb
jmp 0x17811
movb $0x0, -0x109(%rbp)
jmp 0x17811
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x52a0
jmp 0x17947
leaq -0x1b0(%rbp), %rdi
callq 0x52a0
jmp 0x17832
movb $0x0, -0x1(%rbp)
movl $0x1, -0x1b4(%rbp)
jmp 0x1790f
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x17840
leaq -0xb8(%rbp), %rdi
callq 0x18610
movb %al, -0x2aa(%rbp)
jmp 0x17854
movb -0x2aa(%rbp), %al
testb $0x1, %al
jne 0x178f9
jmp 0x17864
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x2b8(%rbp)
jmp 0x17879
movq -0x2b8(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
callq 0x51b0
jmp 0x1788e
leaq -0x1d8(%rbp), %rdi
callq 0x14ad0
movb %al, -0x2b9(%rbp)
jmp 0x178a2
movb -0x2b9(%rbp), %al
testb $0x1, %al
jne 0x178ae
jmp 0x178ed
leaq -0x108(%rbp), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x185b0
jmp 0x178c3
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x178d1
jmp 0x178ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x52a0
jmp 0x17947
leaq -0x1d8(%rbp), %rdi
callq 0x52a0
jmp 0x178fb
jmp 0x178fd
jmp 0x178ff
jmp 0x17901
jmp 0x17903
jmp 0x17905
movl $0x0, -0x1b4(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x52a0
movl -0x1b4(%rbp), %eax
testl %eax, %eax
jne 0x17a61
jmp 0x1792b
jmp 0x1792d
leaq -0x118(%rbp), %rdi
xorl %esi, %esi
callq 0x18630
movq %rax, -0x1e0(%rbp)
jmp 0x1717e
leaq -0x150(%rbp), %rdi
callq 0x52a0
jmp 0x17a90
jmp 0x1795a
leaq -0xb8(%rbp), %rdi
callq 0x18610
movb %al, -0x2ba(%rbp)
jmp 0x1796e
movb -0x2ba(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1797c
jmp 0x179b6
leaq -0xb8(%rbp), %rdi
callq 0x18340
movq %rax, -0x2c8(%rbp)
jmp 0x17991
movq -0x2c8(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x185b0
jmp 0x179a6
leaq -0xb8(%rbp), %rdi
callq 0x18360
jmp 0x179b4
jmp 0x1795a
jmp 0x179b8
leaq -0x108(%rbp), %rdi
callq 0x18660
movb %al, -0x2c9(%rbp)
jmp 0x179cc
movb -0x2c9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x179da
jmp 0x17a4c
leaq -0x108(%rbp), %rdi
callq 0x18680
movq %rax, -0x2d8(%rbp)
jmp 0x179ef
movq -0x2d8(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x51b0
jmp 0x17a04
movq -0x18(%rbp), %rdi
leaq -0x200(%rbp), %rsi
callq 0x186a0
jmp 0x17a16
leaq -0x108(%rbp), %rdi
callq 0x18710
jmp 0x17a24
leaq -0x200(%rbp), %rdi
callq 0x52a0
jmp 0x179b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x52a0
jmp 0x17a90
movb -0x109(%rbp), %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl $0x1, -0x1b4(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x18730
leaq -0xb8(%rbp), %rdi
callq 0x184e0
leaq -0x30(%rbp), %rdi
callq 0x18750
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x18730
leaq -0xb8(%rbp), %rdi
callq 0x184e0
leaq -0x30(%rbp), %rdi
callq 0x18750
movq -0x60(%rbp), %rdi
callq 0x5730
nopw (%rax,%rax)