// File: /workspace/github_c_makefile_star2_O0/atomlang[P]atomlang/src/pk_compiler.c
// Function: compileFromImport
static void compileFromImport(Compiler* compiler) {
  ASSERT(compiler->scope_depth == DEPTH_GLOBAL, OOPS);

  // Import the library and push it on the stack. If the import failed
  // lib_from would be NULL.
  Script* lib_from = compilerImport(compiler);

  // At this point the script would be on the stack before executing the next
  // instruction.
  consume(compiler, TK_IMPORT, "Expected keyword 'import'.");

  if (match(compiler, TK_STAR)) {
    // from math import *
    if (lib_from) compilerImportAll(compiler, lib_from);

  } else {
    do {
      // Consume the symbol name to import from the script.
      consume(compiler, TK_NAME, "Expected symbol to import.");
      const char* name = compiler->previous.start;
      uint32_t length = (uint32_t)compiler->previous.length;
      int line = compiler->previous.line;

      // Add the name of the symbol to the names buffer.
      int name_index = (int)scriptAddName(compiler->script, compiler->vm,
                                          name, length);

      // Don't pop the lib since it'll be used for the next entry.
      emitOpcode(compiler, OP_GET_ATTRIB_KEEP);
      emitShort(compiler, name_index); //< Name of the attrib.

      // Check if it has an alias.
      if (match(compiler, TK_AS)) {
        // Consuming it'll update the previous token which would be the name of
        // the binding variable.
        consume(compiler, TK_NAME, "Expected a name after 'as'.");
      }

      // Set the imported symbol binding name, which wold be in the last token
      // consumed by the first one or after the as keyword.
      name = compiler->previous.start;
      length = (uint32_t)compiler->previous.length;
      line = compiler->previous.line;

      // Get the variable to bind the imported symbol, if we already have a
      // variable with that name override it, otherwise use a new variable.
      int var_index = compilerImportName(compiler, line, name, length);
      if (var_index != -1) emitStoreVariable(compiler, var_index, true);
      emitOpcode(compiler, OP_POP);

    } while (match(compiler, TK_COMMA) && (skipNewLines(compiler), true));
  }

  // Done getting all the attributes, now pop the lib from the stack.
  emitOpcode(compiler, OP_POP);

  // Always end the import statement.
  consumeEndStatement(compiler);
}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
cmpl $-0x1, 0xa0(%rax)
je 0x19991
movq 0x1969a(%rip), %rax     # 0x32ff8
movq (%rax), %rdi
leaq 0x107e8(%rip), %rsi     # 0x2a150
leaq 0x108f5(%rip), %rdx     # 0x2a264
leaq 0x11e67(%rip), %rcx     # 0x2b7dd
leaq 0x1165a(%rip), %r8      # 0x2afd7
movl $0x94d, %r9d            # imm = 0x94D
movb $0x0, %al
callq 0x2240
ud2
callq 0x2040
jmp 0x19993
movq 0x30(%rsp), %rdi
callq 0x19d40
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
movl $0x30, %esi
leaq 0x11e3c(%rip), %rdx     # 0x2b7ef
callq 0x160a0
movq 0x30(%rsp), %rdi
movl $0x17, %esi
callq 0x15f50
testb $0x1, %al
jne 0x199cd
jmp 0x199e9
cmpq $0x0, 0x28(%rsp)
je 0x199e4
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x19ec0
jmp 0x19b3b
jmp 0x199eb
movq 0x30(%rsp), %rdi
movl $0x47, %esi
leaq 0x11e0e(%rip), %rdx     # 0x2b80a
callq 0x160a0
movq 0x30(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x30(%rsp), %rax
movq 0x18b0(%rax), %rdi
movq 0x30(%rsp), %rax
movq (%rax), %rsi
movq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %ecx
callq 0x4c60
movl %eax, 0x14(%rsp)
movq 0x30(%rsp), %rdi
movl $0x31, %esi
callq 0x16650
movq 0x30(%rsp), %rdi
movl 0x14(%rsp), %esi
callq 0x16690
movq 0x30(%rsp), %rdi
movl $0x31, %esi
callq 0x15f50
testb $0x1, %al
jne 0x19a7f
jmp 0x19a95
movq 0x30(%rsp), %rdi
movl $0x47, %esi
leaq 0x11d95(%rip), %rdx     # 0x2b825
callq 0x160a0
movq 0x30(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rax
movl 0x40(%rax), %eax
movl %eax, 0x1c(%rsp)
movq 0x30(%rsp), %rax
movl 0x44(%rax), %eax
movl %eax, 0x18(%rsp)
movq 0x30(%rsp), %rdi
movl 0x18(%rsp), %esi
movq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %ecx
callq 0x1a140
movl %eax, 0x10(%rsp)
cmpl $-0x1, 0x10(%rsp)
je 0x19af0
movq 0x30(%rsp), %rdi
movl 0x10(%rsp), %esi
movl $0x1, %edx
callq 0x18350
movq 0x30(%rsp), %rdi
movl $0x25, %esi
callq 0x16650
movq 0x30(%rsp), %rdi
movl $0x5, %esi
callq 0x15f50
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0xf(%rsp)
jne 0x19b1d
jmp 0x19b2d
movq 0x30(%rsp), %rdi
callq 0x15f40
movb $0x1, %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0x199eb
jmp 0x19b3b
movq 0x30(%rsp), %rdi
movl $0x25, %esi
callq 0x16650
movq 0x30(%rsp), %rdi
callq 0x16110
addq $0x38, %rsp
retq
nopl (%rax)