Skip to content

Commit

Permalink
refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 25, 2023
1 parent 38de040 commit 3029d38
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 106 deletions.
12 changes: 9 additions & 3 deletions components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl Compiler {
// there are two types of variables: global and local, they are compiled differently
// they are distinguished by the lexical scope depth
if self.scope_depth == 0 {
chunk.emit_byte(Code::GlobalVarDeclaration(name_str.to_string()))
chunk.emit_byte(Code::GlobalVarDeclaration {
name: name_str.to_string(),
})
} else {
let local = Local::new(name_str, self.scope_depth);
self.locals.push(local)
Expand Down Expand Up @@ -120,9 +122,13 @@ impl Compiler {
syntax::Expr::Variable(word) => {
let name = word.as_str(db);
if let Some(index) = self.resolve_local(name) {
chunk.emit_byte(Code::ReadLocalVariable(index))
chunk.emit_byte(Code::ReadLocalVariable {
index_in_stack: index,
})
} else {
chunk.emit_byte(Code::GlobalVariable(name.to_string()))
chunk.emit_byte(Code::ReadGlobalVariable {
name: name.to_string(),
})
}
}
syntax::Expr::Assign { name, value } => {
Expand Down
8 changes: 4 additions & 4 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,23 @@ impl VM {
// FIXME: This should be a call to a intrinsic function.
println!("{:?}", value);
}
bytecode::Code::GlobalVarDeclaration(name) => {
bytecode::Code::GlobalVarDeclaration { name } => {
let value = self.pop();
self.globals.insert(name, value);
}
bytecode::Code::Nil => {
self.push(Value::Nil);
}
bytecode::Code::GlobalVariable(name) => {
bytecode::Code::ReadGlobalVariable { name } => {
let value = self.globals.get(&name).expect("variable not found");
self.push(value.clone());
}
bytecode::Code::Assign(name) => {
let value = self.peek();
self.globals.insert(name, value.clone());
}
bytecode::Code::ReadLocalVariable(index) => {
let value = self.stack[index].clone();
bytecode::Code::ReadLocalVariable { index_in_stack } => {
let value = self.stack[index_in_stack].clone();
self.push(value);
}
bytecode::Code::Pop => {
Expand Down
12 changes: 9 additions & 3 deletions components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ pub enum Code {
LessEqual,
String(String),
Print,
GlobalVarDeclaration(String), // name of the variable
GlobalVariable(String), // name of the variable
ReadLocalVariable(usize), // index of the variable in the stack
GlobalVarDeclaration {
name: String, // name of the variable
},
ReadGlobalVariable {
name: String, // name of the variable
},
ReadLocalVariable {
index_in_stack: usize, // index of the variable in the stack
},
Nil,
Assign(String),
Pop,
Expand Down
42 changes: 21 additions & 21 deletions lox_tests/assignment/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Chunk {
1.0,
),
),
GlobalVarDeclaration(
"a",
),
GlobalVarDeclaration {
name: "a",
},
Constant(
F64(
2.0,
Expand All @@ -16,9 +16,9 @@ Chunk {
Assign(
"a",
),
GlobalVariable(
"a",
),
ReadGlobalVariable {
name: "a",
},
Print,
Constant(
F64(
Expand All @@ -29,25 +29,25 @@ Chunk {
"a",
),
Print,
GlobalVariable(
"a",
),
GlobalVarDeclaration(
"b",
),
ReadGlobalVariable {
name: "a",
},
GlobalVarDeclaration {
name: "b",
},
Nil,
GlobalVarDeclaration(
"c",
),
GlobalVariable(
"b",
),
GlobalVarDeclaration {
name: "c",
},
ReadGlobalVariable {
name: "b",
},
Assign(
"c",
),
GlobalVariable(
"c",
),
ReadGlobalVariable {
name: "c",
},
Print,
],
}
42 changes: 21 additions & 21 deletions lox_tests/assignment/execute
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ stack: [
),
]

execute: GlobalVarDeclaration(
"a",
)
execute: GlobalVarDeclaration {
name: "a",
}
stack: []

execute: Constant(
Expand All @@ -34,9 +34,9 @@ stack: [
),
]

execute: GlobalVariable(
"a",
)
execute: ReadGlobalVariable {
name: "a",
}
stack: [
Number(
2.0,
Expand Down Expand Up @@ -86,9 +86,9 @@ stack: [
),
]

execute: GlobalVariable(
"a",
)
execute: ReadGlobalVariable {
name: "a",
}
stack: [
Number(
2.0,
Expand All @@ -98,9 +98,9 @@ stack: [
),
]

execute: GlobalVarDeclaration(
"b",
)
execute: GlobalVarDeclaration {
name: "b",
}
stack: [
Number(
2.0,
Expand All @@ -115,18 +115,18 @@ stack: [
Nil,
]

execute: GlobalVarDeclaration(
"c",
)
execute: GlobalVarDeclaration {
name: "c",
}
stack: [
Number(
2.0,
),
]

execute: GlobalVariable(
"b",
)
execute: ReadGlobalVariable {
name: "b",
}
stack: [
Number(
2.0,
Expand All @@ -148,9 +148,9 @@ stack: [
),
]

execute: GlobalVariable(
"c",
)
execute: ReadGlobalVariable {
name: "c",
}
stack: [
Number(
2.0,
Expand Down
12 changes: 6 additions & 6 deletions lox_tests/local_var/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ Chunk {
2.0,
),
),
ReadLocalVariable(
1,
),
ReadLocalVariable {
index_in_stack: 1,
},
Print,
Pop,
ReadLocalVariable(
0,
),
ReadLocalVariable {
index_in_stack: 0,
},
Print,
Pop,
],
Expand Down
12 changes: 6 additions & 6 deletions lox_tests/local_var/execute
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ stack: [
),
]

execute: ReadLocalVariable(
1,
)
execute: ReadLocalVariable {
index_in_stack: 1,
}
stack: [
Number(
1.0,
Expand Down Expand Up @@ -55,9 +55,9 @@ stack: [
),
]

execute: ReadLocalVariable(
0,
)
execute: ReadLocalVariable {
index_in_stack: 0,
}
stack: [
Number(
1.0,
Expand Down
42 changes: 21 additions & 21 deletions lox_tests/var/bytecode
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ Chunk {
1.0,
),
),
GlobalVarDeclaration(
"a",
),
GlobalVarDeclaration {
name: "a",
},
Nil,
GlobalVarDeclaration(
"b",
),
GlobalVariable(
"a",
),
GlobalVarDeclaration {
name: "b",
},
ReadGlobalVariable {
name: "a",
},
Print,
String(
"hello",
),
GlobalVarDeclaration(
"str1",
),
GlobalVarDeclaration {
name: "str1",
},
String(
"world",
),
GlobalVarDeclaration(
"str2",
),
GlobalVariable(
"str1",
),
GlobalVariable(
"str2",
),
GlobalVarDeclaration {
name: "str2",
},
ReadGlobalVariable {
name: "str1",
},
ReadGlobalVariable {
name: "str2",
},
Add,
Print,
],
Expand Down
Loading

0 comments on commit 3029d38

Please sign in to comment.