PtcRunner.Lisp.Eval.Patterns (PtcRunner v0.10.1)

Copy Markdown View Source

Pattern matching for let bindings in Lisp evaluation.

Handles destructuring patterns including variables, map destructuring, sequence destructuring, and :as patterns.

Summary

Functions

Coerces a value to match the expected shape of a pattern.

Matches a pattern against a value, returning variable bindings on success.

Types

bindings()

@type bindings() :: %{required(atom()) => term()}

match_result()

@type match_result() :: {:ok, bindings()} | {:error, {:destructure_error, String.t()}}

pattern()

@type pattern() :: term()

Functions

coerce_for_pattern(arg1, args)

@spec coerce_for_pattern(pattern(), term()) ::
  term() | {:error, {:destructure_error, String.t()}}

Coerces a value to match the expected shape of a pattern.

Used for rest-arg keyword arguments: when a variadic rest pattern expects map destructuring, converts a flat key-value list to a map. Returns an error tuple for odd-length lists. Returns the value unchanged for non-map patterns.

Examples

iex> pattern = {:destructure, {:keys, [:a], []}}
iex> PtcRunner.Lisp.Eval.Patterns.coerce_for_pattern(pattern, [:a, 1])
%{a: 1}

iex> PtcRunner.Lisp.Eval.Patterns.coerce_for_pattern({:var, :xs}, [1, 2])
[1, 2]

match_pattern(arg, value)

@spec match_pattern(pattern(), term()) :: match_result()

Matches a pattern against a value, returning variable bindings on success.