Clean up main.rs, stdout, some compile warnings

This commit is contained in:
2025-06-22 15:03:59 -07:00
parent b65e188cb9
commit c29361214a
3 changed files with 9 additions and 41 deletions
-16
View File
@@ -86,22 +86,6 @@ pub fn parse_stat_labels(stat: &Stat) -> ParsedStat {
result result
} }
pub fn print_labels(b: &Stat) {
let code = &b.code;
let mut parser = ParseState::new(code);
assert!(
grammar::program(&mut parser),
"Couldn't parse code: {:?}",
code
);
for capture in parser.captures() {
if capture.kind() == Tag::Label {
println!("- {}", capture.text());
}
}
}
mod grammar { mod grammar {
use peg_macro::grammar; use peg_macro::grammar;
+3 -22
View File
@@ -5,7 +5,7 @@ mod preprocess;
mod world; mod world;
use anyhow::anyhow; use anyhow::anyhow;
use labels::{parse::print_labels, process::process_labels}; use labels::process::process_labels;
use preprocess::eval::Context; use preprocess::eval::Context;
use std::{env, error::Error, fs, path::PathBuf, process::exit}; use std::{env, error::Error, fs, path::PathBuf, process::exit};
use world::World; use world::World;
@@ -28,33 +28,14 @@ fn main() -> Result<(), Box<dyn Error>> {
.ok_or(anyhow!("Couldn't get world's directory"))?; .ok_or(anyhow!("Couldn't get world's directory"))?;
let eval_context = Context::new(&world_dir); let eval_context = Context::new(&world_dir);
println!("num boards: {}", &world.boards.len()); // Codegen: Evaluate all macros
for board in &mut world.boards { for board in &mut world.boards {
println!("board: {}", board.name);
for stat in &mut board.stats { for stat in &mut board.stats {
// Print some of the data parsed.
let (x, y) = (stat.x as usize, stat.y as usize);
let terrain = if x < 1 || x > 60 || y < 1 || y > 25 {
None
} else {
let terrain_index = (x - 1) + (y - 1) * 60;
Some(board.terrain[terrain_index])
};
println!(" stat at ({}, {}): {:?}, {:?}", x, y, terrain, stat.code);
// Evaluate macros
stat.code = eval_context.eval_program(&stat.code)?; stat.code = eval_context.eval_program(&stat.code)?;
} }
board.name.push_str(" (♪)");
}
for board in &world.boards {
println!("# Board: {}", board.name);
for stat in &board.stats {
print_labels(stat);
}
} }
// Resolve labels to proper ZZT-OOP
for mut board in &mut world.boards { for mut board in &mut world.boards {
process_labels(&mut board); process_labels(&mut board);
} }
+6 -3
View File
@@ -21,9 +21,6 @@ trait FileLoaderTrait {
struct FileLoader { struct FileLoader {
working_dir: PathBuf, working_dir: PathBuf,
} }
struct MockFileLoader {
content: String,
}
impl FileLoaderTrait for FileLoader { impl FileLoaderTrait for FileLoader {
fn load(&self, path: &Path) -> Result<String> { fn load(&self, path: &Path) -> Result<String> {
@@ -34,6 +31,12 @@ impl FileLoaderTrait for FileLoader {
} }
} }
#[cfg(test)]
struct MockFileLoader {
content: String,
}
#[cfg(test)]
impl FileLoaderTrait for MockFileLoader { impl FileLoaderTrait for MockFileLoader {
fn load(&self, _path: &Path) -> Result<String> { fn load(&self, _path: &Path) -> Result<String> {
Ok(self.content.clone()) Ok(self.content.clone())