Skip to content
Snippets Groups Projects
Commit 4d9fd76d authored by Per Lindgren's avatar Per Lindgren
Browse files

borrow check, wip2

parent 53a6eeba
No related branches found
No related tags found
No related merge requests found
...@@ -35,3 +35,29 @@ struct Mem(VecDeque<IdVal>); ...@@ -35,3 +35,29 @@ struct Mem(VecDeque<IdVal>);
On a function call, the parameters are pushed to the stack, and a new stack frame is generated (on entry of each sequence of statements). On a function call, the parameters are pushed to the stack, and a new stack frame is generated (on entry of each sequence of statements).
References are handled through `Ref(Id)`, and are transitive. You cannot reference memory items in any other way (so this is a limitation compared to real Rust). References are handled through `Ref(Id)`, and are transitive. You cannot reference memory items in any other way (so this is a limitation compared to real Rust).
## check
The `check` module performs basic type checking and borrow checking. In the following the basic reasoning is discussed.
``` Rust
pub enum Type {
Unit, // (), // maybe just a special case of tuple, see todo
Never, // !
Bool, // bool
I32, // i32
Ref(Box<Type>), // & mut? Type -- used in parser
Mut(Box<Type>), // mut Type
Unknown, // Not yet assigned
}
pub type IdType = HashMap<Id, Type>;
type Scope = i32;
pub struct VarEnv(VecDeque<IdType>);
```
The `var_env : VarEnv` holds a stack of scopes. A lookup (`get(Id)-> Option<(Scope, &Type)>`), returns the type and what scope it is defined in (0, being arguments, 1, function body, 2 ... inner scopes). Inner scopes emerge from blocks, or block statements (if/while etc).
fn main() {
let mut a = 1;
let b = &mut a;
let c = &mut a;
let c: &mut i32 = b;
}
...@@ -23,6 +23,7 @@ pub fn new_fn_env(v: &Vec<FnDecl>) -> FnEnv { ...@@ -23,6 +23,7 @@ pub fn new_fn_env(v: &Vec<FnDecl>) -> FnEnv {
} }
pub type IdType = HashMap<Id, Type>; pub type IdType = HashMap<Id, Type>;
type Scope = i32; type Scope = i32;
#[derive(Debug)] #[derive(Debug)]
......
...@@ -170,3 +170,8 @@ fn check_let_if() { ...@@ -170,3 +170,8 @@ fn check_let_if() {
fn wip() { fn wip() {
check(&read_file::parse("examples/wip.rs")).unwrap(); check(&read_file::parse("examples/wip.rs")).unwrap();
} }
#[test]
fn borrow() {
check(&read_file::parse("examples/borrow.rs")).unwrap();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment