diff options
| -rw-r--r-- | TODO | 2 | ||||
| -rw-r--r-- | src/compiler.rs | 2 | ||||
| -rw-r--r-- | src/vm.rs | 24 | ||||
| -rw-r--r-- | tests/simple.tdy | 26 |
4 files changed, 41 insertions, 13 deletions
@@ -1,6 +1,4 @@ PONG -- complete plain structs - - type system - structs in structs - foreign function - constructors? ({.x = }-ish) diff --git a/src/compiler.rs b/src/compiler.rs index be2fe44..83dca4a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -850,7 +850,7 @@ impl Compiler { "float" => Ok(Type::Float), "bool" => Ok(Type::Bool), "str" => Ok(Type::String), - _ => Err(()), + x => self.find_blob(x).map(|blob| Type::BlobInstance(blob)).ok_or(()), } } _ => Err(()), @@ -743,6 +743,30 @@ impl VM { } } + Op::Get(field) => { + let inst = self.stack.pop(); + if let Some(Value::BlobInstance(ty, _)) = inst { + let value = self.blobs[ty].name_to_field.get(&field).unwrap().1.as_value(); + self.stack.push(value); + } else { + self.stack.push(Value::Nil); + error!(self, ErrorKind::RuntimeTypeError(Op::Get(field.clone()), vec![inst.unwrap()])); + } + } + + Op::Set(field) => { + let value = self.stack.pop().unwrap(); + let inst = self.stack.pop(); + if let Some(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()])); + } + } else { + error!(self, ErrorKind::RuntimeTypeError(Op::Set(field.clone()), vec![inst.unwrap()])); + } + } + Op::PopUpvalue => { self.stack.pop().unwrap(); } diff --git a/tests/simple.tdy b/tests/simple.tdy index 28bd5e2..de6841b 100644 --- a/tests/simple.tdy +++ b/tests/simple.tdy @@ -1,12 +1,18 @@ -fac : fn int -> int = fn a: int -> int { - if a <= 1 { - ret 1 - } - b := fac(a - 1) - ret a * b +blob A { + a: int + b: int + c: float } -print fac(5) -// print b(fn a: int -> { -// print a -// }) +hello : fn A -> = fn a: A { + print a.a + print a.b + print a.c +} + +a := A() +a.a = 1 +a.b = 2 +a.c = 3. + +hello(a) |
