// File: /workspace/github_c_makefile_star2_O0/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: checkCallFunction
void checkCallFunction(TDLList *L, char *id, tBSTNodePtrLocal *node) {
    
    functionData data;
    //sem bude vracat hodnotu BSTSearchLoacal
    int type = 0;
    
    //volanu funkciu nieje mozne volat, lebo je prekryta identifikatorom premennej
    if (MainStackSearch(mainstack, id, &type)) {
        error_call(ERR_SEM_OTHER, L);
        BSTDisposeGlobal(&functions);
        BSTDisposeLocal(node);
    }
    
    //ak sa vola print, treba overit iba identifikatory v symtables
    if (!strcmp(id, "print")) {
        //ak najdeme pravu zatvorku, sme na konci volania
        while (L->Act->tdata.type != t_RIGHT_BRACKET) {
            TDLLSucc(L);
            //nasli sme id, treba overit v symtable
            if (L->Act->tdata.type == t_IDENTIFIER) {
                
                //if (BSTSearchLocal(*node, L->Act->tdata.lex, &type, unneeded)) {
                if (MainStackSearch(mainstack, L->Act->tdata.lex, &type)) {
                    return;
                } else {
                    error_call(ERR_SEM_RETURN, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
            }
            
        }
        return;
    }
    
    //sem bude vracat data BSTSearchGlobal
    if (BSTSearchGlobal(functions, id, &data)) {
        //v priprade existencie navratovych typov sa nesmie volat mimo priradenia
        if (data.numOfReturns > 0) {
            if (L->Act->lptr->tdata.type == t_EOL) {
                error_call(ERR_SEM_RETURN, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
        }
        
        for (int i = 0; i < data.numOfParams; i++) {
            //posun z '(' -> 'parameter' alebo ',' -> 'parameter'
            TDLLSucc(L);
            TDLLSucc(L);
            //ak je parameter id
            if (L->Act->tdata.type == t_IDENTIFIER) {
                //musi byt v lokalnej/nadlokalnej symtable
                //nesmie to byt '_'
                if (MainStackSearch(mainstack, L->Act->tdata.lex, &type) && (strcmp(L->Act->tdata.lex, "_"))) {
                    //jeho typ musi byt rovnaky ako typ parametru na pozicii i vo funkcii
                    if (type == data.paramsType[i]) {
                        //vsetko sedi, kontroluj dalsi parameter
                        continue;
                    } else {
                        error_call(ERR_SEM_RETURN, L);
                        BSTDisposeGlobal(&functions);
                        BSTDisposeLocal(node);
                    }
                } else {
                    error_call(ERR_SEM_RETURN, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
            } else {
                //parameter nie je identifikator, je napr konstanta, retazcovy literal...
                switch (L->Act->tdata.type) {
                    case t_INT_ZERO | t_INT_NON_ZERO:
                        if (data.paramsType[i] == t_INT_ID) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                    case t_STRING:
                        if (data.paramsType[i] == t_STRING_ID) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                    case t_FLOAT:
                        if (data.paramsType[i] == t_FLOAT64) {
                            //parameter v poriadku, kontroluj dalsi parameter
                            continue;
                        } else {
                            error_call(ERR_SEM_RETURN, L);
                            BSTDisposeGlobal(&functions);
                            BSTDisposeLocal(node);
                        }
                        break;
                        
                    default:
                        break;
                }
            }
        }
        //aktivny prvok by mal byt na poslednom predavanom parametry
        //ak vpravo od neho neni '(' znamena to nespravny pocet parametrov
        if (strcmp(L->Act->rptr->tdata.lex, ")")) {
            //fix pre pripad nula parametrov
            if (L->Act->tdata.type == t_IDENTIFIER && L->Act->rptr->rptr->tdata.type == t_RIGHT_BRACKET) {
                return;
            }
            error_call(ERR_SEM_RETURN, L);
        }
    }else{
        error_call(ERR_SEM_UNDEF, L);
        BSTDisposeGlobal(&functions);
        BSTDisposeLocal(node);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f90, %rsp           # imm = 0x2F90
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x1024(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1030(%rbp)
leaq 0x8a85(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x1030(%rbp), %rdi
leaq -0x1024(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x7a27
jmp 0x7a4a
movq -0x8(%rbp), %rsi
movl $0x7, %edi
callq 0x64e0
leaq 0xa994(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
leaq 0x5b77(%rip), %rsi      # 0xd5cc
callq 0x1070
cmpl $0x0, %eax
jne 0x7aff
jmp 0x7a65
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x7, 0x8(%rax)
je 0x7afa
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x7af5
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x1038(%rbp)
leaq 0x89e1(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x1038(%rbp), %rdi
leaq -0x1024(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x7acb
jmp 0x7ad0
jmp 0x7dd7
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa8eb(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7af5
jmp 0x7a65
jmp 0x7dd7
movq 0xa8ca(%rip), %rdi      # 0x123d0
movq -0x10(%rbp), %rsi
leaq -0x1020(%rbp), %rdx
callq 0x66f0
testb $0x1, %al
jne 0x7b1f
jmp 0x7db4
cmpl $0x0, -0x101c(%rbp)
jle 0x7b5f
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x7b5d
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa881(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7b5f
movl $0x0, -0x1028(%rbp)
movl -0x1028(%rbp), %eax
cmpl -0x1020(%rbp), %eax
jge 0x7d5e
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rdi
callq 0x12e0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x7c60
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x1040(%rbp)
leaq 0x88d0(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x1040(%rbp), %rdi
leaq -0x1024(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x7bdc
jmp 0x7c38
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x59d5(%rip), %rsi      # 0xd5c3
callq 0x1070
cmpl $0x0, %eax
je 0x7c38
movl -0x1024(%rbp), %eax
movslq -0x1028(%rbp), %rcx
cmpl -0x818(%rbp,%rcx,4), %eax
jne 0x7c13
jmp 0x7d4a
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa7a8(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7c5b
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa783(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7d48
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x1044(%rbp)
subl $0x11, %eax
je 0x7c99
jmp 0x7c78
movl -0x1044(%rbp), %eax
subl $0x29, %eax
je 0x7d0c
jmp 0x7c89
movl -0x1044(%rbp), %eax
subl $0x2b, %eax
je 0x7cd4
jmp 0x7d44
movslq -0x1028(%rbp), %rax
cmpl $0x18, -0x818(%rbp,%rax,4)
jne 0x7caf
jmp 0x7d4a
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa70c(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7d46
movslq -0x1028(%rbp), %rax
cmpl $0x14, -0x818(%rbp,%rax,4)
jne 0x7ce7
jmp 0x7d4a
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa6d4(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7d46
movslq -0x1028(%rbp), %rax
cmpl $0x17, -0x818(%rbp,%rax,4)
jne 0x7d1f
jmp 0x7d4a
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
leaq 0xa69c(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
jmp 0x7d46
jmp 0x7d46
jmp 0x7d48
jmp 0x7d4a
movl -0x1028(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1028(%rbp)
jmp 0x7b69
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq (%rax), %rdi
leaq 0x585e(%rip), %rsi      # 0xd5d2
callq 0x1070
cmpl $0x0, %eax
je 0x7db2
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x7da4
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x7, 0x8(%rax)
jne 0x7da4
jmp 0x7dd7
movq -0x8(%rbp), %rsi
movl $0x6, %edi
callq 0x64e0
jmp 0x7dd7
movq -0x8(%rbp), %rsi
movl $0x3, %edi
callq 0x64e0
leaq 0xa607(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x18(%rbp), %rdi
callq 0x6d90
addq $0x2f90, %rsp           # imm = 0x2F90
popq %rbp
retq