// File: /workspace/github_cpp_makefile_star2_O2/iamlouk[P]asbi/src/parser.cc
// Function: asbi::Parser::parse_final()
ast::Node* Parser::parse_final() {
	tok::Token t = tokenizer.next();
	switch (t.type) {
	case tok::Int:
		return new ast::Number(static_cast<double>(t.integer));
	case tok::Real:
		return new ast::Number(t.real);
	case tok::Bool:
		return new ast::Bool(t.integer != 0 ? true : false);
	case tok::String:{
		auto str = new ast::String(ctx->new_stringconstant(*t.str));
		delete t.str;
		return str;
	}
	case tok::Identifier:{
		auto var = new ast::Variable(ctx->new_stringconstant(*t.str));
		delete t.str;
		return var;
	}
	case tok::Nil:
		return new ast::Nil();
	case tok::Colon:{
		t = tokenizer.next();
		if (t.type != tok::Identifier)
			throw utils::parser_error("expected identifier (symbol)", t);
		auto sym = new ast::Symbol(ctx->new_stringconstant(*t.str));
		delete t.str;
		return sym;
	}
	case tok::LeftBracket:{
		if (tokenizer.peek().type == tok::RightBracket) {
			tokenizer.next();
			return parse_lambda(std::vector<ast::Node*>());
		}

		auto node = parse_expr();
		t = tokenizer.next();
		if (t.type == tok::RightBracket) {
			if (tokenizer.peek().type == tok::Arrow) {
				std::vector<ast::Node*> vec = { node };
				return parse_lambda(vec);
			}
			return node;
		}

		std::vector<ast::Node*> vec = { node };
		while (t.type == tok::Comma) {
			if (tokenizer.peek().type == tok::RightBracket) {
				t = tokenizer.next();
				break;
			}
			vec.push_back(parse_expr());
			t = tokenizer.next();
		}

		if (t.type != tok::RightBracket)
			throw utils::parser_error("expected ',' or ')' while parsing tuple", t);

		if (tokenizer.peek().type == tok::Arrow)
			return parse_lambda(std::move(vec));

		throw utils::parser_error("tuples unimplemented", t);
	}
	case tok::LeftSquareBracket:{
		if (tokenizer.peek().type == tok::Comma) {
			tokenizer.next();
			expect(tok::RightSquareBracket);
			return new ast::List(std::vector<ast::Node*>());
		}

		if (tokenizer.peek().type == tok::KeyValue) {
			tokenizer.next();
			expect(tok::RightSquareBracket);
			return new ast::Map(std::vector<std::pair<ast::Node*,ast::Node*>>());
		}

		auto node = parse_expr();
		auto token = tokenizer.next();
		if (token.type == tok::KeyValue)
			return parse_map(node);

		std::vector<ast::Node*> vec = { node };
		while (token.type == tok::Comma) {
			vec.push_back(parse_expr());
			token = tokenizer.next();
		}

		if (token.type != tok::RightSquareBracket)
			throw utils::parser_error("expected ',' or ')' while parsing list", t);

		return new ast::List(std::move(vec));
	}
	case tok::LeftCurlyBracket:
		return parse_block();
	default:
		throw utils::parser_error("unexpected token", t);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
callq 0x66b4
movq %rax, %r14
movq %rdx, %r15
cmpl $0x5, %r14d
ja 0x8859
movl %r14d, %eax
leaq 0xbe83(%rip), %rcx      # 0x146b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0xec64
leaq 0x16dec(%rip), %rcx     # 0x1f640
jmp 0x89c5
leal -0x17(%r14), %eax
cmpl $0x8, %eax
ja 0x8d25
leaq 0xbe5b(%rip), %rcx      # 0x146c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %rbx, %rdi
callq 0x668a
movq %rbx, %rdi
cmpl $0x18, %eax
jne 0x8a2f
callq 0x66b4
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %rbx, %rdi
callq 0x8f64
movq %rax, %r14
leaq 0x80(%rsp), %rdi
jmp 0x8c9f
movq %rbx, %rdi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x80f4
movq %rbx, %rdi
callq 0x668a
movq %rbx, %rdi
cmpl $0x1d, %eax
jne 0x8aa0
callq 0x66b4
pushq $0x1c
popq %rsi
movq %rbx, %rdi
callq 0x796c
pushq $0x20
popq %rdi
callq 0x5390
movq %rax, %r14
xorpd %xmm0, %xmm0
leaq 0x60(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %rax, %rdi
callq 0x9418
leaq 0x60(%rsp), %rdi
jmp 0x8c9f
movq %rbx, %rdi
callq 0x66b4
movq %rax, %r12
movq %rdx, %r15
testl %r12d, %r12d
jne 0x8cef
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0xec64
leaq 0x16fb3(%rip), %rcx     # 0x1f910
jmp 0x89c5
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
leaq 0x16f0f(%rip), %rax     # 0x1f880
addq $0x10, %rax
testl %r15d, %r15d
movq %rax, (%r14)
setne 0x8(%r14)
jmp 0x8ca4
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
leaq 0x16ea1(%rip), %rax     # 0x1f838
addq $0x10, %rax
movq %rax, (%r14)
movq %r15, 0x8(%r14)
jmp 0x8ca4
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
movq 0x30(%rbx), %rdi
movq %r15, %rsi
callq 0xec64
leaq 0x16f03(%rip), %rcx     # 0x1f8c8
addq $0x10, %rcx
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x5170
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x53a0
jmp 0x8ca4
pushq $0x10
popq %rdi
callq 0x5390
movq %rax, %r14
cvtsi2sd %r15d, %xmm0
leaq 0x16e39(%rip), %rax     # 0x1f838
addq $0x10, %rax
movq %rax, (%r14)
movsd %xmm0, 0x8(%r14)
jmp 0x8ca4
pushq $0x8
popq %rdi
callq 0x5390
movq %rax, %r14
leaq 0x16dcd(%rip), %rax     # 0x1f7f0
addq $0x10, %rax
movq %rax, (%r14)
jmp 0x8ca4
callq 0x7b36
movq %rax, %r14
movq %rbx, %rdi
callq 0x66b4
movq %rax, %r15
cmpl $0x18, %r15d
jne 0x8af5
movq %rbx, %rdi
callq 0x668a
cmpl $0xc, %eax
jne 0x8ca4
leaq 0x8(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x7(%rsp), %rcx
callq 0x93e2
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x9318
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x8f64
movq %rax, %r14
leaq 0x28(%rsp), %rdi
jmp 0x8c95
callq 0x668a
movq %rbx, %rdi
cmpl $0xd, %eax
jne 0x8b5f
callq 0x66b4
pushq $0x1c
popq %rsi
movq %rbx, %rdi
callq 0x796c
pushq $0x20
popq %rdi
callq 0x5390
movq %rax, %r14
xorpd %xmm0, %xmm0
leaq 0x40(%rsp), %rsi
movapd %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movq %rax, %rdi
callq 0x9430
leaq 0x40(%rsp), %rdi
callq 0x95ea
jmp 0x8ca4
movq %rdx, %r12
leaq 0x8(%rsp), %rsi
movq %r14, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x7(%rsp), %rcx
callq 0x93e2
leaq 0x10(%rsp), %r14
leaq 0x8(%rsp), %r13
cmpl $0x1d, %r15d
jne 0x8c3c
movq %rbx, %rdi
callq 0x668a
cmpl $0x18, %eax
je 0x8c2e
movq %rbx, %rdi
callq 0x7b36
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x97fe
movq %rbx, %rdi
callq 0x66b4
movq %rax, %r15
movq %rdx, %r12
jmp 0x8b1c
callq 0x7b36
movq %rax, %r12
movq %rbx, %rdi
callq 0x66b4
movq %rax, %rbp
cmpl $0xd, %ebp
jne 0x8b93
movq %rbx, %rdi
movq %r12, %rsi
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x9184
leaq 0x8(%rsp), %rsi
movq %r12, (%rsi)
leaq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
leaq 0x7(%rsp), %rcx
callq 0x93e2
leaq 0x10(%rsp), %r12
leaq 0x8(%rsp), %r13
cmpl $0x1d, %ebp
jne 0x8be1
movq %rbx, %rdi
callq 0x7b36
movq %rax, 0x8(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x97fe
movq %rbx, %rdi
callq 0x66b4
movq %rax, %rbp
jmp 0x8bb7
cmpl $0x1c, %ebp
jne 0x8cb9
pushq $0x20
popq %rdi
callq 0x5390
movq %rax, %r14
movq 0x10(%rsp), %rax
leaq 0x98(%rsp), %rsi
movq %rax, (%rsi)
movups 0x18(%rsp), %xmm0
movups %xmm0, 0x8(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %r14, %rdi
callq 0x9418
leaq 0x98(%rsp), %rdi
jmp 0x8c95
movq %rbx, %rdi
callq 0x66b4
movq %rax, %r15
movq %rdx, %r12
cmpl $0x18, %r15d
jne 0x8d5b
movq %rbx, %rdi
callq 0x668a
cmpl $0xc, %eax
jne 0x8d7d
movq 0x10(%rsp), %rax
leaq 0xb0(%rsp), %rsi
movq %rax, (%rsi)
movupd 0x18(%rsp), %xmm0
movupd %xmm0, 0x8(%rsi)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movq %rbx, %rdi
callq 0x8f64
movq %rax, %r14
leaq 0xb0(%rsp), %rdi
callq 0x9768
leaq 0x10(%rsp), %rdi
callq 0x9768
movq %r14, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r12
leaq 0xbb68(%rip), %rsi      # 0x14833
movq %rax, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x9e26
leaq 0x16890(%rip), %rsi     # 0x1f570
leaq 0x611(%rip), %rdx       # 0x92f8
movq %r12, %rdi
callq 0x55f0
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0xbad8(%rip), %rsi      # 0x147d9
movq %rax, %rdi
movq %r12, %rdx
movq %r15, %rcx
callq 0x9e26
leaq 0x1685a(%rip), %rsi     # 0x1f570
leaq 0x5db(%rip), %rdx       # 0x92f8
movq %r14, %rdi
callq 0x55f0
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r12
leaq 0xb9cd(%rip), %rsi      # 0x14704
movq %rax, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x9e26
leaq 0x16824(%rip), %rsi     # 0x1f570
leaq 0x5a5(%rip), %rdx       # 0x92f8
movq %r12, %rdi
callq 0x55f0
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0xba89(%rip), %rsi      # 0x147f6
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x9e26
jmp 0x8d9d
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r14
leaq 0xba8f(%rip), %rsi      # 0x1481e
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x9e26
leaq 0x167cc(%rip), %rsi     # 0x1f570
leaq 0x54d(%rip), %rdx       # 0x92f8
movq %r14, %rdi
callq 0x55f0
jmp 0x8e80
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x9768
pushq $0x20
popq %rsi
movq %r14, %rdi
callq 0x53a0
jmp 0x8e83
jmp 0x8dda
movq %rax, %rbx
movq %r14, %rdi
jmp 0x8e6f
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x8e03
jmp 0x8e80
jmp 0x8e0e
jmp 0x8e0e
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x9768
jmp 0x8e83
jmp 0x8e80
jmp 0x8e80
movq %rax, %rbx
jmp 0x8e8d
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x95ea
jmp 0x8e44
movq %rax, %rbx
movq %r12, %rdi
jmp 0x8e30
movq %rax, %rbx
movq %r14, %rdi
callq 0x5290
jmp 0x8e8d
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x9768
pushq $0x20
jmp 0x8e5e
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
jmp 0x8e88
jmp 0x8e59
jmp 0x8e59
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x53a0
jmp 0x8e8d
movq %rax, %rbx
movq %r12, %rdi
callq 0x5290
jmp 0x8e83
jmp 0x8e80
jmp 0x8e80
jmp 0x8e80
jmp 0x8e80
jmp 0x8e80
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x9768
movq %rbx, %rdi
callq 0x5600
nop