Skip to content

Commit ae61e9a

Browse files
committed
Some more work on scripting runtime
1 parent e27163d commit ae61e9a

File tree

6 files changed

+71
-30
lines changed

6 files changed

+71
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ members = ["core/*", "backends/*", "editor"]
1717
opt-level = 3
1818

1919
[features]
20-
default = ["audio", "graphics", "input"]
20+
default = ["audio", "graphics", "input"]
2121

2222
[workspace.dependencies]
2323
# shared dependencies
@@ -47,4 +47,3 @@ required-features = ["desktop"]
4747
[[example]]
4848
name = "hello-world"
4949
required-features = ["desktop"]
50-

core/scripting/src/lang/wren.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,7 @@ mod tests {
500500
};
501501
}
502502

503-
// parse_file_test!(it_should_parse_test01,
504-
// "assets/scripts/wren/test01.wren"); parse_file_test!
505-
// (it_should_parse_test02, "assets/scripts/wren/test02.wren");
506-
// parse_file_test!(it_should_parse_test03,
507-
// "assets/scripts/wren/test03.wren");
503+
parse_file_test!(it_should_parse_test01, "assets/scripts/wren/test01.wren");
504+
parse_file_test!(it_should_parse_test02, "assets/scripts/wren/test02.wren");
505+
parse_file_test!(it_should_parse_test03, "assets/scripts/wren/test03.wren");
508506
}

core/scripting/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
//! Scripting engine for Surreal
22
3-
pub use lang::*;
4-
pub use runtime::*;
5-
6-
mod lang;
7-
mod runtime;
3+
pub mod lang;
4+
pub mod runtime;

core/scripting/src/runtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Runtime components for script engine.
22
3-
mod compiler;
4-
mod isolates;
5-
mod machine;
3+
pub mod compiler;
4+
pub mod isolates;
5+
pub mod machine;
66

77
/// A bytecode instruction for the virtual machine.
88
#[derive(Debug, PartialEq)]
9-
enum Opcode {
9+
pub enum Opcode {
1010
NoOp,
1111
Return,
1212
Constant(u16),

core/scripting/src/runtime/compiler.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ use crate::{lang::ast::*, runtime::Opcode};
44
#[derive(Debug)]
55
pub enum CompileError {}
66

7+
/// Compiles a single expression into a sequence of opcodes
8+
pub fn compile_expression(expression: &Expression) -> Result<Vec<Opcode>, CompileError> {
9+
let mut compiler = Compiler::default();
10+
11+
compiler.compile_expression(expression)?;
12+
13+
Ok(compiler.instructions)
14+
}
15+
716
/// Compiles a set of [`Statement`]s into [`Opcode`]s.
8-
pub fn compile(statements: &[Statement]) -> Result<Vec<Opcode>, CompileError> {
17+
pub fn compile_statements(statements: &[Statement]) -> Result<Vec<Opcode>, CompileError> {
918
let mut compiler = Compiler::default();
1019

1120
for statement in statements {
@@ -22,6 +31,7 @@ struct Compiler {
2231
}
2332

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

51+
/// Compiles a single expression into a sequence of opcodes.
4152
pub fn compile_expression(&mut self, expression: &Expression) -> Result<(), CompileError> {
4253
match expression {
4354
Expression::Literal(literal) => {
4455
let value = literal.clone();
45-
4656
self.instructions.push(Opcode::Literal(value))
4757
}
4858
Expression::Binary(left, operator, right) => {
4959
self.compile_expression(left)?;
5060
self.compile_expression(right)?;
51-
5261
self.instructions.push(Opcode::Binary(*operator));
5362
}
5463
Expression::Unary(operator, value) => {
5564
self.compile_expression(value)?;
56-
5765
self.instructions.push(Opcode::Unary(*operator));
5866
}
5967
}
@@ -68,21 +76,42 @@ mod tests {
6876

6977
use super::*;
7078

71-
#[test]
72-
fn test_compile_basic_program() {
73-
let statements = vec![Statement::Return(Expression::Binary(
79+
macro_rules! compile_test {
80+
($name:ident, $statements:expr, $expected:expr) => {
81+
#[test]
82+
fn $name() {
83+
let instructions = compile_statements($statements).unwrap();
84+
assert_eq!(instructions, $expected);
85+
}
86+
};
87+
}
88+
89+
compile_test!(
90+
test_compile_basic_program,
91+
&vec![Statement::Return(Expression::Binary(
7492
Box::new(Expression::Literal(Variant::I64(1))),
7593
BinaryOp::Add,
7694
Box::new(Expression::Literal(Variant::I64(2))),
77-
))];
78-
79-
let instructions = compile(&statements).unwrap();
80-
81-
assert_eq!(instructions, vec![
95+
))],
96+
vec![
8297
Opcode::Literal(Variant::I64(1)),
8398
Opcode::Literal(Variant::I64(2)),
8499
Opcode::Binary(BinaryOp::Add),
85100
Opcode::Return,
86-
]);
87-
}
101+
]
102+
);
103+
104+
compile_test!(
105+
test_compile_single_expression,
106+
&vec![Statement::Expression(Expression::Binary(
107+
Box::new(Expression::Literal(Variant::I64(3))),
108+
BinaryOp::Multiply,
109+
Box::new(Expression::Literal(Variant::I64(4))),
110+
))],
111+
vec![
112+
Opcode::Literal(Variant::I64(3)),
113+
Opcode::Literal(Variant::I64(4)),
114+
Opcode::Binary(BinaryOp::Multiply),
115+
]
116+
);
88117
}

core/scripting/tests/wren.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use common::Variant;
2+
3+
#[test]
4+
pub fn it_should_compile_and_execute_wren_programs() {
5+
let program = "1 + 2 * 3";
6+
7+
let expression = surreal_scripting::lang::wren::parse(program).unwrap();
8+
let mut opcodes = surreal_scripting::runtime::compiler::compile_expression(&expression).unwrap();
9+
10+
opcodes.push(surreal_scripting::runtime::Opcode::Return);
11+
12+
println!("{:?}", opcodes);
13+
14+
let mut machine = surreal_scripting::runtime::machine::VirtualMachine::default();
15+
let result = machine.execute(&opcodes).unwrap();
16+
17+
assert_eq!(result, Some(Variant::I64(7)));
18+
}

0 commit comments

Comments
 (0)