From 519a9e5360f2cf0438dd09cfa3070bb9c8819f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 18:50:28 +0100 Subject: BlobInstance -> Instance --- src/compiler.rs | 2 +- src/lib.rs | 12 ++++++------ src/vm.rs | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index f419527..e8607f7 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1085,7 +1085,7 @@ impl Compiler { "float" => Ok(Type::Float), "bool" => Ok(Type::Bool), "str" => Ok(Type::String), - x => self.find_blob(x).map(|blob| Type::BlobInstance(blob)).ok_or(()), + x => self.find_blob(x).map(|blob| Type::Instance(blob)).ok_or(()), } } _ => Err(()), diff --git a/src/lib.rs b/src/lib.rs index ee176e5..c2aee90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,14 +85,14 @@ pub enum Type { Tuple(Vec), Function(Vec, Box), Blob(usize), - BlobInstance(usize), + Instance(usize), } impl PartialEq for Type { fn eq(&self, other: &Self) -> bool { match (self, other) { (Type::Void, Type::Void) => true, - (Type::BlobInstance(a), Type::BlobInstance(b)) => a == b, + (Type::Instance(a), Type::Instance(b)) => a == b, (Type::Blob(a), Type::Blob(b)) => a == b, (Type::Int, Type::Int) => true, (Type::Float, Type::Float) => true, @@ -111,7 +111,7 @@ impl PartialEq for Type { impl From<&Value> for Type { fn from(value: &Value) -> Type { match value { - Value::BlobInstance(i, _) => Type::BlobInstance(*i), + Value::Instance(i, _) => Type::Instance(*i), Value::Blob(i) => Type::Blob(*i), Value::Tuple(v) => { Type::Tuple(v.iter().map(|x| Type::from(x)).collect()) @@ -137,7 +137,7 @@ impl From<&Type> for Value { match ty { Type::Void => Value::Nil, Type::Blob(i) => Value::Blob(*i), - Type::BlobInstance(i) => Value::BlobInstance(*i, Rc::new(RefCell::new(Vec::new()))), + Type::Instance(i) => Value::Instance(*i, Rc::new(RefCell::new(Vec::new()))), Type::Tuple(fields) => { Value::Tuple(Rc::new(fields.iter().map(Value::from).collect())) } @@ -164,7 +164,7 @@ impl From for Value { pub enum Value { Ty(Type), Blob(usize), - BlobInstance(usize, Rc>>), + Instance(usize, Rc>>), Tuple(Rc>), Float(f64), Int(i64), @@ -181,7 +181,7 @@ impl Debug for Value { match self { Value::Ty(ty) => write!(fmt, "(type {:?})", ty), Value::Blob(i) => write!(fmt, "(blob {})", i), - Value::BlobInstance(i, v) => write!(fmt, "(inst {} {:?})", i, v), + Value::Instance(i, v) => write!(fmt, "(inst {} {:?})", i, v), Value::Float(f) => write!(fmt, "(float {})", f), Value::Int(i) => write!(fmt, "(int {})", i), Value::Bool(b) => write!(fmt, "(bool {})", b), diff --git a/src/vm.rs b/src/vm.rs index c4d72a1..2cef854 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -283,7 +283,7 @@ impl VM { Op::Get(field) => { let inst = self.pop(); let field = self.string(field); - if let Value::BlobInstance(ty, values) = inst { + if let Value::Instance(ty, values) = inst { let slot = self.blobs[ty].fields.get(field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { @@ -294,7 +294,7 @@ impl VM { Op::Set(field) => { let (inst, value) = self.poppop(); let field = self.string(field); - if let Value::BlobInstance(ty, values) = inst { + if let Value::Instance(ty, values) = inst { let slot = self.blobs[ty].fields.get(field).unwrap().0; values.borrow_mut()[slot] = value; } else { @@ -402,7 +402,7 @@ impl VM { } self.pop(); - self.push(Value::BlobInstance(blob_id, Rc::new(RefCell::new(values)))); + self.push(Value::Instance(blob_id, Rc::new(RefCell::new(values)))); } Value::Function(_, block) => { let inner = block.borrow(); @@ -569,7 +569,7 @@ impl VM { Op::Get(field) => { let inst = self.pop(); let field = self.string(field); - if let Value::BlobInstance(ty, _) = inst { + if let Value::Instance(ty, _) = inst { let value = Value::from(&self.blobs[ty].fields.get(field).unwrap().1); self.push(value); } else { @@ -582,7 +582,7 @@ impl VM { let (inst, value) = self.poppop(); let field = self.string(field); - if let Value::BlobInstance(ty, _) = inst { + if let Value::Instance(ty, _) = inst { let ty = &self.blobs[ty].fields.get(field).unwrap().1; if ty != &Type::from(&value) { error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); @@ -659,7 +659,7 @@ impl VM { } self.pop(); - self.push(Value::BlobInstance(blob_id, Rc::new(RefCell::new(values)))); + self.push(Value::Instance(blob_id, Rc::new(RefCell::new(values)))); } Value::Function(_, block) => { let inner = block.borrow(); -- cgit v1.2.1 From f098c32e89626f75d83118d4d95d209299d28587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 19:18:25 +0100 Subject: change how blobs are stored --- src/compiler.rs | 33 ++++++++++++++++++++++----------- src/lib.rs | 38 ++++++++++++++++++++++++-------------- src/vm.rs | 28 ++++++++++------------------ 3 files changed, 56 insertions(+), 43 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index e8607f7..9a29d6d 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -228,11 +228,13 @@ pub(crate) struct Compiler { errors: Vec, blocks: Vec>>, - blobs: Vec, + blob_id: usize, functions: HashMap, constants: Vec, strings: Vec, + + } macro_rules! push_frame { @@ -290,7 +292,7 @@ impl Compiler { errors: vec![], blocks: Vec::new(), - blobs: Vec::new(), + blob_id: 0, functions: HashMap::new(), @@ -309,6 +311,12 @@ impl Compiler { }).unwrap() } + fn new_blob_id(&mut self) -> usize { + let id = self.blob_id; + self.blob_id += 1; + id + } + fn add_constant(&mut self, value: Value) -> usize { self.constants.push(value); self.constants.len() - 1 @@ -612,9 +620,10 @@ impl Compiler { } fn find_blob(&self, name: &str) -> Option { - self.blobs.iter().enumerate() - .find(|(_, x)| x.name == name) - .map(|(i, _)| i) + self.constants.iter().enumerate().find_map(|(i, x)| match x { + Value::Blob(b) if b.name == name => Some(i), + _ => None, + }) } fn call(&mut self, block: &mut Block) { @@ -805,8 +814,7 @@ impl Compiler { } } } else if let Some(blob) = self.find_blob(&name) { - let string = self.add_constant(Value::Blob(blob)); - add_op(self, block, Op::Constant(string)); + add_op(self, block, Op::Constant(blob)); parse_branch!(self, block, self.call(block)); } else if let Some(slot) = self.find_extern_function(&name) { let string = self.add_constant(Value::ExternFunction(slot)); @@ -1085,7 +1093,11 @@ impl Compiler { "float" => Ok(Type::Float), "bool" => Ok(Type::Bool), "str" => Ok(Type::String), - x => self.find_blob(x).map(|blob| Type::Instance(blob)).ok_or(()), + x => { + let blob = self.find_blob(x) + .unwrap_or_else(|| { error!(self, "Unkown blob."); 0 } ); + Ok(Type::from(&self.constants[blob])) + } } } _ => Err(()), @@ -1103,7 +1115,7 @@ impl Compiler { expect!(self, Token::LeftBrace, "Expected 'blob' body. AKA '{'."); - let mut blob = Blob::new(&name); + let mut blob = Blob::new(self.new_blob_id(), &name); loop { if matches!(self.peek(), Token::EOF | Token::RightBrace) { break; } if matches!(self.peek(), Token::Newline) { self.eat(); continue; } @@ -1131,7 +1143,7 @@ impl Compiler { expect!(self, Token::RightBrace, "Expected '}' after 'blob' body. AKA '}'."); - self.blobs.push(blob); + self.constants.push(Value::Blob(Rc::new(blob))); } fn blob_field(&mut self, block: &mut Block) { @@ -1341,7 +1353,6 @@ impl Compiler { if self.errors.is_empty() { Ok(Prog { blocks: self.blocks.clone(), - blobs: self.blobs.iter().map(|x| Rc::new(x.clone())).collect(), functions: functions.iter().map(|(_, f)| *f).collect(), constants: self.constants.clone(), strings: self.strings.clone(), diff --git a/src/lib.rs b/src/lib.rs index c2aee90..497dba3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,16 +84,16 @@ pub enum Type { String, Tuple(Vec), Function(Vec, Box), - Blob(usize), - Instance(usize), + Blob(Rc), + Instance(Rc), } impl PartialEq for Type { fn eq(&self, other: &Self) -> bool { match (self, other) { (Type::Void, Type::Void) => true, - (Type::Instance(a), Type::Instance(b)) => a == b, - (Type::Blob(a), Type::Blob(b)) => a == b, + (Type::Instance(a), Type::Instance(b)) => *a == *b, + (Type::Blob(a), Type::Blob(b)) => *a == *b, (Type::Int, Type::Int) => true, (Type::Float, Type::Float) => true, (Type::Bool, Type::Bool) => true, @@ -111,8 +111,8 @@ impl PartialEq for Type { impl From<&Value> for Type { fn from(value: &Value) -> Type { match value { - Value::Instance(i, _) => Type::Instance(*i), - Value::Blob(i) => Type::Blob(*i), + Value::Instance(b, _) => Type::Instance(Rc::clone(b)), + Value::Blob(b) => Type::Blob(Rc::clone(b)), Value::Tuple(v) => { Type::Tuple(v.iter().map(|x| Type::from(x)).collect()) } @@ -136,8 +136,11 @@ impl From<&Type> for Value { fn from(ty: &Type) -> Self { match ty { Type::Void => Value::Nil, - Type::Blob(i) => Value::Blob(*i), - Type::Instance(i) => Value::Instance(*i, Rc::new(RefCell::new(Vec::new()))), + Type::Blob(b) => Value::Blob(Rc::clone(b)), + Type::Instance(b) => { + Value::Instance(Rc::clone(b), + Rc::new(RefCell::new(Vec::new()))) + } Type::Tuple(fields) => { Value::Tuple(Rc::new(fields.iter().map(Value::from).collect())) } @@ -163,8 +166,8 @@ impl From for Value { #[derive(Clone)] pub enum Value { Ty(Type), - Blob(usize), - Instance(usize, Rc>>), + Blob(Rc), + Instance(Rc, Rc>>), Tuple(Rc>), Float(f64), Int(i64), @@ -180,8 +183,8 @@ impl Debug for Value { fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Value::Ty(ty) => write!(fmt, "(type {:?})", ty), - Value::Blob(i) => write!(fmt, "(blob {})", i), - Value::Instance(i, v) => write!(fmt, "(inst {} {:?})", i, v), + Value::Blob(b) => write!(fmt, "(blob {})", b.name), + Value::Instance(b, v) => write!(fmt, "(inst {} {:?})", b.name, v), Value::Float(f) => write!(fmt, "(float {})", f), Value::Int(i) => write!(fmt, "(int {})", i), Value::Bool(b) => write!(fmt, "(bool {})", b), @@ -257,14 +260,22 @@ impl UpValue { #[derive(Debug, Clone)] pub struct Blob { + pub id: usize, pub name: String, /// Maps field names to their slot and type. pub fields: HashMap, } +impl PartialEq for Blob { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + impl Blob { - fn new(name: &str) -> Self { + fn new(id: usize, name: &str) -> Self { Self { + id: id, name: String::from(name), fields: HashMap::new(), } @@ -713,7 +724,6 @@ impl Block { #[derive(Clone)] pub struct Prog { pub blocks: Vec>>, - pub blobs: Vec>, pub functions: Vec, pub constants: Vec, pub strings: Vec, diff --git a/src/vm.rs b/src/vm.rs index 2cef854..b292a79 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -6,7 +6,7 @@ use std::rc::Rc; use owo_colors::OwoColorize; -use crate::{Blob, Block, Op, Prog, UpValue, Value, op}; +use crate::{Block, Op, Prog, UpValue, Value, op}; use crate::error::{Error, ErrorKind}; use crate::RustFunction; use crate::Type; @@ -57,7 +57,6 @@ pub struct VM { stack: Vec, frames: Vec, - blobs: Vec>, constants: Vec, strings: Vec, @@ -87,7 +86,6 @@ impl VM { stack: Vec::new(), frames: Vec::new(), - blobs: Vec::new(), constants: Vec::new(), strings: Vec::new(), @@ -284,7 +282,7 @@ impl VM { let inst = self.pop(); let field = self.string(field); if let Value::Instance(ty, values) = inst { - let slot = self.blobs[ty].fields.get(field).unwrap().0; + let slot = ty.fields.get(field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); @@ -295,7 +293,7 @@ impl VM { let (inst, value) = self.poppop(); let field = self.string(field); if let Value::Instance(ty, values) = inst { - let slot = self.blobs[ty].fields.get(field).unwrap().0; + let slot = ty.fields.get(field).unwrap().0; values.borrow_mut()[slot] = value; } else { error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); @@ -393,16 +391,14 @@ impl VM { Op::Call(num_args) => { let new_base = self.stack.len() - 1 - num_args; match self.stack[new_base].clone() { - Value::Blob(blob_id) => { - let blob = &self.blobs[blob_id]; - + Value::Blob(blob) => { let mut values = Vec::with_capacity(blob.fields.len()); for _ in 0..values.capacity() { values.push(Value::Nil); } self.pop(); - self.push(Value::Instance(blob_id, Rc::new(RefCell::new(values)))); + self.push(Value::Instance(blob, Rc::new(RefCell::new(values)))); } Value::Function(_, block) => { let inner = block.borrow(); @@ -483,7 +479,6 @@ impl VM { // Initalizes the VM for running. Run cannot be called before this. pub(crate) fn init(&mut self, prog: &Prog) { let block = Rc::clone(&prog.blocks[0]); - self.blobs = prog.blobs.clone(); self.constants = prog.constants.clone(); self.strings = prog.strings.clone(); @@ -570,7 +565,7 @@ impl VM { let inst = self.pop(); let field = self.string(field); if let Value::Instance(ty, _) = inst { - let value = Value::from(&self.blobs[ty].fields.get(field).unwrap().1); + let value = Value::from(ty.fields.get(field).unwrap().1.clone()); self.push(value); } else { self.push(Value::Nil); @@ -583,9 +578,9 @@ impl VM { let field = self.string(field); if let Value::Instance(ty, _) = inst { - let ty = &self.blobs[ty].fields.get(field).unwrap().1; + let ty = &ty.fields.get(field).unwrap().1; if ty != &Type::from(&value) { - error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); + error!(self, ErrorKind::RuntimeTypeError(op, vec![Value::from(ty)])); } } else { error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); @@ -646,9 +641,7 @@ impl VM { Op::Call(num_args) => { let new_base = self.stack.len() - 1 - num_args; match self.stack[new_base].clone() { - Value::Blob(blob_id) => { - let blob = &self.blobs[blob_id]; - + Value::Blob(blob) => { let mut values = Vec::with_capacity(blob.fields.len()); for _ in 0..values.capacity() { values.push(Value::Nil); @@ -659,7 +652,7 @@ impl VM { } self.pop(); - self.push(Value::Instance(blob_id, Rc::new(RefCell::new(values)))); + self.push(Value::Instance(blob, Rc::new(RefCell::new(values)))); } Value::Function(_, block) => { let inner = block.borrow(); @@ -771,7 +764,6 @@ impl VM { pub(crate) fn typecheck(&mut self, prog: &Prog) -> Result<(), Vec> { let mut errors = Vec::new(); - self.blobs = prog.blobs.clone(); self.constants = prog.constants.clone(); self.strings = prog.strings.clone(); self.runtime = false; -- cgit v1.2.1 From 4d2121c548492c591d3366f6a3b919b098c349d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 22:23:11 +0100 Subject: allow usages of blobs before definition --- src/compiler.rs | 61 +++++++++++++++++++++++++++++++++++---------------------- src/lib.rs | 11 +++++++++++ 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 9a29d6d..95290a0 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, path::{Path, PathBuf}}; use std::cell::RefCell; -use std::collections::HashMap; +use std::collections::{HashMap, hash_map::Entry}; use std::rc::Rc; use crate::{Blob, Block, Op, Prog, RustFunction, Type, Value}; @@ -229,12 +229,11 @@ pub(crate) struct Compiler { blocks: Vec>>, blob_id: usize, + unkowns: HashMap, functions: HashMap, constants: Vec, strings: Vec, - - } macro_rules! push_frame { @@ -293,6 +292,7 @@ impl Compiler { blocks: Vec::new(), blob_id: 0, + unkowns: HashMap::new(), functions: HashMap::new(), @@ -619,11 +619,18 @@ impl Compiler { Self::find_and_capture_variable(name, self.frames.iter_mut().rev()) } - fn find_blob(&self, name: &str) -> Option { - self.constants.iter().enumerate().find_map(|(i, x)| match x { + fn find_blob(&mut self, name: &str) -> usize { + let res = self.constants.iter().enumerate().find_map(|(i, x)| match x { Value::Blob(b) if b.name == name => Some(i), _ => None, - }) + }); + if res.is_some() { + return res.unwrap(); + } + let constant = self.add_constant(Value::Nil); + let line = self.line(); + let entry = self.unkowns.entry(name.to_string()); + entry.or_insert((constant, line)).0 } fn call(&mut self, block: &mut Block) { @@ -680,9 +687,6 @@ impl Compiler { break; } } - if !self.panic { - println!("LINE {} -- ", self.line()); - } } _ => { @@ -788,6 +792,16 @@ impl Compiler { Token::Identifier(name) => name, _ => unreachable!(), }; + + // Global functions take precedence + if let Some(slot) = self.find_extern_function(&name) { + let string = self.add_constant(Value::ExternFunction(slot)); + add_op(self, block, Op::Constant(string)); + self.call(block); + return; + } + + // Variables if let Some(var) = self.find_variable(&name) { if var.upvalue { add_op(self, block, Op::ReadUpvalue(var.slot)); @@ -803,26 +817,22 @@ impl Compiler { add_op(self, block, Op::Get(string)); } else { error!(self, "Expected fieldname after '.'."); - break; + return; } } _ => { if !parse_branch!(self, block, self.call(block)) { - break + return; } } } } - } else if let Some(blob) = self.find_blob(&name) { - add_op(self, block, Op::Constant(blob)); - parse_branch!(self, block, self.call(block)); - } else if let Some(slot) = self.find_extern_function(&name) { - let string = self.add_constant(Value::ExternFunction(slot)); - add_op(self, block, Op::Constant(string)); - self.call(block); - } else { - error!(self, format!("Using undefined variable {}.", name)); } + + // Blobs - Always returns a blob since it's filled in if it isn't used. + let blob = self.find_blob(&name); + add_op(self, block, Op::Constant(blob)); + parse_branch!(self, block, self.call(block)); } fn define_variable(&mut self, name: &str, typ: Type, _block: &mut Block) -> Result { @@ -1094,8 +1104,7 @@ impl Compiler { "bool" => Ok(Type::Bool), "str" => Ok(Type::String), x => { - let blob = self.find_blob(x) - .unwrap_or_else(|| { error!(self, "Unkown blob."); 0 } ); + let blob = self.find_blob(x); Ok(Type::from(&self.constants[blob])) } } @@ -1143,7 +1152,13 @@ impl Compiler { expect!(self, Token::RightBrace, "Expected '}' after 'blob' body. AKA '}'."); - self.constants.push(Value::Blob(Rc::new(blob))); + let blob = Value::Blob(Rc::new(blob)); + if let Entry::Occupied(entry) = self.unkowns.entry(name) { + let (_, (slot, _)) = entry.remove_entry(); + self.constants[slot] = blob; + } else { + self.constants.push(blob); + } } fn blob_field(&mut self, block: &mut Block) { diff --git a/src/lib.rs b/src/lib.rs index 497dba3..7253141 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1183,4 +1183,15 @@ a := 0 a <=> -1 ", ); + test_multiple!( + declaration_order, + simple: " +a := A() + +blob A { + a: int +} +", + + ); } -- cgit v1.2.1 From 3c5a33f008b40e272d3a720aa81fb5a8568a4527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 22:33:17 +0100 Subject: give better error messages --- src/compiler.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/compiler.rs b/src/compiler.rs index 95290a0..faca7f8 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -361,16 +361,21 @@ impl Compiler { } fn error(&mut self, kind: ErrorKind, message: Option) { + self.error_on_line(kind, self.line(), message); + } + + fn error_on_line(&mut self, kind: ErrorKind, line: usize, message: Option) { if self.panic { return } self.panic = true; self.errors.push(Error { kind, file: self.current_file.clone(), - line: self.line(), + line, message, }); } + fn peek(&self) -> Token { self.peek_at(0) } @@ -1363,6 +1368,18 @@ impl Compiler { add_op(self, &mut block, Op::Return); block.ty = Type::Function(Vec::new(), Box::new(Type::Void)); + if self.unkowns.len() != 0 { + let errors: Vec<_> = self.unkowns.iter().map(|(name, (_, line))| + (ErrorKind::SyntaxError(*line, Token::Identifier(name.clone())), + *line, + format!("Usage of undefined 'blob': '{}'.", name,) + )) + .collect(); + for (e, l, m) in errors.iter() { + self.error_on_line(e.clone(), *l, Some(m.clone())); + } + } + self.blocks.insert(0, Rc::new(RefCell::new(block))); if self.errors.is_empty() { -- cgit v1.2.1 From 42b6130e649b41671620134ed73fa2ae7b0990a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 22:36:32 +0100 Subject: add more tests --- src/lib.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7253141..907a1cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -833,6 +833,12 @@ mod tests { assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); } + #[test] + fn undefined_blob() { + assert_errs!(run_string("a :: B()\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); + } + + macro_rules! test_multiple { ($mod:ident, $( $fn:ident : $prog:literal ),+ $( , )? ) => { mod $mod { @@ -1183,6 +1189,7 @@ a := 0 a <=> -1 ", ); + test_multiple!( declaration_order, simple: " @@ -1193,5 +1200,15 @@ blob A { } ", + complex: " +a := A() +b := B() +c := C() +b2 := B() + +blob A { } +blob C { } +blob B { } +", ); } -- cgit v1.2.1 From f93bb0b3bf8cf26dfa337b643c90f321bd67520c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 23:33:52 +0100 Subject: sexy constant functions --- src/lib.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 907a1cd..1fa6368 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1192,7 +1192,7 @@ a <=> -1 test_multiple!( declaration_order, - simple: " + blob_simple: " a := A() blob A { @@ -1200,15 +1200,42 @@ blob A { } ", - complex: " + blob_complex: " a := A() b := B() c := C() b2 := B() -blob A { } +blob A { + c: C +} blob C { } blob B { } +", + + constant_function: " +a() +a :: fn {} +", + + constant_function_complex: " +h :: fn -> int { + ret 3 +} + +a() <=> 3 + +k :: fn -> int { + ret h() +} + +a :: fn -> int { + ret q() +} + +q :: fn -> int { + ret k() +} ", ); } -- cgit v1.2.1 From 2a8020706c6309ac23755839cfdb13cf4e11d303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 23:49:42 +0100 Subject: add magic blob inference --- src/compiler.rs | 45 ++++++++++++++++++++++++++++++++++++--------- src/lib.rs | 13 ++++++++++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index faca7f8..ca571ee 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -573,7 +573,7 @@ impl Compiler { /// Entry point for all expression parsing. fn expression(&mut self, block: &mut Block) { match self.peek_four() { - (Token::Fn, ..) => self.function(block), + (Token::Fn, ..) => { self.function(block, None); }, _ => self.parse_precedence(block, Prec::No), } } @@ -624,9 +624,10 @@ impl Compiler { Self::find_and_capture_variable(name, self.frames.iter_mut().rev()) } - fn find_blob(&mut self, name: &str) -> usize { + fn find_constant(&mut self, name: &str) -> usize { let res = self.constants.iter().enumerate().find_map(|(i, x)| match x { Value::Blob(b) if b.name == name => Some(i), + Value::Function(_, b) if b.borrow().name == name => Some(i), _ => None, }); if res.is_some() { @@ -703,11 +704,13 @@ impl Compiler { } // TODO(ed): de-complexify - fn function(&mut self, block: &mut Block) { + fn function(&mut self, block: &mut Block, name: Option<&str>) { expect!(self, Token::Fn, "Expected 'fn' at start of function."); let top = self.stack().len() - 1; - let name = if !self.stack()[top].active { + let name = if let Some(name) = name { + Cow::Owned(String::from(name)) + } else if !self.stack()[top].active { self.stack_mut()[top].active = true; Cow::Borrowed(&self.stack()[top].name) } else { @@ -787,8 +790,11 @@ impl Compiler { let function_block = Rc::new(RefCell::new(function_block)); - let constant = self.add_constant(Value::Function(Vec::new(), Rc::clone(&function_block))); + // Note(ed): We deliberately add the constant as late as possible. + // This behaviour is used in `constant_statement`. + let function = Value::Function(Vec::new(), Rc::clone(&function_block)); self.blocks[block_id] = function_block; + let constant = self.add_constant(function); add_op(self, block, Op::Constant(constant)); } @@ -835,8 +841,8 @@ impl Compiler { } // Blobs - Always returns a blob since it's filled in if it isn't used. - let blob = self.find_blob(&name); - add_op(self, block, Op::Constant(blob)); + let con = self.find_constant(&name); + add_op(self, block, Op::Constant(con)); parse_branch!(self, block, self.call(block)); } @@ -902,6 +908,20 @@ impl Compiler { } fn constant_statement(&mut self, name: &str, typ: Type, block: &mut Block) { + // Magical global constants + if self.frames.len() <= 1 { + if parse_branch!(self, block, self.function(block, Some(name))) { + // Remove the function, since it's a constant and we already + // added it. + block.ops.pop().unwrap(); + if let Entry::Occupied(entry) = self.unkowns.entry(String::from(name)) { + let (_, (slot, _)) = entry.remove_entry(); + self.constants[slot] = self.constants.pop().unwrap(); + } + return; + } + } + let slot = self.define_constant(name, typ.clone(), block); self.expression(block); @@ -1109,8 +1129,15 @@ impl Compiler { "bool" => Ok(Type::Bool), "str" => Ok(Type::String), x => { - let blob = self.find_blob(x); - Ok(Type::from(&self.constants[blob])) + let blob = self.find_constant(x); + if let Value::Blob(blob) = &self.constants[blob] { + Ok(Type::Instance(Rc::clone(blob))) + } else { + // TODO(ed): This is kinda bad. If the type cannot + // be found it tries to infer it during runtime + // and doesn't verify it. + Ok(Type::Unknown) + } } } } diff --git a/src/lib.rs b/src/lib.rs index 1fa6368..146e534 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1045,7 +1045,11 @@ a() <=> 4 a.a = 2 a.b = 3 a.a + a.b <=> 5 - 5 <=> a.a + a.b" + 5 <=> a.a + a.b", + blob_infer: " +blob A { } +a : A = A() +", ); test_multiple!(tuples, @@ -1213,6 +1217,13 @@ blob C { } blob B { } ", + blob_infer: " +blob A { } + +a : A = A() +", + + constant_function: " a() a :: fn {} -- cgit v1.2.1 From 06cc26d9639102f4ab9b0eabbf1ece3f395798e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 23:54:05 +0100 Subject: fix error message --- src/compiler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler.rs b/src/compiler.rs index ca571ee..ec78118 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -1399,7 +1399,7 @@ impl Compiler { let errors: Vec<_> = self.unkowns.iter().map(|(name, (_, line))| (ErrorKind::SyntaxError(*line, Token::Identifier(name.clone())), *line, - format!("Usage of undefined 'blob': '{}'.", name,) + format!("Usage of undefined value: '{}'.", name,) )) .collect(); for (e, l, m) in errors.iter() { -- cgit v1.2.1 From 1a6fd93e49c01f8291cc594445ef63b407375b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Mon, 15 Feb 2021 23:54:19 +0100 Subject: add test for upvalues --- src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 146e534..b1bae9f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1248,5 +1248,20 @@ q :: fn -> int { ret k() } ", + + constant_function_closure: " +q := 1 + +f :: fn -> int { + q += 1 + ret q +} + +f() <=> 2 +f() <=> 3 +f() <=> 4 +f() <=> 5 +", + ); } -- cgit v1.2.1 From ecc84fc8259ab0f2f5754718ed70e3a57048a540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 00:02:23 +0100 Subject: add test that should work --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b1bae9f..8821710 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1261,6 +1261,25 @@ f() <=> 2 f() <=> 3 f() <=> 4 f() <=> 5 +", + + constants_in_inner_functions: " +q : int = 0 + +f :: fn -> fn -> { + g :: fn { + q += 1 + } + ret g +} + +g := f() +g() +q <=> 3 +g() +q <=> 4 +g() +q <=> 5 ", ); -- cgit v1.2.1 From e86b1be782c2c2f57e968557d7f91bbcc7b8b27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 21:10:03 +0100 Subject: fix the failing testcase --- src/compiler.rs | 3 +++ src/lib.rs | 12 +++++++++--- src/vm.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index ec78118..361d93a 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -917,6 +917,9 @@ impl Compiler { if let Entry::Occupied(entry) = self.unkowns.entry(String::from(name)) { let (_, (slot, _)) = entry.remove_entry(); self.constants[slot] = self.constants.pop().unwrap(); + add_op(self, block, Op::Link(slot)); + } else { + add_op(self, block, Op::Link(self.constants.len() - 1)); } return; } diff --git a/src/lib.rs b/src/lib.rs index 8821710..b39b4bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -480,6 +480,12 @@ pub enum Op { /// Does not affect the stack. Define(usize), + /// Links the upvalues for the given constant + /// function. This updates the constant stack. + /// + /// Does not affect the stack. + Link(usize), + /// Calls "something" with the given number /// of arguments. The callable value is /// then replaced with the result. @@ -1275,11 +1281,11 @@ f :: fn -> fn -> { g := f() g() -q <=> 3 +q <=> 1 g() -q <=> 4 +q <=> 2 g() -q <=> 5 +q <=> 3 ", ); diff --git a/src/vm.rs b/src/vm.rs index b292a79..f5c1cf0 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -258,6 +258,34 @@ impl VM { self.push(value); } + Op::Link(slot) => { + let offset = self.frame().stack_offset; + let constant = self.constant(slot).clone(); + let constant = match constant { + Value::Function(_, block) => { + let mut ups = Vec::new(); + for (slot, is_up, _) in block.borrow().upvalues.iter() { + let up = if *is_up { + if let Value::Function(local_ups, _) = &self.stack[offset] { + Rc::clone(&local_ups[*slot]) + } else { + unreachable!() + } + } else { + let slot = self.frame().stack_offset + slot; + Rc::clone(self.find_upvalue(slot)) + }; + ups.push(up); + } + Value::Function(ups, block) + }, + value => error!(self, + ErrorKind::RuntimeTypeError(op, vec![value.clone()]), + format!("Not a function {:?}.", value)), + }; + self.constants[slot] = constant; + } + Op::Index => { let slot = self.stack.pop().unwrap(); let val = self.stack.pop().unwrap(); @@ -638,6 +666,19 @@ impl VM { } } + Op::Link(slot) => { + println!("{:?}", self.constants); + println!("{:?} - {}", self.constant(slot), slot); + match self.constant(slot).clone() { + Value::Function(_, _) => {} + value => { + error!(self, + ErrorKind::TypeError(op, vec![Type::from(&value)]), + format!("Cannot link non-function {:?}.", value)); + } + }; + } + Op::Call(num_args) => { let new_base = self.stack.len() - 1 - num_args; match self.stack[new_base].clone() { -- cgit v1.2.1 From ef47ba4e8bdec20a57f325efd129cc3e183e0b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 21:10:26 +0100 Subject: clearer test output --- src/lib.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b39b4bb..c838743 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -694,6 +694,8 @@ impl Block { pub fn debug_print(&self) { println!(" === {} ===", self.name.blue()); for (i, s) in self.ops.iter().enumerate() { + // TODO(ed): This print should only do one call to print. + // Otherwise we can get race conditions in a single line. if self.line_offsets.contains_key(&i) { print!("{:5} ", self.line_offsets[&i].red()); } else { @@ -763,6 +765,7 @@ mod tests { use std::time::Duration; use std::sync::mpsc; use std::thread; + use owo_colors::OwoColorize; // Shamelessly stolen from https://github.com/rust-lang/rfcs/issues/2798 pub fn panic_after(d: Duration, f: F) -> T @@ -778,10 +781,19 @@ mod tests { val }); - match done_rx.recv_timeout(d) { - Ok(_) => handle.join().expect("Thread panicked"), - Err(_) => panic!("Thread took too long"), - } + let msg = match done_rx.recv_timeout(d) { + Ok(_) => { + return handle.join().expect("Thread panicked"); + } + Err(mpsc::RecvTimeoutError::Timeout) => { + "Test took too long to complete" + }, + Err(mpsc::RecvTimeoutError::Disconnected) => { + "Test produced incorrect result" + }, + }; + println!(" #### {} ####", msg.red()); + panic!(msg); } #[macro_export] @@ -1010,10 +1022,9 @@ b() <=> 2 b() <=> 3 a() <=> 4 -" +", //TODO this tests doesn't terminate in proper time if we print blocks and ops - /* fibonacci: "fibonacci : fn int -> int = fn n: int -> int { if n == 0 { ret 0 @@ -1024,9 +1035,7 @@ a() <=> 4 } ret fibonacci(n - 1) + fibonacci(n - 2) } - fibonacci(10) <=> 55 - fibonacci(20) <=> 6765" - */ + fibonacci(10) <=> 55", ); test_multiple!( -- cgit v1.2.1 From 6801bea82458ab50e6d81b3bb3f3aac7b2f93ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 21:29:11 +0100 Subject: let some fix --- src/compiler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 361d93a..be99b01 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -630,8 +630,8 @@ impl Compiler { Value::Function(_, b) if b.borrow().name == name => Some(i), _ => None, }); - if res.is_some() { - return res.unwrap(); + if let Some(res) = res { + return res; } let constant = self.add_constant(Value::Nil); let line = self.line(); -- cgit v1.2.1 From 5836cac092821da390b4d754407125864b77288d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 21:34:51 +0100 Subject: unkowns -> unknown --- src/compiler.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index be99b01..94ae2aa 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -229,7 +229,7 @@ pub(crate) struct Compiler { blocks: Vec>>, blob_id: usize, - unkowns: HashMap, + unknown: HashMap, functions: HashMap, constants: Vec, @@ -292,7 +292,7 @@ impl Compiler { blocks: Vec::new(), blob_id: 0, - unkowns: HashMap::new(), + unknown: HashMap::new(), functions: HashMap::new(), @@ -635,7 +635,7 @@ impl Compiler { } let constant = self.add_constant(Value::Nil); let line = self.line(); - let entry = self.unkowns.entry(name.to_string()); + let entry = self.unknown.entry(name.to_string()); entry.or_insert((constant, line)).0 } @@ -914,7 +914,7 @@ impl Compiler { // Remove the function, since it's a constant and we already // added it. block.ops.pop().unwrap(); - if let Entry::Occupied(entry) = self.unkowns.entry(String::from(name)) { + if let Entry::Occupied(entry) = self.unknown.entry(String::from(name)) { let (_, (slot, _)) = entry.remove_entry(); self.constants[slot] = self.constants.pop().unwrap(); add_op(self, block, Op::Link(slot)); @@ -1188,7 +1188,7 @@ impl Compiler { expect!(self, Token::RightBrace, "Expected '}' after 'blob' body. AKA '}'."); let blob = Value::Blob(Rc::new(blob)); - if let Entry::Occupied(entry) = self.unkowns.entry(name) { + if let Entry::Occupied(entry) = self.unknown.entry(name) { let (_, (slot, _)) = entry.remove_entry(); self.constants[slot] = blob; } else { @@ -1398,8 +1398,8 @@ impl Compiler { add_op(self, &mut block, Op::Return); block.ty = Type::Function(Vec::new(), Box::new(Type::Void)); - if self.unkowns.len() != 0 { - let errors: Vec<_> = self.unkowns.iter().map(|(name, (_, line))| + if self.unknown.len() != 0 { + let errors: Vec<_> = self.unknown.iter().map(|(name, (_, line))| (ErrorKind::SyntaxError(*line, Token::Identifier(name.clone())), *line, format!("Usage of undefined value: '{}'.", name,) -- cgit v1.2.1 From 299981e74e625b7dc85ebfe2adbd066f6c68c0d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Tue, 16 Feb 2021 21:35:38 +0100 Subject: remove long test --- src/lib.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c838743..77176c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1023,19 +1023,6 @@ b() <=> 3 a() <=> 4 ", - - //TODO this tests doesn't terminate in proper time if we print blocks and ops - fibonacci: "fibonacci : fn int -> int = fn n: int -> int { - if n == 0 { - ret 0 - } else if n == 1 { - ret 1 - } else if n < 0 { - - } - ret fibonacci(n - 1) + fibonacci(n - 2) - } - fibonacci(10) <=> 55", ); test_multiple!( -- cgit v1.2.1