// File: /workspace/github_cpp_makefile_star2_O1/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 0xb2c0
leal -0x2(%rax), %ecx
cmpl $0x2, %ecx
jae 0xb30f
movq %r12, -0x30(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x71f3(%rip), %rdx      # 0x122e7
leaq -0x50(%rbp), %rdi
movq %rdx, %rsi
callq 0x8354
movq 0x8(%rbx), %r12
cmpq 0x10(%rbx), %r12
je 0xb2f4
movq (%r12), %r15
movq 0x8(%rbx), %rax
cmpq %rax, %r12
je 0xb1be
movl (%rbx), %ecx
cmpl $0x3, %ecx
jne 0xb1c7
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 0x6992
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r15
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rdx
addq %rsi, %rdx
leaq -0xd0(%rbp), %r14
movq %r14, %rdi
callq 0x6992
movq %r15, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0xbf0e
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xb1a3
callq 0x3250
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xb24c
callq 0x3250
jmp 0xb24c
movl 0x20(%r15), %r13d
jmp 0xb24c
cmpl $0x2, %ecx
jne 0xb240
addq $0x8, %rax
cmpq %rax, %r12
je 0xb25f
cmpl $0x2, %ecx
jne 0xb240
cmpl $0x7, (%r15)
jne 0xb240
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r14
leaq -0x80(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
leaq -0x90(%rbp), %rdi
callq 0x6992
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
callq 0xc1d2
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
je 0xb232
callq 0x3250
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0xb0b0
jmp 0xb24c
movq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0xb0b0
addq $0x8, %r12
cmpq 0x10(%rbx), %r12
jne 0xb10e
jmp 0xb2f4
addq $0x20, %r15
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x3180
leaq -0xb0(%rbp), %r14
movq -0x30(%rbp), %rax
movq 0x8(%rax), %r15
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
addq %rsi, %rdx
movq %r14, %rdi
callq 0x6992
movq %r15, %rdi
movl %r13d, %esi
movq %r14, %rdx
callq 0xc0de
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
jmp 0xb1ab
cmpl $0x7, %eax
je 0xb3d9
cmpl $0x10, %eax
jne 0xb487
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rbx
cmpq %rbx, %r14
je 0xb46c
movq (%r14), %rsi
movq %r12, %rdi
callq 0xb0b0
addq $0x8, %r14
jmp 0xb2da
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xb45a
callq 0x3250
jmp 0xb45a
cmpl $0x4, %eax
jne 0xb487
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 0x6992
movq %r14, %rdi
movq %r15, %rsi
callq 0xc284
movq %rax, %r14
movq -0x110(%rbp), %rdi
cmpq %r12, %rdi
je 0xb362
callq 0x3250
testq %r14, %r14
jne 0xb45a
movq 0xcc5e(%rip), %r14      # 0x17fd0
leaq 0x72c0(%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 0x72a6(%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 0xb45a
movq 0x8(%rbx), %r14
movq %rbx, -0x30(%rbp)
cmpq 0x10(%rbx), %r14
je 0xb45a
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 0x6992
movq %r12, %rdi
movl %r13d, %esi
leaq -0xf0(%rbp), %rdx
callq 0xc158
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
je 0xb449
callq 0x3250
addq $0x8, %r14
movq -0x30(%rbp), %rax
cmpq 0x10(%rax), %r14
movq %r15, %r12
jne 0xb3ee
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 0xc272
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rbx
cmpq %rbx, %r14
je 0xb45a
movq (%r14), %rsi
movq %r12, %rdi
callq 0xb0b0
addq $0x8, %r14
jmp 0xb48f
movq %rax, %r14
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
jmp 0xb50f
movq %rax, %r14
movq -0x110(%rbp), %rdi
cmpq %r12, %rdi
jne 0xb526
jmp 0xb52b
movq %rax, %r14
jmp 0xb52b
movq %rax, %r14
movq -0xb0(%rbp), %rdi
leaq -0xa0(%rbp), %rax
jmp 0xb50f
jmp 0xb4e2
jmp 0xb4e2
movq %rax, %r14
jmp 0xb519
movq %rax, %r14
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0xb507
callq 0x3250
jmp 0xb507
movq %rax, %r14
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0xb519
callq 0x3250
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0xb52b
callq 0x3250
movq %r14, %rdi
callq 0x3420
movq %rax, %r14
movq -0xf0(%rbp), %rdi
cmpq %rbx, %rdi
jne 0xb526
jmp 0xb52b