| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 1 | //! rustnim — Rust -> Nim transpiler. |
| 2 | //! |
| 3 | //! Usage: rustnim <input.rs> [-o <output.nim>] |
| 4 | |
| 5 | mod fmt; |
| Lower `bitflags!` directly, checked against the real crate af6e50f nandithebull 13h ago | 6 | mod macros; |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 7 | mod lower; |
| Settle signed-shr and unsigned-wrap semantics against both compilers 87c9cc8 nandi 17h ago | 8 | mod ty; |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 9 | |
| 10 | use std::path::PathBuf; |
| 11 | use std::process::ExitCode; |
| 12 | |
| 13 | fn main() -> ExitCode { |
| 14 | match run() { |
| 15 | Ok(()) => ExitCode::SUCCESS, |
| 16 | Err(e) => { |
| 17 | eprintln!("rustnim: {e}"); |
| 18 | // Exiting non-zero on any failure is load-bearing: the entire |
| 19 | // point of this project is that a transpiler must never report |
| 20 | // success for work it did not do. |
| 21 | ExitCode::FAILURE |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn run() -> Result<(), String> { |
| 27 | let mut args = std::env::args_os().skip(1); |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 28 | let mut inputs: Vec<PathBuf> = Vec::new(); |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 29 | let mut output: Option<PathBuf> = None; |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 30 | let mut features: Vec<String> = Vec::new(); |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 31 | |
| 32 | while let Some(a) = args.next() { |
| 33 | match a.to_string_lossy().as_ref() { |
| 34 | "-o" | "--output" => { |
| 35 | output = Some( |
| 36 | args.next() |
| 37 | .ok_or("`-o` needs a path")? |
| 38 | .into(), |
| 39 | ); |
| 40 | } |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 41 | "--cfg" => { |
| 42 | let v = args.next().ok_or("`--cfg` needs an argument")?; |
| 43 | let v = v.to_string_lossy().into_owned(); |
| 44 | let f = v |
| 45 | .strip_prefix("feature=") |
| 46 | .ok_or("only `--cfg feature=<name>` is supported")?; |
| 47 | features.push(f.trim_matches('"').to_string()); |
| 48 | } |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 49 | "-h" | "--help" => { |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 50 | println!("usage: rustnim <input.rs>... [--cfg feature=<name>]... [-o <output.nim>]"); |
| 51 | println!(); |
| 52 | println!("Several inputs are concatenated into one Nim module, in the"); |
| 53 | println!("order given. That is how a multi-file crate is handled: Nim"); |
| 54 | println!("has no equivalent of Rust's per-file `mod`, so the items are"); |
| 55 | println!("flattened. Names must not collide across the files."); |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 56 | return Ok(()); |
| 57 | } |
| 58 | s if s.starts_with('-') => return Err(format!("unknown flag `{s}`")), |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 59 | _ => inputs.push(a.into()), |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 60 | } |
| 61 | } |
| 62 | |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 63 | if inputs.is_empty() { |
| 64 | return Err("no input file; usage: rustnim <input.rs>... [-o <output.nim>]".into()); |
| 65 | } |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 66 | |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 67 | // Several files become one Nim module: Rust's `mod` has no Nim analogue |
| 68 | // inside a single output file, so the items are flattened in argument |
| 69 | // order. A name collision between two files is a Nim compile error, which |
| 70 | // is a loud failure rather than a silently shadowed definition. |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 15h ago | 71 | // The first input is the crate root; each later one is a module named by |
| 72 | // its file stem. That is what keeps `lower::decode` and `mixed::decode` |
| 73 | // apart once everything is flattened into a single Nim module. |
| 74 | let mut files = Vec::new(); |
| 75 | for (i, input) in inputs.iter().enumerate() { |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 76 | let src = std::fs::read_to_string(input) |
| 77 | .map_err(|e| format!("cannot read {}: {e}", input.display()))?; |
| 78 | let parsed: syn::File = syn::parse_file(&src) |
| 79 | .map_err(|e| format!("{}: parse error: {e}", input.display()))?; |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 15h ago | 80 | let module = if i == 0 { |
| 81 | String::new() |
| 82 | } else { |
| 83 | input |
| 84 | .file_stem() |
| 85 | .map(|s| s.to_string_lossy().into_owned()) |
| 86 | .unwrap_or_default() |
| 87 | }; |
| 88 | files.push((module, parsed)); |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 89 | } |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 90 | |
| Add enums, Option/Result, `?`, and the machinery base16ct needs around them b0ccd80 nandithebull 16h ago | 91 | let mut lowerer = lower::Lowerer::new(); |
| 92 | lowerer.features = features; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 16h ago | 93 | // A `mod x;` is satisfied when x.rs is one of the inputs. |
| 94 | lowerer.modules = inputs |
| 95 | .iter() |
| 96 | .filter_map(|p| p.file_stem().map(|s| s.to_string_lossy().into_owned())) |
| 97 | .collect(); |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 15h ago | 98 | let nim = lowerer.lower_file(&files)?; |
| Add the differential test runner, and a lowering to measure with it 8ac32af nandithebull 17h ago | 99 | |
| 100 | match output { |
| 101 | Some(p) => std::fs::write(&p, nim) |
| 102 | .map_err(|e| format!("cannot write {}: {e}", p.display()))?, |
| 103 | None => print!("{nim}"), |
| 104 | } |
| 105 | Ok(()) |
| 106 | } |