Add if/ifx conditionals, count registers, and SIVP class compatibility

This commit is contained in:
gloamxun committed 2026-10-19 23:42:05 +08:00
1 parent c3ae426a10
commit 7f25075742
3 files changed
+227 -7

No files matched your search

+18
View File
@@ -50,6 +50,10 @@ pub(crate) struct TeXEngineState {
/// style files and document-level allocations from colliding.
#[serde(default)]
pub(crate) next_skip_register: usize,
#[serde(default)]
pub(crate) next_count_register: usize,
#[serde(default)]
pub(crate) next_dimen_register: usize,
#[serde(skip)]
pub(crate) inputs: InputStack,
}
@@ -85,6 +89,20 @@ impl TeXEngineState {
index
}
pub(crate) fn allocate_count_register(&mut self) -> usize {
const FIRST_ALLOCATED_COUNT: usize = 1_000;
let index = self.next_count_register.max(FIRST_ALLOCATED_COUNT);
self.next_count_register = index + 1;
index
}
pub(crate) fn allocate_dimen_register(&mut self) -> usize {
const FIRST_ALLOCATED_DIMEN: usize = 1_000;
let index = self.next_dimen_register.max(FIRST_ALLOCATED_DIMEN);
self.next_dimen_register = index + 1;
index
}
pub(crate) fn end_group(&mut self) {
self.semantic.end_group();
self.count_registers.end_group();
+30
View File
@@ -84,6 +84,24 @@ const PRELUDE: &str = r#"
\def\&{\texrraampersand}
\def\thetable{1}
\def\thefigure{1}
\def\arabic#1{0}
\def\roman#1{i}
\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}
\def\settowidth#1#2{}
\def\pagenumbering#1{}
\def\newtheorem#1#2{}
\def\theoremstyle#1{}
\def\frenchspacing{}
\def\nonfrenchspacing{}
\def\markboth#1#2{}
\def\markright#1{}
\def\settoheight#1#2{}
\def\settodepth#1#2{}
\def\LaTeX{LaTeX}
\def\TeX{TeX}
\def\\{\texrrarowbreak}
@@ -572,6 +590,18 @@ const PRELUDE: &str = r#"
\countdef\brokenpenalty=203
\countdef\footins=204
\countdef\predisplaypenalty=213
\countdef\@beginparpenalty=222
\countdef\@endparpenalty=223
\countdef\@itempenalty=224
\countdef\adjdemerits=225
\countdef\binoppenalty=226
\countdef\relpenalty=227
\countdef\floatingpenalty=228
\countdef\postdisplaypenalty=229
\countdef\linepenalty=230
\skipdef\cmidrulewidth=146
\skipdef\lightrulewidth=147
\skipdef\heavyrulewidth=148
\countdef\postdisplaypenalty=214
\countdef\doublehyphendemerits=215
\countdef\finalhyphendemerits=216
+179 -7
View File
@@ -16,7 +16,7 @@ use texlang::{
types::MuGlue,
vm::{self, TokenStream, implement_has_component},
};
use texlang_stdlib::script;
use texlang_stdlib::{conditional, conditional::Condition, script};
use texrra_core::{CatCode, MathCode};
use super::engine::arithmetic as engine_arithmetic;
@@ -3051,6 +3051,96 @@ fn newlength_primitive(
Ok(())
}
fn new_count_register_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let scope = register_alias_scope(input);
let name = read_unexpanded_argument(input, token, "expects a count name")?;
let name_tokens = name
.iter()
.copied()
.filter(|item| !matches!(item.value(), token::Value::Space(_)))
.collect::<Vec<_>>();
let name_tokens = match (name_tokens.first(), name_tokens.last()) {
(Some(first), Some(last))
if matches!(first.value(), token::Value::BeginGroup(_))
&& matches!(last.value(), token::Value::EndGroup(_)) =>
{
&name_tokens[1..name_tokens.len() - 1]
}
_ => name_tokens.as_slice(),
};
let [name] = name_tokens else {
return Err(input.fatal_error(error::SimpleTokenError::new(
token,
"\\newcount expects one control sequence",
)));
};
let token::Value::CommandRef(command_ref) = name.value() else {
return Err(input.fatal_error(error::SimpleTokenError::new(
token,
"\\newcount expects one control sequence",
)));
};
let index = input.state_mut().engine.allocate_count_register();
input.commands_map_mut().insert_variable_command(
command_ref,
texlang::variable::Command::new_array_with_external_grouping(
count_register_ref,
count_register_mut,
texlang::variable::IndexResolver::Static(index.into()),
),
scope,
);
Ok(())
}
fn new_dimen_register_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
) -> texlang::prelude::Result<()> {
let scope = register_alias_scope(input);
let name = read_unexpanded_argument(input, token, "expects a dimension name")?;
let name_tokens = name
.iter()
.copied()
.filter(|item| !matches!(item.value(), token::Value::Space(_)))
.collect::<Vec<_>>();
let name_tokens = match (name_tokens.first(), name_tokens.last()) {
(Some(first), Some(last))
if matches!(first.value(), token::Value::BeginGroup(_))
&& matches!(last.value(), token::Value::EndGroup(_)) =>
{
&name_tokens[1..name_tokens.len() - 1]
}
_ => name_tokens.as_slice(),
};
let [name] = name_tokens else {
return Err(input.fatal_error(error::SimpleTokenError::new(
token,
"\\newdimen expects one control sequence",
)));
};
let token::Value::CommandRef(command_ref) = name.value() else {
return Err(input.fatal_error(error::SimpleTokenError::new(
token,
"\\newdimen expects one control sequence",
)));
};
let index = input.state_mut().engine.allocate_dimen_register();
input.commands_map_mut().insert_variable_command(
command_ref,
texlang::variable::Command::new_array_with_external_grouping(
dimension_register_ref,
dimension_register_mut,
texlang::variable::IndexResolver::Static(index.into()),
),
scope,
);
Ok(())
}
fn environment_definition_primitive(
token: token::Token,
input: &mut vm::ExecutionInput<CompilerState>,
@@ -4876,6 +4966,63 @@ fn is_builtin_package(package: &str) -> bool {
| "doi"
| "authblk"
| "fancyhdr"
| "cuted"
| "stfloats"
| "float"
| "caption"
| "subcaption"
| "multirow"
| "array"
| "tabularx"
| "xcolor"
| "color"
| "graphics"
| "amsbsy"
| "bm"
| "mathrsfs"
| "upgreek"
| "textcomp"
| "newtxtext"
| "newtxmath"
| "mathptmx"
| "times"
| "helvet"
| "courier"
| "lineno"
| "setspace"
| "enumitem"
| "titlesec"
| "tocloft"
| "appendix"
| "etoolbox"
| "xparse"
| "iftex"
| "ifpdf"
| "ifthen"
| "calc"
| "dcolumn"
| "siunitx"
| "makecell"
| "threeparttable"
| "rotating"
| "sidecap"
| "wrapfig"
| "subfigure"
| "psfrag"
| "overpic"
| "amsthm"
| "mathtools"
| "thmtools"
| "ntheorem"
| "fix-cm"
| "lmodern"
| "mathpazo"
| "txfonts"
| "pxfonts"
| "fourier"
| "charter"
| "mathdesign"
| "concrete"
)
}
@@ -5060,12 +5207,7 @@ fn define_latex_command(
"command is already defined",
)));
}
LatexCommandDefinition::Renew if !already_defined => {
return Err(input.fatal_error(error::SimpleTokenError::new(
*name,
"command is not defined",
)));
}
LatexCommandDefinition::Renew if !already_defined => true,
LatexCommandDefinition::Provide if already_defined => false,
_ => true,
};
@@ -6833,11 +6975,28 @@ fn endinput_primitive(
Ok(())
}
struct IfTokens;
impl conditional::Condition<CompilerState> for IfTokens {
const DOC: Option<&'static str> = Some("Compare two tokens for equality");
fn evaluate(input: &mut vm::ExpansionInput<CompilerState>) -> texlang::prelude::Result<bool> {
let left = input.next()?;
let right = input.next()?;
match (left, right) {
(Some(left), Some(right)) => Ok(left.value() == right.value()),
_ => Ok(false),
}
}
}
fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<CompilerState>> {
let mut built_ins =
texlang_stdlib::built_in_commands_without_catcode_mathcode_and_endlinechar_and_registers::<
CompilerState,
>();
built_ins.insert("if", IfTokens::build_if_command());
built_ins.insert("ifx", IfTokens::build_if_command());
built_ins.insert(
"input",
texlang::command::BuiltIn::new_expansion(input_primitive),
@@ -7060,6 +7219,19 @@ fn built_in_commands() -> HashMap<&'static str, texlang::command::BuiltIn<Compil
texlang::command::BuiltIn::new_execution(newcounter_primitive)
.with_tag(texlang_stdlib::registers::countdef_tag()),
);
built_ins.insert(
"newcount",
texlang::command::BuiltIn::new_execution(new_count_register_primitive)
.with_tag(texlang_stdlib::registers::countdef_tag()),
);
built_ins.insert(
"newdimen",
texlang::command::BuiltIn::new_execution(new_dimen_register_primitive),
);
built_ins.insert(
"newskip",
texlang::command::BuiltIn::new_execution(newlength_primitive),
);
built_ins.insert(
"makebox",
texlang::command::BuiltIn::new_execution(makebox_primitive),