// File: zhengwangsun[P]pas2c/semantics.cpp
// Function: factor(int)
int factor(int k)
{
	TreeNode tNode= tree.node[k];
	TreeNode childNode;
	childNode=tree.node[tNode.child[0]];
    int flag1;
    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)
        {
            cout<<"id is not defined where factor"<<endl;
            return -1;
        }
        else
        {
            code[currentLine].append(childNode.value);
            flag1 = factor1(tree.node[k].child[1], tree.node[id_position].value);
            
            if (flag1 == 10 && idType == 4)//factor'是( expr_list )，id是函数
            {
                return checkFunReturnType(id_position);
            }
            else if (flag1 == 10 && idType == 5)//id是过程，没返回值
            {
                return 0;//会不会return -1
            }
            else if (flag1 == 11 && idType == 1)//factor'是[expression]，id是int
            {
                return 1;//相当于整型数组
            }
            else if (flag1 == 11 && idType == 2)//factor'是[expression]，id是real
            {
                return 2;//相当于实数型数组
            }
            else if (flag1 == 11 && idType == 3)//factor'是[expression]，id是real
            {
                return 3;//相当于布尔型数组
            }
            else if (flag1 == 0 && idType != -1)
                return idType;
            else if (idType == 0 && flag1 != 0)//应该没有这种情况
                return flag1;
            else
            {
                cout<<"type not match where factor"<<endl;
                return -1;
            }
        }
    }
    else if (lookahead(k) == 119)//119是num
    {
    	code[currentLine].append(childNode.value);
        return tree.node[tree.node[k].child[0]].numType + 1;//1是int，2是real
    }
    else if (lookahead(k) == 102)//102是(
    {
    	code[currentLine].append("(");
        int temp = expression(tree.node[k].child[1],1);
        code[currentLine].append(")");
        return temp;
    }
    else if (lookahead(k) == 134)//134是not
    {
    	code[currentLine].append("!");
        return factor(tree.node[k].child[1]);
    }
    else if (lookahead(k) == 135)//135是true
    {
    	code[currentLine].append("true ");
        return 3;
    }
    else if (lookahead(k) == 136)//136false
    {
    	code[currentLine].append("false ");
        return 3;
    }
    else
    {
        cout<<"factor error!"<<endl;
        return -1;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movl %edi, -0x8(%rbp)
movslq -0x8(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb34d8(%rip), %rax     # 0xc6138
movq %rax, -0x1b0(%rbp)
leaq 0x8(%rax,%rcx), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x14920
leaq -0x158(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x149d0
movq -0x1b0(%rbp), %rax
movq -0x1a8(%rbp), %rdi
movslq -0xac(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
leaq 0x8(%rax,%rcx), %rsi
callq 0x14a10
jmp 0x12cb3
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1b4(%rbp)
jmp 0x12cc3
movl -0x1b4(%rbp), %eax
cmpl $0x65, %eax
jne 0x12fae
movslq -0x8(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb3454(%rip), %rax     # 0xc6138
movl 0xc(%rax,%rcx), %eax
movl %eax, -0x16c(%rbp)
movl $0xffffffff, -0x170(%rbp) # imm = 0xFFFFFFFF
movl -0x16c(%rbp), %edi
callq 0xe0d0
movl %eax, -0x1b8(%rbp)
jmp 0x12d0b
movl -0x1b8(%rbp), %eax
movl %eax, -0x170(%rbp)
cmpl $-0x1, -0x170(%rbp)
jne 0x12d7e
movq 0x132a1(%rip), %rdi     # 0x25fc8
leaq 0xbe30(%rip), %rsi      # 0x1eb5e
callq 0x3280
movq %rax, -0x1c0(%rbp)
jmp 0x12d3c
movq -0x1c0(%rbp), %rdi
movq 0x13266(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x12d51
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x174(%rbp)
jmp 0x13250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
jmp 0x13274
movslq 0xad89b(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xacc10(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x34a0
jmp 0x12da1
movslq -0x8(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb3385(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %ecx
movl %ecx, -0x1c4(%rbp)
movslq -0x16c(%rbp), %rcx
imulq $0xa8, %rcx, %rcx
leaq 0x88(%rax,%rcx), %rsi
leaq -0x198(%rbp), %rdi
callq 0x30e0
jmp 0x12de1
movl -0x1c4(%rbp), %edi
leaq -0x198(%rbp), %rsi
callq 0x134d0
movl %eax, -0x1c8(%rbp)
jmp 0x12dfb
movl -0x1c8(%rbp), %eax
movl %eax, -0x168(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x34c8
cmpl $0xa, -0x168(%rbp)
jne 0x12e73
cmpl $0x4, -0x170(%rbp)
jne 0x12e73
movl -0x16c(%rbp), %edi
callq 0x13b50
movl %eax, -0x1cc(%rbp)
jmp 0x12e38
movl -0x1cc(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x160(%rbp)
movl %eax, -0x164(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x34c8
jmp 0x13274
cmpl $0xa, -0x168(%rbp)
jne 0x12e9b
cmpl $0x5, -0x170(%rbp)
jne 0x12e9b
movl $0x0, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
cmpl $0xb, -0x168(%rbp)
jne 0x12ec3
cmpl $0x1, -0x170(%rbp)
jne 0x12ec3
movl $0x1, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
cmpl $0xb, -0x168(%rbp)
jne 0x12eeb
cmpl $0x2, -0x170(%rbp)
jne 0x12eeb
movl $0x2, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
cmpl $0xb, -0x168(%rbp)
jne 0x12f13
cmpl $0x3, -0x170(%rbp)
jne 0x12f13
movl $0x3, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
cmpl $0x0, -0x168(%rbp)
jne 0x12f3d
cmpl $-0x1, -0x170(%rbp)
je 0x12f3d
movl -0x170(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
cmpl $0x0, -0x170(%rbp)
jne 0x12f67
cmpl $0x0, -0x168(%rbp)
je 0x12f67
movl -0x168(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movq 0x1305a(%rip), %rdi     # 0x25fc8
leaq 0xbc08(%rip), %rsi      # 0x1eb7d
callq 0x3280
movq %rax, -0x1d8(%rbp)
jmp 0x12f83
movq -0x1d8(%rbp), %rdi
movq 0x1301f(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x12f98
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x174(%rbp)
jmp 0x13250
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1dc(%rbp)
jmp 0x12fbe
movl -0x1dc(%rbp), %eax
cmpl $0x77, %eax
jne 0x13039
movslq 0xad650(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac9c5(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq -0xd8(%rbp), %rsi
callq 0x34a0
jmp 0x12fec
movslq -0x8(%rbp), %rcx
leaq 0xb3141(%rip), %rax     # 0xc6138
addq $0x8, %rax
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rcx
leaq 0xb3128(%rip), %rax     # 0xc6138
addq $0x8, %rax
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movl 0xa0(%rax), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1e0(%rbp)
jmp 0x13049
movl -0x1e0(%rbp), %eax
cmpl $0x66, %eax
jne 0x130ea
movslq 0xad5c1(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac936(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xc0c2(%rip), %rsi      # 0x1f136
callq 0x3490
jmp 0x1307b
movslq -0x8(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb30ab(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %edi
movl $0x1, %esi
callq 0x11130
movl %eax, -0x1e4(%rbp)
jmp 0x130a3
movl -0x1e4(%rbp), %eax
movl %eax, -0x19c(%rbp)
movslq 0xad56a(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac8df(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xc231(%rip), %rsi      # 0x1f2fc
callq 0x3490
jmp 0x130d2
movl -0x19c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1e8(%rbp)
jmp 0x130fa
movl -0x1e8(%rbp), %eax
cmpl $0x86, %eax
jne 0x13165
movslq 0xad512(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac887(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xbb0a(%rip), %rsi      # 0x1ec2d
callq 0x3490
jmp 0x1312a
movslq -0x8(%rbp), %rax
imulq $0xa8, %rax, %rcx
leaq 0xb2ffc(%rip), %rax     # 0xc6138
movl 0x10(%rax,%rcx), %edi
callq 0x12c40
movl %eax, -0x1ec(%rbp)
jmp 0x1314d
movl -0x1ec(%rbp), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1f0(%rbp)
jmp 0x13175
movl -0x1f0(%rbp), %eax
cmpl $0x87, %eax
jne 0x131bb
movslq 0xad497(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac80c(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xb9fb(%rip), %rsi      # 0x1eb99
callq 0x3490
jmp 0x131a5
movl $0x3, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movl -0x8(%rbp), %edi
callq 0xca50
movl %eax, -0x1f4(%rbp)
jmp 0x131cb
movl -0x1f4(%rbp), %eax
cmpl $0x88, %eax
jne 0x1320e
movslq 0xad441(%rip), %rax     # 0xc0620
shlq $0x5, %rax
leaq 0xac7b6(%rip), %rdi     # 0xbf9a0
addq %rax, %rdi
leaq 0xb9ab(%rip), %rsi      # 0x1eb9f
callq 0x3490
jmp 0x131fb
movl $0x3, -0x4(%rbp)
movl $0x1, -0x174(%rbp)
jmp 0x13250
movq 0x12db3(%rip), %rdi     # 0x25fc8
leaq 0xb98a(%rip), %rsi      # 0x1eba6
callq 0x3280
movq %rax, -0x200(%rbp)
jmp 0x1322a
movq -0x200(%rbp), %rdi
movq 0x12d78(%rip), %rsi     # 0x25fb0
callq 0x32b0
jmp 0x1323f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl $0x1, -0x174(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x14a90
leaq -0xb0(%rbp), %rdi
callq 0x14a90
movl -0x4(%rbp), %eax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
leaq -0x158(%rbp), %rdi
callq 0x14a90
leaq -0xb0(%rbp), %rdi
callq 0x14a90
movq -0x160(%rbp), %rdi
callq 0x3450
nopl (%rax,%rax)