use crate::prelude::*;
pub struct Preprocessor {
defines: FxHashMap<String, String>,
}
impl Default for Preprocessor {
fn default() -> Self {
Self::new()
}
}
impl Preprocessor {
fn new() -> Self {
Preprocessor {
defines: FxHashMap::default(),
}
}
fn process_line(&mut self, line: &str) -> String {
if line.trim_start().starts_with("#define") {
let parts: Vec<_> = line.split_whitespace().collect();
if parts.len() > 2 {
self.defines
.insert(parts[1].to_string(), parts[2..].join(" "));
}
String::new() } else {
self.expand_macros(line)
}
}
fn expand_macros(&self, line: &str) -> String {
let mut expanded_line = line.to_string();
for (key, value) in &self.defines {
expanded_line = expanded_line.replace(key, value);
}
expanded_line
}
pub fn process_module(&mut self, module: &str) -> String {
let mut processed_file = String::new();
for line in module.lines() {
processed_file.push_str(&self.process_line(line));
processed_file.push('\n');
}
processed_file
}
}