From 4253c1c20013ab16564aa3ec34585dd1a358d182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Sat, 30 Jan 2021 22:34:19 +0100 Subject: add in tuples --- src/vm.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index fa998b8..51d3245 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -175,6 +175,26 @@ impl VM { self.stack.push(value); } + Op::Index => { + let slot = self.stack.pop().unwrap(); + let val = self.stack.pop().unwrap(); + match (val, slot) { + (Value::Tuple(v), Value::Int(slot)) => { + let slot = slot as usize; + if v.len() < slot { + self.stack.push(Value::Nil); + let len = v.len(); + error!(self, ErrorKind::IndexOutOfBounds(Value::Tuple(v), len, slot)); + } + self.stack.push(v[slot].clone()); + } + (val, slot) => { + self.stack.push(Value::Nil); + error!(self, ErrorKind::RuntimeTypeError(op, vec![val, slot]), String::from("Cannot index type")); + } + } + } + Op::Get(field) => { let inst = self.stack.pop(); if let Some(Value::BlobInstance(ty, values)) = inst { @@ -519,14 +539,14 @@ impl VM { Op::Set(field) => { let value = self.stack.pop().unwrap(); - let inst = self.stack.pop(); - if let Some(Value::BlobInstance(ty, _)) = inst { + let inst = self.stack.pop().unwrap(); + if let Value::BlobInstance(ty, _) = inst { let ty = &self.blobs[ty].name_to_field.get(&field).unwrap().1; if ty != &Type::from(&value) { - error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst.unwrap()])); + error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst])); } } else { - error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst.unwrap()])); + error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst])); } } -- cgit v1.2.1