// File: /workspace/github_c_makefile_star2_O2/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
andl $0x0, 0x1f48(%rsp)
andl $0x0, 0x1f4c(%rsp)
movq 0x8(%rdi), %rax
movq 0x18(%rax), %rcx
movl 0x8(%rcx), %ecx
movabsq $0x100800000000020, %rdx # imm = 0x100800000000020
cmpq $0x38, %rcx
ja 0x40d0
btq %rcx, %rdx
jae 0x40d0
xorl %eax, %eax
jmp 0x436a
movq %rsi, 0x1f50(%rsp)
leaq 0x83b9(%rip), %rbx      # 0xc498
leaq 0x1f48(%rsp), %r12
pushq $0x3
popq %rbp
leaq 0xa2ef(%rip), %r13      # 0xe3e0
movl 0x8(%rax), %ecx
cmpl $0x13, %ecx
jne 0x413a
movq (%rax), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %r12, %rsi
callq 0x3eb3
testb %al, %al
jne 0x4193
movl %ebp, %edi
movq %r15, %rsi
callq 0x3ac4
movq %r13, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movq 0x8(%r15), %rax
movl 0x8(%rax), %ecx
cmpl $0x2b, %ecx
je 0x4173
cmpl $0x11, %ecx
je 0x4166
cmpl $0x29, %ecx
je 0x4183
cmpl $0x10, %ecx
je 0x4166
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x41c4
movq %r15, %rdi
callq 0x12c2
movq 0x8(%r15), %rax
jmp 0x40f1
movl $0x18, 0x1f48(%rsp)
jmp 0x418e
movl $0x14, 0x1f48(%rsp)
movb $0x1, %r13b
jmp 0x41d0
movl $0x17, 0x1f48(%rsp)
xorl %r13d, %r13d
jmp 0x41d0
movq 0x8(%r15), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x41c4
cmpb $0x0, 0x1(%rax)
jne 0x41c4
pushq $0x3
popq %rdi
movq %r15, %rsi
callq 0x3ac4
leaq 0xa229(%rip), %rdi      # 0xe3e0
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
cmpl $0x14, 0x1f48(%rsp)
sete %r13b
pushq $0x5
popq %rbp
leaq 0xa206(%rip), %r12      # 0xe3e0
movabsq $0x1c0000006, %rbx      # imm = 0x1C0000006
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpq $0x38, %rax
ja 0x4205
movabsq $0x100800000000020, %rdx # imm = 0x100800000000020
btq %rax, %rdx
jb 0x4357
testb %r13b, %r13b
je 0x423a
cmpl $0xb, %eax
ja 0x423a
movl $0xb00, %edx            # imm = 0xB00
btl %eax, %edx
jae 0x423a
movl %ebp, %edi
movq %r15, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpl $0x20, %eax
ja 0x4268
movl %eax, %edx
btq %rdx, %rbx
jae 0x4268
movl %ebp, %edi
movq %r15, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movq 0x8(%r15), %rcx
movl 0x8(%rcx), %eax
cmpl $0x13, %eax
jne 0x42fa
movq (%rcx), %rax
movl $0x3e9, %ecx            # imm = 0x3E9
movq %rsp, %rdi
leaq 0x8215(%rip), %rsi      # 0xc498
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
leaq 0x1f4c(%rsp), %rsi
callq 0x3eb3
testb %al, %al
je 0x42d9
movq 0x8(%r15), %rax
movq (%rax), %rax
cmpb $0x5f, (%rax)
jne 0x42c7
cmpb $0x0, 0x1(%rax)
jne 0x42c7
pushq $0x3
popq %rdi
movq %r15, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movl 0x1f48(%rsp), %eax
cmpl 0x1f4c(%rsp), %eax
jne 0x4318
jmp 0x4332
movl %ebp, %edi
movq %r15, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movq 0x8(%r15), %rax
movl 0x8(%rax), %eax
cmpl $0x2b, %eax
je 0x434b
cmpl $0x11, %eax
je 0x430e
cmpl $0x29, %eax
je 0x433f
cmpl $0x10, %eax
jne 0x4332
cmpl $0x18, 0x1f48(%rsp)
je 0x4332
movl %ebp, %edi
movq %r15, %rsi
callq 0x3ac4
movq %r12, %rdi
callq 0x3c99
movq %r14, %rdi
callq 0x3e46
movq %r15, %rdi
callq 0x12c2
jmp 0x41e4
cmpl $0x17, 0x1f48(%rsp)
jne 0x4318
jmp 0x4332
cmpl $0x14, 0x1f48(%rsp)
jne 0x4318
jmp 0x4332
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