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