macro_rules! triton_program { {$($source_code:tt)*} => { ... }; }
Expand description
Compile an entire program written in Triton assembly.
Triton VM can run the resulting Program
; see there for
details.
It is possible to use string-like interpolation to insert instructions, arguments, labels, or other substrings into the program.
§Examples
let program = triton_program!(
read_io 1 push 5 mul
call check_eq_15
push 17 write_io 1
halt
// assert that the top of the stack is 15
check_eq_15:
push 15 eq assert
return
);
Any type with an appropriate Display
implementation can be
interpolated. This includes, for example, primitive types like u64
and &str
, but also
Instruction
s,
BFieldElement
s, and
Label
s, among others.
let element_0 = BFieldElement::new(0);
let label = "my_label";
let instruction_push = Instruction::Push(bfe!(42));
let dup_arg = 1;
let program = triton_program!(
push {element_0}
call {label} halt
{label}:
{instruction_push}
dup {dup_arg}
skiz recurse return
);
§Panics
Panics if the program cannot be parsed. Examples for parsing errors are:
- unknown (e.g. misspelled) instructions
- invalid instruction arguments, e.g.,
push 1.5
orswap 42
- missing or duplicate labels
- invalid labels, e.g., using a reserved keyword or starting a label with a digit
For a version that returns a Result
, see Program::from_code()
.