// File: taurheim[P]AgNES/parser.cpp
// Function: Parser::statement()
ASTNode * Parser::statement() {
    /*
        "if" "(" <expression> ")" <statement> [ "else" <statement> ]
        | "while" "(" <expression> ")" <statement>
        | "for" "(" <assignment> ")" ";" <expression> ";" <assignment> ")" <statement>
        | "return" [<expression>] ";"
        | <assignment> ";"
        | <identifier> "(" [ <expression> {"," <expression> } ] ")" ";"
        | "{" {<statement>} "}"
        | ";"
    */
    StatementNode * node = new StatementNode();

    switch(t.type) {
        case T_NES_WAITFORFRAME: {
            next();
            node->type = STMT_NES_WAITFORFRAME;
            expect(T_SEMI);
            break;
        }
        case T_NES_SETSPRITEX: {
            next();
            node->type = STMT_NES_SETSPRITEX;
            node->children.push_back(identifier());
            expect(T_SEMI);
            break;
        }
        case T_NES_SETSPRITEY: {
            next();
            node->type = STMT_NES_SETSPRITEY;
            node->children.push_back(identifier());
            expect(T_SEMI);
            break;
        }
        case T_WHILE: {
            node->type = STMT_WHILE;
            next();
            expect(T_LPAREN);
            node->children.push_back(expression());
            expect(T_RPAREN);
            node->children.push_back(statement());
            break;
        }
        case T_IF: {
            node->type = STMT_IF;
            next();
            expect(T_LPAREN);
            node->children.push_back(expression());
            expect(T_RPAREN);
            node->children.push_back(statement());
            if (t.type == T_ELSE) {
                next();
                node->children.push_back(statement());
            }
            break;
        }
        case T_LCURLY: {
            node->type = STMT_BLOCK;
            next();
            while (t.type != T_RCURLY) {
                node->children.push_back(statement());
            }
            expect(T_RCURLY);
            break;
        }
        case T_RETURN: {
            node->type = STMT_RETURN;
            next();
            if (t.type != T_SEMI) {
                node->children.push_back(expression());
            }
            expect(T_SEMI);
            break;
        }
        case T_SEMI: {
            node->type = STMT_EMPTY;
            next();
            break;
        }
        case T_IDENT:
        {
            // Can be either an assignment or a function call
            // Check second character - if it's a "(" then it's a function call
            TokenType decider = lookahead(1);
            if (decider == T_LPAREN) {
                // Function call
                node->type = STMT_FUNCTIONCALL;
                node->children.push_back(identifier());
                expect(T_LPAREN);

                // First arg
                if(t.type != T_RPAREN) {
                    node->children.push_back(expression());
                }

                // 2.. args
                while (t.type != T_RPAREN) {
                    expect(T_COMMA);
                    node->children.push_back(expression());
                }

                expect(T_RPAREN);
                expect(T_SEMI);
            } else {
                // Assignment
                node->type = STMT_ASSIGN;
                node->children.push_back(assignment());
            }
            break;
        }
        default:
        {
            reject("statement not recognized.");
        }
    }

    return node;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x110(%rbp)
movl $0x28, %edi
callq 0x3300
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x108(%rbp)
callq 0x143d0
jmp 0x12158
movq -0x110(%rbp), %rax
movq -0x108(%rbp), %rcx
movq %rcx, -0x10(%rbp)
movl 0x18(%rax), %eax
addl $-0x3, %eax
movl %eax, %ecx
movq %rcx, -0x118(%rbp)
subl $0x1d, %eax
ja 0x126fd
movq -0x118(%rbp), %rax
leaq 0x12178(%rip), %rcx     # 0x24308
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x108(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x32c0
jmp 0x12798
movq -0x110(%rbp), %rdi
callq 0x114b0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x8, 0x20(%rax)
movl $0x3, -0x20(%rbp)
leaq -0x20(%rbp), %rsi
callq 0x11bc0
jmp 0x12759
movq -0x110(%rbp), %rdi
callq 0x114b0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x9, 0x20(%rax)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x120(%rbp)
callq 0x11e50
movq -0x120(%rbp), %rdi
movq %rax, -0x28(%rbp)
leaq -0x28(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x3, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x11bc0
jmp 0x12759
movq -0x110(%rbp), %rdi
callq 0x114b0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0xa, 0x20(%rax)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x128(%rbp)
callq 0x11e50
movq -0x128(%rbp), %rdi
movq %rax, -0x38(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x3, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x11bc0
jmp 0x12759
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x0, 0x20(%rax)
callq 0x114b0
movq -0x110(%rbp), %rdi
movl $0x4, -0x40(%rbp)
leaq -0x40(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x138(%rbp)
callq 0x129d0
movq -0x138(%rbp), %rdi
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x5, -0x4c(%rbp)
leaq -0x4c(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x130(%rbp)
callq 0x12120
movq -0x130(%rbp), %rdi
movq %rax, -0x58(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x14310
jmp 0x12759
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x2, 0x20(%rax)
callq 0x114b0
movq -0x110(%rbp), %rdi
movl $0x4, -0x5c(%rbp)
leaq -0x5c(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x148(%rbp)
callq 0x129d0
movq -0x148(%rbp), %rdi
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x5, -0x6c(%rbp)
leaq -0x6c(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x140(%rbp)
callq 0x12120
movq -0x140(%rbp), %rdi
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rax
cmpl $0x14, 0x18(%rax)
jne 0x12440
movq -0x110(%rbp), %rdi
callq 0x114b0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x150(%rbp)
callq 0x12120
movq -0x150(%rbp), %rdi
movq %rax, -0x80(%rbp)
leaq -0x80(%rbp), %rsi
callq 0x14310
jmp 0x12759
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x6, 0x20(%rax)
callq 0x114b0
movq -0x110(%rbp), %rax
cmpl $0x7, 0x18(%rax)
je 0x124a0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x158(%rbp)
callq 0x12120
movq -0x158(%rbp), %rdi
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rsi
callq 0x14310
jmp 0x1245c
movq -0x110(%rbp), %rdi
movl $0x7, -0x8c(%rbp)
leaq -0x8c(%rbp), %rsi
callq 0x11bc0
jmp 0x12759
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x3, 0x20(%rax)
callq 0x114b0
movq -0x110(%rbp), %rax
cmpl $0x3, 0x18(%rax)
je 0x1251b
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x160(%rbp)
callq 0x129d0
movq -0x160(%rbp), %rdi
movq %rax, -0x98(%rbp)
leaq -0x98(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x3, -0x9c(%rbp)
leaq -0x9c(%rbp), %rsi
callq 0x11bc0
jmp 0x12759
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x7, 0x20(%rax)
callq 0x114b0
jmp 0x12759
movq -0x110(%rbp), %rdi
movl $0x1, %esi
callq 0x11970
movl %eax, -0xa0(%rbp)
cmpl $0x4, -0xa0(%rbp)
jne 0x126bb
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x5, 0x20(%rax)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x168(%rbp)
callq 0x11e50
movq -0x168(%rbp), %rdi
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rsi
callq 0x14310
movq -0x110(%rbp), %rdi
movl $0x4, -0xac(%rbp)
leaq -0xac(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rax
cmpl $0x5, 0x18(%rax)
je 0x1261c
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x170(%rbp)
callq 0x129d0
movq -0x170(%rbp), %rdi
movq %rax, -0xb8(%rbp)
leaq -0xb8(%rbp), %rsi
callq 0x14310
jmp 0x1261e
movq -0x110(%rbp), %rax
cmpl $0x5, 0x18(%rax)
je 0x1267f
movq -0x110(%rbp), %rdi
movl $0xa, -0xbc(%rbp)
leaq -0xbc(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x178(%rbp)
callq 0x129d0
movq -0x178(%rbp), %rdi
movq %rax, -0xc8(%rbp)
leaq -0xc8(%rbp), %rsi
callq 0x14310
jmp 0x1261e
movq -0x110(%rbp), %rdi
movl $0x5, -0xcc(%rbp)
leaq -0xcc(%rbp), %rsi
callq 0x11bc0
movq -0x110(%rbp), %rdi
movl $0x3, -0xd0(%rbp)
leaq -0xd0(%rbp), %rsi
callq 0x11bc0
jmp 0x126fb
movq -0x110(%rbp), %rdi
movq -0x10(%rbp), %rax
movl $0x4, 0x20(%rax)
movq -0x10(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x180(%rbp)
callq 0x12d90
movq -0x180(%rbp), %rdi
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rsi
callq 0x14310
jmp 0x12759
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x3530
movq -0x188(%rbp), %rdx
leaq 0x11de7(%rip), %rsi     # 0x24505
leaq -0xf8(%rbp), %rdi
callq 0x33f0
jmp 0x1272c
movq -0x110(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x11650
jmp 0x12741
leaq -0xf8(%rbp), %rdi
callq 0x31d0
leaq -0xf9(%rbp), %rdi
callq 0x3350
movq -0x10(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1278c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x31d0
leaq -0xf9(%rbp), %rdi
callq 0x3350
movq -0x18(%rbp), %rdi
callq 0x3520
nopw %cs:(%rax,%rax)