// File: /workspace/github_cpp_makefile_star2_O1/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
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
xorl %eax, %eax
movq %rax, -0x30(%rbp)
movq %rax, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
cmpq %rax, 0x20(%rdi)
je 0xbccd
movq %rdi, %rbx
leaq 0x8(%rdi), %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), %rcx
movq -0x38(%rbp), %rsi
cmpq %rcx, %rsi
je 0xb822
cmpl $0x4, -0x4(%rsi)
jne 0xb822
movq 0x18(%rbx), %rax
cmpb $0xa, (%rax,%r15)
jne 0xbb7f
addq $-0x4, %rsi
jmp 0xbb7b
movq 0x18(%rbx), %rdx
movb (%rdx,%r15), %al
movzbl %al, %edi
leal -0x20(%rdi), %r8d
cmpl $0x9, %r8d
ja 0xb863
leaq 0x3c5a(%rip), %r9       # 0xf498
movslq (%r9,%r8,4), %rdi
addq %r9, %rdi
jmpq *%rdi
movl $0x4, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbbd2
movl $0x4, (%rsi)
jmp 0xbb77
cmpl $0x9, %edi
je 0xb895
cmpl $0xa, %edi
je 0xbb7f
movl $0x0, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbba0
movl $0x0, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x38(%rbp)
jmp 0xbbad
cmpq %rcx, %rsi
je 0xb8a4
cmpl $0x3, -0x4(%rsi)
je 0xbb7f
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0xbb55
movb %al, (%rsi)
incq 0x8(%rbx)
jmp 0xbb60
leaq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq -0x60(%rbp), %rdi
leaq 0x3d10(%rip), %rdx      # 0xf5e0
movq %rdx, %rsi
callq 0x3ba8
movq %r14, %rax
addq $0x8, %rax
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0xbaac
movq %r14, -0xf0(%rbp)
movl -0x4(%rax), %eax
testl %eax, %eax
je 0xb99f
cmpl $0x1, %eax
je 0xbaac
leaq -0x60(%rbp), %rdi
leaq 0x3cc6(%rip), %rsi      # 0xf5e0
callq 0x30b0
testl %eax, %eax
je 0xba3e
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 0xb0d6
movq %rax, %r13
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xb97d
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0xa8(%rbp)
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xba12
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0xba25
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 0xc8dd
movq %r13, %rdi
movq %r12, %rsi
callq 0x3220
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xb9f3
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 0xba93
movq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9bf4
movq -0x58(%rbp), %rdx
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
leaq 0x3baa(%rip), %rcx      # 0xf5e0
xorl %r8d, %r8d
callq 0x3280
movq -0x38(%rbp), %rax
movq $-0x1, %r13
cmpl $0x2, -0x4(%rax)
movq %r12, %rax
jne 0xba93
movq -0x78(%rbp), %rdx
addq $-0x8, %rdx
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xba7c
movq (%rdx), %rax
movq %rax, (%rsi)
addq $0x8, 0x10(%r14)
movq $-0x8, %r13
leaq -0x78(%rbp), %rax
jmp 0xba93
movq $-0x8, %r13
movq -0x88(%rbp), %rdi
callq 0x9bf4
leaq -0x78(%rbp), %rax
addq %r13, (%rax)
movq -0x38(%rbp), %rax
addq $-0x4, %rax
movq %rax, -0x38(%rbp)
cmpq -0x40(%rbp), %rax
jne 0xb8fb
leaq -0x60(%rbp), %rdi
leaq 0x3b29(%rip), %rsi      # 0xf5e0
callq 0x30b0
testl %eax, %eax
je 0xbc03
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 0xb0d6
movq %rax, %r13
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xbb1a
movq -0xb8(%rbp), %rsi
incq %rsi
callq 0x31c0
movq %r13, -0xa8(%rbp)
movq 0x10(%r14), %rsi
cmpq 0x18(%r14), %rsi
je 0xbbf0
movq %r13, (%rsi)
addq $0x8, 0x10(%r14)
jmp 0xbc03
movl $0x1, -0x60(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbbe1
movl $0x1, (%rsi)
jmp 0xbb77
addq %r15, %rdx
movq %rbx, %rdi
callq 0xe158
movl $0x3, -0x60(%rbp)
movq -0x38(%rbp), %rsi
cmpq -0x30(%rbp), %rsi
je 0xbb91
movl $0x3, (%rsi)
addq $0x4, %rsi
movq %rsi, -0x38(%rbp)
incq %r15
cmpq 0x20(%rbx), %r15
jb 0xb7d5
jmp 0xbccd
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xe018
jmp 0xbb7f
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xe018
movq 0x18(%rbx), %rdx
addq %r15, %rdx
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0xbbc8
movb (%rdx), %al
movb %al, (%rsi)
incq 0x8(%rbx)
jmp 0xbb7f
movq %rbx, %rdi
callq 0xe158
jmp 0xbb7f
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xe018
jmp 0xbb7f
leaq -0x40(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0xe018
jmp 0xbb7f
movq -0x88(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9bf4
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 0xbc47
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 0xbc29
movq -0x38(%rbp), %rax
leaq -0x4(%rax), %rsi
movq %rsi, -0x38(%rbp)
movl $0x2, -0xa8(%rbp)
cmpq -0x30(%rbp), %rsi
je 0xbc70
movl $0x2, -0x4(%rax)
movq %rax, -0x38(%rbp)
jmp 0xbc80
leaq -0x40(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0xe018
movq %r14, -0xa8(%rbp)
movq -0x78(%rbp), %rsi
cmpq -0x70(%rbp), %rsi
je 0xbc9b
movq %r14, (%rsi)
addq $0x8, -0x78(%rbp)
jmp 0xbcab
leaq -0x80(%rbp), %rdi
leaq -0xa8(%rbp), %rdx
callq 0x9fcc
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xbb7f
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbb7f
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0xbcf7
cmpl $0x2, -0x4(%rax)
jne 0xbcf7
addq $-0x4, %rax
movq -0x78(%rbp), %rcx
movq -0x8(%rcx), %rbx
addq $-0x8, %rcx
movq %rcx, -0x78(%rbp)
movq %rax, -0x38(%rbp)
jmp 0xbcf9
xorl %ebx, %ebx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0xbd0e
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xbd23
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xbdb0
jmp 0xbdb0
jmp 0xbd92
jmp 0xbd92
jmp 0xbdb0
jmp 0xbdb0
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
leaq -0xb8(%rbp), %rax
cmpq %rax, %rdi
je 0xbd95
movq -0xb8(%rbp), %rsi
jmp 0xbd86
jmp 0xbd92
jmp 0xbdb0
jmp 0xbd92
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
leaq -0xd8(%rbp), %rax
cmpq %rax, %rdi
je 0xbd95
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbd95
jmp 0xbd92
movq %rax, %rbx
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xbdb3
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x31c0
jmp 0xbdb3
movq %rax, %rbx
movq -0x80(%rbp), %rdi
testq %rdi, %rdi
je 0xbdc8
movq -0x70(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq -0x40(%rbp), %rdi
testq %rdi, %rdi
je 0xbddd
movq -0x30(%rbp), %rsi
subq %rdi, %rsi
callq 0x31c0
movq %rbx, %rdi
callq 0x32e0
nop