Skip to content

Commit

Permalink
Messing more with scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Jul 13, 2024
1 parent 3acba85 commit 8f802e3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serializer-json = ["common/json"]
serializer-ron = ["common/ron"]
serializer-xml = ["common/xml"]
serializer-yaml = ["common/yaml"]
lang-basic = ["scripting/basic"]
lang-wren = ["scripting/wren"]
lang-lua = ["scripting/lua"]

Expand Down
1 change: 1 addition & 0 deletions crates/scripting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ version.workspace = true
common = { package = "surreal-common", path = "../common" }

[features]
basic = []
wren = []
lua = []
46 changes: 38 additions & 8 deletions crates/scripting/src/lang.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//! Scripting language abstractions
use common::ToVirtualPath;
#[cfg(feature = "lua")]
pub use lua::*;
#[cfg(feature = "wren")]
pub use wren::*;
use common::{Singleton, ToVirtualPath};

use super::*;

#[cfg(feature = "basic")]
pub mod basic;
#[cfg(feature = "lua")]
mod lua;
pub mod lua;
#[cfg(feature = "wren")]
mod wren;
pub mod wren;

/// The shared virtual machine for all scripts
#[derive(Singleton, Default)]
struct ScriptVirtualMachine {}

/// Represents a scripting language
pub trait ScriptLanguage {
Expand All @@ -22,7 +26,30 @@ pub struct Script {
module: ast::Module,
}

impl Script {
/// Loads a script from the given path
pub fn from_path<L: ScriptLanguage>(path: impl ToVirtualPath) -> Result<Self, ScriptError> {
L::load(path)
}

/// Executes the script
pub fn execute(&self) {
todo!()
}

/// Evaluates the script with the given arguments
pub fn evaluate(&self, _arguments: &[Variant]) -> Vec<Variant> {
todo!()
}

/// Calls the given top-level function with the given arguments
pub fn call(&self, _name: &str, _arguments: &[Variant]) -> Vec<Variant> {
todo!()
}
}

/// Represents an error that occurred while parsing a script
#[derive(Debug, Eq, PartialEq)]
pub enum ScriptError {
NotFound,
ParseError,
Expand All @@ -31,7 +58,10 @@ pub enum ScriptError {
mod ast {
//! The internal AST representation of a script
pub struct Module {}
pub struct Module {
pub name: String,
}

pub enum Statement {}
pub enum Expression {}
pub enum Literal {}
Expand Down
24 changes: 24 additions & 0 deletions crates/scripting/src/lang/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! BASIC language support for Surreal
use super::*;

/// The BASIC script language
pub struct BASIC;

impl ScriptLanguage for BASIC {
fn load(_path: impl ToVirtualPath) -> Result<Script, ScriptError> {
todo!()
}
}

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

#[test]
fn test_load_basic_files_from_file_system() {
let script = Script::from_path::<BASIC>("tests/test.basic").unwrap();

assert_eq!(script.module.name, "test");
}
}
51 changes: 47 additions & 4 deletions crates/scripting/src/lang/wren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,67 @@
use super::*;

/// The Wren scripting language and virtual machine.
/// The Wren scripting language.
pub struct Wren;

impl ScriptLanguage for Wren {
fn load(path: impl ToVirtualPath) -> Result<Script, ScriptError> {
let path = path.to_virtual_path();
let text = path.read_all_text().map_err(|_| ScriptError::ParseError)?;

parser::parse(&text).map_err(|_| ScriptError::ParseError)?;
let text = path.read_all_text().map_err(|_| ScriptError::ParseError)?;
let module = parser::parse(&text).map_err(|_| ScriptError::ParseError)?;

todo!()
Ok(Script { module })
}
}

mod parser {
use common::{StringSpan, ToStringSpan};

use super::*;

struct Token<'a> {
span: StringSpan<'a>,
kind: TokenKind,
}

enum TokenKind {
Identifier,
Keyword,
Operator,
Literal,
Comment,
}

pub fn parse(text: &str) -> Result<ast::Module, ()> {
let mut tokens = Vec::new();
let mut start = 0;

for (i, c) in text.chars().enumerate() {
if c.is_alphabetic() {
if start < i {
tokens.push(Token {
span: text.to_string_span().slice(start, i),
kind: TokenKind::Identifier,
});
}

start = i + 1;
}
}

todo!()
}
}

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

#[test]
fn test_load_wren_files_from_file_system() {
let script = Script::from_path::<Wren>("tests/test.wren").unwrap();

assert_eq!(script.module.name, "test");
}
}

0 comments on commit 8f802e3

Please sign in to comment.