// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f58, %rsp           # imm = 0x2F58
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x1f4c(%rsp), %rax
andl $0x0, (%rax)
leaq 0x7ea7(%rip), %rsi      # 0xc498
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %rax, %rsi
callq 0x3eb3
testb %al, %al
je 0x462a
pushq $0x7
popq %rdi
movq %rbx, %rsi
callq 0x3ac4
leaq 0x9dc3(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
leaq 0x3eee(%rip), %rsi      # 0x851f
movq %r15, %rdi
callq 0x1080
testl %eax, %eax
je 0x477c
movq 0x9d98(%rip), %rdi      # 0xe3e0
leaq 0x1f50(%rsp), %rdx
movq %r15, %rsi
callq 0x3b85
testb %al, %al
je 0x4824
cmpl $0x0, 0x1f54(%rsp)
jle 0x4697
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x4697
pushq $0x6
popq %rdi
movq %rbx, %rsi
callq 0x3ac4
leaq 0x9d56(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
xorl %r12d, %r12d
pushq $0x6
popq %rbp
leaq 0x9d3c(%rip), %r15      # 0xe3e0
leaq 0x1f4c(%rsp), %r13
movslq 0x1f50(%rsp), %rax
cmpq %rax, %r12
jge 0x47f1
movq %rbx, %rdi
callq 0x12c2
movq %rbx, %rdi
callq 0x12c2
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
cmpl $0x11, %ecx
je 0x476f
cmpl $0x2b, %ecx
je 0x4735
cmpl $0x29, %ecx
je 0x4762
cmpl $0x13, %ecx
jne 0x475a
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x7d9a(%rip), %rsi      # 0xc498
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r13, %rsi
callq 0x3eb3
testb %al, %al
je 0x4740
movq 0x8(%rbx), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4722
cmpb $0x0, 0x1(%rax)
je 0x4740
movl 0x1f4c(%rsp), %eax
cmpl 0x2758(%rsp,%r12,4), %eax
jne 0x4740
jmp 0x475a
cmpl $0x14, 0x2758(%rsp,%r12,4)
je 0x475a
movl %ebp, %edi
movq %rbx, %rsi
callq 0x3ac4
movq %r15, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
incq %r12
jmp 0x46ac
cmpl $0x17, 0x2758(%rsp,%r12,4)
jne 0x4740
jmp 0x475a
cmpl $0x18, 0x2758(%rsp,%r12,4)
jne 0x4740
jmp 0x475a
leaq 0x7d15(%rip), %r15      # 0xc498
leaq 0x1f4c(%rsp), %r12
pushq $0x6
popq %rbp
leaq 0x9c4b(%rip), %r13      # 0xe3e0
movq 0x8(%rbx), %rax
cmpl $0x7, 0x8(%rax)
je 0x4843
movq %rbx, %rdi
callq 0x12c2
movq 0x8(%rbx), %rax
cmpl $0x13, 0x8(%rax)
jne 0x4795
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x3eb3
testb %al, %al
jne 0x4843
movl %ebp, %edi
movq %rbx, %rsi
callq 0x3ac4
movq %r13, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
jmp 0x4795
movq 0x8(%rbx), %rcx
movq 0x18(%rcx), %rax
movq (%rax), %rdx
cmpb $0x29, (%rdx)
jne 0x4807
cmpb $0x0, 0x1(%rdx)
je 0x4843
cmpl $0x13, 0x8(%rcx)
jne 0x4817
movq 0x18(%rax), %rax
cmpl $0x7, 0x8(%rax)
je 0x4843
pushq $0x6
popq %rdi
movq %rbx, %rsi
callq 0x3ac4
jmp 0x4843
pushq $0x3
popq %rdi
movq %rbx, %rsi
callq 0x3ac4
leaq 0x9baa(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq