// File: /workspace/github_c_makefile_star2_O2/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, %r14
movq %rdi, %rbx
movabsq $0x800000004, %r12      # imm = 0x800000004
movabsq $0x100000004, %r15      # imm = 0x100000004
leaq 0x4a16(%rip), %rax      # 0xb0cc
movl (%rax), %edx
leaq 0x4a01(%rip), %rax      # 0xb0c0
movl (%rax), %ecx
leaq 0x4a00(%rip), %rax      # 0xb0c8
movl (%rax), %eax
leaq 0x11cb(%rip), %rdi      # 0x789c
movl 0x4(%rsi), %r8d
leal -0x24(%r8), %r9d
cmpl $0x1a, %r9d
ja 0x678b
movslq (%rdi,%r9,4), %r9
addq %rdi, %r9
jmpq *%r9
movq 0x28(%rsi), %rsi
cmpl $0x38, %r8d
movq %r15, %rbx
cmoveq %r12, %rbx
movq 0x8(%rsi), %rsi
cmpl $0x27, %r8d
cmoveq %r12, %rbx
jmp 0x66d1
movq 0x28(%rsi), %rax
movq (%rax), %rsi
movq 0x10(%rax), %rbx
cmpl $0x35, %r8d
ja 0x67e7
movl %r8d, %eax
movabsq $0x3a780000000000, %rcx # imm = 0x3A780000000000
btq %rax, %rcx
jae 0x67e7
movq %r15, %rdi
movq %r14, %rdx
callq 0x6687
testl %eax, %eax
je 0x675d
leaq 0x1587(%rip), %rdi      # 0x7cd3
leaq 0x12a7(%rip), %rsi      # 0x79fa
movl $0x23c, %edx            # imm = 0x23C
jmp 0x6959
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6687
testl %eax, %eax
je 0x6994
leaq 0x1559(%rip), %rdi      # 0x7cd3
leaq 0x1279(%rip), %rsi      # 0x79fa
movl $0x241, %edx            # imm = 0x241
jmp 0x6959
testl %r8d, %r8d
jne 0x6946
movl (%rsi), %edi
cmpl %edx, %edi
jne 0x68e7
movq 0x8(%rsi), %r15
movq 0x18(%r14), %rdi
movq %r15, %rsi
callq 0x5d94
testq %rax, %rax
jne 0x67df
movq 0x10(%r14), %rdi
movq %r15, %rsi
callq 0x5d94
testq %rax, %rax
jne 0x67df
leaq 0x11ba5(%rip), %rax     # 0x18370
movq (%rax), %rdi
movq %r15, %rsi
callq 0x5d94
testq %rax, %rax
je 0x6998
movq (%rax), %rsi
jmp 0x6930
movq %r12, %rdi
movq %r14, %rdx
callq 0x6687
testl %eax, %eax
je 0x68a4
leaq 0x1507(%rip), %rdi      # 0x7d08
leaq 0x11f2(%rip), %rsi      # 0x79fa
movl $0x24d, %edx            # imm = 0x24D
jmp 0x6959
movq 0x28(%rsi), %rax
movq (%rax), %rcx
movq 0x10(%rax), %r12
movq 0x8(%rcx), %rsi
leaq 0x11b50(%rip), %rax     # 0x18378
movq (%rax), %rdi
callq 0x5dc4
testq %rax, %rax
je 0x68d2
movq %rax, %r15
xorl %eax, %eax
cmpl $0x22, 0x4(%r12)
cmovneq %r12, %rax
movq 0x10(%r15), %r12
movq %rax, %r13
testq %r12, %r12
je 0x68f3
testq %r13, %r13
je 0x68f3
movq 0x28(%r13), %rax
movq (%rax), %rsi
movq (%r12), %rdi
movq %r14, %rdx
callq 0x6687
testl %eax, %eax
jne 0x690d
movq 0x18(%r12), %r12
cmpl $0x21, 0x4(%r13)
movl $0x0, %eax
jne 0x684c
movq 0x28(%r13), %rax
movq 0x10(%rax), %rcx
cmpl $0x22, 0x4(%rcx)
movl $0x0, %eax
cmovneq %rcx, %rax
jmp 0x684c
movq %r12, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6687
testl %eax, %eax
je 0x6994
leaq 0x1447(%rip), %rdi      # 0x7d08
leaq 0x1132(%rip), %rsi      # 0x79fa
movl $0x252, %edx            # imm = 0x252
jmp 0x6959
leaq 0x145b(%rip), %rdi      # 0x7d34
leaq 0x111a(%rip), %rsi      # 0x79fa
movl $0x27e, %edx            # imm = 0x27E
jmp 0x6959
cmpl %ecx, %edi
jne 0x6922
movq %rbx, %rdi
movq %r15, %rsi
jmp 0x6933
orq %r13, %r12
je 0x6970
leaq 0x146f(%rip), %rdi      # 0x7d6e
leaq 0x10f4(%rip), %rsi      # 0x79fa
movl $0x2a9, %edx            # imm = 0x2A9
jmp 0x6959
leaq 0x1443(%rip), %rdi      # 0x7d57
leaq 0x10df(%rip), %rsi      # 0x79fa
movl $0x295, %edx            # imm = 0x295
jmp 0x6959
cmpl %eax, %edi
jne 0x6946
movabsq $0x200000004, %rsi      # imm = 0x200000004
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6a26
leaq 0x146f(%rip), %rdi      # 0x7dbc
leaq 0x10a6(%rip), %rsi      # 0x79fa
movl $0x2ba, %edx            # imm = 0x2BA
callq 0x4784
pushq $0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r15), %rsi
movq %rbx, %rdi
callq 0x6a26
testl %eax, %eax
je 0x6994
leaq 0x1400(%rip), %rdi      # 0x7d86
leaq 0x106d(%rip), %rsi      # 0x79fa
movl $0x2b0, %edx            # imm = 0x2B0
jmp 0x6959
xorl %eax, %eax
jmp 0x6961
leaq 0x1312(%rip), %rdi      # 0x7cb1
leaq 0x1054(%rip), %rsi      # 0x79fa
movl $0x20f, %edx            # imm = 0x20F
jmp 0x6959