@@ -6,67 +6,24 @@ use crate::codegen::ConstantPool;
6
6
use serde:: { Deserialize , Serialize } ;
7
7
use std:: fmt:: Display ;
8
8
9
+ /// All types necessary for the AST.
10
+
11
+ pub type Prg = Vec < Class > ;
12
+
9
13
#[ derive( Debug , Default , Clone , Deserialize , Serialize , PartialEq ) ]
10
14
pub struct Class {
11
15
pub name : String ,
12
16
pub fields : Vec < FieldDecl > ,
13
17
pub methods : Vec < MethodDecl > ,
14
18
}
15
19
16
- impl Display for Class {
17
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
18
- let mut fields = String :: new ( ) ;
19
- for field in & self . fields {
20
- fields. push_str ( & format ! ( "{}: {}, " , field. name, field. field_type) ) ;
21
- }
22
- let mut methods = String :: new ( ) ;
23
- for method in & self . methods {
24
- methods. push_str ( & format ! ( "{}: {}, " , method. name, method. ret_type) ) ;
25
- }
26
- write ! (
27
- f,
28
- "class {} {{\n \t fields: {}\n \t methods: {}\n }}" ,
29
- self . name, fields, methods
30
- )
31
- }
32
- }
33
-
34
20
#[ derive( Debug , Clone , Deserialize , Serialize , PartialEq ) ]
35
21
pub struct FieldDecl {
36
22
pub field_type : Type ,
37
23
pub name : String ,
38
24
pub val : Option < Expr > ,
39
25
}
40
26
41
- impl FieldDecl {
42
- /// See https://docs.oracle.com/javase/specs/jvms/se15/html/jvms-4.html#jvms-4.5
43
- pub fn as_bytes ( & self , class_name : & str , constant_pool : & mut ConstantPool ) -> Vec < u8 > {
44
- use crate :: codegen:: Constant ;
45
- use crate :: codegen:: FieldRef ;
46
- use crate :: codegen:: NameAndType ;
47
-
48
- let mut bytes = Vec :: new ( ) ;
49
- // No access modifier
50
- bytes. extend_from_slice ( & [ 0x0 , 0x0 ] ) ;
51
- // Name index
52
- bytes. extend_from_slice (
53
- & constant_pool
54
- . add ( Constant :: Utf8 ( self . name . clone ( ) ) )
55
- . to_be_bytes ( ) ,
56
- ) ;
57
- // Descripter index
58
- bytes. extend_from_slice (
59
- & constant_pool
60
- . add ( Constant :: Utf8 ( self . field_type . to_ir_string ( ) ) )
61
- . to_be_bytes ( ) ,
62
- ) ;
63
- // Attributes count
64
- bytes. extend_from_slice ( & [ 0x0 , 0x0 ] ) ;
65
- if let Some ( val) = & self . val { }
66
- bytes
67
- }
68
- }
69
-
70
27
#[ derive( Debug , Clone , Deserialize , Serialize , PartialEq , Eq ) ]
71
28
pub struct MethodDecl {
72
29
pub ret_type : Type ,
@@ -112,6 +69,122 @@ pub enum Expr {
112
69
TypedExpr ( Box < Expr > , Type ) ,
113
70
}
114
71
72
+ #[ derive( Debug , Clone , Deserialize , Serialize ) ]
73
+ pub enum UnaryOp {
74
+ Pos ,
75
+ Neg ,
76
+ Not ,
77
+ }
78
+
79
+ #[ derive( Debug , Clone , Deserialize , Serialize ) ]
80
+ pub enum BinaryOp {
81
+ Add ,
82
+ Sub ,
83
+ Mul ,
84
+ Div ,
85
+ Mod ,
86
+ And ,
87
+ Or ,
88
+ Le ,
89
+ Ge ,
90
+ Lt ,
91
+ Gt ,
92
+ Eq ,
93
+ Ne ,
94
+ }
95
+
96
+ #[ derive( Debug , Clone , Deserialize , Serialize , PartialEq , Hash , Eq ) ]
97
+ pub enum Type {
98
+ Int ,
99
+ Bool ,
100
+ Char ,
101
+ String ,
102
+ Void ,
103
+ Null ,
104
+ Class ( String ) ,
105
+ }
106
+
107
+ /// All necessary methods/implementations for the type system
108
+
109
+ impl Display for Type {
110
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
111
+ match self {
112
+ Type :: Int => write ! ( f, "int" ) ,
113
+ Type :: Bool => write ! ( f, "boolean" ) ,
114
+ Type :: Char => write ! ( f, "char" ) ,
115
+ Type :: String => write ! ( f, "String" ) ,
116
+ Type :: Void => write ! ( f, "void" ) ,
117
+ Type :: Null => write ! ( f, "null" ) ,
118
+ Type :: Class ( name) => write ! ( f, "{}" , name) ,
119
+ }
120
+ }
121
+ }
122
+
123
+ impl Type {
124
+ fn as_bytes ( & self ) -> Vec < u8 > {
125
+ self . to_ir_string ( ) . as_bytes ( ) . to_vec ( )
126
+ }
127
+ pub fn to_ir_string ( & self ) -> String {
128
+ match self {
129
+ Type :: Int => "I" ,
130
+ Type :: Char => "C" ,
131
+ Type :: Bool => "Z" ,
132
+ Type :: String => "Ljava/lang/String;" ,
133
+ Type :: Void => "V" ,
134
+ Type :: Class ( name) => name,
135
+ _ => panic ! ( "Invalid type: {}" , self ) ,
136
+ }
137
+ . to_string ( )
138
+ }
139
+ }
140
+
141
+ impl Display for Class {
142
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
143
+ let mut fields = String :: new ( ) ;
144
+ for field in & self . fields {
145
+ fields. push_str ( & format ! ( "{}: {}, " , field. name, field. field_type) ) ;
146
+ }
147
+ let mut methods = String :: new ( ) ;
148
+ for method in & self . methods {
149
+ methods. push_str ( & format ! ( "{}: {}, " , method. name, method. ret_type) ) ;
150
+ }
151
+ write ! (
152
+ f,
153
+ "class {} {{\n \t fields: {}\n \t methods: {}\n }}" ,
154
+ self . name, fields, methods
155
+ )
156
+ }
157
+ }
158
+
159
+ impl FieldDecl {
160
+ /// See https://docs.oracle.com/javase/specs/jvms/se15/html/jvms-4.html#jvms-4.5
161
+ pub fn as_bytes ( & self , class_name : & str , constant_pool : & mut ConstantPool ) -> Vec < u8 > {
162
+ use crate :: codegen:: Constant ;
163
+ use crate :: codegen:: FieldRef ;
164
+ use crate :: codegen:: NameAndType ;
165
+
166
+ let mut bytes = Vec :: new ( ) ;
167
+ // No access modifier
168
+ bytes. extend_from_slice ( & [ 0x0 , 0x0 ] ) ;
169
+ // Name index
170
+ bytes. extend_from_slice (
171
+ & constant_pool
172
+ . add ( Constant :: Utf8 ( self . name . clone ( ) ) )
173
+ . to_be_bytes ( ) ,
174
+ ) ;
175
+ // Descripter index
176
+ bytes. extend_from_slice (
177
+ & constant_pool
178
+ . add ( Constant :: Utf8 ( self . field_type . to_ir_string ( ) ) )
179
+ . to_be_bytes ( ) ,
180
+ ) ;
181
+ // Attributes count
182
+ bytes. extend_from_slice ( & [ 0x0 , 0x0 ] ) ;
183
+ if let Some ( val) = & self . val { }
184
+ bytes
185
+ }
186
+ }
187
+
115
188
impl Expr {
116
189
/// Gets the type if one is present
117
190
pub ( crate ) fn get_type ( & self ) -> Option < Type > {
@@ -122,13 +195,6 @@ impl Expr {
122
195
}
123
196
}
124
197
125
- #[ derive( Debug , Clone , Deserialize , Serialize ) ]
126
- pub enum UnaryOp {
127
- Pos ,
128
- Neg ,
129
- Not ,
130
- }
131
-
132
198
impl Display for UnaryOp {
133
199
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
134
200
match self {
@@ -150,23 +216,6 @@ impl From<&str> for UnaryOp {
150
216
}
151
217
}
152
218
153
- #[ derive( Debug , Clone , Deserialize , Serialize ) ]
154
- pub enum BinaryOp {
155
- Add ,
156
- Sub ,
157
- Mul ,
158
- Div ,
159
- Mod ,
160
- And ,
161
- Or ,
162
- Le ,
163
- Ge ,
164
- Lt ,
165
- Gt ,
166
- Eq ,
167
- Ne ,
168
- }
169
-
170
219
impl Display for BinaryOp {
171
220
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
172
221
match self {
@@ -228,48 +277,3 @@ impl BinaryOp {
228
277
}
229
278
}
230
279
}
231
-
232
- #[ derive( Debug , Clone , Deserialize , Serialize , PartialEq , Hash , Eq ) ]
233
- pub enum Type {
234
- Int ,
235
- Bool ,
236
- Char ,
237
- String ,
238
- Void ,
239
- Null ,
240
- Class ( String ) ,
241
- }
242
-
243
- impl Display for Type {
244
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
245
- match self {
246
- Type :: Int => write ! ( f, "int" ) ,
247
- Type :: Bool => write ! ( f, "boolean" ) ,
248
- Type :: Char => write ! ( f, "char" ) ,
249
- Type :: String => write ! ( f, "String" ) ,
250
- Type :: Void => write ! ( f, "void" ) ,
251
- Type :: Null => write ! ( f, "null" ) ,
252
- Type :: Class ( name) => write ! ( f, "{}" , name) ,
253
- }
254
- }
255
- }
256
-
257
- impl Type {
258
- fn as_bytes ( & self ) -> Vec < u8 > {
259
- self . to_ir_string ( ) . as_bytes ( ) . to_vec ( )
260
- }
261
- pub fn to_ir_string ( & self ) -> String {
262
- match self {
263
- Type :: Int => "I" ,
264
- Type :: Char => "C" ,
265
- Type :: Bool => "Z" ,
266
- Type :: String => "Ljava/lang/String;" ,
267
- Type :: Void => "V" ,
268
- Type :: Class ( name) => name,
269
- _ => panic ! ( "Invalid type: {}" , self ) ,
270
- }
271
- . to_string ( )
272
- }
273
- }
274
-
275
- pub type Prg = Vec < Class > ;
0 commit comments