microlisp 0.1.0
A small Scheme-subset interpreter in modern C.
Loading...
Searching...
No Matches
microlisp.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) 2026 microlisp contributors
4 *
5 * Public API for microlisp: a small Scheme-subset interpreter.
6 *
7 * The language is a strict R5RS-flavored Scheme subset:
8 *
9 * Types int64, boolean (#t/#f), symbol, string, pair, '(),
10 * procedure (closure or built-in primitive)
11 * Forms quote, if, define, lambda, set!, let, let*, letrec,
12 * begin, and, or, cond
13 * Built-ins arithmetic (+ - * / quotient remainder modulo)
14 * comparison (= < > <= >=)
15 * list ops (cons car cdr list pair? null? length)
16 * predicates (eq? eqv? equal? number? symbol?
17 * string? procedure? boolean?)
18 * I/O (display newline write error)
19 *
20 * Out of scope for v0.1: floats, chars, vectors, macros, continuations,
21 * exceptions (callers handle errors as status codes).
22 *
23 * The implementation uses tagged pointers (low-3-bit tag on uintptr_t)
24 * for the value representation and a mark-and-sweep garbage collector
25 * for heap-allocated objects. Proper tail-call optimization is provided
26 * by a trampolined evaluator -- tail-recursive Scheme programs run in
27 * O(1) C-stack space.
28 *
29 * @par Thread safety:
30 * Each ::microlisp_state owns its own GC heap, symbol table, and
31 * global environment. A given state is **MT-Unsafe**: all calls on
32 * the same state must be serialized by the caller. Different
33 * states may be used concurrently from different threads (every
34 * function is MT-Safe-per-instance).
35 *
36 * @par Memory:
37 * Allocation goes through an optional ::microlisp_allocator; pass
38 * NULL to use platform @c malloc / @c free. The allocator is used
39 * for both bookkeeping and GC-managed heap objects.
40 *
41 * @par Embedding model:
42 * The v0.1 API is intentionally minimal: callers supply Scheme
43 * source as a byte buffer, get back a printed result and a status
44 * code. A value-level C API (build values from C, call Scheme
45 * procedures with C-constructed arguments) is deferred to a
46 * future minor release once the internal representation is
47 * stable.
48 */
49#ifndef MICROLISP_MICROLISP_H
50#define MICROLISP_MICROLISP_H
51
52#include "microlisp/export.h"
53#include "microlisp/version.h"
54
55#include <stddef.h>
56#include <stdint.h>
57#include <stdio.h>
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
63/* -- Types ------------------------------------------------------------------ */
64
146
150
163typedef struct microlisp_allocator {
174 void *(*alloc)(size_t size, void *user);
178 void (*free)(void *ptr, void *user);
180 void *user;
182
184#define MICROLISP_DEFAULT_MAX_READ_DEPTH 256
185
187#define MICROLISP_DEFAULT_MAX_EVAL_DEPTH 1024
188
190#define MICROLISP_DEFAULT_MAX_PRINT_DEPTH 1024
191
193#define MICROLISP_DEFAULT_MAX_EQUAL_DEPTH 1024
194
238
239/* -- State lifecycle -------------------------------------------------------- */
240
258
262
263/* -- Evaluation ------------------------------------------------------------- */
264
302 const char *source,
303 size_t source_len,
304 char **out_bytes,
305 size_t *out_len);
306
327 FILE *in_file, FILE *out_file,
328 const char *prompt);
329
348MICROLISP_API void microlisp_buffer_free(microlisp_state *state, char *bytes);
349
350/* -- Diagnostics ------------------------------------------------------------ */
351
362MICROLISP_API const char *microlisp_state_error(const microlisp_state *state);
363
371MICROLISP_API void microlisp_state_error_position(const microlisp_state *state, size_t *out_line,
372 size_t *out_column);
373
374/* -- Misc ------------------------------------------------------------------- */
375
377MICROLISP_API const char *microlisp_version(void);
378
384
385#ifdef __cplusplus
386} /* extern "C" */
387#endif
388
389#endif /* MICROLISP_MICROLISP_H */
#define MICROLISP_API
Definition export.h:26
#define MICROLISP_NODISCARD
Definition export.h:36
MICROLISP_API const char * microlisp_status_string(microlisp_status status)
Human-readable name of a status code.
Definition status.c:13
MICROLISP_API MICROLISP_NODISCARD microlisp_status microlisp_state_create(const microlisp_options *opts, microlisp_state **out_state)
Create a fresh interpreter state with the built-in primitive bindings loaded in the top-level environ...
Definition state.c:112
MICROLISP_API MICROLISP_NODISCARD microlisp_status microlisp_eval(microlisp_state *state, const char *source, size_t source_len, char **out_bytes, size_t *out_len)
Evaluate a source buffer of Scheme code as a sequence of top-level forms.
Definition state.c:225
MICROLISP_API const char * microlisp_state_error(const microlisp_state *state)
Retrieve a human-readable description of the most recent error on state.
Definition state.c:65
MICROLISP_API MICROLISP_NODISCARD microlisp_status microlisp_repl(microlisp_state *state, FILE *in_file, FILE *out_file, const char *prompt)
Run an interactive Read-Eval-Print Loop reading from in_file and writing prompts and results to out_f...
Definition state.c:328
MICROLISP_API void microlisp_buffer_free(microlisp_state *state, char *bytes)
Free a buffer previously returned by microlisp_eval via out_bytes .
Definition state.c:310
microlisp_status
Error and status codes returned by every fallible API entry point.
Definition microlisp.h:71
@ MICROLISP_ERR_UNBOUND
Evaluator looked up a symbol that has no binding in any frame of the active environment.
Definition microlisp.h:95
@ MICROLISP_OK
Success.
Definition microlisp.h:73
@ MICROLISP_ERR_ARITY
Procedure called with the wrong number of arguments.
Definition microlisp.h:102
@ MICROLISP_ERR_TYPE
Evaluator received a value of the wrong type – e.g.
Definition microlisp.h:99
@ MICROLISP_ERR_SYNTAX
Malformed special form – e.g.
Definition microlisp.h:121
@ MICROLISP_ERR_IO
An output stream reported a write error during display , write , or newline , or while writing the re...
Definition microlisp.h:117
@ MICROLISP_ERR_READ_TRUNCATED
Reader reached end-of-input mid-form (e.g.
Definition microlisp.h:87
@ MICROLISP_ERR_DIV_ZERO
Division by zero in / , quotient , remainder , or modulo .
Definition microlisp.h:106
@ MICROLISP_ERR_READ_DEPTH
Reader nesting depth exceeded the configured limit (DoS guard against pathologically deep inputs).
Definition microlisp.h:91
@ MICROLISP_ERR_PRINT_DEPTH
Printer recursion depth exceeded the configured limit.
Definition microlisp.h:136
@ MICROLISP_ERR_NOMEM
Allocator (or platform malloc) returned NULL.
Definition microlisp.h:79
@ MICROLISP_ERR_OVERFLOW
Integer operation would overflow the int64_t range.
Definition microlisp.h:109
@ MICROLISP_ERR_EQUAL_DEPTH
Structural-equality (eqv?, equal?) recursion depth exceeded the configured limit.
Definition microlisp.h:144
@ MICROLISP_ERR_READ_SYNTAX
Reader encountered a syntax error – bad token, unmatched paren, malformed string escape,...
Definition microlisp.h:83
@ MICROLISP_ERR_INVALID_ARG
A required argument was NULL or otherwise structurally invalid.
Definition microlisp.h:76
@ MICROLISP_ERR_EVAL_DEPTH
Evaluator recursion depth exceeded the configured limit.
Definition microlisp.h:127
@ MICROLISP_ERR_USER
A Scheme program invoked the error built-in.
Definition microlisp.h:113
MICROLISP_API const char * microlisp_version(void)
Library version (e.g.
Definition version.c:8
MICROLISP_API void microlisp_state_error_position(const microlisp_state *state, size_t *out_line, size_t *out_column)
Source position of the most recent error, as line and column (both 1-based).
Definition state.c:72
MICROLISP_API void microlisp_state_destroy(microlisp_state *state)
Destroy a state, releasing its GC heap and all interned symbols.
Definition state.c:189
Caller-supplied allocator.
Definition microlisp.h:163
void * user
Opaque user data passed verbatim to alloc and free.
Definition microlisp.h:180
void(* free)(void *ptr, void *user)
Free a pointer previously returned by alloc.
Definition microlisp.h:178
Options controlling a state.
Definition microlisp.h:199
size_t max_equal_depth
Maximum structural-equality walk depth.
Definition microlisp.h:229
size_t max_read_depth
Maximum reader nesting depth.
Definition microlisp.h:206
size_t max_eval_depth
Maximum evaluator recursion depth.
Definition microlisp.h:214
size_t max_print_depth
Maximum printer pair-walk depth.
Definition microlisp.h:222
size_t gc_initial_threshold
Hard ceiling on heap objects between GC collections.
Definition microlisp.h:236
const microlisp_allocator * allocator
Allocator.
Definition microlisp.h:201
Definition microlisp_internal.h:267