Skip to main content

Module awk_builtins

Module awk_builtins 

Source
Expand description

AWK operation IDs for Op::ExtendedWide(id, payload) dispatch.

These IDs are used by AWK frontends (awkrs) to emit bytecode that drives AWK-specific semantics — field access, record I/O, print/printf, the string builtins (sub/gsub/split/sprintf/match/…), and special variables (NF/NR/FS/OFS/…) — through a registered AwkHost.

Unlike the universal arithmetic/array ops (which the VM executes natively), AWK ops cannot use fusevm::Value faithfully: AWK has POSIX numeric-string duality, CONVFMT/OFMT formatting, field/$0 coupling, and SUBSEP associative arrays. Those semantics live host-side. The VM dispatches the AWK op range to vm.awk_host (see crate::vm::VM::set_awk_host); operands that aren’t carried in the payload come from the value stack.

§Encoding

AWK ops are emitted as Op::ExtendedWide(id, payload) where id is one of the constants below (all >= AWK_OP_BASE). payload carries the inline integer operand for ops that need one (field index for field ops, argument count for variadic ops, name-pool index for special-variable ops). Value operands are pushed on the stack in source order before the op.

use fusevm::{ChunkBuilder, Op};
use fusevm::awk_builtins::*;

// `$1` — push the field index, then read the field.
let mut b = ChunkBuilder::new();
b.emit(Op::LoadInt(1), 1);
b.emit(Op::ExtendedWide(AWK_FIELD_GET, 0), 1);

Modules§

getline_source
getline source kinds carried in the AWK_GETLINE payload.
signal
AWK control-flow signal codes carried by Op::AwkSignal(code). The op halts the chunk; the frontend driver reads VM::awk_signal() and maps the code to its own control-flow (next record / next file / exit).

Constants§

AWK_AND
and(v1, v2, ...) — bitwise AND of ≥2 args. Payload = argument count.
AWK_ARRAY_CLEAR
delete arr — clear the whole array (no stack operand).
AWK_ARRAY_DELETE
delete arr[k] — stack [key].
AWK_ARRAY_EXISTS
(k in arr) — stack [key]; pushes Bool.
AWK_ARRAY_GET
arr[k] — stack [key]; pushes the element (auto-vivifies to “” per POSIX).
AWK_ARRAY_LEN
length(arr) — push the element count.
AWK_ARRAY_SET
arr[k] = v — stack [value, key].
AWK_ATAN2
atan2(y, x) — arctangent of y/x. Stack [y, x].
AWK_CHR
chr(n) — single-character string for codepoint n (empty if invalid). Pops one argument, pushes a string.
AWK_COMPL
compl(v) — bitwise complement. Stack [v].
AWK_COS
cos(x) — cosine (radians). Stack [x].
AWK_EXP
exp(x) — e^x. Stack [x].
AWK_FIELD_GET
$i — read field i. i is popped from the stack; pushes the field value.
AWK_FIELD_SET
$i = v — assign field i. Stack: [value, index]; rebuilds $0/NF.
AWK_GENSUB
gensub(re, repl, how [, target]) — return target (or $0 when omitted) with re matches replaced per how ("g"/"G" = all, positive integer = that occurrence), expanding & and \1..\9 backrefs in repl. Payload = argument count (3..=4). Pushes the result string. Host-bound: regex compilation honors IGNORECASE and the 3-arg form reads $0.
AWK_GETLINE
getline family. payload encodes the source (plain/var/file/cmd). Stack holds the source operand (file/cmd string) when applicable; pushes status (1 = record read, 0 = EOF, -1 = error).
AWK_GSUB
gsub(re, repl [, target]) — pops payload args; pushes substitution count.
AWK_INDEX
index(s, t) — stack [s, t]; pushes 1-based position or 0.
AWK_INT
int(x) — truncate toward zero. Stack [x].
AWK_INTDIV
intdiv(a, b) — integer (truncating) quotient a / b as a number. Non-bignum path only; division by zero pushes Undef (a host may raise the gawk fatal). Pops two arguments, pushes a number.
AWK_INTDIV0
intdiv0(a, b) — like intdiv, but division by zero yields 0 (the “safe” gawk variant; never errors). Pops two arguments, pushes a number.
AWK_LENGTH
length / length(x) — pops payload args (0 ⇒ length($0)).
AWK_LOG
log(x) — natural log. Stack [x].
AWK_LSHIFT
lshift(v, n) — left shift v by n & 0x3f bits. Stack [v, n].
AWK_MATCH
match(s, re) — stack [s, re]; sets RSTART/RLENGTH, pushes RSTART.
AWK_MKBOOL
mkbool(x)1 if x is truthy, else 0. Pops one argument, pushes a number.
AWK_MKTIME
mktime(datespec [, utc])"YYYY MM DD HH MM SS" → epoch seconds (or -1). Payload = argument count (1..=2). Pushes a number. Host-independent.
AWK_NF
NF — push the current field count.
AWK_OP_BASE
First AWK op ID. The VM routes ExtendedWide ops with id >= AWK_OP_BASE (and < AWK_OP_END) to the registered AwkHost. Chosen high to leave the low Extended/ExtendedWide id space free for generic frontend handlers (stryke, etc.) that use set_extension_wide_handler.
AWK_OP_END
One past the last AWK op ID (exclusive upper bound of the reserved range).
AWK_OR
or(v1, v2, ...) — bitwise OR of ≥2 args. Payload = argument count.
AWK_ORD
ord(s) — Unicode scalar value of the first character of s (0 if empty). Nullary payload-free; pops one argument, pushes a number.
AWK_PRINT
print a, b, ... — pops payload args, joins with OFS, ends with ORS.
AWK_PRINTF
printf fmt, a, ... — pops payload args (last popped is the format).
AWK_RAND
rand() — next pseudo-random number in [0, 1). Nullary; pushes a number. Advances the VM-owned LCG seed. Host-independent.
AWK_RSHIFT
rshift(v, n) — right shift v by n & 0x3f bits. Stack [v, n].
AWK_SET_RECORD
$0 = v — replace the whole record. Stack: [value]; resplits fields.
AWK_SIN
sin(x) — sine (radians). Stack [x].
AWK_SPECIAL_GET
Read a special AWK variable by name-pool index (FS/OFS/ORS/RS/ NR/FNR/SUBSEP/RSTART/RLENGTH/FILENAME/CONVFMT/OFMT/…).
AWK_SPECIAL_SET
Assign a special AWK variable by name-pool index. Stack: [value].
AWK_SPLIT
split(s, arr [, fs]) — pops payload args; pushes field count.
AWK_SPRINTF
sprintf(fmt, a, ...) — like AWK_PRINTF but pushes the formatted string.
AWK_SQRT
sqrt(x) — square root. Stack [x].
AWK_SRAND
srand([x]) — reseed the PRNG, returning the previous seed (low 32 bits). Payload = argument count (0 → seed from clock, 1 → seed from popped value).
AWK_STRFTIME
strftime([fmt [, ts [, utc]]]) — format a timestamp. Payload = argument count (0..=3). Pushes a string. Host-independent (chrono + system tz).
AWK_STRTONUM
strtonum(s) — parse 0x… hex, 0… octal, else longest decimal/float prefix. Stack [s]; returns a number. Host-independent.
AWK_SUB
sub(re, repl [, target]) — pops payload args; pushes substitution count.
AWK_SUBSTR
substr(s, m [, n]) — pops payload args (2 or 3).
AWK_SYSTIME
systime() — seconds since the Unix epoch. Nullary; pushes a number. Host-independent (reads the system clock via std::time, no deps).
AWK_TOLOWER
tolower(s) — stack [s].
AWK_TOUPPER
toupper(s) — stack [s].
AWK_XOR
xor(v1, v2, ...) — bitwise XOR of ≥2 args. Payload = argument count.

Functions§

is_awk_op
True when id falls in the reserved AWK op range.