Shader packs, clean room 4: function-like macros and the standard macros

The macro engine the GLSL preprocessor needs, shared with the .properties
reader: function-like macros take their arguments (expanded first, raw
beside ##, which pastes) when a ( follows their name; a macro is still not
expanded inside its own expansion. Text is cut into preprocessing tokens,
so numbers such as 1e5 or 0x1F are never mistaken for names.

The #if/#elif/#else/#endif state moves out of the .properties reader so
the preprocessor uses the same code.

Standard macros: MC_GL_VERSION and MC_GLSL_VERSION 460, the 24
MC_RENDER_STAGE_* as ascending integers in draw order, render and shadow
quality 1.0, MC_HAND_DEPTH 0.125, MAX_COLOR_BUFFERS 16.

Nothing uses it yet: players see no change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-24 21:09:07 +02:00
parent 397d529db7
commit 4c65ed2f5c
4 changed files with 343 additions and 103 deletions
+104
View File
@@ -20,6 +20,110 @@ pub fn eval_condition(expr: &str, macros: &Macros) -> Result<i64, String> {
}
}
/// The text after a line's `#`, split into the directive word and the rest without its `//`
/// comment. `None` when the first word is followed by something other than white space, `//` or
/// the end of the line (`#ifdefault=1` is a comment, not `#ifdef`).
pub(crate) fn split_directive(after_hash: &str) -> Option<(&str, &str)> {
let line = after_hash.trim_start();
let len = line
.find(|c: char| !c.is_ascii_alphabetic())
.unwrap_or(line.len());
let (word, rest) = line.split_at(len);
let ok = rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with("//");
ok.then(|| (word, rest.split("//").next().unwrap_or("").trim()))
}
/// One level of `#if` ... `#endif`.
struct Branch {
/// This branch's lines are read.
on: bool,
/// A branch of this `#if` was already taken (the rest are skipped).
taken: bool,
/// The enclosing level is read.
outer: bool,
seen_else: bool,
}
/// The state of nested conditional directives.
#[derive(Default)]
pub(crate) struct Conditionals(Vec<Branch>);
impl Conditionals {
/// Lines here are read.
pub(crate) fn active(&self) -> bool {
self.0.last().is_none_or(|b| b.on)
}
/// An `#if` is still open.
pub(crate) fn open(&self) -> bool {
!self.0.is_empty()
}
/// Applies `#word rest` when it is a conditional directive (`Ok(false)` when it is not). A
/// condition that can't be evaluated reads as false and returns why.
pub(crate) fn apply(
&mut self,
word: &str,
rest: &str,
macros: &Macros,
) -> Result<bool, String> {
let on = self.active();
match word {
"if" | "ifdef" | "ifndef" => {
let value = if on {
test(word, rest, macros)
} else {
Ok(false)
};
let v = *value.as_ref().unwrap_or(&false);
self.0.push(Branch {
on: v,
taken: v,
outer: on,
seen_else: false,
});
value.map(|_| true)
}
"elif" | "else" => {
let b = self
.0
.last_mut()
.ok_or_else(|| format!("`#{word}` without `#if`"))?;
if b.seen_else {
return Err(format!("`#{word}` after `#else`"));
}
b.seen_else = word == "else";
let take = b.outer && !b.taken;
let value = match word {
"else" => Ok(take),
_ if take => test("if", rest, macros),
_ => Ok(false),
};
b.on = *value.as_ref().unwrap_or(&false);
b.taken |= b.on;
value.map(|_| true)
}
"endif" => self
.0
.pop()
.map(|_| true)
.ok_or_else(|| "`#endif` without `#if`".into()),
_ => Ok(false),
}
}
}
fn test(word: &str, rest: &str, macros: &Macros) -> Result<bool, String> {
let name = rest.split_whitespace().next().unwrap_or("");
match word {
"ifdef" => Ok(macros.is_defined(name)),
"ifndef" => Ok(!macros.is_defined(name)),
_ => eval_condition(rest, macros)
.map(|v| v != 0)
.map_err(|e| format!("`#{word} {rest}`: {e}; read as false")),
}
}
/// `defined X` and `defined(X)` become `1` or `0` before anything is expanded.
fn resolve_defined(expr: &str, macros: &Macros) -> Result<String, String> {
let mut out = String::with_capacity(expr.len());
+1 -1
View File
@@ -18,6 +18,6 @@ mod properties;
pub use condition::eval_condition;
pub use discover::{Found, discover};
pub use lang::Lang;
pub use macros::{MC_VERSION, Macros};
pub use macros::{MC_VERSION, Macros, RENDER_STAGES};
pub use pack::{Pack, PackError};
pub use properties::{Entry, Note, Properties};
+214 -28
View File
@@ -22,12 +22,48 @@ pub const fn mc_version(id: &str) -> u32 {
parts[0] * 10000 + parts[1] * 100 + parts[2]
}
#[derive(Clone, Debug, PartialEq)]
struct Macro {
/// `None` for an object-like macro; the parameter names of a function-like one.
params: Option<Vec<String>>,
body: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Macros(BTreeMap<String, String>);
pub struct Macros(BTreeMap<String, Macro>);
/// `MC_RENDER_STAGE_*` in the order the stages draw (spec 5.4); each is its index (OQ-7).
pub const RENDER_STAGES: [&str; 24] = [
"NONE",
"SKY",
"SUNSET",
"CUSTOM_SKY",
"SUN",
"MOON",
"STARS",
"VOID",
"TERRAIN_SOLID",
"TERRAIN_CUTOUT_MIPPED",
"TERRAIN_CUTOUT",
"ENTITIES",
"BLOCK_ENTITIES",
"DESTROY",
"OUTLINE",
"DEBUG",
"HAND_SOLID",
"TERRAIN_TRANSLUCENT",
"TRIPWIRE",
"PARTICLES",
"CLOUDS",
"RAIN_SNOW",
"WORLD_BORDER",
"HAND_TRANSLUCENT",
];
impl Macros {
/// The standard macros that need no renderer: `MC_VERSION` and this system's `MC_OS_*`.
/// The graphics ones arrive with the renderer.
/// The standard macros that need no renderer (spec 1.5.1): `MC_VERSION`, this system's
/// `MC_OS_*`, the GL and GLSL versions Horizon's translation accepts, the render stages and
/// the host settings at their defaults. The GPU vendor and family arrive with the renderer.
pub fn standard() -> Self {
let mut m = Self::default();
m.define("MC_VERSION", MC_VERSION.to_string());
@@ -38,11 +74,32 @@ impl Macros {
_ => "MC_OS_OTHER",
};
m.define(os, "");
m.define("MC_GL_VERSION", "460");
m.define("MC_GLSL_VERSION", "460");
for (i, stage) in RENDER_STAGES.iter().enumerate() {
m.define(format!("MC_RENDER_STAGE_{stage}"), i.to_string());
}
m.define("MC_RENDER_QUALITY", "1.0");
m.define("MC_SHADOW_QUALITY", "1.0");
m.define("MC_HAND_DEPTH", "0.125");
m.define("MAX_COLOR_BUFFERS", "16");
m
}
pub fn define(&mut self, name: impl Into<String>, value: impl Into<String>) {
self.0.insert(name.into(), value.into());
let body = value.into();
self.0.insert(name.into(), Macro { params: None, body });
}
pub fn define_function(&mut self, name: &str, params: Vec<String>, body: &str) {
let body = body.to_owned();
self.0.insert(
name.to_owned(),
Macro {
params: Some(params),
body,
},
);
}
pub fn undef(&mut self, name: &str) {
@@ -53,43 +110,158 @@ impl Macros {
self.0.contains_key(name)
}
/// The value of an object-like macro.
pub fn get(&self, name: &str) -> Option<&str> {
self.0.get(name).map(String::as_str)
self.0
.get(name)
.filter(|m| m.params.is_none())
.map(|m| m.body.as_str())
}
/// `text` with every identifier that names a macro replaced by its value, repeatedly; a macro
/// is not expanded again inside its own expansion, so `A` defined as `A + 1` stops.
/// `text` with every macro expanded, repeatedly; a macro is not expanded again inside its own
/// expansion, so `A` defined as `A + 1` stops. Function-like macros take their arguments
/// (expanded first, raw beside `##`, which pastes) when a `(` follows their name.
pub fn expand(&self, text: &str) -> String {
self.expand_hiding(text, &mut Vec::new())
}
fn expand_hiding<'a>(&'a self, text: &str, hidden: &mut Vec<&'a str>) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(start) = rest.find(is_ident_start) {
out.push_str(&rest[..start]);
// A digit run glued to letters (`1e5`, `0x1F`) is a number, not a name.
let glued = out
.chars()
.next_back()
.is_some_and(|c| c.is_ascii_alphanumeric() || c == '.');
let len = rest[start..]
.find(|c: char| !is_ident(c))
.unwrap_or(rest.len() - start);
let word = &rest[start..start + len];
match self.0.get_key_value(word) {
Some((name, value)) if !glued && !hidden.contains(&name.as_str()) => {
hidden.push(name);
out.push_str(&self.expand_hiding(value, hidden));
hidden.pop();
}
_ => out.push_str(word),
let tokens = lex(text);
let mut i = 0;
while i < tokens.len() {
let word = tokens[i];
i += 1;
let Some((name, m)) = self.0.get_key_value(word) else {
out.push_str(word);
continue;
};
if hidden.contains(&name.as_str()) {
out.push_str(word);
continue;
}
rest = &rest[start + len..];
let body = match &m.params {
None => m.body.clone(),
Some(params) => {
let Some((args, next)) = arguments(&tokens, i) else {
out.push_str(word);
continue;
};
i = next;
self.substitute(params, &args, &m.body, hidden)
}
};
hidden.push(name);
out.push_str(&self.expand_hiding(&body, hidden));
hidden.pop();
}
out.push_str(rest);
out
}
/// A function-like macro's body with its parameters replaced.
fn substitute<'a>(
&'a self,
params: &[String],
args: &[String],
body: &str,
hidden: &mut Vec<&'a str>,
) -> String {
let tokens = lex(body);
let pasted = |at: usize, step: isize| {
let mut j = at as isize + step;
while (0..tokens.len() as isize).contains(&j) && tokens[j as usize].trim().is_empty() {
j += step;
}
(0..tokens.len() as isize).contains(&j) && tokens[j as usize] == "##"
};
let mut out = String::new();
for (at, token) in tokens.iter().enumerate() {
match params.iter().position(|p| p == token) {
Some(k) => {
let arg = args.get(k).map_or("", |a| a.trim());
if pasted(at, -1) || pasted(at, 1) {
out.push_str(arg);
} else {
out.push_str(&self.expand_hiding(arg, hidden));
}
}
None => out.push_str(token),
}
}
// `a ## b` → `ab`.
let mut joined = String::with_capacity(out.len());
for (k, part) in out.split("##").enumerate() {
joined.push_str(if k == 0 { part.trim_end() } else { part.trim() });
}
if out.contains("##") { joined } else { out }
}
}
/// The arguments of a call whose `(` is the next non-blank token from `i`, split at top-level
/// commas, and the index after its `)`. `None` when no `(` follows or it never closes.
fn arguments(tokens: &[&str], mut i: usize) -> Option<(Vec<String>, usize)> {
while tokens.get(i)?.trim().is_empty() {
i += 1;
}
if tokens[i] != "(" {
return None;
}
let (mut depth, mut args, mut from) = (0, Vec::new(), i + 1);
for (j, t) in tokens.iter().enumerate().skip(i + 1) {
match *t {
"(" => depth += 1,
")" if depth == 0 => {
args.push(tokens[from..j].concat());
return Some((args, j + 1));
}
")" => depth -= 1,
"," if depth == 0 => {
args.push(tokens[from..j].concat());
from = j + 1;
}
_ => {}
}
}
None
}
/// Preprocessing tokens: identifiers, numbers (with their suffixes and exponents), runs of
/// white space, `##`, and single characters. Concatenated they give back `text`.
pub(crate) fn lex(text: &str) -> Vec<&str> {
let bytes = text.as_bytes();
let mut out = Vec::new();
let mut i = 0;
while i < bytes.len() {
let c = bytes[i] as char;
let start = i;
i += 1;
if is_ident_start(c) {
while i < bytes.len() && is_ident(bytes[i] as char) {
i += 1;
}
} else if c.is_ascii_digit() || (c == '.' && bytes.get(i).is_some_and(u8::is_ascii_digit)) {
while i < bytes.len() {
let d = bytes[i] as char;
let exp_sign = (d == '+' || d == '-') && matches!(bytes[i - 1], b'e' | b'E');
if !(is_ident(d) || d == '.' || exp_sign) {
break;
}
i += 1;
}
} else if c.is_whitespace() {
while i < bytes.len() && (bytes[i] as char).is_whitespace() {
i += 1;
}
} else if c == '#' && bytes.get(i) == Some(&b'#') {
i += 1;
} else {
while !text.is_char_boundary(i) {
i += 1;
}
}
out.push(&text[start..i]);
}
out
}
pub(crate) fn is_ident_start(c: char) -> bool {
@@ -128,4 +300,18 @@ mod tests {
m.define("e5", "x");
assert_eq!(m.expand("1e5"), "1e5");
}
#[test]
fn function_like_macros_take_arguments_and_paste() {
let mut m = Macros::default();
m.define("N", "4");
m.define_function("SQ", vec!["x".into()], "((x) * (x))");
m.define_function("CAT", vec!["a".into(), "b".into()], "a ## b");
m.define_function("PI", vec![], "3.14");
assert_eq!(m.expand("SQ(N + 1)"), "((4 + 1) * (4 + 1))");
assert_eq!(m.expand("SQ(min(N, 2))"), "((min(4, 2)) * (min(4, 2)))");
assert_eq!(m.expand("CAT(N, 2) CAT(col, ortex4)"), "N2 colortex4");
assert_eq!(m.expand("PI() SQ SQ(1"), "3.14 SQ SQ(1");
assert_eq!(m.get("SQ"), None);
}
}
+24 -74
View File
@@ -6,7 +6,8 @@
//! No other escape is read: values are names, numbers and expressions. A later duplicate key
//! replaces the earlier one, with a note.
use crate::{Macros, eval_condition};
use crate::Macros;
use crate::condition::{Conditionals, split_directive};
/// One `key=value`, with the line it starts on (from 1).
#[derive(Clone, Debug, PartialEq)]
@@ -30,29 +31,20 @@ pub struct Properties {
pub notes: Vec<Note>,
}
/// One branch level of `#if` ... `#endif`.
struct Branch {
/// This branch's lines are read.
on: bool,
/// A branch of this `#if` was already taken (the rest are skipped).
taken: bool,
/// The enclosing level is read.
outer: bool,
seen_else: bool,
}
impl Properties {
/// Reads `text`; `#define`/`#undef` change the macros for the lines after them only.
pub fn parse(text: &str, macros: &Macros) -> Self {
let mut out = Self::default();
let mut macros = macros.clone();
let mut branches: Vec<Branch> = Vec::new();
let mut conditionals = Conditionals::default();
let mut lines = text.lines().zip(1u32..);
while let Some((raw, number)) = lines.next() {
let line = raw.trim();
let on = branches.last().is_none_or(|b| b.on);
let on = conditionals.active();
if let Some(directive) = line.strip_prefix('#') {
out.directive(directive.trim_start(), number, &mut branches, &mut macros);
if let Some((word, rest)) = split_directive(directive) {
out.directive(word, rest, number, &mut conditionals, &mut macros);
}
continue;
}
// Continuation lines belong to their entry even inside a skipped branch.
@@ -76,7 +68,7 @@ impl Properties {
_ => out.note(number, "not a key=value line"),
}
}
if !branches.is_empty() {
if conditionals.open() {
out.note(text.lines().count() as u32, "`#if` without `#endif`");
}
out
@@ -111,72 +103,30 @@ impl Properties {
});
}
/// A line starting with `#`. It is a directive only when its first word is one; anything
/// else (`# text`, `#commented.key=1`, `#ifdefault`) is a comment.
/// A line starting with `#` whose first word may be a directive; any other word makes it a
/// comment.
fn directive(
&mut self,
line: &str,
word: &str,
rest: &str,
number: u32,
branches: &mut Vec<Branch>,
conditionals: &mut Conditionals,
macros: &mut Macros,
) {
let word_len = line
.find(|c: char| !c.is_ascii_alphabetic())
.unwrap_or(line.len());
let (word, rest) = line.split_at(word_len);
if !(rest.is_empty() || rest.starts_with(char::is_whitespace) || rest.starts_with("//")) {
return;
}
let rest = rest.split("//").next().unwrap_or("").trim();
let on = branches.last().is_none_or(|b| b.on);
match word {
"if" | "ifdef" | "ifndef" => {
let value = on && self.condition(word, rest, number, macros);
branches.push(Branch {
on: value,
taken: value,
outer: on,
seen_else: false,
});
}
"elif" | "else" => {
let Some(b) = branches.last_mut() else {
return self.note(number, &format!("`#{word}` without `#if`"));
};
if b.seen_else {
return self.note(number, &format!("`#{word}` after `#else`"));
match conditionals.apply(word, rest, macros) {
Err(e) => self.note(number, &e),
Ok(true) => {}
Ok(false) if conditionals.active() => match word {
"define" => {
let (name, value) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));
macros.define(name, value.trim());
}
let take = b.outer && !b.taken;
b.seen_else = word == "else";
b.on = take && (word == "else" || self.condition("if", rest, number, macros));
b.taken |= b.on;
}
"endif" => {
if branches.pop().is_none() {
self.note(number, "`#endif` without `#if`");
}
}
"define" if on => {
let (name, value) = rest.split_once(char::is_whitespace).unwrap_or((rest, ""));
macros.define(name, value.trim());
}
"undef" if on => macros.undef(rest),
_ => {}
"undef" => macros.undef(rest),
_ => {}
},
Ok(false) => {}
}
}
fn condition(&mut self, word: &str, rest: &str, number: u32, macros: &Macros) -> bool {
let name = rest.split_whitespace().next().unwrap_or("");
let result = match word {
"ifdef" => Ok(macros.is_defined(name) as i64),
"ifndef" => Ok(!macros.is_defined(name) as i64),
_ => eval_condition(rest, macros),
};
result.unwrap_or_else(|e| {
self.note(number, &format!("`#{word} {rest}`: {e}; read as false"));
0
}) != 0
}
}
/// An odd number of trailing backslashes: the last one joins the next line (`\\` is a literal).