Skip to content

Commit

Permalink
Started logging via log crate
Browse files Browse the repository at this point in the history
Introducting the log-crate to allow general debug and tracing in Tokay for better debugging and analysis.
  • Loading branch information
phorward committed Mar 22, 2024
1 parent 69252ac commit b190455
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 47 deletions.
69 changes: 69 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ static_expression_evaluation = [] # Evaluates static expressions like 1+2+3 dur

[dependencies]
charclass = "0.2" # use crates.io-version
#charclass = { version = "0.2", path = "../charclass" } # use local version
# charclass = { version = "0.2", path = "../charclass" } # use local version
clap = { version = "4", features = ["derive"] }
env_logger = "0"
indexmap = "2"
log = { version = "0", features = ["release_max_level_warn"] }
num = "0.4"
num-bigint = "0.4"
num-parse = "0.1" # use crates.io-version
#num-parse = { version = "0.1", path = "../num-parse" } # use local version
# num-parse = { version = "0.1", path = "../num-parse" } # use local version
rustyline = "13"
tokay-macros = "0.4" # use crates.io-version
#tokay-macros = { version = "0.4", path = "macros" } # use local version
# tokay-macros = { version = "0.4", path = "macros" } # use local version
17 changes: 13 additions & 4 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::value;
use crate::value::RefValue;
use crate::vm::*;
use indexmap::{indexset, IndexMap, IndexSet};
use log;
use std::cell::RefCell;

/** Tokay compiler instance
Expand Down Expand Up @@ -76,14 +77,17 @@ impl Compiler {
pub(super) fn compile_from_ast(
&mut self,
ast: &RefValue,
name: Option<String>,
) -> Result<Option<Program>, Vec<Error>> {
log::trace!("compile_from_ast");

// Create main parselet from current main model
let main_parselet = ImlParselet::new(ImlParseletInstance::new(
// TODO: Keep backward compatible: copy Compiler's main model into the main_parselet
Some(self.main.clone()),
None,
None,
Some("__main__".to_string()),
Some(name.unwrap_or("__main__".to_string())),
5,
false,
));
Expand Down Expand Up @@ -142,8 +146,7 @@ impl Compiler {
println!("--- Intermediate main ---\n{:#?}", main_parselet);
}

let mut program = ImlProgram::new(ImlValue::from(main_parselet));
program.debug = self.debug > 1;
let program = ImlProgram::new(ImlValue::from(main_parselet));

match program.compile() {
Ok(program) => {
Expand All @@ -160,6 +163,8 @@ impl Compiler {

/** Compile a Tokay program from a Reader source into the compiler. */
pub fn compile(&mut self, reader: Reader) -> Result<Option<Program>, Vec<Error>> {
log::trace!("compile");

// Create the Tokay parser when not already done
if self.parser.is_none() {
self.parser = Some(Parser::new());
Expand All @@ -179,7 +184,7 @@ impl Compiler {
//println!("###\n{:#?}\n###", ast);
}

self.compile_from_ast(&ast)
self.compile_from_ast(&ast, None)
}

/// Shortcut to compile a Tokay program from a &str into the compiler.
Expand All @@ -196,12 +201,16 @@ impl Compiler {
(althought they are different objects, but the same value)
*/
pub(super) fn register_static(&self, value: RefValue) -> ImlValue {
log::trace!("register_static value = {:?}", value);
let mut statics = self.statics.borrow_mut();

if let Some(value) = statics.get(&value) {
log::trace!("value already known");
ImlValue::Value(value.clone())
} else {
statics.insert(value.clone());

log::trace!("value added to registry");
ImlValue::Value(value)
}
}
Expand Down
41 changes: 25 additions & 16 deletions src/compiler/iml/imlprogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ use crate::vm::Program;
use crate::Error;
use crate::{Object, RefValue};
use indexmap::{indexmap, IndexMap, IndexSet};
use log;
use std::collections::{HashMap, HashSet};

#[derive(Debug)]
pub(in crate::compiler) struct ImlProgram {
main: ImlValue,
statics: IndexMap<ImlValue, Option<Parselet>>, // static values with optional final parselet replacement
pub errors: Vec<Error>, // errors collected during finalization (at least these are unresolved symbols)
pub debug: bool, // Debug
}

impl ImlProgram {
pub fn new(main: ImlValue) -> Self {
ImlProgram {
main: main.clone(),
statics: indexmap!(main => None),
errors: Vec::new(),
debug: false,
}
}

Expand Down Expand Up @@ -58,13 +59,21 @@ impl ImlProgram {
and nullable parselet detection occurs.
*/
pub fn compile(mut self) -> Result<Program, Vec<Error>> {
log::info!("{} compile", self.main);

let mut finalize = HashSet::new(); // list of consuming parselets required to be finalized

// Loop until end of statics is reached
let mut idx = 0;

// self.statics grows inside of this while loop, therefore this condition.
while idx < self.statics.len() {
log::trace!(
"idx = {: >3}, statics.len() = {: >3}",
idx,
self.statics.len()
);

// Pick only intermediate parselets, other static values are directly moved
let parselet = match self.statics.get_index_mut(idx).unwrap() {
(_, Some(_)) => unreachable!(), // may not exist!
Expand All @@ -75,7 +84,7 @@ impl ImlProgram {
}
};

// println!("\n::: {} {:?} :::\n", idx, parselet.borrow().name);
log::trace!("idx = {: >3}, parselet = {:?}", idx, parselet.borrow().name);

// Memoize parselets required to be finalized (needs a general rework later...)
if parselet.borrow().model.borrow().is_consuming {
Expand All @@ -96,12 +105,10 @@ impl ImlProgram {
}

// Finalize parselets
if self.debug {
println!("--- Parselets to finalize ---");
log::info!("{} has {} parselets to finalize", self.main, finalize.len());

for (i, parselet) in finalize.iter().enumerate() {
println!("{:?} => {:#?}", i, parselet);
}
for (i, parselet) in finalize.iter().enumerate() {
log::trace!(" {: >3} => {:#?}", i, parselet);
}

let leftrec = self.finalize(finalize);
Expand All @@ -127,11 +134,11 @@ impl ImlProgram {
})
.collect();

/*
log::info!("{} has {} statics compiled", self.main, statics.len());

for (i, value) in statics.iter().enumerate() {
println!("{} : {:?}", i, value.borrow());
log::trace!(" {: >3} : {:#?}", i, value);
}
*/

Ok(Program::new(statics))
}
Expand Down Expand Up @@ -370,17 +377,19 @@ impl ImlProgram {
}
}

/*
println!("--- final config ---");
log::info!(
"{} has {} parselets finalized",
self.statics.keys()[0],
parselets.len()
);

for parselet in &parselets {
println!(
"{} consuming={:?}",
log::trace!(
" {} consuming={:?}",
parselet.borrow().name.as_deref().unwrap_or("(unnamed)"),
configs[&parselet]
);
}
*/

configs.into_iter().map(|(k, v)| (k, v.leftrec)).collect()
}
Expand Down
Loading

0 comments on commit b190455

Please sign in to comment.