This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Bracmat is a programming language implemented in C (C99) for exploration and transformation of complex/unstructured data via pattern matching on tree structures (strings, XML, HTML, JSON, algebraic expressions). It includes a floating-point array subsystem called UFP (Unfancyfied Floating Point) added in 2023.
The project has two compilation modes that must both remain working:
From src/ (modular — each .c compiled separately):
cd src
make # Standard build with readline (Linux/macOS), requires libreadline-dev
make potu1 # Single-source mode (-DSINGLESOURCE), no readline
make potusafe # Sandboxed build (no file ops, system calls — for JNI/Python use)
make potuurl # Like potu, plus HTTP/HTTPS fetching via libcurl (-DHAVE_LIBCURL)From singlesource/ (single auto-generated file):
cd singlesource
make # Builds bracmat from generated bracmat.c
gcc bracmat.c -lm # Minimal manual build (C99 required)
make bracmaturl # Like bracmat, plus libcurl support (-DHAVE_LIBCURL)singlesource/bracmat.c is auto-generated by the Bracmat script singlesource/one.bra — do not hand-edit it. Regenerate it by running make in singlesource/.
Install dependencies (Linux):
sudo apt-get install libreadline-dev # required for potu / potuurl
sudo apt-get install libcurl4-openssl-dev # required for potuurl / bracmaturlThe test suite is valid.bra in the root. Run it interactively:
./src/bracmat
{?} get$"valid.bra"
{?} !rOr in batch mode:
./src/bracmat "get'\"valid.bra\";!r"Every bug fix and new feature requires a corresponding test added to valid.bra.
cd src && make profiling # Runs valid.bra; output: bracmat1.prof
cd src && make coverage # Runs valid.bra; then: gcov a-potu.cThe ~40 .c files compile in two modes:
- Normal mode: each
.cfile compiled independently; each must include all needed headers. - SINGLESOURCE mode:
potu.c#includes all other.cfiles in dependency order (low-level first). The.hfiles that contain declarations become inactive in this mode. This order mirrorssinglesource/bracmat.c.
input.c— Parses Bracmat source into an internal tree ofpsk(pointer-to-sk) nodes.evaluate.c— Main evaluator; dispatches on operator type (:match,&and,|or,=assign,$call, etc.).treematch.c/stringmatch.c— Tree and string pattern matching respectively;?varunifies,!vardereferences,@filters atoms,#filters numbers.canonization.c— Simplifies/normalizes expressions after evaluation.functions.c— ~80 built-in functions (lst,put,get,str,map,vap, etc.). When built with-DHAVE_LIBCURL,get$also acceptshttp:///https://URLs and fetches content via libcurl.
All data is a binary tree of sk nodes (defined in nodestruct.h). Node flags (30+ bits) encode the operator, type filters, and reference count in a single union. Key flag/macro definitions are in flags.h and nodedefs.h. Memory is managed via reference counting in memory.c.
| Module | Purpose |
|---|---|
rational.c |
Exact rational arithmetic |
calculation.c |
UFP floating-point arrays (64-bit doubles, multidimensional, no pattern matching) |
hash.c |
Hash-table objects |
xml.c |
XML/SGML/HTML parsing |
json.c |
JSON parsing and output |
object.c / objectdef.c |
OOP: classes, instances, methods |
variables.c |
Dynamic (not lexical) scoping, unification |
encoding.c / unicaseconv.c / unichartypes.c |
Unicode support; tables generated from UnicodeData.txt via UnicodeData.bra |
potu.c is the main entry point. It handles interactive REPL (with optional GNU readline), batch/command-line mode, and — when SINGLESOURCE is defined — #includes all other .c files.
- Python:
Python-module/prythat.pyx(Cython wrapper) - Java:
java-JNI/dk_cst_bracmat.c(JNI wrapper) - WebAssembly:
WebAssembly/(emscripten; seeemscriptenHowToHTML.sh)
The potusafe / bracmatsafe targets disable: -DNO_C_INTERFACE -DNO_FILE_RENAME -DNO_FILE_REMOVE -DNO_SYSTEM_CALL -DNO_LOW_LEVEL_FILE_HANDLING. Use this variant when embedding Bracmat as a library in JNI or Python modules.
| Construct | Meaning |
|---|---|
?x |
Unify/bind variable x |
!x |
Dereference variable x |
@ |
Atom (non-compound) filter in pattern |
# |
Number filter in pattern |
e1:e2 |
Match e1 against pattern e2 |
e1&e2 |
Evaluate e1, then e2 (and) |
e1|e2 |
Try e1, on failure try e2 (or) |
f$arg |
Call function f with argument arg |
v=body |
Define/assign v |
lex.bra— Detects unused variables/functions and variables used outside their lexical scope.project.bra— Wizard to scaffold a new Bracmat program.help.bra/doc/help— Language documentation in Bracmat format;get$"help"in the REPL loads it. Generates HTML or LaTeX docs.