// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x1f90, %rsp           # imm = 0x1F90
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movb $0x0, -0x29(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x725f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x38, 0x8(%rax)
je 0x725f
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x5, 0x8(%rax)
jne 0x7268
movb $0x0, -0x1(%rbp)
jmp 0x76ab
jmp 0x726a
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x7319
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
leaq 0x91f6(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x38(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x72b0
jmp 0x72f4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x6301(%rip), %rsi      # 0xd5c3
callq 0x1070
cmpl $0x0, %eax
jne 0x72ef
movq -0x10(%rbp), %rsi
movl $0x3, %edi
callq 0x64e0
leaq 0xb0ef(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
jmp 0x7397
movq -0x10(%rbp), %rsi
movl $0x3, %edi
callq 0x64e0
leaq 0xb0c7(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
jmp 0x7319
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x10, 0x8(%rax)
jne 0x7330
movl $0x18, -0x24(%rbp)
jmp 0x7397
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x11, 0x8(%rax)
jne 0x7347
movl $0x18, -0x24(%rbp)
jmp 0x7397
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2b, 0x8(%rax)
jne 0x735e
movl $0x14, -0x24(%rbp)
jmp 0x7397
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x29, 0x8(%rax)
jne 0x7375
movl $0x17, -0x24(%rbp)
jmp 0x7397
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
cmpl $0x2f, 0x8(%rax)
jne 0x7389
jmp 0x7397
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x726a
cmpl $0x14, -0x24(%rbp)
jne 0x73a1
movb $0x1, -0x29(%rbp)
jmp 0x73a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2f, 0x8(%rax)
je 0x769e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x38, 0x8(%rax)
jne 0x73c8
jmp 0x769e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x5, 0x8(%rax)
jne 0x73db
jmp 0x769e
testb $0x1, -0x29(%rbp)
je 0x7430
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0xb, 0x8(%rax)
je 0x740b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x8, 0x8(%rax)
je 0x740b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x9, 0x8(%rax)
jne 0x742e
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xafb0(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
jmp 0x7430
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1, 0x8(%rax)
je 0x7476
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2, 0x8(%rax)
je 0x7476
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1e, 0x8(%rax)
je 0x7476
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1f, 0x8(%rax)
je 0x7476
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x20, 0x8(%rax)
jne 0x7499
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xaf45(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x13, 0x8(%rax)
jne 0x757c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
leaq 0x8fc7(%rip), %rsi      # 0x10488
movq %rsp, %rdi
movl $0x3e9, %ecx            # imm = 0x3E9
rep  movsq (%rsi), %es:(%rdi)
movq -0x40(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x6ec0
testb $0x1, %al
jne 0x74df
jmp 0x7557
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rdi
leaq 0x60d2(%rip), %rsi      # 0xd5c3
callq 0x1070
cmpl $0x0, %eax
jne 0x751e
movq -0x10(%rbp), %rsi
movl $0x3, %edi
callq 0x64e0
leaq 0xaec0(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
je 0x7549
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xae95(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xae64(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
jmp 0x757c
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x10, 0x8(%rax)
jne 0x75c1
cmpl $0x18, -0x24(%rbp)
je 0x75b3
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xae2b(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x11, 0x8(%rax)
jne 0x7606
cmpl $0x18, -0x24(%rbp)
je 0x75f8
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xade6(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x2b, 0x8(%rax)
jne 0x764b
cmpl $0x14, -0x24(%rbp)
je 0x763d
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xada1(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x29, 0x8(%rax)
jne 0x7690
cmpl $0x17, -0x24(%rbp)
je 0x7682
movq -0x10(%rbp), %rsi
movl $0x5, %edi
callq 0x64e0
leaq 0xad5c(%rip), %rdi      # 0x123d0
callq 0x6920
movq -0x20(%rbp), %rdi
callq 0x6d90
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movq -0x10(%rbp), %rdi
callq 0x12e0
jmp 0x73a3
movl -0x24(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x1f90, %rsp           # imm = 0x1F90
popq %rbp
retq
nopl (%rax)