Skip to content

otherfunc/otherfunc-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

otherfunc-core

Shared trait and types for the OtherFunc interpreter ecosystem.

Overview

Defines the Interpreter trait that all language crates implement, plus supporting types for async I/O and execution statistics.

Types

  • Interpreter trait — execute(code, input) -> Result<String, String>, with optional execute_with_io/resume_with_io for coroutine-style HTTP outbound and set_env for injecting secrets
  • IoRequest — enum yielded when an interpreter needs external I/O: HttpRequest { url, method, headers, body } or KvRequest { op, key, value }
  • KvOpGet, Set, or Delete for persistent key-value storage
  • ExecResultComplete(String) or NeedsIo(IoRequest), returned by execute_with_io
  • ExecutionStatsinstructions_executed: u64 + execution_time: Duration

As a Library

use otherfunc_core::Interpreter;

// All interpreter crates (otherfunc-brainfuck, otherfunc-forth, etc.)
// implement this trait:
fn run(interp: &mut dyn Interpreter, code: &str) -> Result<String, String> {
    interp.execute(code, "")
}

Coroutine I/O pattern

use otherfunc_core::{ExecResult, Interpreter};

fn run_with_io(interp: &mut dyn Interpreter, code: &str) -> Result<String, String> {
    match interp.execute_with_io(code, "")? {
        ExecResult::Complete(output) => Ok(output),
        ExecResult::NeedsIo(req) => {
            let response = "..."; // fulfill the I/O request
            match interp.resume_with_io(response)? {
                ExecResult::Complete(output) => Ok(output),
                ExecResult::NeedsIo(_) => todo!("loop until complete"),
            }
        }
    }
}

Tests

cargo test -p otherfunc-core

About

Shared Interpreter trait and types for OtherFunc

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages