// File: vssense[P]Compiler/src/front-end/translator.cpp
// Function: TranslateStatement(Node*, _IO_FILE*, unsigned long)
void TranslateStatement(Node* node, FILE* file, size_t num_tabs)
{
    assert(node);
    assert(file);

    SetTabs(file, num_tabs);

    switch (node->type)
    {
        case ASSG_TYPE :
        {
            if (node->left->type == ID_TYPE)
            {
                fprintf(file, "%s %s ", node->left->value.name, OPERATORS[ASSIGNMENT].rus_name);
            }
            else
            {
                fprintf(file, "can't interpret this construction");
            }
            
            TranslateExpression(node->right, file);
            fprintf(file, ";\n");
            break;
        }
        case DECL_TYPE :
        {
            fprintf(file, "%s %s %s ", OPERATORS[DECLARATOR].rus_name, node->left->value.name, OPERATORS[ASSIGNMENT].rus_name);
            TranslateExpression(node->right, file);
            fprintf(file, ";\n");
            break;
        }
        case COND_TYPE :
        {
            TranslateCondition(node, file, num_tabs);
            break;
        }
        case LOOP_TYPE :
        {
            TranslateLoop(node, file, num_tabs);
            break;
        }
        case CALL_TYPE :
        {
            if (!TranslateStdCall(node, file))
            {
                fprintf(file, "%s(", node->left->value.name);
                TranslateParams(node->right, file);
                fprintf(file, ");\n");
            }
            break;
        }
        case JUMP_TYPE :
        {
            fprintf(file, "%s ", OPERATORS[RET].rus_name);
            if (node->right != nullptr)
            {
                TranslateExpression(node->right, file);
            }
            fprintf(file, ";\n");
            break;
        }
        default :
        {
            fprintf(file, "can't interpret this construction ");
            break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x643d
jmp 0x645c
leaq 0xcb3(%rip), %rdi       # 0x70f7
leaq 0x1e59(%rip), %rsi      # 0x82a4
movl $0x78, %edx
leaq 0x1fd7(%rip), %rcx      # 0x842e
callq 0x2090
cmpq $0x0, -0x10(%rbp)
je 0x6465
jmp 0x6484
leaq 0x13d7(%rip), %rdi      # 0x7843
leaq 0x1e31(%rip), %rsi      # 0x82a4
movl $0x79, %edx
leaq 0x1faf(%rip), %rcx      # 0x842e
callq 0x2090
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x63b0
movq -0x8(%rbp), %rax
movl (%rax), %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x20(%rbp)
subl $0xc, %eax
ja 0x6647
movq -0x20(%rbp), %rax
leaq 0x1d8d(%rip), %rcx      # 0x8240
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
cmpl $0x4, (%rax)
jne 0x64f0
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rdx
movq 0x4520(%rip), %rcx      # 0xaa00
leaq 0x1f9c(%rip), %rsi      # 0x8483
movb $0x0, %al
callq 0x2140
jmp 0x6502
movq -0x10(%rbp), %rdi
leaq 0x1f63(%rip), %rsi      # 0x845e
movb $0x0, %al
callq 0x2140
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x6660
movq -0x10(%rbp), %rdi
leaq 0x17b1(%rip), %rsi      # 0x7ccf
movb $0x0, %al
callq 0x2140
jmp 0x6659
movq -0x10(%rbp), %rdi
movq 0x440b(%rip), %rdx      # 0xa940
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rcx
movq 0x44b8(%rip), %r8       # 0xaa00
leaq 0x1f31(%rip), %rsi      # 0x8480
movb $0x0, %al
callq 0x2140
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x6660
movq -0x10(%rbp), %rdi
leaq 0x175d(%rip), %rsi      # 0x7ccf
movb $0x0, %al
callq 0x2140
jmp 0x6659
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x6820
jmp 0x6659
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x6930
jmp 0x6659
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x69f0
testb $0x1, %al
jne 0x65fc
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rdx
leaq 0x1ddf(%rip), %rsi      # 0x83b1
movb $0x0, %al
callq 0x2140
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x6af0
movq -0x10(%rbp), %rdi
leaq 0x1e95(%rip), %rsi      # 0x848a
movb $0x0, %al
callq 0x2140
jmp 0x6659
movq -0x10(%rbp), %rdi
movq 0x4397(%rip), %rdx      # 0xa9a0
leaq 0x1e76(%rip), %rsi      # 0x8486
movb $0x0, %al
callq 0x2140
movq -0x8(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x6633
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rsi
callq 0x6660
movq -0x10(%rbp), %rdi
leaq 0x1691(%rip), %rsi      # 0x7ccf
movb $0x0, %al
callq 0x2140
jmp 0x6659
movq -0x10(%rbp), %rdi
leaq 0x1e3c(%rip), %rsi      # 0x848e
movb $0x0, %al
callq 0x2140
addq $0x20, %rsp
popq %rbp
retq
nop