// File: zhengwangsun[P]pas2c/semantics.cpp
// Function: statement(int)
void statement(int k)
{
	TreeNode tNode= tree.node[k];
	TreeNode childNode;
	childNode=tree.node[tNode.child[0]];
    if(lookahead(k) == 101)//101是id
    {
        int id_position = tree.node[k].child[0];//将id节点在树中的下标值赋给id_position
        int idType = -1;
        
        idType = checkIdType(id_position);
        
        if (idType != -1)//说明找到
        {
            string idValue=tree.node[id_position].value;

            code[currentLine].append(childNode.value);

            statement1(tree.node[k].child[1], idType, idValue);
            
        }
    }
    else if (lookahead(k) == 18)//18是compound_statement
    {
        compound_statement(tree.node[k].child[0]);
    }
    else if (lookahead(k) == 124)//124是if
    {
    	code[currentLine].append("if(");
        int ifFlag = expression(tree.node[k].child[1],1);
        
        if(ifFlag== 3)//如果表达式的值是布尔类型
        {
			code[currentLine++].append(")\n");
			code[currentLine++].append("{\n");

            statement(tree.node[k].child[3]);
            currentLine++;
			code[currentLine++].append("}\n");//}单独占一行 

			code[currentLine++].append("else\n");

			code[currentLine++].append("{\n");
			
            statement(tree.node[k].child[5]);
            code[currentLine++].append("}\n");//}单独占一行	
        }
        else
        {
            cout<<"if expresion type wrong where statement!"<<endl;
            exit(1);
        }
    }
    else if (lookahead(k) == 127)//127是while
    {
    	code[currentLine].append("while(");
        if (expression(tree.node[k].child[1],1) == 3)//如果表达式的值是布尔类型
        {
        	code[currentLine++].append(")\n");
			code[currentLine++].append("{\n");
            statement(tree.node[k].child[3]);
            code[currentLine++].append("}\n");
        }
        else
            cout<<"expresion type wrong where statement!"<<endl;
    }
    else if (lookahead(k) == 129)//117是read
    {
        identifier_list(-1, tree.node[k].child[2], 3);//statement调用是3
    }
    else if (lookahead(k) == 130)//130是write
    {
        expression_list(tree.node[k].child[2], 2, " ");
    }
    else
        cout<<"statement error!"<<endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl %edi, -0x4(%rbp)
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5d28(%rip), %rax     # 0xc6138
movq %rax, -0x1f0(%rbp)
leaq 0x8(%rax,%rcx), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x14920
leaq -0x158(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x149d0
movq -0x1f0(%rbp), %rax
movq -0x1e8(%rbp), %rdi
movslq -0xac(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
leaq 0x8(%rax,%rcx), %rsi
callq 0x14a10
jmp 0x10463
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x1f4(%rbp)
jmp 0x10473
movl -0x1f4(%rbp), %eax
cmpl $0x65, %eax
jne 0x105f2
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5ca4(%rip), %rax     # 0xc6138
movl 0xc(%rax,%rcx), %eax
movl %eax, -0x168(%rbp)
movl $0xffffffff, -0x16c(%rbp) # imm = 0xFFFFFFFF
movl -0x168(%rbp), %edi
callq 0xe0d0
movl %eax, -0x1f8(%rbp)
jmp 0x104bb
movl -0x1f8(%rbp), %eax
movl %eax, -0x16c(%rbp)
cmpl $-0x1, -0x16c(%rbp)
je 0x105ed
movslq -0x168(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5c4f(%rip), %rax     # 0xc6138
leaq 0x88(%rax,%rcx), %rsi
leaq -0x190(%rbp), %rdi
callq 0x30e0
jmp 0x104ff
movslq 0xb011a(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xaf48f(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x34a0
jmp 0x10522
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5c04(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %eax
movl %eax, -0x200(%rbp)
movl -0x16c(%rbp), %eax
movl %eax, -0x1fc(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x30e0
jmp 0x1055f
movl -0x1fc(%rbp), %esi
movl -0x200(%rbp), %edi
leaq -0x1b0(%rbp), %rdx
callq 0x10bf0
jmp 0x10579
leaq -0x1b0(%rbp), %rdi
callq 0x34c8
leaq -0x190(%rbp), %rdi
callq 0x34c8
jmp 0x105ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x10b27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x105dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x34c8
leaq -0x190(%rbp), %rdi
callq 0x34c8
jmp 0x10b27
jmp 0x10b06
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x204(%rbp)
jmp 0x10602
movl -0x204(%rbp), %eax
cmpl $0x12, %eax
jne 0x1062f
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5b19(%rip), %rax     # 0xc6138
movl 0xc(%rax,%rcx), %edi
callq 0xe870
jmp 0x1062a
jmp 0x10b04
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x208(%rbp)
jmp 0x1063f
movl -0x208(%rbp), %eax
cmpl $0x7c, %eax
jne 0x1084a
movslq 0xaffcb(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xaf340(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xe12d(%rip), %rsi      # 0x1e797
callq 0x3490
jmp 0x10671
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5ab5(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %edi
movl $0x1, %esi
callq 0x11130
movl %eax, -0x20c(%rbp)
jmp 0x10699
movl -0x20c(%rbp), %eax
movl %eax, -0x1b4(%rbp)
cmpl $0x3, -0x1b4(%rbp)
jne 0x1080a
movslq 0xaff67(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xaff5d(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf2d2(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdee5(%rip), %rsi      # 0x1e5bd
callq 0x3490
jmp 0x106df
movslq 0xaff3a(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xaff30(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf2a5(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdf0d(%rip), %rsi      # 0x1e612
callq 0x3490
jmp 0x1070c
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5a1a(%rip), %rax     # 0xc6138
movl 0x18(%rax,%rcx), %edi
callq 0x103f0
jmp 0x10729
movl 0xafef1(%rip), %eax     # 0xc0620
incl %eax
movl %eax, 0xafee9(%rip)     # 0xc0620
movslq 0xafee2(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafed8(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf24d(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xde6e(%rip), %rsi      # 0x1e5cb
callq 0x3490
jmp 0x10764
movslq 0xafeb5(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafeab(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf220(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xe011(%rip), %rsi      # 0x1e79b
callq 0x3490
jmp 0x10791
movslq 0xafe88(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafe7e(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf1f3(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xde5b(%rip), %rsi      # 0x1e612
callq 0x3490
jmp 0x107be
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5968(%rip), %rax     # 0xc6138
movl 0x20(%rax,%rcx), %edi
callq 0x103f0
jmp 0x107db
movslq 0xafe3e(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafe34(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf1a9(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xddca(%rip), %rsi      # 0x1e5cb
callq 0x3490
jmp 0x10808
jmp 0x10845
movq 0x157b7(%rip), %rdi     # 0x25fc8
leaq 0xdf89(%rip), %rsi      # 0x1e7a1
callq 0x3280
movq %rax, -0x218(%rbp)
jmp 0x10826
movq -0x218(%rbp), %rdi
movq 0x1577c(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x1083b
movl $0x1, %edi
callq 0x3340
jmp 0x10b02
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x21c(%rbp)
jmp 0x1085a
movl -0x21c(%rbp), %eax
cmpl $0x7f, %eax
jne 0x109a1
movslq 0xafdb0(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xaf125(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdf45(%rip), %rsi      # 0x1e7ca
callq 0x3490
jmp 0x1088c
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb589a(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %edi
movl $0x1, %esi
callq 0x11130
movl %eax, -0x220(%rbp)
jmp 0x108b4
movl -0x220(%rbp), %eax
cmpl $0x3, %eax
jne 0x10969
movslq 0xafd56(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafd4c(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf0c1(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdcd4(%rip), %rsi      # 0x1e5bd
callq 0x3490
jmp 0x108f0
movslq 0xafd29(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafd1f(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf094(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdcfc(%rip), %rsi      # 0x1e612
callq 0x3490
jmp 0x1091d
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5809(%rip), %rax     # 0xc6138
movl 0x18(%rax,%rcx), %edi
callq 0x103f0
jmp 0x1093a
movslq 0xafcdf(%rip), %rax     # 0xc0620
movl %eax, %ecx
incl %ecx
movl %ecx, 0xafcd5(%rip)     # 0xc0620
shlq $0x5, %rax
leaq 0xaf04a(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xdc6b(%rip), %rsi      # 0x1e5cb
callq 0x3490
jmp 0x10967
jmp 0x1099c
movq 0x15658(%rip), %rdi     # 0x25fc8
leaq 0xde2d(%rip), %rsi      # 0x1e7a4
callq 0x3280
movq %rax, -0x228(%rbp)
jmp 0x10985
movq -0x228(%rbp), %rdi
movq 0x1561d(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x1099a
jmp 0x1099c
jmp 0x10b00
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x22c(%rbp)
jmp 0x109b1
movl -0x22c(%rbp), %eax
cmpl $0x81, %eax
jne 0x109ea
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb5768(%rip), %rax     # 0xc6138
movl 0x14(%rax,%rcx), %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x3, %edx
callq 0xcd10
jmp 0x109e5
jmp 0x10afe
movl -0x4(%rbp), %edi
callq 0xca50
movl %eax, -0x230(%rbp)
jmp 0x109fa
movl -0x230(%rbp), %eax
cmpl $0x82, %eax
jne 0x10ac9
movslq -0x4(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb571b(%rip), %rax     # 0xc6138
movl 0x14(%rax,%rcx), %eax
movl %eax, -0x23c(%rbp)
leaq -0x1d9(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x3460
movq -0x238(%rbp), %rdx
leaq 0xdab3(%rip), %rsi      # 0x1e4fb
leaq -0x1d8(%rbp), %rdi
callq 0x8750
jmp 0x10a56
movl -0x23c(%rbp), %edi
movl $0x2, %esi
leaq -0x1d8(%rbp), %rdx
callq 0x112f0
jmp 0x10a6f
leaq -0x1d8(%rbp), %rdi
callq 0x34c8
leaq -0x1d9(%rbp), %rdi
callq 0x32d0
jmp 0x10afc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x10abb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x34c8
leaq -0x1d9(%rbp), %rdi
callq 0x32d0
jmp 0x10b27
movq 0x154f8(%rip), %rdi     # 0x25fc8
leaq 0xdc67(%rip), %rsi      # 0x1e73e
callq 0x3280
movq %rax, -0x248(%rbp)
jmp 0x10ae5
movq -0x248(%rbp), %rdi
movq 0x154bd(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x10afa
jmp 0x10afc
jmp 0x10afe
jmp 0x10b00
jmp 0x10b02
jmp 0x10b04
jmp 0x10b06
leaq -0x158(%rbp), %rdi
callq 0x14a90
leaq -0xb0(%rbp), %rdi
callq 0x14a90
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0x14a90
leaq -0xb0(%rbp), %rdi
callq 0x14a90
movq -0x160(%rbp), %rdi
callq 0x3450
nopl (%rax,%rax)