about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/errors.rs8
-rw-r--r--users/tazjin/rlox/src/bytecode/mod.rs34
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs9
3 files changed, 33 insertions, 18 deletions
diff --git a/users/tazjin/rlox/src/bytecode/errors.rs b/users/tazjin/rlox/src/bytecode/errors.rs
index 89ab1867a4e6..c7871bc384f2 100644
--- a/users/tazjin/rlox/src/bytecode/errors.rs
+++ b/users/tazjin/rlox/src/bytecode/errors.rs
@@ -1,3 +1,5 @@
+use std::fmt;
+
 #[derive(Debug)]
 pub enum ErrorKind {
     // CompileError,
@@ -10,4 +12,10 @@ pub struct Error {
     pub kind: ErrorKind,
 }
 
+impl fmt::Display for Error {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "[line NYI] Error: {:?}", self.kind)
+    }
+}
+
 pub type LoxResult<T> = Result<T, Error>;
diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs
index 2d83c4272239..31278f8c4b8d 100644
--- a/users/tazjin/rlox/src/bytecode/mod.rs
+++ b/users/tazjin/rlox/src/bytecode/mod.rs
@@ -9,19 +9,29 @@ mod value;
 mod vm;
 
 use chunk::Chunk;
-use opcode::OpCode;
+pub struct Interpreter {}
 
-pub fn main() {
-    let mut chunk: Chunk = Default::default();
+impl crate::Lox for Interpreter {
+    type Error = errors::Error;
+    type Value = value::Value;
 
-    let constant = chunk.add_constant(1.2);
-    chunk.add_op(OpCode::OpConstant(constant), 1);
+    fn create() -> Self {
+        Interpreter {}
+    }
 
-    let constant = chunk.add_constant(2.0);
-    chunk.add_op(OpCode::OpConstant(constant), 2);
-
-    chunk.add_op(OpCode::OpAdd, 3);
-    chunk.add_op(OpCode::OpReturn, 4);
-
-    vm::interpret(chunk).expect("it should work");
+    fn interpret(&mut self, _: String) -> Result<Self::Value, Vec<Self::Error>> {
+        let chunk: Chunk = Default::default();
+        vm::interpret(chunk).map_err(|e| vec![e])
+    }
 }
+
+// pub fn main() {
+//     let mut chunk: Chunk = Default::default();
+//     let constant = chunk.add_constant(1.2);
+//     chunk.add_op(OpCode::OpConstant(constant), 1);
+//     let constant = chunk.add_constant(2.0);
+//     chunk.add_op(OpCode::OpConstant(constant), 2);
+//     chunk.add_op(OpCode::OpAdd, 3);
+//     chunk.add_op(OpCode::OpReturn, 4);
+//     vm::interpret(chunk).expect("it should work");
+// }
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index fd91cd7be627..33bc358f44e8 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -32,7 +32,7 @@ macro_rules! binary_op {
 }
 
 impl VM {
-    fn run(&mut self) -> LoxResult<()> {
+    fn run(&mut self) -> LoxResult<Value> {
         loop {
             let op = &self.chunk.code[self.ip];
 
@@ -42,10 +42,7 @@ impl VM {
             self.ip += 1;
 
             match op {
-                OpCode::OpReturn => {
-                    println!("{:?}", self.pop());
-                    return Ok(());
-                }
+                OpCode::OpReturn => return Ok(self.pop()),
 
                 OpCode::OpConstant(idx) => {
                     let c = *self.chunk.constant(*idx);
@@ -66,7 +63,7 @@ impl VM {
     }
 }
 
-pub fn interpret(chunk: chunk::Chunk) -> LoxResult<()> {
+pub fn interpret(chunk: chunk::Chunk) -> LoxResult<Value> {
     let mut vm = VM {
         chunk,
         ip: 0,