8000 [refactoring] Adapting the type checker to the new environment · UnBCIC-TP2/r-python@7c9b722 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c9b722

Browse files
committed
[refactoring] Adapting the type checker to the new environment
1 parent e0dc0c8 commit 7c9b722

File tree

3 files changed

+817
-822
lines changed

3 files changed

+817
-822
lines changed

src/environment/environment.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
use crate::ir::ast::Function;
32
use crate::ir::ast::Name;
43

@@ -7,96 +6,97 @@ use std::collections::LinkedList;
76

87
pub struct Scope<A> {
98
pub variables: HashMap<Name, A>,
10-
pub functions: HashMap<Name, Function>
9+
pub functions: HashMap<Name, Function>,
1110
}
1211

1312
impl<A> Scope<A> {
1413
fn new() -> Scope<A> {
15-
Scope { variables: HashMap::new(), functions: HashMap::new() }
14+
Scope {
15+
variables: HashMap::new(),
16+
functions: HashMap::new(),
17+
}
1618
}
1719

1820
fn map_variable(&mut self, var: Name, value: A) -> () {
19-
self.variables.insert(var, value);
20-
return ()
21+
self.variables.insert(var, value);
22+
return ();
2123
}
2224

2325
fn lookup(&mut self, var: Name) -> Option<&A> {
24-
self.variables.get(&var)
26+
self.variables.get(&var)
2527
}
2628
}
2729

2830
pub struct Environment<A> {
2931
pub globals: Scope<A>,
30-
pub stack: LinkedList<Scope<A>>
32+
pub stack: LinkedList<Scope<A>>,
3133
}
3234

3335
impl<A> Environment<A> {
34-
3536
pub fn new() -> Environment<A> {
36-
Environment { globals: Scope::new(), stack: LinkedList::new() }
37+
Environment {
38+
globals: Scope::new(),
39+
stack: LinkedList::new(),
40+
}
3741
}
38-
42+
3943
pub fn map_variable(&mut self, var: Name, value: A) -> () {
40-
match self.stack.front_mut() {
41-
None => self.globals.map_variable(var, value),
42-
Some(top) => top.map_variable(var, value)
43-
44-
};
45-
return ()
44+
match self.stack.front_mut() {
45+
None => self.globals.map_variable(var, value),
46+
Some(top) => top.map_variable(var, value),
47+
};
48+
return ();
4649
}
4750

4851
pub fn lookup(&mut self, var: Name) -> Option<&A> {
49-
match self.stack.front_mut() {
50-
None => self.globals.lookup(var),
51-
Some(top) => top.lookup(var)
52-
}
52+
match self.stack.front_mut() {
53+
None => self.globals.lookup(var),
54+
Some(top) => top.lookup(var),
55+
}
5356
}
5457

5558
pub fn push(&mut self) -> () {
56-
self.stack.push_front(Scope::new());
59+
self.stack.push_front(Scope::new());
5760
}
5861

5962
pub fn pop(&mut self) -> () {
60-
self.stack.pop_front();
63+
self.stack.pop_front();
6164
}
6265
}
6366

64-
6567
#[cfg(test)]
6668
mod tests {
67-
use crate::environment::environment::{Scope, Environment};
68-
69+
use crate::environment::environment::{Environment, Scope};
70+
6971
#[test]
7072
fn eval_map_and_lookup_var() {
71-
let mut s: Scope<i32> = Scope::new();
73+
let mut s: Scope<i32> = Scope::new();
74+
75+
s.map_variable("x".to_string(), 32);
76+
s.map_variable("y".to_string(), 23);
7277

73-
s.map_variable("x".to_string(), 32);
74-
s.map_variable("y".to_string(), 23);
75-
76-
assert_eq!(Some(32), s.lookup("x".to_string()).copied());
77-
assert_eq!(Some(23), s.lookup("y".to_string()).copied());
78+
assert_eq!(Some(32), s.lookup("x".to_string()).copied());
79+
assert_eq!(Some(23), s.lookup("y".to_string()).copied());
7880
}
7981

8082
#[test]
8183
fn eval_environment() {
82-
let mut env: Environment<i32> = Environment::new();
84+
let mut env: Environment<i32> = Environment::new();
8385

84-
env.map_variable("x".to_string(), 32);
86+
env.map_variable("x".to_string(), 32);
8587

86-
env.push();
88+
env.push();
8789

88-
env.map_variable("x".to_string(), 55);
89-
env.map_variable("y".to_string(), 23);
90-
91-
assert_eq!(Some(55), env.lookup("x".to_string()).copied());
92-
assert_eq!(Some(23), env.lookup("y".to_string()).copied());
93-
assert_eq!(None, env.lookup("a".to_string()).copied());
90+
env.map_variable("x".to_string(), 55);
91+
env.map_variable("y".to_string(), 23);
9492

95-
env.pop();
93+
assert_eq!(Some(55), env.lookup("x".to_string()).copied());
94+
assert_eq!(Some(23), env.lookup("y".to_string()).copied());
95+
assert_eq!(None, env.lookup("a".to_string()).copied());
9696

97-
assert_eq!(Some(32), env.lookup("x".to_string()).copied());
98-
assert_eq!(None, env.lookup("y".to_string()).copied());
99-
}
100-
97+
env.pop();
10198

99+
assert_eq!(Some(32), env.lookup("x".to_string()).copied());
100+
assert_eq!(None, env.lookup("y".to_string()).copied());
101+
}
102102
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use std::collections::HashMap;
1010
use std::fs::File;
1111
use std::io::Write;*/
1212

13+
pub mod environment;
1314
pub mod interpreter;
1415
pub mod ir;
1516
pub mod parser;
1617
pub mod tc;
17-
pub mod environment;
1818

1919
fn main() {
2020
println!("Hello, world!");

0 commit comments

Comments
 (0)
0