Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/target
target
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[workspace]
members = ["preprocessor"]

[package]
name = "mcc"
version = "0.1.0"
edition = "2024"

[dependencies]
mcc-preprocessor = { path = "./preprocessor" }

anyhow = { version = "1.0.95", features = ["backtrace"] }
clap = { version = "4.5.23", features = ["derive"] }
console = "0.15.10"
Expand Down
7 changes: 7 additions & 0 deletions preprocessor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "mcc-preprocessor"
version = "0.1.0"
edition = "2024"

[dependencies]
197 changes: 197 additions & 0 deletions preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
use std::collections::BTreeMap;
use std::str::FromStr;

pub fn main(input: &str) -> String {
let mut defines: BTreeMap<&str, &str> = BTreeMap::new();
let mut cond_counter = CondCounter::default();

input
.lines()
.filter_map(|line| -> Option<String> {
match line.strip_prefix('#') {
// Line contains a preprocessor directive
Some(rest) => {
handle_directive(rest, &mut defines, &mut cond_counter);
None
}

// Line does not contain a preprocessor directive
None => {
if !cond_counter.currently_active() {
return None;
}

// TODO: This is horrible performance wise, right?
let mut line: String = line.to_owned();
for (define, replace_with) in &defines {
line = line.replace(define, replace_with);
}

Some(line)
}
}
})
.collect()
}

enum Directive {
// Macros
Define,
Undef,

// Conditionals
// If,
// Elif,
Else,
Endif,

// Macro conditionals
Ifdef,
Ifndef,
Elifdef,
Elifndef,

// Output
Error,
Warning,
}

impl FromStr for Directive {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"define" => Ok(Self::Define),
"undef" => Ok(Self::Undef),

// "if" => Ok(Self::If),
// "elif" => Ok(Self::Elif),
"else" => Ok(Self::Else),
"endif" => Ok(Self::Endif),

"ifdef" => Ok(Self::Ifdef),
"ifndef" => Ok(Self::Ifndef),
"elifdef" => Ok(Self::Elifdef),
"elifndef" => Ok(Self::Elifndef),

"error" => Ok(Self::Error),
"warning" => Ok(Self::Warning),

_ => Err(format!("unknown preprocessor directive: {s}")),
}
}
}

fn handle_directive<'a>(
rest: &'a str,
defines: &mut BTreeMap<&'a str, &'a str>,
cond_counter: &mut CondCounter,
) {
let (directive, rest) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));

// TODO: Handle parsing error
let directive: Directive = directive.parse().unwrap();

match directive {
Directive::Define => {
if cond_counter.currently_active() {
let (name, remains) = rest.split_once(char::is_whitespace).unwrap();
defines.insert(name, remains);
}
}
Directive::Undef => {
if cond_counter.currently_active() {
let name = rest;
assert!(!name.chars().any(char::is_whitespace));
defines.remove(name).unwrap();
}
}

Directive::Else => {
assert!(rest.is_empty());
cond_counter.else_if(|| true);
}
Directive::Endif => {
assert!(rest.is_empty());
cond_counter.pop().unwrap();
}

Directive::Ifdef => {
let name = rest;
assert!(!name.chars().any(char::is_whitespace));
cond_counter.push(defines.contains_key(name));
}
Directive::Ifndef => {
let name = rest;
assert!(!name.chars().any(char::is_whitespace));
cond_counter.push(!defines.contains_key(name));
}
Directive::Elifdef => {
let name = rest;
assert!(!name.chars().any(char::is_whitespace));
cond_counter.else_if(|| defines.contains_key(name));
}
Directive::Elifndef => {
let name = rest;
assert!(!name.chars().any(char::is_whitespace));
cond_counter.else_if(|| !defines.contains_key(name));
}

Directive::Error => {
if cond_counter.currently_active() {
panic!("{rest}");
}
}
Directive::Warning => {
if cond_counter.currently_active() {
eprintln!("{rest}");
}
}
}
}

use utils::*;
mod utils {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CondState {
Active,
Inactive { was_active: bool },
}

#[derive(Debug, Default)]
pub struct CondCounter(Vec<CondState>);

impl CondCounter {
pub fn push(&mut self, active: bool) {
self.0.push(match active {
true => CondState::Active,
false => CondState::Inactive { was_active: false },
});
}

pub fn currently_active(&self) -> bool {
self.0
.last()
.copied()
.map(|state| state == CondState::Active)
.unwrap_or(true)
}

// TODO: Not sure I like this name
pub fn else_if(&mut self, pred: impl Fn() -> bool) {
let current = self.0.last_mut().unwrap();
match *current {
CondState::Active => *current = CondState::Inactive { was_active: true },
CondState::Inactive { was_active: true } => { /* nothing to do */ }
CondState::Inactive { was_active: false } => {
if pred() {
*current = CondState::Active;
}
}
}
}

pub fn pop(&mut self) -> Option<CondState> {
self.0.pop()
}
}
}
35 changes: 35 additions & 0 deletions resources/with_macros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#define VALUE 3 * 2

int main(void) {
#ifdef PIZZA
return 10;
#endif

#ifndef VALUE
int a = 2;
#else
int a = 10;
#endif

#ifdef PIZZA
int b = 0;
#elifndef VALUE
int b = 1;
#elifdef VALUE
int b = 2;
#elifdef VALUE
int b = 3;
#else
int b = 4;
#endif

#ifdef BURGER
#warning "Burger is defined"
#elifndef PIZZA
#warning "PIZZA is not defined"
#else
#error "This should not happen"
#endif

return a * b * VALUE;
}
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ pub fn compiler(
anyhow!("failed to lex file")
})?;
if options.lex {
println!("{tokens:?}");
println!("{tokens:#?}");
return Ok(false);
}

Expand Down Expand Up @@ -221,3 +221,12 @@ pub fn compiler(

Ok(true)
}

pub fn preprocessor(input: &Path, output: &Path) -> Result<(), anyhow::Error> {
let input_content =
std::fs::read_to_string(input).context("failed to read input file for preprocessor")?;
let output_content = mcc_preprocessor::main(&input_content);
std::fs::write(output, output_content.as_bytes())
.context("failed to write preprocessor output to file")?;
Ok(())
}
Loading