Skip to content

Commit

Permalink
Added parser tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
patbuc committed Nov 26, 2023
1 parent 2a95ff6 commit a5b534a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ edition = "2021"
[dependencies]
colored = "2.0.4"
lazy_static = "1.4.0"
tracing = "0.1.40"
tracing-attributes = { version = "0.1.27", features = [] }
tracing-subscriber = "0.3.18"

[features]
disassemble = []
5 changes: 4 additions & 1 deletion src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ impl Compiler {
return if parser.had_error {
None
} else {
parser.blocks.pop()
let block = parser.blocks.pop();
#[cfg(feature = "disassemble")]
block.as_ref()?.disassemble_block();
block
};
}
}
3 changes: 3 additions & 0 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ struct Token {
line: u32,
}

#[derive(Debug)]
struct Scanner {
source: Vec<char>,
start: usize,
current: usize,
line: u32,
}

#[derive(Debug)]
struct Parser {
scanner: Scanner,
blocks: Vec<Block>,
Expand All @@ -30,6 +32,7 @@ struct Parser {
panic_mode: bool,
}

#[derive(Debug)]
pub(crate) struct Compiler {
parser: Option<Parser>,
}
8 changes: 8 additions & 0 deletions src/compiler/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::compiler::{Parser, Scanner, Token};
use crate::vm::opcodes::OpCode;
use crate::vm::{Block, Value};
use rules::{ParseRule, Precedence};
use tracing_attributes::instrument;

mod rules;

Expand All @@ -19,6 +20,7 @@ impl Parser {
}
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
pub(in crate::compiler) fn advance(&mut self) {
std::mem::swap(&mut self.previous_token, &mut self.current_token);
loop {
Expand All @@ -30,10 +32,12 @@ impl Parser {
}
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
pub(in crate::compiler) fn expression(&mut self) {
self.parse_precedence(Precedence::Assignment);
}

#[cfg_attr(feature = "disassemble", instrument(skip(self, precedence)))]
fn parse_precedence(&mut self, precedence: Precedence) {
self.advance();
let prefix_rule = self.get_rule(self.previous_token.token_type.clone()).prefix;
Expand All @@ -54,16 +58,19 @@ impl Parser {
}
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
fn number(&mut self) {
let value = self.previous_token.token.parse::<f64>().unwrap();
self.emit_constant(value);
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
fn grouping(&mut self) {
self.expression();
self.consume(TokenType::RightParen, "Expect end of expression");
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
fn binary(&mut self) {
let operator_type = self.previous_token.token_type.clone();
let rule = self.get_rule(operator_type.clone());
Expand All @@ -78,6 +85,7 @@ impl Parser {
}
}

#[cfg_attr(feature = "disassemble", instrument(skip(self)))]
fn unary(&mut self) {
let operator_type = self.previous_token.token_type.clone();

Expand Down
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::vm::VirtualMachine;
fn main() {
print_tagline();

#[cfg(feature = "disassemble")]
setup_tracing();

let args: Vec<String> = env::args().collect();
if args.len() == 1 {
run_repl();
Expand All @@ -23,6 +26,16 @@ fn main() {
}
}

#[cfg(feature = "disassemble")]
fn setup_tracing() {
tracing_subscriber::fmt()
.with_span_events(
tracing_subscriber::fmt::format::FmtSpan::ENTER
| tracing_subscriber::fmt::format::FmtSpan::CLOSE,
)
.init()
}

fn print_tagline() {
println!(
"Hi, this is {} - a toy language you didn't wait for.",
Expand Down
2 changes: 1 addition & 1 deletion src/vm/block/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::vm::Block;
#[cfg(feature = "disassemble")]
impl Block {
#[allow(dead_code)]
pub(in crate::vm) fn disassemble_block(&self) {
pub(crate) fn disassemble_block(&self) {
println!();
println!("=== <{}> ===", self.name);

Expand Down
3 changes: 3 additions & 0 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ pub struct VirtualMachine {
stack: Vec<Value>,
}

#[derive(Debug)]
pub(crate) struct Block {
name: String,
constants: Constants,
instructions: Vec<u8>,
lines: Vec<Line>,
}

#[derive(Debug)]
struct Constants {
values: Vec<Value>,
}

#[derive(Debug)]
struct Line {
pub line: usize,
pub offset: usize,
Expand Down
13 changes: 9 additions & 4 deletions src/vm/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ impl VirtualMachine {
}

pub fn interpret(&mut self, source: String) -> Result {
self.reset();

let mut compiler = Compiler::new();
let block = compiler.compile(source);
return if let Some(block) = block {
Expand All @@ -20,11 +22,16 @@ impl VirtualMachine {
};
}

fn reset(&mut self) {
self.ip = 0;
self.stack.clear();
}

#[inline(always)]
fn run(&mut self, mut block: Block) -> Result {
loop {
#[cfg(feature = "disassemble")]
block.disassemble_instruction(self.ip);
// #[cfg(feature = "disassemble")]
// block.disassemble_instruction(self.ip);
match OpCode::from_u8(block.read_u8(self.ip)) {
OpCode::Return => {
let value = self.pop();
Expand Down Expand Up @@ -105,8 +112,6 @@ mod tests {
fn can_create_vm() {
let vm = super::VirtualMachine::new();
assert_eq!(0, vm.ip);
// assert_eq!(0, vm.block.instructions.len());
// assert_eq!(0, vm.block.constants.len());
assert_eq!(0, vm.stack.len());
}

Expand Down

0 comments on commit a5b534a

Please sign in to comment.