Skip to content

Commit 8e99eef

Browse files
committed
Adding TOKAY_LOG-support
1 parent 7bed043 commit 8e99eef

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,40 @@ print(factorial(int(Number)))
197197
198198
The Tokay homepage [tokay.dev](https://tokay.dev) provides links to a quick start and documentation. The documentation source code is maintained in a [separate repository](https://github.com/tokay-lang/tokay-docs).
199199
200+
## Debugging
201+
202+
For debugging, there are two methods to use.
203+
204+
### Tracing using the `log`-crate
205+
206+
For Rust standard trace, use the [`env_logger` facilities](https://docs.rs/env_logger/latest/env_logger/). Full trace is only compiled into debug executables, the release version only provides warning level and upwards.
207+
208+
```
209+
$ RUST_LOG=tokay=debug tokay
210+
```
211+
212+
Alternatively, tracing can be activated for the `__main__`-program by setting `TOKAY_LOG`. This is used to start tracing when the internal parser has been compiled and executed already, and parsed the actual program. `TOKAY_LOG` can be set to any `RUST_LOG`-compliant format, as it becomes `RUST_LOG` right after.
213+
214+
```
215+
$ TOKAY_LOG=tokay=debug tokay
216+
```
217+
218+
### Built-in AST and VM debugger using `TOKAY_DEBUG` and `TOKAY_PARSER_DEBUG`
219+
220+
Set `TOKAY_DEBUG` to a debug level between 1-6. This can also be achieved using `tokay -dddd` where every `d` increments the debug level. Additionally, `TOKAY_INSPECT` can be set to one or a list of parselet name (-prefixes) which should be inspected in VM step-by-step trace (`TOKAY_DEBUG=6`).
221+
222+
| Level | Mode |
223+
| ----- | --------------------------------- |
224+
| 0 | No debug |
225+
| 1 | Print constructed AST |
226+
| 2 | Print final intermediate program |
227+
| 3 | Print compiled VM program |
228+
| 4 | Print VM execution trace |
229+
| 5 | Print VM stack contents |
230+
| 6 | VM opcode debugger |
231+
232+
`TOKAY_PARSER_DEBUG` sets the specific debug level for the parser, which is implemented in Tokay itself and is part of the compiler. Only levels > 2 can be recognized here, as the AST of the parser is built into the code.
233+
200234
## Logo
201235
202236
The Tokay programming language is named after the [Tokay gecko (Gekko gecko)](https://en.wikipedia.org/wiki/Tokay_gecko) from Asia, shouting out "token" in the night.

src/compiler/compiler.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::reader::*;
66
use crate::value;
77
use crate::value::RefValue;
88
use crate::vm::*;
9+
use env_logger;
910
use indexmap::{indexset, IndexMap, IndexSet};
1011
use log;
1112
use std::cell::RefCell;
@@ -184,6 +185,12 @@ impl Compiler {
184185
//println!("###\n{:#?}\n###", ast);
185186
}
186187

188+
// When TOKAY_LOG is set, set RUST_LOG to the setting *after* internal compilations
189+
if let Ok(log) = std::env::var("TOKAY_LOG") {
190+
std::env::set_var("RUST_LOG", log.clone());
191+
env_logger::init();
192+
}
193+
187194
self.compile_from_ast(&ast, None)
188195
}
189196

src/compiler/iml/imlvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl ImlValue {
380380
ops.push(Op::LoadFast(*addr))
381381
}
382382
}
383-
Self::Instance(instance) => {
383+
Self::Instance(_instance) => {
384384
todo!();
385385
}
386386
_ => unreachable!("{}", self),

src/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ fn repl(opts: &Opts) -> rustyline::Result<()> {
185185
}
186186

187187
fn main() -> rustyline::Result<()> {
188-
env_logger::init();
188+
// TOKAY_LOG setting has precedes over RUST_LOG setting.
189+
if std::env::var("TOKAY_LOG").is_err() {
190+
env_logger::init();
191+
}
189192

190193
// Handle command-line arguments from Opts.
191194
let opts = Opts::parse();

0 commit comments

Comments
 (0)