1
1
pub type Name = String ;
2
2
3
- use nom:: IResult ;
4
- use std:: collections:: HashMap ;
5
-
6
- #[ derive( Debug , PartialEq , Clone ) ]
7
- pub struct Frame < A > {
8
- pub parent_function : Option < Function > ,
9
- pub parent_key : Option < ( Name , i32 ) > ,
10
- pub variables : HashMap < Name , A > ,
11
- pub tests : HashMap < Name , Function > ,
12
- }
13
-
14
- impl < A > Frame < A > {
15
- pub fn new ( func : Option < Function > , key : Option < ( Name , i32 ) > ) -> Frame < A > {
16
- let variables: HashMap < Name , A > = HashMap :: new ( ) ;
17
- let tests: HashMap < Name , Function > = HashMap :: new ( ) ;
18
- return Frame {
19
- parent_function : func,
20
- parent_key : key,
21
- variables,
22
- tests,
23
- } ;
24
- }
25
- }
26
-
27
- #[ derive( Debug , PartialEq , Clone ) ]
28
- pub struct Environment < A > {
29
- pub scope : Function ,
30
- pub recursion : i32 ,
31
- pub stack : HashMap < ( Name , i32 ) , Frame < A > > ,
32
- pub type_env : HashMap < Name , Vec < ValueConstructor > > ,
33
- }
34
-
35
- impl < A > Environment < A > {
36
- pub fn new ( ) -> Environment < A > {
37
- let frame: Frame < A > = Frame :: new ( None , None ) ;
38
- let scope = Function :: new ( ) ;
39
-
40
- return Environment {
41
- scope,
42
- recursion : 0 ,
43
- stack : HashMap :: from ( [ ( ( "__main__" . to_string ( ) , 0 ) , frame) ] ) ,
44
- type_env : HashMap :: new ( ) ,
45
- } ;
46
- }
47
-
48
- pub fn scope_key ( & self ) -> ( Name , i32 ) {
49
- return ( self . scope_name ( ) , self . recursion ) ;
50
- }
51
-
52
- pub fn scope_name ( & self ) -> Name {
53
- return self . scope . name . clone ( ) ;
54
- }
55
-
56
- pub fn scope_return ( & self ) -> Option < & A > {
57
- return self . search_frame ( self . scope_name ( ) ) ;
58
- }
59
-
60
- pub fn get_frame ( & self , key : ( Name , i32 ) ) -> & Frame < A > {
61
- return self . stack . get ( & key) . unwrap ( ) ;
62
- }
63
-
64
- pub fn search_frame ( & self , name : Name ) -> Option < & A > {
65
- return self
66
- . stack
67
- . get ( & self . scope_key ( ) )
68
- . unwrap ( )
69
- . variables
70
- . get ( & name) ;
71
- }
72
-
73
- pub fn insert_frame ( & mut self , func : Function ) -> ( ) {
74
- let new_frame: Frame < A > = Frame :: new ( Some ( self . scope . clone ( ) ) , Some ( self . scope_key ( ) ) ) ;
75
-
76
- self . stack
77
- . insert ( ( func. name . clone ( ) , self . scope_key ( ) . 1 + 1 ) , new_frame) ;
78
- self . scope = func;
79
- self . recursion += 1 ;
80
- }
81
-
82
- pub fn remove_frame ( & mut self ) -> ( ) {
83
- let recursion = self . scope_key ( ) . 1 - 1 ;
84
- self . scope = self
85
- . stack
86
- . remove ( & self . scope_key ( ) )
87
- . unwrap ( )
88
- . parent_function
89
- . unwrap ( ) ;
90
- self . recursion = recursion;
91
- }
92
-
93
- pub fn insert_variable ( & mut self , name : Name , kind : A ) -> ( ) {
94
- if let Some ( frame) = self . stack . get_mut ( & self . scope_key ( ) ) {
95
- frame. variables . insert ( name, kind) ;
96
- }
97
- }
98
-
99
- pub fn insert_type ( & mut self , name : Name , constructors : Vec < ValueConstructor > ) {
100
- self . type_env . insert ( name, constructors) ;
101
- }
102
-
103
- pub fn get_type ( & self , name : & Name ) -> Option < & Vec < ValueConstructor > > {
104
- self . type_env . get ( name)
105
- }
106
-
107
- pub fn insert_test ( & mut self , name : Name , test : Function ) -> ( ) {
108
- if let Some ( frame) = self . stack . get_mut ( & self . scope_key ( ) ) {
109
- frame. tests . insert ( name, test) ;
110
- }
111
- }
112
- }
113
-
114
3
#[ derive( Clone , Debug , PartialEq ) ]
115
4
pub struct Function {
116
5
pub name : Name ,
@@ -145,21 +34,6 @@ impl FormalArgument {
145
34
}
146
35
}
147
36
148
- #[ derive( Debug , PartialEq , Clone ) ]
149
- pub struct TestEnvironment < A > {
150
- pub name : Name ,
151
- pub env : Environment < A > ,
152
- }
153
-
154
- impl < A > TestEnvironment < A > {
155
- pub fn new ( ) -> TestEnvironment < A > {
156
- return TestEnvironment {
157
- name : "__test__" . to_string ( ) ,
158
- env : Environment :: < A > :: new ( ) ,
159
- } ;
160
- }
161
- }
162
-
163
37
#[ derive( Clone , Debug , PartialEq ) ]
164
38
pub enum Type {
165
39
TInteger ,
@@ -260,21 +134,4 @@ pub enum Statement {
260
134
FuncDef ( Function ) ,
261
135
Return ( Box < Expression > ) ,
262
136
TypeDeclaration ( Name , Vec < ValueConstructor > ) ,
263
- }
264
-
265
- #[ derive( Debug ) ]
266
- pub enum ParseError {
267
- IndentationError ( usize ) ,
268
- UnexpectedToken ( String ) ,
269
- InvalidExpression ( String ) ,
270
- }
271
-
272
- pub fn with_error_context < ' a , T > (
273
- parser : impl Fn ( & ' a str ) -> IResult < & ' a str , T > ,
274
- _context : & ' a str ,
275
- ) -> impl Fn ( & ' a str ) -> IResult < & ' a str , T > {
276
- move |input| {
277
- parser ( input)
278
- . map_err ( |_| nom:: Err :: Error ( nom:: error:: Error :: new ( input, nom:: error:: ErrorKind :: Tag ) ) )
279
- }
280
- }
137
+ }
0 commit comments