From cede973c76e061e06c7bdaad3922be5d9305f950 Mon Sep 17 00:00:00 2001 From: Mel Date: Sun, 24 Oct 2021 23:04:22 +0200 Subject: Array element assignment and constants --- src/interpret/value.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'src/interpret/value.rs') diff --git a/src/interpret/value.rs b/src/interpret/value.rs index 1a7422b..f923742 100644 --- a/src/interpret/value.rs +++ b/src/interpret/value.rs @@ -2,7 +2,7 @@ use crate::parse::ast::nodes::FnNode; use std::{cell::RefCell, fmt::Display, rc::Rc}; use thiserror::Error; -type Ref = RefCell>; +type ReferenceOnCopy = Rc>; #[derive(Clone, Debug)] pub enum Value { @@ -10,8 +10,8 @@ pub enum Value { Float(f64), Int(i64), Bool(bool), - Array(Vec), - Fn(Ref), + Array(ReferenceOnCopy>), + Fn(ReferenceOnCopy), Void, } @@ -173,17 +173,45 @@ impl Value { match self { Value::Array(a) => { - if index < 0 || index as usize >= a.len() { + let array = a.borrow(); + if index < 0 || index as usize >= array.len() { Err(OperationError::ArrayIndexOutOfRange { index, - length: a.len(), + length: array.len(), }) } else { - Ok(a[index as usize].clone()) + Ok(array[index as usize].clone()) } } // Maybe allow string subscripts? - x => Err(OperationError::ArrayType(x)), + x => Err(OperationError::ArrayType(x.clone())), + } + } + + pub fn subscript_assign( + &mut self, + index: Value, + value: Value, + ) -> Result { + let index = match index { + Value::Int(i) => i, + i => return Err(OperationError::ArrayIndexType(i)), + }; + + match self { + Value::Array(a) => { + let mut array = a.borrow_mut(); + if index < 0 || index as usize >= array.len() { + Err(OperationError::ArrayIndexOutOfRange { + index, + length: array.len(), + }) + } else { + array[index as usize] = value; + Ok(array[index as usize].clone()) + } + } + x => Err(OperationError::ArrayType(x.clone())), } } } @@ -213,7 +241,8 @@ impl Display for Value { write!( f, "[{}]", - a.iter() + a.borrow() + .iter() .map(|v| format!("{}", v)) .collect::>() .join(", ") -- cgit 1.4.1