Support nested if-prefixed conditionals, char, and SIVP math/preamble commands

This commit is contained in:
gloamxun committed 2026-10-19 23:55:03 +08:00
1 parent 64f4446113
commit c1a7dedd6a
4 files changed
+149 -8

No files matched your search

+56 -4
View File
@@ -82,6 +82,7 @@ const PRELUDE: &str = r#"
\def\midrule{\texrratablerule}
\def\bottomrule{\texrratablerule}
\def\&{\texrraampersand}
\def\%{\char37}
\def\thetable{1}
\def\thefigure{1}
\def\arabic#1{0}
@@ -89,13 +90,20 @@ const PRELUDE: &str = r#"
\def\Roman#1{I}
\def\alph#1{a}
\def\Alph#1{A}
\def\@ifpackageloaded#1#2#3{#2}
\def\@ifclassloaded#1#2#3{#2}
\def\@ifundefined#1#2#3{#3}
\long\def\@ifpackageloaded#1#2#3{#2}
\long\def\@ifclassloaded#1#2#3{#2}
\long\def\@ifundefined#1#2#3{#3}
\def\settowidth#1#2{}
\def\pagenumbering#1{}
\def\newtheorem#1#2{}
\def\theoremstyle#1{}
\def\newtheoremstyle#1#2#3#4#5#6#7#8#9{}
\def\setcitestyle#1{}
\newskip\bibsep
\newskip\bibhang
\def\allowdisplaybreaks{}
\def\@ifstar#1#2{#2}
\long\def\g@addto@macro#1#2{}
\def\frenchspacing{}
\def\nonfrenchspacing{}
\def\markboth#1#2{}
@@ -130,6 +138,50 @@ const PRELUDE: &str = r#"
\def\substack#1{\begin{smallmatrix}#1\end{smallmatrix}}
\def\mathord#1{\texrramathclassstart{ordinary}#1\texrramathclassend}
\def\mathop#1{\texrramathclassstart{operator}#1\texrramathclassend}
\def\max{\mathop{\mathrm{max}}}
\def\resizebox#1#2#3{#3}
\def\scalebox#1#2{#2}
\def\rotatebox#1#2{#2}
\def\hsize{\textwidth}
\def\odot{\mathbin{\circ}}
\def\otimes{\mathbin{\times}}
\def\oplus{\mathbin{+}}
\def\ominus{\mathbin{-}}
\def\circ{\mathbin{\char176}}
\def\bullet{\mathbin{\char183}}
\def\ast{\mathbin{*}}
\def\star{\mathbin{*}}
\def\mid{\mathrel{|}}
\def\vert{|}
\def\Vert{\|}
\def\setminus{\mathbin{\backslash}}
\def\backslash{\char92}
\def\langle{\mathopen{\char60}}
\def\rangle{\mathclose{\char62}}
\def\lfloor{\mathopen{\char91}}
\def\rfloor{\mathclose{\char93}}
\def\lceil{\mathopen{\char123}}
\def\rceil{\mathclose{\char125}}
\def\min{\mathop{\mathrm{min}}}
\def\sup{\mathop{\mathrm{sup}}}
\def\inf{\mathop{\mathrm{inf}}}
\def\lim{\mathop{\mathrm{lim}}}
\def\det{\mathop{\mathrm{det}}}
\def\log{\mathop{\mathrm{log}}}
\def\ln{\mathop{\mathrm{ln}}}
\def\exp{\mathop{\mathrm{exp}}}
\def\sin{\mathop{\mathrm{sin}}}
\def\cos{\mathop{\mathrm{cos}}}
\def\tan{\mathop{\mathrm{tan}}}
\def\arg{\mathop{\mathrm{arg}}}
\def\deg{\mathop{\mathrm{deg}}}
\def\dim{\mathop{\mathrm{dim}}}
\def\ker{\mathop{\mathrm{ker}}}
\def\gcd{\mathop{\mathrm{gcd}}}
\def\hom{\mathop{\mathrm{hom}}}
\def\mathbin#1{\texrramathclassstart{binary}#1\texrramathclassend}
\def\mathrel#1{\texrramathclassstart{relation}#1\texrramathclassend}
\def\mathopen#1{\texrramathclassstart{opening}#1\texrramathclassend}
@@ -364,7 +416,7 @@ const PRELUDE: &str = r#"
\def\nobreakspace{ }
\def\ { }
\def~{ }
\def\AtBeginDocument#1{#1}
\long\def\AtBeginDocument#1{#1}
\def\newgeometry#1{\texrrageometry{#1}}
\def\fancyheadoffset#1{}
\def\lhead#1{\fancyhead[L]{#1}}
+35
View File
@@ -3006,6 +3006,25 @@ fn define_engine_macro(
);
}
fn char_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let code = i32::parse(input)?;
let character = u32::try_from(code)
.ok()
.and_then(char::from_u32)
.ok_or_else(|| {
input.fatal_error(error::SimpleTokenError::new(
token,
format!("\\char expects a valid character code, got {code}"),
))
})?;
let character_token = token::Token::new_other(character, token.trace_key());
input.as_mut().expansions_mut().push(character_token);
Ok(())
}
fn newlength_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -4977,6 +4996,18 @@ fn is_builtin_package(package: &str) -> bool {
| "xcolor"
| "color"
| "graphics"
| "manyfoot"
| "algorithm"
| "algorithmicx"
| "algpseudocode"
| "balance"
| "listings"
| "fix-cm"
| "apacite"
| "setspace"
| "threeparttable"
| "appendix"
| "vruler"
| "amsbsy"
| "bm"
| "mathrsfs"
@@ -7131,6 +7162,10 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
texlang_stdlib::input::get_read::<16, CompilerState>()
.with_tag(engine_prefix::global_assignment_tag()),
);
built_ins.insert(
"char",
texlang::command::BuiltIn::new_execution(char_primitive),
);
built_ins.insert(
"chardef",
texlang_stdlib::chardef::get_chardef::<CompilerState>()