// File: /workspace/github_cpp_makefile_star2_O3/taurheim[P]AgNES/semanticAnalyzer.cpp
// Function: SemanticAnalyzer::parseNode(ASTNode*)
void SemanticAnalyzer::parseNode(ASTNode * node) {
    switch (node->nodeType) {
        case AST_DECLARATION:
        case AST_FUNCTION:
            {
                VarType varType;
                std::string functionName = "";
                for (auto i = node->children.begin(); i != node->children.end(); i++) {
                    ASTNode * childNode = *i;
                    // Is TypeNode
                    if (i == node->children.begin()) {
                        varType = ((TypeNode *)*i)->varType;
                    } else if(node->nodeType == AST_DECLARATION) {
                        // Is AST_VARDECLARATION 
                        IdentifierNode * idNode = (IdentifierNode *)(*i)->children[0];
                        std::string varName = idNode->name;
                        symbolTable->addVariable(varType, varName);
                    } else if (node->nodeType == AST_FUNCTION && i == node->children.begin() + 1) {
                        // Second child of AST_FUNCTION is an Identifier
                        IdentifierNode * idNode = (IdentifierNode *)(*i);
                        functionName = idNode->name;
                        symbolTable->addFunction(varType, functionName);
                    } else if (node->nodeType == AST_FUNCTION && childNode->nodeType == AST_PARAMETERLIST) {
                        // Open the scope for the body of the function
                        symbolTable->openScope(functionName);
                        parseNode(childNode);
                    } else {
                        parseNode(childNode);
                    }
                }
                break;
            }
        case AST_PARAMETERLIST:
            {
                for (auto i = node->children.begin(); i != node->children.end(); i++) {
                    ASTNode * childNode = *i; // Parameter
                    TypeNode * paramType = (TypeNode*) childNode->children[0];
                    IdentifierNode * paramIdentifier = (IdentifierNode*) childNode->children[1];

                    symbolTable->addParam(paramType->varType, paramIdentifier->name);
                }
                break;
            }
        case AST_FUNCTIONBODY:
            {
                for (auto & n : node->children) {
                    parseNode(n);
                }
                symbolTable->closeScope();
                break;
            }
        case AST_IDENTIFIER:
            {
                IdentifierNode * idNode= (IdentifierNode*) node;
                STEntry * entry = symbolTable->lookup(idNode->name);
                if(entry == nullptr) {
                    std::cout << "Identifier \"" << idNode->name << "\" is undefined." << std::endl;
                }
                break;
            }
        default:
            for (auto & n : node->children) {
                parseNode(n);
            }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r12
movl (%rsi), %eax
cmpl $0x6, %eax
jg 0xb16a
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0xb19e
movq %r12, %r14
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x7348(%rip), %rdx      # 0x122e7
leaq -0x50(%rbp), %rdi
movq %rdx, %rsi
callq 0x82f0
movq 0x8(%rbx), %r12
cmpq 0x10(%rbx), %r12
je 0xb14f
movq (%r12), %r15
movq 0x8(%rbx), %rax
cmpq %rax, %r12
je 0xb060
movl (%rbx), %ecx
cmpl $0x2, %ecx
je 0xb06c
cmpl $0x3, %ecx
jne 0xb0d2
movq 0x8(%r15), %rax
movq (%rax), %rax
leaq -0x60(%rbp), %rcx
movq %rcx, -0x70(%rbp)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x6906
movq 0x8(%r14), %r15
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
callq 0x6906
movq %r15, %rdi
movl -0x30(%rbp), %esi
movq %r13, %rdx
callq 0xbd92
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xb053
callq 0x3250
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
jmp 0xb137
movl 0x20(%r15), %eax
movl %eax, -0x30(%rbp)
jmp 0xb141
addq $0x8, %rax
cmpq %rax, %r12
je 0xb0df
cmpl $0x7, (%r15)
jne 0xb0d2
movq 0x8(%r14), %r13
leaq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x90(%rbp), %rdi
callq 0x6906
movq %r13, %rdi
leaq -0x90(%rbp), %rsi
callq 0xc052
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xb0c5
callq 0x3250
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf5c
jmp 0xb141
movq %r14, %rdi
movq %r15, %rsi
callq 0xaf5c
jmp 0xb141
addq $0x20, %r15
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x3180
leaq -0xb0(%rbp), %r13
movq 0x8(%r14), %r15
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r13, %rdi
callq 0x6906
movq %r15, %rdi
movl -0x30(%rbp), %esi
movq %r13, %rdx
callq 0xbf5e
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
cmpq %rax, %rdi
je 0xb141
callq 0x3250
addq $0x8, %r12
cmpq 0x10(%rbx), %r12
jne 0xafb9
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xb2e9
callq 0x3250
jmp 0xb2e9
cmpl $0x7, %eax
je 0xb268
cmpl $0x10, %eax
jne 0xb316
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rbx
cmpq %rbx, %r14
je 0xb2fb
movq (%r14), %rsi
movq %r12, %rdi
callq 0xaf5c
addq $0x8, %r14
jmp 0xb184
cmpl $0x4, %eax
jne 0xb316
movq 0x8(%r12), %r14
leaq -0x100(%rbp), %r12
movq %r12, -0x10(%r12)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
addq %rsi, %rdx
leaq -0x110(%rbp), %r15
movq %r15, %rdi
callq 0x6906
movq %r14, %rdi
movq %r15, %rsi
callq 0xc104
movq %rax, %r14
movq -0x110(%rbp), %rdi
cmpq %r12, %rdi
je 0xb1f1
callq 0x3250
testq %r14, %r14
jne 0xb2e9
movq 0xcdcf(%rip), %r14      # 0x17fd0
leaq 0x7431(%rip), %rsi      # 0x12639
movl $0xc, %edx
movq %r14, %rdi
callq 0x32d0
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdx
movq %r14, %rdi
callq 0x32d0
movq %rax, %rbx
leaq 0x7417(%rip), %rsi      # 0x12646
movl $0xf, %edx
movq %rax, %rdi
callq 0x32d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
jmp 0xb2e9
movq 0x8(%rbx), %r14
movq %rbx, -0x30(%rbp)
cmpq 0x10(%rbx), %r14
je 0xb2e9
leaq -0xe0(%rbp), %rbx
movq (%r14), %rax
movq 0x8(%rax), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %r12, %r15
movq 0x8(%r12), %r12
movl 0x20(%rcx), %r13d
movq %rbx, -0xf0(%rbp)
movq 0x20(%rax), %rsi
movq 0x28(%rax), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x6906
movq %r12, %rdi
movl %r13d, %esi
leaq -0xf0(%rbp), %rdx
callq 0xbfd8
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xb2d8
callq 0x3250
addq $0x8, %r14
movq -0x30(%rbp), %rax
cmpq 0x10(%rax), %r14
movq %r15, %r12
jne 0xb27d
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r12), %rdi
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xc0f2
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rbx
cmpq %rbx, %r14
je 0xb2e9
movq (%r14), %rsi
movq %r12, %rdi
callq 0xaf5c
addq $0x8, %r14
jmp 0xb31e
movq %rax, %r14
movq -0x110(%rbp), %rdi
cmpq %r12, %rdi
jne 0xb3b5
jmp 0xb3ba
movq %rax, %r14
jmp 0xb3ba
movq %rax, %r14
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
jmp 0xb399
movq %rax, %r14
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
jmp 0xb399
jmp 0xb3a5
jmp 0xb3a5
movq %rax, %r14
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xb391
callq 0x3250
jmp 0xb391
movq %rax, %r14
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xb3a8
callq 0x3250
jmp 0xb3a8
movq %rax, %r14
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xb3ba
callq 0x3250
movq %r14, %rdi
callq 0x3420
movq %rax, %r14
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
jne 0xb3b5
jmp 0xb3ba