openscript 0.1.0

High-performance AI-powered scripting language runtime
Documentation
//! OpenScript: High-Performance AI-Powered Scripting Language Runtime
//!
//! OpenScript is a modern scripting language designed for automation, AI integration,
//! and rapid prototyping. It combines the simplicity of shell scripting with the
//! power of AI services and modern language features.
//!
//! # Features
//!
//! - **AI Integration**: Native support for OpenAI GPT, DALL-E, and other AI services
//! - **High Performance**: Built in Rust with zero-cost abstractions
//! - **Memory Safe**: Guaranteed memory safety without garbage collection
//! - **Concurrent**: Built-in support for async/await and parallel execution
//! - **Extensible**: Plugin architecture for custom commands and integrations
//!
//! # Quick Start
//!
//! 
//! use openscript::{Runtime, Script};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let mut runtime = Runtime::new();
//!     let script = Script::parse("echo 'Hello, OpenScript!'")?;
//!     let result = runtime.execute(script).await?;
//!     println!("{}", result);
//!     Ok(())
//! }
//! 

#![warn(missing_docs, rust_2018_idioms)]
#![deny(unsafe_code)]

pub mod ast;
pub mod error;
pub mod lexer;
pub mod parser;
pub mod runtime;
pub mod value;

pub use error::{Error, Result};
// pub use runtime::Runtime;
pub use value::Value;

use ast::Statement;
use std::fmt;

/// Represents a parsed OpenScript program
#[derive(Debug, Clone)]
pub struct Script {
    statements: Vec<Statement>,
    source: String,
}

impl Script {
    /// Parse OpenScript source code into an executable script
    ///
    /// # Arguments
    ///
    /// * `source` - The OpenScript source code to parse
    ///
    /// # Returns
    ///
    /// A `Result` containing the parsed `Script` or a parse error
    ///
    /// # Examples
    ///
    /// 
    /// use openscript::Script;
    ///
    /// let script = Script::parse(r#"
    ///     name = "OpenScript"
    ///     echo "Hello from ${name}!"
    /// "#)?;
    /// # Ok::<(), openscript::Error>(())
    /// 
    pub fn parse(source: &str) -> Result<Self> {
        let mut lexer = lexer::Lexer::new(source);
        let tokens = lexer.tokenize()?;
        let mut parser = parser::Parser::new(tokens);
        let program = parser.parse()?;
        
        Ok(Script {
            statements: program.statements,
            source: source.to_string(),
        })
    }

    /// Get the parsed statements
    pub fn statements(&self) -> &[Statement] {
        &self.statements
    }

    /// Get the original source code
    pub fn source(&self) -> &str {
        &self.source
    }
}

impl fmt::Display for Script {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.source)
    }
}

/// Convenience function to execute a script string directly
///
/// This function creates a new runtime, parses the script, and executes it.
/// For better performance when running multiple scripts, create a `Runtime`
/// instance and reuse it.
///
/// # Arguments
///
/// * `source` - The OpenScript source code to execute
///
/// # Returns
///
/// A `Result` containing the execution result or an error
pub async fn execute(_source: &str) -> Result<Value> {
    // let script = Script::parse(source)?;
    // let mut runtime = Runtime::new();
    // runtime.execute(script).await
    unimplemented!()
}

#[cfg(test)]
mod tests {
    // use super::*;

    // #[tokio::test]
    // async fn test_basic_execution() {
    //     let result = execute("echo 'Hello, World!'").await.unwrap();
    //     assert_eq!(result.to_string(), "Hello, World!");
    // }

    // #[tokio::test]
    // async fn test_variable_assignment() {
    //     let result = execute(r#"
    //         name = "OpenScript"
    //         echo "Hello from ${name}!"
    //     "#).await.unwrap();
    //     assert_eq!(result.to_string(), "Hello from OpenScript!");
    // }
}