1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Simple example of using the Glyph VM
use glyph_runtime::{Instruction, VMConfig, Value, VM};
fn main() {
// Create VM with default configuration
let config = VMConfig::default();
let mut vm = VM::new(config);
// Create a simple program that:
// 1. Pushes two numbers
// 2. Adds them
// 3. Creates a list with the result
// 4. Returns the list
let program = vec![vec![
Instruction::Push(Value::Int(10)),
Instruction::Push(Value::Int(32)),
Instruction::Add,
Instruction::Push(Value::Str("Result".to_string())),
Instruction::Swap,
Instruction::MakeList(2),
Instruction::Halt,
]];
// Load and execute the program
vm.load_bytecode(program);
match vm.execute() {
Ok(result) => {
println!("Program result: {result}");
// Show telemetry if any
if !vm.telemetry().is_empty() {
println!("\nTelemetry:");
for (event, value) in vm.telemetry() {
println!(" {event}: {value}");
}
}
// Show memory usage
println!(
"\nMemory usage: {} bytes ({:.2}%)",
vm.memory_usage(),
vm.memory_usage_percentage()
);
}
Err(e) => {
eprintln!("Execution error: {e}");
}
}
}