libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
ast.h File Reference

Abstract Syntax Tree representation for SNOBOL patterns. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "snobol/arena.h"
Include dependency graph for ast.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  snobol_ast_version_t
struct  ast_node

Macros

#define SNOBOL_AST_VERSION_MAJOR   1
#define SNOBOL_AST_VERSION_MINOR   0
#define SNOBOL_AST_VERSION_PATCH   0
#define SNOBOL_AST_VERSION_STRING   "1.0.0"
#define SNOBOL_AST_VERSION_CHECK(major, minor)

Typedefs

typedef struct ast_node ast_node_t

Enumerations

enum  ast_type_t {
  AST_LITERAL , AST_CONCAT , AST_ALT , AST_REPETITION ,
  AST_SPAN , AST_BREAK , AST_ANY , AST_NOTANY ,
  AST_ARBNO , AST_CAP , AST_ASSIGN , AST_LEN ,
  AST_EVAL , AST_DYNAMIC_EVAL , AST_ANCHOR , AST_EMIT ,
  AST_LABEL , AST_GOTO , AST_TABLE_ACCESS , AST_TABLE_UPDATE ,
  AST_BREAKX , AST_BAL , AST_FENCE , AST_REM ,
  AST_RPOS , AST_RTAB , AST_POS , AST_TAB ,
  AST_ABORT , AST_FAIL , AST_SUCCEED , AST_REG_REF
}
enum  anchor_type_t { ANCHOR_START , ANCHOR_END }

Functions

ast_node_t * snobol_ast_create_lit (const char *text, size_t len)
ast_node_t * snobol_ast_create_concat (ast_node_t **parts, size_t count)
ast_node_t * snobol_ast_create_alt (ast_node_t *left, ast_node_t *right)
ast_node_t * snobol_ast_create_arbno (ast_node_t *sub)
ast_node_t * snobol_ast_create_cap (int reg, ast_node_t *sub)
ast_node_t * snobol_ast_create_span (const char *set, size_t len)
ast_node_t * snobol_ast_create_any (const char *set, size_t len)
ast_node_t * snobol_ast_create_repeat (ast_node_t *sub, int32_t min, int32_t max)
ast_node_t * snobol_ast_create_label (char *name, ast_node_t *target)
ast_node_t * snobol_ast_create_goto (const char *label)
ast_node_t * snobol_ast_clone (const ast_node_t *node)
void snobol_ast_free (ast_node_t *node)
uint32_t snobol_ast_depth (const ast_node_t *node)
void snobol_ast_set_arena (snobol_arena_t *arena)
snobol_arena_t * snobol_ast_clear_arena (void)
const char * snobol_ast_type_name (ast_type_t type)
void snobol_ast_dump (const ast_node_t *node, FILE *out, int indent)
snobol_ast_version_t snobol_ast_get_version (void)
bool snobol_ast_version_check (uint16_t required_major, uint16_t required_minor)
const char * snobol_ast_version_string (void)
ast_node_t * snobol_ast_create_literal (const char *text, size_t len)
ast_node_t * snobol_ast_create_break (const char *set, size_t len)
ast_node_t * snobol_ast_create_notany (const char *set, size_t len)
ast_node_t * snobol_ast_create_assign (int var, int reg)
ast_node_t * snobol_ast_create_len (int32_t n)
ast_node_t * snobol_ast_create_anchor (anchor_type_t atype)
ast_node_t * snobol_ast_create_emit (const char *text, size_t len, int reg)
ast_node_t * snobol_ast_create_dynamic_eval (ast_node_t *expr)
ast_node_t * snobol_ast_create_eval (int fn, int reg)
ast_node_t * snobol_ast_create_table_access (const char *table, ast_node_t *key)
ast_node_t * snobol_ast_create_table_update (const char *table, ast_node_t *key, ast_node_t *value)
ast_node_t * snobol_ast_create_breakx (const char *set, size_t len)
ast_node_t * snobol_ast_create_bal (uint32_t open_cp, uint32_t close_cp)
ast_node_t * snobol_ast_create_fence (void)
ast_node_t * snobol_ast_create_rem (void)
ast_node_t * snobol_ast_create_rpos (int32_t n)
ast_node_t * snobol_ast_create_rtab (int32_t n)
ast_node_t * snobol_ast_create_pos (int32_t n)
ast_node_t * snobol_ast_create_tab (int32_t n)
ast_node_t * snobol_ast_create_abort (void)
ast_node_t * snobol_ast_create_fail (void)
ast_node_t * snobol_ast_create_succeed (void)
ast_node_t * snobol_ast_create_regref (int reg)

Detailed Description

Abstract Syntax Tree representation for SNOBOL patterns.

Uses tagged union structs for type-safe AST nodes. Memory ownership: parent nodes own child nodes recursively. Caller is responsible for calling snobol_ast_free() to free entire tree.

Version: 1.0.0

  • Major: Breaking changes to AST structure or memory layout
  • Minor: New node types or non-breaking additions
  • Patch: Bug fixes, no API changes

Definition in file ast.h.

Macro Definition Documentation

◆ SNOBOL_AST_VERSION_CHECK

#define SNOBOL_AST_VERSION_CHECK ( major,
minor )
Value:
#define SNOBOL_AST_VERSION_MAJOR
Definition ast.h:33
#define SNOBOL_AST_VERSION_MINOR
Definition ast.h:34

Check if AST version is compatible Returns true if major version matches and minor version >= required

Definition at line 42 of file ast.h.

◆ SNOBOL_AST_VERSION_MAJOR

#define SNOBOL_AST_VERSION_MAJOR   1

AST version information Use snobol_ast_get_version() to retrieve at runtime

Definition at line 33 of file ast.h.

◆ SNOBOL_AST_VERSION_MINOR

#define SNOBOL_AST_VERSION_MINOR   0

Definition at line 34 of file ast.h.

◆ SNOBOL_AST_VERSION_PATCH

#define SNOBOL_AST_VERSION_PATCH   0

Definition at line 35 of file ast.h.

◆ SNOBOL_AST_VERSION_STRING

#define SNOBOL_AST_VERSION_STRING   "1.0.0"

Definition at line 36 of file ast.h.

Typedef Documentation

◆ ast_node_t

typedef struct ast_node ast_node_t

Forward declarations

Definition at line 129 of file ast.h.

Enumeration Type Documentation

◆ anchor_type_t

Anchor type enumeration

Enumerator
ANCHOR_START 
ANCHOR_END 

Definition at line 121 of file ast.h.

◆ ast_type_t

enum ast_type_t

AST node type tags

Enumerator
AST_LITERAL 
AST_CONCAT 
AST_ALT 
AST_REPETITION 
AST_SPAN 
AST_BREAK 
AST_ANY 
AST_NOTANY 
AST_ARBNO 
AST_CAP 
AST_ASSIGN 
AST_LEN 
AST_EVAL 
AST_DYNAMIC_EVAL 
AST_ANCHOR 
AST_EMIT 
AST_LABEL 
AST_GOTO 
AST_TABLE_ACCESS 
AST_TABLE_UPDATE 
AST_BREAKX 
AST_BAL 
AST_FENCE 
AST_REM 
AST_RPOS 
AST_RTAB 
AST_POS 
AST_TAB 
AST_ABORT 
AST_FAIL 
AST_SUCCEED 
AST_REG_REF 

Definition at line 58 of file ast.h.

Function Documentation

◆ snobol_ast_clear_arena()

snobol_arena_t * snobol_ast_clear_arena ( void )

Clear the thread-local AST arena binding and return the previously bound arena (or NULL if none). Convenient for reclaiming node storage after the tree has been freed.

Returns
The arena that was bound before this call (may be NULL).

◆ snobol_ast_clone()

ast_node_t * snobol_ast_clone ( const ast_node_t * node)

Deep-clone an AST node and all children recursively

Parameters
nodeNode to clone (NULL returns NULL)
Returns
New AST node (caller owns, must free with snobol_ast_free)

◆ snobol_ast_create_abort()

ast_node_t * snobol_ast_create_abort ( void )

Create AST_ABORT node (no args)

◆ snobol_ast_create_alt()

ast_node_t * snobol_ast_create_alt ( ast_node_t * left,
ast_node_t * right )

Create an alternation AST node

Parameters
leftLeft alternative (ownership transferred)
rightRight alternative (ownership transferred)
Returns
New AST node (caller owns)

◆ snobol_ast_create_anchor()

ast_node_t * snobol_ast_create_anchor ( anchor_type_t atype)

Create AST_ANCHOR node

◆ snobol_ast_create_any()

ast_node_t * snobol_ast_create_any ( const char * set,
size_t len )

Create an any AST node

Parameters
setCharacter set string (copied, or NULL for any char)
lenLength of set (0 if NULL)
Returns
New AST node (caller owns)

◆ snobol_ast_create_arbno()

ast_node_t * snobol_ast_create_arbno ( ast_node_t * sub)

Create an arbno (zero-or-more) AST node

Parameters
subSub-pattern (ownership transferred)
Returns
New AST node (caller owns)

◆ snobol_ast_create_assign()

ast_node_t * snobol_ast_create_assign ( int var,
int reg )

Create AST_ASSIGN node

◆ snobol_ast_create_bal()

ast_node_t * snobol_ast_create_bal ( uint32_t open_cp,
uint32_t close_cp )

Create AST_BAL node

◆ snobol_ast_create_break()

ast_node_t * snobol_ast_create_break ( const char * set,
size_t len )

Create AST_BREAK node

◆ snobol_ast_create_breakx()

ast_node_t * snobol_ast_create_breakx ( const char * set,
size_t len )

Create AST_BREAKX node

◆ snobol_ast_create_cap()

ast_node_t * snobol_ast_create_cap ( int reg,
ast_node_t * sub )

Create a capture AST node

Parameters
regRegister number
subSub-pattern to capture (ownership transferred)
Returns
New AST node (caller owns)

◆ snobol_ast_create_concat()

ast_node_t * snobol_ast_create_concat ( ast_node_t ** parts,
size_t count )

Create a concatenation AST node

Parameters
partsArray of child nodes (ownership transferred)
countNumber of parts
Returns
New AST node (caller owns)

◆ snobol_ast_create_dynamic_eval()

ast_node_t * snobol_ast_create_dynamic_eval ( ast_node_t * expr)

Create AST_DYNAMIC_EVAL node

◆ snobol_ast_create_emit()

ast_node_t * snobol_ast_create_emit ( const char * text,
size_t len,
int reg )

Create AST_EMIT node

◆ snobol_ast_create_eval()

ast_node_t * snobol_ast_create_eval ( int fn,
int reg )

Create AST_EVAL node

◆ snobol_ast_create_fail()

ast_node_t * snobol_ast_create_fail ( void )

Create AST_FAIL node (no args)

◆ snobol_ast_create_fence()

ast_node_t * snobol_ast_create_fence ( void )

Create AST_FENCE node (no args)

◆ snobol_ast_create_goto()

ast_node_t * snobol_ast_create_goto ( const char * label)

Create a goto AST node

Parameters
labelTarget label name (copied)
Returns
New AST node (caller owns)

◆ snobol_ast_create_label()

ast_node_t * snobol_ast_create_label ( char * name,
ast_node_t * target )

Create a label AST node

Parameters
nameLabel name (copied, ownership transferred)
targetTarget pattern (ownership transferred)
Returns
New AST node (caller owns)

◆ snobol_ast_create_len()

ast_node_t * snobol_ast_create_len ( int32_t n)

Create AST_LEN node

◆ snobol_ast_create_lit()

ast_node_t * snobol_ast_create_lit ( const char * text,
size_t len )

Create a literal AST node

Parameters
textLiteral text (copied, must be freed by caller if not using create_lit)
lenLength of text
Returns
New AST node (caller owns, must free with snobol_ast_free)

◆ snobol_ast_create_literal()

ast_node_t * snobol_ast_create_literal ( const char * text,
size_t len )

Alias for snobol_ast_create_lit

◆ snobol_ast_create_notany()

ast_node_t * snobol_ast_create_notany ( const char * set,
size_t len )

Create AST_NOTANY node

◆ snobol_ast_create_pos()

ast_node_t * snobol_ast_create_pos ( int32_t n)

Create AST_POS node

◆ snobol_ast_create_regref()

ast_node_t * snobol_ast_create_regref ( int reg)

Create AST_REG_REF node (value of register vN as a table key).

◆ snobol_ast_create_rem()

ast_node_t * snobol_ast_create_rem ( void )

Create AST_REM node (no args)

◆ snobol_ast_create_repeat()

ast_node_t * snobol_ast_create_repeat ( ast_node_t * sub,
int32_t min,
int32_t max )

Create a repetition AST node

Parameters
subSub-pattern (ownership transferred)
minMinimum repetitions
maxMaximum repetitions (-1 for unbounded)
Returns
New AST node (caller owns)

◆ snobol_ast_create_rpos()

ast_node_t * snobol_ast_create_rpos ( int32_t n)

Create AST_RPOS node

◆ snobol_ast_create_rtab()

ast_node_t * snobol_ast_create_rtab ( int32_t n)

Create AST_RTAB node

◆ snobol_ast_create_span()

ast_node_t * snobol_ast_create_span ( const char * set,
size_t len )

Create a span AST node

Parameters
setCharacter set string (copied)
lenLength of set
Returns
New AST node (caller owns)

◆ snobol_ast_create_succeed()

ast_node_t * snobol_ast_create_succeed ( void )

Create AST_SUCCEED node (no args)

◆ snobol_ast_create_tab()

ast_node_t * snobol_ast_create_tab ( int32_t n)

Create AST_TAB node

◆ snobol_ast_create_table_access()

ast_node_t * snobol_ast_create_table_access ( const char * table,
ast_node_t * key )

Create AST_TABLE_ACCESS node

◆ snobol_ast_create_table_update()

ast_node_t * snobol_ast_create_table_update ( const char * table,
ast_node_t * key,
ast_node_t * value )

Create AST_TABLE_UPDATE node

◆ snobol_ast_depth()

uint32_t snobol_ast_depth ( const ast_node_t * node)

Subtree height of an AST node.

The value is maintained by the node factories, so this is O(1): a leaf is 1 and NULL is 0.

Parameters
nodeAST node, or NULL
Returns
Subtree height in nodes

◆ snobol_ast_dump()

void snobol_ast_dump ( const ast_node_t * node,
FILE * out,
int indent )

Dump AST to file for debugging

Parameters
nodeRoot node
outFile to write to (e.g., stdout)
indentCurrent indentation level (0 for root)

◆ snobol_ast_free()

void snobol_ast_free ( ast_node_t * node)

Free an AST node and all children recursively

Parameters
nodeNode to free (NULL is safe)

◆ snobol_ast_get_version()

snobol_ast_version_t snobol_ast_get_version ( void )

Get AST version information

Returns
Version structure with major, minor, patch, and string representation

◆ snobol_ast_set_arena()

void snobol_ast_set_arena ( snobol_arena_t * arena)

Bind a bump arena that subsequent AST node allocations draw from. Pass NULL to revert to the default heap (calloc) allocator. The binding is thread-local so concurrent compiles on different threads use independent arenas. When the arena is exhausted, allocations transparently fall back to calloc. Owned sub-allocations (strings, concat part arrays) always use the heap. The caller is responsible for resetting/freeing the arena after the tree has been consumed (e.g. via snobol_ast_free followed by snobol_arena_reset).

Parameters
arenaArena to use, or NULL for the default allocator.

◆ snobol_ast_type_name()

const char * snobol_ast_type_name ( ast_type_t type)

Get a string representation of AST node type for debugging

Parameters
typeNode type
Returns
Static string (do not free)

◆ snobol_ast_version_check()

bool snobol_ast_version_check ( uint16_t required_major,
uint16_t required_minor )

Check if AST library version is compatible with required version

Parameters
required_majorRequired major version
required_minorRequired minor version
Returns
true if compatible (same major, minor >= required)

◆ snobol_ast_version_string()

const char * snobol_ast_version_string ( void )

Get AST version as a string (e.g., "1.0.0")

Returns
Version string (static, do not free)