PtcRunner.Lisp.ClojureValidator (PtcRunner v0.9.0)

Copy Markdown View Source

Validates PTC-Lisp programs against Babashka/Clojure.

Provides validation to ensure:

  1. PTC-Lisp programs are valid Clojure syntax
  2. Runtime functions behave identically to Clojure equivalents

Usage

# Check if Babashka is available
PtcRunner.Lisp.ClojureValidator.available?()

# Validate syntax only (fast)
PtcRunner.Lisp.ClojureValidator.validate_syntax("(+ 1 2)")

# Execute and get result
PtcRunner.Lisp.ClojureValidator.execute("(+ 1 2)")

Installation

Install Babashka with: mix ptc.install_babashka

Summary

Functions

Check if Babashka is available.

Get the path to the Babashka binary.

Compare a PTC-Lisp result with a Clojure result.

Execute source in Babashka and return the result.

Validate that source is valid Clojure syntax.

Wrap PTC-Lisp source with Clojure stubs for PTC-specific features.

Functions

available?()

@spec available?() :: boolean()

Check if Babashka is available.

Looks for bb at _build/tools/bb first, then in system PATH.

bb_path()

@spec bb_path() :: String.t() | nil

Get the path to the Babashka binary.

Returns nil if not found.

compare_results(ptc_result, clj_result)

@spec compare_results(any(), any()) :: :match | {:mismatch, String.t()}

Compare a PTC-Lisp result with a Clojure result.

Handles normalization of types that differ between systems:

  • Elixir atoms vs Clojure keywords
  • Map key type differences

Returns :match if equivalent, {:mismatch, details} otherwise.

execute(source, opts \\ [])

@spec execute(
  String.t(),
  keyword()
) :: {:ok, any()} | {:error, String.t()}

Execute source in Babashka and return the result.

Options

  • :timeout - Timeout in milliseconds (default: 5000)
  • :context - Context map to inject as ctx binding
  • :memory - Memory map to inject as memory binding

Examples

iex> PtcRunner.Lisp.ClojureValidator.execute("(+ 1 2 3)")
{:ok, 6}

iex> PtcRunner.Lisp.ClojureValidator.execute("(filter even? [1 2 3 4])")
{:ok, [2, 4]}

validate_syntax(source)

@spec validate_syntax(String.t()) :: :ok | {:error, String.t()}

Validate that source is valid Clojure syntax.

Returns :ok if valid, {:error, reason} if invalid.

Examples

iex> PtcRunner.Lisp.ClojureValidator.validate_syntax("(+ 1 2)")
:ok

iex> PtcRunner.Lisp.ClojureValidator.validate_syntax("(+ 1 2")
{:error, "Syntax error: ..."}

wrap_with_stubs(source, context \\ %{}, memory \\ %{})

@spec wrap_with_stubs(String.t(), map(), map()) :: String.t()

Wrap PTC-Lisp source with Clojure stubs for PTC-specific features.

Adds definitions for:

  • ctx - Context data as a map
  • memory - Memory data as a map
  • PTC-specific functions: where, all-of, any-of, none-of, etc.