aboutsummaryrefslogtreecommitdiffstats
path: root/src/vm.rs
diff options
context:
space:
mode:
authorEdvard Thörnros <edvard.thornros@gmail.com>2021-01-30 22:34:19 +0100
committerEdvard Thörnros <edvard.thornros@gmail.com>2021-01-30 22:34:19 +0100
commit4253c1c20013ab16564aa3ec34585dd1a358d182 (patch)
treef1d96d0aa8c4af949b1284129d6366d697139bb0 /src/vm.rs
parent1a82b85817646aded501051f4e9d651f7c0d4970 (diff)
downloadsylt-4253c1c20013ab16564aa3ec34585dd1a358d182.tar.gz
add in tuples
Diffstat (limited to 'src/vm.rs')
-rw-r--r--src/vm.rs28
1 files changed, 24 insertions, 4 deletions
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]));
}
}