aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/compiler.rs18
-rw-r--r--src/lib.rs50
-rw-r--r--src/vm.rs19
3 files changed, 69 insertions, 18 deletions
diff --git a/src/compiler.rs b/src/compiler.rs
index 6bb749c..7b9fd9a 100644
--- a/src/compiler.rs
+++ b/src/compiler.rs
@@ -796,10 +796,10 @@ impl Compiler {
let mut args = Vec::new();
let mut return_type = Type::Void;
- let mut function_block = Block::new(&name, &self.current_file, self.line());
+ let mut function_block = Block::new(&name, &self.current_file);
let block_id = self.blocks.len();
- let temp_block = Block::new(&name, &self.current_file, self.line());
+ let temp_block = Block::new(&name, &self.current_file);
self.blocks.push(Rc::new(RefCell::new(temp_block)));
let _ret = push_frame!(self, function_block, {
@@ -961,12 +961,18 @@ 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.unknown.entry(String::from(name)) {
+ let slot = 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));
+ slot
} else {
- add_op(self, block, Op::Link(self.constants.len() - 1));
+ self.constants.len() - 1
+ };
+ add_op(self, block, Op::Link(slot));
+ if let Value::Function(_, block) = &self.constants[slot] {
+ block.borrow_mut().mark_constant();
+ } else {
+ unreachable!();
}
return;
}
@@ -1428,7 +1434,7 @@ impl Compiler {
let main = Variable::new("/main/", false, Type::Void);
let _ = self.define(main);
- let mut block = Block::new(name, file, 0);
+ let mut block = Block::new(name, file);
while self.peek() != Token::EOF {
self.statement(&mut block);
expect!(self, Token::Newline | Token::EOF,
diff --git a/src/lib.rs b/src/lib.rs
index 52b0d9b..40bae2b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -151,7 +151,7 @@ impl From<&Type> for Value {
Type::String => Value::String(Rc::new("".to_string())),
Type::Function(_, _) => Value::Function(
Vec::new(),
- Rc::new(RefCell::new(Block::empty_with_type(ty)))),
+ Rc::new(RefCell::new(Block::stubbed_block(ty)))),
}
}
}
@@ -626,35 +626,58 @@ mod op {
}
#[derive(Debug)]
+enum BlockLinkState {
+ Linked,
+ Unlinked,
+ Nothing,
+}
+
+#[derive(Debug)]
pub struct Block {
pub ty: Type,
upvalues: Vec<(usize, bool, Type)>,
+ linking: BlockLinkState,
pub name: String,
pub file: PathBuf,
ops: Vec<Op>,
last_line_offset: usize,
line_offsets: HashMap<usize, usize>,
- line: usize,
}
impl Block {
- fn new(name: &str, file: &Path, line: usize) -> Self {
+ fn new(name: &str, file: &Path) -> Self {
Self {
ty: Type::Void,
upvalues: Vec::new(),
+ linking: BlockLinkState::Nothing,
+
name: String::from(name),
file: file.to_owned(),
ops: Vec::new(),
last_line_offset: 0,
line_offsets: HashMap::new(),
- line,
}
}
+ fn mark_constant(&mut self) {
+ if self.upvalues.is_empty() {
+ return;
+ }
+ self.linking = BlockLinkState::Unlinked;
+ }
+
+ fn link(&mut self) {
+ self.linking = BlockLinkState::Linked;
+ }
+
+ fn needs_linking(&self) -> bool {
+ matches!(self.linking, BlockLinkState::Unlinked)
+ }
+
// Used to create empty functions.
- fn empty_with_type(ty: &Type) -> Self {
- let mut block = Block::new("/empty/", Path::new(""), 0);
+ fn stubbed_block(ty: &Type) -> Self {
+ let mut block = Block::new("/empty/", Path::new(""));
block.ty = ty.clone();
block
}
@@ -857,6 +880,21 @@ mod tests {
}
#[test]
+ fn call_before_link() {
+ let prog = "
+a := 1
+f()
+c := 5
+
+f :: fn {
+ c <=> 5
+}
+a
+ ";
+ assert_errs!(run_string(prog, true, Vec::new()), [ErrorKind::InvalidProgram, ErrorKind::RuntimeTypeError(_, _)]);
+ }
+
+ #[test]
fn unused_variable() {
assert_errs!(run_string("a := 1", true, Vec::new()), [ErrorKind::SyntaxError(1, _)]);
}
diff --git a/src/vm.rs b/src/vm.rs
index 70cb5e4..8d8ec23 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -557,6 +557,13 @@ impl VM {
Value::Function(_, block) => {
self.push(Value::Function(Vec::new(), block.clone()));
+ if block.borrow().needs_linking() {
+ error!(self,
+ ErrorKind::InvalidProgram,
+ format!("Calling function '{}' before all captured variables are declared.",
+ block.borrow().name));
+ }
+
let mut types = Vec::new();
for (slot, is_up, ty) in block.borrow().upvalues.iter() {
if *is_up {
@@ -667,10 +674,10 @@ impl VM {
}
Op::Link(slot) => {
- println!("{:?}", self.constants);
- println!("{:?} - {}", self.constant(slot), slot);
match self.constant(slot).clone() {
- Value::Function(_, _) => {}
+ Value::Function(_, block) => {
+ block.borrow_mut().link();
+ }
value => {
error!(self,
ErrorKind::TypeError(op, vec![Type::from(&value)]),
@@ -733,7 +740,7 @@ impl VM {
}
_ => {
error!(self,
- ErrorKind::TypeError(op, vec![Type::from(&self.stack[new_base])]),
+ ErrorKind::InvalidProgram,
format!("Tried to call non-function {:?}", self.stack[new_base]));
}
}
@@ -773,7 +780,7 @@ impl VM {
});
if self.print_blocks {
- println!("\n [[{}]]\n", "TYPECHECK".purple());
+ println!("\n [[{} - {}]]\n", "TYPECHECKING".purple(), self.frame().block.borrow().name);
self.frame().block.borrow().debug_print();
}
@@ -833,7 +840,7 @@ mod tests {
i()
}
f",
- [ErrorKind::TypeError(_, _)]);
+ [ErrorKind::InvalidProgram]);
test_string!(wrong_params, "
f : fn -> int = fn a: int -> int {}