Skip to content

Commit b190455

Browse files
committed
Started logging via log crate
Introducting the log-crate to allow general debug and tracing in Tokay for better debugging and analysis.
1 parent 69252ac commit b190455

File tree

8 files changed

+150
-47
lines changed

8 files changed

+150
-47
lines changed

Cargo.lock

+69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ static_expression_evaluation = [] # Evaluates static expressions like 1+2+3 dur
2929

3030
[dependencies]
3131
charclass = "0.2" # use crates.io-version
32-
#charclass = { version = "0.2", path = "../charclass" } # use local version
32+
# charclass = { version = "0.2", path = "../charclass" } # use local version
3333
clap = { version = "4", features = ["derive"] }
34+
env_logger = "0"
3435
indexmap = "2"
36+
log = { version = "0", features = ["release_max_level_warn"] }
3537
num = "0.4"
3638
num-bigint = "0.4"
3739
num-parse = "0.1" # use crates.io-version
38-
#num-parse = { version = "0.1", path = "../num-parse" } # use local version
40+
# num-parse = { version = "0.1", path = "../num-parse" } # use local version
3941
rustyline = "13"
4042
tokay-macros = "0.4" # use crates.io-version
41-
#tokay-macros = { version = "0.4", path = "macros" } # use local version
43+
# tokay-macros = { version = "0.4", path = "macros" } # use local version

src/compiler/compiler.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::value;
77
use crate::value::RefValue;
88
use crate::vm::*;
99
use indexmap::{indexset, IndexMap, IndexSet};
10+
use log;
1011
use std::cell::RefCell;
1112

1213
/** Tokay compiler instance
@@ -76,14 +77,17 @@ impl Compiler {
7677
pub(super) fn compile_from_ast(
7778
&mut self,
7879
ast: &RefValue,
80+
name: Option<String>,
7981
) -> Result<Option<Program>, Vec<Error>> {
82+
log::trace!("compile_from_ast");
83+
8084
// Create main parselet from current main model
8185
let main_parselet = ImlParselet::new(ImlParseletInstance::new(
8286
// TODO: Keep backward compatible: copy Compiler's main model into the main_parselet
8387
Some(self.main.clone()),
8488
None,
8589
None,
86-
Some("__main__".to_string()),
90+
Some(name.unwrap_or("__main__".to_string())),
8791
5,
8892
false,
8993
));
@@ -142,8 +146,7 @@ impl Compiler {
142146
println!("--- Intermediate main ---\n{:#?}", main_parselet);
143147
}
144148

145-
let mut program = ImlProgram::new(ImlValue::from(main_parselet));
146-
program.debug = self.debug > 1;
149+
let program = ImlProgram::new(ImlValue::from(main_parselet));
147150

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

161164
/** Compile a Tokay program from a Reader source into the compiler. */
162165
pub fn compile(&mut self, reader: Reader) -> Result<Option<Program>, Vec<Error>> {
166+
log::trace!("compile");
167+
163168
// Create the Tokay parser when not already done
164169
if self.parser.is_none() {
165170
self.parser = Some(Parser::new());
@@ -179,7 +184,7 @@ impl Compiler {
179184
//println!("###\n{:#?}\n###", ast);
180185
}
181186

182-
self.compile_from_ast(&ast)
187+
self.compile_from_ast(&ast, None)
183188
}
184189

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

201207
if let Some(value) = statics.get(&value) {
208+
log::trace!("value already known");
202209
ImlValue::Value(value.clone())
203210
} else {
204211
statics.insert(value.clone());
212+
213+
log::trace!("value added to registry");
205214
ImlValue::Value(value)
206215
}
207216
}

src/compiler/iml/imlprogram.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ use crate::vm::Program;
66
use crate::Error;
77
use crate::{Object, RefValue};
88
use indexmap::{indexmap, IndexMap, IndexSet};
9+
use log;
910
use std::collections::{HashMap, HashSet};
1011

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

1819
impl ImlProgram {
1920
pub fn new(main: ImlValue) -> Self {
2021
ImlProgram {
22+
main: main.clone(),
2123
statics: indexmap!(main => None),
2224
errors: Vec::new(),
23-
debug: false,
2425
}
2526
}
2627

@@ -58,13 +59,21 @@ impl ImlProgram {
5859
and nullable parselet detection occurs.
5960
*/
6061
pub fn compile(mut self) -> Result<Program, Vec<Error>> {
62+
log::info!("{} compile", self.main);
63+
6164
let mut finalize = HashSet::new(); // list of consuming parselets required to be finalized
6265

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

6669
// self.statics grows inside of this while loop, therefore this condition.
6770
while idx < self.statics.len() {
71+
log::trace!(
72+
"idx = {: >3}, statics.len() = {: >3}",
73+
idx,
74+
self.statics.len()
75+
);
76+
6877
// Pick only intermediate parselets, other static values are directly moved
6978
let parselet = match self.statics.get_index_mut(idx).unwrap() {
7079
(_, Some(_)) => unreachable!(), // may not exist!
@@ -75,7 +84,7 @@ impl ImlProgram {
7584
}
7685
};
7786

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

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

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

102-
for (i, parselet) in finalize.iter().enumerate() {
103-
println!("{:?} => {:#?}", i, parselet);
104-
}
110+
for (i, parselet) in finalize.iter().enumerate() {
111+
log::trace!(" {: >3} => {:#?}", i, parselet);
105112
}
106113

107114
let leftrec = self.finalize(finalize);
@@ -127,11 +134,11 @@ impl ImlProgram {
127134
})
128135
.collect();
129136

130-
/*
137+
log::info!("{} has {} statics compiled", self.main, statics.len());
138+
131139
for (i, value) in statics.iter().enumerate() {
132-
println!("{} : {:?}", i, value.borrow());
140+
log::trace!(" {: >3} : {:#?}", i, value);
133141
}
134-
*/
135142

136143
Ok(Program::new(statics))
137144
}
@@ -370,17 +377,19 @@ impl ImlProgram {
370377
}
371378
}
372379

373-
/*
374-
println!("--- final config ---");
380+
log::info!(
381+
"{} has {} parselets finalized",
382+
self.statics.keys()[0],
383+
parselets.len()
384+
);
375385

376386
for parselet in &parselets {
377-
println!(
378-
"{} consuming={:?}",
387+
log::trace!(
388+
" {} consuming={:?}",
379389
parselet.borrow().name.as_deref().unwrap_or("(unnamed)"),
380390
configs[&parselet]
381391
);
382392
}
383-
*/
384393

385394
configs.into_iter().map(|(k, v)| (k, v.leftrec)).collect()
386395
}

0 commit comments

Comments
 (0)