Module: MilkTea::CLI::CommandEmitC

Included in:
MilkTea::CLI
Defined in:
lib/milk_tea/tooling/cli/commands/emit_c.rb

Instance Method Summary collapse

Instance Method Details

#emit_c_commandObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/milk_tea/tooling/cli/commands/emit_c.rb', line 6

def emit_c_command
  args = @argv.dup
  @argv = []
  until args.empty?
    arg = args.shift
    @argv << arg
  end

  unless @argv.any?
    @err.puts("missing source file path")
    print_usage(@err)
    return 1
  end

  resolution = extract_resolution_flags!
  input_paths = @argv.dup
  return 1 unless ensure_known_source_operands!("emit-c", input_paths)

  program_paths = input_paths.map { |p| resolve_program_path(p) }
  return 1 if program_paths.include?(nil)

  ensure_current_lockfiles!(input_paths) if resolution[:frozen]

  multiple = program_paths.length > 1
  program_paths.each_with_index do |path, index|
    program = make_module_loader(path, locked: resolution[:locked], platform: ModuleLoader.default_host_platform).check_program(path)
    if multiple
      @out.puts("/* --- #{path} --- */")
    end
    @out.write(CBackend.generate_c(Lowering.lower(program), emit_line_directives: false))
    @out.puts if multiple && index < program_paths.length - 1
  end
  0
end

#resolve_program_path(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/milk_tea/tooling/cli/commands/emit_c.rb', line 41

def resolve_program_path(path)
  return path unless File.directory?(path)

  manifest_path = File.join(path, "package.toml")
  unless File.file?(manifest_path)
    @err.puts("no package.toml found in #{path}")
    return nil
  end

  manifest = PackageManifest.load(path)
  entry_path = manifest.source_path
  unless entry_path && File.file?(entry_path)
    @err.puts("no build entry found for #{path}")
    return nil
  end

  entry_path
rescue PackageManifestError => e
  @err.puts("failed to load package manifest for #{path}: #{e.message}")
  nil
end