about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-14T19·49+0300
committertazjin <mail@tazj.in>2021-01-14T20·26+0000
commit39439d59e8e9ddb1e2b7802f3aff092d77de7acf (patch)
treee7c808364ec2078faa42739877d4ffdba5bd4ec9 /users/tazjin/rlox/src/interpreter.rs
parent20a6cfeee233bde9ba1f482fa4545f5e395d6235 (diff)
feat(tazjin/rlox): Implement early return from functions r/2108
In the book this is implemented via exceptions as control flow, and
I'm sticking somewhat closely to that by doing it via an error
variant.

Change-Id: I9c7b84d6bb28265ab94021ea681df84f16a53da2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2395
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index 5fb5283bc791..5fdde9adac68 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -40,7 +40,18 @@ impl Callable {
                     fn_env.define(param, value)?;
                 }
 
-                lox.interpret_block(Rc::new(RwLock::new(fn_env)), &func.body)
+                let result = lox.interpret_block(Rc::new(RwLock::new(fn_env)), &func.body);
+
+                match result {
+                    // extract returned values if applicable
+                    Err(Error {
+                        kind: ErrorKind::FunctionReturn(value),
+                        ..
+                    }) => Ok(value),
+
+                    // otherwise just return the result itself
+                    _ => result,
+                }
             }
         }
     }
@@ -221,6 +232,12 @@ impl Interpreter {
             Statement::If(if_stmt) => return self.interpret_if(if_stmt),
             Statement::While(while_stmt) => return self.interpret_while(while_stmt),
             Statement::Function(func) => return self.interpret_function(func.clone()),
+            Statement::Return(ret) => {
+                return Err(Error {
+                    line: 0,
+                    kind: ErrorKind::FunctionReturn(self.eval(&ret.value)?),
+                })
+            }
         };
 
         Ok(value)