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

Compiler for SNOBOL pattern AST to bytecode. More...

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

Go to the source code of this file.

Functions

int compile_ast_to_bytecode_c (ast_node_t *ast, bool case_insensitive, uint8_t **out_bc, size_t *out_len)
const char * compile_ast_error (void)
int compile_template_to_bytecode (const char *tpl, size_t len, uint8_t **out_bc, size_t *out_len)
void compiler_free (uint8_t *bc)
int snobol_template_bind_tables (uint8_t *bc, size_t bc_len, const char **names, const uint16_t *ids, size_t n)

Detailed Description

Compiler for SNOBOL pattern AST to bytecode.

Accepts C AST (ast_node_t*) from the C-based parser and compiles to bytecode.

Definition in file compiler.h.

Function Documentation

◆ compile_ast_error()

const char * compile_ast_error ( void )

Last specific error from compile_ast_to_bytecode_c().

Returns
Human-readable message for the most recent failed compile, or NULL when the last compile succeeded or failed without a specific message. The pointer is valid until the next compile call.

◆ compile_ast_to_bytecode_c()

int compile_ast_to_bytecode_c ( ast_node_t * ast,
bool case_insensitive,
uint8_t ** out_bc,
size_t * out_len )

Compile AST to bytecode

Parameters
astAST node to compile
case_insensitiveEnable case-insensitive matching
out_bcOutput bytecode buffer (allocated, caller must free via compiler_free())
out_lenOutput bytecode length
Returns
0 on success, -1 on error

◆ compile_template_to_bytecode()

int compile_template_to_bytecode ( const char * tpl,
size_t len,
uint8_t ** out_bc,
size_t * out_len )

Compile template string to bytecode

Parameters
tplTemplate string
lenTemplate length
out_bcOutput bytecode buffer (allocated, caller must free via compiler_free())
out_lenOutput bytecode length
Returns
0 on success, -1 on error

◆ compiler_free()

void compiler_free ( uint8_t * bc)

Free compiled bytecode

Parameters
bcBytecode buffer to free

◆ snobol_template_bind_tables()

int snobol_template_bind_tables ( uint8_t * bc,
size_t bc_len,
const char ** names,
const uint16_t * ids,
size_t n )

Resolve symbolic table names embedded in compiled template bytecode.

Walks bc looking for OP_EMIT_TABLE instructions whose table_id equals SNBL_TABLE_ID_UNBOUND (0xFFFF). For each such instruction the embedded name is compared against the names array; on a match the table_id field is patched in-place with the corresponding entry from ids.

The function patches as many entries as it can. If any name cannot be resolved it leaves the table_id at SNBL_TABLE_ID_UNBOUND and returns -1 after processing the entire bytecode.

Binding is a one-shot operation: already-bound entries (table_id != 0xFFFF) are left unchanged. The caller is responsible for external synchronisation if bc may be shared across threads.

Parameters
bcMutable compiled template bytecode buffer
bc_lenLength of bc in bytes
namesArray of n C-string table names (may be NULL entries)
idsArray of n table IDs corresponding to names
nNumber of entries in names and ids
Returns
0 on complete success, -1 if any name was unresolvable