Skip to content

Commit

Permalink
add feature debug_print_code and remove unwanted functions
Browse files Browse the repository at this point in the history
  • Loading branch information
binoyjayan committed Sep 19, 2023
1 parent 28d0eb2 commit 8d757f9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -9,5 +9,6 @@ lazy_static = "1.4.0"
byteorder = "1.4.3"

[features]
debug_print_code = []
debug_trace_execution = []
# default = ["debug_trace_execution"]
12 changes: 12 additions & 0 deletions src/code/definitions.rs
Expand Up @@ -163,6 +163,18 @@ impl Instructions {
}
}

// Disassemble instructions after compilation
#[allow(dead_code)]
pub fn disassemble(&self) {
println!(
"--------- Instructions [len: {:<4} --------------------",
self.len(),
);
print!("{}", self);
println!("------------------------------------------------------");
}

// Print current instructions during execution
#[allow(dead_code)]
pub fn print(&self, ip: usize) {
println!(
Expand Down
2 changes: 1 addition & 1 deletion src/common/builtins.rs
Expand Up @@ -139,7 +139,7 @@ fn builtin_str(args: Vec<Rc<Object>>) -> Result<Rc<Object>, String> {
}

fn builtin_time(args: Vec<Rc<Object>>) -> Result<Rc<Object>, String> {
if args.len() != 0 {
if !args.is_empty() {
return Err(String::from("'time' takes no argument(s)"));
}
let current_time = SystemTime::now();
Expand Down
56 changes: 37 additions & 19 deletions src/compiler/mod.rs
Expand Up @@ -105,12 +105,43 @@ impl Compiler {
}

pub fn bytecode(&self) -> Bytecode {
let instructions = self.get_curr_instructions();
let constants = self.constants.clone();
#[cfg(feature = "debug_print_code")]
{
instructions.disassemble();
self.print_constants();
}
Bytecode {
instructions: self.get_curr_instructions(),
constants: self.constants.clone(),
instructions,
constants,
}
}

#[allow(dead_code)]
fn print_constants(&self) {
let constants = self.constants.clone();
println!(
"----------- Constants [len: {:<4} ---------------------",
constants.len(),
);

for (i, obj) in constants.iter().enumerate() {
println!("[{}] {}", i, obj);
match obj {
Object::Clos(cl) => {
let closure = cl.clone();
closure.func.instructions.disassemble();
}
Object::CompiledFunc(func) => {
func.instructions.disassemble();
}
_ => {}
}
}
println!("------------------------------------------------------");
}

// Helper to add a constant to the constants pool
pub fn add_constant(&mut self, obj: Object) -> usize {
self.constants.push(obj);
Expand Down Expand Up @@ -217,28 +248,15 @@ impl Compiler {
}

fn compile_block_statement(&mut self, stmt: BlockStatement) -> Result<(), CompileError> {
self.compile_statements_nounwrap(stmt.statements)
for stmt in stmt.statements {
self.compile_statement(stmt)?;
}
Ok(())
}

fn compile_statements(&mut self, statements: Vec<Statement>) -> Result<(), CompileError> {
self.compile_statements_nounwrap(statements)
// if let Object::Return(retval) = result {
// return Ok(*retval);
// }
// Ok(result)
}

fn compile_statements_nounwrap(
&mut self,
statements: Vec<Statement>,
) -> Result<(), CompileError> {
// let mut result = Object::Nil;
for stmt in statements {
// let result =
self.compile_statement(stmt)?;
// if let Object::Return(_) = result {
// return Ok(result);
// }
}
Ok(())
}
Expand Down

0 comments on commit 8d757f9

Please sign in to comment.