Skip to content

Commit

Permalink
supports compiling var decl
Browse files Browse the repository at this point in the history
  • Loading branch information
xffxff committed Sep 24, 2023
1 parent f8d9c64 commit 87a9f29
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
11 changes: 10 additions & 1 deletion components/lox-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ pub fn compile_file(db: &dyn crate::Db, input_file: InputFile) -> Chunk {
compile_expr(db, expr, &mut chunk);
chunk.emit_byte(Code::Print)
},
syntax::Stmt::Var { name, initializer } => todo!()
syntax::Stmt::Var { name, initializer } => {
if let Some(initializer) = initializer {
compile_expr(db, initializer, &mut chunk);
} else {
chunk.emit_byte(Code::Nil)
}

let name_str = name.as_str(db);
chunk.emit_byte(Code::VarDeclaration(name_str.to_string()))
}
}
}
chunk
Expand Down
2 changes: 2 additions & 0 deletions components/lox-execute/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ impl VM {
// FIXME: This should be a call to a intrinsic function.
println!("{:?}", value);
},
bytecode::Code::VarDeclaration(_) => todo!(),
bytecode::Code::Nil => todo!(),
}
if let Some(step_inspect) = &mut step_inspect {
step_inspect(instruction, self);
Expand Down
4 changes: 3 additions & 1 deletion components/lox-ir/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ pub enum Code {
Less,
LessEqual,
String(String),
Print
Print,
VarDeclaration(String), // name of the variable
Nil,
}

#[derive(PartialEq, Eq, Debug, Clone, Default)]
Expand Down
11 changes: 10 additions & 1 deletion lox_tests/var/bytecode
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Chunk {
code: [],
code: [
Constant(
F64(
1.0,
),
),
VarDeclaration(
"a",
),
],
}

0 comments on commit 87a9f29

Please sign in to comment.