// File: /workspace/github_cpp_makefile_star2_O3/BlinfoldKing[P]lisval/src/lib/parser.cpp
// Function: Parser::parse()
Token* Parser::parse() {
	vector<State> state;
	Token* res = NULL;

	vector<Token*> list_stack;
	for (size_t i = 0; i < this->str.length(); i++) {
        /* debugState(state); */
		List* new_token_list = new List();
		new_token_list->type = TokenType::LIST;

        if (state.size() > 0 && state.back() == State::COMMENT) {
            if (this->str[i] == '\n') state.pop_back();
        } else if(this->str[i] == '#') {
            state.push_back(State::COMMENT);
        } else if (this->str[i] == '(') {
			state.push_back(State::LIST_START);
		} else if (this->str[i] == ')') {
			string newItem = "";
			while(state.size() && state.back() != State::LIST_START) {
				if (state.back() == State::CHAR) {
					newItem = this->parse_stack.back() + newItem;
					this->parse_stack.pop_back();
				} else {
					if (newItem != "") {
						Token* new_token = createSingularToken(newItem);
						new_token_list->list.push_back(new_token);

						newItem = "";
					}

					if (state.back() == State::LIST_TOKEN) {
						new_token_list->list.push_back(list_stack.back());
						list_stack.pop_back();
					} else {
						this->parse_stack.pop_back();
					}
				}

				state.pop_back();
			}

			if (newItem != "") {
				Token* new_token = createSingularToken(newItem);
				new_token_list->list.push_back(new_token);
			}

			reverse(new_token_list->list.begin(), new_token_list->list.end());

			state.pop_back();
			state.push_back(State::LIST_TOKEN);
			list_stack.push_back(new_token_list);
		} else if (this->str[i] == ' ' || this->str[i] == '\t') {
            if (state.size() < 1 || state.back() != State::SPACE) {
				this->parse_stack.push_back(this->str[i]);
				state.push_back(State::SPACE);
			}
		} else if (this->str[i] == '\n') {
            continue;
        } else {
			state.push_back(State::CHAR);
			this->parse_stack.push_back(this->str[i]);
		}

	}

	if (state.size() > 0 && state.back() == State::LIST_TOKEN) {
		res = list_stack.back();
		list_stack.pop_back();
		state.pop_back();
	}

	return res;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0x30(%rbp)
movq %rax, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movl $0x0, %edi
movl $0x0, %r14d
cmpq %rax, 0x20(%rbx)
je 0xbc24
leaq 0x8(%rbx), %r12
xorl %r15d, %r15d
movl $0x20, %edi
callq 0x31b0
movq %rax, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movq $0x0, 0x18(%rax)
movl $0x1, (%rax)
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rsi
cmpq %rax, %rsi
je 0xb745
cmpl $0x4, -0x4(%rsi)
movq 0x18(%rbx), %rdx
jne 0xb749
cmpb $0xa, (%rdx,%r15)
jne 0xbaa2
addq $-0x4, %rsi
jmp 0xba9e
movq 0x18(%rbx), %rdx
movb (%rdx,%r15), %cl
movzbl %cl, %edi
leal -0x20(%rdi), %r8d
cmpl $0x9, %r8d
ja 0xb786
leaq 0x3cff(%rip), %r9       # 0xf460
movslq (%r9,%r8,4), %rdi
addq %r9, %rdi
jmpq *%rdi
movl $0x4, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbaf5
movl $0x4, (%rsi)
jmp 0xba9a
cmpl $0x9, %edi
je 0xb7b8
cmpl $0xa, %edi
je 0xbaa2
movl $0x0, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbac3
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x38(%rbp)
jmp 0xbad4
cmpq %rax, %rsi
je 0xb7c7
cmpl $0x3, -0x4(%rsi)
je 0xbaa2
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0xba78
movb %cl, (%rsi)
incq (%r12)
jmp 0xba83
leaq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x3db5(%rip), %rdx      # 0xf5a8
movq %rdx, %rsi
callq 0x3bb0
movq %r14, %rax
addq $0x8, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0xb9cf
movq %r14, -0xf0(%rbp)
movl -0x4(%rax), %eax
testl %eax, %eax
je 0xb8c2
cmpl $0x1, %eax
je 0xb9cf
leaq -0x60(%rbp), %rdi
leaq 0x3d6b(%rip), %rsi      # 0xf5a8
callq 0x30b0
testl %eax, %eax
je 0xb961
leaq -0xd8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xe8(%rbp), %rdi
callq 0x3a18
leaq -0xe8(%rbp), %rdi
callq 0xb022
movq %rax, %r13
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xb8a0
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0xa8(%rbp)
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xb935
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0xb948
movq %r15, %r14
movq %rbx, %r15
movq %r12, %rbx
movq (%r12), %rax
movsbl -0x1(%rax), %esi
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
leaq -0x60(%rbp), %r13
movq %r13, %rdx
callq 0xc807
movq %r13, %rdi
movq %r12, %rsi
callq 0x3220
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xb916
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x31c0
movq $-0x1, %r13
movq %rbx, %r12
movq %rbx, %rax
movq %r15, %rbx
movq %r14, %r15
movq -0xf0(%rbp), %r14
jmp 0xb9b6
movq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9afe
movq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
leaq 0x3c4f(%rip), %rcx      # 0xf5a8
xorl %r8d, %r8d
callq 0x3280
movq -0x38(%rbp), %rax
movq $-0x1, %r13
cmpl $0x2, -0x4(%rax)
movq %r12, %rax
jne 0xb9b6
movq -0x78(%rbp), %rdx
addq $-0x8, %rdx
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xb99f
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r14)
movq $-0x8, %r13
leaq -0x78(%rbp), %rax
jmp 0xb9b6
movq $-0x8, %r13
movq -0x88(%rbp), %rdi
callq 0x9afe
leaq -0x78(%rbp), %rax
addq %r13, (%rax)
movq -0x38(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x38(%rbp)
cmpq -0x40(%rbp), %rax
jne 0xb81e
leaq -0x60(%rbp), %rdi
leaq 0x3bce(%rip), %rsi      # 0xf5a8
callq 0x30b0
testl %eax, %eax
je 0xbb26
leaq -0xb8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdx
addq %rsi, %rdx
leaq -0xc8(%rbp), %rdi
callq 0x3a18
leaq -0xc8(%rbp), %rdi
callq 0xb022
movq %rax, %r13
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xba3d
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0xa8(%rbp)
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xbb13
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0xbb26
movl $0x1, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbb04
movl $0x1, (%rsi)
jmp 0xba9a
addq %r15, %rdx
movq %rbx, %rdi
callq 0xe04c
movl $0x3, -0x60(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0xbab4
movl $0x3, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x38(%rbp)
incq %r15
cmpq 0x20(%rbx), %r15
jb 0xb6f8
jmp 0xbbf0
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xdf0c
jmp 0xbaa2
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xdf0c
movq 0x18(%rbx), %rdx
addq %r15, %rdx
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0xbaeb
movb (%rdx), %al
movb %al, (%rsi)
incq (%r12)
jmp 0xbaa2
movq %rbx, %rdi
callq 0xe04c
jmp 0xbaa2
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xdf0c
jmp 0xbaa2
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xdf0c
jmp 0xbaa2
movq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9afe
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
cmpq %rcx, %rax
setne %dl
addq $-0x8, %rcx
cmpq %rax, %rcx
seta %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0xbb6a
addq $0x8, %rax
movq -0x8(%rax), %rdx
movq (%rcx), %rsi
movq %rsi, -0x8(%rax)
movq %rdx, (%rcx)
addq $-0x8, %rcx
leaq 0x8(%rax), %rdx
cmpq %rcx, %rax
movq %rdx, %rax
jb 0xbb4c
movq -0x38(%rbp), %rax
leaq -0x4(%rax), %rsi
movq %rsi, -0x38(%rbp)
movl $0x2, -0xa8(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbb93
movl $0x2, -0x4(%rax)
movq %rax, -0x38(%rbp)
jmp 0xbba3
leaq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xdf0c
movq %r14, -0xa8(%rbp)
movq -0x78(%rbp), %rsi
cmpq -0x70(%rbp), %rsi
je 0xbbbe
movq %r14, (%rsi)
addq $0x8, -0x78(%rbp)
jmp 0xbbce
leaq -0x80(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9ecc
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xbaa2
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbaa2
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rax
cmpq %rdi, %rax
je 0xbc1d
cmpl $0x2, -0x4(%rax)
jne 0xbc1d
addq $-0x4, %rax
movq -0x78(%rbp), %rcx
movq -0x8(%rcx), %r14
addq $-0x8, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, -0x38(%rbp)
jmp 0xbc20
xorl %r14d, %r14d
movq -0x80(%rbp), %rax
testq %rax, %rax
je 0xbc3c
movq -0x70(%rbp), %rsi
subq %rax, %rsi
movq %rax, %rdi
callq 0x31c0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xbc4d
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbcda
jmp 0xbcda
jmp 0xbcbc
jmp 0xbcbc
jmp 0xbcda
jmp 0xbcda
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xbcbf
movq -0xb8(%rbp), %rsi
jmp 0xbcb0
jmp 0xbcbc
jmp 0xbcda
jmp 0xbcbc
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xbcbf
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbcbf
jmp 0xbcbc
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xbcdd
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbcdd
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0xbcf2
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xbd07
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop