// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rdi, %r14
movabsq $0x800000004, %r12      # imm = 0x800000004
movabsq $0x100000004, %r15      # imm = 0x100000004
leaq 0x530c(%rip), %rax      # 0xc0cc
leaq 0x52f9(%rip), %rcx      # 0xc0c0
leaq 0x52fa(%rip), %rdx      # 0xc0c8
leaq 0x1b33(%rip), %rdi      # 0x8908
leaq 0x1ac0(%rip), %r8       # 0x889c
movl 0x4(%rsi), %r9d
leal -0x24(%r9), %r10d
cmpl $0x1a, %r10d
ja 0x6df4
movslq (%r8,%r10,4), %r10
addq %r8, %r10
jmpq *%r10
testl %r9d, %r9d
jne 0x6efd
movl (%rsi), %r11d
cmpl (%rax), %r11d
je 0x6fcc
cmpl (%rcx), %r11d
je 0x6ec7
cmpl (%rdx), %r11d
je 0x700e
cmpl $0x1a, %r10d
ja 0x6efd
movslq (%rdi,%r10,4), %r10
addq %rdi, %r10
jmpq *%r10
movq 0x28(%rsi), %rsi
movq 0x8(%rsi), %rsi
movq %r12, %r14
cmpl $0x38, %r9d
je 0x6ddc
movq %r15, %r14
cmpl $0x27, %r9d
jne 0x6ddc
movq %r12, %r14
jmp 0x6ddc
movq 0x28(%rsi), %rax
movq (%rax), %rsi
movq 0x10(%rax), %r14
cmpl $0x35, %r9d
ja 0x6ed2
movl %r9d, %eax
movabsq $0x3a780000000000, %rcx # imm = 0x3A780000000000
btq %rax, %rcx
jae 0x6ed2
movq %r15, %rdi
movq %rbx, %rdx
callq 0x6d91
testl %eax, %eax
je 0x6e99
leaq 0x1ea7(%rip), %rdi      # 0x8d2f
leaq 0x1bc7(%rip), %rsi      # 0x8a56
movl $0x23c, %edx            # imm = 0x23C
jmp 0x70c3
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6d91
testl %eax, %eax
je 0x70dc
leaq 0x1e79(%rip), %rdi      # 0x8d2f
leaq 0x1b99(%rip), %rsi      # 0x8a56
movl $0x241, %edx            # imm = 0x241
jmp 0x70c3
movq %r14, %rdi
movq %r15, %rsi
jmp 0x701b
movq %r12, %rdi
movq %rbx, %rdx
callq 0x6d91
testl %eax, %eax
je 0x702e
leaq 0x1e78(%rip), %rdi      # 0x8d64
leaq 0x1b63(%rip), %rsi      # 0x8a56
movl $0x24d, %edx            # imm = 0x24D
jmp 0x70c3
leaq 0x1f14(%rip), %rdi      # 0x8e18
leaq 0x1b4b(%rip), %rsi      # 0x8a56
movl $0x2ba, %edx            # imm = 0x2BA
jmp 0x70c3
movq 0x28(%rsi), %rax
movq (%rax), %rcx
movq 0x10(%rax), %r15
movq 0x8(%rcx), %rsi
leaq 0x1244d(%rip), %rax     # 0x19378
movq (%rax), %rdi
callq 0x63a5
testq %rax, %rax
je 0x7059
xorl %r13d, %r13d
cmpl $0x22, 0x4(%r15)
cmovneq %r15, %r13
movq %rax, -0x30(%rbp)
movq 0x10(%rax), %r12
testq %r12, %r12
je 0x706e
testq %r13, %r13
je 0x706e
movq 0x28(%r13), %rax
movq (%rax), %rsi
movq (%r12), %rdi
movq %rbx, %rdx
callq 0x6d91
movl %eax, %r15d
testl %eax, %eax
je 0x6f96
leaq 0x1e30(%rip), %rdi      # 0x8db3
leaq 0x1acc(%rip), %rsi      # 0x8a56
movl $0x295, %edx            # imm = 0x295
callq 0x4a54
jmp 0x6fbd
movq 0x18(%r12), %r12
cmpl $0x21, 0x4(%r13)
jne 0x6fba
movq 0x28(%r13), %rax
movq 0x10(%rax), %rax
cmpl $0x22, 0x4(%rax)
movl $0x0, %r13d
cmovneq %rax, %r13
jmp 0x6fbd
xorl %r13d, %r13d
movl $0x1, %eax
testl %r15d, %r15d
je 0x6f50
jmp 0x70cd
movq 0x8(%rsi), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0x6370
testq %rax, %rax
jne 0x6fed
movq 0x10(%rbx), %rdi
movq %r15, %rsi
callq 0x6370
testq %rax, %rax
jne 0x7004
leaq 0x12377(%rip), %rax     # 0x19370
movq (%rax), %rdi
movq %r15, %rsi
callq 0x6370
testq %rax, %rax
je 0x7088
movq (%rax), %rsi
jmp 0x7018
movabsq $0x200000004, %rsi      # imm = 0x200000004
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7159
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6d91
testl %eax, %eax
je 0x70dc
leaq 0x1d19(%rip), %rdi      # 0x8d64
leaq 0x1a04(%rip), %rsi      # 0x8a56
movl $0x252, %edx            # imm = 0x252
jmp 0x70c3
leaq 0x1d30(%rip), %rdi      # 0x8d90
leaq 0x19ef(%rip), %rsi      # 0x8a56
movl $0x27e, %edx            # imm = 0x27E
jmp 0x70c3
orq %r13, %r12
je 0x709d
leaq 0x1d50(%rip), %rdi      # 0x8dca
leaq 0x19d5(%rip), %rsi      # 0x8a56
movl $0x2a9, %edx            # imm = 0x2A9
jmp 0x70c3
leaq 0x1c7e(%rip), %rdi      # 0x8d0d
leaq 0x19c0(%rip), %rsi      # 0x8a56
movl $0x20f, %edx            # imm = 0x20F
jmp 0x70c3
movq -0x30(%rbp), %rax
movq (%rax), %rsi
movq %r14, %rdi
callq 0x7159
testl %eax, %eax
je 0x70dc
leaq 0x1d2b(%rip), %rdi      # 0x8de2
leaq 0x1998(%rip), %rsi      # 0x8a56
movl $0x2b0, %edx            # imm = 0x2B0
callq 0x4a54
movl $0x1, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x70cd