Skip to main content

scan_ops/
scan_ops.rs

1//! Walk the op tree from `PL_main_start` to `op_next == NULL`, printing
2//! each op's address and op-name.
3//!
4//! ```text
5//! cargo run --example 100_scan_ops -- -e 'print "FOO\n"'
6//! ```
7//!
8//! North-star example for Step 1 of the rebuild plan
9//! (`docs/plan/README.md` §4 Step 1.3): exercises the new `Perl` struct
10//! (NonNull-backed), the `PL_main_start!(my_perl)` macro generated by
11//! libperl-macrogen, and `PL_op_name` lookups.
12
13use std::env;
14use std::ffi::CStr;
15
16use libperl_rs::{op, Perl, PL_main_start, PL_op_name};
17
18fn scan_ops(mut op: *const op) {
19    while !op.is_null() {
20        let ty = unsafe { (*op).op_type() } as usize;
21        let name = unsafe { CStr::from_ptr(PL_op_name[ty]) }
22            .to_str()
23            .unwrap_or("<utf8 error>");
24        println!("{op:#?} {name}");
25        op = unsafe { (*op).op_next as *const op };
26    }
27}
28
29fn main() {
30    let mut perl = Perl::new();
31    perl.parse_env_args(env::args(), env::vars());
32
33    // Standard C-style THX setup at the top of the function:
34    let my_perl = perl.as_ptr();
35    scan_ops(PL_main_start!(my_perl));
36}