Skip to content

Commit f8bed88

Browse files
Flippchenmfloto
andcommitted
Reordered Types
Co-authored-by: mfloto <[email protected]>
1 parent 467db45 commit f8bed88

File tree

1 file changed

+120
-116
lines changed

1 file changed

+120
-116
lines changed

lib/src/types.rs

+120-116
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,24 @@ use crate::codegen::ConstantPool;
66
use serde::{Deserialize, Serialize};
77
use std::fmt::Display;
88

9+
/// All types necessary for the AST.
10+
11+
pub type Prg = Vec<Class>;
12+
913
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq)]
1014
pub struct Class {
1115
pub name: String,
1216
pub fields: Vec<FieldDecl>,
1317
pub methods: Vec<MethodDecl>,
1418
}
1519

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\tfields: {}\n\tmethods: {}\n}}",
29-
self.name, fields, methods
30-
)
31-
}
32-
}
33-
3420
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
3521
pub struct FieldDecl {
3622
pub field_type: Type,
3723
pub name: String,
3824
pub val: Option<Expr>,
3925
}
4026

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-
7027
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
7128
pub struct MethodDecl {
7229
pub ret_type: Type,
@@ -112,6 +69,122 @@ pub enum Expr {
11269
TypedExpr(Box<Expr>, Type),
11370
}
11471

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\tfields: {}\n\tmethods: {}\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+
115188
impl Expr {
116189
/// Gets the type if one is present
117190
pub(crate) fn get_type(&self) -> Option<Type> {
@@ -122,13 +195,6 @@ impl Expr {
122195
}
123196
}
124197

125-
#[derive(Debug, Clone, Deserialize, Serialize)]
126-
pub enum UnaryOp {
127-
Pos,
128-
Neg,
129-
Not,
130-
}
131-
132198
impl Display for UnaryOp {
133199
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134200
match self {
@@ -150,23 +216,6 @@ impl From<&str> for UnaryOp {
150216
}
151217
}
152218

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-
170219
impl Display for BinaryOp {
171220
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172221
match self {
@@ -228,48 +277,3 @@ impl BinaryOp {
228277
}
229278
}
230279
}
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

Comments
 (0)