Skip to content

Commit 942537a

Browse files
chapter17: type str and print (doesn't compile yet)
1 parent 8cdc767 commit 942537a

4 files changed

Lines changed: 99 additions & 52 deletions

File tree

src/sea_of_nodes/nodes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl Node {
436436
fn copy_f(self, sea: &mut Nodes) -> Option<Node> {
437437
let op = match &sea[self] {
438438
Op::Add => Op::AddF,
439-
Op::Bool(b) => todo!(),
439+
Op::Bool(_) => todo!(),
440440
Op::Div => Op::DivF,
441441
Op::Minus => return Some(sea.create((Op::MinusF, vec![None, None]))),
442442
Op::Mul => Op::MulF,

src/sea_of_nodes/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ impl<'a> Types<'a> {
133133
let int_one = interner.get_int(1, 1);
134134
let int_u1 = interner.get_int(0, 1);
135135

136-
let struct_top = interner.get_struct("$TOP", &[]);
137-
let struct_bot = interner.get_struct("$BOT", &[]);
136+
let struct_top = interner.get_struct("$TOP", Some(&[]));
137+
let struct_bot = interner.get_struct("$BOT", Some(&[]));
138138

139139
Self {
140140
bot,
@@ -279,7 +279,7 @@ impl<'a> Types<'a> {
279279

280280
pub fn make_init(&self, t: Ty<'a>) -> Option<Ty<'a>> {
281281
match *t {
282-
Type::Int(_) => Some(self.int_zero),
282+
Type::Int(_) => Some(*self.int_zero),
283283
Type::MemPtr(_) => Some(*self.ptr_null),
284284
_ => None,
285285
}

src/sea_of_nodes/types/type.rs

Lines changed: 89 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,67 @@ pub enum Type<'a> {
2222
impl<'t> Type<'t> {
2323
pub fn unwrap_int(&'t self) -> i64 {
2424
match self {
25-
Type::Int(Int::Constant(value)) => *value,
25+
Type::Int(i) if i.min == i.max => i.min,
2626
_ => unreachable!(),
2727
}
2828
}
2929

30-
// This is used by error messages, and is a shorted print.
30+
/// This is used by error messages, and is a shorted print.
3131
pub fn str(&self) -> Cow<str> {
3232
use Cow::*;
33-
match self {
34-
Type::Bot => Borrowed("Bot"),
35-
Type::Top => Borrowed("Top"),
36-
Type::Ctrl => Borrowed("Ctrl"),
37-
Type::XCtrl => Borrowed("~Ctrl"),
38-
Type::Int(i) => match i {
39-
Int::Bot => Borrowed("int"),
40-
Int::Top => Borrowed("~int"),
41-
Int::Constant(c) => Owned(c.to_string()),
33+
Borrowed(match self {
34+
Type::Bot => "Bot",
35+
Type::Top => "Top",
36+
Type::Ctrl => "Ctrl",
37+
Type::XCtrl => "~Ctrl",
38+
Type::Float(f) => match (f.sz, f.con()) {
39+
(-64, 0.0) => "~flt",
40+
(-32, 0.0) => "~f32",
41+
(32, 0.0) => "f32",
42+
(64, 0.0) => "flt",
43+
_ => return Owned(format!("{}{}", f.con(), if f.is_f32() { "f" } else { "" })),
4244
},
43-
Type::Tuple { .. } => Owned(self.to_string()),
44-
Type::Struct(s) => Borrowed(s.name()),
45+
Type::Int(i) => match (i.min, i.max) {
46+
(i64::MAX, i64::MIN) => "~int",
47+
(i64::MIN, i64::MAX) => "int",
48+
(0, 1) => "bool",
49+
(-128, 127) => "i8",
50+
(-32768, 32767) => "i16",
51+
(-2147483648, 2147483647) => "i32",
52+
(0, 255) => "u8",
53+
(0, 65535) => "u16",
54+
(0, 4294967295) => "u32",
55+
(min, max) => {
56+
return Owned(if min == max {
57+
format!("{}", min)
58+
} else {
59+
format!("[{}-{}]", min, max)
60+
})
61+
}
62+
},
63+
Type::Tuple(ts) => {
64+
let mut sb = "[ ".to_string();
65+
for t in *ts {
66+
sb.push_str(t.str().as_ref());
67+
sb.push_str(", ");
68+
}
69+
sb.pop();
70+
sb.pop();
71+
sb.push(']');
72+
return Owned(sb);
73+
}
74+
Type::Struct(s) => s.name,
4575
Type::MemPtr(p) => {
46-
if *p.to.data() == Struct::Top && p.nil {
47-
Borrowed("null")
48-
} else if *p.to.data() == Struct::Bot && !p.nil {
49-
Borrowed("*void")
76+
if p.nil && p.to.name() == "$TOP" && p.to.data().fields == Some(&[]) {
77+
"null"
78+
} else if !p.nil && p.to.name() == "$BOT" && p.to.data().fields == Some(&[]) {
79+
"*void"
5080
} else {
51-
Owned(format!("*{}{}", p.to.str(), if p.nil { "?" } else { "" }))
81+
return Owned(format!("*{}{}", p.to.str(), if p.nil { "?" } else { "" }));
5282
}
5383
}
54-
Type::Mem(_) => Owned(self.to_string()),
55-
}
84+
Type::Mem(_) => return Owned(self.to_string()),
85+
})
5686
}
5787
}
5888

@@ -63,41 +93,54 @@ impl<'t> Display for Type<'t> {
6393
Type::Top => "Top",
6494
Type::Ctrl => "Ctrl",
6595
Type::XCtrl => "~Ctrl",
66-
Type::Int(Int::Bot) => "IntBot",
67-
Type::Int(Int::Top) => "IntTop",
68-
Type::Int(Int::Constant(c)) => return write!(f, "{c}"),
96+
Type::Float(float) => match (float.sz, float.con()) {
97+
(-64, 0.0) => "FltTop",
98+
(-32, 0.0) => "F32Top",
99+
(32, 0.0) => "F32Bot",
100+
(64, 0.0) => "FltBot",
101+
(_, c) => return write!(f, "{}", c),
102+
},
103+
Type::Int(_) => return f.write_str(self.str().as_ref()),
69104
Type::Tuple(types) => {
70105
write!(f, "[")?;
71106
for (i, ty) in types.iter().enumerate() {
72107
if i > 0 {
73-
write!(f, ",")?;
108+
write!(f, ", {ty}")?;
109+
} else {
110+
write!(f, " {ty}")?;
74111
}
75-
write!(f, "{ty}")?;
76112
}
77113
return write!(f, "]");
78114
}
79-
Type::Struct(s) => match s {
80-
Struct::Bot => "$BOT",
81-
Struct::Top => "$TOP",
82-
Struct::Struct { name, fields } => {
83-
writeln!(f, "{name} {{")?;
84-
for (name, ty) in fields.iter() {
85-
writeln!(f, " {name}:{ty};")?;
115+
Type::Struct(s) => {
116+
write!(f, "{}", s.name)?;
117+
// Forward reference struct, just print the name
118+
if let Some(fs) = s.fields {
119+
write!(f, " {{")?;
120+
for field in fs {
121+
write!(f, " {} ", field.ty)?;
122+
if field.final_field {
123+
write!(f, "!")?;
124+
}
125+
write!(f, "{}; ", field.fname)?;
86126
}
87-
return write!(f, "}}");
127+
write!(f, "}}")?;
88128
}
89-
},
90-
Type::MemPtr(p) => match *p {
91-
MemPtr { to, nil: true } if *to.data() == Struct::Top => "null",
92-
MemPtr { to, nil: false } if *to.data() == Struct::Bot => "*void",
93-
MemPtr { to, nil } => {
94-
return write!(f, "*{to}{}", if nil { "?" } else { "" });
129+
return Ok(());
130+
}
131+
Type::MemPtr(p) => {
132+
if p.nil && p.to.name() == "$TOP" && p.to.data().fields == Some(&[]) {
133+
"null"
134+
} else if !p.nil && p.to.name() == "$BOT" && p.to.data().fields == Some(&[]) {
135+
"*void"
136+
} else {
137+
return write!(f, "*{}{}", p.to, if p.nil { "?" } else { "" });
95138
}
96-
},
97-
Type::Mem(m) => match m {
98-
Mem::Bot => "MEM#BOT",
99-
Mem::Top => "MEMTOP",
100-
Mem::Alias(a) => return write!(f, "MEM#{a}"),
139+
}
140+
Type::Mem(m) => match m.alias {
141+
0 => "MEM#TOP",
142+
u32::MAX => "MEM#BOT",
143+
_ => return write!(f, "MEM#{}:{}", m.alias, m.t),
101144
},
102145
})
103146
}
@@ -218,7 +261,7 @@ impl<'t> Ty<'t> {
218261
Type::Tuple(types) => {
219262
tys.get_tuple_from_slice(&types.iter().map(|t| t.dual(tys)).collect::<Vec<_>>())
220263
}
221-
Type::Struct(s) => *self.as_struct().dual(tys),
264+
Type::Struct(_) => *self.as_struct().dual(tys),
222265
Type::MemPtr(p) => *tys.get_mem_ptr(p.to.dual(tys), !p.nil),
223266
Type::Mem(m) => *tys.get_mem(m.alias, m.t.dual(tys)),
224267
}
@@ -265,7 +308,7 @@ impl<'t> Ty<'t> {
265308
Type::XCtrl => tys.top, // why?
266309
Type::Int(_) => *tys.int_top,
267310
Type::Float(_) => *tys.float_top,
268-
Type::Tuple(types) => tys.top, // why?
311+
Type::Tuple(_) => tys.top, // why?
269312
Type::Struct(_) => *self.as_struct().lub(tys),
270313
Type::MemPtr(MemPtr { to, .. }) => *tys.get_mem_ptr(to.lub(tys), false),
271314
Type::Mem(m) => *tys.get_mem(m.alias, m.t.lub(tys)),

src/sea_of_nodes/types/type_float.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ impl Float {
1919
pub fn con(&self) -> f64 {
2020
f64::from_bits(self.con)
2121
}
22+
23+
pub fn is_f32(&self) -> bool {
24+
let v = self.con();
25+
v as f32 as f64 == v
26+
}
2227
}
2328

2429
impl<'t> TyFloat<'t> {
2530
pub fn sz(self) -> i8 {
2631
self.data().sz
2732
}
2833
pub fn is_f32(&self) -> bool {
29-
let v = self.data().con();
30-
v as f32 as f64 == v
34+
self.data().is_f32()
3135
}
3236

3337
pub fn meet(self, that: TyFloat<'t>, tys: &Types<'t>) -> TyFloat<'t> {

0 commit comments

Comments
 (0)