// File: /workspace/github_c_makefile_star2_O1/MartinNovotny25[P]VUT-FIT-IFJ2020/semantics.c
// Function: isExpression
bool isExpression(TDLList *L, int *type, tBSTNodePtrLocal *node) {
    //tu sa ulozi typ prveho literalu/id
    int typ = 0;
    //tu sa ulozi typ dalsich literalov/id
    int typ2 = 0;
    bool isString = false;
    
    //za prvym literalom je eol alebo ';' alebo ','
    if (L->Act->rptr->tdata.type == t_EOL || L->Act->rptr->tdata.type == T_SEMICOLON || L->Act->rptr->tdata.type == t_COMMA) {
        return false;
    }
    //ulozim typ prveho literalu/id
    while (true) {
        //nasli sme prvy id
        if (L->Act->tdata.type == t_IDENTIFIER) {
            //overim ci je v symtable, ulozime jeho typ do typ
            if (MainStackSearch(mainstack, L->Act->tdata.lex, &typ)) {
                //nemoze to byt '_'
                if (!strcmp(L->Act->tdata.lex, "_")) {
                    error_call(ERR_SEM_UNDEF, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
                break;
            } else {
                //neni v symtable, volaj error
                error_call(ERR_SEM_UNDEF, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
        }
        //ak najdeme literal ulozim jeho typ do premennej typ
        if (L->Act->tdata.type == t_INT_ZERO) {
            typ = t_INT_ID;
            break;
        }
        if (L->Act->tdata.type == t_INT_NON_ZERO) {
            typ = t_INT_ID;
            break;
        }
        if (L->Act->tdata.type == t_STRING) {
            typ = t_STRING_ID;
            break;
        }
        if (L->Act->tdata.type == t_FLOAT) {
            typ = t_FLOAT64;
            break;
        }
        //ak narazime na eol, daco je zle
        if (L->Act->rptr->tdata.type == t_EOL) {
            break;
        }
        TDLLSucc(L);
    }
    //ak najdeme string, nastavime flag isString
    if (typ == t_STRING_ID) {
        isString = true;
    }
    
    
    //prehladam dalsie literaly/id
    //ak narazime na eol alebo ';', ukonci cyklus
    while (L->Act->tdata.type != t_EOL) {
        if (L->Act->tdata.type == T_SEMICOLON) {
            break;
        }
        if (L->Act->tdata.type == t_COMMA) {
            break;
        }
        //ak najdeme operator iny ako '+' a sme v retazcovom vyraze, je to chyba
        if (isString) {
            if (L->Act->tdata.type == t_MINUS ||
                L->Act->tdata.type == t_DIVIDE ||
                L->Act->tdata.type == t_MULTIPLY) {
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
        }
        //najdeme relacny operator
        if (L->Act->tdata.type == t_LESS ||
            L->Act->tdata.type == t_GREATER ||
            L->Act->tdata.type == t_LESSOREQUAL ||
            L->Act->tdata.type == t_GREATEROREQUAL ||
            L->Act->tdata.type == t_EQUAL) {
            error_call(ERR_SEM_EXCOMPAT, L);
            BSTDisposeGlobal(&functions);
            BSTDisposeLocal(node);
        }
        //nasli sme id
        if (L->Act->tdata.type == t_IDENTIFIER) {
            //nchadza sa v tabulke, do typ sa ulozi jeho typ
            if (MainStackSearch(mainstack, L->Act->tdata.lex, &typ2)) {
                //nesmie to byt '_'
                if (!strcmp(L->Act->tdata.lex, "_")) {
                    error_call(ERR_SEM_UNDEF, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
                //typ prveho a n-teho id sa nerovnaju
                if (typ != typ2) {
                    error_call(ERR_SEM_EXCOMPAT, L);
                    BSTDisposeGlobal(&functions);
                    BSTDisposeLocal(node);
                }
                TDLLSucc(L);
                continue;
            } else {
            error_call(ERR_SEM_EXCOMPAT, L);
            BSTDisposeGlobal(&functions);
            BSTDisposeLocal(node);
                
            }
            
        }
        //nasli sme literal t_INT_ZERO
        if (L->Act->tdata.type == t_INT_ZERO) {
            //porovname typy
            if (typ != t_INT_ID) {
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            TDLLSucc(L);
            continue;
        }
        if (L->Act->tdata.type == t_INT_NON_ZERO) {
            //porovname typy
            if (typ != t_INT_ID) {
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            TDLLSucc(L);
            continue;
        }
        if (L->Act->tdata.type == t_STRING) {
            //porovname typy
            if (typ != t_STRING_ID) {
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            TDLLSucc(L);
            continue;
        }if (L->Act->tdata.type == t_FLOAT) {
            //porovname typy
            if (typ != t_FLOAT64) {
                error_call(ERR_SEM_EXCOMPAT, L);
                BSTDisposeGlobal(&functions);
                BSTDisposeLocal(node);
            }
            TDLLSucc(L);
            continue;
        }
        TDLLSucc(L);
    }
    //vsetko v poriadku, vratim true a datovy typ
    *type = typ;
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f58, %rsp           # imm = 0x1F58
movq %rdx, %r14
movq %rdi, %r15
xorl %eax, %eax
movl %eax, 0x1f48(%rsp)
movl %eax, 0x1f4c(%rsp)
movq 0x8(%rdi), %rcx
movq 0x18(%rcx), %rcx
movl 0x8(%rcx), %ecx
movabsq $0x100800000000020, %rbx # imm = 0x100800000000020
cmpq $0x38, %rcx
ja 0x495b
btq %rcx, %rbx
jb 0x4c00
movq %rsi, 0x1f50(%rsp)
leaq 0x7b1e(%rip), %r12      # 0xc488
leaq 0x1f48(%rsp), %r13
leaq 0x9a57(%rip), %rbp      # 0xe3d0
movq 0x8(%r15), %rax
cmpl $0x13, 0x8(%rax)
jne 0x49c0
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r13, %rsi
callq 0x471e
testb %al, %al
jne 0x4a1d
movl $0x3, %edi
movq %r15, %rsi
callq 0x42c8
movq %rbp, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq 0x8(%r15), %rax
movl 0x8(%rax), %ecx
cmpl $0x28, %ecx
jg 0x49d8
cmpl $0x10, %ecx
je 0x49f6
cmpl $0x11, %ecx
jne 0x49e2
jmp 0x49f6
cmpl $0x29, %ecx
je 0x4a03
cmpl $0x2b, %ecx
je 0x4a10
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x4a55
movq %r15, %rdi
callq 0x1695
jmp 0x4979
movl $0x18, 0x1f48(%rsp)
jmp 0x4a55
movl $0x17, 0x1f48(%rsp)
jmp 0x4a55
movl $0x14, 0x1f48(%rsp)
jmp 0x4a55
movq 0x8(%r15), %rax
movq (%rax), %rdi
leaq 0x3aed(%rip), %rsi      # 0x8518
callq 0x1080
testl %eax, %eax
jne 0x4a55
movl $0x3, %edi
movq %r15, %rsi
callq 0x42c8
leaq 0x9988(%rip), %rdi      # 0xe3d0
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movl 0x1f48(%rsp), %r13d
leaq 0x996c(%rip), %r12      # 0xe3d0
movabsq $0x1c0000006, %rbp      # imm = 0x1C0000006
movq 0x8(%r15), %rax
movl 0x8(%rax), %eax
cmpq $0x38, %rax
ja 0x4a85
btq %rax, %rbx
jb 0x4bed
cmpl $0x14, %r13d
jne 0x4ab7
cmpl $0xb, %eax
ja 0x4ab7
movl $0xb00, %ecx            # imm = 0xB00
btl %eax, %ecx
jae 0x4ab7
movl $0x5, %edi
movq %r15, %rsi
callq 0x42c8
movq %r12, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq 0x8(%r15), %rax
movl 0x8(%rax), %eax
cmpq $0x20, %rax
ja 0x4ae7
btq %rax, %rbp
jae 0x4ae7
movl $0x5, %edi
movq %r15, %rsi
callq 0x42c8
movq %r12, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq 0x8(%r15), %rax
cmpl $0x13, 0x8(%rax)
jne 0x4b81
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x7981(%rip), %rsi      # 0xc488
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
leaq 0x1f4c(%rsp), %rsi
callq 0x471e
testb %al, %al
je 0x4b64
movq 0x8(%r15), %rax
movq (%rax), %rdi
leaq 0x39ec(%rip), %rsi      # 0x8518
callq 0x1080
testl %eax, %eax
jne 0x4b52
movl $0x3, %edi
movq %r15, %rsi
callq 0x42c8
movq %r12, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movl 0x1f48(%rsp), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4bb7
jmp 0x4bd4
movl $0x5, %edi
movq %r15, %rsi
callq 0x42c8
movq %r12, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq 0x8(%r15), %rax
movl 0x8(%rax), %eax
cmpl $0x28, %eax
jg 0x4ba3
cmpl $0x10, %eax
je 0x4b97
cmpl $0x11, %eax
jne 0x4bd4
cmpl $0x18, 0x1f48(%rsp)
jne 0x4bb7
jmp 0x4bd4
cmpl $0x29, %eax
je 0x4be1
cmpl $0x2b, %eax
jne 0x4bd4
cmpl $0x14, 0x1f48(%rsp)
je 0x4bd4
movl $0x5, %edi
movq %r15, %rsi
callq 0x42c8
movq %r12, %rdi
callq 0x44f1
movq %r14, %rdi
callq 0x46ab
movq %r15, %rdi
callq 0x1695
jmp 0x4a6e
cmpl $0x17, 0x1f48(%rsp)
jne 0x4bb7
jmp 0x4bd4
movl 0x1f48(%rsp), %eax
movq 0x1f50(%rsp), %rcx
movl %eax, (%rcx)
movb $0x1, %al
addq $0x1f58, %rsp           # imm = 0x1F58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq