// File: /workspace/github_c_makefile_star2_O1/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
movl $0x0, (%rax)
leaq 0x75f2(%rip), %rsi      # 0xc488
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
rep  movsq (%rsi), %es:(%rdi)
movq %r15, %rdi
movq %rax, %rsi
callq 0x471e
testb %al, %al
je 0x4ed1
movl $0x7, %edi
movq %rbx, %rsi
callq 0x42c8
leaq 0x950c(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
leaq 0x3649(%rip), %rsi      # 0x8521
movq %r15, %rdi
callq 0x1080
testl %eax, %eax
je 0x5040
movq 0x94e1(%rip), %rdi      # 0xe3d0
leaq 0x1f50(%rsp), %rdx
movq %r15, %rsi
callq 0x43a8
testb %al, %al
je 0x50c3
cmpl $0x0, 0x1f54(%rsp)
jle 0x4f40
movq 0x8(%rbx), %rax
movq 0x10(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x4f40
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42c8
leaq 0x949d(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
cmpl $0x0, 0x1f50(%rsp)
jle 0x50e6
xorl %r13d, %r13d
leaq 0x7530(%rip), %r15      # 0xc488
leaq 0x1f4c(%rsp), %r12
leaq 0x9469(%rip), %rbp      # 0xe3d0
movq %rbx, %rdi
callq 0x1695
movq %rbx, %rdi
callq 0x1695
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
cmpl $0x28, %eax
jg 0x4fdb
cmpl $0x11, %eax
je 0x5026
cmpl $0x13, %eax
jne 0x500d
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r15, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x471e
testb %al, %al
je 0x4ff0
movq 0x8(%rbx), %rax
movq (%rax), %rdi
leaq 0x3559(%rip), %rsi      # 0x8518
callq 0x1080
testl %eax, %eax
je 0x4ff0
movl 0x1f4c(%rsp), %eax
cmpl 0x2758(%rsp,%r13,4), %eax
jne 0x4ff0
jmp 0x500d
cmpl $0x29, %eax
je 0x5033
cmpl $0x2b, %eax
jne 0x500d
cmpl $0x14, 0x2758(%rsp,%r13,4)
je 0x500d
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42c8
movq %rbp, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
incq %r13
movslq 0x1f50(%rsp), %rax
cmpq %rax, %r13
jl 0x4f67
jmp 0x50e6
cmpl $0x18, 0x2758(%rsp,%r13,4)
jne 0x4ff0
jmp 0x500d
cmpl $0x17, 0x2758(%rsp,%r13,4)
jne 0x4ff0
jmp 0x500d
movq 0x8(%rbx), %rax
cmpl $0x7, 0x8(%rax)
je 0x511f
leaq 0x7433(%rip), %r15      # 0xc488
leaq 0x1f4c(%rsp), %r12
leaq 0x936c(%rip), %r13      # 0xe3d0
movq %rbx, %rdi
callq 0x1695
movq 0x8(%rbx), %rax
cmpl $0x13, 0x8(%rax)
jne 0x50b7
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 0x471e
testb %al, %al
jne 0x511f
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42c8
movq %r13, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq 0x8(%rbx), %rax
cmpl $0x7, 0x8(%rax)
jne 0x5064
jmp 0x511f
movl $0x3, %edi
movq %rbx, %rsi
callq 0x42c8
leaq 0x92f9(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
jmp 0x511f
movq 0x8(%rbx), %r15
movq 0x18(%r15), %r14
movq (%r14), %rdi
leaq 0x342f(%rip), %rsi      # 0x8527
callq 0x1080
testl %eax, %eax
je 0x511f
cmpl $0x13, 0x8(%r15)
jne 0x5112
movq 0x18(%r14), %rax
cmpl $0x7, 0x8(%rax)
je 0x511f
movl $0x6, %edi
movq %rbx, %rsi
callq 0x42c8
addq $0x2f58, %rsp           # imm = 0x2F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq