// File: /workspace/github_c_makefile_star2_O2/naleksiev[P]fin/src/fin_ast.c
// Function: fin_ast_parse
fin_ast_module_t* fin_ast_parse(fin_ctx_t* ctx, const char* str) {
    fin_lex_t* lex = fin_lex_create(ctx->alloc, str);
    fin_str_t* name = fin_str_create(ctx, "<noname>", -1);

/*
    if (fin_lex_match(&lex, fin_lex_type_module)) {
    }
    while (fin_lex_match(lex, fin_lex_type_import)) {
    }
*/

    fin_ast_func_t*  funcs = NULL;
    fin_ast_func_t** func_tail = &funcs;

    fin_ast_enum_t*  enums = NULL;
    fin_ast_enum_t** enum_tail = &enums;

    fin_ast_type_t*  types = NULL;
    fin_ast_type_t** type_tail = &types;

    while (fin_lex_get_type(lex) != fin_lex_type_eof) {
        if (fin_lex_get_type(lex) == fin_lex_type_struct) {
            *type_tail = fin_ast_parse_type(ctx, lex);
            type_tail = &(*type_tail)->next;
        }
        else  if (fin_lex_get_type(lex) == fin_lex_type_enum) {
            *enum_tail = fin_ast_parse_enum(ctx, lex);
            enum_tail = &(*enum_tail)->next;
        }
        else {
            *func_tail = fin_ast_parse_func(ctx, lex);
            func_tail = &(*func_tail)->next;
        }
    }

    fin_lex_destroy(ctx->alloc, lex);

    fin_ast_module_t* module = (fin_ast_module_t*)ctx->alloc(NULL, sizeof(fin_ast_module_t));
    module->ctx = ctx;
    module->name = name;
    module->types = types;
    module->enums = enums;
    module->funcs = funcs;
    return module;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movq (%rdi), %rdi
callq 0x4f40
movq %rax, %r15
leaq 0x5c42(%rip), %rsi      # 0x90b8
pushq $-0x1
popq %rdx
movq %rbx, %rdi
callq 0x781e
movq %rax, -0x50(%rbp)
leaq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
andq $0x0, (%rax)
leaq -0x60(%rbp), %rax
movq %rax, -0x38(%rbp)
andq $0x0, (%rax)
leaq -0x58(%rbp), %rax
movq %rax, -0x40(%rbp)
andq $0x0, (%rax)
movq %r15, %rdi
callq 0x5559
cmpl $0x41, %eax
je 0x3769
movq %r15, %rdi
callq 0x5559
cmpl $0x3e, %eax
je 0x35cb
movq %r15, %rdi
callq 0x5559
movq (%rbx), %rcx
xorl %edi, %edi
cmpl $0x3f, %eax
je 0x368b
pushq $0x30
popq %rsi
callq *%rcx
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39ad
movq %rax, 0x8(%r12)
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r12)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x391a
movq %rax, 0x20(%r12)
movq %r15, %rdi
pushq $0x2e
popq %rsi
callq 0x38da
leaq 0x10(%r12), %r14
movq %r12, -0x30(%rbp)
andq $0x0, 0x10(%r12)
movq %r14, %r12
movq %r15, %rdi
pushq $0x2f
popq %rsi
callq 0x5537
testb %al, %al
jne 0x359c
cmpq $0x0, (%r14)
je 0x355d
movq %r15, %rdi
pushq $0x9
popq %rsi
callq 0x38da
xorl %edi, %edi
pushq $0x18
popq %rsi
callq *(%rbx)
movq %rax, %r13
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39ad
movq %rax, 0x8(%r13)
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r13)
andq $0x0, 0x10(%r13)
movq %r13, (%r12)
leaq 0x10(%r13), %r12
jmp 0x353d
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4511
movq -0x30(%rbp), %rcx
movq %rax, 0x18(%rcx)
andq $0x0, 0x28(%rcx)
movq -0x48(%rbp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
addq $0x28, %rax
movq %rax, -0x48(%rbp)
jmp 0x34a9
xorl %edi, %edi
pushq $0x20
popq %rsi
callq *(%rbx)
movq %rax, %r13
movq %r15, %rdi
pushq $0x3e
popq %rsi
callq 0x38da
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r13)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x391a
movq %rax, 0x10(%r13)
movq %r15, %rdi
pushq $0x32
popq %rsi
callq 0x38da
leaq 0x8(%r13), %r12
andq $0x0, 0x8(%r13)
movq %r15, %rdi
pushq $0x33
popq %rsi
callq 0x5537
testb %al, %al
jne 0x3672
xorl %edi, %edi
pushq $0x18
popq %rsi
callq *(%rbx)
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x39ad
movq %rax, 0x8(%r14)
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r14)
movq %r15, %rdi
pushq $0xc
popq %rsi
callq 0x38da
andq $0x0, 0x10(%r14)
movq %r14, (%r12)
leaq 0x10(%r14), %r12
jmp 0x361a
andq $0x0, 0x18(%r13)
movq -0x40(%rbp), %rax
movq %r13, (%rax)
addq $0x18, %r13
movq %r13, -0x40(%rbp)
jmp 0x34a9
pushq $0x18
popq %rsi
callq *%rcx
movq %rax, %r14
movq %r15, %rdi
pushq $0x3f
popq %rsi
callq 0x38da
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r14)
movq %r15, %rdi
pushq $0x32
popq %rsi
callq 0x38da
leaq 0x8(%r14), %r13
movq %r14, -0x30(%rbp)
andq $0x0, 0x8(%r14)
movq %r13, %r12
movq %r15, %rdi
pushq $0x33
popq %rsi
callq 0x5537
testb %al, %al
jne 0x3749
cmpq $0x0, (%r12)
je 0x36f0
movq %r15, %rdi
pushq $0x9
popq %rsi
callq 0x38da
xorl %edi, %edi
pushq $0x18
popq %rsi
callq *(%rbx)
movq %rax, %r14
movq %r15, %rdi
callq 0x55f7
movq %rbx, %rdi
movq %rax, %rsi
callq 0x781e
movq %rax, (%r14)
movq %r15, %rdi
pushq $0x10
popq %rsi
callq 0x5537
movl %eax, %ecx
movl $0x0, %eax
testb %cl, %cl
je 0x3733
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x3a1d
movq %rax, 0x8(%r14)
andq $0x0, 0x10(%r14)
movq %r14, (%r13)
movq %r14, %r13
addq $0x10, %r13
jmp 0x36cf
movq -0x30(%rbp), %rcx
andq $0x0, 0x10(%rcx)
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq %rcx, %rax
addq $0x10, %rax
movq %rax, -0x38(%rbp)
jmp 0x34a9
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x5528
pushq $0x28
popq %rsi
xorl %edi, %edi
callq *(%rbx)
movq %rbx, (%rax)
movq -0x50(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq -0x58(%rbp), %rcx
movq %rcx, 0x10(%rax)
movq -0x60(%rbp), %rcx
movq %rcx, 0x18(%rax)
movq -0x68(%rbp), %rcx
movq %rcx, 0x20(%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq