11use crate :: ir:: ast:: Function ;
22use crate :: ir:: ast:: Name ;
33use crate :: ir:: ast:: ValueConstructor ;
4+ use indexmap:: IndexMap ;
45use std:: collections:: HashMap ;
56use std:: collections:: LinkedList ;
67
@@ -9,6 +10,7 @@ pub struct Scope<A> {
910 pub variables : HashMap < Name , ( bool , A ) > ,
1011 pub functions : HashMap < Name , Function > ,
1112 pub adts : HashMap < Name , Vec < ValueConstructor > > ,
13+ pub tests : IndexMap < Name , Function > ,
1214}
1315
1416impl < A : Clone > Scope < A > {
@@ -17,6 +19,7 @@ impl<A: Clone> Scope<A> {
1719 variables : HashMap :: new ( ) ,
1820 functions : HashMap :: new ( ) ,
1921 adts : HashMap :: new ( ) ,
22+ tests : IndexMap :: new ( ) , //TODO: Apresentar Mudança no Environment
2023 }
2124 }
2225
@@ -30,6 +33,11 @@ impl<A: Clone> Scope<A> {
3033 return ( ) ;
3134 }
3235
36+ fn map_test ( & mut self , test : Function ) -> ( ) {
37+ self . tests . insert ( test. name . clone ( ) , test) ;
38+ return ( ) ;
39+ }
40+
3341 fn map_adt ( & mut self , name : Name , adt : Vec < ValueConstructor > ) -> ( ) {
3442 self . adts . insert ( name. clone ( ) , adt) ;
3543 return ( ) ;
@@ -45,6 +53,10 @@ impl<A: Clone> Scope<A> {
4553 self . functions . get ( name)
4654 }
4755
56+ fn lookup_test ( & self , name : & Name ) -> Option < & Function > {
57+ self . tests . get ( name)
58+ }
59+
4860 fn lookup_adt ( & self , name : & Name ) -> Option < & Vec < ValueConstructor > > {
4961 self . adts . get ( name)
5062 }
@@ -78,6 +90,13 @@ impl<A: Clone> Environment<A> {
7890 }
7991 }
8092
93+ pub fn map_test ( & mut self , test : Function ) -> ( ) {
94+ match self . stack . front_mut ( ) {
95+ None => self . globals . map_test ( test) ,
96+ Some ( top) => top. map_test ( test) ,
97+ }
98+ }
99+
81100 pub fn map_adt ( & mut self , name : Name , cons : Vec < ValueConstructor > ) -> ( ) {
82101 match self . stack . front_mut ( ) {
83102 None => self . globals . map_adt ( name, cons) ,
@@ -103,6 +122,28 @@ impl<A: Clone> Environment<A> {
103122 self . globals . lookup_function ( name)
104123 }
105124
125+ pub fn lookup_test ( & self , name : & Name ) -> Option < & Function > {
126+ for scope in self . stack . iter ( ) {
127+ if let Some ( test) = scope. lookup_test ( name) {
128+ return Some ( test) ;
129+ }
130+ }
131+ self . globals . lookup_test ( name)
132+ }
133+
134+ pub fn get_all_tests ( & self ) -> Vec < Function > {
135+ let mut tests = Vec :: new ( ) ;
136+ for scope in self . stack . iter ( ) {
137+ for test in scope. tests . values ( ) {
138+ tests. push ( test. clone ( ) ) ;
139+ }
140+ }
141+ for test in self . globals . tests . values ( ) {
142+ tests. push ( test. clone ( ) ) ;
143+ }
144+ tests
145+ }
146+
106147 pub fn lookup_adt ( & self , name : & Name ) -> Option < & Vec < ValueConstructor > > {
107148 for scope in self . stack . iter ( ) {
108149 if let Some ( cons) = scope. lookup_adt ( name) {
@@ -147,6 +188,22 @@ impl<A: Clone> Environment<A> {
147188 }
148189}
149190
191+ pub struct TestResult {
192+ pub name : Name ,
193+ pub result : bool ,
194+ pub error : Option < String > ,
195+ }
196+
197+ impl TestResult {
198+ pub fn new ( name : Name , result : bool , error : Option < String > ) -> Self {
199+ TestResult {
200+ name,
201+ result,
202+ error,
203+ }
204+ }
205+ }
206+
150207#[ cfg( test) ]
151208mod tests {
152209 use super :: * ;
0 commit comments