// File: /workspace/github_c_makefile_star2_O0/masyagin1998[P]GCI/src/parser/parser.c
// Function: primary_expr_ast_read
static enum PARSER_CODES primary_expr_ast_read(struct PARSER*parser, struct PRIMARY_EXPR_AST**primary_expr)
{
    enum PARSER_CODES r;

    switch ((parser->tok)->token_type) {
    case TOKEN_TYPE_IDENT: {
        struct IDENT_AST*ident = create_ident_ast((parser->tok)->str_val,
                                                  (parser->tok)->frag.starting.line,
                                                  (parser->tok)->frag.starting.pos);
        token_free((parser->tok));
        lexer_next_token(parser->lexer, &(parser->tok));

        /* check, if name is part of variable or name is function name. */
        switch ((parser->tok)->token_type) {
        case TOKEN_TYPE_LPAREN: {
            struct FUNCTION_CALL_AST*function_call;

            r = function_call_ast_read(parser, &function_call, ident);
            if (r != PARSER_OK) {
                ident_ast_free(ident);
                goto err0;
            }
            
            (*primary_expr) = create_primary_expr_ast(function_call, AST_PRIMARY_EXPR_TYPE_FUNCTION_CALL);
            
            break;
        }
        default: {
            struct VARIABLE_AST*var_name;
            r = variable_ast_read(parser, &var_name, ident);
            if (r != PARSER_OK) {
                ident_ast_free(ident);
                goto err0;
            }

            (*primary_expr) = create_primary_expr_ast(var_name, AST_PRIMARY_EXPR_TYPE_VARIABLE);
            
            break;
        }
        }        
        
        break;
    }
    case TOKEN_TYPE_HAS_PROPERTY: {
        struct HAS_PROPERTY_EXPR_AST*has_property_expr;
        r = has_property_expr_ast_read(parser, &has_property_expr);
        if (r != PARSER_OK) {
            goto err0;
        }

        (*primary_expr) = create_primary_expr_ast(has_property_expr, AST_PRIMARY_EXPR_TYPE_HAS_PROPERTY);

        break;
    }        
    case TOKEN_TYPE_LEN: {
        struct LEN_EXPR_AST*len_expr;
        r = len_expr_ast_read(parser, &len_expr);
        if (r != PARSER_OK) {
            goto err0;
        }

        (*primary_expr) = create_primary_expr_ast(len_expr, AST_PRIMARY_EXPR_TYPE_LEN);
        
        break;
    }
    case TOKEN_TYPE_NUMBER: {
        struct NUMBER_AST*number;
        r = number_ast_read(parser, &number);
        if (r != PARSER_OK) {
            goto err0;
        }

        (*primary_expr) = create_primary_expr_ast(number, AST_PRIMARY_EXPR_TYPE_NUMBER);
        
        break;
    }
    case TOKEN_TYPE_LPAREN: {
        struct LOGICAL_OR_EXPR_AST*logical_or_expr;

        token_free((parser->tok));
        lexer_next_token(parser->lexer, &(parser->tok));

        r = logical_or_expr_ast_read(parser, &logical_or_expr);
        if (r != PARSER_OK) {
            goto err0;
        }

        if ((parser->tok)->token_type != TOKEN_TYPE_RPAREN) {
            logical_or_expr_ast_free(logical_or_expr);
            r = PARSER_INVALID_TOKEN;
            set_parser_error(parser, 1, TOKEN_TYPE_SEMI);
            goto err0;
        }        
        token_free((parser->tok));
        lexer_next_token(parser->lexer, &(parser->tok));

        (*primary_expr) = create_primary_expr_ast(logical_or_expr, AST_PRIMARY_EXPR_TYPE_LOGICAL_OR_EXPR);
        
        break;
    }
    default: {
        r = PARSER_INVALID_TOKEN;
        set_parser_error(parser, 5, TOKEN_TYPE_IDENT, TOKEN_TYPE_NUMBER, TOKEN_TYPE_LPAREN, TOKEN_TYPE_LEN, TOKEN_TYPE_HAS_PROPERTY);
        goto err0;
        break;
    }
    }

    return PARSER_OK;

 err0:
    (*primary_expr) = NULL;
    return r;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
addl $-0x9, %eax
movl %eax, %ecx
movq %rcx, -0x60(%rbp)
subl $0x14, %eax
ja 0x7ca5
movq -0x60(%rbp), %rax
leaq 0x10d9d(%rip), %rcx     # 0x18814
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, %rdi
addq $0x58, %rdi
movq 0x18(%rax), %rsi
movq 0x20(%rax), %rdx
callq 0xd670
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rsi
movq (%rsi), %rdi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movl (%rax), %eax
subl $0x1b, %eax
jne 0x7b10
jmp 0x7ace
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq -0x30(%rbp), %rsi
callq 0x4e30
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7af6
movq -0x28(%rbp), %rdi
callq 0x8b30
jmp 0x7ce4
movq -0x30(%rbp), %rdi
movl $0x2, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7b50
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq -0x38(%rbp), %rsi
callq 0x4f80
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7b38
movq -0x28(%rbp), %rdi
callq 0x8b30
jmp 0x7ce4
movq -0x38(%rbp), %rdi
movl $0x3, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7cdb
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x7d00
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7b70
jmp 0x7ce4
movq -0x40(%rbp), %rdi
xorl %esi, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7cdb
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x7f20
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7ba5
jmp 0x7ce4
movq -0x48(%rbp), %rdi
movl $0x1, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7cdb
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x80c0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7bdd
jmp 0x7ce4
movq -0x50(%rbp), %rdi
movl $0x4, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7cdb
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x10(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x64c0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
je 0x7c36
jmp 0x7ce4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1c, (%rax)
je 0x7c6a
movq -0x58(%rbp), %rdi
callq 0xa030
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x1, %esi
movl $0x23, %edx
movb $0x0, %al
callq 0x4030
jmp 0x7ce4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x31b0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x8, %rsi
callq 0x1fd0
movq -0x58(%rbp), %rdi
movl $0x5, %esi
callq 0xcd60
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
jmp 0x7cdb
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
movl $0x5, %esi
movl $0xc, %edx
movl $0x1d, %ecx
movl $0x1b, %r8d
movl $0xa, %r9d
movl $0x9, (%rsp)
movb $0x0, %al
callq 0x4030
jmp 0x7ce4
movl $0x0, -0x4(%rbp)
jmp 0x7cf5
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movl -0x1c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop