feat(tazjin/rlox): Implement early return from functions

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
This commit is contained in:
Vincent Ambo 2021-01-14 22:49:07 +03:00 committed by tazjin
parent 20a6cfeee2
commit 39439d59e8
3 changed files with 47 additions and 1 deletions

View file

@ -1,3 +1,5 @@
use crate::interpreter::Value;
#[derive(Debug)]
pub enum ErrorKind {
UnexpectedChar(char),
@ -12,6 +14,16 @@ pub enum ErrorKind {
InternalError(String),
InvalidAssignmentTarget(String),
RuntimeError(String),
// This variant is not an error, rather it is used for
// short-circuiting out of a function body that hits a `return`
// statement.
//
// It's implemented this way because in the original book the
// author uses exceptions for control flow, and this is the
// closest equivalent that I had available without diverging too
// much.
FunctionReturn(Value),
}
#[derive(Debug)]