Skip to content

Commit

Permalink
Some more work on scripting runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Nov 14, 2024
1 parent e27163d commit ae61e9a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 30 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ members = ["core/*", "backends/*", "editor"]
opt-level = 3

[features]
default = ["audio", "graphics", "input"]
default = ["audio", "graphics", "input"]

[workspace.dependencies]
# shared dependencies
Expand Down Expand Up @@ -47,4 +47,3 @@ required-features = ["desktop"]
[[example]]
name = "hello-world"
required-features = ["desktop"]

8 changes: 3 additions & 5 deletions core/scripting/src/lang/wren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,7 @@ mod tests {
};
}

// parse_file_test!(it_should_parse_test01,
// "assets/scripts/wren/test01.wren"); parse_file_test!
// (it_should_parse_test02, "assets/scripts/wren/test02.wren");
// parse_file_test!(it_should_parse_test03,
// "assets/scripts/wren/test03.wren");
parse_file_test!(it_should_parse_test01, "assets/scripts/wren/test01.wren");
parse_file_test!(it_should_parse_test02, "assets/scripts/wren/test02.wren");
parse_file_test!(it_should_parse_test03, "assets/scripts/wren/test03.wren");
}
7 changes: 2 additions & 5 deletions core/scripting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! Scripting engine for Surreal
pub use lang::*;
pub use runtime::*;

mod lang;
mod runtime;
pub mod lang;
pub mod runtime;
8 changes: 4 additions & 4 deletions core/scripting/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Runtime components for script engine.
mod compiler;
mod isolates;
mod machine;
pub mod compiler;
pub mod isolates;
pub mod machine;

/// A bytecode instruction for the virtual machine.
#[derive(Debug, PartialEq)]
enum Opcode {
pub enum Opcode {
NoOp,
Return,
Constant(u16),
Expand Down
57 changes: 43 additions & 14 deletions core/scripting/src/runtime/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ use crate::{lang::ast::*, runtime::Opcode};
#[derive(Debug)]
pub enum CompileError {}

/// Compiles a single expression into a sequence of opcodes
pub fn compile_expression(expression: &Expression) -> Result<Vec<Opcode>, CompileError> {
let mut compiler = Compiler::default();

compiler.compile_expression(expression)?;

Ok(compiler.instructions)
}

/// Compiles a set of [`Statement`]s into [`Opcode`]s.
pub fn compile(statements: &[Statement]) -> Result<Vec<Opcode>, CompileError> {
pub fn compile_statements(statements: &[Statement]) -> Result<Vec<Opcode>, CompileError> {
let mut compiler = Compiler::default();

for statement in statements {
Expand All @@ -22,6 +31,7 @@ struct Compiler {
}

impl Compiler {
/// Compiles a single statement into a sequence of opcodes.
pub fn compile_statement(&mut self, statement: &Statement) -> Result<(), CompileError> {
match statement {
Statement::Expression(expression) => {
Expand All @@ -38,22 +48,20 @@ impl Compiler {
Ok(())
}

/// Compiles a single expression into a sequence of opcodes.
pub fn compile_expression(&mut self, expression: &Expression) -> Result<(), CompileError> {
match expression {
Expression::Literal(literal) => {
let value = literal.clone();

self.instructions.push(Opcode::Literal(value))
}
Expression::Binary(left, operator, right) => {
self.compile_expression(left)?;
self.compile_expression(right)?;

self.instructions.push(Opcode::Binary(*operator));
}
Expression::Unary(operator, value) => {
self.compile_expression(value)?;

self.instructions.push(Opcode::Unary(*operator));
}
}
Expand All @@ -68,21 +76,42 @@ mod tests {

use super::*;

#[test]
fn test_compile_basic_program() {
let statements = vec![Statement::Return(Expression::Binary(
macro_rules! compile_test {
($name:ident, $statements:expr, $expected:expr) => {
#[test]
fn $name() {
let instructions = compile_statements($statements).unwrap();
assert_eq!(instructions, $expected);
}
};
}

compile_test!(
test_compile_basic_program,
&vec![Statement::Return(Expression::Binary(
Box::new(Expression::Literal(Variant::I64(1))),
BinaryOp::Add,
Box::new(Expression::Literal(Variant::I64(2))),
))];

let instructions = compile(&statements).unwrap();

assert_eq!(instructions, vec![
))],
vec![
Opcode::Literal(Variant::I64(1)),
Opcode::Literal(Variant::I64(2)),
Opcode::Binary(BinaryOp::Add),
Opcode::Return,
]);
}
]
);

compile_test!(
test_compile_single_expression,
&vec![Statement::Expression(Expression::Binary(
Box::new(Expression::Literal(Variant::I64(3))),
BinaryOp::Multiply,
Box::new(Expression::Literal(Variant::I64(4))),
))],
vec![
Opcode::Literal(Variant::I64(3)),
Opcode::Literal(Variant::I64(4)),
Opcode::Binary(BinaryOp::Multiply),
]
);
}
18 changes: 18 additions & 0 deletions core/scripting/tests/wren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use common::Variant;

#[test]
pub fn it_should_compile_and_execute_wren_programs() {
let program = "1 + 2 * 3";

let expression = surreal_scripting::lang::wren::parse(program).unwrap();
let mut opcodes = surreal_scripting::runtime::compiler::compile_expression(&expression).unwrap();

opcodes.push(surreal_scripting::runtime::Opcode::Return);

println!("{:?}", opcodes);

let mut machine = surreal_scripting::runtime::machine::VirtualMachine::default();
let result = machine.execute(&opcodes).unwrap();

assert_eq!(result, Some(Variant::I64(7)));
}

0 comments on commit ae61e9a

Please sign in to comment.