// File: vssense[P]Compiler/src/front-end/tree.cpp
// Function: GetPrimaryExpression(Parser*, unsigned long*)
Node* GetPrimaryExpression(Parser* parser, size_t* ofs)
{
    assert(parser);

    if (TYPE == TYPE_OP && VALUE.op == OPENING_ROUND_BRACKET)
    {
        CheckOpeningRoundBracket(parser, ofs);
        Node* tmp = GetExpression(parser, ofs);
        CheckClosingRoundBracket(parser, ofs);

        return tmp;
    }

    if (TYPE == TYPE_ID)
    {
        if (NEXT_TYPE == TYPE_OP && NEXT_VALUE.op == OPENING_ROUND_BRACKET) // call
        {
            Node* tmp = ID(nullptr, nullptr);
            return ConstructNode(CALL_TYPE, tmp, GetParams(parser, ofs));             
        }
        else if (NEXT_TYPE == TYPE_OP && NEXT_VALUE.op == OPENING_SQUARE_BRACKET)
        {
            return GetMemoryAccess(parser, ofs);
        }
        else
        {
            return ID(nullptr, nullptr);
        }
    }

    int val = VALUE.number;
    (*ofs)++;

    return ConstructNode(NUMB_TYPE, { .number = val });
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x92d9
jmp 0x92f8
leaq 0x228f(%rip), %rdi      # 0xb56f
leaq 0x2cd0(%rip), %rsi      # 0xbfb7
movl $0x184, %edx            # imm = 0x184
leaq 0x2f2a(%rip), %rcx      # 0xc21d
callq 0x20a0
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
cmpl $0x2, (%rax)
jne 0x935f
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
cmpl $0x13, 0x8(%rax)
jne 0x935f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x81d0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8bb0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8270
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x94fd
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
cmpl $0x1, (%rax)
jne 0x94bf
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, (%rax)
jne 0x942a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x13, 0x8(%rax)
jne 0x942a
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rsi
movl $0x4, %edi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x7980
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9510
movq -0x50(%rbp), %rsi
movq %rax, %rdx
movl $0xc, %edi
callq 0x7af0
movq %rax, -0x8(%rbp)
jmp 0x94fd
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x2, (%rax)
jne 0x947d
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
movq (%rcx), %rcx
addq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x15, 0x8(%rax)
jne 0x947d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9670
movq %rax, -0x8(%rbp)
jmp 0x94fd
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rdx
movq (%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, (%rdx)
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rsi
movl $0x4, %edi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x7980
movq %rax, -0x8(%rbp)
jmp 0x94fd
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x18(%rbp), %rcx
imulq $0x18, (%rcx), %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rcx
addq $0x1, %rcx
movq %rcx, (%rax)
movl -0x3c(%rbp), %eax
movl %eax, -0x48(%rbp)
movq -0x48(%rbp), %rsi
movl $0xf, %edi
callq 0x7b30
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)