PtcRunner.Lisp.Env (PtcRunner v0.9.0)

Copy Markdown View Source

Builds the initial environment with builtins for PTC-Lisp.

Provides the foundation environment with all builtin functions and their descriptors. The environment supports multiple binding types:

  • {:normal, fun} - Fixed-arity function
  • {:variadic, fun, identity} - Variadic function with identity value for 0-arg case
  • {:variadic_nonempty, name, fun} - Variadic function requiring at least 1 argument
  • {:multi_arity, name, tuple_of_funs} - Multiple arities where tuple index = arity - min_arity
  • {:collect, fun} - Collects all args into a list and passes to unary function

Summary

Functions

Check if a name is a builtin function.

Get the list of builtin functions for a category.

Get a human-readable name for a category.

Check if a namespace is a known Clojure-style namespace.

Check if a name is a namespaced constant.

Get the category for a Clojure-style namespace.

Types

binding()

@type binding() ::
  {:normal, function()}
  | {:variadic, function(), term()}
  | {:variadic_nonempty, atom(), function()}
  | {:multi_arity, atom(), tuple()}
  | {:collect, function()}
  | {:constant, term()}

env()

@type env() :: %{required(atom()) => binding()}

Functions

builtin?(name)

@spec builtin?(atom()) :: boolean()

Check if a name is a builtin function.

Returns true if the given atom is a builtin function name.

Examples

iex> PtcRunner.Lisp.Env.builtin?(:map)
true

iex> PtcRunner.Lisp.Env.builtin?(:filter)
true

iex> PtcRunner.Lisp.Env.builtin?(:my_var)
false

builtins_by_category(atom)

@spec builtins_by_category(atom()) :: [atom()]

Get the list of builtin functions for a category.

Used to provide helpful error messages when a function is not available.

Examples

iex> :join in PtcRunner.Lisp.Env.builtins_by_category(:string)
true

iex> :set in PtcRunner.Lisp.Env.builtins_by_category(:set)
true

category_name(atom)

@spec category_name(atom()) :: String.t()

Get a human-readable name for a category.

Examples

iex> PtcRunner.Lisp.Env.category_name(:string)
"String"

iex> PtcRunner.Lisp.Env.category_name(:core)
"Core"

clojure_namespace?(ns)

@spec clojure_namespace?(atom()) :: boolean()

Check if a namespace is a known Clojure-style namespace.

Examples

iex> PtcRunner.Lisp.Env.clojure_namespace?(:"clojure.string")
true

iex> PtcRunner.Lisp.Env.clojure_namespace?(:str)
true

iex> PtcRunner.Lisp.Env.clojure_namespace?(:my_ns)
false

constant?(ns, name)

Check if a name is a namespaced constant.

initial()

@spec initial() :: env()

namespace_category(ns)

@spec namespace_category(atom()) :: atom() | nil

Get the category for a Clojure-style namespace.

Returns :string, :set, or :core.

Examples

iex> PtcRunner.Lisp.Env.namespace_category(:"clojure.string")
:string

iex> PtcRunner.Lisp.Env.namespace_category(:str)
:string