Skip to content

Commit

Permalink
Clone example context instead of re-interpreting
Browse files Browse the repository at this point in the history
  • Loading branch information
Bzero committed Sep 24, 2024
1 parent 42f2976 commit 17c2a9f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions numbat/examples/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ and — where sensible — units allow for [binary prefixes](https://en.wikipedi
}
}

fn inspect_functions_in_module(ctx: &Context, module: String) {
fn inspect_functions_in_module(ctx: &Context, prelude_ctx: &Context, module: String) {
for (fn_name, name, signature, description, url, examples, code_source) in ctx.functions() {
let CodeSource::Module(module_path, _) = code_source else {
unreachable!();
Expand Down Expand Up @@ -86,11 +86,7 @@ fn inspect_functions_in_module(ctx: &Context, module: String) {
println!();

for (example_code, example_description) in examples {
let mut example_ctx = prepare_context();
let _result = example_ctx
.interpret("use prelude", CodeSource::Internal)
.unwrap();

let mut example_ctx = prelude_ctx.clone();
let extra_import = if !example_ctx
.resolver()
.imported_modules
Expand Down Expand Up @@ -201,14 +197,19 @@ fn main() {
let mut ctx = prepare_context();
let _result = ctx.interpret("use all", CodeSource::Internal).unwrap();

let mut example_ctx = prepare_context();
let _result = example_ctx
.interpret("use prelude", CodeSource::Internal)
.unwrap();

let mut args = std::env::args();
args.next();
if let Some(arg) = args.next() {
match arg.as_str() {
"units" => inspect_units(&ctx),
"functions" => {
let module = args.next().unwrap();
inspect_functions_in_module(&ctx, module)
inspect_functions_in_module(&ctx, &example_ctx, module)
}
_ => eprintln!("USAGE: inspect [units|functions <module>]"),
}
Expand Down

0 comments on commit 17c2a9f

Please sign in to comment.