From c16f77289248633fb3229877e33e5e68dd27a826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 29 Jan 2021 20:12:43 +0100 Subject: blobs as parameters. also typecheck --- TODO | 2 -- src/compiler.rs | 2 +- src/vm.rs | 24 ++++++++++++++++++++++++ tests/simple.tdy | 26 ++++++++++++++++---------- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 1801fbc..e8657bf 100644 --- a/TODO +++ b/TODO @@ -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(()), diff --git a/src/vm.rs b/src/vm.rs index 4e501ba..2b5b2bf 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -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) -- cgit v1.2.1