// File: /workspace/github_cpp_makefile_star2_O2/iamlouk[P]asbi/src/parser.cc
// Function: asbi::Parser::parse_assign()
ast::Node* Parser::parse_assign() {
	auto left = parse_logic();
	if (tokenizer.peek().type == tok::Assign) {
		tokenizer.next();
		auto right = parse_expr();

		ast::Variable* var = dynamic_cast<ast::Variable*>(left);
		if (var != nullptr)
			return new ast::AssignVariable(var, right);

		ast::Access* acs = dynamic_cast<ast::Access*>(left);
		if (acs != nullptr)
			return new ast::AssignAccess(acs, right);

		throw utils::parser_error("one can only assign to variables or dict subscripts", left);
	}

	if (tokenizer.peek().type == tok::Declare) {
		auto tok = tokenizer.next();

		auto right = parse_expr();
		auto var = dynamic_cast<ast::Variable*>(left);
		if (var != nullptr) {
			std::vector<std::pair<ast::Variable*, ast::Node*>> decls;
			decls.push_back(std::make_pair(var, right));
			while (tokenizer.peek().type == tok::Comma) {
				tokenizer.next();
				auto node = parse_logic();
				auto var = dynamic_cast<ast::Variable*>(node);
				if (var == nullptr)
					throw utils::parser_error("expected Variable", node);

				ast::Node* val = nullptr;
				if (tokenizer.peek().type == tok::Declare) {
					tokenizer.next();
					val = parse_logic();
				}

				decls.push_back(std::make_pair(var, val));
			}

			return new ast::VariableDecl(std::move(decls));
		}

		ast::List* list = dynamic_cast<ast::List*>(left);
		if (list != nullptr) {
			auto node = new ast::DestructList(std::move(list->values), right);
			list->values.clear();
			delete list;
			return node;
		}

		ast::Map* map = dynamic_cast<ast::Map*>(left);
		if (map != nullptr) {
			/*std::vector<std::pair<ast::Variable*, ast::Node*>> vars;
			for (auto [key, val]: map->values) {
				auto var = dynamic_cast<ast::Variable*>(val);
				if (var == nullptr)
					throw utils::parser_error("only variables allowed in destructuring", left);

				vars.push_back(std::make_pair(var, key));
			}
			map->values.clear();
			delete map;
			return new ast::DestructMap(std::move(vars), right);*/

			throw utils::parser_error("unimplemented!", left);
		}

		throw utils::parser_error("one can only declare variables or destruct using ':='", tok /*left*/);
	}

	return left;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r14
callq 0x82ba
movq %rax, %rbx
movq %r14, %rdi
callq 0x668a
movq %r14, %rdi
cmpl $0x6, %eax
jne 0x7d6b
callq 0x66b4
movq %r14, %rdi
callq 0x7b36
testq %rbx, %rbx
je 0x7fd1
movq %rax, %r14
leaq 0x17851(%rip), %rsi     # 0x1f588
leaq 0x17932(%rip), %rdx     # 0x1f670
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
je 0x7e72
movq %rax, %r15
pushq $0x18
popq %rdi
callq 0x5390
movq %rax, %rbx
leaq 0x179fa(%rip), %rax     # 0x1f760
jmp 0x7ea8
callq 0x668a
cmpl $0x7, %eax
jne 0x7f8c
movq %r14, %rdi
callq 0x66b4
movq %rax, %r15
movq %rdx, %r12
movq %r14, %rdi
callq 0x7b36
testq %rbx, %rbx
je 0x8021
movq %rax, %r13
leaq 0x177e6(%rip), %rsi     # 0x1f588
leaq 0x178c7(%rip), %rdx     # 0x1f670
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
je 0x7f08
xorps %xmm0, %xmm0
movq %rsp, %rdi
andq $0x0, 0x10(%rdi)
movaps %xmm0, (%rdi)
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
movq %r13, 0x8(%rsi)
callq 0x993e
leaq 0x1788e(%rip), %r15     # 0x1f670
movq %rsp, %r12
leaq 0x18(%rsp), %r13
movq %r14, %rdi
callq 0x668a
cmpl $0x1d, %eax
jne 0x7ebc
movq %r14, %rdi
callq 0x66b4
movq %r14, %rdi
callq 0x82ba
movq %rax, %rbx
testq %rax, %rax
je 0x7f9e
movq %rbx, %rdi
leaq 0x17767(%rip), %rsi     # 0x1f588
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
je 0x7f9e
movq %rax, %rbp
movq %r14, %rdi
callq 0x668a
cmpl $0x7, %eax
jne 0x7e56
movq %r14, %rdi
callq 0x66b4
movq %r14, %rdi
callq 0x82ba
jmp 0x7e58
xorl %eax, %eax
movq %rbp, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x993e
jmp 0x7dea
leaq 0x1770f(%rip), %rsi     # 0x1f588
leaq 0x17838(%rip), %rdx     # 0x1f6b8
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
je 0x7fd1
movq %rax, %r15
pushq $0x18
popq %rdi
callq 0x5390
movq %rax, %rbx
leaq 0x17900(%rip), %rax     # 0x1f7a8
addq $0x10, %rax
movq %rax, (%rbx)
movq %r15, 0x8(%rbx)
movq %r14, 0x10(%rbx)
jmp 0x7f8c
pushq $0x20
popq %rdi
callq 0x5390
movq %rax, %rbx
movq (%rsp), %rax
leaq 0x40(%rsp), %rsi
movq %rax, (%rsi)
movups 0x8(%rsp), %xmm0
movups %xmm0, 0x8(%rsi)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq %rbx, %rdi
callq 0x937e
leaq 0x40(%rsp), %rdi
callq 0x9500
movq %rsp, %rdi
callq 0x9500
jmp 0x7f8c
leaq 0x17679(%rip), %rsi     # 0x1f588
leaq 0x17cc2(%rip), %rdx     # 0x1fbd8
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
je 0x8004
movq %rax, %r14
pushq $0x28
popq %rdi
callq 0x5390
movq %rax, %rbx
movq 0x8(%r14), %rax
leaq 0x28(%rsp), %rsi
movq %rax, (%rsi)
movq 0x10(%r14), %rax
movq %rax, 0x8(%rsi)
movq 0x18(%r14), %rax
movq %rax, 0x10(%rsi)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
andq $0x0, 0x18(%r14)
movq %rbx, %rdi
movq %r13, %rdx
callq 0x9396
leaq 0x28(%rsp), %rdi
callq 0x9768
movq 0x8(%r14), %rax
cmpq %rax, 0x10(%r14)
je 0x7f83
movq %rax, 0x10(%r14)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r15
leaq 0xc7ad(%rip), %rsi      # 0x1475d
movq %rax, %rdi
movq %rbx, %rdx
callq 0x9f2e
leaq 0x175ae(%rip), %rsi     # 0x1f570
leaq 0x132f(%rip), %rdx      # 0x92f8
movq %r15, %rdi
callq 0x55f0
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r15
leaq 0xc746(%rip), %rsi      # 0x14729
movq %rax, %rdi
movq %rbx, %rdx
callq 0x9f2e
leaq 0x1757b(%rip), %rsi     # 0x1f570
leaq 0x12fc(%rip), %rdx      # 0x92f8
movq %r15, %rdi
callq 0x55f0
leaq 0x1757d(%rip), %rsi     # 0x1f588
leaq 0x17bde(%rip), %rdx     # 0x1fbf0
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x53e0
testq %rax, %rax
jne 0x8057
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %rbx
leaq 0xc74b(%rip), %rsi      # 0x1477e
movq %rax, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x9e26
leaq 0x17528(%rip), %rsi     # 0x1f570
leaq 0x12a9(%rip), %rdx      # 0x92f8
movq %rbx, %rdi
callq 0x55f0
pushq $0x48
popq %rdi
callq 0x51b0
movq %rax, %r15
leaq 0xc706(%rip), %rsi      # 0x1476f
movq %rax, %rdi
movq %rbx, %rdx
callq 0x9f2e
jmp 0x7fee
jmp 0x80bb
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x9768
pushq $0x28
popq %rsi
movq %rbx, %rdi
callq 0x53a0
jmp 0x80ec
movq %rax, %r14
movq %rbx, %rdi
jmp 0x80c1
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x9500
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0x53a0
jmp 0x80e4
jmp 0x80e1
jmp 0x80e1
movq %rax, %r14
movq %r15, %rdi
callq 0x5290
jmp 0x80ec
jmp 0x80e1
jmp 0x80e1
jmp 0x80e1
movq %rax, %r14
movq %r15, %rdi
callq 0x5290
jmp 0x80e4
jmp 0x80e1
jmp 0x80e1
jmp 0x80e1
movq %rax, %r14
movq %rsp, %rdi
callq 0x9500
movq %r14, %rdi
callq 0x5600