From e85c4984dbca6cb1bf579accf7d0ad694e619eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 15 Jan 2021 13:59:27 +0100 Subject: parse function types in function types in... --- src/vm.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index eb4de31..daf2ead 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -22,7 +22,8 @@ pub enum Value { Int(i64), Bool(bool), String(Rc), - Function(usize, Rc), + Function(Vec, Type, Rc), + Nil, } impl Debug for Value { @@ -32,7 +33,8 @@ impl Debug for Value { Value::Int(i) => write!(fmt, "(int {})", i), Value::Bool(b) => write!(fmt, "(bool {})", b), Value::String(s) => write!(fmt, "(string \"{}\")", s), - Value::Function(arity, block) => write!(fmt, "(func {}-{})", block.name, arity), + Value::Function(args, ret, block) => write!(fmt, "(fn {}: {:?} -> {:?})", block.name, args, ret), + Value::Nil => write!(fmt, "(nil)"), } } } @@ -75,9 +77,6 @@ pub enum Op { #[derive(Debug)] pub struct Block { - pub args: Vec, - pub ret: Type, - pub name: String, pub file: PathBuf, pub ops: Vec, @@ -89,9 +88,6 @@ pub struct Block { impl Block { pub fn new(name: &str, file: &Path, line: usize) -> Self { Self { - args: Vec::new(), - ret: Type::UnkownType, - name: String::from(name), file: file.to_owned(), ops: Vec::new(), @@ -105,6 +101,10 @@ impl Block { (self.file.clone(), self.line) } + pub fn last_op(&self) -> Option<&Op> { + self.ops.last() + } + pub fn add_line(&mut self, token_position: usize) { if token_position != self.last_line_offset { self.line_offsets.insert(self.curr(), token_position); @@ -228,7 +228,7 @@ impl VM { } pub fn run(&mut self, block: Rc) -> Result<(), Error>{ - if let Err(err) = crate::typer::VM::new().print_ops(true).typecheck(Rc::clone(&block)) { + if let Err(err) = crate::typer::VM::new().print_ops(true).typecheck(Type::NoType, Rc::clone(&block)) { println!("TYPE ERROR: {}", err); } @@ -404,12 +404,12 @@ impl VM { Op::Call(num_args) => { let new_base = self.stack.len() - 1 - num_args; match &self.stack[new_base] { - Value::Function(arity, block) => { - if arity != &num_args { + Value::Function(args, ret, block) => { + if args.len() != num_args { error!(self, ErrorKind::InvalidProgram, format!("Invalid number of arguments, got {} expected {}.", - num_args, arity)); + num_args, args.len())); } if self.print_blocks { block.debug_print(); -- cgit v1.2.1