-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
None working example of new `Scopes::add` for context.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,18 +43,25 @@ where | |
None | ||
} | ||
|
||
pub fn add(&mut self, s: K, val: T) { | ||
self.scopes.last_mut().unwrap().insert(s, val); | ||
/// Note: Does not check for existing key, replaces old value | ||
pub fn add(&mut self, s: K, val: T) -> std::result::Result<(), &str> { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Champii
Owner
|
||
match self.scopes.last_mut().unwrap().insert(s, val) { | ||
Some(prev) => { Err("Key '{prev}', already exists!") }, | ||
None => { Ok(()) }, | ||
} | ||
} | ||
|
||
pub fn extend(&mut self, other: &Scope<K, T>) { | ||
self.scopes.last_mut().unwrap().extend(other.clone()) | ||
} | ||
|
||
/// `push` should take a scope object | ||
This comment has been minimized.
Sorry, something went wrong.
Champii
Owner
|
||
pub fn push(&mut self) { | ||
self.scopes.push(Scope::new()) | ||
} | ||
|
||
/// `pop` should return the popped value | ||
/// in a `Option<V>` or a `Result<V, E>` | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Champii
Owner
|
||
pub fn pop(&mut self) { | ||
self.scopes.pop(); | ||
} | ||
|
@@ -68,23 +75,26 @@ mod tests { | |
fn basic_scope() { | ||
let mut scopes = Scopes::default(); | ||
|
||
scopes.add("a", 1); | ||
scopes.add("b", 2); | ||
assert_eq!(scopes.add("a", 1), Ok(()) ); | ||
assert_eq!(scopes.add("b", 2), Ok(()) ); | ||
|
||
assert_eq!(scopes.get("a").unwrap(), 1); | ||
assert_eq!(scopes.get("b").unwrap(), 2); | ||
|
||
/// Push new scope | ||
scopes.push(); | ||
|
||
scopes.add("b", 4); | ||
assert_eq!(scopes.add("b", 4), Ok(()) ); | ||
|
||
assert_eq!(scopes.get("a").unwrap(), 1); | ||
assert_eq!(scopes.get("b").unwrap(), 4); | ||
|
||
scopes.add("a", 3); | ||
assert_eq!(scopes.add("a", 3), Ok(()) ); | ||
|
||
assert_eq!(scopes.get("a").unwrap(), 3); | ||
assert_eq!(scopes.get("b").unwrap(), 4); | ||
assert_eq!(scopes.get("a").unwrap(), 1); | ||
assert_eq!(scopes.get("b").unwrap(), 2); | ||
|
||
assert_eq!(scopes.add("a", 4), Err("Key 'a', already exists!")); | ||
|
||
scopes.pop(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,14 @@ impl<'a> ResolveCtx<'a> { | |
} | ||
|
||
/// TODO: GitHub Issue #150 | ||
/// Could use #![feature(map_try_insert)] | ||
/// <<: Ensures unique entry into the map. | ||
pub fn add_to_current_scope(&mut self, name: String, node_id: NodeId) { | ||
if let Some(ref mut scopes) = self.scopes.get_mut(&self.cur_scope) { | ||
scopes.add(name, node_id); | ||
match scopes.add(name, node_id) { | ||
Ok(_) => { }, | ||
Err(_err) => { unimplemented!{} }, | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
Champii
Owner
|
||
} | ||
} | ||
|
||
|
@@ -36,10 +41,14 @@ impl<'a> ResolveCtx<'a> { | |
struct_scope_name.path.push(Identifier::new(struct_name, 0)); | ||
|
||
if let Some(ref mut scopes) = self.scopes.get_mut(&struct_scope_name) { | ||
scopes.add(name.clone(), node_id); | ||
match scopes.add(name.clone(), node_id) { | ||
Ok(_) => { }, | ||
Err(_err) => { unimplemented!{} }, | ||
} | ||
} | ||
} | ||
|
||
|
||
pub fn new_struct(&mut self, name: Identifier) { | ||
let mut struct_scope_name = self.cur_scope.clone(); | ||
struct_scope_name.path.push(name); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
foo: a, b = a + b | ||
foo: a = a + 2 | ||
This comment has been minimized.
Sorry, something went wrong.
Champii
Owner
|
||
|
||
main: -> foo 2, 5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-1 |
The
Result
here is nice, but we don't really care about a descriptive error at this point, we could just return theOption<T>
from theinsert
and check if its aSome
orNone
in theadd_to_current_scope()
method.Note that we could use a Result with a nice Error, but that would mean to add an internal error just for that (I don't like to have a raw
&str
as error type)In the end, this is up to you :)