Change &mut Environment to &Environment
This commit is contained in:
+7
-7
@@ -124,7 +124,7 @@ fn handle_module_error(input: Either<MachineErrorAt, Vec<ParseError>>) -> Error
|
||||
fn eval(
|
||||
options: &CompileOptions,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
value: Value,
|
||||
) -> Option<Value> {
|
||||
let result = vm.evaluate_value(
|
||||
@@ -145,7 +145,7 @@ fn eval(
|
||||
fn run_interactive(
|
||||
compile_options: &CompileOptions,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
) -> Result<(), Error> {
|
||||
let mut reader = InteractiveReader::new("> ", ">> ");
|
||||
|
||||
@@ -171,7 +171,7 @@ fn run_interactive(
|
||||
fn run_module<P: AsRef<Path>>(
|
||||
compile_options: &CompileOptions,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
path: P,
|
||||
) -> Result<(), Error> {
|
||||
let path = path.as_ref();
|
||||
@@ -202,8 +202,8 @@ fn main() -> ExitCode {
|
||||
vm.trace_calls = args.trace.contains(&Trace::Call);
|
||||
vm.trace_returns = args.trace.contains(&Trace::Return);
|
||||
vm.trace_stack = args.trace.contains(&Trace::Stack);
|
||||
let mut env = Environment::default();
|
||||
prelude::load(&mut env);
|
||||
let env = Environment::default();
|
||||
prelude::load(&env);
|
||||
let mut arguments = vec![];
|
||||
if let Some(script) = args.module.as_ref() {
|
||||
arguments.push(format!("{}", script.display()));
|
||||
@@ -214,8 +214,8 @@ fn main() -> ExitCode {
|
||||
Value::list_or_nil(arguments.into_iter().map(|arg| Value::String(arg.into()))),
|
||||
);
|
||||
let result = match args.module.as_ref() {
|
||||
Some(module) => run_module(&compile_options, &mut vm, &mut env, module),
|
||||
None => run_interactive(&compile_options, &mut vm, &mut env),
|
||||
Some(module) => run_module(&compile_options, &mut vm, &env, module),
|
||||
None => run_interactive(&compile_options, &mut vm, &env),
|
||||
};
|
||||
match result {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
|
||||
+3
-3
@@ -104,7 +104,7 @@ impl<R: BufRead> ModuleReader<R> {
|
||||
pub fn read_expression(
|
||||
&mut self,
|
||||
options: &CompileOptions,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
) -> Result<Option<Rc<Expression>>, Either<MachineErrorAt, Vec<ParseError>>> {
|
||||
loop {
|
||||
let value =
|
||||
@@ -127,7 +127,7 @@ impl<R: BufRead> ModuleReader<R> {
|
||||
mut self,
|
||||
module_name: Option<IdentifierValue>,
|
||||
options: &CompileOptions,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
) -> Result<Rc<BytecodeFunction>, Either<MachineErrorAt, Vec<ParseError>>> {
|
||||
let mut cx = CompileContext::new(options.clone(), module_name);
|
||||
let mut body = FunctionBody {
|
||||
@@ -249,7 +249,7 @@ fn read_inner<R: BufRead>(
|
||||
pub fn read<R: Reader>(
|
||||
reader: &mut R,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
) -> Result<Option<Value>, MachineErrorAt> {
|
||||
let raw_value = reader.read().map_err(Into::into)?;
|
||||
let Some(raw_value) = raw_value else {
|
||||
|
||||
+6
-6
@@ -22,11 +22,11 @@ pub struct Environment {
|
||||
}
|
||||
|
||||
impl Environment {
|
||||
pub fn defun_native<S, D, F>(&mut self, identifier: S, docstring: D, function: F) -> Value
|
||||
pub fn defun_native<S, D, F>(&self, identifier: S, docstring: D, function: F) -> Value
|
||||
where
|
||||
S: Into<IdentifierValue>,
|
||||
D: Into<StringValue>,
|
||||
F: Fn(&mut Machine, &mut Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
F: Fn(&mut Machine, &Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
{
|
||||
let identifier = identifier.into();
|
||||
let native = NativeFunction::new(identifier.clone(), docstring, function);
|
||||
@@ -35,11 +35,11 @@ impl Environment {
|
||||
value
|
||||
}
|
||||
|
||||
pub fn defmacro_native<S, D, F>(&mut self, identifier: S, docstring: D, function: F)
|
||||
pub fn defmacro_native<S, D, F>(&self, identifier: S, docstring: D, function: F)
|
||||
where
|
||||
S: Into<IdentifierValue>,
|
||||
D: Into<StringValue>,
|
||||
F: Fn(&mut Machine, &mut Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
F: Fn(&mut Machine, &Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
{
|
||||
let identifier = identifier.into();
|
||||
let native = NativeFunction::new(identifier.clone(), docstring, function);
|
||||
@@ -49,7 +49,7 @@ impl Environment {
|
||||
}
|
||||
|
||||
pub fn defmacro_bytecode<S: Into<IdentifierValue>>(
|
||||
&mut self,
|
||||
&self,
|
||||
identifier: S,
|
||||
value: Rc<BytecodeFunction>,
|
||||
) {
|
||||
@@ -73,7 +73,7 @@ impl Environment {
|
||||
}
|
||||
|
||||
pub fn set_global_value<S: Into<IdentifierValue>, V: Into<Value>>(
|
||||
&mut self,
|
||||
&self,
|
||||
identifier: S,
|
||||
value: V,
|
||||
) {
|
||||
|
||||
+10
-14
@@ -150,7 +150,7 @@ impl Machine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_get_global(&mut self, env: &mut Environment) -> Result<(), MachineError> {
|
||||
fn execute_get_global(&mut self, env: &Environment) -> Result<(), MachineError> {
|
||||
let identifier = self.pop()?;
|
||||
let Value::Identifier(identifier) = identifier else {
|
||||
return Err(MachineError::InvalidInstructionArgument(
|
||||
@@ -168,7 +168,7 @@ impl Machine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_set_global(&mut self, env: &mut Environment) -> Result<(), MachineError> {
|
||||
fn execute_set_global(&mut self, env: &Environment) -> Result<(), MachineError> {
|
||||
let identifier = self.pop()?;
|
||||
let Value::Identifier(identifier) = identifier else {
|
||||
return Err(MachineError::InvalidInstructionArgument(
|
||||
@@ -186,7 +186,7 @@ impl Machine {
|
||||
|
||||
fn execute_call(
|
||||
&mut self,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
argument_count: usize,
|
||||
) -> Result<(), MachineError> {
|
||||
let base_pointer = self
|
||||
@@ -380,7 +380,7 @@ impl Machine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_declare_macro(&mut self, env: &mut Environment) -> Result<(), MachineError> {
|
||||
fn execute_declare_macro(&mut self, env: &Environment) -> Result<(), MachineError> {
|
||||
let identifier = self.pop()?;
|
||||
let function = self.pop()?;
|
||||
let Value::Identifier(identifier) = identifier else {
|
||||
@@ -448,7 +448,7 @@ impl Machine {
|
||||
frame.closure.function.disassemble(frame.ip, 0, 0, false);
|
||||
}
|
||||
|
||||
fn execute_next(&mut self, env: &mut Environment) -> Result<(), MachineError> {
|
||||
fn execute_next(&mut self, env: &Environment) -> Result<(), MachineError> {
|
||||
if self.trace_instructions {
|
||||
if self.trace_stack {
|
||||
self.trace_stack();
|
||||
@@ -571,7 +571,7 @@ impl Machine {
|
||||
|
||||
pub fn evaluate_closure(
|
||||
&mut self,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
closure: ClosureValue,
|
||||
argument_count: usize,
|
||||
) -> Result<Value, MachineErrorAt> {
|
||||
@@ -593,7 +593,7 @@ impl Machine {
|
||||
|
||||
pub fn evaluate_closure_args(
|
||||
&mut self,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
closure: ClosureValue,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineErrorAt> {
|
||||
@@ -635,7 +635,7 @@ impl Machine {
|
||||
&mut self,
|
||||
compile_options: CompileOptions,
|
||||
chunk_name: Option<IdentifierValue>,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
value: Value,
|
||||
) -> Result<Value, MachineErrorAt> {
|
||||
let value = value.macro_expand(self, env, false)?;
|
||||
@@ -669,7 +669,7 @@ mod tests {
|
||||
};
|
||||
|
||||
fn try_eval(
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
mut instructions: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
) -> (Machine, Result<Value, MachineErrorAt>) {
|
||||
@@ -691,11 +691,7 @@ mod tests {
|
||||
(machine, result)
|
||||
}
|
||||
|
||||
fn eval0(
|
||||
env: &mut Environment,
|
||||
instructions: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
) -> (Machine, Value) {
|
||||
fn eval0(env: &Environment, instructions: Vec<u8>, constants: Vec<Value>) -> (Machine, Value) {
|
||||
let (machine, value) = try_eval(env, instructions, constants);
|
||||
let value = value.expect("evaluation failed");
|
||||
(machine, value)
|
||||
|
||||
+2
-2
@@ -13,7 +13,7 @@ pub trait MacroExpand: Sized {
|
||||
fn macro_expand(
|
||||
&self,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
tail: bool,
|
||||
) -> Result<Self, MachineErrorAt>;
|
||||
}
|
||||
@@ -22,7 +22,7 @@ impl MacroExpand for Value {
|
||||
fn macro_expand(
|
||||
&self,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
tail: bool,
|
||||
) -> Result<Self, MachineErrorAt> {
|
||||
match self {
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
// // vectors
|
||||
// env.defun_native("getv", |vm, _, args| {
|
||||
// let [vec, index] = args else {
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
env.defun_native(
|
||||
"->string",
|
||||
"Converts a value to string representation",
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::{
|
||||
vm::{Value, env::Environment, value::Keyword},
|
||||
};
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
env.defun_native(
|
||||
"explain",
|
||||
"Provides an explanation for a given value",
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::{
|
||||
vm::{Value, env::Environment},
|
||||
};
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
env.defun_native("eval", "Evaluates the given expression", |vm, env, args| {
|
||||
// TODO eval in env
|
||||
let [value] = args else {
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
// env.defun_native("map", |vm, env, args| {
|
||||
// let [f, xs] = args else {
|
||||
// return Err(vm.error_at_ip(MachineErrorKind::InvalidArgument));
|
||||
|
||||
@@ -76,7 +76,7 @@ impl Stream {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
let stdin: Rc<dyn NativeObject> = Rc::new(Stream::Stdin);
|
||||
let stdout: Rc<dyn NativeObject> = Rc::new(Stream::Stdout);
|
||||
|
||||
|
||||
+19
-19
@@ -12,7 +12,7 @@ use crate::{
|
||||
|
||||
pub(crate) fn dispatch_arithmetic(
|
||||
instruction: Instruction,
|
||||
) -> fn(&mut Machine, &mut Environment, &[Value]) -> Result<Value, MachineError> {
|
||||
) -> fn(&mut Machine, &Environment, &[Value]) -> Result<Value, MachineError> {
|
||||
match instruction {
|
||||
Instruction::Add => builtin_add,
|
||||
Instruction::Sub => builtin_sub,
|
||||
@@ -30,7 +30,7 @@ pub(crate) fn dispatch_arithmetic(
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn load(env: &mut Environment) {
|
||||
pub(super) fn load(env: &Environment) {
|
||||
env.defun_native("+", "Adds values", builtin_add);
|
||||
env.defun_native("-", "Subtracts values", builtin_sub);
|
||||
let mul = env.defun_native("*", "Multiplies values", builtin_mul);
|
||||
@@ -137,14 +137,14 @@ where
|
||||
|
||||
pub(crate) fn builtin_add(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_fold(value_add, Value::Number(0.into()), args)
|
||||
}
|
||||
pub(crate) fn builtin_sub(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
match args {
|
||||
@@ -165,14 +165,14 @@ pub(crate) fn builtin_sub(
|
||||
}
|
||||
pub(crate) fn builtin_mul(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_fold_t(Mul::mul, NumberValue::from(1), args)
|
||||
}
|
||||
pub(crate) fn builtin_mod(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
let [x, y] = args else {
|
||||
@@ -184,7 +184,7 @@ pub(crate) fn builtin_mod(
|
||||
}
|
||||
pub(crate) fn builtin_div(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
match args {
|
||||
@@ -206,35 +206,35 @@ pub(crate) fn builtin_div(
|
||||
|
||||
// pub(crate) fn builtin_and(
|
||||
// vm: &mut Machine,
|
||||
// _env: &mut Environment,
|
||||
// _env: &Environment,
|
||||
// args: &[Value],
|
||||
// ) -> Result<Value, MachineError> {
|
||||
// builtin_fold_t(BitAnd::bitand, true, args)
|
||||
// }
|
||||
// pub(crate) fn builtin_or(
|
||||
// vm: &mut Machine,
|
||||
// _env: &mut Environment,
|
||||
// _env: &Environment,
|
||||
// args: &[Value],
|
||||
// ) -> Result<Value, MachineError> {
|
||||
// builtin_fold_t(BitOr::bitor, false, args)
|
||||
// }
|
||||
// pub(crate) fn builtin_bitwise_and(
|
||||
// vm: &mut Machine,
|
||||
// _env: &mut Environment,
|
||||
// _env: &Environment,
|
||||
// args: &[Value],
|
||||
// ) -> Result<Value, MachineError> {
|
||||
// builtin_fold_t(BitAnd::bitand, 1, args)
|
||||
// }
|
||||
// pub(crate) fn builtin_bitwise_or(
|
||||
// vm: &mut Machine,
|
||||
// _env: &mut Environment,
|
||||
// _env: &Environment,
|
||||
// args: &[Value],
|
||||
// ) -> Result<Value, MachineError> {
|
||||
// builtin_fold_t(BitOr::bitor, 0, args)
|
||||
// }
|
||||
// pub(crate) fn builtin_bitwise_xor(
|
||||
// vm: &mut Machine,
|
||||
// _env: &mut Environment,
|
||||
// _env: &Environment,
|
||||
// args: &[Value],
|
||||
// ) -> Result<Value, MachineError> {
|
||||
// builtin_fold_t(BitXor::bitxor, 0, args)
|
||||
@@ -296,42 +296,42 @@ pub(crate) fn builtin_cmp(
|
||||
}
|
||||
pub(crate) fn builtin_cmp_eq(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Eq)
|
||||
}
|
||||
pub(crate) fn builtin_cmp_ne(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Ne)
|
||||
}
|
||||
pub(crate) fn builtin_cmp_gt(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Gt)
|
||||
}
|
||||
pub(crate) fn builtin_cmp_lt(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Lt)
|
||||
}
|
||||
pub(crate) fn builtin_cmp_ge(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Ge)
|
||||
}
|
||||
pub(crate) fn builtin_cmp_le(
|
||||
vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
builtin_cmp(vm, args, CompareOperation::Le)
|
||||
@@ -339,7 +339,7 @@ pub(crate) fn builtin_cmp_le(
|
||||
|
||||
pub(crate) fn builtin_not(
|
||||
_vm: &mut Machine,
|
||||
_env: &mut Environment,
|
||||
_env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
let [x] = args else {
|
||||
|
||||
@@ -10,7 +10,7 @@ mod math;
|
||||
|
||||
pub(crate) use math::*;
|
||||
|
||||
pub fn load(env: &mut Environment) {
|
||||
pub fn load(env: &Environment) {
|
||||
math::load(env);
|
||||
eval::load(env);
|
||||
functional::load(env);
|
||||
|
||||
@@ -144,7 +144,7 @@ impl AnyFunction {
|
||||
pub fn invoke(
|
||||
&self,
|
||||
vm: &mut Machine,
|
||||
env: &mut Environment,
|
||||
env: &Environment,
|
||||
args: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
match self {
|
||||
|
||||
@@ -22,7 +22,7 @@ pub trait NativeObject: Any + fmt::Debug + fmt::Display {
|
||||
pub struct NativeValue(Rc<dyn NativeObject>);
|
||||
|
||||
pub type NativeFunctionImpl =
|
||||
Rc<dyn Fn(&mut Machine, &mut Environment, &[Value]) -> Result<Value, MachineError> + 'static>;
|
||||
Rc<dyn Fn(&mut Machine, &Environment, &[Value]) -> Result<Value, MachineError> + 'static>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct NativeFunction {
|
||||
@@ -36,7 +36,7 @@ impl NativeFunction {
|
||||
where
|
||||
S: Into<IdentifierValue>,
|
||||
D: Into<StringValue>,
|
||||
F: Fn(&mut Machine, &mut Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
F: Fn(&mut Machine, &Environment, &[Value]) -> Result<Value, MachineError> + 'static,
|
||||
{
|
||||
Self {
|
||||
name: name.into(),
|
||||
@@ -56,7 +56,7 @@ impl NativeFunction {
|
||||
pub fn invoke(
|
||||
&self,
|
||||
machine: &mut Machine,
|
||||
environment: &mut Environment,
|
||||
environment: &Environment,
|
||||
arguments: &[Value],
|
||||
) -> Result<Value, MachineError> {
|
||||
(self.inner)(machine, environment, arguments)
|
||||
|
||||
@@ -18,7 +18,7 @@ impl Read for SliceReader<'_> {
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn eval_str_in(code: &str, env: &mut Environment) -> Result<Value, MachineErrorAt> {
|
||||
fn eval_str_in(code: &str, env: &Environment) -> Result<Value, MachineErrorAt> {
|
||||
let mut machine = Machine::default();
|
||||
let reader = BufReader::new(SliceReader(code.as_bytes()));
|
||||
let mut reader = FileReader::new(reader);
|
||||
|
||||
Reference in New Issue
Block a user