Download lib/forge/compiler.ex from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 8.77 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/compiler.ex
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/lib/forge/compiler.ex
-
curl -L -o compiler.ex https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/lib/forge/compiler.ex
8.77 kB
| defmodule Forge.Compiler do | |
| @moduledoc """ | |
| Compiler - Elixir source code compilation pipeline. | |
| This module provides the core compilation pipeline for the Forge tournament system. | |
| It transforms Elixir source code through the complete compiler pipeline: | |
| Elixir Source | |
| ↓ | |
| Elixir AST | |
| ↓ | |
| Quoted Representation | |
| ↓ | |
| Erlang Forms | |
| ↓ | |
| BEAM Compilation | |
| ↓ | |
| BEAM Instructions | |
| Each stage preserves provenance and produces hashable artifacts. | |
| """ | |
| alias Forge.Hash | |
| alias Forge.Provenance | |
| alias Forge.Artifact | |
| @type compile_options :: %{ | |
| compiler_flags: list(String.t()), | |
| otp_version: String.t(), | |
| elixir_version: String.t(), | |
| target_arch: String.t(), | |
| target_os: String.t(), | |
| sandbox: boolean(), | |
| timeout: integer() | |
| } | |
| @type compile_result :: %{ | |
| artifact_id: String.t(), | |
| provenance: Provenance.provenance_chain(), | |
| source: String.t(), | |
| ast: map() | nil, | |
| quoted: any() | nil, | |
| forms: any() | nil, | |
| beam_path: String.t() | nil, | |
| beam_binary: binary() | nil, | |
| errors: list(String.t()), | |
| warnings: list(String.t()), | |
| metrics: map() | |
| } | |
| @doc """ | |
| Compiles Elixir source code through the complete pipeline. | |
| ## Parameters | |
| - source: Elixir source code as a string | |
| - options: Compile options | |
| ## Returns | |
| - {:ok, compile_result} on success | |
| - {:error, reason, details} on failure | |
| """ | |
| @spec compile(String.t(), compile_options()) :: {:ok, compile_result()} | {:error, String.t(), map()} | |
| def compile(source, options \\ []) do | |
| default_options = %{ | |
| compiler_flags: [], | |
| otp_version: System.get_env("OTP_VERSION") || otp_version(), | |
| elixir_version: System.get_env("ELIXIR_VERSION") || elixir_version(), | |
| target_arch: System.get_env("TARGET_ARCH") || arch(), | |
| target_os: System.get_env("TARGET_OS") || os(), | |
| sandbox: true, | |
| timeout: 30_000 | |
| } | |
| options = Map.merge(default_options, options) | |
| # Generate unique artifact ID | |
| artifact_id = Hash.artifact_id(Hash.sha256(source) |> elem(1)) | |
| # Create provenance chain | |
| provenance = Provenance.create( | |
| artifact_id, | |
| "contestant_#{:rand.uniform(1000000)}", | |
| "challenge_#{:rand.uniform(1000000)}", | |
| source, | |
| %{ | |
| version: options.elixir_version, | |
| otp: options.otp_version, | |
| flags: options.compiler_flags | |
| }, | |
| %{ | |
| arch: options.target_arch, | |
| os: options.target_os | |
| } | |
| ) |> elem(1) | |
| case provenance do | |
| {:ok, chain} -> | |
| # Compile through each stage | |
| with {:ok, ast_result} <- compile_to_ast(source, options), | |
| {:ok, chain} <- Provenance.add_stage(chain, :ast, ast_result.ast), | |
| {:ok, quoted_result} <- compile_to_quoted(source, options), | |
| {:ok, chain} <- Provenance.add_stage(chain, :quoted, inspect(quoted_result.quoted)), | |
| {:ok, forms_result} <- compile_to_forms(source, options), | |
| {:ok, chain} <- Provenance.add_stage(chain, :erlang_forms, inspect(forms_result.forms)), | |
| {:ok, beam_result} <- compile_to_beam(source, options) do | |
| {:ok, beam_binary} = File.read(beam_result.beam_path) | |
| {:ok, chain} = Provenance.add_stage(chain, :beam, beam_binary) | |
| # Extract BEAM metadata | |
| metadata = extract_beam_metadata(beam_result.beam_path) | |
| result = %{ | |
| artifact_id: artifact_id, | |
| provenance: chain, | |
| source: source, | |
| ast: ast_result.ast, | |
| quoted: quoted_result.quoted, | |
| forms: forms_result.forms, | |
| beam_path: beam_result.beam_path, | |
| beam_binary: beam_binary, | |
| errors: beam_result.errors, | |
| warnings: beam_result.warnings, | |
| metrics: %{ | |
| source_size: byte_size(source), | |
| beam_size: byte_size(beam_binary), | |
| function_count: metadata.function_count || 0, | |
| module_count: metadata.module_count || 0, | |
| instruction_count: metadata.instruction_count || 0 | |
| } | |
| } | |
| {:ok, result} | |
| else | |
| {:error, error, _details} -> | |
| # Add error information to provenance | |
| error_stage = get_error_stage(error) | |
| {:ok, chain} = Provenance.add_stage(chain, error_stage, error) | |
| result = %{ | |
| artifact_id: artifact_id, | |
| provenance: chain, | |
| source: source, | |
| ast: nil, | |
| quoted: nil, | |
| forms: nil, | |
| beam_path: nil, | |
| beam_binary: nil, | |
| errors: [error], | |
| warnings: [], | |
| metrics: %{} | |
| } | |
| {:error, "Compilation failed at #{Atom.to_string(error_stage)}", result} | |
| end | |
| {:error, reason} -> | |
| {:error, "Provenance creation failed: #{reason}", %{}} | |
| end | |
| end | |
| @doc """ | |
| Compiles source to AST. | |
| """ | |
| defp compile_to_ast(source, _options) do | |
| try do | |
| quoted = Code.string_to_quoted(source) | |
| ast = Macro.expand_once(quoted, __ENV__) | |
| {:ok, %{ast: ast}} | |
| rescue | |
| e -> {:error, :ast, inspect(e)} | |
| end | |
| end | |
| @doc """ | |
| Compiles source to quoted representation. | |
| """ | |
| defp compile_to_quoted(source, _options) do | |
| try do | |
| quoted = Code.string_to_quoted(source) | |
| {:ok, %{quoted: quoted}} | |
| rescue | |
| e -> {:error, :quoted, inspect(e)} | |
| end | |
| end | |
| @doc """ | |
| Compiles source to Erlang forms. | |
| """ | |
| defp compile_to_forms(source, _options) do | |
| try do | |
| quoted = Code.string_to_quoted(source) | |
| forms = Code.quoted_to_algebra(quoted) | |
| {:ok, %{forms: forms}} | |
| rescue | |
| e -> {:error, :erlang_forms, inspect(e)} | |
| end | |
| end | |
| @doc """ | |
| Compiles source to BEAM bytecode. | |
| """ | |
| defp compile_to_beam(source, options) do | |
| try do | |
| # Create a temporary module for compilation | |
| module_name = "ForgeContestant_#{:rand.uniform(1000000)}" | |
| # Wrap source in a module if it's not already | |
| wrapped_source = if String.contains?(source, "defmodule") do | |
| source | |
| else | |
| "defmodule #{module_name} do\n#{source}\nend" | |
| end | |
| # Write to temporary file | |
| temp_dir = Path.join([:code.priv_dir(:forge), "temp"]) | |
| File.mkdir_p!(temp_dir) | |
| temp_file = Path.join(temp_dir, "#{module_name}.ex") | |
| File.write!(temp_file, wrapped_source) | |
| # Compile the file | |
| beam_path = Path.join(temp_dir, "#{module_name}.beam") | |
| compile_result = Code.compile_file(temp_file, beam_path, options.compiler_flags) | |
| # Clean up temp files (optional - keep for debugging) | |
| {:ok, %{ | |
| beam_path: beam_path, | |
| errors: [], | |
| warnings: [] | |
| }} | |
| rescue | |
| e -> {:error, :beam, inspect(e)} | |
| end | |
| end | |
| @doc """ | |
| Extracts metadata from a BEAM file. | |
| """ | |
| def extract_beam_metadata(beam_path) do | |
| try do | |
| beam = :beam_lib.read(beam_path) | |
| %{ | |
| module_name: beam.module, | |
| module_count: 1, | |
| function_count: length(beam.functions) || 0, | |
| instruction_count: Enum.reduce(beam.functions || [], 0, fn func, acc -> acc + length(func.instructions) end), | |
| version: beam.version, | |
| source_file: beam.source || nil, | |
| compiler: beam.compiler || nil, | |
| md5: beam.md5 || nil | |
| } | |
| rescue | |
| _ -> %{ | |
| module_name: nil, | |
| module_count: 0, | |
| function_count: 0, | |
| instruction_count: 0 | |
| } | |
| end | |
| end | |
| @doc """ | |
| Gets the current OTP version. | |
| """ | |
| def otp_version do | |
| System.version() |> String.split("/") |> List.first() |> String.trim() | |
| end | |
| @doc """ | |
| Gets the current Elixir version. | |
| """ | |
| def elixir_version do | |
| System.get_env("ELIXIR_VERSION") || to_string(:elixir) <> " " <> :erlang.system_info(:otp_release) | |
| end | |
| @doc """ | |
| Gets the current architecture. | |
| """ | |
| def arch do | |
| System.get_env("TARGET_ARCH") || :erlang.system_info(:system_architecture) | |
| end | |
| @doc """ | |
| Gets the current OS. | |
| """ | |
| def os do | |
| System.get_env("TARGET_OS") || :erlang.system_info(:system_name) | |
| end | |
| @doc """ | |
| Determines which stage a compilation error occurred at. | |
| """ | |
| defp get_error_stage(error) do | |
| cond do | |
| String.contains?(to_string(error), "AST") -> :ast | |
| String.contains?(to_string(error), "quoted") -> :quoted | |
| String.contains?(to_string(error), "forms") -> :erlang_forms | |
| String.contains?(to_string(error), "BEAM") -> :beam | |
| true -> :source | |
| end | |
| end | |
| end | |