glyph-runtime 0.0.1

Runtime execution engine for the Glyph programming language
Documentation
//! Example demonstrating capability-based security in the Glyph VM

use glyph_runtime::{Capability, Instruction, VMConfig, Value, VM};

fn main() {
    println!("=== Glyph VM Capability Demo ===\n");

    // Test 1: VM without network capability
    println!("Test 1: Attempting network access without capability");
    let config = VMConfig::default();
    let mut vm = VM::new(config);

    vm.load_bytecode(vec![vec![
        Instruction::Push(Value::Str("Checking network access...".to_string())),
        Instruction::TraceValue("attempt".to_string()),
        Instruction::RequireCapability("network.fetch".to_string()),
        Instruction::Push(Value::Str("Network access granted!".to_string())),
        Instruction::Halt,
    ]]);

    match vm.execute() {
        Ok(result) => println!("Result: {result}"),
        Err(e) => println!("Expected error: {e}"),
    }

    // Test 2: VM with proper capabilities
    println!("\nTest 2: Attempting audio with granted capability");
    let mut config = VMConfig::default();
    config.capabilities.grant(Capability::AudioSpeak);
    let mut vm = VM::new(config);

    vm.load_bytecode(vec![vec![
        Instruction::CheckCapability("audio.speak".to_string()),
        Instruction::JumpIfNot(4), // Skip if no capability
        Instruction::Push(Value::Str("Hello from Glyph!".to_string())),
        Instruction::TraceValue("audio_message".to_string()),
        Instruction::Push(Value::Str("Audio capability granted".to_string())),
        Instruction::Halt,
    ]]);

    match vm.execute() {
        Ok(result) => {
            println!("Result: {result}");
            for (event, value) in vm.telemetry() {
                println!("  Telemetry - {event}: {value}");
            }
        }
        Err(e) => println!("Error: {e}"),
    }

    // Test 3: Memory-limited VM
    println!("\nTest 3: Memory-limited execution");
    let mut config = VMConfig::default();
    config.capabilities.grant(Capability::MemoryLimited(1024)); // 1KB limit
    let mut vm = VM::new(config);

    // Create a program that builds a large list
    let mut program = vec![
        Instruction::Push(Value::Int(0)), // Counter
        Instruction::BindLocal("counter".to_string()),
    ];

    // Loop: Create lists until we hit memory limit or counter reaches 100
    let loop_start = 2;
    program.extend(vec![
        // Load and increment counter
        Instruction::LoadLocal("counter".to_string()),
        Instruction::Push(Value::Int(1)),
        Instruction::Add,
        Instruction::Dup,
        Instruction::BindLocal("counter".to_string()),
        // Check if counter >= 100
        Instruction::Push(Value::Int(100)),
        Instruction::Ge,
        Instruction::JumpIf(20), // Jump to exit
        // Create a list with some data
        Instruction::Push(Value::Str("Item".to_string())),
        Instruction::LoadLocal("counter".to_string()),
        Instruction::MakeList(2),
        Instruction::TraceValue("created_list".to_string()),
        // Continue loop
        Instruction::Jump(loop_start),
        // Exit
        Instruction::Push(Value::Str("Completed successfully".to_string())),
        Instruction::Halt,
    ]);

    vm.load_bytecode(vec![program]);

    match vm.execute() {
        Ok(result) => {
            println!("Result: {result}");
            println!(
                "Memory usage: {} bytes ({:.2}%)",
                vm.memory_usage(),
                vm.memory_usage_percentage()
            );

            let telemetry = vm.telemetry();
            println!("Created {} lists", telemetry.len());
        }
        Err(e) => println!("Error: {e}"),
    }

    // Test 4: Pattern matching capabilities
    println!("\nTest 4: Pattern-based capability matching");
    let mut config = VMConfig::default();
    config.capabilities.grant(Capability::NetworkFetch(
        "https://api.example.com/*".to_string(),
    ));
    let mut vm = VM::new(config);

    vm.load_bytecode(vec![vec![
        // Check allowed URL
        Instruction::Push(Value::Str("https://api.example.com/data".to_string())),
        Instruction::TraceValue("checking_url".to_string()),
        Instruction::CheckCapability("network.fetch".to_string()),
        Instruction::TraceValue("capability_check_result".to_string()),
        // Result
        Instruction::Push(Value::Str("Pattern capability check complete".to_string())),
        Instruction::Halt,
    ]]);

    match vm.execute() {
        Ok(result) => {
            println!("Result: {result}");
            for (event, value) in vm.telemetry() {
                println!("  {event}: {value}");
            }
        }
        Err(e) => println!("Error: {e}"),
    }
}