// File: /workspace/github_c_makefile_star2_O0/gregtour[P]minimal-compiler/typechecker.c
// Function: CheckExpressionType
int CheckExpressionType(TYPE type, EXPR* expr, FUNC_SEGMENT* segment)
{
    AssertS(expr, "Attempting to check null expression.");

    // check tokens //
    if (expr->production == 0)
    {
        // <identifier>
        if (expr->token == gSymbolIdentifier)
        {
            const char* identifier = expr->string;
            VARLIST* variable;
            variable = GetVariableFromList(segment->locals, identifier);
            if (variable == NULL) {
                variable = GetVariableFromList(segment->params, identifier);
            } if (variable == NULL) {
                variable = GetVariableFromList(gPrgrmVariables, identifier);
            }

            if (variable == NULL)
            {
                CompilerError("Undefined variable in expression.");
                return 1;
            } else {
                return (CanCoerceToType(type, variable->type));
            }
        // <integer>
        } else if (expr->token == gSymbolInteger) {
            TYPE integer;
            integer.basic = TYPE_INTEGER;
            integer.size = 4;
            return (CanCoerceToType(type, integer));
        // <string>
        } else if (expr->token == gSymbolString) {
            TYPE string;
            string.basic = TYPE_STRING;
            string.size = 4;
            return (CanCoerceToType(type, string));
        }
    }

    // check booleans //
    if (expr->production == PROD_FINAL_BOOLEAN) {
        TYPE boolType;
        boolType.basic = TYPE_INTEGER;
        boolType.size = 4;
        return (CanCoerceToType(type, boolType));
    }

    // check binary operators //
    if (IsBinaryOperator(expr->production))
    {
        EXPR* subExprA;
        EXPR* subExprB;
        TYPE nonvoid;

        subExprA = (EXPR*)(expr->children[0]);
        subExprB = (EXPR*)(expr->children[2]);

        if (IsIntegerOnlyOperator(expr->production))
        {
            TYPE intType;
            intType.basic = TYPE_INTEGER;
            intType.size = 4;
            if (CheckExpressionType(intType, subExprA, segment) != 0)
            {
                CompilerError("Attempting to use non-integer in integer operations.");
                return 1;
            }
            if (CheckExpressionType(intType, subExprB, segment) != 0)
            {
                CompilerError("Attempting to use non-integer in integer operations.");
                return 1;
            }

            return 0;
        }

        nonvoid.basic = TYPE_NON_VOID;
        nonvoid.size = 4;

        if (CheckExpressionType(nonvoid, subExprA, segment) != 0)
        {
            CompilerError("Attempting to use void value in expression.");
            return 1;
        }
        if (CheckExpressionType(nonvoid, subExprB, segment) != 0)
        {
            CompilerError("Attempting to use void value in expression.");
            return 1;
        }

        return 0;
    }

    // check unary operators //
    if (IsUnaryOperator(expr->production))
    {
        TYPE nonvoid;
        TYPE integer;
        EXPR* subExpr = (EXPR*)(expr->children[1]);

        nonvoid.basic = TYPE_NON_VOID;
        nonvoid.size = 4;

        integer.basic = TYPE_INTEGER;
        integer.size = 4;

        if (expr->production == PROD_LOGIC_NOT
            || expr->production == PROD_FACTOR_NOT)
        {
            return CheckExpressionType(nonvoid, subExpr, segment);
        }

        return CheckExpressionType(integer, subExpr, segment);
    }

    // check function invocation //
    if (expr->production == PROD_FINAL_FUNCTION_CALL)
    {
        // <final> ::= <identifier> ( <arg*> )
        ARG_LIST* arguments;
        const char* identifier;
        FUNCTION* method;
        VARLIST* paramIterator;

        identifier = expr->children[0]->string;
        arguments = (ARG_LIST*)(expr->children[2]);

        method = GetFunctionFromList(gPrgrmFunctions, identifier);

        if (method == NULL) {
            CompilerError("Error calling undeclared function.");
            return 1;
        }

        // check argument types used as parameters, and the argument count
        if (arguments->production == PROD_ARG_STAR) {
            arguments = NULL;
        }
        // begin loop
        paramIterator = method->parameters;
        while (paramIterator && arguments)
        {
            EXPR* argExpr;
            TYPE  ptype;

            //<arg*> ::= <expr>
            //<arg*> ::= <expr> , <arg*>
            //<arg*> ::= <epsilon>
            argExpr = (EXPR*)(arguments->children[0]);
            ptype = paramIterator->type;

            if (CheckExpressionType(ptype, argExpr, segment) != 0)
            {
                CompilerError("Illegal argument type.");
                return 1;
            }

            // advance the parameter iterator
            paramIterator = paramIterator->next;
            // advance the argument iterator
            if (arguments->production == PROD_ARG_STAR_EXPR_B) {
                arguments = (ARG_LIST*)(arguments->children[2]);
                if (arguments->production == PROD_ARG_STAR) {
                    arguments = NULL;
                }
            } else {
                arguments = NULL;
            }
        }

        // check the argument vs. parameter count
        if (paramIterator != NULL || arguments != NULL)
        {
            CompilerError("Illegal argument count.");
            return 1;
        }

        // check function return type
        if (CanCoerceToType(type, method->type) != 0)
        {
            CompilerError("Type error: use of function with invalid return type.");
            return 1;
        }

        return 0;

    }

    // unhandled case : should throw an exception
    AssertS(0, "Control flow error.");
    CompilerError("Unexpected compiler state.");
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0xc(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0xa2b6
movq -0x18(%rbp), %rax
movl (%rax), %eax
leaq 0x4f1a(%rip), %rcx      # 0xf0bc
cmpl (%rcx), %eax
jne 0xa248
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x9260
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xa1e7
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x9260
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xa205
leaq 0x1216b(%rip), %rax     # 0x1c360
movq (%rax), %rdi
movq -0x28(%rbp), %rsi
callq 0x9260
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0xa230
leaq 0x19ef(%rip), %rdi      # 0xbc02
leaq 0x1707(%rip), %rsi      # 0xb921
movl $0x20f, %edx            # imm = 0x20F
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0x30(%rbp), %rax
movq -0xc(%rbp), %rdi
movq (%rax), %rsi
callq 0xa850
movl %eax, -0x4(%rbp)
jmp 0xa704
movq -0x18(%rbp), %rax
movl (%rax), %eax
leaq 0x4e5b(%rip), %rcx      # 0xf0b0
cmpl (%rcx), %eax
jne 0xa27c
movl $0x1, -0x34(%rbp)
movl $0x4, -0x38(%rbp)
movq -0xc(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xa850
movl %eax, -0x4(%rbp)
jmp 0xa704
movq -0x18(%rbp), %rax
movl (%rax), %eax
leaq 0x4e2f(%rip), %rcx      # 0xf0b8
cmpl (%rcx), %eax
jne 0xa2b0
movl $0x2, -0x3c(%rbp)
movl $0x4, -0x40(%rbp)
movq -0xc(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0xa850
movl %eax, -0x4(%rbp)
jmp 0xa704
jmp 0xa2b2
jmp 0xa2b4
jmp 0xa2b6
movq -0x18(%rbp), %rax
cmpl $0x3e, 0x4(%rax)
jne 0xa2e3
movl $0x1, -0x44(%rbp)
movl $0x4, -0x48(%rbp)
movq -0xc(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xa850
movl %eax, -0x4(%rbp)
jmp 0xa704
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edi
callq 0xa710
cmpl $0x0, %eax
je 0xa448
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edi
callq 0xa770
cmpl $0x0, %eax
je 0xa3ba
movl $0x1, -0x64(%rbp)
movl $0x4, -0x68(%rbp)
movq -0x50(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x68(%rbp), %rdi
callq 0xa170
cmpl $0x0, %eax
je 0xa374
leaq 0x18cd(%rip), %rdi      # 0xbc24
leaq 0x15c3(%rip), %rsi      # 0xb921
movl $0x23c, %edx            # imm = 0x23C
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x68(%rbp), %rdi
callq 0xa170
cmpl $0x0, %eax
je 0xa3ae
leaq 0x1893(%rip), %rdi      # 0xbc24
leaq 0x1589(%rip), %rsi      # 0xb921
movl $0x241, %edx            # imm = 0x241
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movl $0x0, -0x4(%rbp)
jmp 0xa704
movl $0x8, -0x5c(%rbp)
movl $0x4, -0x60(%rbp)
movq -0x50(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rdi
callq 0xa170
cmpl $0x0, %eax
je 0xa402
leaq 0x1874(%rip), %rdi      # 0xbc59
leaq 0x1535(%rip), %rsi      # 0xb921
movl $0x24d, %edx            # imm = 0x24D
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0x58(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rdi
callq 0xa170
cmpl $0x0, %eax
je 0xa43c
leaq 0x183a(%rip), %rdi      # 0xbc59
leaq 0x14fb(%rip), %rsi      # 0xb921
movl $0x252, %edx            # imm = 0x252
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movl $0x0, -0x4(%rbp)
jmp 0xa704
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edi
callq 0xa7c0
cmpl $0x0, %eax
je 0xa4cb
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x80(%rbp)
movl $0x8, -0x6c(%rbp)
movl $0x4, -0x70(%rbp)
movl $0x1, -0x74(%rbp)
movl $0x4, -0x78(%rbp)
movq -0x18(%rbp), %rax
cmpl $0x27, 0x4(%rax)
je 0xa499
movq -0x18(%rbp), %rax
cmpl $0x38, 0x4(%rax)
jne 0xa4b2
movq -0x80(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x70(%rbp), %rdi
callq 0xa170
movl %eax, -0x4(%rbp)
jmp 0xa704
movq -0x80(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x78(%rbp), %rdi
callq 0xa170
movl %eax, -0x4(%rbp)
jmp 0xa704
movq -0x18(%rbp), %rax
cmpl $0x3c, 0x4(%rax)
jne 0xa6e5
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
leaq 0x11e5f(%rip), %rax     # 0x1c368
movq (%rax), %rdi
movq -0x90(%rbp), %rsi
callq 0x92c0
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
jne 0xa54d
leaq 0x1755(%rip), %rdi      # 0xbc85
leaq 0x13ea(%rip), %rsi      # 0xb921
movl $0x27e, %edx            # imm = 0x27E
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0x88(%rbp), %rax
cmpl $0x22, 0x4(%rax)
jne 0xa565
movq $0x0, -0x88(%rbp)
movq -0x98(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xa0(%rbp)
xorl %eax, %eax
cmpq $0x0, -0xa0(%rbp)
movb %al, -0xb1(%rbp)
je 0xa59a
cmpq $0x0, -0x88(%rbp)
setne %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
testb $0x1, %al
jne 0xa5a9
jmp 0xa66e
movq -0x88(%rbp), %rax
movq 0x28(%rax), %rax
movq (%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0xb0(%rbp), %rdi
callq 0xa170
cmpl $0x0, %eax
je 0xa60f
leaq 0x16b6(%rip), %rdi      # 0xbca8
leaq 0x1328(%rip), %rsi      # 0xb921
movl $0x295, %edx            # imm = 0x295
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0xa0(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x21, 0x4(%rax)
jne 0xa65e
movq -0x88(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x22, 0x4(%rax)
jne 0xa65c
movq $0x0, -0x88(%rbp)
jmp 0xa669
movq $0x0, -0x88(%rbp)
jmp 0xa577
cmpq $0x0, -0xa0(%rbp)
jne 0xa682
cmpq $0x0, -0x88(%rbp)
je 0xa6a3
leaq 0x1636(%rip), %rdi      # 0xbcbf
leaq 0x1291(%rip), %rsi      # 0xb921
movl $0x2a9, %edx            # imm = 0x2A9
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movq -0x98(%rbp), %rax
movq -0xc(%rbp), %rdi
movq (%rax), %rsi
callq 0xa850
cmpl $0x0, %eax
je 0xa6dc
leaq 0x1615(%rip), %rdi      # 0xbcd7
leaq 0x1258(%rip), %rsi      # 0xb921
movl $0x2b0, %edx            # imm = 0x2B0
callq 0x6e20
movl $0x1, -0x4(%rbp)
jmp 0xa704
movl $0x0, -0x4(%rbp)
jmp 0xa704
leaq 0x1621(%rip), %rdi      # 0xbd0d
leaq 0x122e(%rip), %rsi      # 0xb921
movl $0x2ba, %edx            # imm = 0x2BA
callq 0x6e20
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq