Errors refactoring - Code compiles.
This commit is contained in:
parent
743379c516
commit
c5f099340b
7 changed files with 34 additions and 42 deletions
src/interpreter
|
@ -1,6 +1,6 @@
|
|||
use std::{cell::RefCell, fmt::Debug, rc::Rc};
|
||||
|
||||
use crate::errors::InterpreterError;
|
||||
use crate::errors::SloxResult;
|
||||
|
||||
use super::{EnvironmentRef, Value};
|
||||
|
||||
|
@ -11,11 +11,7 @@ pub trait Callable: Debug + ToString {
|
|||
|
||||
/// Run the callable in the execution environment with the specified
|
||||
/// arguments.
|
||||
fn call(
|
||||
&self,
|
||||
environment: &EnvironmentRef,
|
||||
arguments: Vec<Value>,
|
||||
) -> Result<Value, InterpreterError>;
|
||||
fn call(&self, environment: &EnvironmentRef, arguments: Vec<Value>) -> SloxResult<Value>;
|
||||
}
|
||||
|
||||
/// A reference to a callable.
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use crate::{errors::InterpreterError, tokens::Token};
|
||||
use crate::{
|
||||
errors::{ErrorKind, SloxError, SloxResult},
|
||||
tokens::Token,
|
||||
};
|
||||
|
||||
use super::{native_fn, CallableRef, Value};
|
||||
|
||||
|
@ -45,11 +48,12 @@ impl Environment {
|
|||
}
|
||||
|
||||
/// Define a new variable.
|
||||
pub fn define(&mut self, name: &Token, value: Variable) -> Result<(), InterpreterError> {
|
||||
pub fn define(&mut self, name: &Token, value: Variable) -> SloxResult<()> {
|
||||
if self.values.contains_key(&name.lexeme as &str) {
|
||||
Err(InterpreterError::new(
|
||||
Err(SloxError::with_token(
|
||||
ErrorKind::Runtime,
|
||||
name,
|
||||
&format!("variable '{}' already defined in scope", name.lexeme),
|
||||
format!("variable '{}' already defined in scope", name.lexeme),
|
||||
))
|
||||
} else {
|
||||
self.values.insert(name.lexeme.clone(), value);
|
||||
|
@ -58,33 +62,36 @@ impl Environment {
|
|||
}
|
||||
|
||||
/// Get the value of a variable.
|
||||
pub fn get(&self, name: &Token) -> Result<Value, InterpreterError> {
|
||||
pub fn get(&self, name: &Token) -> SloxResult<Value> {
|
||||
match self.values.get(&name.lexeme as &str) {
|
||||
None => match &self.enclosing {
|
||||
None => Err(InterpreterError::new(
|
||||
None => Err(SloxError::with_token(
|
||||
ErrorKind::Runtime,
|
||||
name,
|
||||
&format!("undefined variable '{}'", name.lexeme),
|
||||
format!("undefined variable '{}'", name.lexeme),
|
||||
)),
|
||||
Some(parent) => parent.borrow().get(name),
|
||||
},
|
||||
Some(None) => Err(InterpreterError::new(
|
||||
Some(None) => Err(SloxError::with_token(
|
||||
ErrorKind::Runtime,
|
||||
name,
|
||||
&format!("variable '{}' has not been initialized", name.lexeme),
|
||||
format!("variable '{}' has not been initialized", name.lexeme),
|
||||
)),
|
||||
Some(Some(value)) => Ok(value.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Assign a value to an existing variable.
|
||||
pub fn assign(&mut self, name: &Token, value: Value) -> Result<(), InterpreterError> {
|
||||
pub fn assign(&mut self, name: &Token, value: Value) -> SloxResult<()> {
|
||||
if self.values.contains_key(&name.lexeme as &str) {
|
||||
self.values.insert(name.lexeme.clone(), Some(value));
|
||||
Ok(())
|
||||
} else {
|
||||
match &mut self.enclosing {
|
||||
None => Err(InterpreterError::new(
|
||||
None => Err(SloxError::with_token(
|
||||
ErrorKind::Runtime,
|
||||
name,
|
||||
&format!("undefined variable '{}'", name.lexeme),
|
||||
format!("undefined variable '{}'", name.lexeme),
|
||||
)),
|
||||
Some(parent) => parent.borrow_mut().assign(name, value),
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use itertools::izip;
|
|||
|
||||
use crate::{
|
||||
ast,
|
||||
errors::InterpreterError,
|
||||
errors::SloxResult,
|
||||
interpreter::{Environment, Interpretable, InterpreterFlowControl},
|
||||
tokens::Token,
|
||||
};
|
||||
|
@ -39,11 +39,7 @@ impl Callable for Function {
|
|||
self.params.len()
|
||||
}
|
||||
|
||||
fn call(
|
||||
&self,
|
||||
environment: &EnvironmentRef,
|
||||
arguments: Vec<Value>,
|
||||
) -> Result<Value, InterpreterError> {
|
||||
fn call(&self, environment: &EnvironmentRef, arguments: Vec<Value>) -> SloxResult<Value> {
|
||||
assert_eq!(arguments.len(), self.arity());
|
||||
let param_env = Environment::create_child(environment);
|
||||
for (arg, value) in izip!(self.params.iter(), arguments.into_iter()) {
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::{
|
|||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use crate::errors::InterpreterError;
|
||||
use crate::errors::SloxResult;
|
||||
|
||||
use super::{Callable, CallableRef, EnvironmentRef, Value};
|
||||
|
||||
|
@ -20,11 +20,7 @@ impl Callable for Clock {
|
|||
0
|
||||
}
|
||||
|
||||
fn call(
|
||||
&self,
|
||||
_environment: &EnvironmentRef,
|
||||
_arguments: Vec<Value>,
|
||||
) -> Result<Value, InterpreterError> {
|
||||
fn call(&self, _environment: &EnvironmentRef, _arguments: Vec<Value>) -> SloxResult<Value> {
|
||||
let now = SystemTime::now();
|
||||
let since_epoch = now
|
||||
.duration_since(UNIX_EPOCH)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue