robinpath 0.2.0

RobinPath - A lightweight, fast scripting language interpreter for automation and data processing
Documentation
pub mod ast;
pub mod error;
pub mod executor;
pub mod lexer;
pub mod modules;
pub mod parser;
pub mod stream;
pub mod value;

pub use error::RobinPathError;
pub use executor::{BuiltinCallback, BuiltinFn, Environment};
pub use value::Value;

use executor::Executor;
use lexer::Lexer;
use parser::Parser;

pub struct RobinPath {
    executor: Executor,
}

impl RobinPath {
    pub fn new() -> Self {
        let mut env = Environment::new();
        modules::register_all_modules(&mut env);
        Self {
            executor: Executor::new(env),
        }
    }

    pub fn with_environment(env: Environment) -> Self {
        Self {
            executor: Executor::new(env),
        }
    }

    /// Parse source code into AST
    pub fn parse(source: &str) -> Result<Vec<ast::Stmt>, RobinPathError> {
        let tokens = Lexer::new(source).tokenize()?;
        let stmts = Parser::new(tokens).parse()?;
        Ok(stmts)
    }

    /// Execute source code and return the final value
    pub fn execute(&mut self, source: &str) -> Result<Value, RobinPathError> {
        let stmts = Self::parse(source)?;
        self.executor.execute(&stmts)
    }

    /// Execute pre-parsed AST
    pub fn execute_ast(&mut self, stmts: &[ast::Stmt]) -> Result<Value, RobinPathError> {
        self.executor.execute(stmts)
    }

    /// Get captured output lines
    pub fn output(&self) -> &[String] {
        &self.executor.environment.output
    }

    /// Clear captured output
    pub fn clear_output(&mut self) {
        self.executor.environment.output.clear();
    }

    /// Get a variable value
    pub fn get_variable(&self, name: &str) -> Option<&Value> {
        self.executor.environment.variables.get(name)
    }

    /// Set a variable
    pub fn set_variable(&mut self, name: &str, value: Value) {
        self.executor.environment.variables.insert(name.to_string(), value);
    }

    /// Register an external builtin function (for use by extension crates)
    pub fn register_builtin(
        &mut self,
        name: &str,
        func: impl Fn(&[Value], Option<&BuiltinCallback>) -> Result<Value, String> + Send + Sync + 'static,
    ) {
        self.executor.environment.register_builtin(name, func);
    }
}

impl Default for RobinPath {
    fn default() -> Self {
        Self::new()
    }
}