// File: /workspace/github_cpp_makefile_star2_O2/silverneko[P]Lambda-calculus-interpreter/src/main.cpp
// Function: normalForm(Expression const&, Context const&)
Object normalForm(const Expression& expr, const Context& env){
  switch( expr.type ){
    case Expression::Constant:
      return makeNormalForm( expr );
      break;
    case Expression::Var:
      if( env.exist(expr.name) ){
        Object &res = env[expr.name];
        if( res.isNormalForm() )
          return res;
        if( res.isPrimitive() )
          return res;
        return res = normalForm( res.expr(), res.env() );
      }else{
        return makeNormalForm( expr );
      }
      break;
    case Expression::Lambda:
      {
        Expression expr2(expr);
        expr2.body = new Expression(normalForm(*expr.body, env.erase( expr.name )).expr());
        return makeNormalForm( expr2 );
      // return Object(expr2, env);
      }
      break;
    case Expression::Ap:
      {
        const Expression& body = *expr.body;
        const Expression& arg = *expr.arg;
        Object callee(weakNormalForm(body, env));
        if( callee.callable() ){
          Object res = callee.call(arg, env);
          if( res.isNormalForm() )
            return res;
          if( res.isPrimitive() )
            return res;
          return normalForm(res.expr(), res.env());
        }else{
          Expression expr2(Expression::Ap);
          expr2.body = new Expression(callee.expr());
          expr2.arg = new Expression(normalForm(arg, env).expr());
          return makeNormalForm( expr2 );
        }
      }
      break;
    case Expression::Nothing:
      cerr << "[Normal form] Unexpected expression type: " << expr.type << endl;
      exit(1);
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x120, %rsp            # imm = 0x120
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movl (%rsi), %eax
leaq 0x6d9e(%rip), %rcx      # 0xb018
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x8(%r12), %r14
movq %r15, %rdi
movq %r14, %rsi
callq 0x3ec0
testb %al, %al
je 0x42c4
movq %r15, %rdi
movq %r14, %rsi
callq 0x3e1a
movq %rax, %r14
movl (%rax), %eax
testl %eax, %eax
je 0x42b4
cmpl $0x2, %eax
jne 0x4462
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6ffa
jmp 0x44c1
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3fc8
jmp 0x44c1
movq 0x28(%r12), %rsi
movq 0x30(%r12), %r12
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rdx
callq 0x3fdc
cmpl $0x0, (%r14)
je 0x4431
cmpl $0x3, 0xb8(%rsp)
je 0x4431
leaq 0x10(%rsp), %r14
movl $0x4, -0x8(%r14)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rax)
pushq $0x38
popq %rdi
callq 0x31b0
movq %rax, %r13
leaq 0xb8(%rsp), %rsi
movq %rax, %rdi
callq 0x715c
movq %r13, 0x30(%rsp)
pushq $0x38
popq %rdi
callq 0x31b0
movq %rax, %r13
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x4258
leaq 0x48(%rsp), %rsi
movq %r13, %rdi
callq 0x715c
leaq 0x8(%rsp), %r15
movq %r13, 0x30(%r15)
leaq 0x40(%rsp), %rdi
callq 0x6fd6
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3fc8
movq %r14, %rdi
callq 0x30c0
jmp 0x44b4
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x715c
pushq $0x38
popq %rdi
callq 0x31b0
movq %rax, %r14
movq 0x28(%r12), %r13
addq $0x8, %r12
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3dbc
leaq 0xb0(%rsp), %rdi
leaq 0x8(%rsp), %rdx
movq %r13, %rsi
callq 0x4258
leaq 0xb8(%rsp), %rsi
movq %r14, %rdi
callq 0x715c
leaq 0x40(%rsp), %r15
movq %r14, 0x28(%r15)
leaq 0xb0(%rsp), %rdi
callq 0x6fd6
leaq 0x10(%rsp), %rdi
callq 0x750c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3fc8
leaq 0x48(%rsp), %rdi
callq 0x30c0
jmp 0x44c1
leaq 0x40(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0x3edc
testl $0xfffffffd, 0x40(%rsp) # imm = 0xFFFFFFFD
jne 0x4495
leaq 0x40(%rsp), %rsi
movq %rbx, %rdi
callq 0x711a
jmp 0x44aa
leaq 0x8(%r14), %rsi
movq %r14, %rdx
addq $0x40, %rdx
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
callq 0x4258
movq %r14, %rdi
movq %r15, %rsi
callq 0x70d6
movq %rbx, %rdi
movq %rax, %rsi
callq 0x6ffa
jmp 0x44b4
leaq 0x48(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movq %rbx, %rdi
callq 0x4258
leaq 0x40(%rsp), %rdi
callq 0x6fd6
leaq 0xb0(%rsp), %rdi
callq 0x6fd6
movq %rbx, %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq 0xcb14(%rip), %rdi      # 0x10ff0
leaq 0x6f42(%rip), %rsi      # 0xb425
callq 0x31a0
movl (%r12), %esi
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x30e0
pushq $0x1
popq %rdi
callq 0x31e0
jmp 0x4536
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x6fd6
jmp 0x451a
jmp 0x4517
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r13, %rdi
callq 0x31c0
jmp 0x456f
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x6fd6
jmp 0x4577
movq %rax, %rbx
jmp 0x4577
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x6fd6
jmp 0x4550
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x750c
jmp 0x455f
movq %rax, %rbx
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x31c0
jmp 0x4589
movq %rax, %rbx
movq %r14, %rdi
callq 0x30c0
leaq 0xb0(%rsp), %rdi
callq 0x6fd6
jmp 0x4593
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x30c0
movq %rbx, %rdi
callq 0x32b0