Skip to content

Commit 0597d32

Browse files
committed
make the function node in bytecode more readable
1 parent 86c6cf4 commit 0597d32

File tree

20 files changed

+325
-621
lines changed

20 files changed

+325
-621
lines changed

components/lox-ir/src/bytecode.rs

+76-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#[derive(Clone, PartialEq, Eq, Debug)]
1+
use salsa::DebugWithDb;
2+
3+
#[derive(Clone, Debug, PartialEq, Eq)]
24
pub enum Code {
35
Return,
46
Constant(eq_float::F64),
@@ -43,11 +45,69 @@ pub enum Code {
4345
},
4446
}
4547

48+
impl<'db> DebugWithDb<dyn crate::Db + 'db> for Code {
49+
fn fmt(
50+
&self,
51+
f: &mut std::fmt::Formatter<'_>,
52+
db: &dyn crate::Db,
53+
_include_all_fields: bool,
54+
) -> std::fmt::Result {
55+
match self {
56+
Code::Return => write!(f, "return"),
57+
Code::Constant(c) => write!(f, "constant({})", c),
58+
Code::True => write!(f, "true"),
59+
Code::False => write!(f, "false"),
60+
Code::Add => write!(f, "add"),
61+
Code::Subtract => write!(f, "subtract"),
62+
Code::Multiply => write!(f, "multiply"),
63+
Code::Divide => write!(f, "divide"),
64+
Code::Negate => write!(f, "negate"),
65+
Code::Not => write!(f, "not"),
66+
Code::Equal => write!(f, "equal"),
67+
Code::NotEqual => write!(f, "not_equal"),
68+
Code::Greater => write!(f, "greater"),
69+
Code::GreaterEqual => write!(f, "greater_equal"),
70+
Code::Less => write!(f, "less"),
71+
Code::LessEqual => write!(f, "less_equal"),
72+
Code::String(s) => write!(f, "string({:?})", s),
73+
Code::Print => write!(f, "print"),
74+
Code::GlobalVarDeclaration { name } => write!(f, "global_var_declaration({})", name),
75+
Code::ReadGlobalVariable { name } => write!(f, "read_global_variable({})", name),
76+
Code::ReadLocalVariable { index_in_stack } => {
77+
write!(f, "read_local_variable({})", index_in_stack)
78+
}
79+
Code::Nil => write!(f, "nil"),
80+
Code::WriteGlobalVariable { name } => write!(f, "write_global_variable({})", name),
81+
Code::WriteLocalVariable { index_in_stack } => {
82+
write!(f, "write_local_variable({})", index_in_stack)
83+
}
84+
Code::Pop => write!(f, "pop"),
85+
Code::JumpIfFalse(ip) => write!(f, "jump_if_false({})", ip),
86+
Code::Jump(ip) => write!(f, "jump({})", ip),
87+
Code::Function(function) => write!(f, "function({:?})", &function.debug(db)),
88+
Code::Call { arity } => write!(f, "call({})", arity),
89+
}
90+
}
91+
}
92+
4693
#[derive(PartialEq, Eq, Debug, Clone, Default)]
4794
pub struct Chunk {
4895
code: Vec<Code>,
4996
}
5097

98+
impl<'db> DebugWithDb<dyn crate::Db + 'db> for Chunk {
99+
fn fmt(
100+
&self,
101+
f: &mut std::fmt::Formatter<'_>,
102+
db: &dyn crate::Db,
103+
_include_all_fields: bool,
104+
) -> std::fmt::Result {
105+
f.debug_list()
106+
.entries(self.code.iter().map(|c| c.debug(db)))
107+
.finish()
108+
}
109+
}
110+
51111
impl Chunk {
52112
pub fn emit_byte(&mut self, byte: Code) -> usize {
53113
tracing::debug!(?byte, "emitting byte");
@@ -72,9 +132,23 @@ impl Chunk {
72132
}
73133
}
74134

75-
#[derive(PartialEq, Eq, Debug, Clone, Default)]
135+
#[derive(PartialEq, Eq, Clone, Debug, Default)]
76136
pub struct CompiledFunction {
77137
pub name: String,
78138
pub arity: usize,
79139
pub chunk: Chunk,
80140
}
141+
142+
impl DebugWithDb<dyn crate::Db> for CompiledFunction {
143+
fn fmt(
144+
&self,
145+
f: &mut std::fmt::Formatter<'_>,
146+
db: &dyn crate::Db,
147+
_include_all_fields: bool,
148+
) -> std::fmt::Result {
149+
f.debug_struct("Function")
150+
.field("name", &self.name)
151+
.field("chunk", &self.chunk.debug(db))
152+
.finish()
153+
}
154+
}

lox_tests/and/bytecode

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
CompiledFunction {
1+
Function {
22
name: "main",
3-
arity: 0,
4-
chunk: Chunk {
5-
code: [
6-
False,
7-
JumpIfFalse(
8-
4,
9-
),
10-
Pop,
11-
True,
12-
Pop,
13-
],
14-
},
3+
chunk: [
4+
false,
5+
jump_if_false(4),
6+
pop,
7+
true,
8+
pop,
9+
],
1510
}

lox_tests/arithmetic/bytecode

+26-73
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,28 @@
1-
CompiledFunction {
1+
Function {
22
name: "main",
3-
arity: 0,
4-
chunk: Chunk {
5-
code: [
6-
Constant(
7-
F64(
8-
1.0,
9-
),
10-
),
11-
Constant(
12-
F64(
13-
2.0,
14-
),
15-
),
16-
Add,
17-
Pop,
18-
Constant(
19-
F64(
20-
1.0,
21-
),
22-
),
23-
Constant(
24-
F64(
25-
2.0,
26-
),
27-
),
28-
Constant(
29-
F64(
30-
3.0,
31-
),
32-
),
33-
Multiply,
34-
Add,
35-
Pop,
36-
Constant(
37-
F64(
38-
1.0,
39-
),
40-
),
41-
Constant(
42-
F64(
43-
2.0,
44-
),
45-
),
46-
Constant(
47-
F64(
48-
3.0,
49-
),
50-
),
51-
Divide,
52-
Subtract,
53-
Pop,
54-
Constant(
55-
F64(
56-
1.0,
57-
),
58-
),
59-
Constant(
60-
F64(
61-
2.0,
62-
),
63-
),
64-
Constant(
65-
F64(
66-
3.0,
67-
),
68-
),
69-
Negate,
70-
Multiply,
71-
Add,
72-
Pop,
73-
],
74-
},
3+
chunk: [
4+
constant(1),
5+
constant(2),
6+
add,
7+
pop,
8+
constant(1),
9+
constant(2),
10+
constant(3),
11+
multiply,
12+
add,
13+
pop,
14+
constant(1),
15+
constant(2),
16+
constant(3),
17+
divide,
18+
subtract,
19+
pop,
20+
constant(1),
21+
constant(2),
22+
constant(3),
23+
negate,
24+
multiply,
25+
add,
26+
pop,
27+
],
7528
}

lox_tests/assignment/bytecode

+22-57
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,24 @@
1-
CompiledFunction {
1+
Function {
22
name: "main",
3-
arity: 0,
4-
chunk: Chunk {
5-
code: [
6-
Constant(
7-
F64(
8-
1.0,
9-
),
10-
),
11-
GlobalVarDeclaration {
12-
name: "a",
13-
},
14-
Constant(
15-
F64(
16-
2.0,
17-
),
18-
),
19-
WriteGlobalVariable {
20-
name: "a",
21-
},
22-
Pop,
23-
ReadGlobalVariable {
24-
name: "a",
25-
},
26-
Print,
27-
Constant(
28-
F64(
29-
3.0,
30-
),
31-
),
32-
WriteGlobalVariable {
33-
name: "a",
34-
},
35-
Print,
36-
ReadGlobalVariable {
37-
name: "a",
38-
},
39-
GlobalVarDeclaration {
40-
name: "b",
41-
},
42-
Nil,
43-
GlobalVarDeclaration {
44-
name: "c",
45-
},
46-
ReadGlobalVariable {
47-
name: "b",
48-
},
49-
WriteGlobalVariable {
50-
name: "c",
51-
},
52-
Pop,
53-
ReadGlobalVariable {
54-
name: "c",
55-
},
56-
Print,
57-
],
58-
},
3+
chunk: [
4+
constant(1),
5+
global_var_declaration(a),
6+
constant(2),
7+
write_global_variable(a),
8+
pop,
9+
read_global_variable(a),
10+
print,
11+
constant(3),
12+
write_global_variable(a),
13+
print,
14+
read_global_variable(a),
15+
global_var_declaration(b),
16+
nil,
17+
global_var_declaration(c),
18+
read_global_variable(b),
19+
write_global_variable(c),
20+
pop,
21+
read_global_variable(c),
22+
print,
23+
],
5924
}

lox_tests/boolean/bytecode

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
CompiledFunction {
1+
Function {
22
name: "main",
3-
arity: 0,
4-
chunk: Chunk {
5-
code: [
6-
True,
7-
Pop,
8-
False,
9-
Pop,
10-
True,
11-
Not,
12-
Pop,
13-
False,
14-
Not,
15-
Pop,
16-
],
17-
},
3+
chunk: [
4+
true,
5+
pop,
6+
false,
7+
pop,
8+
true,
9+
not,
10+
pop,
11+
false,
12+
not,
13+
pop,
14+
],
1815
}

0 commit comments

Comments
 (0)