_*_ _
 / \

hex

\* programming language *\_/_/_/ *

Standard Library

hex comes with a standard library providing common functionality implemented in hex itself.

At present, it only comprises utils.hex, a collection of common utility symbols, which is distributed in .hbx format along with the hex executable. To pre-load this file, you can start hex like this:

hex -l utils.hbx

Standard Utilities (utils.hex)

This file contains a collection of standardized utility symbols extending the native symbols of the hex programming language.

Input/Output Symbols

These additional symbols are used for input and output operations.

dputs Symbol

a → a

Duplicates and prints the top item on the stack followed by a new line.

Control Flow Symbols

These symbols are used for controlling the flow of execution in a program.

unless Symbol

q1 q2 → *

If q1 pushes 0x0 on the stack, dequotes q2.

when Symbol

q1 q2 → *

If q1 pushes 0x1 on the stack, dequotes q2.

Quotation Symbols

These additional symbols are used for manipulating quotations.

cons Symbol

a q1 → q2

Prepends a to the beginning of q1.

each Symbol

q1 q2 → *

Applies q2 to each element of q1.

filter Symbol

q1 q2→ q3

Returns q3 containing only the elements of q1 that satisfy q2.

max Symbol

q → a

Pushes the maximum item of q on the stack.

min Symbol

q → a

Pushes the minimum item of q on the stack.

pop Symbol

q1 → q2

Removes the last item at the end of q1.

push Symbol

q1 a → q2

Appends a to the end of q1.

rem Symbol

q1 i → q2

Removes the item at index i of q1.

reverse Symbol

q1 → q2

Reverses the order of the items in a quotation.

set Symbol

q1 a i → q2

Sets the item at index i of q1 to a.

sort Symbol

q1 q2 → q3

Sorts the items of q1 based on the comparison quotation q2 (must push 0x0 or 0x1 on the stack).

swons Symbol

q1 a → q2

Prepends a to the beginning of q1.

times Symbol

q i → *

Execute quotation q i times.

uncons Symbol

q1 → a q2

Removes the first item from q1 and pushes it on the stack along with q2 containing the rest of the items of q1.

unswons Symbol

q1 → q2 a

Removes the first item from q1 and pushes it on the stack along with q2 containing the rest of the items of q1.

Stack Management Symbols

These additional symbols are used for manipulating the stack.

dip Symbol

a1 (a2) → a2 a1

Dequotes the first item on the stack and restores the second item on top.

over Symbol

a1 a2 → a1 a2 a1

Copies the second item on the stack and pushes it on top

String Symbols

These additional symbols are used for manipulating strings.

begins Symbol

s1 s2 → s3

Pushes 0x1 on the stack if s1 begins with s2, or 0x0 otherwise.

ends Symbol

s1 s2 → s3

Pushes 0x1 on the stack if s1 ends with s2, or 0x0 otherwise.

fmt Symbol

s1 q → s2

Substitutes ${0} to ${9} placeholders in s1 with items in q.

gsub Symbol

s1 s2 s3 → s4

Replaces all occurrences of s2 with s3 in s1.

slice Symbol

s1 i1 i2 → s2

Extracts the portion of the string between indices i1 and i2.