// File: /workspace/github_c_makefile_star2_O3/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), %rdx
movl 0x8(%rdx), %edx
movabsq $0x100800000000020, %rbx # imm = 0x100800000000020
cmpq $0x38, %rdx
ja 0x48ef
btq %rdx, %rbx
jb 0x4b9f
movq %rsi, 0x1f50(%rsp)
leaq 0x7b8a(%rip), %r12      # 0xc488
leaq 0x1f48(%rsp), %r13
leaq 0x9ac3(%rip), %rbp      # 0xe3d0
movl 0x8(%rcx), %eax
cmpl $0x13, %eax
jne 0x495d
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r13, %rsi
callq 0x46d5
testb %al, %al
jne 0x49c0
movl $0x3, %edi
movq %r15, %rsi
callq 0x42d0
movq %rbp, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpl $0x28, %eax
jg 0x496e
cmpl $0x10, %eax
je 0x4993
cmpl $0x11, %eax
jne 0x4978
jmp 0x4993
cmpl $0x29, %eax
je 0x49a0
cmpl $0x2b, %eax
je 0x49b0
movq 0x18(%rcx), %rax
cmpl $0x2f, 0x8(%rax)
je 0x49f3
movq %r15, %rdi
callq 0x1691
movq 0x8(%r15), %rcx
jmp 0x490d
movl $0x18, 0x1f48(%rsp)
jmp 0x49ab
movl $0x17, 0x1f48(%rsp)
xorl %r13d, %r13d
jmp 0x49ff
movl $0x14, 0x1f48(%rsp)
movb $0x1, %r13b
jmp 0x49ff
movq 0x8(%r15), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x49f3
cmpb $0x0, 0x1(%rax)
jne 0x49f3
movl $0x3, %edi
movq %r15, %rsi
callq 0x42d0
leaq 0x99ea(%rip), %rdi      # 0xe3d0
callq 0x44a6
movq %r14, %rdi
callq 0x4662
cmpl $0x14, 0x1f48(%rsp)
sete %r13b
leaq 0x99ca(%rip), %r12      # 0xe3d0
movabsq $0x1c0000006, %rbp      # imm = 0x1C0000006
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpq $0x38, %rax
ja 0x4a27
btq %rax, %rbx
jb 0x4b8c
testb %r13b, %r13b
je 0x4a5f
cmpl $0xb, %eax
ja 0x4a5f
movl $0xb00, %edx            # imm = 0xB00
btl %eax, %edx
jae 0x4a5f
movl $0x5, %edi
movq %r15, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpl $0x20, %eax
ja 0x4a90
movl %eax, %edx
btq %rdx, %rbp
jae 0x4a90
movl $0x5, %edi
movq %r15, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpl $0x13, %eax
jne 0x4b27
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x79dd(%rip), %rsi      # 0xc488
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
leaq 0x1f4c(%rsp), %rsi
callq 0x46d5
testb %al, %al
je 0x4b03
movq 0x8(%r15), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x4af1
cmpb $0x0, 0x1(%rax)
jne 0x4af1
movl $0x3, %edi
movq %r15, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movl 0x1f48(%rsp), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4b56
jmp 0x4b73
movl $0x5, %edi
movq %r15, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq 0x8(%r15), %rax
movl 0x8(%rax), %eax
cmpl $0x28, %eax
jg 0x4b42
cmpl $0x10, %eax
je 0x4b36
cmpl $0x11, %eax
jne 0x4b73
cmpl $0x18, 0x1f48(%rsp)
jne 0x4b56
jmp 0x4b73
cmpl $0x29, %eax
je 0x4b80
cmpl $0x2b, %eax
jne 0x4b73
cmpl $0x14, 0x1f48(%rsp)
je 0x4b73
movl $0x5, %edi
movq %r15, %rsi
callq 0x42d0
movq %r12, %rdi
callq 0x44a6
movq %r14, %rdi
callq 0x4662
movq %r15, %rdi
callq 0x1691
jmp 0x4a10
cmpl $0x17, 0x1f48(%rsp)
jne 0x4b56
jmp 0x4b73
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