1mod clause;
55mod component;
56mod exclusivity;
57pub mod lgx_lower;
58mod lgx_resolve;
59mod math;
60mod modal;
61mod nonstandard;
62mod noun;
63mod pragmatics;
64mod quantifier;
65mod question;
66mod verb;
67
68#[cfg(test)]
69#[path = "../../../logicaffeine_tests/unit/logicaffeine_language/parser/tests.rs"]
70mod tests;
71
72pub use clause::ClauseParsing;
73pub use modal::ModalParsing;
74pub use noun::NounParsing;
75pub use pragmatics::PragmaticsParsing;
76pub use quantifier::QuantifierParsing;
77pub use question::QuestionParsing;
78pub use verb::{ImperativeVerbParsing, LogicVerbParsing};
79
80use crate::analysis::TypeRegistry;
81use crate::arena_ctx::AstContext;
82use crate::ast::stmt::{Pattern, ReadSource, StmtId};
83use crate::ast::{
84 AspectOperator, BinaryOpKind, CompressionCodec, Expr, Literal, LogicExpr, MatchArm,
85 NeoEventData, NumberKind, QuantifierKind, SendLayout, Stmt, TemporalOperator, Term,
86 ThematicRole, TypeExpr,
87};
88use crate::drs::{Case, Gender, Number, ReferentSource};
89use crate::drs::{Drs, DrsMark, WorldState};
90use crate::error::{ParseError, ParseErrorKind};
91use crate::lexer::Lexer;
92use crate::lexicon::{self, Aspect, Definiteness, Time, VerbClass};
93use crate::optimization::{by_keyword, pin_from_str, Opt, OptimizationConfig, PinSet};
94use crate::token::{BlockType, FocusKind, Span, Token, TokenType};
95use logicaffeine_base::{Interner, Symbol, SymbolEq};
96use std::collections::HashSet;
97
98pub(super) type ParseResult<T> = Result<T, ParseError>;
99
100use std::ops::{Deref, DerefMut};
101
102#[derive(Debug, Clone, Default)]
109pub struct NestedStmtSpans(std::collections::HashMap<StmtId, Span>);
110
111impl NestedStmtSpans {
112 pub fn get(&self, stmt: StmtId) -> Option<Span> {
113 self.0.get(&stmt).copied()
114 }
115
116 pub fn len(&self) -> usize {
117 self.0.len()
118 }
119
120 pub fn is_empty(&self) -> bool {
121 self.0.is_empty()
122 }
123}
124
125#[derive(Default)]
130pub(super) struct StmtList<'a> {
131 stmts: Vec<Stmt<'a>>,
132 spans: Vec<Option<Span>>,
133}
134
135impl<'a> StmtList<'a> {
136 pub(super) fn read(&mut self, stmt: Stmt<'a>, span: Span) {
138 self.carry(stmt, Some(span));
139 }
140
141 pub(super) fn wrote(&mut self, stmt: Stmt<'a>) {
143 self.carry(stmt, None);
144 }
145
146 pub(super) fn carry(&mut self, stmt: Stmt<'a>, span: Option<Span>) {
148 self.stmts.push(stmt);
149 self.spans.push(span);
150 }
151
152 pub(super) fn append(&mut self, rest: StmtList<'a>) {
154 self.stmts.extend(rest.stmts);
155 self.spans.extend(rest.spans);
156 }
157
158 pub(super) fn last(&self) -> Option<&Stmt<'a>> {
159 self.stmts.last()
160 }
161}
162
163impl<'a> IntoIterator for StmtList<'a> {
164 type Item = (Stmt<'a>, Option<Span>);
165 type IntoIter = std::iter::Zip<std::vec::IntoIter<Stmt<'a>>, std::vec::IntoIter<Option<Span>>>;
166
167 fn into_iter(self) -> Self::IntoIter {
168 self.stmts.into_iter().zip(self.spans)
169 }
170}
171
172#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
174pub enum ParserMode {
175 #[default]
177 Declarative,
178 Imperative,
180}
181
182#[derive(Debug, Clone, Copy)]
184pub(super) enum CopulaTemporal {
185 Always,
186 Never,
187 Eventually,
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
195pub enum NegativeScopeMode {
196 #[default]
199 Narrow,
200 Wide,
203}
204
205#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
213pub enum ModalPreference {
214 #[default]
216 Default,
217 Epistemic,
219 Deontic,
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub(super) enum IndefiniteScope {
230 Negated,
233 Antecedent,
238 Local,
240}
241
242#[derive(Debug, Clone, Copy)]
244pub enum ResolvedPronoun {
245 Variable(Symbol),
247 Constant(Symbol),
249}
250
251#[derive(Clone)]
256struct Backtrack {
257 pos: usize,
258 drs: DrsMark,
259}
260
261#[derive(Clone)]
262struct ParserCheckpoint {
263 pos: usize,
264 var_counter: usize,
265 bindings_len: usize,
266 island: u32,
267 time: Option<Time>,
268 negative_depth: u32,
269 drs: DrsMark,
270}
271
272pub struct ParserGuard<'p, 'a, 'ctx, 'int> {
305 parser: &'p mut Parser<'a, 'ctx, 'int>,
306 checkpoint: ParserCheckpoint,
307 committed: bool,
308}
309
310impl<'p, 'a, 'ctx, 'int> ParserGuard<'p, 'a, 'ctx, 'int> {
311 pub fn commit(mut self) {
313 self.committed = true;
314 }
315}
316
317impl<'p, 'a, 'ctx, 'int> Drop for ParserGuard<'p, 'a, 'ctx, 'int> {
318 fn drop(&mut self) {
319 if !self.committed {
320 self.parser.restore(self.checkpoint.clone());
321 }
322 }
323}
324
325impl<'p, 'a, 'ctx, 'int> Deref for ParserGuard<'p, 'a, 'ctx, 'int> {
326 type Target = Parser<'a, 'ctx, 'int>;
327 fn deref(&self) -> &Self::Target {
328 self.parser
329 }
330}
331
332impl<'p, 'a, 'ctx, 'int> DerefMut for ParserGuard<'p, 'a, 'ctx, 'int> {
333 fn deref_mut(&mut self) -> &mut Self::Target {
334 self.parser
335 }
336}
337
338#[derive(Clone, Debug)]
343pub struct EventTemplate<'a> {
344 pub verb: Symbol,
346 pub agent: Option<Term<'a>>,
349 pub non_agent_roles: Vec<(ThematicRole, Term<'a>)>,
351 pub modifiers: Vec<Symbol>,
353}
354
355#[derive(Clone, Copy, Debug, PartialEq)]
358enum MoeAct {
359 Swiglu,
360 ClampedSwiglu { limit: f64, slope: f64 },
361}
362
363#[derive(Clone, Copy, Debug, PartialEq, Eq)]
365enum MoeScore {
366 Softmax,
367 Sigmoid,
368}
369
370#[derive(Clone, Copy, Debug)]
376struct MoeOptions {
377 router_bias: Option<Symbol>,
378 biases: [Option<Symbol>; 3],
379 score: MoeScore,
380 selection_bias: Option<Symbol>,
381 groups: Option<(i64, i64)>,
382 renormalize: bool,
383 scale: Option<f64>,
384 weigh_inputs: bool,
385 act: MoeAct,
386}
387
388#[derive(Clone, Copy, Debug)]
391struct AttentionShape {
392 h: i64,
393 kv: i64,
394 hd: i64,
395 vd: i64,
396 l: i64,
397 rows: i64,
398}
399
400#[derive(Clone, Copy, Debug)]
403enum Window {
404 Sliding(i64),
405 Chunked(i64),
406}
407
408#[derive(Clone, Copy, Debug)]
411struct AttentionOptions {
412 window: Option<Window>,
413 scale: Option<f64>,
414 cap: Option<f64>,
415 sinks: Option<Symbol>,
416}
417
418#[derive(Clone, Copy)]
436enum TensorElemMap {
437 Requant { m: i64, s: i64 },
439 Clamp { lo: i64, hi: i64 },
441 Relu,
443}
444
445#[derive(Clone, Copy)]
448enum TensorBinop {
449 Matmul,
450 Add,
451 Hadamard,
452}
453
454impl TensorBinop {
455 fn word(self) -> &'static str {
456 match self {
457 TensorBinop::Matmul => "matmul",
458 TensorBinop::Add => "plus",
459 TensorBinop::Hadamard => "hadamard",
460 }
461 }
462}
463
464#[derive(Clone, Copy)]
466enum ReductionKind {
467 Softmax,
468 Rmsnorm,
469 Layernorm,
470}
471
472impl ReductionKind {
473 fn word(self) -> &'static str {
474 match self {
475 ReductionKind::Softmax => "softmax",
476 ReductionKind::Rmsnorm => "rmsnorm",
477 ReductionKind::Layernorm => "layernorm",
478 }
479 }
480}
481
482enum ConstructionTarget {
485 Variant { enum_name: Symbol, variant: Symbol },
488 Type(Symbol),
491}
492
493#[derive(Clone, Debug)]
495pub(super) struct MountSite {
496 pub(super) name: Symbol,
497 pub(super) span: Span,
499 pub(super) is_root: bool,
502 pub(super) target: Option<String>,
504}
505
506pub struct Parser<'a, 'ctx, 'int> {
507 pub(super) tokens: Vec<Token>,
509 pub(super) current: usize,
511 pub(super) program_opt_flags: OptimizationConfig,
515 pub(super) program_tier_pins: PinSet,
519 pub(super) required_opts: Vec<Opt>,
523 pub(super) var_counter: usize,
525 pub(super) user_bound: std::collections::HashSet<Symbol>,
530 pub(super) tensor_shapes: std::collections::HashMap<Symbol, (i64, i64)>,
535 pub(super) tensor_int: std::collections::HashSet<Symbol>,
539 pub(super) components: std::collections::HashSet<Symbol>,
544 pub(super) declared_function_names: std::collections::HashSet<Symbol>,
550 pub(super) directives: std::collections::HashSet<Symbol>,
553 pub(super) lgx_components: std::collections::HashSet<Symbol>,
557 pub(super) lgx_stores: std::collections::HashMap<Symbol, Vec<Symbol>>,
560 pub(super) session_declared: bool,
563 pub(super) lgx_pending_key: Option<&'a Expr<'a>>,
576 pub(super) lgx_pending_reuse: Option<Symbol>,
583 pub(super) lgx_state_types: Vec<(Symbol, Option<Symbol>)>,
589 pub(super) lgx_fetch_fails: Option<(usize, Symbol, Symbol)>,
592 pub(super) lgx_sweeps: Vec<(Symbol, usize)>,
596 pub(super) lgx_timers: Vec<(Symbol, usize, bool, Option<Symbol>)>,
600 pub(super) lgx_fail_handlers: Vec<(Symbol, usize)>,
603 pub(super) lgx_pending_events: Vec<(&'a Expr<'a>, &'a Expr<'a>, &'a Expr<'a>)>,
613 pub(super) lgx_pending_reload: bool,
616 pub(super) lgx_html_slots: usize,
620 pub(super) lgx_wrappers: Vec<Symbol>,
622 pub(super) lgx_declared_pass: bool,
625 pub(super) lgx_store_watchers: Vec<(Symbol, Symbol, Symbol, usize)>,
629 pub(super) lgx_key_override: Option<Symbol>,
632 pub(super) lgx_placed: Vec<Symbol>,
634 pub(super) lgx_loop_placed: Vec<Symbol>,
637 pub(super) lgx_paint_root: Option<Symbol>,
639 pub(super) lgx_all_components: Vec<crate::ast::component::ComponentDef<'a>>,
641 pub(super) store_calls: Vec<(Symbol, Symbol, Span)>,
644 pub(super) use_sites: Vec<(Symbol, Span)>,
647 pub(super) all_writes: Vec<(Symbol, Span, component::WriteVerb)>,
652 pub(super) journal_action_sites: Vec<(Symbol, Span)>,
656 pub(super) mount_sites: Vec<MountSite>,
661 pub(super) timer_stops: Vec<(Symbol, Span)>,
664 pub(super) component_writes: Vec<component::ComponentWrite>,
667 pub(super) in_component: bool,
670 pub fragment_mode: bool,
693 pub(super) component_occurrence: usize,
697 pub(super) deferred_functions: Vec<Stmt<'a>>,
703 pub(super) lgx_listeners: Vec<crate::parser::lgx_lower::ListenerSite>,
707 pub(super) lgx_listener_events: Vec<Symbol>,
710 pub(super) lgx_views: crate::parser::lgx_lower::ViewLowering<'a>,
713 pub(super) styled_components: Vec<Symbol>,
716 pub(super) views: Vec<crate::ast::view::ViewDef<'a>>,
721 pub(super) json_encoders_needed: Vec<Symbol>,
725 pub(super) json_decoders_needed: Vec<Symbol>,
728 pub(super) lgx_findings: Vec<crate::lgx::check::Finding>,
731 pub(super) value_copy_findings: Vec<crate::lint::Finding>,
733 pub(super) copy_lint_scope: Option<crate::lint::CopyLintScope>,
738 pub(super) pending_time: Option<Time>,
740 pub(super) donkey_bindings: Vec<(Symbol, Symbol, bool, bool)>,
742 pub(super) interner: &'int mut Interner,
744 pub(super) ctx: AstContext<'a>,
746 pub(super) current_island: u32,
748 pub(super) pp_attach_to_noun: bool,
750 pub(super) filler_gap: Option<Symbol>,
752 pub(super) negative_depth: u32,
754 pub(super) discourse_event_var: Option<Symbol>,
756 pub(super) last_event_template: Option<EventTemplate<'a>>,
758 pub(super) pragmatic: bool,
763 pub(super) noun_priority_mode: bool,
765 pub(super) collective_mode: bool,
767 pub(super) distributive_marker: bool,
771 pub(super) pending_cardinal: Option<u32>,
773 pub(super) mode: ParserMode,
775 pub(super) type_registry: Option<TypeRegistry>,
777 pub(super) constructor_frames: Vec<(Symbol, Option<Symbol>, Vec<Symbol>)>,
787 pub(super) event_reading_mode: bool,
789 pub(super) drs: Drs,
791 pub(super) negative_scope_mode: NegativeScopeMode,
793 pub(super) modal_preference: ModalPreference,
795 pub(super) world_state: &'ctx mut WorldState,
797 pub(super) in_negative_quantifier: bool,
799 pub(super) pending_partitive: Option<(u32, &'a LogicExpr<'a>, Symbol)>,
804 pub(super) pending_subject_restriction: Option<(Symbol, &'a LogicExpr<'a>)>,
812 pub(super) nominal_np_context: bool,
822 pub(super) contracts: std::collections::HashMap<String, (i64, i64)>,
827 pub(super) in_test_block: bool,
830 pub(super) test_names: std::collections::HashSet<String>,
833 pub(super) stmt_spans: Vec<Span>,
846 pub(super) call_spans: std::collections::HashMap<usize, Span>,
851 pub(super) site_tracking: Option<(Symbol, Symbol)>,
863 pub(super) failure_sites: Vec<crate::ast::stmt::FailureSite>,
870 pub(super) nested_stmt_spans: NestedStmtSpans,
873 pub(super) moves: Vec<(Symbol, crate::drs::OwnershipState)>,
877 pub(super) recursion_depth: usize,
883}
884
885impl<'a, 'ctx, 'int> Parser<'a, 'ctx, 'int> {
886 pub fn new(
890 mut tokens: Vec<Token>,
891 world_state: &'ctx mut WorldState,
892 interner: &'int mut Interner,
893 ctx: AstContext<'a>,
894 types: TypeRegistry,
895 ) -> Self {
896 if !matches!(tokens.last().map(|t| &t.kind), Some(TokenType::EOF)) {
901 let end = tokens.last().map(|t| t.span.end).unwrap_or(0);
902 let empty = interner.intern("");
903 tokens.push(Token::new(TokenType::EOF, empty, Span::new(end, end)));
904 }
905 Parser {
906 tokens,
907 current: 0,
908 contracts: std::collections::HashMap::new(),
909 in_test_block: false,
910 test_names: std::collections::HashSet::new(),
911 program_opt_flags: OptimizationConfig::all_on(),
912 program_tier_pins: PinSet::none(),
913 required_opts: Vec::new(),
914 var_counter: 0,
915 user_bound: std::collections::HashSet::new(),
916 tensor_shapes: std::collections::HashMap::new(),
917 tensor_int: std::collections::HashSet::new(),
918 components: std::collections::HashSet::new(),
919 declared_function_names: std::collections::HashSet::new(),
920 directives: std::collections::HashSet::new(),
921 lgx_components: std::collections::HashSet::new(),
922 lgx_stores: std::collections::HashMap::new(),
923 session_declared: false,
924 lgx_pending_key: None,
925 lgx_pending_reuse: None,
926 lgx_state_types: Vec::new(),
927 lgx_fetch_fails: None,
928 lgx_sweeps: Vec::new(),
929 lgx_timers: Vec::new(),
930 lgx_fail_handlers: Vec::new(),
931 lgx_pending_events: Vec::new(),
932 lgx_declared_pass: false,
933 lgx_pending_reload: false,
934 lgx_html_slots: 0,
935 lgx_wrappers: Vec::new(),
936 lgx_store_watchers: Vec::new(),
937 lgx_key_override: None,
938 lgx_placed: Vec::new(),
939 lgx_loop_placed: Vec::new(),
940 lgx_paint_root: None,
941 lgx_all_components: Vec::new(),
942 store_calls: Vec::new(),
943 use_sites: Vec::new(),
944 all_writes: Vec::new(),
945 journal_action_sites: Vec::new(),
946 mount_sites: Vec::new(),
947 timer_stops: Vec::new(),
948 component_writes: Vec::new(),
949 in_component: false,
950 fragment_mode: false,
951 site_tracking: None,
952 failure_sites: Vec::new(),
953 component_occurrence: 0,
954 views: Vec::new(),
955 lgx_findings: Vec::new(),
956 value_copy_findings: Vec::new(),
957 copy_lint_scope: None,
958 json_encoders_needed: Vec::new(),
959 json_decoders_needed: Vec::new(),
960 deferred_functions: Vec::new(),
961 styled_components: Vec::new(),
962 lgx_listeners: Vec::new(),
963 lgx_listener_events: Vec::new(),
964 lgx_views: Default::default(),
965 pending_time: None,
966 donkey_bindings: Vec::new(),
967 interner,
968 ctx,
969 current_island: 0,
970 pp_attach_to_noun: false,
971 filler_gap: None,
972 negative_depth: 0,
973 discourse_event_var: None,
974 last_event_template: None,
975 pragmatic: false,
976 noun_priority_mode: false,
977 collective_mode: false,
978 distributive_marker: false,
979 pending_cardinal: None,
980 mode: ParserMode::Declarative,
981 type_registry: Some(types),
982 constructor_frames: Vec::new(),
983 event_reading_mode: false,
984 drs: Drs::new(), negative_scope_mode: NegativeScopeMode::default(),
986 modal_preference: ModalPreference::default(),
987 pending_subject_restriction: None,
988 nominal_np_context: false,
989 world_state,
990 in_negative_quantifier: false,
991 pending_partitive: None,
992 stmt_spans: Vec::new(),
993 call_spans: std::collections::HashMap::new(),
994 nested_stmt_spans: NestedStmtSpans::default(),
995 moves: Vec::new(),
996 recursion_depth: 0,
997 }
998 }
999
1000 fn record_call_span(&mut self, args: &[&'a Expr<'a>], start_tok: usize) {
1003 if let Some(first) = args.first() {
1004 let span = self.span_from(start_tok);
1005 self.call_spans.insert(*first as *const Expr as usize, span);
1006 }
1007 }
1008
1009 pub fn stmt_spans(&self) -> &[Span] {
1012 &self.stmt_spans
1013 }
1014
1015 pub fn nested_stmt_spans(&self) -> &NestedStmtSpans {
1018 &self.nested_stmt_spans
1019 }
1020
1021 pub fn failure_sites(&self) -> &[crate::ast::stmt::FailureSite] {
1027 &self.failure_sites
1028 }
1029
1030 fn span_from(&self, start_tok: usize) -> Span {
1034 let start = self
1035 .tokens
1036 .get(start_tok)
1037 .map(|t| t.span.start)
1038 .unwrap_or(0);
1039 let end = self
1040 .tokens
1041 .get(self.current.saturating_sub(1))
1042 .map(|t| t.span.end)
1043 .unwrap_or(start);
1044 Span::new(start, end)
1045 }
1046
1047 fn record_stmt_span(&mut self, start_tok: usize) {
1051 let span = self.span_from(start_tok);
1052 self.stmt_spans.push(span);
1053 }
1054
1055 fn span_since(&self, start_tok: usize) -> Span {
1060 let start = self
1061 .tokens
1062 .get(start_tok)
1063 .map(|t| t.span.start)
1064 .unwrap_or(0);
1065 let end = self
1066 .tokens
1067 .get(start_tok..self.current)
1068 .unwrap_or_default()
1069 .iter()
1070 .rev()
1071 .find(|t| {
1072 !matches!(
1073 t.kind,
1074 TokenType::Indent | TokenType::Dedent | TokenType::Newline
1075 )
1076 })
1077 .map(|t| t.span.end)
1078 .unwrap_or(start);
1079 Span::new(start, end.max(start))
1080 }
1081
1082 pub fn set_discourse_event_var(&mut self, var: Symbol) {
1083 self.discourse_event_var = Some(var);
1084 }
1085
1086 pub fn drs_mut(&mut self) -> &mut Drs {
1088 &mut self.world_state.drs
1089 }
1090
1091 pub fn drs_ref(&self) -> &Drs {
1093 &self.world_state.drs
1094 }
1095
1096 pub fn swap_drs_with_world_state(&mut self) {
1100 std::mem::swap(&mut self.drs, &mut self.world_state.drs);
1101 }
1102
1103 pub fn has_world_state(&self) -> bool {
1105 true
1106 }
1107
1108 pub fn mode(&self) -> ParserMode {
1109 self.mode
1110 }
1111
1112 pub fn is_known_type(&self, sym: Symbol) -> bool {
1115 self.type_registry
1116 .as_ref()
1117 .map(|r| r.is_type(sym))
1118 .unwrap_or(false)
1119 }
1120
1121 pub fn is_generic_type(&self, sym: Symbol) -> bool {
1124 self.type_registry
1125 .as_ref()
1126 .map(|r| r.is_generic(sym))
1127 .unwrap_or(false)
1128 }
1129
1130 fn get_generic_param_count(&self, sym: Symbol) -> Option<usize> {
1132 use crate::analysis::TypeDef;
1133 self.type_registry.as_ref().and_then(|r| {
1134 match r.get(sym) {
1135 Some(TypeDef::Generic { param_count }) => Some(*param_count),
1136 Some(TypeDef::Struct { generics, .. }) | Some(TypeDef::Enum { generics, .. })
1142 if !generics.is_empty() =>
1143 {
1144 Some(generics.len())
1145 }
1146 _ => None,
1147 }
1148 })
1149 }
1150
1151 fn find_variant(&self, sym: Symbol, span: Span) -> ParseResult<Option<Symbol>> {
1166 let Some(registry) = self.type_registry.as_ref() else {
1167 return Ok(None);
1168 };
1169 let candidates = registry.find_distinct_variant_candidates(sym, self.interner);
1170 match candidates.len() {
1171 0 => Ok(None),
1172 1 => Ok(Some(candidates[0].0)),
1173 _ => {
1174 let variant_str = self.interner.resolve(sym);
1175 let mut enum_names: Vec<&str> = candidates
1176 .iter()
1177 .map(|(e, _)| self.interner.resolve(*e))
1178 .collect();
1179 enum_names.sort_unstable();
1180 enum_names.dedup();
1181 let named = match enum_names.split_last() {
1182 Some((last, [])) => (*last).to_string(),
1183 Some((last, rest)) => format!("{} and {last}", rest.join(", ")),
1184 None => String::new(),
1185 };
1186 let forms: Vec<String> = enum_names
1187 .iter()
1188 .map(|e| format!("`a new {variant_str} from {e}`"))
1189 .collect();
1190 let taught = match forms.split_last() {
1191 Some((last, [])) => last.clone(),
1192 Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
1193 None => String::new(),
1194 };
1195 Err(ParseError {
1196 kind: ParseErrorKind::Custom(format!(
1197 "'{variant_str}' is declared by more than one type here: {named}. 'a \
1198 new {variant_str}' cannot say which one is meant: write {taught} to name \
1199 the one you want. (An `Inspect` arm matching a value already known to be \
1200 one of them has no such ambiguity and needs no `from`.)"
1201 )),
1202 span,
1203 })
1204 }
1205 }
1206 }
1207
1208 fn resolve_construction_target(
1241 &mut self,
1242 base_type_name: Symbol,
1243 base_span: Span,
1244 ) -> ParseResult<ConstructionTarget> {
1245 if !self.check(&TokenType::From) {
1246 return Ok(match self.find_variant(base_type_name, base_span)? {
1247 Some(enum_name) => ConstructionTarget::Variant {
1248 enum_name,
1249 variant: base_type_name,
1250 },
1251 None => ConstructionTarget::Type(base_type_name),
1252 });
1253 }
1254 self.advance(); let qualifier_span = self.current_span();
1256 let qualifier = self.expect_identifier()?;
1257 let qualified = {
1258 let joined = format!(
1259 "{}::{}",
1260 self.interner.resolve(qualifier),
1261 self.interner.resolve(base_type_name)
1262 );
1263 self.interner.intern(&joined)
1264 };
1265
1266 use crate::analysis::TypeDef;
1267 let registry = self.type_registry.as_ref();
1268 let qualified_is_real_type = registry.is_some_and(|r| r.is_type(qualified));
1269 let is_declared_variant =
1270 registry.is_some_and(|r| r.variant_in(qualifier, base_type_name).is_some());
1271 let qualifier_is_enum =
1272 registry.is_some_and(|r| matches!(r.get(qualifier), Some(TypeDef::Enum { .. })));
1273
1274 if qualified_is_real_type && is_declared_variant {
1275 let base_str = self.interner.resolve(base_type_name);
1276 let qualifier_str = self.interner.resolve(qualifier);
1277 return Err(ParseError {
1278 kind: ParseErrorKind::Custom(format!(
1279 "'{qualifier_str}' names both an enum declaring the variant '{base_str}' \
1280 and an imported module whose own '{qualifier_str}::{base_str}' is a type — \
1281 'a new {base_str} from {qualifier_str}' cannot say which is meant. Rename \
1282 one of the two so 'from' has only one reading here."
1283 )),
1284 span: qualifier_span,
1285 });
1286 }
1287 if qualified_is_real_type {
1288 return Ok(ConstructionTarget::Type(qualified));
1289 }
1290 if is_declared_variant {
1291 return Ok(ConstructionTarget::Variant {
1292 enum_name: qualifier,
1293 variant: base_type_name,
1294 });
1295 }
1296 if qualifier_is_enum {
1297 let base_str = self.interner.resolve(base_type_name).to_string();
1298 let qualifier_str = self.interner.resolve(qualifier).to_string();
1299 let names = self
1300 .type_registry
1301 .as_ref()
1302 .and_then(|r| r.variant_names_of(qualifier));
1303 let suggestion = names.and_then(|names| {
1304 let owned: Vec<String> = names
1305 .iter()
1306 .map(|n| self.interner.resolve(*n).to_string())
1307 .collect();
1308 let refs: Vec<&str> = owned.iter().map(String::as_str).collect();
1309 crate::suggest::find_similar(&base_str, &refs, 2).map(str::to_string)
1310 });
1311 let hint = suggestion
1312 .map(|s| format!(" Did you mean '{s}'?"))
1313 .unwrap_or_default();
1314 return Err(ParseError {
1315 kind: ParseErrorKind::Custom(format!(
1316 "'{base_str}' is not a variant of '{qualifier_str}'.{hint}"
1317 )),
1318 span: qualifier_span,
1319 });
1320 }
1321 let candidates = self
1322 .type_registry
1323 .as_ref()
1324 .map(|r| r.find_distinct_variant_candidates(base_type_name, self.interner))
1325 .unwrap_or_default();
1326 if !candidates.is_empty() {
1327 let base_str = self.interner.resolve(base_type_name).to_string();
1328 let qualifier_str = self.interner.resolve(qualifier).to_string();
1329 let mut enum_names: Vec<&str> = candidates
1330 .iter()
1331 .map(|(e, _)| self.interner.resolve(*e))
1332 .collect();
1333 enum_names.sort_unstable();
1334 enum_names.dedup();
1335 let forms: Vec<String> = enum_names
1336 .iter()
1337 .map(|e| format!("`a new {base_str} from {e}`"))
1338 .collect();
1339 let taught = match forms.split_last() {
1340 Some((last, [])) => last.clone(),
1341 Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
1342 None => String::new(),
1343 };
1344 return Err(ParseError {
1345 kind: ParseErrorKind::Custom(format!(
1346 "'{qualifier_str}' is not an enum, so 'a new {base_str} from {qualifier_str}' \
1347 cannot build a variant from it. '{base_str}' is a variant of: write {taught}."
1348 )),
1349 span: qualifier_span,
1350 });
1351 }
1352 Ok(ConstructionTarget::Type(qualified))
1353 }
1354
1355 fn bare_word_from_is_a_qualifier(&mut self, sym: Symbol) -> bool {
1372 let sym_is_a_variant_somewhere = self
1373 .type_registry
1374 .as_ref()
1375 .is_some_and(|r| !r.find_variant_candidates(sym).is_empty());
1376 if !sym_is_a_variant_somewhere {
1377 return false;
1378 }
1379 let Some(qualifier_sym) = self.peek_field_name_at(1) else {
1380 return false;
1381 };
1382 use crate::analysis::TypeDef;
1383 let qualifier_is_enum = self
1384 .type_registry
1385 .as_ref()
1386 .is_some_and(|r| matches!(r.get(qualifier_sym), Some(TypeDef::Enum { .. })));
1387 if qualifier_is_enum {
1388 return true;
1389 }
1390 let qualified = {
1391 let joined = format!(
1392 "{}::{}",
1393 self.interner.resolve(qualifier_sym),
1394 self.interner.resolve(sym)
1395 );
1396 self.interner.intern(&joined)
1397 };
1398 self.type_registry
1399 .as_ref()
1400 .is_some_and(|r| r.is_type(qualified))
1401 }
1402
1403 fn consume_type_name(&mut self) -> ParseResult<Symbol> {
1405 let t = self.advance().clone();
1406 match t.kind {
1407 TokenType::Noun(s) | TokenType::Adjective(s) => Ok(s),
1408 TokenType::ProperName(s) => Ok(s),
1409 TokenType::Verb { .. } => Ok(t.lexeme),
1411 TokenType::Tally => Ok(self.interner.intern("Tally")),
1413 TokenType::SharedSet => Ok(self.interner.intern("SharedSet")),
1414 TokenType::SharedSequence => Ok(self.interner.intern("SharedSequence")),
1415 TokenType::CollaborativeSequence => Ok(self.interner.intern("CollaborativeSequence")),
1416 TokenType::SharedMap => Ok(self.interner.intern("SharedMap")),
1417 TokenType::Divergent => Ok(self.interner.intern("Divergent")),
1418 TokenType::Article(_) => Ok(t.lexeme),
1420 _ if self.token_is_word_identifier(&t) => Ok(t.lexeme),
1441 other => Err(ParseError {
1442 kind: ParseErrorKind::ExpectedContentWord { found: other },
1443 span: self.current_span(),
1444 }),
1445 }
1446 }
1447
1448 fn parse_type_expression(&mut self) -> ParseResult<TypeExpr<'a>> {
1453 use noun::NounParsing;
1454
1455 if self.check_word("fn") {
1457 if let Some(next) = self.tokens.get(self.current + 1) {
1458 if matches!(next.kind, TokenType::LParen) {
1459 self.advance(); self.advance(); let mut inputs = Vec::new();
1464 if !self.check(&TokenType::RParen) {
1465 inputs.push(self.parse_type_expression()?);
1466 while self.check(&TokenType::Comma) {
1467 self.advance(); inputs.push(self.parse_type_expression()?);
1469 }
1470 }
1471
1472 if !self.check(&TokenType::RParen) {
1473 return Err(ParseError {
1474 kind: ParseErrorKind::ExpectedKeyword {
1475 keyword: ")".to_string(),
1476 },
1477 span: self.current_span(),
1478 });
1479 }
1480 self.advance(); if !self.check(&TokenType::Arrow) {
1484 return Err(ParseError {
1485 kind: ParseErrorKind::ExpectedKeyword {
1486 keyword: "->".to_string(),
1487 },
1488 span: self.current_span(),
1489 });
1490 }
1491 self.advance(); let output = self.parse_type_expression()?;
1494 let output_ref = self.ctx.alloc_type_expr(output);
1495 let inputs_ref = self.ctx.alloc_type_exprs(inputs);
1496 return Ok(TypeExpr::Function {
1497 inputs: inputs_ref,
1498 output: output_ref,
1499 });
1500 }
1501 }
1502 }
1503
1504 if self.check(&TokenType::LParen) {
1507 self.advance(); if self.current + 1 < self.tokens.len()
1510 && matches!(self.tokens[self.current + 1].kind, TokenType::Colon)
1511 {
1512 let mut names = Vec::new();
1513 let mut types = Vec::new();
1514 loop {
1515 names.push(self.consume_type_name()?); if !self.check(&TokenType::Colon) {
1517 return Err(ParseError {
1518 kind: ParseErrorKind::ExpectedKeyword {
1519 keyword: ":".to_string(),
1520 },
1521 span: self.current_span(),
1522 });
1523 }
1524 self.advance(); types.push(self.parse_type_expression()?);
1526 if self.check(&TokenType::Comma) {
1527 self.advance(); continue;
1529 }
1530 break;
1531 }
1532 if !self.check(&TokenType::RParen) {
1533 return Err(ParseError {
1534 kind: ParseErrorKind::ExpectedKeyword {
1535 keyword: ")".to_string(),
1536 },
1537 span: self.current_span(),
1538 });
1539 }
1540 self.advance(); let field_names = self.ctx.alloc_syms(names);
1542 let field_types = self.ctx.alloc_type_exprs(types);
1543 return Ok(TypeExpr::Record {
1544 field_names,
1545 field_types,
1546 });
1547 }
1548 let inner = self.parse_type_expression()?;
1549 if self.check(&TokenType::Comma) {
1550 let mut elems = vec![inner];
1552 while self.check(&TokenType::Comma) {
1553 self.advance(); elems.push(self.parse_type_expression()?);
1555 }
1556 if !self.check(&TokenType::RParen) {
1557 return Err(ParseError {
1558 kind: ParseErrorKind::ExpectedKeyword {
1559 keyword: ")".to_string(),
1560 },
1561 span: self.current_span(),
1562 });
1563 }
1564 self.advance(); let params = self.ctx.alloc_type_exprs(elems);
1566 return Ok(TypeExpr::Tuple(params));
1567 }
1568 if !self.check(&TokenType::RParen) {
1569 return Err(ParseError {
1570 kind: ParseErrorKind::ExpectedKeyword {
1571 keyword: ")".to_string(),
1572 },
1573 span: self.current_span(),
1574 });
1575 }
1576 self.advance(); return Ok(inner);
1578 }
1579
1580 if self.check(&TokenType::Persistent) {
1582 self.advance(); let inner = self.parse_type_expression()?;
1584 let inner_ref = self.ctx.alloc_type_expr(inner);
1585 return Ok(TypeExpr::Persistent { inner: inner_ref });
1586 }
1587
1588 let mut base = self.consume_type_name()?;
1590
1591 let base_name = self.interner.resolve(base);
1593 if base_name == "SharedSet" || base_name == "ORSet" {
1594 if self.check(&TokenType::LParen) {
1595 self.advance(); if self.check(&TokenType::RemoveWins) {
1597 self.advance(); base = self.interner.intern("SharedSet_RemoveWins");
1599 } else if self.check(&TokenType::AddWins) {
1600 self.advance(); base = self.interner.intern("SharedSet_AddWins");
1603 }
1604 if !self.check(&TokenType::RParen) {
1605 return Err(ParseError {
1606 kind: ParseErrorKind::ExpectedKeyword {
1607 keyword: ")".to_string(),
1608 },
1609 span: self.current_span(),
1610 });
1611 }
1612 self.advance(); }
1614 }
1615
1616 let base_name = self.interner.resolve(base);
1618 if base_name == "SharedSequence" || base_name == "RGA" {
1619 if self.check(&TokenType::LParen) {
1620 self.advance(); if self.check(&TokenType::YATA) {
1622 self.advance(); base = self.interner.intern("SharedSequence_YATA");
1624 }
1625 if !self.check(&TokenType::RParen) {
1626 return Err(ParseError {
1627 kind: ParseErrorKind::ExpectedKeyword {
1628 keyword: ")".to_string(),
1629 },
1630 span: self.current_span(),
1631 });
1632 }
1633 self.advance(); }
1635 }
1636
1637 let base_type = if self.check(&TokenType::From) {
1639 self.advance(); let module_name = self.consume_type_name()?;
1641 let module_str = self.interner.resolve(module_name);
1642 let base_str = self.interner.resolve(base);
1643 let qualified = format!("{}::{}", module_str, base_str);
1644 let qualified_sym = self.interner.intern(&qualified);
1645 TypeExpr::Named(qualified_sym)
1646 } else {
1647 let base_name = self.interner.resolve(base);
1649 let base_is_quantity = base_name == "Quantity";
1653 let param_count = self
1654 .get_generic_param_count(base)
1655 .or_else(|| crate::analysis::registry::builtin_generic_param_count(base_name));
1658
1659 if let Some(count) = param_count {
1661 let has_preposition =
1662 self.check_of_preposition() || self.check_preposition_is("from");
1663 let maybe_direct = !has_preposition
1665 && base_name == "Maybe"
1666 && matches!(
1667 self.peek().kind,
1668 TokenType::Noun(_)
1669 | TokenType::Adjective(_)
1670 | TokenType::ProperName(_)
1671 | TokenType::Verb { .. }
1672 );
1673 if has_preposition || maybe_direct {
1674 if has_preposition {
1675 self.advance(); }
1677
1678 let mut params = Vec::new();
1679 for i in 0..count {
1680 if i > 0 {
1681 if self.check(&TokenType::And)
1683 || self.check_to_preposition()
1684 || self.check(&TokenType::Comma)
1685 {
1686 self.advance();
1687 }
1688 }
1689 let param = if base_is_quantity {
1693 let dim_sym = self.peek().lexeme;
1694 self.advance();
1695 TypeExpr::Named(dim_sym)
1696 } else {
1697 self.parse_type_expression()?
1698 };
1699 params.push(param);
1700 }
1701
1702 let params_slice = self.ctx.alloc_type_exprs(params);
1703 TypeExpr::Generic {
1704 base,
1705 params: params_slice,
1706 }
1707 } else {
1708 let is_primitive = self
1710 .type_registry
1711 .as_ref()
1712 .map(|r| r.is_type(base))
1713 .unwrap_or(false)
1714 || matches!(
1715 base_name,
1716 "Int"
1717 | "Nat"
1718 | "Text"
1719 | "Bool"
1720 | "Boolean"
1721 | "Real"
1722 | "Unit"
1723 | "Word8"
1724 | "Word16"
1725 | "Word32"
1726 | "Word64"
1727 | "Lanes8Word32"
1728 | "Lanes4Word32"
1729 | "Lanes16Word8"
1730 | "Lanes4Word64"
1731 | "Lanes16Word16"
1732 );
1733 if is_primitive {
1734 TypeExpr::Primitive(base)
1735 } else {
1736 TypeExpr::Named(base)
1737 }
1738 }
1739 } else {
1740 let is_primitive = self
1742 .type_registry
1743 .as_ref()
1744 .map(|r| r.is_type(base))
1745 .unwrap_or(false)
1746 || matches!(
1747 base_name,
1748 "Int"
1749 | "Nat"
1750 | "Text"
1751 | "Bool"
1752 | "Boolean"
1753 | "Real"
1754 | "Unit"
1755 | "Word8"
1756 | "Word16"
1757 | "Word32"
1758 | "Word64"
1759 | "Lanes8Word32"
1760 | "Lanes4Word32"
1761 | "Lanes16Word8"
1762 | "Lanes4Word64"
1763 | "Lanes16Word16"
1764 );
1765 if is_primitive {
1766 TypeExpr::Primitive(base)
1767 } else {
1768 TypeExpr::Named(base)
1770 }
1771 }
1772 };
1773
1774 if self.check(&TokenType::Where) {
1776 self.advance(); let predicate_expr = self.parse_condition()?;
1780
1781 let bound_var = self
1783 .extract_bound_var(&predicate_expr)
1784 .unwrap_or_else(|| self.interner.intern("it"));
1785
1786 let predicate = self
1788 .expr_to_logic_predicate(&predicate_expr, bound_var)
1789 .ok_or_else(|| ParseError {
1790 kind: ParseErrorKind::InvalidRefinementPredicate,
1791 span: self.peek().span,
1792 })?;
1793
1794 let base_alloc = self.ctx.alloc_type_expr(base_type);
1796
1797 return Ok(TypeExpr::Refinement {
1798 base: base_alloc,
1799 var: bound_var,
1800 predicate,
1801 });
1802 }
1803
1804 Ok(base_type)
1805 }
1806
1807 fn extract_bound_var(&self, expr: &Expr<'a>) -> Option<Symbol> {
1809 match expr {
1810 Expr::Identifier(sym) => Some(*sym),
1811 Expr::BinaryOp { left, .. } => self.extract_bound_var(left),
1812 _ => None,
1813 }
1814 }
1815
1816 fn expr_to_logic_predicate(
1819 &mut self,
1820 expr: &Expr<'a>,
1821 bound_var: Symbol,
1822 ) -> Option<&'a LogicExpr<'a>> {
1823 match expr {
1824 Expr::BinaryOp { op, left, right } => {
1825 let pred_name = match op {
1827 BinaryOpKind::Gt => "Greater",
1828 BinaryOpKind::Lt => "Less",
1829 BinaryOpKind::GtEq => "GreaterEqual",
1830 BinaryOpKind::LtEq => "LessEqual",
1831 BinaryOpKind::Eq => "Equal",
1832 BinaryOpKind::NotEq => "NotEqual",
1833 BinaryOpKind::And => {
1834 let left_logic = self.expr_to_logic_predicate(left, bound_var)?;
1836 let right_logic = self.expr_to_logic_predicate(right, bound_var)?;
1837 return Some(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1838 left: left_logic,
1839 op: TokenType::And,
1840 right: right_logic,
1841 }));
1842 }
1843 BinaryOpKind::Or => {
1844 let left_logic = self.expr_to_logic_predicate(left, bound_var)?;
1845 let right_logic = self.expr_to_logic_predicate(right, bound_var)?;
1846 return Some(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1847 left: left_logic,
1848 op: TokenType::Or,
1849 right: right_logic,
1850 }));
1851 }
1852 _ => return None, };
1854 let pred_sym = self.interner.intern(pred_name);
1855
1856 let left_term = self.expr_to_term(left)?;
1858 let right_term = self.expr_to_term(right)?;
1859
1860 let args = self.ctx.terms.alloc_slice([left_term, right_term]);
1861 Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
1862 name: pred_sym,
1863 args,
1864 world: None,
1865 }))
1866 }
1867 _ => None,
1868 }
1869 }
1870
1871 fn expr_to_term(&mut self, expr: &Expr<'a>) -> Option<Term<'a>> {
1873 match expr {
1874 Expr::Identifier(sym) => Some(Term::Variable(*sym)),
1875 Expr::Literal(lit) => {
1876 match lit {
1877 Literal::Number(n) => Some(Term::Value {
1878 kind: NumberKind::Integer(*n),
1879 unit: None,
1880 dimension: None,
1881 }),
1882 Literal::Boolean(b) => {
1883 let sym = self.interner.intern(if *b { "true" } else { "false" });
1884 Some(Term::Constant(sym))
1885 }
1886 _ => None, }
1888 }
1889 _ => None,
1890 }
1891 }
1892
1893 pub fn process_block_headers(&mut self) {
1894 use crate::token::BlockType;
1895
1896 while self.current < self.tokens.len() {
1897 if let TokenType::BlockHeader { block_type } = &self.tokens[self.current].kind {
1898 self.mode = match block_type {
1899 BlockType::Main
1900 | BlockType::Function
1901 | BlockType::Component
1902 | BlockType::Test { .. }
1903 | BlockType::Page { .. }
1904 | BlockType::Store
1905 | BlockType::Session
1906 | BlockType::Directive
1907 | BlockType::Route { .. }
1908 | BlockType::Middleware
1909 | BlockType::Redirect { .. }
1910 | BlockType::ErrorPage { .. } => ParserMode::Imperative,
1911 BlockType::Theorem
1912 | BlockType::Definition
1913 | BlockType::Define
1914 | BlockType::Proof
1915 | BlockType::Example
1916 | BlockType::Logic
1917 | BlockType::Note
1918 | BlockType::TypeDef
1919 | BlockType::Policy
1920 | BlockType::Requires
1921 | BlockType::Axiom
1922 | BlockType::Theory
1923 | BlockType::Hardware
1924 | BlockType::Property
1925 | BlockType::SuspectedTypo { .. } => ParserMode::Declarative,
1926 BlockType::No | BlockType::Tier | BlockType::Require => self.mode, };
1928 self.current += 1;
1929 } else {
1930 break;
1931 }
1932 }
1933 }
1934
1935 pub(super) fn event_with_conditions(
1939 &mut self,
1940 event: NeoEventData<'a>,
1941 conditions: Vec<&'a LogicExpr<'a>>,
1942 ) -> &'a LogicExpr<'a> {
1943 let binds_its_conditions = !event.suppress_existential && !conditions.is_empty();
1944 let event_var = event.event_var;
1945 let mut combined = self
1946 .ctx
1947 .exprs
1948 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
1949 suppress_existential: event.suppress_existential || binds_its_conditions,
1950 ..event
1951 })));
1952 for condition in conditions {
1953 combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1954 left: combined,
1955 op: TokenType::And,
1956 right: condition,
1957 });
1958 }
1959 if binds_its_conditions {
1960 combined = self.ctx.exprs.alloc(LogicExpr::Quantifier {
1961 kind: QuantifierKind::Existential,
1962 variable: event_var,
1963 body: combined,
1964 island_id: self.current_island,
1965 });
1966 }
1967 combined
1968 }
1969
1970 pub fn get_event_var(&mut self) -> Symbol {
1971 self.discourse_event_var
1972 .unwrap_or_else(|| self.interner.intern("e"))
1973 }
1974
1975 pub fn capture_event_template(
1976 &mut self,
1977 verb: Symbol,
1978 roles: &[(ThematicRole, Term<'a>)],
1979 modifiers: &[Symbol],
1980 ) {
1981 let non_agent_roles: Vec<_> = roles
1982 .iter()
1983 .filter(|(role, _)| *role != ThematicRole::Agent)
1984 .cloned()
1985 .collect();
1986 let agent = roles
1987 .iter()
1988 .find(|(role, _)| *role == ThematicRole::Agent)
1989 .map(|(_, t)| t.clone());
1990 self.last_event_template = Some(EventTemplate {
1991 verb,
1992 agent,
1993 non_agent_roles,
1994 modifiers: modifiers.to_vec(),
1995 });
1996 }
1997
1998 pub(super) fn parse_whose_relative(
2008 &mut self,
2009 gap_var: Symbol,
2010 ) -> ParseResult<&'a LogicExpr<'a>> {
2011 self.advance(); let possessed = self.parse_noun_phrase(false)?;
2013 let m = self.next_var_name();
2014 let noun_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2015 name: possessed.noun,
2016 args: self.ctx.terms.alloc_slice([Term::Variable(m)]),
2017 world: None,
2018 });
2019 let possesses = self.interner.intern("Possesses");
2020 let poss_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2021 name: possesses,
2022 args: self
2023 .ctx
2024 .terms
2025 .alloc_slice([Term::Variable(gap_var), Term::Variable(m)]),
2026 world: None,
2027 });
2028 let mut body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2029 left: noun_pred,
2030 op: TokenType::And,
2031 right: poss_pred,
2032 });
2033 for &adj in possessed.adjectives {
2034 let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2035 name: adj,
2036 args: self.ctx.terms.alloc_slice([Term::Variable(m)]),
2037 world: None,
2038 });
2039 body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2040 left: body,
2041 op: TokenType::And,
2042 right: adj_pred,
2043 });
2044 }
2045 let vp = self.parse_predicate_with_subject_as_var(m)?;
2046 body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2047 left: body,
2048 op: TokenType::And,
2049 right: vp,
2050 });
2051 Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
2052 kind: QuantifierKind::Existential,
2053 variable: m,
2054 body,
2055 island_id: self.current_island,
2056 }))
2057 }
2058
2059 pub(super) fn parse_where_relative(
2060 &mut self,
2061 gap_var: Symbol,
2062 ) -> ParseResult<&'a LogicExpr<'a>> {
2063 self.advance(); let subj_sym = if self.check_pronoun() {
2068 let lex = self.interner.resolve(self.peek().lexeme).to_string();
2069 self.advance();
2070 let mut chars = lex.chars();
2071 let cap = match chars.next() {
2072 Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
2073 None => lex,
2074 };
2075 self.interner.intern(&cap)
2076 } else {
2077 self.parse_noun_phrase(true)?.noun
2078 };
2079 let boundary = {
2086 let mut found = None;
2087 let mut k = self.current;
2088 while let Some(tok) = self.tokens.get(k) {
2089 match &tok.kind {
2090 TokenType::Comma => {
2091 found = Some(k);
2092 break;
2093 }
2094 TokenType::And | TokenType::Or
2095 if self
2096 .tokens
2097 .get(k + 1)
2098 .is_some_and(|t| t.kind.is_generalized_quantifier())
2099 || matches!(
2100 self.tokens.get(k + 1).map(|t| &t.kind),
2101 Some(TokenType::Article(_))
2102 | Some(TokenType::ProperName(_))
2103 | Some(TokenType::All)
2104 | Some(TokenType::Some)
2105 | Some(TokenType::Any)
2106 | Some(TokenType::No)
2107 ) =>
2108 {
2109 found = Some(k);
2110 break;
2111 }
2112 TokenType::Period
2113 | TokenType::EOF
2114 | TokenType::Is
2115 | TokenType::Are
2116 | TokenType::Was
2117 | TokenType::Were => break,
2118 _ => k += 1,
2119 }
2120 }
2121 found
2122 };
2123 let saved_boundary = boundary.map(|i| (i, self.tokens[i].clone()));
2124 if let Some(i) = boundary {
2125 let span = self.tokens[i].span;
2126 self.tokens[i] =
2127 crate::token::Token::new(TokenType::Period, self.interner.intern("."), span);
2128 }
2129 let inner_result = self.parse_predicate_with_subject(subj_sym);
2130 if let Some((i, tok)) = saved_boundary {
2131 self.tokens[i] = tok;
2132 }
2133 let inner = inner_result?;
2134 let ev = self.get_event_var();
2135 let loc = self.interner.intern("In");
2136 let loc_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2137 name: loc,
2138 args: self
2139 .ctx
2140 .terms
2141 .alloc_slice([Term::Variable(ev), Term::Variable(gap_var)]),
2142 world: None,
2143 });
2144 Ok(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2145 left: inner,
2146 op: TokenType::And,
2147 right: loc_pred,
2148 }))
2149 }
2150
2151 fn parse_embedded_wh_clause(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
2152 let var_name = self.interner.intern("x");
2154 let var_term = Term::Variable(var_name);
2155
2156 if self.check_verb() {
2157 let verb = self.consume_verb();
2159 let body = self.ctx.exprs.alloc(LogicExpr::Predicate {
2160 name: verb,
2161 args: self.ctx.terms.alloc_slice([var_term]),
2162 world: None,
2163 });
2164 return Ok(body);
2165 }
2166
2167 if self.check_content_word() || self.check_article() {
2168 let subject = self.parse_noun_phrase(true)?;
2170 if self.check_verb() {
2171 let verb = self.consume_verb();
2172 let body = self.ctx.exprs.alloc(LogicExpr::Predicate {
2173 name: verb,
2174 args: self
2175 .ctx
2176 .terms
2177 .alloc_slice([Term::Constant(subject.noun), var_term]),
2178 world: None,
2179 });
2180 return Ok(body);
2181 }
2182 }
2183
2184 Ok(self.ctx.exprs.alloc(LogicExpr::Atom(var_name)))
2186 }
2187
2188 pub fn set_pp_attachment_mode(&mut self, attach_to_noun: bool) {
2189 self.pp_attach_to_noun = attach_to_noun;
2190 }
2191
2192 pub fn set_noun_priority_mode(&mut self, mode: bool) {
2193 self.noun_priority_mode = mode;
2194 }
2195
2196 pub fn set_collective_mode(&mut self, mode: bool) {
2197 self.collective_mode = mode;
2198 }
2199
2200 pub fn set_distributive_marker(&mut self, on: bool) {
2204 self.distributive_marker = on;
2205 }
2206
2207 pub fn set_pragmatic_mode(&mut self, on: bool) {
2211 self.pragmatic = on;
2212 }
2213
2214 pub fn set_event_reading_mode(&mut self, mode: bool) {
2215 self.event_reading_mode = mode;
2216 }
2217
2218 pub fn set_negative_scope_mode(&mut self, mode: NegativeScopeMode) {
2219 self.negative_scope_mode = mode;
2220 }
2221
2222 pub fn set_modal_preference(&mut self, pref: ModalPreference) {
2223 self.modal_preference = pref;
2224 }
2225
2226 fn checkpoint(&self) -> ParserCheckpoint {
2227 ParserCheckpoint {
2228 pos: self.current,
2229 var_counter: self.var_counter,
2230 bindings_len: self.donkey_bindings.len(),
2231 island: self.current_island,
2232 time: self.pending_time,
2233 negative_depth: self.negative_depth,
2234 drs: self.drs.mark(),
2235 }
2236 }
2237
2238 fn restore(&mut self, cp: ParserCheckpoint) {
2239 self.current = cp.pos;
2240 self.var_counter = cp.var_counter;
2241 self.donkey_bindings.truncate(cp.bindings_len);
2242 self.current_island = cp.island;
2243 self.pending_time = cp.time;
2244 self.negative_depth = cp.negative_depth;
2245 self.drs.rewind(&cp.drs);
2246 }
2247
2248 fn backtrack_point(&self) -> Backtrack {
2250 Backtrack {
2251 pos: self.current,
2252 drs: self.drs.mark(),
2253 }
2254 }
2255
2256 fn backtrack_to(&mut self, point: &Backtrack) {
2259 self.current = point.pos;
2260 self.drs.rewind(&point.drs);
2261 }
2262
2263 fn is_negative_context(&self) -> bool {
2264 self.negative_depth % 2 == 1
2265 }
2266
2267 pub(super) fn indefinite_scope(&self) -> IndefiniteScope {
2269 if self.in_negative_quantifier || self.is_negative_context() {
2270 IndefiniteScope::Negated
2271 } else if self.drs.in_conditional_antecedent() {
2272 IndefiniteScope::Antecedent
2273 } else {
2274 IndefiniteScope::Local
2275 }
2276 }
2277
2278 pub(super) fn introduce_indefinite(
2280 &mut self,
2281 var: Symbol,
2282 noun: Symbol,
2283 gender: Gender,
2284 number: Number,
2285 ) -> IndefiniteScope {
2286 let scope = self.indefinite_scope();
2287 if scope == IndefiniteScope::Negated {
2288 self.drs.introduce_referent_with_source(
2289 var,
2290 noun,
2291 gender,
2292 number,
2293 ReferentSource::NegationScope,
2294 );
2295 } else {
2296 self.drs.introduce_referent(var, noun, gender, number);
2297 }
2298 scope
2299 }
2300
2301 pub fn guard(&mut self) -> ParserGuard<'_, 'a, 'ctx, 'int> {
2302 ParserGuard {
2303 checkpoint: self.checkpoint(),
2304 parser: self,
2305 committed: false,
2306 }
2307 }
2308
2309 pub(super) fn try_parse<F, T>(&mut self, op: F) -> Option<T>
2310 where
2311 F: FnOnce(&mut Self) -> ParseResult<T>,
2312 {
2313 let cp = self.checkpoint();
2314 match op(self) {
2315 Ok(res) => Some(res),
2316 Err(_) => {
2317 self.restore(cp);
2318 None
2319 }
2320 }
2321 }
2322
2323 fn resolve_pronoun(&mut self, gender: Gender, number: Number) -> ParseResult<ResolvedPronoun> {
2324 if self.world_state.in_discourse_mode() && self.world_state.has_prior_modal_context() {
2329 if let Some(candidate) = self.world_state.resolve_via_telescope(gender) {
2332 return Ok(ResolvedPronoun::Variable(candidate.variable));
2333 }
2334 let blocked_candidates: Vec<_> = self
2338 .world_state
2339 .telescope_candidates()
2340 .iter()
2341 .filter(|c| c.in_modal_scope)
2342 .collect();
2343 if !blocked_candidates.is_empty() {
2344 let has_upcoming_modal = self.has_modal_subordination_ahead();
2347 if has_upcoming_modal {
2348 if let Some(candidate) = blocked_candidates.into_iter().find(|c| {
2350 c.gender == gender
2351 || gender == Gender::Unknown
2352 || c.gender == Gender::Unknown
2353 }) {
2354 return Ok(ResolvedPronoun::Variable(candidate.variable));
2355 }
2356 }
2357 return Err(ParseError {
2359 kind: ParseErrorKind::ScopeViolation(
2360 "Cannot access hypothetical entity from reality. Use modal subordination (e.g., 'would') to continue a hypothetical context.".to_string()
2361 ),
2362 span: self.current_span(),
2363 });
2364 }
2365 }
2367
2368 let current_box = self.drs.current_box_index();
2370 match self.drs.resolve_pronoun(current_box, gender, number) {
2371 Ok(sym) => {
2372 return Ok(if self.drs.is_rigid_referent(sym) {
2376 ResolvedPronoun::Constant(sym)
2377 } else {
2378 ResolvedPronoun::Variable(sym)
2379 });
2380 }
2381 Err(crate::drs::ScopeError::InaccessibleReferent {
2382 gender: g, reason, ..
2383 }) => {
2384 if self.world_state.in_discourse_mode() {
2388 if let Some(candidate) = self.world_state.resolve_via_telescope(g) {
2389 return Ok(ResolvedPronoun::Variable(candidate.variable));
2390 }
2391 }
2392 return Err(ParseError {
2394 kind: ParseErrorKind::ScopeViolation(reason),
2395 span: self.current_span(),
2396 });
2397 }
2398 Err(crate::drs::ScopeError::NoMatchingReferent {
2399 gender: g,
2400 number: n,
2401 }) => {
2402 if !self.world_state.has_prior_modal_context() {
2404 if let Some(candidate) = self.world_state.resolve_via_telescope(g) {
2405 return Ok(ResolvedPronoun::Variable(candidate.variable));
2406 }
2407 }
2408
2409 if self.world_state.in_discourse_mode() {
2411 return Err(ParseError {
2412 kind: ParseErrorKind::UnresolvedPronoun {
2413 gender: g,
2414 number: n,
2415 },
2416 span: self.current_span(),
2417 });
2418 }
2419
2420 let deictic_name = match (g, n) {
2423 (Gender::Male, Number::Singular) => "Him",
2424 (Gender::Female, Number::Singular) => "Her",
2425 (Gender::Neuter, Number::Singular) => "It",
2426 (Gender::Male, Number::Plural) | (Gender::Female, Number::Plural) => "Them",
2427 (Gender::Neuter, Number::Plural) => "Them",
2428 (Gender::Unknown, _) => "Someone",
2429 };
2430 let sym = self.interner.intern(deictic_name);
2431 self.drs.introduce_referent_with_source(
2435 sym,
2436 sym,
2437 g,
2438 n,
2439 crate::drs::ReferentSource::ProperName,
2440 );
2441 return Ok(ResolvedPronoun::Constant(sym));
2442 }
2443 }
2444 }
2445
2446 fn resolve_donkey_pronoun(&mut self, gender: Gender) -> Option<Symbol> {
2447 for (noun_class, var_name, used, _wide_neg) in self.donkey_bindings.iter_mut().rev() {
2448 let noun_str = self.interner.resolve(*noun_class);
2449 let noun_gender = Self::infer_noun_gender(noun_str);
2450 if noun_gender == gender || gender == Gender::Neuter || noun_gender == Gender::Unknown {
2451 *used = true; return Some(*var_name);
2453 }
2454 }
2455 None
2456 }
2457
2458 fn infer_noun_gender(noun: &str) -> Gender {
2459 let lower = noun.to_lowercase();
2460 if lexicon::is_female_noun(&lower) {
2461 Gender::Female
2462 } else if lexicon::is_male_noun(&lower) {
2463 Gender::Male
2464 } else if lexicon::is_neuter_noun(&lower) {
2465 Gender::Neuter
2466 } else {
2467 Gender::Unknown
2468 }
2469 }
2470
2471 fn is_plural_noun(noun: &str) -> bool {
2472 let lower = noun.to_lowercase();
2473 if lexicon::is_proper_name(&lower) {
2475 return false;
2476 }
2477 if lexicon::is_irregular_plural(&lower) {
2478 return true;
2479 }
2480 lower.ends_with('s') && !lower.ends_with("ss") && lower.len() > 2
2481 }
2482
2483 fn singularize_noun(noun: &str) -> String {
2484 let lower = noun.to_lowercase();
2485 if let Some(singular) = lexicon::singularize(&lower) {
2486 return singular.to_string();
2487 }
2488 if lower.ends_with('s') && !lower.ends_with("ss") && lower.len() > 2 {
2489 let base = &lower[..lower.len() - 1];
2490 let mut chars: Vec<char> = base.chars().collect();
2491 if !chars.is_empty() {
2492 chars[0] = chars[0].to_uppercase().next().unwrap();
2493 }
2494 return chars.into_iter().collect();
2495 }
2496 let mut chars: Vec<char> = lower.chars().collect();
2497 if !chars.is_empty() {
2498 chars[0] = chars[0].to_uppercase().next().unwrap();
2499 }
2500 chars.into_iter().collect()
2501 }
2502
2503 fn infer_gender(name: &str) -> Gender {
2504 let lower = name.to_lowercase();
2505 if lexicon::is_male_name(&lower) {
2506 Gender::Male
2507 } else if lexicon::is_female_name(&lower) {
2508 Gender::Female
2509 } else {
2510 Gender::Unknown
2511 }
2512 }
2513
2514 fn scalar_implicature(&mut self, expr: &'a LogicExpr<'a>) -> Option<&'a LogicExpr<'a>> {
2524 if let LogicExpr::Quantifier {
2525 kind: QuantifierKind::Existential | QuantifierKind::Most | QuantifierKind::Many,
2526 variable,
2527 body,
2528 island_id,
2529 } = expr
2530 {
2531 let alt_body = if let LogicExpr::BinaryOp {
2533 left,
2534 op: TokenType::And,
2535 right,
2536 } = body
2537 {
2538 self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2539 left,
2540 op: TokenType::Implies,
2541 right,
2542 })
2543 } else {
2544 body
2545 };
2546 let universal = self.ctx.exprs.alloc(LogicExpr::Quantifier {
2547 kind: QuantifierKind::Universal,
2548 variable: *variable,
2549 body: alt_body,
2550 island_id: *island_id,
2551 });
2552 let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
2553 op: TokenType::Not,
2554 operand: universal,
2555 });
2556 Some(self.ctx.exprs.alloc(LogicExpr::Implicature {
2557 assertion: expr,
2558 implicature: negated,
2559 }))
2560 } else {
2561 None
2562 }
2563 }
2564
2565 fn coerce_agent(&mut self, np: &crate::ast::NounPhrase<'a>) -> Term<'a> {
2566 let base = self.noun_phrase_to_term(np);
2567 if crate::lexicon::is_institution_metonym(&self.interner.resolve(np.noun).to_lowercase()) {
2568 let gov = self.interner.intern("GovernmentOf");
2569 Term::Function(gov, self.ctx.terms.alloc_slice([base]))
2570 } else {
2571 base
2572 }
2573 }
2574
2575 fn next_var_name(&mut self) -> Symbol {
2576 const VARS: &[&str] = &["x", "y", "z", "w", "v", "u"];
2577 let idx = self.var_counter;
2578 self.var_counter += 1;
2579 if idx < VARS.len() {
2580 self.interner.intern(VARS[idx])
2581 } else {
2582 let name = format!("x{}", idx - VARS.len() + 1);
2583 self.interner.intern(&name)
2584 }
2585 }
2586
2587 fn parse_i64_numeral(&self, num_str: &str) -> ParseResult<i64> {
2593 self.parse_i64_numeral_signed(num_str, false)
2594 }
2595
2596 fn parse_i64_numeral_signed(&self, num_str: &str, negative: bool) -> ParseResult<i64> {
2600 let clean = num_str.replace('_', "");
2601 let sign = if negative { "-" } else { "" };
2602 let radix = |digits: &str, base: u32| -> Result<i64, ()> {
2609 if let Ok(v) = i64::from_str_radix(&format!("{}{}", sign, digits), base) {
2610 return Ok(v);
2611 }
2612 if !negative {
2613 if let Ok(u) = u64::from_str_radix(digits, base) {
2614 return Ok(u as i64);
2615 }
2616 }
2617 Err(())
2618 };
2619 let parsed = if let Some(hex) = clean
2620 .strip_prefix("0x")
2621 .or_else(|| clean.strip_prefix("0X"))
2622 {
2623 radix(hex, 16)
2624 } else if let Some(bin) = clean
2625 .strip_prefix("0b")
2626 .or_else(|| clean.strip_prefix("0B"))
2627 {
2628 radix(bin, 2)
2629 } else if let Some(oct) = clean
2630 .strip_prefix("0o")
2631 .or_else(|| clean.strip_prefix("0O"))
2632 {
2633 radix(oct, 8)
2634 } else {
2635 format!("{}{}", sign, clean).parse::<i64>().map_err(|_| ())
2636 };
2637 parsed.map_err(|_| ParseError {
2638 kind: ParseErrorKind::ExpectedKeyword {
2639 keyword: format!(
2640 "a representable Int literal — `{}` does not fit the Int range (±9223372036854775807)",
2641 num_str
2642 ),
2643 },
2644 span: self.current_span(),
2645 })
2646 }
2647
2648 fn is_radix_numeral(num_str: &str) -> bool {
2652 let s = num_str.strip_prefix('_').unwrap_or(num_str);
2653 s.len() > 2
2654 && s.starts_with('0')
2655 && matches!(s.as_bytes()[1], b'x' | b'X' | b'b' | b'B' | b'o' | b'O')
2656 }
2657
2658 fn parse_index_numeral(&mut self, sym: Symbol) -> ParseResult<&'a Expr<'a>> {
2664 let num_str = self.interner.resolve(sym).to_string();
2665 if let Ok(n) = self.parse_i64_numeral(&num_str) {
2666 if n == 0 {
2667 return Err(ParseError {
2668 kind: ParseErrorKind::ZeroIndex,
2669 span: self.current_span(),
2670 });
2671 }
2672 return Ok(self
2673 .ctx
2674 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(n))));
2675 }
2676 if let Ok(f) = num_str.parse::<f64>() {
2677 return Ok(self
2678 .ctx
2679 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Float(f))));
2680 }
2681 Err(ParseError {
2682 kind: ParseErrorKind::ExpectedKeyword {
2683 keyword: format!("a numeric index (got '{}')", num_str),
2684 },
2685 span: self.current_span(),
2686 })
2687 }
2688
2689 fn fresh_desugar_temp(&mut self, tag: &str) -> Symbol {
2692 let idx = self.var_counter;
2693 self.var_counter += 1;
2694 self.interner.intern(&format!("__{}_{}", tag, idx))
2695 }
2696
2697 fn capture_place_indices(
2701 &mut self,
2702 place: &'a Expr<'a>,
2703 prefix: &mut Vec<Stmt<'a>>,
2704 ) -> &'a Expr<'a> {
2705 match place {
2706 Expr::Identifier(_) => place,
2707 Expr::FieldAccess { object, field } => {
2708 let object = self.capture_place_indices(object, prefix);
2709 self.ctx.alloc_imperative_expr(Expr::FieldAccess {
2710 object,
2711 field: *field,
2712 })
2713 }
2714 Expr::Index { collection, index } => {
2715 let collection = self.capture_place_indices(collection, prefix);
2716 let key = self.fresh_desugar_temp("place_key");
2717 prefix.push(Stmt::Let {
2718 var: key,
2719 ty: None,
2720 value: index,
2721 mutable: false,
2722 });
2723 let index = self.ctx.alloc_imperative_expr(Expr::Identifier(key));
2724 self.ctx
2725 .alloc_imperative_expr(Expr::Index { collection, index })
2726 }
2727 _ => {
2728 let root = self.fresh_desugar_temp("place_root");
2729 prefix.push(Stmt::Let {
2730 var: root,
2731 ty: None,
2732 value: place,
2733 mutable: true,
2734 });
2735 self.ctx.alloc_imperative_expr(Expr::Identifier(root))
2736 }
2737 }
2738 }
2739
2740 fn desugar_place_set_index(
2757 &mut self,
2758 collection: &'a Expr<'a>,
2759 index: &'a Expr<'a>,
2760 value: &'a Expr<'a>,
2761 ) -> Stmt<'a> {
2762 let (base, key) = match collection {
2763 Expr::Index {
2764 collection: base,
2765 index: key,
2766 } => (*base, *key),
2767 _ => {
2768 return Stmt::SetIndex {
2769 collection,
2770 index,
2771 value,
2772 }
2773 }
2774 };
2775 let t_idx = self.fresh_desugar_temp("place_i");
2776 let t_val = self.fresh_desugar_temp("place_v");
2777 let t_key = self.fresh_desugar_temp("place_k");
2778 let t_inner = self.fresh_desugar_temp("place_t");
2779 let idx_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_idx));
2780 let val_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_val));
2781 let key_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_key));
2782 let inner_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_inner));
2783 let mut body = vec![
2784 Stmt::Let {
2785 var: t_idx,
2786 ty: None,
2787 value: index,
2788 mutable: false,
2789 },
2790 Stmt::Let {
2791 var: t_val,
2792 ty: None,
2793 value,
2794 mutable: false,
2795 },
2796 ];
2797 let base = self.capture_place_indices(base, &mut body);
2798 let read = self.ctx.alloc_imperative_expr(Expr::Index {
2799 collection: base,
2800 index: key_ref,
2801 });
2802 let write_back = self.desugar_place_set_index(base, key_ref, inner_ref);
2803 body.extend([
2804 Stmt::Let {
2805 var: t_key,
2806 ty: None,
2807 value: key,
2808 mutable: false,
2809 },
2810 Stmt::Let {
2811 var: t_inner,
2812 ty: None,
2813 value: read,
2814 mutable: true,
2815 },
2816 Stmt::SetIndex {
2817 collection: inner_ref,
2818 index: idx_ref,
2819 value: val_ref,
2820 },
2821 write_back,
2822 ]);
2823 let body = self.alloc_block(body);
2824 Stmt::Splice { body }
2825 }
2826
2827 fn desugar_place_push(&mut self, value: &'a Expr<'a>, collection: &'a Expr<'a>) -> Stmt<'a> {
2830 let (base, key) = match collection {
2831 Expr::Index {
2832 collection: base,
2833 index: key,
2834 } => (*base, *key),
2835 _ => return Stmt::Push { value, collection },
2836 };
2837 let t_val = self.fresh_desugar_temp("place_v");
2838 let t_key = self.fresh_desugar_temp("place_k");
2839 let t_inner = self.fresh_desugar_temp("place_t");
2840 let val_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_val));
2841 let key_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_key));
2842 let inner_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_inner));
2843 let mut body = vec![Stmt::Let {
2844 var: t_val,
2845 ty: None,
2846 value,
2847 mutable: false,
2848 }];
2849 let base = self.capture_place_indices(base, &mut body);
2850 let read = self.ctx.alloc_imperative_expr(Expr::Index {
2851 collection: base,
2852 index: key_ref,
2853 });
2854 let write_back = self.desugar_place_set_index(base, key_ref, inner_ref);
2855 body.extend([
2856 Stmt::Let {
2857 var: t_key,
2858 ty: None,
2859 value: key,
2860 mutable: false,
2861 },
2862 Stmt::Let {
2863 var: t_inner,
2864 ty: None,
2865 value: read,
2866 mutable: true,
2867 },
2868 Stmt::Push {
2869 value: val_ref,
2870 collection: inner_ref,
2871 },
2872 write_back,
2873 ]);
2874 let body = self.alloc_block(body);
2875 Stmt::Splice { body }
2876 }
2877
2878 fn try_parse_every_where(
2881 &mut self,
2882 ) -> ParseResult<Option<(Symbol, &'a Expr<'a>, &'a Expr<'a>)>> {
2883 if !self.check_word("every") {
2884 return Ok(None);
2885 }
2886 let saved = self.backtrack_point();
2887 self.advance(); if !self.check_word_at(self.current + 1, "in") {
2889 self.backtrack_to(&saved);
2890 return Ok(None);
2891 }
2892 let Ok(binder) = self.expect_identifier() else {
2893 self.backtrack_to(&saved);
2894 return Ok(None);
2895 };
2896 self.advance(); let list = self.parse_imperative_expr()?;
2898 self.expect_keyword("where")?;
2899 let condition = self.parse_condition()?;
2900 Ok(Some((binder, list, condition)))
2901 }
2902
2903 fn desugar_keep_where(
2910 &mut self,
2911 binder: Symbol,
2912 list: &'a Expr<'a>,
2913 condition: &'a Expr<'a>,
2914 keep: bool,
2915 ) -> (Symbol, Vec<Stmt<'a>>) {
2916 let kept = self.fresh_desugar_temp("where_k");
2917 let test = if keep {
2918 condition
2919 } else {
2920 self.ctx
2921 .alloc_imperative_expr(Expr::Not { operand: condition })
2922 };
2923 let kept_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
2924 let item_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(binder));
2925 let length = self.ctx.alloc_imperative_expr(Expr::Length {
2926 collection: kept_ref,
2927 });
2928 let zero = self
2929 .ctx
2930 .alloc_imperative_expr(Expr::Literal(Literal::Number(0)));
2931 let not_empty = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
2932 op: BinaryOpKind::Gt,
2933 left: length,
2934 right: zero,
2935 });
2936 let pop = self.alloc_block(vec![Stmt::Pop {
2937 collection: kept_ref,
2938 into: None,
2939 }]);
2940 let push = self.alloc_block(vec![Stmt::Push {
2941 value: item_ref,
2942 collection: kept_ref,
2943 }]);
2944 let per_item = self.alloc_block(vec![Stmt::If {
2945 cond: test,
2946 then_block: push,
2947 else_block: None,
2948 }]);
2949 let body = vec![
2950 Stmt::Let {
2951 var: kept,
2952 ty: None,
2953 value: list,
2954 mutable: true,
2955 },
2956 Stmt::While {
2957 cond: not_empty,
2958 body: pop,
2959 decreasing: None,
2960 },
2961 Stmt::Repeat {
2962 pattern: Pattern::Identifier(binder),
2963 iterable: list,
2964 body: per_item,
2965 },
2966 ];
2967 (kept, body)
2968 }
2969
2970 #[inline]
2978 fn te_int(&self, n: i64) -> &'a Expr<'a> {
2979 self.ctx
2980 .alloc_imperative_expr(Expr::Literal(Literal::Number(n)))
2981 }
2982 #[inline]
2983 fn te_float(&self, v: f64) -> &'a Expr<'a> {
2984 self.ctx
2985 .alloc_imperative_expr(Expr::Literal(Literal::Float(v)))
2986 }
2987 #[inline]
2988 fn te_call(&self, function: Symbol, args: Vec<&'a Expr<'a>>) -> &'a Expr<'a> {
2989 self.ctx
2990 .alloc_imperative_expr(Expr::Call { function, args })
2991 }
2992 #[inline]
2993 fn te_id(&self, s: Symbol) -> &'a Expr<'a> {
2994 self.ctx.alloc_imperative_expr(Expr::Identifier(s))
2995 }
2996 #[inline]
2997 fn te_bin(&self, op: BinaryOpKind, left: &'a Expr<'a>, right: &'a Expr<'a>) -> &'a Expr<'a> {
2998 self.ctx
2999 .alloc_imperative_expr(Expr::BinaryOp { op, left, right })
3000 }
3001 #[inline]
3002 fn te_index(&self, collection: &'a Expr<'a>, index: &'a Expr<'a>) -> &'a Expr<'a> {
3003 self.ctx
3004 .alloc_imperative_expr(Expr::Index { collection, index })
3005 }
3006 #[inline]
3007 fn te_block(&self, stmts: Vec<Stmt<'a>>) -> &'a [Stmt<'a>] {
3008 self.alloc_block(stmts)
3009 }
3010
3011 fn take_int_literal(&mut self) -> Option<i64> {
3013 if let TokenType::Number(sym) = &self.peek().kind {
3014 let sym = *sym;
3015 let s = self.interner.resolve(sym).to_string();
3016 if let Ok(n) = self.parse_i64_numeral_signed(&s, false) {
3017 self.advance();
3018 return Some(n);
3019 }
3020 }
3021 None
3022 }
3023
3024 fn take_epsilon(&mut self, norm: &str) -> ParseResult<f64> {
3027 if !(self.check_word("with") && self.check_word_at(self.current + 1, "epsilon")) {
3028 return Ok(1e-6);
3029 }
3030 self.advance();
3031 self.advance();
3032 let value = match self.peek().kind {
3033 TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3034 _ => None,
3035 };
3036 match value {
3037 Some(eps) if eps.is_finite() && eps > 0.0 => {
3038 self.advance();
3039 Ok(eps)
3040 }
3041 _ => Err(ParseError {
3042 kind: ParseErrorKind::Custom(format!(
3043 "`{norm} … with epsilon E` — E must be a positive number."
3044 )),
3045 span: self.current_span(),
3046 }),
3047 }
3048 }
3049
3050 fn take_window(&mut self) -> ParseResult<Option<Window>> {
3054 if !self.check_word("within") {
3055 return Ok(None);
3056 }
3057 self.advance();
3058 let (words, form): (&[&str], &str) = if self.check_word("chunks") {
3059 (&["chunks", "of"], "within chunks of C")
3060 } else {
3061 (&["a", "window", "of"], "within a window of W")
3062 };
3063 for w in words {
3064 if !self.check_word(w) {
3065 return Err(ParseError {
3066 kind: ParseErrorKind::Custom(format!("`attention … {form}` — expected `{w}`.")),
3067 span: self.current_span(),
3068 });
3069 }
3070 self.advance();
3071 }
3072 match self.take_int_literal() {
3073 Some(n) if n > 0 && words[0] == "chunks" => Ok(Some(Window::Chunked(n))),
3074 Some(n) if n > 0 => Ok(Some(Window::Sliding(n))),
3075 _ => Err(ParseError {
3076 kind: ParseErrorKind::Custom(format!(
3077 "`attention … {form}` — {} must be a positive whole number of positions.",
3078 &form[form.len() - 1..]
3079 )),
3080 span: self.current_span(),
3081 }),
3082 }
3083 }
3084
3085 fn take_sinks(&mut self, heads: i64) -> ParseResult<Option<Symbol>> {
3088 if !(self.check_word("with") && self.check_word_at(self.current + 1, "sinks")) {
3089 return Ok(None);
3090 }
3091 self.advance();
3092 self.advance();
3093 let sym = self.peek().lexeme;
3094 let Some(&(r, c)) = self.tensor_shapes.get(&sym) else {
3095 return Err(ParseError {
3096 kind: ParseErrorKind::Custom(
3097 "`attention … with sinks S` — S must be a declared tensor of a logit per query head.".to_string(),
3098 ),
3099 span: self.current_span(),
3100 });
3101 };
3102 if (r, c) != (1, heads) {
3103 return Err(ParseError {
3104 kind: ParseErrorKind::Custom(format!(
3105 "`attention … with sinks S` — S must be 1×{heads} (a logit per query head), and `{}` is {r}×{c}.",
3106 self.interner.resolve(sym)
3107 )),
3108 span: self.current_span(),
3109 });
3110 }
3111 self.advance();
3112 Ok(Some(sym))
3113 }
3114
3115 fn take_attention_scale(&mut self) -> ParseResult<Option<f64>> {
3118 if !self.check_word("scaled") {
3119 return Ok(None);
3120 }
3121 self.advance();
3122 if !self.check_word("by") {
3123 return Err(ParseError {
3124 kind: ParseErrorKind::Custom("`attention … scaled by S` — expected `by` after `scaled`.".to_string()),
3125 span: self.current_span(),
3126 });
3127 }
3128 self.advance();
3129 let v = match self.peek().kind {
3130 TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3131 _ => None,
3132 };
3133 match v.filter(|v| v.is_finite() && *v > 0.0) {
3134 Some(v) => {
3135 self.advance();
3136 Ok(Some(v))
3137 }
3138 None => Err(ParseError {
3139 kind: ParseErrorKind::Custom("`attention … scaled by S` — S must be a positive number.".to_string()),
3140 span: self.current_span(),
3141 }),
3142 }
3143 }
3144
3145 fn take_cap(&mut self, what: &str) -> ParseResult<Option<f64>> {
3148 if !self.check_word("capped") {
3149 return Ok(None);
3150 }
3151 self.advance();
3152 if !self.check_word("at") {
3153 return Err(ParseError {
3154 kind: ParseErrorKind::Custom(format!("`{what} capped at C` — expected `at` after `capped`.")),
3155 span: self.current_span(),
3156 });
3157 }
3158 self.advance();
3159 let v = match self.peek().kind {
3160 TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3161 _ => None,
3162 };
3163 match v.filter(|v| v.is_finite() && *v > 0.0) {
3164 Some(v) => {
3165 self.advance();
3166 Ok(Some(v))
3167 }
3168 None => Err(ParseError {
3169 kind: ParseErrorKind::Custom(format!("`{what} capped at C` — C must be a positive number.")),
3170 span: self.current_span(),
3171 }),
3172 }
3173 }
3174
3175 fn take_across_heads(&mut self, what: &str) -> ParseResult<Option<i64>> {
3177 if !self.check_word("across") {
3178 return Ok(None);
3179 }
3180 self.advance();
3181 let h = self.take_int_literal().filter(|h| *h > 0);
3182 if h.is_none() || !self.check_word("heads") {
3183 return Err(ParseError {
3184 kind: ParseErrorKind::Custom(format!("`{what} across H heads` — H must be a positive whole number of heads.")),
3185 span: self.current_span(),
3186 });
3187 }
3188 self.advance();
3189 Ok(h)
3190 }
3191
3192 fn take_moe_activation(&mut self) -> ParseResult<MoeAct> {
3195 if !self.check_word("using") {
3196 return Ok(MoeAct::Swiglu);
3197 }
3198 self.advance();
3199 let clause = "`moe … using a swiglu clamped at L with slope A`";
3200 let want = |s: &mut Self, w: &str| -> ParseResult<()> {
3201 if !s.check_word(w) {
3202 return Err(ParseError {
3203 kind: ParseErrorKind::Custom(format!("{clause} — expected `{w}`.")),
3204 span: s.current_span(),
3205 });
3206 }
3207 s.advance();
3208 Ok(())
3209 };
3210 let number = |s: &mut Self, what: &str| -> ParseResult<f64> {
3211 let v = match s.peek().kind {
3212 TokenType::Number(sym) => s.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3213 _ => None,
3214 };
3215 match v.filter(|v| v.is_finite() && *v > 0.0) {
3216 Some(v) => {
3217 s.advance();
3218 Ok(v)
3219 }
3220 None => Err(ParseError {
3221 kind: ParseErrorKind::Custom(format!("{clause} — {what} must be a positive number.")),
3222 span: s.current_span(),
3223 }),
3224 }
3225 };
3226 for w in ["a", "swiglu", "clamped", "at"] {
3227 want(self, w)?;
3228 }
3229 let limit = number(self, "L")?;
3230 want(self, "with")?;
3231 want(self, "slope")?;
3232 let slope = number(self, "A")?;
3233 Ok(MoeAct::ClampedSwiglu { limit, slope })
3234 }
3235
3236 fn bind_tensor(&mut self, var: Symbol, shape: (i64, i64), int: bool) {
3241 self.tensor_shapes.insert(var, shape);
3242 if int {
3243 self.tensor_int.insert(var);
3244 } else {
3245 self.tensor_int.remove(&var);
3246 }
3247 }
3248
3249 fn new_tensor_seq(&mut self, int: bool) -> &'a Expr<'a> {
3251 let seq_sym = self.interner.intern("Seq");
3252 let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
3253 self.ctx.alloc_imperative_expr(Expr::New {
3254 type_name: seq_sym,
3255 type_args: vec![TypeExpr::Named(elem_sym)],
3256 init_fields: vec![],
3257 })
3258 }
3259
3260 fn tensor_zero(&mut self, int: bool) -> &'a Expr<'a> {
3262 if int {
3263 self.te_int(0)
3264 } else {
3265 self.ctx
3266 .alloc_imperative_expr(Expr::Literal(Literal::Float(0.0)))
3267 }
3268 }
3269
3270 fn desugar_tensor_new(
3271 &mut self,
3272 var: Symbol,
3273 rows: i64,
3274 cols: i64,
3275 is_float: bool,
3276 ) -> Stmt<'a> {
3277 let new_seq = self.new_tensor_seq(!is_float);
3278 let zero = self.tensor_zero(!is_float);
3279 let ctr = self.fresh_desugar_temp("tfill");
3280 let fill_body = self.te_block(vec![
3281 Stmt::Push {
3282 value: zero,
3283 collection: self.te_id(var),
3284 },
3285 Stmt::Set {
3286 target: ctr,
3287 value: self.te_bin(BinaryOpKind::Add, self.te_id(ctr), self.te_int(1)),
3288 },
3289 ]);
3290 let fill_loop = Stmt::While {
3291 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ctr), self.te_int(rows * cols)),
3292 body: fill_body,
3293 decreasing: None,
3294 };
3295 let body = self.te_block(vec![
3296 Stmt::Let {
3297 var,
3298 ty: None,
3299 value: new_seq,
3300 mutable: true,
3301 },
3302 Stmt::Let {
3303 var: ctr,
3304 ty: None,
3305 value: self.te_int(0),
3306 mutable: true,
3307 },
3308 fill_loop,
3309 ]);
3310 Stmt::Splice { body }
3311 }
3312
3313 fn desugar_tensor_matmul(
3318 &mut self,
3319 var: Symbol,
3320 a: Symbol,
3321 b: Symbol,
3322 m: i64,
3323 k: i64,
3324 n: i64,
3325 int: bool,
3326 ) -> Stmt<'a> {
3327 let new_seq = self.new_tensor_seq(int);
3328 let zero = self.tensor_zero(int);
3329 let fctr = self.fresh_desugar_temp("cfill");
3330 let (vi, vk, vj) = (
3331 self.fresh_desugar_temp("mm_i"),
3332 self.fresh_desugar_temp("mm_k"),
3333 self.fresh_desugar_temp("mm_j"),
3334 );
3335 let fill_body = self.te_block(vec![
3337 Stmt::Push {
3338 value: zero,
3339 collection: self.te_id(var),
3340 },
3341 Stmt::Set {
3342 target: fctr,
3343 value: self.te_bin(BinaryOpKind::Add, self.te_id(fctr), self.te_int(1)),
3344 },
3345 ]);
3346 let fill_loop = Stmt::While {
3347 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(fctr), self.te_int(m * n)),
3348 body: fill_body,
3349 decreasing: None,
3350 };
3351 let cidx = self.te_bin(
3353 BinaryOpKind::Add,
3354 self.te_bin(
3355 BinaryOpKind::Add,
3356 self.te_bin(BinaryOpKind::Multiply, self.te_id(vi), self.te_int(n)),
3357 self.te_id(vj),
3358 ),
3359 self.te_int(1),
3360 );
3361 let aidx = self.te_bin(
3362 BinaryOpKind::Add,
3363 self.te_bin(
3364 BinaryOpKind::Add,
3365 self.te_bin(BinaryOpKind::Multiply, self.te_id(vi), self.te_int(k)),
3366 self.te_id(vk),
3367 ),
3368 self.te_int(1),
3369 );
3370 let bidx = self.te_bin(
3371 BinaryOpKind::Add,
3372 self.te_bin(
3373 BinaryOpKind::Add,
3374 self.te_bin(BinaryOpKind::Multiply, self.te_id(vk), self.te_int(n)),
3375 self.te_id(vj),
3376 ),
3377 self.te_int(1),
3378 );
3379 let prod = self.te_bin(
3380 BinaryOpKind::Multiply,
3381 self.te_index(self.te_id(a), aidx),
3382 self.te_index(self.te_id(b), bidx),
3383 );
3384 let acc = self.te_bin(
3385 BinaryOpKind::Add,
3386 self.te_index(self.te_id(var), cidx),
3387 prod,
3388 );
3389 let set_c = Stmt::SetIndex {
3390 collection: self.te_id(var),
3391 index: cidx,
3392 value: acc,
3393 };
3394 let j_body = self.te_block(vec![
3396 set_c,
3397 Stmt::Set {
3398 target: vj,
3399 value: self.te_bin(BinaryOpKind::Add, self.te_id(vj), self.te_int(1)),
3400 },
3401 ]);
3402 let j_loop = Stmt::While {
3403 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vj), self.te_int(n)),
3404 body: j_body,
3405 decreasing: None,
3406 };
3407 let k_body = self.te_block(vec![
3409 Stmt::Let {
3410 var: vj,
3411 ty: None,
3412 value: self.te_int(0),
3413 mutable: true,
3414 },
3415 j_loop,
3416 Stmt::Set {
3417 target: vk,
3418 value: self.te_bin(BinaryOpKind::Add, self.te_id(vk), self.te_int(1)),
3419 },
3420 ]);
3421 let k_loop = Stmt::While {
3422 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vk), self.te_int(k)),
3423 body: k_body,
3424 decreasing: None,
3425 };
3426 let i_body = self.te_block(vec![
3428 Stmt::Let {
3429 var: vk,
3430 ty: None,
3431 value: self.te_int(0),
3432 mutable: true,
3433 },
3434 k_loop,
3435 Stmt::Set {
3436 target: vi,
3437 value: self.te_bin(BinaryOpKind::Add, self.te_id(vi), self.te_int(1)),
3438 },
3439 ]);
3440 let i_loop = Stmt::While {
3441 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vi), self.te_int(m)),
3442 body: i_body,
3443 decreasing: None,
3444 };
3445 let body = self.te_block(vec![
3446 Stmt::Let {
3447 var,
3448 ty: None,
3449 value: new_seq,
3450 mutable: true,
3451 },
3452 Stmt::Let {
3453 var: fctr,
3454 ty: None,
3455 value: self.te_int(0),
3456 mutable: true,
3457 },
3458 fill_loop,
3459 Stmt::Let {
3460 var: vi,
3461 ty: None,
3462 value: self.te_int(0),
3463 mutable: true,
3464 },
3465 i_loop,
3466 ]);
3467 Stmt::Splice { body }
3468 }
3469
3470 fn desugar_tensor_elementwise(
3475 &mut self,
3476 var: Symbol,
3477 a: Symbol,
3478 b: Symbol,
3479 total: i64,
3480 op: BinaryOpKind,
3481 int: bool,
3482 row: Option<i64>,
3483 ) -> Stmt<'a> {
3484 let new_seq = self.new_tensor_seq(int);
3485 let p = self.fresh_desugar_temp("ew_p");
3486 let b_at = match row {
3488 Some(w) => self.te_bin(
3489 BinaryOpKind::Add,
3490 self.te_bin(
3491 BinaryOpKind::Modulo,
3492 self.te_bin(BinaryOpKind::Subtract, self.te_id(p), self.te_int(1)),
3493 self.te_int(w),
3494 ),
3495 self.te_int(1),
3496 ),
3497 None => self.te_id(p),
3498 };
3499 let elem = self.te_bin(
3500 op,
3501 self.te_index(self.te_id(a), self.te_id(p)),
3502 self.te_index(self.te_id(b), b_at),
3503 );
3504 let loop_body = self.te_block(vec![
3505 Stmt::Push {
3506 value: elem,
3507 collection: self.te_id(var),
3508 },
3509 Stmt::Set {
3510 target: p,
3511 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3512 },
3513 ]);
3514 let loop_stmt = Stmt::While {
3515 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3516 body: loop_body,
3517 decreasing: None,
3518 };
3519 let body = self.te_block(vec![
3520 Stmt::Let {
3521 var,
3522 ty: None,
3523 value: new_seq,
3524 mutable: true,
3525 },
3526 Stmt::Let {
3527 var: p,
3528 ty: None,
3529 value: self.te_int(1),
3530 mutable: true,
3531 },
3532 loop_stmt,
3533 ]);
3534 Stmt::Splice { body }
3535 }
3536
3537 fn desugar_tensor_scale(&mut self, var: Symbol, x: Symbol, total: i64, factor: f64) -> Stmt<'a> {
3539 let new_seq = self.new_tensor_seq(false);
3540 let p = self.fresh_desugar_temp("sc_p");
3541 let elem = self.te_bin(BinaryOpKind::Multiply, self.te_index(self.te_id(x), self.te_id(p)), self.te_float(factor));
3542 let loop_body = self.te_block(vec![
3543 Stmt::Push {
3544 value: elem,
3545 collection: self.te_id(var),
3546 },
3547 Stmt::Set {
3548 target: p,
3549 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3550 },
3551 ]);
3552 let body = self.te_block(vec![
3553 Stmt::Let {
3554 var,
3555 ty: None,
3556 value: new_seq,
3557 mutable: true,
3558 },
3559 Stmt::Let {
3560 var: p,
3561 ty: None,
3562 value: self.te_int(1),
3563 mutable: true,
3564 },
3565 Stmt::While {
3566 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3567 body: loop_body,
3568 decreasing: None,
3569 },
3570 ]);
3571 Stmt::Splice { body }
3572 }
3573
3574 fn capped(&self, x: &'a Expr<'a>, c: f64) -> &'a Expr<'a> {
3577 let e = self.te_bin(BinaryOpKind::Pow, self.te_float(std::f64::consts::E), self.te_bin(BinaryOpKind::Multiply, x, self.te_float(2.0 / c)));
3578 let fraction = self.te_bin(BinaryOpKind::Divide, self.te_float(2.0), self.te_bin(BinaryOpKind::Add, e, self.te_float(1.0)));
3579 self.te_bin(BinaryOpKind::Multiply, self.te_float(c), self.te_bin(BinaryOpKind::Subtract, self.te_float(1.0), fraction))
3580 }
3581
3582 fn desugar_tensor_cap(&mut self, var: Symbol, x: Symbol, total: i64, cap: f64) -> Stmt<'a> {
3584 let new_seq = self.new_tensor_seq(false);
3585 let p = self.fresh_desugar_temp("cp_p");
3586 let elem = self.capped(self.te_index(self.te_id(x), self.te_id(p)), cap);
3587 let loop_body = self.te_block(vec![
3588 Stmt::Push { value: elem, collection: self.te_id(var) },
3589 Stmt::Set { target: p, value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)) },
3590 ]);
3591 let body = self.te_block(vec![
3592 Stmt::Let { var, ty: None, value: new_seq, mutable: true },
3593 Stmt::Let { var: p, ty: None, value: self.te_int(1), mutable: true },
3594 Stmt::While {
3595 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3596 body: loop_body,
3597 decreasing: None,
3598 },
3599 ]);
3600 Stmt::Splice { body }
3601 }
3602
3603 fn part_copy(&mut self, var: Symbol, width: i64, read: impl Fn(&Self, Symbol) -> &'a Expr<'a>) -> [Stmt<'a>; 2] {
3606 let iv = self.fresh_desugar_temp("pc_i");
3607 let body = self.te_block(vec![
3608 Stmt::Push { value: read(self, iv), collection: self.te_id(var) },
3609 Stmt::Set { target: iv, value: self.te_bin(BinaryOpKind::Add, self.te_id(iv), self.te_int(1)) },
3610 ]);
3611 [
3612 Stmt::Let { var: iv, ty: None, value: self.te_int(1), mutable: true },
3613 Stmt::While {
3614 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(iv), self.te_int(width)),
3615 body,
3616 decreasing: None,
3617 },
3618 ]
3619 }
3620
3621 fn counted(&self, iv: Symbol, bound: i64, mut body: Vec<Stmt<'a>>) -> [Stmt<'a>; 2] {
3623 body.push(Stmt::Set { target: iv, value: self.te_bin(BinaryOpKind::Add, self.te_id(iv), self.te_int(1)) });
3624 [
3625 Stmt::Let { var: iv, ty: None, value: self.te_int(0), mutable: true },
3626 Stmt::While {
3627 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(iv), self.te_int(bound)),
3628 body: self.te_block(body),
3629 decreasing: None,
3630 },
3631 ]
3632 }
3633
3634 fn desugar_tensor_head_slice(
3637 &mut self,
3638 var: Symbol,
3639 x: Symbol,
3640 groups: i64,
3641 every: i64,
3642 from: i64,
3643 width: i64,
3644 int: bool,
3645 ) -> Stmt<'a> {
3646 let new_seq = self.new_tensor_seq(int);
3647 let g = self.fresh_desugar_temp("hs_g");
3648 let copy = self.part_copy(var, width, |s, iv| {
3649 let at = s.te_bin(BinaryOpKind::Add, s.te_bin(BinaryOpKind::Multiply, s.te_id(g), s.te_int(every)), s.te_id(iv));
3650 s.te_index(s.te_id(x), s.te_bin(BinaryOpKind::Add, at, s.te_int(from)))
3651 });
3652 let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3653 stmts.extend(self.counted(g, groups, Vec::from(copy)));
3654 Stmt::Splice { body: self.te_block(stmts) }
3655 }
3656
3657 #[allow(clippy::too_many_arguments)]
3661 fn desugar_tensor_join(
3662 &mut self,
3663 var: Symbol,
3664 a: Symbol,
3665 b: Symbol,
3666 rows: i64,
3667 heads: i64,
3668 ha: i64,
3669 hb: i64,
3670 int: bool,
3671 ) -> Stmt<'a> {
3672 let new_seq = self.new_tensor_seq(int);
3673 let (r, h) = (self.fresh_desugar_temp("jn_r"), self.fresh_desugar_temp("jn_h"));
3674 let part = |s: &Self, t: Symbol, width: i64, iv: Symbol| {
3675 let row = s.te_bin(BinaryOpKind::Multiply, s.te_id(r), s.te_int(heads * width));
3676 let head = s.te_bin(BinaryOpKind::Multiply, s.te_id(h), s.te_int(width));
3677 s.te_index(s.te_id(t), s.te_bin(BinaryOpKind::Add, s.te_bin(BinaryOpKind::Add, row, head), s.te_id(iv)))
3678 };
3679 let mut per_head = Vec::from(self.part_copy(var, ha, |s, iv| part(s, a, ha, iv)));
3680 per_head.extend(self.part_copy(var, hb, |s, iv| part(s, b, hb, iv)));
3681 let per_row = Vec::from(self.counted(h, heads, per_head));
3682 let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3683 stmts.extend(self.counted(r, rows, per_row));
3684 Stmt::Splice { body: self.te_block(stmts) }
3685 }
3686
3687 #[allow(clippy::too_many_arguments)]
3692 fn desugar_tensor_head_matmul(
3693 &mut self,
3694 var: Symbol,
3695 x: Symbol,
3696 w: Symbol,
3697 m: i64,
3698 heads: i64,
3699 a: i64,
3700 b: i64,
3701 int: bool,
3702 ) -> Stmt<'a> {
3703 let new_seq = self.new_tensor_seq(int);
3704 let zero = self.tensor_zero(int);
3705 let fill = self.fresh_desugar_temp("hm_fill");
3706 let (t, h, e, r) = (
3707 self.fresh_desugar_temp("hm_t"),
3708 self.fresh_desugar_temp("hm_h"),
3709 self.fresh_desugar_temp("hm_e"),
3710 self.fresh_desugar_temp("hm_r"),
3711 );
3712 let id = |s: Symbol| self.te_id(s);
3713 let it = |n: i64| self.te_int(n);
3714 let add = |l: &'a Expr<'a>, r: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, l, r);
3715 let mul = |l: &'a Expr<'a>, r: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, l, r);
3716 let y_at = add(add(add(mul(id(t), it(heads * b)), mul(id(h), it(b))), id(r)), it(1));
3717 let x_at = add(add(add(mul(id(t), it(heads * a)), mul(id(h), it(a))), id(e)), it(1));
3718 let w_at = add(add(mul(add(mul(id(h), it(a)), id(e)), it(b)), id(r)), it(1));
3719 let acc = add(
3720 self.te_index(id(var), y_at),
3721 mul(self.te_index(id(x), x_at), self.te_index(id(w), w_at)),
3722 );
3723 let set = Stmt::SetIndex { collection: id(var), index: y_at, value: acc };
3724 let per_r = Vec::from(self.counted(r, b, vec![set]));
3725 let per_e = Vec::from(self.counted(e, a, per_r));
3726 let per_h = Vec::from(self.counted(h, heads, per_e));
3727 let nest = self.counted(t, m, per_h);
3728 let filled = self.counted(fill, m * heads * b, vec![Stmt::Push { value: zero, collection: id(var) }]);
3729 let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3730 stmts.extend(filled);
3731 stmts.extend(nest);
3732 Stmt::Splice { body: self.te_block(stmts) }
3733 }
3734
3735 fn desugar_tensor_map(
3739 &mut self,
3740 var: Symbol,
3741 x: Symbol,
3742 total: i64,
3743 map: TensorElemMap,
3744 int: bool,
3745 ) -> Stmt<'a> {
3746 let new_seq = self.new_tensor_seq(int);
3747 let p = self.fresh_desugar_temp("em_p");
3748 let xp = self.te_index(self.te_id(x), self.te_id(p));
3749 let elem = match map {
3750 TensorElemMap::Requant { m, s } => self.te_bin(
3751 BinaryOpKind::Shr,
3752 self.te_bin(BinaryOpKind::Multiply, xp, self.te_int(m)),
3753 self.te_int(s),
3754 ),
3755 TensorElemMap::Clamp { lo, hi } => {
3756 let (min_sym, max_sym) = (self.interner.intern("min"), self.interner.intern("max"));
3757 let lower = self.te_call(max_sym, vec![xp, self.te_int(lo)]);
3758 self.te_call(min_sym, vec![lower, self.te_int(hi)])
3759 }
3760 TensorElemMap::Relu => {
3761 let max_sym = self.interner.intern("max");
3762 let zero = self.tensor_zero(int);
3763 self.te_call(max_sym, vec![xp, zero])
3764 }
3765 };
3766 let loop_body = self.te_block(vec![
3767 Stmt::Push {
3768 value: elem,
3769 collection: self.te_id(var),
3770 },
3771 Stmt::Set {
3772 target: p,
3773 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3774 },
3775 ]);
3776 let loop_stmt = Stmt::While {
3777 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3778 body: loop_body,
3779 decreasing: None,
3780 };
3781 let body = self.te_block(vec![
3782 Stmt::Let {
3783 var,
3784 ty: None,
3785 value: new_seq,
3786 mutable: true,
3787 },
3788 Stmt::Let {
3789 var: p,
3790 ty: None,
3791 value: self.te_int(1),
3792 mutable: true,
3793 },
3794 loop_stmt,
3795 ]);
3796 Stmt::Splice { body }
3797 }
3798
3799 fn desugar_tensor_rmsnorm(&mut self, var: Symbol, x: Symbol, total: i64, eps: f64) -> Stmt<'a> {
3802 let sqrt_sym = self.interner.intern("sqrt");
3803 let seq_sym = self.interner.intern("Seq");
3804 let real_sym = self.interner.intern("Real");
3805 let ss = self.fresh_desugar_temp("rn_ss");
3806 let p = self.fresh_desugar_temp("rn_p");
3807 let denom = self.fresh_desugar_temp("rn_d");
3808 let q = self.fresh_desugar_temp("rn_q");
3809 let sq = self.te_bin(
3811 BinaryOpKind::Multiply,
3812 self.te_index(self.te_id(x), self.te_id(p)),
3813 self.te_index(self.te_id(x), self.te_id(p)),
3814 );
3815 let ss_body = self.te_block(vec![
3816 Stmt::Set {
3817 target: ss,
3818 value: self.te_bin(BinaryOpKind::Add, self.te_id(ss), sq),
3819 },
3820 Stmt::Set {
3821 target: p,
3822 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3823 },
3824 ]);
3825 let ss_loop = Stmt::While {
3826 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3827 body: ss_body,
3828 decreasing: None,
3829 };
3830 let mean = self.te_bin(
3832 BinaryOpKind::Divide,
3833 self.te_id(ss),
3834 self.te_float(total as f64),
3835 );
3836 let mean_eps = self.te_bin(BinaryOpKind::Add, mean, self.te_float(eps));
3837 let denom_expr = self.te_call(sqrt_sym, vec![mean_eps]);
3838 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
3839 type_name: seq_sym,
3840 type_args: vec![TypeExpr::Named(real_sym)],
3841 init_fields: vec![],
3842 });
3843 let norm = self.te_bin(
3845 BinaryOpKind::Divide,
3846 self.te_index(self.te_id(x), self.te_id(q)),
3847 self.te_id(denom),
3848 );
3849 let n_body = self.te_block(vec![
3850 Stmt::Push {
3851 value: norm,
3852 collection: self.te_id(var),
3853 },
3854 Stmt::Set {
3855 target: q,
3856 value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
3857 },
3858 ]);
3859 let n_loop = Stmt::While {
3860 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(total)),
3861 body: n_body,
3862 decreasing: None,
3863 };
3864 let body = self.te_block(vec![
3865 Stmt::Let {
3866 var: ss,
3867 ty: None,
3868 value: self.te_float(0.0),
3869 mutable: true,
3870 },
3871 Stmt::Let {
3872 var: p,
3873 ty: None,
3874 value: self.te_int(1),
3875 mutable: true,
3876 },
3877 ss_loop,
3878 Stmt::Let {
3879 var: denom,
3880 ty: None,
3881 value: denom_expr,
3882 mutable: false,
3883 },
3884 Stmt::Let {
3885 var,
3886 ty: None,
3887 value: new_seq,
3888 mutable: true,
3889 },
3890 Stmt::Let {
3891 var: q,
3892 ty: None,
3893 value: self.te_int(1),
3894 mutable: true,
3895 },
3896 n_loop,
3897 ]);
3898 Stmt::Splice { body }
3899 }
3900
3901 #[allow(clippy::too_many_arguments)]
3909 fn desugar_tensor_rmsnorm_weighted(
3910 &mut self,
3911 var: Symbol,
3912 x: Symbol,
3913 residual: Option<Symbol>,
3914 weight: Symbol,
3915 rows: i64,
3916 cols: i64,
3917 eps: f64,
3918 weight_rows: i64,
3919 ) -> Stmt<'a> {
3920 let sqrt_sym = self.interner.intern("sqrt");
3921 let seq_sym = self.interner.intern("Seq");
3922 let real_sym = self.interner.intern("Real");
3923 let r = self.fresh_desugar_temp("wr_r");
3924 let ss = self.fresh_desugar_temp("wr_ss");
3925 let i = self.fresh_desugar_temp("wr_i");
3926 let denom = self.fresh_desugar_temp("wr_d");
3927 let j = self.fresh_desugar_temp("wr_j");
3928 let idx = |s: &Self, inner: Symbol| {
3930 s.te_bin(
3931 BinaryOpKind::Add,
3932 s.te_bin(
3933 BinaryOpKind::Multiply,
3934 s.te_bin(BinaryOpKind::Subtract, s.te_id(r), s.te_int(1)),
3935 s.te_int(cols),
3936 ),
3937 s.te_id(inner),
3938 )
3939 };
3940 let elem = |s: &Self, inner: Symbol| {
3942 let xe = s.te_index(s.te_id(x), idx(s, inner));
3943 match residual {
3944 Some(rz) => s.te_bin(
3945 BinaryOpKind::Add,
3946 xe,
3947 s.te_index(s.te_id(rz), idx(s, inner)),
3948 ),
3949 None => xe,
3950 }
3951 };
3952 let xi = elem(self, i);
3954 let xi2 = elem(self, i);
3955 let ss_body = self.te_block(vec![
3956 Stmt::Set {
3957 target: ss,
3958 value: self.te_bin(
3959 BinaryOpKind::Add,
3960 self.te_id(ss),
3961 self.te_bin(BinaryOpKind::Multiply, xi, xi2),
3962 ),
3963 },
3964 Stmt::Set {
3965 target: i,
3966 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
3967 },
3968 ]);
3969 let ss_loop = Stmt::While {
3970 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
3971 body: ss_body,
3972 decreasing: None,
3973 };
3974 let mean = self.te_bin(
3976 BinaryOpKind::Divide,
3977 self.te_id(ss),
3978 self.te_float(cols as f64),
3979 );
3980 let mean_eps = self.te_bin(BinaryOpKind::Add, mean, self.te_float(eps));
3981 let denom_expr = self.te_call(sqrt_sym, vec![mean_eps]);
3982 let xj = elem(self, j);
3984 let normed = self.te_bin(BinaryOpKind::Divide, xj, self.te_id(denom));
3985 let w_at = if weight_rows > 1 {
3986 self.te_bin(
3987 BinaryOpKind::Add,
3988 self.te_bin(
3989 BinaryOpKind::Multiply,
3990 self.te_bin(
3991 BinaryOpKind::Modulo,
3992 self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
3993 self.te_int(weight_rows),
3994 ),
3995 self.te_int(cols),
3996 ),
3997 self.te_id(j),
3998 )
3999 } else {
4000 self.te_id(j)
4001 };
4002 let weighted = self.te_bin(
4003 BinaryOpKind::Multiply,
4004 normed,
4005 self.te_index(self.te_id(weight), w_at),
4006 );
4007 let n_body = self.te_block(vec![
4008 Stmt::Push {
4009 value: weighted,
4010 collection: self.te_id(var),
4011 },
4012 Stmt::Set {
4013 target: j,
4014 value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
4015 },
4016 ]);
4017 let n_loop = Stmt::While {
4018 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(j), self.te_int(cols)),
4019 body: n_body,
4020 decreasing: None,
4021 };
4022 let row_body = self.te_block(vec![
4024 Stmt::Set {
4025 target: ss,
4026 value: self.te_float(0.0),
4027 },
4028 Stmt::Set {
4029 target: i,
4030 value: self.te_int(1),
4031 },
4032 ss_loop,
4033 Stmt::Set {
4034 target: denom,
4035 value: denom_expr,
4036 },
4037 Stmt::Set {
4038 target: j,
4039 value: self.te_int(1),
4040 },
4041 n_loop,
4042 Stmt::Set {
4043 target: r,
4044 value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
4045 },
4046 ]);
4047 let row_loop = Stmt::While {
4048 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
4049 body: row_body,
4050 decreasing: None,
4051 };
4052 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4053 type_name: seq_sym,
4054 type_args: vec![TypeExpr::Named(real_sym)],
4055 init_fields: vec![],
4056 });
4057 let body = self.te_block(vec![
4058 Stmt::Let {
4059 var,
4060 ty: None,
4061 value: new_seq,
4062 mutable: true,
4063 },
4064 Stmt::Let {
4065 var: r,
4066 ty: None,
4067 value: self.te_int(1),
4068 mutable: true,
4069 },
4070 Stmt::Let {
4071 var: ss,
4072 ty: None,
4073 value: self.te_float(0.0),
4074 mutable: true,
4075 },
4076 Stmt::Let {
4077 var: i,
4078 ty: None,
4079 value: self.te_int(1),
4080 mutable: true,
4081 },
4082 Stmt::Let {
4083 var: denom,
4084 ty: None,
4085 value: self.te_float(0.0),
4086 mutable: true,
4087 },
4088 Stmt::Let {
4089 var: j,
4090 ty: None,
4091 value: self.te_int(1),
4092 mutable: true,
4093 },
4094 row_loop,
4095 ]);
4096 Stmt::Splice { body }
4097 }
4098
4099 fn desugar_tensor_layernorm(
4104 &mut self,
4105 var: Symbol,
4106 x: Symbol,
4107 weight: Symbol,
4108 bias: Symbol,
4109 rows: i64,
4110 cols: i64,
4111 eps: f64,
4112 ) -> Stmt<'a> {
4113 let sqrt_sym = self.interner.intern("sqrt");
4114 let seq_sym = self.interner.intern("Seq");
4115 let real_sym = self.interner.intern("Real");
4116 let r = self.fresh_desugar_temp("ln_r");
4117 let sm = self.fresh_desugar_temp("ln_sm");
4118 let mean = self.fresh_desugar_temp("ln_mean");
4119 let vs = self.fresh_desugar_temp("ln_vs");
4120 let denom = self.fresh_desugar_temp("ln_d");
4121 let i = self.fresh_desugar_temp("ln_i");
4122 let j = self.fresh_desugar_temp("ln_j");
4123 let idx = |s: &Self, inner: Symbol| {
4124 s.te_bin(
4125 BinaryOpKind::Add,
4126 s.te_bin(
4127 BinaryOpKind::Multiply,
4128 s.te_bin(BinaryOpKind::Subtract, s.te_id(r), s.te_int(1)),
4129 s.te_int(cols),
4130 ),
4131 s.te_id(inner),
4132 )
4133 };
4134 let sum_body = self.te_block(vec![
4136 Stmt::Set {
4137 target: sm,
4138 value: self.te_bin(
4139 BinaryOpKind::Add,
4140 self.te_id(sm),
4141 self.te_index(self.te_id(x), idx(self, i)),
4142 ),
4143 },
4144 Stmt::Set {
4145 target: i,
4146 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4147 },
4148 ]);
4149 let sum_loop = Stmt::While {
4150 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
4151 body: sum_body,
4152 decreasing: None,
4153 };
4154 let mean_expr = self.te_bin(
4155 BinaryOpKind::Divide,
4156 self.te_id(sm),
4157 self.te_float(cols as f64),
4158 );
4159 let d1 = self.te_bin(
4161 BinaryOpKind::Subtract,
4162 self.te_index(self.te_id(x), idx(self, i)),
4163 self.te_id(mean),
4164 );
4165 let d2 = self.te_bin(
4166 BinaryOpKind::Subtract,
4167 self.te_index(self.te_id(x), idx(self, i)),
4168 self.te_id(mean),
4169 );
4170 let var_body = self.te_block(vec![
4171 Stmt::Set {
4172 target: vs,
4173 value: self.te_bin(
4174 BinaryOpKind::Add,
4175 self.te_id(vs),
4176 self.te_bin(BinaryOpKind::Multiply, d1, d2),
4177 ),
4178 },
4179 Stmt::Set {
4180 target: i,
4181 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4182 },
4183 ]);
4184 let var_loop = Stmt::While {
4185 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
4186 body: var_body,
4187 decreasing: None,
4188 };
4189 let var_mean = self.te_bin(
4190 BinaryOpKind::Divide,
4191 self.te_id(vs),
4192 self.te_float(cols as f64),
4193 );
4194 let var_eps = self.te_bin(BinaryOpKind::Add, var_mean, self.te_float(eps));
4195 let denom_expr = self.te_call(sqrt_sym, vec![var_eps]);
4196 let centered = self.te_bin(
4198 BinaryOpKind::Subtract,
4199 self.te_index(self.te_id(x), idx(self, j)),
4200 self.te_id(mean),
4201 );
4202 let normed = self.te_bin(BinaryOpKind::Divide, centered, self.te_id(denom));
4203 let scaled = self.te_bin(
4204 BinaryOpKind::Multiply,
4205 normed,
4206 self.te_index(self.te_id(weight), self.te_id(j)),
4207 );
4208 let shifted = self.te_bin(
4209 BinaryOpKind::Add,
4210 scaled,
4211 self.te_index(self.te_id(bias), self.te_id(j)),
4212 );
4213 let n_body = self.te_block(vec![
4214 Stmt::Push {
4215 value: shifted,
4216 collection: self.te_id(var),
4217 },
4218 Stmt::Set {
4219 target: j,
4220 value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
4221 },
4222 ]);
4223 let n_loop = Stmt::While {
4224 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(j), self.te_int(cols)),
4225 body: n_body,
4226 decreasing: None,
4227 };
4228 let row_body = self.te_block(vec![
4229 Stmt::Set {
4230 target: sm,
4231 value: self.te_float(0.0),
4232 },
4233 Stmt::Set {
4234 target: i,
4235 value: self.te_int(1),
4236 },
4237 sum_loop,
4238 Stmt::Set {
4239 target: mean,
4240 value: mean_expr,
4241 },
4242 Stmt::Set {
4243 target: vs,
4244 value: self.te_float(0.0),
4245 },
4246 Stmt::Set {
4247 target: i,
4248 value: self.te_int(1),
4249 },
4250 var_loop,
4251 Stmt::Set {
4252 target: denom,
4253 value: denom_expr,
4254 },
4255 Stmt::Set {
4256 target: j,
4257 value: self.te_int(1),
4258 },
4259 n_loop,
4260 Stmt::Set {
4261 target: r,
4262 value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
4263 },
4264 ]);
4265 let row_loop = Stmt::While {
4266 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
4267 body: row_body,
4268 decreasing: None,
4269 };
4270 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4271 type_name: seq_sym,
4272 type_args: vec![TypeExpr::Named(real_sym)],
4273 init_fields: vec![],
4274 });
4275 let body = self.te_block(vec![
4276 Stmt::Let {
4277 var,
4278 ty: None,
4279 value: new_seq,
4280 mutable: true,
4281 },
4282 Stmt::Let {
4283 var: r,
4284 ty: None,
4285 value: self.te_int(1),
4286 mutable: true,
4287 },
4288 Stmt::Let {
4289 var: sm,
4290 ty: None,
4291 value: self.te_float(0.0),
4292 mutable: true,
4293 },
4294 Stmt::Let {
4295 var: mean,
4296 ty: None,
4297 value: self.te_float(0.0),
4298 mutable: true,
4299 },
4300 Stmt::Let {
4301 var: vs,
4302 ty: None,
4303 value: self.te_float(0.0),
4304 mutable: true,
4305 },
4306 Stmt::Let {
4307 var: denom,
4308 ty: None,
4309 value: self.te_float(0.0),
4310 mutable: true,
4311 },
4312 Stmt::Let {
4313 var: i,
4314 ty: None,
4315 value: self.te_int(1),
4316 mutable: true,
4317 },
4318 Stmt::Let {
4319 var: j,
4320 ty: None,
4321 value: self.te_int(1),
4322 mutable: true,
4323 },
4324 row_loop,
4325 ]);
4326 Stmt::Splice { body }
4327 }
4328
4329 fn desugar_tensor_softmax(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4332 const E: f64 = std::f64::consts::E;
4333 let max_sym = self.interner.intern("max");
4334 let seq_sym = self.interner.intern("Seq");
4335 let real_sym = self.interner.intern("Real");
4336 let m = self.fresh_desugar_temp("sm_m");
4337 let p = self.fresh_desugar_temp("sm_p");
4338 let s = self.fresh_desugar_temp("sm_s");
4339 let p2 = self.fresh_desugar_temp("sm_p2");
4340 let q = self.fresh_desugar_temp("sm_q");
4341 let max_body = self.te_block(vec![
4343 Stmt::Set {
4344 target: m,
4345 value: self.te_call(
4346 max_sym,
4347 vec![self.te_id(m), self.te_index(self.te_id(x), self.te_id(p))],
4348 ),
4349 },
4350 Stmt::Set {
4351 target: p,
4352 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
4353 },
4354 ]);
4355 let max_loop = Stmt::While {
4356 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
4357 body: max_body,
4358 decreasing: None,
4359 };
4360 let expo = |s2: &Self, idx: Symbol| {
4362 s2.te_bin(
4363 BinaryOpKind::Pow,
4364 s2.te_float(E),
4365 s2.te_bin(
4366 BinaryOpKind::Subtract,
4367 s2.te_index(s2.te_id(x), s2.te_id(idx)),
4368 s2.te_id(m),
4369 ),
4370 )
4371 };
4372 let s_body = self.te_block(vec![
4374 Stmt::Set {
4375 target: s,
4376 value: self.te_bin(BinaryOpKind::Add, self.te_id(s), expo(self, p2)),
4377 },
4378 Stmt::Set {
4379 target: p2,
4380 value: self.te_bin(BinaryOpKind::Add, self.te_id(p2), self.te_int(1)),
4381 },
4382 ]);
4383 let s_loop = Stmt::While {
4384 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p2), self.te_int(total)),
4385 body: s_body,
4386 decreasing: None,
4387 };
4388 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4389 type_name: seq_sym,
4390 type_args: vec![TypeExpr::Named(real_sym)],
4391 init_fields: vec![],
4392 });
4393 let norm = self.te_bin(BinaryOpKind::Divide, expo(self, q), self.te_id(s));
4395 let n_body = self.te_block(vec![
4396 Stmt::Push {
4397 value: norm,
4398 collection: self.te_id(var),
4399 },
4400 Stmt::Set {
4401 target: q,
4402 value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
4403 },
4404 ]);
4405 let n_loop = Stmt::While {
4406 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(total)),
4407 body: n_body,
4408 decreasing: None,
4409 };
4410 let body = self.te_block(vec![
4411 Stmt::Let {
4412 var: m,
4413 ty: None,
4414 value: self.te_index(self.te_id(x), self.te_int(1)),
4415 mutable: true,
4416 },
4417 Stmt::Let {
4418 var: p,
4419 ty: None,
4420 value: self.te_int(2),
4421 mutable: true,
4422 },
4423 max_loop,
4424 Stmt::Let {
4425 var: s,
4426 ty: None,
4427 value: self.te_float(0.0),
4428 mutable: true,
4429 },
4430 Stmt::Let {
4431 var: p2,
4432 ty: None,
4433 value: self.te_int(1),
4434 mutable: true,
4435 },
4436 s_loop,
4437 Stmt::Let {
4438 var,
4439 ty: None,
4440 value: new_seq,
4441 mutable: true,
4442 },
4443 Stmt::Let {
4444 var: q,
4445 ty: None,
4446 value: self.te_int(1),
4447 mutable: true,
4448 },
4449 n_loop,
4450 ]);
4451 Stmt::Splice { body }
4452 }
4453
4454 fn desugar_tensor_softmax_rows(
4467 &mut self,
4468 var: Symbol,
4469 x: Symbol,
4470 rows: i64,
4471 cols: i64,
4472 ) -> Stmt<'a> {
4473 const E: f64 = std::f64::consts::E;
4474 let max_sym = self.interner.intern("max");
4475 let seq_sym = self.interner.intern("Seq");
4476 let real_sym = self.interner.intern("Real");
4477 let rr = self.fresh_desugar_temp("smr_r");
4478 let m = self.fresh_desugar_temp("smr_m");
4479 let p = self.fresh_desugar_temp("smr_p");
4480 let s = self.fresh_desugar_temp("smr_s");
4481 let p2 = self.fresh_desugar_temp("smr_p2");
4482 let q = self.fresh_desugar_temp("smr_q");
4483 let xi = |s2: &Self, i: Symbol| {
4485 let base = s2.te_bin(
4486 BinaryOpKind::Multiply,
4487 s2.te_bin(BinaryOpKind::Subtract, s2.te_id(rr), s2.te_int(1)),
4488 s2.te_int(cols),
4489 );
4490 s2.te_index(s2.te_id(x), s2.te_bin(BinaryOpKind::Add, base, s2.te_id(i)))
4491 };
4492 let expo = |s2: &Self, i: Symbol| {
4493 s2.te_bin(
4494 BinaryOpKind::Pow,
4495 s2.te_float(E),
4496 s2.te_bin(BinaryOpKind::Subtract, xi(s2, i), s2.te_id(m)),
4497 )
4498 };
4499 let bump = |s2: &Self, v: Symbol| Stmt::Set {
4500 target: v,
4501 value: s2.te_bin(BinaryOpKind::Add, s2.te_id(v), s2.te_int(1)),
4502 };
4503 let max_body = self.te_block(vec![
4504 Stmt::Set {
4505 target: m,
4506 value: self.te_call(max_sym, vec![self.te_id(m), xi(self, p)]),
4507 },
4508 bump(self, p),
4509 ]);
4510 let max_loop = Stmt::While {
4511 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(cols)),
4512 body: max_body,
4513 decreasing: None,
4514 };
4515 let s_body = self.te_block(vec![
4516 Stmt::Set {
4517 target: s,
4518 value: self.te_bin(BinaryOpKind::Add, self.te_id(s), expo(self, p2)),
4519 },
4520 bump(self, p2),
4521 ]);
4522 let s_loop = Stmt::While {
4523 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p2), self.te_int(cols)),
4524 body: s_body,
4525 decreasing: None,
4526 };
4527 let n_body = self.te_block(vec![
4528 Stmt::Push {
4529 value: self.te_bin(BinaryOpKind::Divide, expo(self, q), self.te_id(s)),
4530 collection: self.te_id(var),
4531 },
4532 bump(self, q),
4533 ]);
4534 let n_loop = Stmt::While {
4535 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(cols)),
4536 body: n_body,
4537 decreasing: None,
4538 };
4539 let first = {
4541 let base = self.te_bin(
4542 BinaryOpKind::Multiply,
4543 self.te_bin(BinaryOpKind::Subtract, self.te_id(rr), self.te_int(1)),
4544 self.te_int(cols),
4545 );
4546 self.te_index(
4547 self.te_id(x),
4548 self.te_bin(BinaryOpKind::Add, base, self.te_int(1)),
4549 )
4550 };
4551 let row_body = self.te_block(vec![
4552 Stmt::Let {
4553 var: m,
4554 ty: None,
4555 value: first,
4556 mutable: true,
4557 },
4558 Stmt::Let {
4559 var: p,
4560 ty: None,
4561 value: self.te_int(2),
4562 mutable: true,
4563 },
4564 max_loop,
4565 Stmt::Let {
4566 var: s,
4567 ty: None,
4568 value: self.te_float(0.0),
4569 mutable: true,
4570 },
4571 Stmt::Let {
4572 var: p2,
4573 ty: None,
4574 value: self.te_int(1),
4575 mutable: true,
4576 },
4577 s_loop,
4578 Stmt::Let {
4579 var: q,
4580 ty: None,
4581 value: self.te_int(1),
4582 mutable: true,
4583 },
4584 n_loop,
4585 bump(self, rr),
4586 ]);
4587 let row_loop = Stmt::While {
4588 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(rr), self.te_int(rows)),
4589 body: row_body,
4590 decreasing: None,
4591 };
4592 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4593 type_name: seq_sym,
4594 type_args: vec![TypeExpr::Named(real_sym)],
4595 init_fields: vec![],
4596 });
4597 let body = self.te_block(vec![
4598 Stmt::Let {
4599 var,
4600 ty: None,
4601 value: new_seq,
4602 mutable: true,
4603 },
4604 Stmt::Let {
4605 var: rr,
4606 ty: None,
4607 value: self.te_int(1),
4608 mutable: true,
4609 },
4610 row_loop,
4611 ]);
4612 Stmt::Splice { body }
4613 }
4614
4615 #[allow(clippy::too_many_arguments)]
4625 fn desugar_tensor_rope(
4626 &mut self,
4627 var: Symbol,
4628 q: Symbol,
4629 rc: Symbol,
4630 rs: Symbol,
4631 d_total: i64,
4632 half: i64,
4633 hd: i64,
4634 per_row: Option<i64>,
4635 from: Option<(&'a Expr<'a>, i64)>,
4636 ) -> Stmt<'a> {
4637 let h_count = if hd > 0 { d_total / hd } else { 0 };
4638 let block = h_count / per_row.unwrap_or(h_count).max(1);
4639 let checked = from.map(|(p, cache)| {
4640 if block == 1 {
4641 self.checked_row_index(p, rc, cache)
4642 } else {
4643 self.checked_row_block(p, rc, cache, block)
4644 }
4645 });
4646 let at = checked.as_ref().map(|(at, _)| *at);
4647 let seq_sym = self.interner.intern("Seq");
4648 let real_sym = self.interner.intern("Real");
4649 let rh = self.fresh_desugar_temp("rp_h");
4650 let ri = self.fresh_desugar_temp("rp_i");
4651 let rp = self.fresh_desugar_temp("rp_pt");
4652 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4653 type_name: seq_sym,
4654 type_args: vec![TypeExpr::Named(real_sym)],
4655 init_fields: vec![],
4656 });
4657 let idx_a = |s: &mut Self| {
4660 s.te_bin(
4661 BinaryOpKind::Add,
4662 s.te_bin(
4663 BinaryOpKind::Add,
4664 s.te_bin(BinaryOpKind::Multiply, s.te_id(rh), s.te_int(hd)),
4665 s.te_id(ri),
4666 ),
4667 s.te_int(1),
4668 )
4669 };
4670 let idx_b = |s: &mut Self| {
4671 s.te_bin(
4672 BinaryOpKind::Add,
4673 s.te_bin(
4674 BinaryOpKind::Add,
4675 s.te_bin(
4676 BinaryOpKind::Add,
4677 s.te_bin(BinaryOpKind::Multiply, s.te_id(rh), s.te_int(hd)),
4678 s.te_id(ri),
4679 ),
4680 s.te_int(half),
4681 ),
4682 s.te_int(1),
4683 )
4684 };
4685 let idx_i = |s: &mut Self| {
4686 let within = s.te_bin(BinaryOpKind::Add, s.te_id(ri), s.te_int(1));
4687 match (per_row, at) {
4688 (None, _) => within,
4689 (Some(hpr), None) => s.te_bin(
4690 BinaryOpKind::Add,
4691 s.te_bin(
4692 BinaryOpKind::Multiply,
4693 s.te_bin(BinaryOpKind::Divide, s.te_id(rh), s.te_int(hpr)),
4694 s.te_int(half),
4695 ),
4696 within,
4697 ),
4698 (Some(hpr), Some(at)) => {
4699 let row = s.te_bin(
4700 BinaryOpKind::Add,
4701 s.te_bin(BinaryOpKind::Subtract, s.te_id(at), s.te_int(1)),
4702 s.te_bin(BinaryOpKind::Divide, s.te_id(rh), s.te_int(hpr)),
4703 );
4704 s.te_bin(
4705 BinaryOpKind::Add,
4706 s.te_bin(BinaryOpKind::Multiply, row, s.te_int(half)),
4707 within,
4708 )
4709 }
4710 }
4711 };
4712 let qa1 = idx_a(self);
4714 let ii1 = idx_i(self);
4715 let qb1 = idx_b(self);
4716 let ii2 = idx_i(self);
4717 let m1 = self.te_bin(
4718 BinaryOpKind::Multiply,
4719 self.te_index(self.te_id(q), qa1),
4720 self.te_index(self.te_id(rc), ii1),
4721 );
4722 let m2 = self.te_bin(
4723 BinaryOpKind::Multiply,
4724 self.te_index(self.te_id(q), qb1),
4725 self.te_index(self.te_id(rs), ii2),
4726 );
4727 let push1 = self.te_bin(BinaryOpKind::Subtract, m1, m2);
4728 let inner1 = self.te_block(vec![
4729 Stmt::Push {
4730 value: push1,
4731 collection: self.te_id(var),
4732 },
4733 Stmt::Set {
4734 target: ri,
4735 value: self.te_bin(BinaryOpKind::Add, self.te_id(ri), self.te_int(1)),
4736 },
4737 ]);
4738 let loop1 = Stmt::While {
4739 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ri), self.te_int(half)),
4740 body: inner1,
4741 decreasing: None,
4742 };
4743 let qa2 = idx_a(self);
4745 let ii3 = idx_i(self);
4746 let qb2 = idx_b(self);
4747 let ii4 = idx_i(self);
4748 let n1 = self.te_bin(
4749 BinaryOpKind::Multiply,
4750 self.te_index(self.te_id(q), qa2),
4751 self.te_index(self.te_id(rs), ii3),
4752 );
4753 let n2 = self.te_bin(
4754 BinaryOpKind::Multiply,
4755 self.te_index(self.te_id(q), qb2),
4756 self.te_index(self.te_id(rc), ii4),
4757 );
4758 let push2 = self.te_bin(BinaryOpKind::Add, n1, n2);
4759 let inner2 = self.te_block(vec![
4760 Stmt::Push {
4761 value: push2,
4762 collection: self.te_id(var),
4763 },
4764 Stmt::Set {
4765 target: ri,
4766 value: self.te_bin(BinaryOpKind::Add, self.te_id(ri), self.te_int(1)),
4767 },
4768 ]);
4769 let loop2 = Stmt::While {
4770 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ri), self.te_int(half)),
4771 body: inner2,
4772 decreasing: None,
4773 };
4774 let pt_idx = self.te_bin(
4778 BinaryOpKind::Add,
4779 self.te_bin(
4780 BinaryOpKind::Add,
4781 self.te_bin(BinaryOpKind::Multiply, self.te_id(rh), self.te_int(hd)),
4782 self.te_id(rp),
4783 ),
4784 self.te_int(1),
4785 );
4786 let inner3 = self.te_block(vec![
4787 Stmt::Push {
4788 value: self.te_index(self.te_id(q), pt_idx),
4789 collection: self.te_id(var),
4790 },
4791 Stmt::Set {
4792 target: rp,
4793 value: self.te_bin(BinaryOpKind::Add, self.te_id(rp), self.te_int(1)),
4794 },
4795 ]);
4796 let loop3 = Stmt::While {
4797 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(rp), self.te_int(hd)),
4798 body: inner3,
4799 decreasing: None,
4800 };
4801 let outer_body = self.te_block(vec![
4802 Stmt::Let {
4803 var: ri,
4804 ty: None,
4805 value: self.te_int(0),
4806 mutable: true,
4807 },
4808 loop1,
4809 Stmt::Set {
4810 target: ri,
4811 value: self.te_int(0),
4812 },
4813 loop2,
4814 Stmt::Let {
4815 var: rp,
4816 ty: None,
4817 value: self.te_int(2 * half),
4818 mutable: true,
4819 },
4820 loop3,
4821 Stmt::Set {
4822 target: rh,
4823 value: self.te_bin(BinaryOpKind::Add, self.te_id(rh), self.te_int(1)),
4824 },
4825 ]);
4826 let outer = Stmt::While {
4827 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(rh), self.te_int(h_count)),
4828 body: outer_body,
4829 decreasing: None,
4830 };
4831 let mut stmts: Vec<Stmt<'a>> = checked
4832 .map(|(_, check)| check.into_iter().collect())
4833 .unwrap_or_default();
4834 stmts.extend([
4835 Stmt::Let {
4836 var,
4837 ty: None,
4838 value: new_seq,
4839 mutable: true,
4840 },
4841 Stmt::Let {
4842 var: rh,
4843 ty: None,
4844 value: self.te_int(0),
4845 mutable: true,
4846 },
4847 outer,
4848 ]);
4849 let body = self.te_block(stmts);
4850 Stmt::Splice { body }
4851 }
4852
4853 fn desugar_tensor_silu(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4854 const E: f64 = std::f64::consts::E;
4855 let seq_sym = self.interner.intern("Seq");
4856 let real_sym = self.interner.intern("Real");
4857 let i = self.fresh_desugar_temp("sl_i");
4858 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4859 type_name: seq_sym,
4860 type_args: vec![TypeExpr::Named(real_sym)],
4861 init_fields: vec![],
4862 });
4863 let xi_num = self.te_index(self.te_id(x), self.te_id(i));
4864 let xi_den = self.te_index(self.te_id(x), self.te_id(i));
4865 let neg = self.te_bin(BinaryOpKind::Subtract, self.te_float(0.0), xi_den);
4866 let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
4867 let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
4868 let y = self.te_bin(BinaryOpKind::Divide, xi_num, denom);
4869 let body = self.te_block(vec![
4870 Stmt::Push {
4871 value: y,
4872 collection: self.te_id(var),
4873 },
4874 Stmt::Set {
4875 target: i,
4876 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4877 },
4878 ]);
4879 let loop_ = Stmt::While {
4880 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
4881 body,
4882 decreasing: None,
4883 };
4884 let outer = self.te_block(vec![
4885 Stmt::Let {
4886 var,
4887 ty: None,
4888 value: new_seq,
4889 mutable: true,
4890 },
4891 Stmt::Let {
4892 var: i,
4893 ty: None,
4894 value: self.te_int(1),
4895 mutable: true,
4896 },
4897 loop_,
4898 ]);
4899 Stmt::Splice { body: outer }
4900 }
4901
4902 fn desugar_tensor_sigmoid(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4906 const E: f64 = std::f64::consts::E;
4907 let seq_sym = self.interner.intern("Seq");
4908 let real_sym = self.interner.intern("Real");
4909 let i = self.fresh_desugar_temp("sg_i");
4910 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4911 type_name: seq_sym,
4912 type_args: vec![TypeExpr::Named(real_sym)],
4913 init_fields: vec![],
4914 });
4915 let neg = self.te_bin(
4916 BinaryOpKind::Subtract,
4917 self.te_float(0.0),
4918 self.te_index(self.te_id(x), self.te_id(i)),
4919 );
4920 let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
4921 let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
4922 let y = self.te_bin(BinaryOpKind::Divide, self.te_float(1.0), denom);
4923 let body = self.te_block(vec![
4924 Stmt::Push {
4925 value: y,
4926 collection: self.te_id(var),
4927 },
4928 Stmt::Set {
4929 target: i,
4930 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4931 },
4932 ]);
4933 let loop_ = Stmt::While {
4934 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
4935 body,
4936 decreasing: None,
4937 };
4938 let outer = self.te_block(vec![
4939 Stmt::Let {
4940 var,
4941 ty: None,
4942 value: new_seq,
4943 mutable: true,
4944 },
4945 Stmt::Let {
4946 var: i,
4947 ty: None,
4948 value: self.te_int(1),
4949 mutable: true,
4950 },
4951 loop_,
4952 ]);
4953 Stmt::Splice { body: outer }
4954 }
4955
4956 fn desugar_tensor_gelu(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4965 const E: f64 = std::f64::consts::E;
4966 const SQRT_2_OVER_PI: f64 = 0.797_884_560_802_865_4;
4967 const K: f64 = 0.044_715;
4968 let seq_sym = self.interner.intern("Seq");
4969 let real_sym = self.interner.intern("Real");
4970 let i = self.fresh_desugar_temp("gl_i");
4971 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4972 type_name: seq_sym,
4973 type_args: vec![TypeExpr::Named(real_sym)],
4974 init_fields: vec![],
4975 });
4976 let x3 = self.te_bin(
4978 BinaryOpKind::Multiply,
4979 self.te_index(self.te_id(x), self.te_id(i)),
4980 self.te_bin(
4981 BinaryOpKind::Multiply,
4982 self.te_index(self.te_id(x), self.te_id(i)),
4983 self.te_index(self.te_id(x), self.te_id(i)),
4984 ),
4985 );
4986 let inner = self.te_bin(
4987 BinaryOpKind::Add,
4988 self.te_index(self.te_id(x), self.te_id(i)),
4989 self.te_bin(BinaryOpKind::Multiply, self.te_float(K), x3),
4990 );
4991 let z = self.te_bin(BinaryOpKind::Multiply, self.te_float(SQRT_2_OVER_PI), inner);
4992 let neg2z = self.te_bin(
4994 BinaryOpKind::Subtract,
4995 self.te_float(0.0),
4996 self.te_bin(BinaryOpKind::Multiply, self.te_float(2.0), z),
4997 );
4998 let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg2z);
4999 let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
5000 let y = self.te_bin(
5001 BinaryOpKind::Divide,
5002 self.te_index(self.te_id(x), self.te_id(i)),
5003 denom,
5004 );
5005 let body = self.te_block(vec![
5006 Stmt::Push {
5007 value: y,
5008 collection: self.te_id(var),
5009 },
5010 Stmt::Set {
5011 target: i,
5012 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5013 },
5014 ]);
5015 let loop_ = Stmt::While {
5016 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
5017 body,
5018 decreasing: None,
5019 };
5020 let outer = self.te_block(vec![
5021 Stmt::Let {
5022 var,
5023 ty: None,
5024 value: new_seq,
5025 mutable: true,
5026 },
5027 Stmt::Let {
5028 var: i,
5029 ty: None,
5030 value: self.te_int(1),
5031 mutable: true,
5032 },
5033 loop_,
5034 ]);
5035 Stmt::Splice { body: outer }
5036 }
5037
5038 fn desugar_tensor_decay(
5056 &mut self,
5057 var: Symbol,
5058 ar: Symbol,
5059 dt: Symbol,
5060 alog: Symbol,
5061 total: i64,
5062 heads: i64,
5063 ) -> Stmt<'a> {
5064 const E: f64 = std::f64::consts::E;
5065 let seq_sym = self.interner.intern("Seq");
5066 let real_sym = self.interner.intern("Real");
5067 let i = self.fresh_desugar_temp("dc_i");
5068 let row = (heads < total).then(|| self.fresh_desugar_temp("dc_row"));
5069 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5070 type_name: seq_sym,
5071 type_args: vec![TypeExpr::Named(real_sym)],
5072 init_fields: vec![],
5073 });
5074 let at = match row {
5075 None => self.te_id(i),
5076 Some(r) => self.te_bin(
5077 BinaryOpKind::Add,
5078 self.te_bin(
5079 BinaryOpKind::Multiply,
5080 self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
5081 self.te_int(heads),
5082 ),
5083 self.te_id(i),
5084 ),
5085 };
5086 let u = self.te_bin(
5088 BinaryOpKind::Add,
5089 self.te_index(self.te_id(ar), at),
5090 self.te_index(self.te_id(dt), self.te_id(i)),
5091 );
5092 let base = self.te_bin(
5093 BinaryOpKind::Add,
5094 self.te_float(1.0),
5095 self.te_bin(BinaryOpKind::Pow, self.te_float(E), u),
5096 );
5097 let rate = self.te_index(self.te_id(alog), self.te_id(i));
5098 let y = self.te_bin(BinaryOpKind::Pow, base, rate);
5099 let body = self.te_block(vec![
5100 Stmt::Push {
5101 value: y,
5102 collection: self.te_id(var),
5103 },
5104 Stmt::Set {
5105 target: i,
5106 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5107 },
5108 ]);
5109 let loop_ = Stmt::While {
5110 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(heads)),
5111 body,
5112 decreasing: None,
5113 };
5114 let outer = match row {
5115 None => vec![
5116 Stmt::Let {
5117 var,
5118 ty: None,
5119 value: new_seq,
5120 mutable: true,
5121 },
5122 Stmt::Let {
5123 var: i,
5124 ty: None,
5125 value: self.te_int(1),
5126 mutable: true,
5127 },
5128 loop_,
5129 ],
5130 Some(r) => {
5131 let row_body = self.te_block(vec![
5132 Stmt::Let {
5133 var: i,
5134 ty: None,
5135 value: self.te_int(1),
5136 mutable: true,
5137 },
5138 loop_,
5139 Stmt::Set {
5140 target: r,
5141 value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
5142 },
5143 ]);
5144 vec![
5145 Stmt::Let {
5146 var,
5147 ty: None,
5148 value: new_seq,
5149 mutable: true,
5150 },
5151 Stmt::Let {
5152 var: r,
5153 ty: None,
5154 value: self.te_int(1),
5155 mutable: true,
5156 },
5157 Stmt::While {
5158 cond: self.te_bin(
5159 BinaryOpKind::LtEq,
5160 self.te_id(r),
5161 self.te_int(total / heads),
5162 ),
5163 body: row_body,
5164 decreasing: None,
5165 },
5166 ]
5167 }
5168 };
5169 let outer = self.te_block(outer);
5170 Stmt::Splice { body: outer }
5171 }
5172
5173 fn desugar_tensor_l2norm(&mut self, var: Symbol, x: Symbol, rows: i64, cols: i64, eps: f64) -> Stmt<'a> {
5178 let sqrt_sym = self.interner.intern("sqrt");
5179 let seq_sym = self.interner.intern("Seq");
5180 let real_sym = self.interner.intern("Real");
5181 let r = self.fresh_desugar_temp("l2_r");
5182 let base = self.fresh_desugar_temp("l2_b");
5183 let ss = self.fresh_desugar_temp("l2_ss");
5184 let p = self.fresh_desugar_temp("l2_p");
5185 let denom = self.fresh_desugar_temp("l2_d");
5186 let q = self.fresh_desugar_temp("l2_q");
5187 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5188 type_name: seq_sym,
5189 type_args: vec![TypeExpr::Named(real_sym)],
5190 init_fields: vec![],
5191 });
5192 let xp = self.te_index(
5194 self.te_id(x),
5195 self.te_bin(BinaryOpKind::Add, self.te_id(base), self.te_id(p)),
5196 );
5197 let sq = self.te_bin(BinaryOpKind::Multiply, xp, xp);
5198 let ss_body = self.te_block(vec![
5199 Stmt::Set {
5200 target: ss,
5201 value: self.te_bin(BinaryOpKind::Add, self.te_id(ss), sq),
5202 },
5203 Stmt::Set {
5204 target: p,
5205 value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
5206 },
5207 ]);
5208 let ss_loop = Stmt::While {
5209 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(cols)),
5210 body: ss_body,
5211 decreasing: None,
5212 };
5213 let denom_expr = self.te_call(
5214 sqrt_sym,
5215 vec![self.te_bin(BinaryOpKind::Add, self.te_id(ss), self.te_float(eps))],
5216 );
5217 let xq = self.te_index(
5218 self.te_id(x),
5219 self.te_bin(BinaryOpKind::Add, self.te_id(base), self.te_id(q)),
5220 );
5221 let n_body = self.te_block(vec![
5222 Stmt::Push {
5223 value: self.te_bin(BinaryOpKind::Divide, xq, self.te_id(denom)),
5224 collection: self.te_id(var),
5225 },
5226 Stmt::Set {
5227 target: q,
5228 value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
5229 },
5230 ]);
5231 let n_loop = Stmt::While {
5232 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(cols)),
5233 body: n_body,
5234 decreasing: None,
5235 };
5236 let row_body = self.te_block(vec![
5237 Stmt::Let {
5238 var: base,
5239 ty: None,
5240 value: self.te_bin(
5241 BinaryOpKind::Multiply,
5242 self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
5243 self.te_int(cols),
5244 ),
5245 mutable: false,
5246 },
5247 Stmt::Let {
5248 var: ss,
5249 ty: None,
5250 value: self.te_float(0.0),
5251 mutable: true,
5252 },
5253 Stmt::Let {
5254 var: p,
5255 ty: None,
5256 value: self.te_int(1),
5257 mutable: true,
5258 },
5259 ss_loop,
5260 Stmt::Let {
5261 var: denom,
5262 ty: None,
5263 value: denom_expr,
5264 mutable: false,
5265 },
5266 Stmt::Let {
5267 var: q,
5268 ty: None,
5269 value: self.te_int(1),
5270 mutable: true,
5271 },
5272 n_loop,
5273 Stmt::Set {
5274 target: r,
5275 value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
5276 },
5277 ]);
5278 let row_loop = Stmt::While {
5279 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
5280 body: row_body,
5281 decreasing: None,
5282 };
5283 let body = self.te_block(vec![
5284 Stmt::Let {
5285 var,
5286 ty: None,
5287 value: new_seq,
5288 mutable: true,
5289 },
5290 Stmt::Let {
5291 var: r,
5292 ty: None,
5293 value: self.te_int(1),
5294 mutable: true,
5295 },
5296 row_loop,
5297 ]);
5298 Stmt::Splice { body }
5299 }
5300
5301 fn desugar_tensor_swiglu(
5304 &mut self,
5305 var: Symbol,
5306 gate: Symbol,
5307 up: Symbol,
5308 total: i64,
5309 ) -> Stmt<'a> {
5310 const E: f64 = std::f64::consts::E;
5311 let seq_sym = self.interner.intern("Seq");
5312 let real_sym = self.interner.intern("Real");
5313 let i = self.fresh_desugar_temp("sg_i");
5314 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5315 type_name: seq_sym,
5316 type_args: vec![TypeExpr::Named(real_sym)],
5317 init_fields: vec![],
5318 });
5319 let g_num = self.te_index(self.te_id(gate), self.te_id(i));
5321 let g_den = self.te_index(self.te_id(gate), self.te_id(i));
5322 let neg = self.te_bin(BinaryOpKind::Subtract, self.te_float(0.0), g_den);
5323 let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
5324 let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
5325 let silu = self.te_bin(BinaryOpKind::Divide, g_num, denom);
5326 let y = self.te_bin(
5327 BinaryOpKind::Multiply,
5328 silu,
5329 self.te_index(self.te_id(up), self.te_id(i)),
5330 );
5331 let body = self.te_block(vec![
5332 Stmt::Push {
5333 value: y,
5334 collection: self.te_id(var),
5335 },
5336 Stmt::Set {
5337 target: i,
5338 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5339 },
5340 ]);
5341 let loop_ = Stmt::While {
5342 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
5343 body,
5344 decreasing: None,
5345 };
5346 let outer = self.te_block(vec![
5347 Stmt::Let {
5348 var,
5349 ty: None,
5350 value: new_seq,
5351 mutable: true,
5352 },
5353 Stmt::Let {
5354 var: i,
5355 ty: None,
5356 value: self.te_int(1),
5357 mutable: true,
5358 },
5359 loop_,
5360 ]);
5361 Stmt::Splice { body: outer }
5362 }
5363
5364 #[allow(clippy::too_many_arguments)]
5375 fn desugar_tensor_causal_conv(
5376 &mut self,
5377 var: Symbol,
5378 x: Symbol,
5379 wc: Symbol,
5380 cs: Symbol,
5381 taps: i64,
5382 chan: i64,
5383 rows: i64,
5384 ) -> Stmt<'a> {
5385 let seq_sym = self.interner.intern("Seq");
5386 let real_sym = self.interner.intern("Real");
5387 let (t1, c1, c2, c3, t2) = (
5388 self.fresh_desugar_temp("cv_t1"),
5389 self.fresh_desugar_temp("cv_c1"),
5390 self.fresh_desugar_temp("cv_c2"),
5391 self.fresh_desugar_temp("cv_c3"),
5392 self.fresh_desugar_temp("cv_t2"),
5393 );
5394 let (acc, zi) = (
5395 self.fresh_desugar_temp("cv_acc"),
5396 self.fresh_desugar_temp("cv_zi"),
5397 );
5398 let row = (rows > 1).then(|| self.fresh_desugar_temp("cv_row"));
5399 let id = |sy: Symbol| self.te_id(sy);
5400 let fl = |v: f64| self.te_float(v);
5401 let it = |n: i64| self.te_int(n);
5402 let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
5403 let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
5404 let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
5405 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5406 let nseq = || {
5407 self.ctx.alloc_imperative_expr(Expr::New {
5408 type_name: seq_sym,
5409 type_args: vec![TypeExpr::Named(real_sym)],
5410 init_fields: vec![],
5411 })
5412 };
5413 let incr = |sy: Symbol| Stmt::Set {
5414 target: sy,
5415 value: add(id(sy), it(1)),
5416 };
5417 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5418 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5419 body: self.te_block(body),
5420 decreasing: None,
5421 };
5422 let shift = while_le(
5424 t1,
5425 taps - 1,
5426 vec![
5427 Stmt::Let {
5428 var: c1,
5429 ty: None,
5430 value: it(1),
5431 mutable: true,
5432 },
5433 while_le(
5434 c1,
5435 chan,
5436 vec![
5437 Stmt::SetIndex {
5438 collection: id(cs),
5439 index: add(mul(sub(id(t1), it(1)), it(chan)), id(c1)),
5440 value: item(cs, add(mul(id(t1), it(chan)), id(c1))),
5441 },
5442 incr(c1),
5443 ],
5444 ),
5445 incr(t1),
5446 ],
5447 );
5448 let in_row = |c: Symbol| match row {
5450 None => id(c),
5451 Some(r) => add(mul(sub(id(r), it(1)), it(chan)), id(c)),
5452 };
5453 let append = while_le(
5455 c2,
5456 chan,
5457 vec![
5458 Stmt::SetIndex {
5459 collection: id(cs),
5460 index: add(it((taps - 1) * chan), id(c2)),
5461 value: item(x, in_row(c2)),
5462 },
5463 incr(c2),
5464 ],
5465 );
5466 let reduce = while_le(
5468 c3,
5469 chan,
5470 vec![
5471 Stmt::Let {
5472 var: acc,
5473 ty: None,
5474 value: fl(0.0),
5475 mutable: true,
5476 },
5477 Stmt::Let {
5478 var: t2,
5479 ty: None,
5480 value: it(1),
5481 mutable: true,
5482 },
5483 while_le(
5484 t2,
5485 taps,
5486 vec![
5487 Stmt::Set {
5488 target: acc,
5489 value: add(
5490 id(acc),
5491 mul(
5492 item(cs, add(mul(sub(id(t2), it(1)), it(chan)), id(c3))),
5493 item(wc, add(mul(sub(id(t2), it(1)), it(chan)), id(c3))),
5494 ),
5495 ),
5496 },
5497 incr(t2),
5498 ],
5499 ),
5500 Stmt::SetIndex {
5501 collection: id(var),
5502 index: in_row(c3),
5503 value: id(acc),
5504 },
5505 incr(c3),
5506 ],
5507 );
5508 let step = vec![
5509 Stmt::Let {
5510 var: t1,
5511 ty: None,
5512 value: it(1),
5513 mutable: true,
5514 },
5515 shift,
5516 Stmt::Let {
5517 var: c2,
5518 ty: None,
5519 value: it(1),
5520 mutable: true,
5521 },
5522 append,
5523 Stmt::Let {
5524 var: c3,
5525 ty: None,
5526 value: it(1),
5527 mutable: true,
5528 },
5529 reduce,
5530 ];
5531 let mut body = vec![
5532 Stmt::Let {
5533 var,
5534 ty: None,
5535 value: nseq(),
5536 mutable: true,
5537 },
5538 Stmt::Let {
5539 var: zi,
5540 ty: None,
5541 value: it(1),
5542 mutable: true,
5543 },
5544 while_le(
5545 zi,
5546 chan * rows,
5547 vec![
5548 Stmt::Push {
5549 value: fl(0.0),
5550 collection: id(var),
5551 },
5552 incr(zi),
5553 ],
5554 ),
5555 ];
5556 match row {
5557 None => body.extend(step),
5558 Some(r) => {
5559 body.push(Stmt::Let {
5560 var: r,
5561 ty: None,
5562 value: it(1),
5563 mutable: true,
5564 });
5565 body.push(while_le(
5566 r,
5567 rows,
5568 step.into_iter().chain(std::iter::once(incr(r))).collect(),
5569 ));
5570 }
5571 }
5572 let body = self.te_block(body);
5573 Stmt::Splice { body }
5574 }
5575
5576 fn desugar_tensor_full_attention(
5583 &mut self,
5584 var: Symbol,
5585 q: Symbol,
5586 k: Symbol,
5587 v: Symbol,
5588 h: i64,
5589 hd: i64,
5590 t: i64,
5591 ) -> Stmt<'a> {
5592 const EU: f64 = std::f64::consts::E;
5593 let scale = 1.0f64 / (hd as f64).sqrt();
5594 let width = h * hd;
5595 let seq_sym = self.interner.intern("Seq");
5596 let real_sym = self.interner.intern("Real");
5597 let max_sym = self.interner.intern("max");
5598 let (row, head, scores, ozi) = (
5599 self.fresh_desugar_temp("fa_row"),
5600 self.fresh_desugar_temp("fa_head"),
5601 self.fresh_desugar_temp("fa_sc"),
5602 self.fresh_desugar_temp("fa_ozi"),
5603 );
5604 let (j_sc, e_sc, acc_sc) = (
5605 self.fresh_desugar_temp("fa_jsc"),
5606 self.fresh_desugar_temp("fa_esc"),
5607 self.fresh_desugar_temp("fa_asc"),
5608 );
5609 let (mx, si_mx, si_dn, den) = (
5610 self.fresh_desugar_temp("fa_mx"),
5611 self.fresh_desugar_temp("fa_simx"),
5612 self.fresh_desugar_temp("fa_sidn"),
5613 self.fresh_desugar_temp("fa_den"),
5614 );
5615 let (e_wv, j_wv, acc_wv) = (
5616 self.fresh_desugar_temp("fa_ewv"),
5617 self.fresh_desugar_temp("fa_jwv"),
5618 self.fresh_desugar_temp("fa_awv"),
5619 );
5620 let id = |sy: Symbol| self.te_id(sy);
5621 let fl = |val: f64| self.te_float(val);
5622 let it = |n: i64| self.te_int(n);
5623 let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
5624 let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
5625 let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
5626 let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
5627 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5628 let nseq = || {
5629 self.ctx.alloc_imperative_expr(Expr::New {
5630 type_name: seq_sym,
5631 type_args: vec![TypeExpr::Named(real_sym)],
5632 init_fields: vec![],
5633 })
5634 };
5635 let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
5636 let incr = |sy: Symbol| Stmt::Set {
5637 target: sy,
5638 value: add(id(sy), it(1)),
5639 };
5640 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5641 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5642 body: self.te_block(body),
5643 decreasing: None,
5644 };
5645 let hbase = mul(sub(id(head), it(1)), it(hd));
5647 let q_read = add(add(mul(sub(id(row), it(1)), it(width)), hbase), id(e_sc));
5648 let k_read = add(
5649 add(
5650 mul(sub(id(j_sc), it(1)), it(width)),
5651 mul(sub(id(head), it(1)), it(hd)),
5652 ),
5653 id(e_sc),
5654 );
5655 let v_read = add(
5656 add(
5657 mul(sub(id(j_wv), it(1)), it(width)),
5658 mul(sub(id(head), it(1)), it(hd)),
5659 ),
5660 id(e_wv),
5661 );
5662 let c_write = add(
5663 add(
5664 mul(sub(id(row), it(1)), it(width)),
5665 mul(sub(id(head), it(1)), it(hd)),
5666 ),
5667 id(e_wv),
5668 );
5669 let score_loop = while_le(
5670 j_sc,
5671 t,
5672 vec![
5673 Stmt::Let {
5674 var: acc_sc,
5675 ty: None,
5676 value: fl(0.0),
5677 mutable: true,
5678 },
5679 Stmt::Let {
5680 var: e_sc,
5681 ty: None,
5682 value: it(1),
5683 mutable: true,
5684 },
5685 while_le(
5686 e_sc,
5687 hd,
5688 vec![
5689 Stmt::Set {
5690 target: acc_sc,
5691 value: add(id(acc_sc), mul(item(q, q_read), item(k, k_read))),
5692 },
5693 incr(e_sc),
5694 ],
5695 ),
5696 Stmt::Push {
5697 value: mul(id(acc_sc), fl(scale)),
5698 collection: id(scores),
5699 },
5700 incr(j_sc),
5701 ],
5702 );
5703 let max_loop = while_le(
5704 si_mx,
5705 t,
5706 vec![
5707 Stmt::Set {
5708 target: mx,
5709 value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
5710 },
5711 incr(si_mx),
5712 ],
5713 );
5714 let den_loop = while_le(
5715 si_dn,
5716 t,
5717 vec![
5718 Stmt::Set {
5719 target: den,
5720 value: add(id(den), expo(sub(item(scores, id(si_dn)), id(mx)))),
5721 },
5722 incr(si_dn),
5723 ],
5724 );
5725 let wv_loop = while_le(
5726 e_wv,
5727 hd,
5728 vec![
5729 Stmt::Let {
5730 var: acc_wv,
5731 ty: None,
5732 value: fl(0.0),
5733 mutable: true,
5734 },
5735 Stmt::Let {
5736 var: j_wv,
5737 ty: None,
5738 value: it(1),
5739 mutable: true,
5740 },
5741 while_le(
5742 j_wv,
5743 t,
5744 vec![
5745 Stmt::Set {
5746 target: acc_wv,
5747 value: add(
5748 id(acc_wv),
5749 mul(
5750 dv(expo(sub(item(scores, id(j_wv)), id(mx))), id(den)),
5751 item(v, v_read),
5752 ),
5753 ),
5754 },
5755 incr(j_wv),
5756 ],
5757 ),
5758 Stmt::SetIndex {
5759 collection: id(var),
5760 index: c_write,
5761 value: id(acc_wv),
5762 },
5763 incr(e_wv),
5764 ],
5765 );
5766 let head_loop = while_le(
5767 head,
5768 h,
5769 vec![
5770 Stmt::Let {
5771 var: scores,
5772 ty: None,
5773 value: nseq(),
5774 mutable: true,
5775 },
5776 Stmt::Let {
5777 var: j_sc,
5778 ty: None,
5779 value: it(1),
5780 mutable: true,
5781 },
5782 score_loop,
5783 Stmt::Let {
5784 var: mx,
5785 ty: None,
5786 value: item(scores, it(1)),
5787 mutable: true,
5788 },
5789 Stmt::Let {
5790 var: si_mx,
5791 ty: None,
5792 value: it(2),
5793 mutable: true,
5794 },
5795 max_loop,
5796 Stmt::Let {
5797 var: den,
5798 ty: None,
5799 value: fl(0.0),
5800 mutable: true,
5801 },
5802 Stmt::Let {
5803 var: si_dn,
5804 ty: None,
5805 value: it(1),
5806 mutable: true,
5807 },
5808 den_loop,
5809 Stmt::Let {
5810 var: e_wv,
5811 ty: None,
5812 value: it(1),
5813 mutable: true,
5814 },
5815 wv_loop,
5816 incr(head),
5817 ],
5818 );
5819 let body = self.te_block(vec![
5820 Stmt::Let {
5821 var,
5822 ty: None,
5823 value: nseq(),
5824 mutable: true,
5825 },
5826 Stmt::Let {
5827 var: ozi,
5828 ty: None,
5829 value: it(1),
5830 mutable: true,
5831 },
5832 while_le(
5833 ozi,
5834 t * width,
5835 vec![
5836 Stmt::Push {
5837 value: fl(0.0),
5838 collection: id(var),
5839 },
5840 incr(ozi),
5841 ],
5842 ),
5843 Stmt::Let {
5844 var: row,
5845 ty: None,
5846 value: it(1),
5847 mutable: true,
5848 },
5849 while_le(
5850 row,
5851 t,
5852 vec![
5853 Stmt::Let {
5854 var: head,
5855 ty: None,
5856 value: it(1),
5857 mutable: true,
5858 },
5859 head_loop,
5860 incr(row),
5861 ],
5862 ),
5863 ]);
5864 Stmt::Splice { body }
5865 }
5866
5867 #[allow(clippy::too_many_arguments)]
5893 fn desugar_tensor_gated_delta(
5894 &mut self,
5895 var: Symbol,
5896 q: Symbol,
5897 k: Symbol,
5898 v: Symbol,
5899 z: Symbol,
5900 b: Symbol,
5901 a: Symbol,
5902 st: Symbol,
5903 gn: Symbol,
5904 hv: i64,
5905 hk: i64,
5906 d: i64,
5907 rows: i64,
5908 eps: f64,
5909 sigmoid_gate: bool,
5910 ) -> Stmt<'a> {
5911 const EU: f64 = std::f64::consts::E;
5912 let seq_sym = self.interner.intern("Seq");
5913 let real_sym = self.interner.intern("Real");
5914 let sqrt_sym = self.interner.intern("sqrt");
5915 let (head, kb, vb, sb, alpha, beta) = (
5916 self.fresh_desugar_temp("gd_h"),
5917 self.fresh_desugar_temp("gd_kb"),
5918 self.fresh_desugar_temp("gd_vb"),
5919 self.fresh_desugar_temp("gd_sb"),
5920 self.fresh_desugar_temp("gd_al"),
5921 self.fresh_desugar_temp("gd_be"),
5922 );
5923 let (uvec, ua, ub, uacc) = (
5924 self.fresh_desugar_temp("gd_U"),
5925 self.fresh_desugar_temp("gd_ua"),
5926 self.fresh_desugar_temp("gd_ub"),
5927 self.fresh_desugar_temp("gd_uacc"),
5928 );
5929 let (sa, sbi, coef) = (
5930 self.fresh_desugar_temp("gd_sa"),
5931 self.fresh_desugar_temp("gd_sbi"),
5932 self.fresh_desugar_temp("gd_coef"),
5933 );
5934 let (ovec, oa, ob, oacc) = (
5935 self.fresh_desugar_temp("gd_O"),
5936 self.fresh_desugar_temp("gd_oa"),
5937 self.fresh_desugar_temp("gd_ob"),
5938 self.fresh_desugar_temp("gd_oacc"),
5939 );
5940 let (ga, ssq, den, na, zi) = (
5941 self.fresh_desugar_temp("gd_ga"),
5942 self.fresh_desugar_temp("gd_ssq"),
5943 self.fresh_desugar_temp("gd_den"),
5944 self.fresh_desugar_temp("gd_na"),
5945 self.fresh_desugar_temp("gd_zi"),
5946 );
5947 let row = (rows > 1).then(|| {
5950 (
5951 self.fresh_desugar_temp("gd_row"),
5952 self.fresh_desugar_temp("gd_rq"),
5953 self.fresh_desugar_temp("gd_rv"),
5954 self.fresh_desugar_temp("gd_rh"),
5955 )
5956 });
5957 let id = |sy: Symbol| self.te_id(sy);
5958 let fl = |val: f64| self.te_float(val);
5959 let it = |n: i64| self.te_int(n);
5960 let add = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, x, y);
5961 let sub = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, x, y);
5962 let mul = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, x, y);
5963 let dv = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, x, y);
5964 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5965 let nseq = || {
5966 self.ctx.alloc_imperative_expr(Expr::New {
5967 type_name: seq_sym,
5968 type_args: vec![TypeExpr::Named(real_sym)],
5969 init_fields: vec![],
5970 })
5971 };
5972 let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
5973 let gate = |g: &'a Expr<'a>| dv(if sigmoid_gate { fl(1.0) } else { g }, add(fl(1.0), expo(sub(fl(0.0), g))));
5974 let incr = |sy: Symbol| Stmt::Set {
5975 target: sy,
5976 value: add(id(sy), it(1)),
5977 };
5978 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5979 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5980 body: self.te_block(body),
5981 decreasing: None,
5982 };
5983 let s_at =
5985 |r: &'a Expr<'a>, c: &'a Expr<'a>| add(add(id(sb), mul(sub(r, it(1)), it(d))), c);
5986
5987 let u_loop = while_le(
5989 ua,
5990 d,
5991 vec![
5992 Stmt::Let {
5993 var: uacc,
5994 ty: None,
5995 value: fl(0.0),
5996 mutable: true,
5997 },
5998 Stmt::Let {
5999 var: ub,
6000 ty: None,
6001 value: it(1),
6002 mutable: true,
6003 },
6004 while_le(
6005 ub,
6006 d,
6007 vec![
6008 Stmt::Set {
6009 target: uacc,
6010 value: add(
6011 id(uacc),
6012 mul(item(st, s_at(id(ua), id(ub))), item(k, add(id(kb), id(ub)))),
6013 ),
6014 },
6015 incr(ub),
6016 ],
6017 ),
6018 Stmt::Push {
6019 value: id(uacc),
6020 collection: id(uvec),
6021 },
6022 incr(ua),
6023 ],
6024 );
6025 let s_loop = while_le(
6027 sa,
6028 d,
6029 vec![
6030 Stmt::Let {
6031 var: coef,
6032 ty: None,
6033 value: mul(
6034 id(beta),
6035 sub(
6036 item(v, add(id(vb), id(sa))),
6037 mul(id(alpha), item(uvec, id(sa))),
6038 ),
6039 ),
6040 mutable: false,
6041 },
6042 Stmt::Let {
6043 var: sbi,
6044 ty: None,
6045 value: it(1),
6046 mutable: true,
6047 },
6048 while_le(
6049 sbi,
6050 d,
6051 vec![
6052 Stmt::SetIndex {
6053 collection: id(st),
6054 index: s_at(id(sa), id(sbi)),
6055 value: add(
6056 mul(id(alpha), item(st, s_at(id(sa), id(sbi)))),
6057 mul(id(coef), item(k, add(id(kb), id(sbi)))),
6058 ),
6059 },
6060 incr(sbi),
6061 ],
6062 ),
6063 incr(sa),
6064 ],
6065 );
6066 let o_loop = while_le(
6068 oa,
6069 d,
6070 vec![
6071 Stmt::Let {
6072 var: oacc,
6073 ty: None,
6074 value: fl(0.0),
6075 mutable: true,
6076 },
6077 Stmt::Let {
6078 var: ob,
6079 ty: None,
6080 value: it(1),
6081 mutable: true,
6082 },
6083 while_le(
6084 ob,
6085 d,
6086 vec![
6087 Stmt::Set {
6088 target: oacc,
6089 value: add(
6090 id(oacc),
6091 mul(item(st, s_at(id(oa), id(ob))), item(q, add(id(kb), id(ob)))),
6092 ),
6093 },
6094 incr(ob),
6095 ],
6096 ),
6097 Stmt::Push {
6098 value: mul(id(oacc), fl(1.0 / (d as f64).sqrt())),
6099 collection: id(ovec),
6100 },
6101 incr(oa),
6102 ],
6103 );
6104 let gate_loop = while_le(
6106 ga,
6107 d,
6108 vec![
6109 Stmt::Set {
6110 target: ssq,
6111 value: add(id(ssq), mul(item(ovec, id(ga)), item(ovec, id(ga)))),
6112 },
6113 incr(ga),
6114 ],
6115 );
6116 let norm_loop = while_le(
6118 na,
6119 d,
6120 vec![
6121 Stmt::SetIndex {
6122 collection: id(var),
6123 index: add(id(vb), id(na)),
6124 value: mul(
6125 mul(item(gn, id(na)), dv(item(ovec, id(na)), id(den))),
6126 gate(item(z, add(id(vb), id(na)))),
6127 ),
6128 },
6129 incr(na),
6130 ],
6131 );
6132 let key_base = mul(
6133 self.te_bin(BinaryOpKind::Modulo, sub(id(head), it(1)), it(hk)),
6134 it(d),
6135 );
6136 let value_base = mul(sub(id(head), it(1)), it(d));
6137 let (key_base, value_base, head_at) = match row {
6138 None => (key_base, value_base, id(head)),
6139 Some((_, rq, rv, rh)) => (
6140 add(id(rq), key_base),
6141 add(id(rv), value_base),
6142 add(id(rh), id(head)),
6143 ),
6144 };
6145 let head_loop = while_le(
6146 head,
6147 hv,
6148 vec![
6149 Stmt::Let {
6150 var: kb,
6151 ty: None,
6152 value: key_base,
6153 mutable: false,
6154 },
6155 Stmt::Let {
6156 var: vb,
6157 ty: None,
6158 value: value_base,
6159 mutable: false,
6160 },
6161 Stmt::Let {
6162 var: sb,
6163 ty: None,
6164 value: mul(sub(id(head), it(1)), it(d * d)),
6165 mutable: false,
6166 },
6167 Stmt::Let {
6168 var: alpha,
6169 ty: None,
6170 value: item(a, head_at),
6171 mutable: false,
6172 },
6173 Stmt::Let {
6174 var: beta,
6175 ty: None,
6176 value: item(b, head_at),
6177 mutable: false,
6178 },
6179 Stmt::Let {
6180 var: uvec,
6181 ty: None,
6182 value: nseq(),
6183 mutable: true,
6184 },
6185 Stmt::Let {
6186 var: ua,
6187 ty: None,
6188 value: it(1),
6189 mutable: true,
6190 },
6191 u_loop,
6192 Stmt::Let {
6193 var: sa,
6194 ty: None,
6195 value: it(1),
6196 mutable: true,
6197 },
6198 s_loop,
6199 Stmt::Let {
6200 var: ovec,
6201 ty: None,
6202 value: nseq(),
6203 mutable: true,
6204 },
6205 Stmt::Let {
6206 var: oa,
6207 ty: None,
6208 value: it(1),
6209 mutable: true,
6210 },
6211 o_loop,
6212 Stmt::Let {
6213 var: ssq,
6214 ty: None,
6215 value: fl(0.0),
6216 mutable: true,
6217 },
6218 Stmt::Let {
6219 var: ga,
6220 ty: None,
6221 value: it(1),
6222 mutable: true,
6223 },
6224 gate_loop,
6225 Stmt::Let {
6226 var: den,
6227 ty: None,
6228 value: self.te_call(sqrt_sym, vec![add(dv(id(ssq), fl(d as f64)), fl(eps))]),
6229 mutable: false,
6230 },
6231 Stmt::Let {
6232 var: na,
6233 ty: None,
6234 value: it(1),
6235 mutable: true,
6236 },
6237 norm_loop,
6238 incr(head),
6239 ],
6240 );
6241 let mut body = vec![
6242 Stmt::Let {
6243 var,
6244 ty: None,
6245 value: nseq(),
6246 mutable: true,
6247 },
6248 Stmt::Let {
6249 var: zi,
6250 ty: None,
6251 value: it(1),
6252 mutable: true,
6253 },
6254 while_le(
6255 zi,
6256 hv * d * rows,
6257 vec![
6258 Stmt::Push {
6259 value: fl(0.0),
6260 collection: id(var),
6261 },
6262 incr(zi),
6263 ],
6264 ),
6265 ];
6266 match row {
6267 None => body.extend([
6268 Stmt::Let {
6269 var: head,
6270 ty: None,
6271 value: it(1),
6272 mutable: true,
6273 },
6274 head_loop,
6275 ]),
6276 Some((r, rq, rv, rh)) => {
6277 let offset = |per: i64| mul(sub(id(r), it(1)), it(per));
6278 body.push(Stmt::Let {
6279 var: r,
6280 ty: None,
6281 value: it(1),
6282 mutable: true,
6283 });
6284 body.push(while_le(
6285 r,
6286 rows,
6287 vec![
6288 Stmt::Let {
6289 var: rq,
6290 ty: None,
6291 value: offset(hk * d),
6292 mutable: false,
6293 },
6294 Stmt::Let {
6295 var: rv,
6296 ty: None,
6297 value: offset(hv * d),
6298 mutable: false,
6299 },
6300 Stmt::Let {
6301 var: rh,
6302 ty: None,
6303 value: offset(hv),
6304 mutable: false,
6305 },
6306 Stmt::Let {
6307 var: head,
6308 ty: None,
6309 value: it(1),
6310 mutable: true,
6311 },
6312 head_loop,
6313 incr(r),
6314 ],
6315 ));
6316 }
6317 }
6318 let body = self.te_block(body);
6319 Stmt::Splice { body }
6320 }
6321
6322 fn desugar_tensor_moe(
6342 &mut self,
6343 var: Symbol,
6344 x: Symbol,
6345 wr: Symbol,
6346 wg: Symbol,
6347 wu: Symbol,
6348 wd: Symbol,
6349 d: i64,
6350 dff: i64,
6351 ne: i64,
6352 k: i64,
6353 opts: MoeOptions,
6354 ) -> Stmt<'a> {
6355 const EU: f64 = std::f64::consts::E;
6356 let seq_sym = self.interner.intern("Seq");
6357 let real_sym = self.interner.intern("Real");
6358 let max_sym = self.interner.intern("max");
6359 let min_sym = self.interner.intern("min");
6360 let (l_seq, p_seq, pm_seq, w_seq) = (
6362 self.fresh_desugar_temp("mo_L"),
6363 self.fresh_desugar_temp("mo_P"),
6364 self.fresh_desugar_temp("mo_Pm"),
6365 self.fresh_desugar_temp("mo_W"),
6366 );
6367 let (er, accr, tr) = (
6368 self.fresh_desugar_temp("mo_er"),
6369 self.fresh_desugar_temp("mo_accr"),
6370 self.fresh_desugar_temp("mo_tr"),
6371 );
6372 let (smm, smi1, sms, smi2, smi3) = (
6373 self.fresh_desugar_temp("mo_m"),
6374 self.fresh_desugar_temp("mo_mi"),
6375 self.fresh_desugar_temp("mo_s"),
6376 self.fresh_desugar_temp("mo_si"),
6377 self.fresh_desugar_temp("mo_ni"),
6378 );
6379 let s_seq = opts.weigh_inputs.then(|| self.fresh_desugar_temp("mo_S"));
6381 let group_temps = opts.groups.map(|_| {
6382 [
6383 "mo_Ps", "mo_Gs", "mo_gi", "mo_gj", "mo_t1", "mo_t2", "mo_gv", "mo_pi", "mo_gr", "mo_gbv", "mo_gbi", "mo_gsi",
6384 "mo_gk",
6385 ]
6386 .map(|tag| self.fresh_desugar_temp(tag))
6387 });
6388 let scale_i = opts.scale.map(|_| self.fresh_desugar_temp("mo_sc"));
6389 let (pci, wzi, wsum, kk, bv, bi, sci, rni) = (
6390 self.fresh_desugar_temp("mo_pci"),
6391 self.fresh_desugar_temp("mo_wzi"),
6392 self.fresh_desugar_temp("mo_wsum"),
6393 self.fresh_desugar_temp("mo_kk"),
6394 self.fresh_desugar_temp("mo_bv"),
6395 self.fresh_desugar_temp("mo_bi"),
6396 self.fresh_desugar_temp("mo_sci"),
6397 self.fresh_desugar_temp("mo_rni"),
6398 );
6399 let (ozi, ee, gate, up) = (
6400 self.fresh_desugar_temp("mo_ozi"),
6401 self.fresh_desugar_temp("mo_ee"),
6402 self.fresh_desugar_temp("mo_gate"),
6403 self.fresh_desugar_temp("mo_up"),
6404 );
6405 let (gig, trg, gacc, giu, tru, uacc) = (
6406 self.fresh_desugar_temp("mo_gig"),
6407 self.fresh_desugar_temp("mo_trg"),
6408 self.fresh_desugar_temp("mo_gacc"),
6409 self.fresh_desugar_temp("mo_giu"),
6410 self.fresh_desugar_temp("mo_tru"),
6411 self.fresh_desugar_temp("mo_uacc"),
6412 );
6413 let (oo, di, dacc) = (
6414 self.fresh_desugar_temp("mo_oo"),
6415 self.fresh_desugar_temp("mo_di"),
6416 self.fresh_desugar_temp("mo_dacc"),
6417 );
6418
6419 let id = |s: Symbol| self.te_id(s);
6421 let fl = |v: f64| self.te_float(v);
6422 let it = |n: i64| self.te_int(n);
6423 let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
6424 let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
6425 let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
6426 let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
6427 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
6428 let nseq = || {
6429 self.ctx.alloc_imperative_expr(Expr::New {
6430 type_name: seq_sym,
6431 type_args: vec![TypeExpr::Named(real_sym)],
6432 init_fields: vec![],
6433 })
6434 };
6435 let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
6436 let silu = |g: &'a Expr<'a>| dv(g, add(fl(1.0), expo(sub(fl(0.0), g))));
6438 let incr = |s: Symbol| Stmt::Set {
6439 target: s,
6440 value: add(id(s), it(1)),
6441 };
6442 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
6443 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
6444 body: self.te_block(body),
6445 decreasing: None,
6446 };
6447
6448 let wr_ix = add(mul(sub(id(tr), it(1)), it(ne)), id(er)); let router_inner = while_le(
6451 tr,
6452 d,
6453 vec![
6454 Stmt::Set {
6455 target: accr,
6456 value: add(id(accr), mul(item(x, id(tr)), item(wr, wr_ix))),
6457 },
6458 incr(tr),
6459 ],
6460 );
6461 let router_loop = while_le(
6462 er,
6463 ne,
6464 vec![
6465 Stmt::Let {
6466 var: accr,
6467 ty: None,
6468 value: fl(0.0),
6469 mutable: true,
6470 },
6471 Stmt::Let {
6472 var: tr,
6473 ty: None,
6474 value: it(1),
6475 mutable: true,
6476 },
6477 router_inner,
6478 Stmt::Push {
6479 value: match opts.router_bias {
6480 Some(br) => add(id(accr), item(br, id(er))),
6481 None => id(accr),
6482 },
6483 collection: id(l_seq),
6484 },
6485 incr(er),
6486 ],
6487 );
6488
6489 let max_loop = while_le(
6491 smi1,
6492 ne,
6493 vec![
6494 Stmt::Set {
6495 target: smm,
6496 value: self.te_call(max_sym, vec![id(smm), item(l_seq, id(smi1))]),
6497 },
6498 incr(smi1),
6499 ],
6500 );
6501 let sum_loop = while_le(
6502 smi2,
6503 ne,
6504 vec![
6505 Stmt::Set {
6506 target: sms,
6507 value: add(id(sms), expo(sub(item(l_seq, id(smi2)), id(smm)))),
6508 },
6509 incr(smi2),
6510 ],
6511 );
6512 let norm_loop = while_le(
6513 smi3,
6514 ne,
6515 vec![
6516 Stmt::Push {
6517 value: match opts.score {
6518 MoeScore::Softmax => dv(expo(sub(item(l_seq, id(smi3)), id(smm))), id(sms)),
6519 MoeScore::Sigmoid => dv(fl(1.0), add(fl(1.0), expo(sub(fl(0.0), item(l_seq, id(smi3)))))),
6521 },
6522 collection: id(p_seq),
6523 },
6524 incr(smi3),
6525 ],
6526 );
6527
6528 let sel_seq = group_temps.map_or(pm_seq, |t| t[0]);
6531 let pm_copy = while_le(
6532 pci,
6533 ne,
6534 vec![
6535 Stmt::Push {
6536 value: match opts.selection_bias {
6537 Some(b) => add(item(p_seq, id(pci)), item(b, id(pci))),
6538 None => item(p_seq, id(pci)),
6539 },
6540 collection: id(sel_seq),
6541 },
6542 incr(pci),
6543 ],
6544 );
6545 let group_block = group_temps.map(|[ps, gs, gi, gj, t1, t2, gv, pi, gr, gbv, gbi, gsi, gk]| {
6549 let (kept, count) = opts.groups.expect("the group temporaries are minted for a group limit");
6550 let per = ne / count;
6551 let at = |g: Symbol, j: Symbol| add(mul(sub(id(g), it(1)), it(per)), id(j));
6552 let two_best = while_le(
6553 gj,
6554 per,
6555 vec![
6556 Stmt::Let {
6557 var: gv,
6558 ty: None,
6559 value: item(ps, at(gi, gj)),
6560 mutable: false,
6561 },
6562 Stmt::If {
6563 cond: self.te_bin(BinaryOpKind::Gt, id(gv), id(t1)),
6564 then_block: self.te_block(vec![
6565 Stmt::Set { target: t2, value: id(t1) },
6566 Stmt::Set { target: t1, value: id(gv) },
6567 ]),
6568 else_block: Some(self.te_block(vec![Stmt::If {
6569 cond: self.te_bin(BinaryOpKind::Gt, id(gv), id(t2)),
6570 then_block: self.te_block(vec![Stmt::Set { target: t2, value: id(gv) }]),
6571 else_block: None,
6572 }])),
6573 },
6574 incr(gj),
6575 ],
6576 );
6577 let scores = while_le(
6578 gi,
6579 count,
6580 vec![
6581 Stmt::Let { var: t1, ty: None, value: fl(-1.0e30), mutable: true },
6582 Stmt::Let { var: t2, ty: None, value: fl(-1.0e30), mutable: true },
6583 Stmt::Let { var: gj, ty: None, value: it(1), mutable: true },
6584 two_best,
6585 Stmt::Push { value: if per > 1 { add(id(t1), id(t2)) } else { id(t1) }, collection: id(gs) },
6587 incr(gi),
6588 ],
6589 );
6590 let masked = while_le(
6591 pi,
6592 ne,
6593 vec![Stmt::Push { value: fl(-1.0e30), collection: id(pm_seq) }, incr(pi)],
6594 );
6595 let scan = while_le(
6596 gsi,
6597 count,
6598 vec![
6599 Stmt::If {
6600 cond: self.te_bin(BinaryOpKind::Gt, item(gs, id(gsi)), id(gbv)),
6601 then_block: self.te_block(vec![
6602 Stmt::Set { target: gbv, value: item(gs, id(gsi)) },
6603 Stmt::Set { target: gbi, value: id(gsi) },
6604 ]),
6605 else_block: None,
6606 },
6607 incr(gsi),
6608 ],
6609 );
6610 let copy = while_le(
6611 gk,
6612 per,
6613 vec![
6614 Stmt::SetIndex { collection: id(pm_seq), index: at(gbi, gk), value: item(ps, at(gbi, gk)) },
6615 incr(gk),
6616 ],
6617 );
6618 let rounds = while_le(
6619 gr,
6620 kept,
6621 vec![
6622 Stmt::Let { var: gbv, ty: None, value: fl(-1.0e30), mutable: true },
6623 Stmt::Let { var: gbi, ty: None, value: it(1), mutable: true },
6624 Stmt::Let { var: gsi, ty: None, value: it(1), mutable: true },
6625 scan,
6626 Stmt::Let { var: gk, ty: None, value: it(1), mutable: true },
6627 copy,
6628 Stmt::SetIndex { collection: id(gs), index: id(gbi), value: fl(-2.0e30) },
6629 incr(gr),
6630 ],
6631 );
6632 vec![
6633 Stmt::Let { var: gs, ty: None, value: nseq(), mutable: true },
6634 Stmt::Let { var: gi, ty: None, value: it(1), mutable: true },
6635 scores,
6636 Stmt::Let { var: pi, ty: None, value: it(1), mutable: true },
6637 masked,
6638 Stmt::Let { var: gr, ty: None, value: it(1), mutable: true },
6639 rounds,
6640 ]
6641 });
6642 let mut zeros = vec![Stmt::Push {
6643 value: fl(0.0),
6644 collection: id(w_seq),
6645 }];
6646 if let Some(s_seq) = s_seq {
6647 zeros.push(Stmt::Push {
6648 value: fl(0.0),
6649 collection: id(s_seq),
6650 });
6651 }
6652 zeros.push(incr(wzi));
6653 let w_zero = while_le(wzi, ne, zeros);
6654 let scan = while_le(
6655 sci,
6656 ne,
6657 vec![
6658 Stmt::If {
6659 cond: self.te_bin(BinaryOpKind::Gt, item(pm_seq, id(sci)), id(bv)),
6660 then_block: self.te_block(vec![
6661 Stmt::Set {
6662 target: bv,
6663 value: item(pm_seq, id(sci)),
6664 },
6665 Stmt::Set {
6666 target: bi,
6667 value: id(sci),
6668 },
6669 ]),
6670 else_block: None,
6671 },
6672 incr(sci),
6673 ],
6674 );
6675 let mut round = vec![
6676 Stmt::Let {
6677 var: bv,
6678 ty: None,
6679 value: fl(-1.0e30),
6680 mutable: true,
6681 },
6682 Stmt::Let {
6683 var: bi,
6684 ty: None,
6685 value: it(1),
6686 mutable: true,
6687 },
6688 Stmt::Let {
6689 var: sci,
6690 ty: None,
6691 value: it(1),
6692 mutable: true,
6693 },
6694 scan,
6695 Stmt::Set {
6696 target: wsum,
6697 value: add(id(wsum), item(p_seq, id(bi))),
6698 },
6699 Stmt::SetIndex {
6700 collection: id(w_seq),
6701 index: id(bi),
6702 value: item(p_seq, id(bi)),
6703 },
6704 ];
6705 if let Some(s_seq) = s_seq {
6706 round.push(Stmt::SetIndex {
6707 collection: id(s_seq),
6708 index: id(bi),
6709 value: fl(1.0),
6710 });
6711 }
6712 round.extend([
6713 Stmt::SetIndex {
6714 collection: id(pm_seq),
6715 index: id(bi),
6716 value: fl(-1.0e30),
6717 },
6718 incr(kk),
6719 ]);
6720 let round_loop = while_le(kk, k, round);
6721 let renorm = while_le(
6722 rni,
6723 ne,
6724 vec![
6725 Stmt::SetIndex {
6726 collection: id(w_seq),
6727 index: id(rni),
6728 value: dv(item(w_seq, id(rni)), id(wsum)),
6729 },
6730 incr(rni),
6731 ],
6732 );
6733
6734 let out_zero = while_le(
6736 ozi,
6737 d,
6738 vec![
6739 Stmt::Push {
6740 value: fl(0.0),
6741 collection: id(var),
6742 },
6743 incr(ozi),
6744 ],
6745 );
6746 let wg_ix = add(
6748 mul(
6749 add(mul(sub(id(ee), it(1)), it(dff)), sub(id(gig), it(1))),
6750 it(d),
6751 ),
6752 id(trg),
6753 );
6754 let gate_inner = while_le(
6755 trg,
6756 d,
6757 vec![
6758 Stmt::Set {
6759 target: gacc,
6760 value: add(id(gacc), mul(item(x, id(trg)), item(wg, wg_ix))),
6761 },
6762 incr(trg),
6763 ],
6764 );
6765 let gate_loop = while_le(
6766 gig,
6767 dff,
6768 vec![
6769 Stmt::Let {
6770 var: gacc,
6771 ty: None,
6772 value: fl(0.0),
6773 mutable: true,
6774 },
6775 Stmt::Let {
6776 var: trg,
6777 ty: None,
6778 value: it(1),
6779 mutable: true,
6780 },
6781 gate_inner,
6782 Stmt::Push {
6783 value: {
6784 let product = if opts.weigh_inputs { mul(item(w_seq, id(ee)), id(gacc)) } else { id(gacc) };
6786 match opts.biases[0] {
6787 Some(bg) => add(product, item(bg, add(mul(sub(id(ee), it(1)), it(dff)), id(gig)))),
6788 None => product,
6789 }
6790 },
6791 collection: id(gate),
6792 },
6793 incr(gig),
6794 ],
6795 );
6796 let wu_ix = add(
6797 mul(
6798 add(mul(sub(id(ee), it(1)), it(dff)), sub(id(giu), it(1))),
6799 it(d),
6800 ),
6801 id(tru),
6802 );
6803 let up_inner = while_le(
6804 tru,
6805 d,
6806 vec![
6807 Stmt::Set {
6808 target: uacc,
6809 value: add(id(uacc), mul(item(x, id(tru)), item(wu, wu_ix))),
6810 },
6811 incr(tru),
6812 ],
6813 );
6814 let up_loop = while_le(
6815 giu,
6816 dff,
6817 vec![
6818 Stmt::Let {
6819 var: uacc,
6820 ty: None,
6821 value: fl(0.0),
6822 mutable: true,
6823 },
6824 Stmt::Let {
6825 var: tru,
6826 ty: None,
6827 value: it(1),
6828 mutable: true,
6829 },
6830 up_inner,
6831 Stmt::Push {
6832 value: {
6833 let product = if opts.weigh_inputs { mul(item(w_seq, id(ee)), id(uacc)) } else { id(uacc) };
6835 match opts.biases[1] {
6836 Some(bu) => add(product, item(bu, add(mul(sub(id(ee), it(1)), it(dff)), id(giu)))),
6837 None => product,
6838 }
6839 },
6840 collection: id(up),
6841 },
6842 incr(giu),
6843 ],
6844 );
6845 let wd_ix = add(
6847 mul(
6848 add(mul(sub(id(ee), it(1)), it(d)), sub(id(oo), it(1))),
6849 it(dff),
6850 ),
6851 id(di),
6852 );
6853 let he_t = match opts.act {
6854 MoeAct::Swiglu => mul(silu(item(gate, id(di))), item(up, id(di))),
6855 MoeAct::ClampedSwiglu { limit, slope } => {
6857 let gc = self.te_call(min_sym, vec![item(gate, id(di)), fl(limit)]);
6858 let uc = self.te_call(max_sym, vec![self.te_call(min_sym, vec![item(up, id(di)), fl(limit)]), fl(-limit)]);
6859 mul(dv(gc, add(fl(1.0), expo(sub(fl(0.0), mul(fl(slope), gc))))), add(uc, fl(1.0)))
6860 }
6861 };
6862 let down_inner = while_le(
6863 di,
6864 dff,
6865 vec![
6866 Stmt::Set {
6867 target: dacc,
6868 value: add(id(dacc), mul(he_t, item(wd, wd_ix))),
6869 },
6870 incr(di),
6871 ],
6872 );
6873 let down_loop = while_le(
6874 oo,
6875 d,
6876 vec![
6877 Stmt::Let {
6878 var: dacc,
6879 ty: None,
6880 value: fl(0.0),
6881 mutable: true,
6882 },
6883 Stmt::Let {
6884 var: di,
6885 ty: None,
6886 value: it(1),
6887 mutable: true,
6888 },
6889 down_inner,
6890 Stmt::SetIndex {
6891 collection: id(var),
6892 index: id(oo),
6893 value: add(
6894 item(var, id(oo)),
6895 mul(
6896 item(s_seq.unwrap_or(w_seq), id(ee)),
6897 match opts.biases[2] {
6898 Some(bd) => add(id(dacc), item(bd, add(mul(sub(id(ee), it(1)), it(d)), id(oo)))),
6899 None => id(dacc),
6900 },
6901 ),
6902 ),
6903 },
6904 incr(oo),
6905 ],
6906 );
6907 let expert_loop = while_le(
6908 ee,
6909 ne,
6910 vec![
6911 Stmt::Let {
6912 var: gate,
6913 ty: None,
6914 value: nseq(),
6915 mutable: true,
6916 },
6917 Stmt::Let {
6918 var: gig,
6919 ty: None,
6920 value: it(1),
6921 mutable: true,
6922 },
6923 gate_loop,
6924 Stmt::Let {
6925 var: up,
6926 ty: None,
6927 value: nseq(),
6928 mutable: true,
6929 },
6930 Stmt::Let {
6931 var: giu,
6932 ty: None,
6933 value: it(1),
6934 mutable: true,
6935 },
6936 up_loop,
6937 Stmt::Let {
6938 var: oo,
6939 ty: None,
6940 value: it(1),
6941 mutable: true,
6942 },
6943 down_loop,
6944 incr(ee),
6945 ],
6946 );
6947
6948 let mut body = vec![
6949 Stmt::Let {
6951 var: l_seq,
6952 ty: None,
6953 value: nseq(),
6954 mutable: true,
6955 },
6956 Stmt::Let {
6957 var: er,
6958 ty: None,
6959 value: it(1),
6960 mutable: true,
6961 },
6962 router_loop,
6963 ];
6964 if opts.score == MoeScore::Softmax {
6966 body.extend([
6967 Stmt::Let {
6968 var: smm,
6969 ty: None,
6970 value: item(l_seq, it(1)),
6971 mutable: true,
6972 },
6973 Stmt::Let {
6974 var: smi1,
6975 ty: None,
6976 value: it(2),
6977 mutable: true,
6978 },
6979 max_loop,
6980 Stmt::Let {
6981 var: sms,
6982 ty: None,
6983 value: fl(0.0),
6984 mutable: true,
6985 },
6986 Stmt::Let {
6987 var: smi2,
6988 ty: None,
6989 value: it(1),
6990 mutable: true,
6991 },
6992 sum_loop,
6993 ]);
6994 }
6995 body.extend([
6996 Stmt::Let {
6997 var: p_seq,
6998 ty: None,
6999 value: nseq(),
7000 mutable: true,
7001 },
7002 Stmt::Let {
7003 var: smi3,
7004 ty: None,
7005 value: it(1),
7006 mutable: true,
7007 },
7008 norm_loop,
7009 Stmt::Let {
7011 var: pm_seq,
7012 ty: None,
7013 value: nseq(),
7014 mutable: true,
7015 },
7016 ]);
7017 if sel_seq != pm_seq {
7018 body.push(Stmt::Let {
7019 var: sel_seq,
7020 ty: None,
7021 value: nseq(),
7022 mutable: true,
7023 });
7024 }
7025 body.extend([
7026 Stmt::Let {
7027 var: pci,
7028 ty: None,
7029 value: it(1),
7030 mutable: true,
7031 },
7032 pm_copy,
7033 ]);
7034 body.extend(group_block.into_iter().flatten());
7035 body.push(Stmt::Let {
7036 var: w_seq,
7037 ty: None,
7038 value: nseq(),
7039 mutable: true,
7040 });
7041 if let Some(s_seq) = s_seq {
7042 body.push(Stmt::Let {
7043 var: s_seq,
7044 ty: None,
7045 value: nseq(),
7046 mutable: true,
7047 });
7048 }
7049 body.extend([
7050 Stmt::Let {
7051 var: wzi,
7052 ty: None,
7053 value: it(1),
7054 mutable: true,
7055 },
7056 w_zero,
7057 Stmt::Let {
7058 var: wsum,
7059 ty: None,
7060 value: fl(0.0),
7061 mutable: true,
7062 },
7063 Stmt::Let {
7064 var: kk,
7065 ty: None,
7066 value: it(1),
7067 mutable: true,
7068 },
7069 round_loop,
7070 ]);
7071 if opts.renormalize {
7072 body.extend([
7073 Stmt::Set {
7074 target: wsum,
7075 value: self.te_call(max_sym, vec![id(wsum), fl(6.103515625e-5)]),
7076 },
7077 Stmt::Let {
7078 var: rni,
7079 ty: None,
7080 value: it(1),
7081 mutable: true,
7082 },
7083 renorm,
7084 ]);
7085 }
7086 if let (Some(scale), Some(sc)) = (opts.scale, scale_i) {
7087 body.extend([
7088 Stmt::Let {
7089 var: sc,
7090 ty: None,
7091 value: it(1),
7092 mutable: true,
7093 },
7094 while_le(
7095 sc,
7096 ne,
7097 vec![
7098 Stmt::SetIndex {
7099 collection: id(w_seq),
7100 index: id(sc),
7101 value: mul(item(w_seq, id(sc)), fl(scale)),
7102 },
7103 incr(sc),
7104 ],
7105 ),
7106 ]);
7107 }
7108 body.extend([
7109 Stmt::Let {
7111 var,
7112 ty: None,
7113 value: nseq(),
7114 mutable: true,
7115 },
7116 Stmt::Let {
7117 var: ozi,
7118 ty: None,
7119 value: it(1),
7120 mutable: true,
7121 },
7122 out_zero,
7123 Stmt::Let {
7124 var: ee,
7125 ty: None,
7126 value: it(1),
7127 mutable: true,
7128 },
7129 expert_loop,
7130 ]);
7131 let body = self.te_block(body);
7132 Stmt::Splice { body }
7133 }
7134
7135 fn desugar_tensor_attention(
7145 &mut self,
7146 var: Symbol,
7147 q: Symbol,
7148 kc: Symbol,
7149 vc: Symbol,
7150 shape: AttentionShape,
7151 upto: Option<&'a Expr<'a>>,
7152 options: AttentionOptions,
7153 ) -> Stmt<'a> {
7154 self.desugar_tensor_attention_block(var, q, kc, vc, shape, upto, options)
7155 }
7156
7157 fn desugar_tensor_attention_block(
7162 &mut self,
7163 var: Symbol,
7164 q: Symbol,
7165 kc: Symbol,
7166 vc: Symbol,
7167 shape: AttentionShape,
7168 upto: Option<&'a Expr<'a>>,
7169 options: AttentionOptions,
7170 ) -> Stmt<'a> {
7171 let AttentionShape { h, kv, hd, vd, l, rows } = shape;
7172 let AttentionOptions { window, scale, cap, sinks } = options;
7173 let group = if kv > 0 { h / kv } else { 1 };
7174 let scale = scale.unwrap_or(1.0f64 / (hd as f64).sqrt());
7175 const EU: f64 = std::f64::consts::E;
7176 let real_sym = self.interner.intern("Real");
7177 let seq_sym = self.interner.intern("Seq");
7178 let max_sym = self.interner.intern("max");
7179 let (head, kvh, scores, ozi) = (
7181 self.fresh_desugar_temp("at_head"),
7182 self.fresh_desugar_temp("at_kvh"),
7183 self.fresh_desugar_temp("at_sc"),
7184 self.fresh_desugar_temp("at_ozi"),
7185 );
7186 let (j_sc, e_sc, acc_sc) = (
7187 self.fresh_desugar_temp("at_jsc"),
7188 self.fresh_desugar_temp("at_esc"),
7189 self.fresh_desugar_temp("at_asc"),
7190 );
7191 let (mx, si_mx, si_dn, den) = (
7192 self.fresh_desugar_temp("at_mx"),
7193 self.fresh_desugar_temp("at_simx"),
7194 self.fresh_desugar_temp("at_sidn"),
7195 self.fresh_desugar_temp("at_den"),
7196 );
7197 let (e_wv, j_wv, acc_wv) = (
7198 self.fresh_desugar_temp("at_ewv"),
7199 self.fresh_desugar_temp("at_jwv"),
7200 self.fresh_desugar_temp("at_awv"),
7201 );
7202 let windowed = window.map(|w| (w, self.fresh_desugar_temp("at_lo"), self.fresh_desugar_temp("at_cnt")));
7204 let block = rows > 1;
7207 let (len, len_check) = match upto {
7208 Some(p) if block => {
7209 let (from, check) = self.checked_attention_block(p, kc, l, rows);
7210 (Some(from), Some(check))
7211 }
7212 Some(p) => {
7213 let (len, check) = self.checked_attention_length(p, kc, l);
7214 (Some(len), Some(check))
7215 }
7216 None => (None, None),
7217 };
7218 let (row, row_len) = if block {
7219 (
7220 Some(self.fresh_desugar_temp("at_row")),
7221 Some(self.fresh_desugar_temp("at_rlen")),
7222 )
7223 } else {
7224 (None, None)
7225 };
7226 let bound = if block { row_len } else { len };
7227 let id = |s: Symbol| self.te_id(s);
7228 let fl = |v: f64| self.te_float(v);
7229 let it = |n: i64| self.te_int(n);
7230 let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
7231 let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
7232 let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
7233 let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
7234 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
7235 let nseq = || {
7236 self.ctx.alloc_imperative_expr(Expr::New {
7237 type_name: seq_sym,
7238 type_args: vec![TypeExpr::Named(real_sym)],
7239 init_fields: vec![],
7240 })
7241 };
7242 let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
7243 let incr = |s: Symbol| Stmt::Set {
7244 target: s,
7245 value: add(id(s), it(1)),
7246 };
7247 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
7248 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
7249 body: self.te_block(body),
7250 decreasing: None,
7251 };
7252 let over_cache = |vv: Symbol, body: Vec<Stmt<'a>>| match bound {
7253 Some(n) => Stmt::While {
7254 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(n)),
7255 body: self.te_block(body),
7256 decreasing: None,
7257 },
7258 None => while_le(vv, l, body),
7259 };
7260 let attended = match bound {
7262 Some(n) => id(n),
7263 None => it(l),
7264 };
7265 let first = match windowed {
7266 Some((_, lo, _)) => id(lo),
7267 None => it(1),
7268 };
7269 let over_scores = |vv: Symbol, body: Vec<Stmt<'a>>| match windowed {
7270 Some((_, _, cnt)) => Stmt::While {
7271 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(cnt)),
7272 body: self.te_block(body),
7273 decreasing: None,
7274 },
7275 None => over_cache(vv, body),
7276 };
7277 let score_of = |j: Symbol| match windowed {
7278 Some((_, lo, _)) => item(scores, add(sub(id(j), id(lo)), it(1))),
7279 None => item(scores, id(j)),
7280 };
7281 let in_row = |at: &'a Expr<'a>, width: i64| match row {
7283 Some(r) => add(mul(sub(id(r), it(1)), it(h * width)), at),
7284 None => at,
7285 };
7286 let q_read = in_row(add(mul(sub(id(head), it(1)), it(hd)), id(e_sc)), hd);
7289 let k_read = add(
7290 add(mul(sub(id(j_sc), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7291 id(e_sc),
7292 );
7293 let v_read = add(
7294 add(mul(sub(id(j_wv), it(1)), it(kv * vd)), mul(id(kvh), it(vd))),
7295 id(e_wv),
7296 );
7297 let ctx_write = in_row(add(mul(sub(id(head), it(1)), it(vd)), id(e_wv)), vd);
7298 let score_inner = while_le(
7300 e_sc,
7301 hd,
7302 vec![
7303 Stmt::Set {
7304 target: acc_sc,
7305 value: add(id(acc_sc), mul(item(q, q_read), item(kc, k_read))),
7306 },
7307 incr(e_sc),
7308 ],
7309 );
7310 let score_loop = over_cache(
7311 j_sc,
7312 vec![
7313 Stmt::Let {
7314 var: acc_sc,
7315 ty: None,
7316 value: fl(0.0),
7317 mutable: true,
7318 },
7319 Stmt::Let {
7320 var: e_sc,
7321 ty: None,
7322 value: it(1),
7323 mutable: true,
7324 },
7325 score_inner,
7326 Stmt::Push {
7327 value: match cap {
7328 Some(c) => self.capped(mul(id(acc_sc), fl(scale)), c),
7329 None => mul(id(acc_sc), fl(scale)),
7330 },
7331 collection: id(scores),
7332 },
7333 incr(j_sc),
7334 ],
7335 );
7336 let max_loop = over_scores(
7338 si_mx,
7339 vec![
7340 Stmt::Set {
7341 target: mx,
7342 value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
7343 },
7344 incr(si_mx),
7345 ],
7346 );
7347 let den_loop = over_scores(
7348 si_dn,
7349 vec![
7350 Stmt::Set {
7351 target: den,
7352 value: add(id(den), expo(sub(item(scores, id(si_dn)), id(mx)))),
7353 },
7354 incr(si_dn),
7355 ],
7356 );
7357 let wv_inner = over_cache(
7359 j_wv,
7360 vec![
7361 Stmt::Set {
7362 target: acc_wv,
7363 value: add(
7364 id(acc_wv),
7365 mul(
7366 dv(expo(sub(score_of(j_wv), id(mx))), id(den)),
7367 item(vc, v_read),
7368 ),
7369 ),
7370 },
7371 incr(j_wv),
7372 ],
7373 );
7374 let wv_loop = while_le(
7375 e_wv,
7376 vd,
7377 vec![
7378 Stmt::Let {
7379 var: acc_wv,
7380 ty: None,
7381 value: fl(0.0),
7382 mutable: true,
7383 },
7384 Stmt::Let {
7385 var: j_wv,
7386 ty: None,
7387 value: first,
7388 mutable: true,
7389 },
7390 wv_inner,
7391 Stmt::SetIndex {
7392 collection: id(var),
7393 index: ctx_write,
7394 value: id(acc_wv),
7395 },
7396 incr(e_wv),
7397 ],
7398 );
7399 let mut head_body = vec![
7400 Stmt::Let {
7401 var: kvh,
7402 ty: None,
7403 value: dv(sub(id(head), it(1)), it(group)),
7404 mutable: false,
7405 },
7406 Stmt::Let {
7407 var: scores,
7408 ty: None,
7409 value: nseq(),
7410 mutable: true,
7411 },
7412 ];
7413 if let Some((w, lo, cnt)) = windowed {
7414 head_body.extend([
7415 Stmt::Let {
7416 var: lo,
7417 ty: None,
7418 value: match w {
7419 Window::Sliding(w) => self.te_call(max_sym, vec![it(1), sub(attended, it(w - 1))]),
7420 Window::Chunked(c) => add(mul(dv(sub(attended, it(1)), it(c)), it(c)), it(1)),
7421 },
7422 mutable: false,
7423 },
7424 Stmt::Let {
7425 var: cnt,
7426 ty: None,
7427 value: add(sub(attended, id(lo)), it(1)),
7428 mutable: false,
7429 },
7430 ]);
7431 }
7432 head_body.extend([
7433 Stmt::Let {
7434 var: j_sc,
7435 ty: None,
7436 value: first,
7437 mutable: true,
7438 },
7439 score_loop,
7440 Stmt::Let {
7441 var: mx,
7442 ty: None,
7443 value: match sinks {
7444 Some(sk) => self.te_call(max_sym, vec![item(scores, it(1)), item(sk, id(head))]),
7445 None => item(scores, it(1)),
7446 },
7447 mutable: true,
7448 },
7449 Stmt::Let {
7450 var: si_mx,
7451 ty: None,
7452 value: it(2),
7453 mutable: true,
7454 },
7455 max_loop,
7456 Stmt::Let {
7457 var: den,
7458 ty: None,
7459 value: match sinks {
7460 Some(sk) => expo(sub(item(sk, id(head)), id(mx))),
7461 None => fl(0.0),
7462 },
7463 mutable: true,
7464 },
7465 Stmt::Let {
7466 var: si_dn,
7467 ty: None,
7468 value: it(1),
7469 mutable: true,
7470 },
7471 den_loop,
7472 Stmt::Let {
7473 var: e_wv,
7474 ty: None,
7475 value: it(1),
7476 mutable: true,
7477 },
7478 wv_loop,
7479 incr(head),
7480 ]);
7481 let head_loop = while_le(head, h, head_body);
7482 let out_zero = while_le(
7483 ozi,
7484 rows * h * vd,
7485 vec![
7486 Stmt::Push {
7487 value: fl(0.0),
7488 collection: id(var),
7489 },
7490 incr(ozi),
7491 ],
7492 );
7493 let mut stmts: Vec<Stmt<'a>> = len_check.map(Vec::from).unwrap_or_default();
7494 stmts.extend([
7495 Stmt::Let {
7496 var,
7497 ty: None,
7498 value: nseq(),
7499 mutable: true,
7500 },
7501 Stmt::Let {
7502 var: ozi,
7503 ty: None,
7504 value: it(1),
7505 mutable: true,
7506 },
7507 out_zero,
7508 ]);
7509 match (row, row_len, len) {
7510 (Some(r), Some(rl), Some(from)) => stmts.extend([
7512 Stmt::Let {
7513 var: r,
7514 ty: None,
7515 value: it(1),
7516 mutable: true,
7517 },
7518 while_le(
7519 r,
7520 rows,
7521 vec![
7522 Stmt::Let {
7523 var: rl,
7524 ty: None,
7525 value: sub(add(id(from), id(r)), it(1)),
7526 mutable: false,
7527 },
7528 Stmt::Let {
7529 var: head,
7530 ty: None,
7531 value: it(1),
7532 mutable: true,
7533 },
7534 head_loop,
7535 incr(r),
7536 ],
7537 ),
7538 ]),
7539 _ => stmts.extend([
7540 Stmt::Let {
7541 var: head,
7542 ty: None,
7543 value: it(1),
7544 mutable: true,
7545 },
7546 head_loop,
7547 ]),
7548 }
7549 let body = self.te_block(stmts);
7550 Stmt::Splice { body }
7551 }
7552
7553 #[allow(clippy::too_many_arguments)]
7566 fn desugar_tensor_lut_attention(
7567 &mut self,
7568 var: Symbol,
7569 q: Symbol,
7570 kc: Symbol,
7571 vc: Symbol,
7572 table: Symbol,
7573 (h, kv, hd, l, n): (i64, i64, i64, i64, i64),
7574 upto: Option<&'a Expr<'a>>,
7575 (m, s): (i64, i64),
7576 ) -> Stmt<'a> {
7577 let group = h / kv;
7578 let max_sym = self.interner.intern("max");
7579 let min_sym = self.interner.intern("min");
7580 let seq_sym = self.interner.intern("Seq");
7581 let int_sym = self.interner.intern("Int");
7582 let (head, kvh, scores, wts, ozi) = (
7583 self.fresh_desugar_temp("la_head"),
7584 self.fresh_desugar_temp("la_kvh"),
7585 self.fresh_desugar_temp("la_sc"),
7586 self.fresh_desugar_temp("la_w"),
7587 self.fresh_desugar_temp("la_ozi"),
7588 );
7589 let (j_sc, e_sc, acc_sc) = (
7590 self.fresh_desugar_temp("la_jsc"),
7591 self.fresh_desugar_temp("la_esc"),
7592 self.fresh_desugar_temp("la_asc"),
7593 );
7594 let (mx, si_mx, j_w, w, den) = (
7595 self.fresh_desugar_temp("la_mx"),
7596 self.fresh_desugar_temp("la_simx"),
7597 self.fresh_desugar_temp("la_jw"),
7598 self.fresh_desugar_temp("la_wt"),
7599 self.fresh_desugar_temp("la_den"),
7600 );
7601 let (e_wv, j_wv, num) = (
7602 self.fresh_desugar_temp("la_ewv"),
7603 self.fresh_desugar_temp("la_jwv"),
7604 self.fresh_desugar_temp("la_num"),
7605 );
7606 let (len, len_check) = match upto {
7607 Some(p) => {
7608 let (len, check) = self.checked_attention_length(p, kc, l);
7609 (Some(len), Some(check))
7610 }
7611 None => (None, None),
7612 };
7613 let id = |s: Symbol| self.te_id(s);
7614 let it = |v: i64| self.te_int(v);
7615 let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
7616 let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
7617 let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
7618 let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
7619 let int_seq = || {
7620 self.ctx.alloc_imperative_expr(Expr::New {
7621 type_name: seq_sym,
7622 type_args: vec![TypeExpr::Named(int_sym)],
7623 init_fields: vec![],
7624 })
7625 };
7626 let incr = |v: Symbol| Stmt::Set {
7627 target: v,
7628 value: add(id(v), it(1)),
7629 };
7630 let let_mut = |v: Symbol, value: &'a Expr<'a>| Stmt::Let {
7631 var: v,
7632 ty: None,
7633 value,
7634 mutable: true,
7635 };
7636 let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
7637 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
7638 body: self.te_block(body),
7639 decreasing: None,
7640 };
7641 let over_cache = |vv: Symbol, body: Vec<Stmt<'a>>| match len {
7642 Some(p) => Stmt::While {
7643 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(p)),
7644 body: self.te_block(body),
7645 decreasing: None,
7646 },
7647 None => while_le(vv, l, body),
7648 };
7649 let q_read = add(mul(sub(id(head), it(1)), it(hd)), id(e_sc));
7651 let k_read = add(
7652 add(mul(sub(id(j_sc), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7653 id(e_sc),
7654 );
7655 let v_read = add(
7656 add(mul(sub(id(j_wv), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7657 id(e_wv),
7658 );
7659 let c_write = add(mul(sub(id(head), it(1)), it(hd)), id(e_wv));
7660 let score_loop = over_cache(
7661 j_sc,
7662 vec![
7663 let_mut(acc_sc, it(0)),
7664 let_mut(e_sc, it(1)),
7665 while_le(
7666 e_sc,
7667 hd,
7668 vec![
7669 Stmt::Set {
7670 target: acc_sc,
7671 value: add(id(acc_sc), mul(item(q, q_read), item(kc, k_read))),
7672 },
7673 incr(e_sc),
7674 ],
7675 ),
7676 Stmt::Push {
7677 value: id(acc_sc),
7678 collection: id(scores),
7679 },
7680 incr(j_sc),
7681 ],
7682 );
7683 let max_loop = over_cache(
7684 si_mx,
7685 vec![
7686 Stmt::Set {
7687 target: mx,
7688 value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
7689 },
7690 incr(si_mx),
7691 ],
7692 );
7693 let lut_index = self.te_call(
7695 min_sym,
7696 vec![
7697 self.te_call(
7698 max_sym,
7699 vec![
7700 self.te_bin(
7701 BinaryOpKind::Shr,
7702 mul(sub(id(mx), item(scores, id(j_w))), it(m)),
7703 it(s),
7704 ),
7705 it(0),
7706 ],
7707 ),
7708 it(n - 1),
7709 ],
7710 );
7711 let weight_loop = over_cache(
7712 j_w,
7713 vec![
7714 Stmt::Let {
7715 var: w,
7716 ty: None,
7717 value: item(table, add(lut_index, it(1))),
7718 mutable: false,
7719 },
7720 Stmt::Push {
7721 value: id(w),
7722 collection: id(wts),
7723 },
7724 Stmt::Set {
7725 target: den,
7726 value: add(id(den), id(w)),
7727 },
7728 incr(j_w),
7729 ],
7730 );
7731 let ctx_loop = while_le(
7732 e_wv,
7733 hd,
7734 vec![
7735 let_mut(num, it(0)),
7736 let_mut(j_wv, it(1)),
7737 over_cache(
7738 j_wv,
7739 vec![
7740 Stmt::Set {
7741 target: num,
7742 value: add(id(num), mul(item(wts, id(j_wv)), item(vc, v_read))),
7743 },
7744 incr(j_wv),
7745 ],
7746 ),
7747 Stmt::SetIndex {
7748 collection: id(var),
7749 index: c_write,
7750 value: self.te_bin(BinaryOpKind::FloorDivide, id(num), id(den)),
7751 },
7752 incr(e_wv),
7753 ],
7754 );
7755 let head_loop = while_le(
7756 head,
7757 h,
7758 vec![
7759 Stmt::Let {
7760 var: kvh,
7761 ty: None,
7762 value: self.te_bin(BinaryOpKind::Divide, sub(id(head), it(1)), it(group)),
7763 mutable: false,
7764 },
7765 let_mut(scores, int_seq()),
7766 let_mut(j_sc, it(1)),
7767 score_loop,
7768 let_mut(mx, item(scores, it(1))),
7769 let_mut(si_mx, it(2)),
7770 max_loop,
7771 let_mut(wts, int_seq()),
7772 let_mut(den, it(0)),
7773 let_mut(j_w, it(1)),
7774 weight_loop,
7775 let_mut(e_wv, it(1)),
7776 ctx_loop,
7777 incr(head),
7778 ],
7779 );
7780 let mut stmts: Vec<Stmt<'a>> = len_check.map(Vec::from).unwrap_or_default();
7781 stmts.extend([
7782 let_mut(var, int_seq()),
7783 let_mut(ozi, it(1)),
7784 while_le(
7785 ozi,
7786 h * hd,
7787 vec![
7788 Stmt::Push {
7789 value: it(0),
7790 collection: id(var),
7791 },
7792 incr(ozi),
7793 ],
7794 ),
7795 let_mut(head, it(1)),
7796 head_loop,
7797 ]);
7798 let body = self.te_block(stmts);
7799 Stmt::Splice { body }
7800 }
7801
7802 fn checked_attention_length(
7805 &mut self,
7806 bound: &'a Expr<'a>,
7807 cache: Symbol,
7808 rows: i64,
7809 ) -> (Symbol, [Stmt<'a>; 2]) {
7810 let len = self.fresh_desugar_temp("at_len");
7811 let in_range = self.te_bin(
7812 BinaryOpKind::And,
7813 self.te_bin(BinaryOpKind::GtEq, self.te_id(len), self.te_int(1)),
7814 self.te_bin(BinaryOpKind::LtEq, self.te_id(len), self.te_int(rows)),
7815 );
7816 let message = format!("attention length out of range: {} has {rows} positions, and attention needs at least 1", self.interner.resolve(cache));
7817 let message = self.interner.intern(&message);
7818 let reason = self
7819 .ctx
7820 .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
7821 (
7822 len,
7823 [
7824 Stmt::Let {
7825 var: len,
7826 ty: None,
7827 value: bound,
7828 mutable: false,
7829 },
7830 Stmt::RuntimeAssert {
7831 condition: in_range,
7832 hard: true,
7833 dim: None,
7834 reason: Some(reason),
7835 },
7836 ],
7837 )
7838 }
7839
7840 fn checked_attention_block(
7843 &mut self,
7844 from: &'a Expr<'a>,
7845 cache: Symbol,
7846 l: i64,
7847 rows: i64,
7848 ) -> (Symbol, [Stmt<'a>; 2]) {
7849 let first = self.fresh_desugar_temp("at_from");
7850 let last = self.te_bin(BinaryOpKind::Add, self.te_id(first), self.te_int(rows - 1));
7851 let in_range = self.te_bin(
7852 BinaryOpKind::And,
7853 self.te_bin(BinaryOpKind::GtEq, self.te_id(first), self.te_int(1)),
7854 self.te_bin(BinaryOpKind::LtEq, last, self.te_int(l)),
7855 );
7856 let message = format!(
7857 "attention length out of range: {} has {l} positions, and a block of {rows} rows from P needs P ≥ 1 and P + {} ≤ {l}",
7858 self.interner.resolve(cache),
7859 rows - 1
7860 );
7861 let message = self.interner.intern(&message);
7862 let reason = self
7863 .ctx
7864 .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
7865 (
7866 first,
7867 [
7868 Stmt::Let {
7869 var: first,
7870 ty: None,
7871 value: from,
7872 mutable: false,
7873 },
7874 Stmt::RuntimeAssert {
7875 condition: in_range,
7876 hard: true,
7877 dim: None,
7878 reason: Some(reason),
7879 },
7880 ],
7881 )
7882 }
7883
7884 fn parse_row_index(&mut self) -> ParseResult<Option<&'a Expr<'a>>> {
7891 if let TokenType::Number(sym) = self.peek().kind {
7892 self.advance();
7893 let text = self.interner.resolve(sym).to_string();
7894 return match self.parse_i64_numeral_signed(&text, false) {
7895 Ok(n) => Ok(Some(self.te_int(n))),
7896 Err(_) => Err(ParseError {
7897 kind: ParseErrorKind::Custom(format!(
7898 "a row index is a whole number — got `{text}`."
7899 )),
7900 span: self.current_span(),
7901 }),
7902 };
7903 }
7904 if self.check(&TokenType::Minus)
7905 && matches!(
7906 self.tokens.get(self.current + 1).map(|t| &t.kind),
7907 Some(TokenType::Number(_))
7908 )
7909 {
7910 self.advance();
7911 let TokenType::Number(sym) = self.peek().kind else {
7912 unreachable!()
7913 };
7914 self.advance();
7915 let text = self.interner.resolve(sym).to_string();
7916 let n = self
7917 .parse_i64_numeral_signed(&text, true)
7918 .map_err(|_| ParseError {
7919 kind: ParseErrorKind::Custom(format!(
7920 "a row index is a whole number — got `-{text}`."
7921 )),
7922 span: self.current_span(),
7923 })?;
7924 return Ok(Some(self.te_int(n)));
7925 }
7926 if self.check(&TokenType::LParen) {
7927 self.advance();
7928 let inner = self.parse_imperative_expr()?;
7929 if !self.check(&TokenType::RParen) {
7930 return Err(ParseError {
7931 kind: ParseErrorKind::ExpectedKeyword {
7932 keyword: ")".to_string(),
7933 },
7934 span: self.current_span(),
7935 });
7936 }
7937 self.advance();
7938 return Ok(Some(inner));
7939 }
7940 if self.check_preposition_is("of") || self.is_at_end() {
7941 return Ok(None);
7942 }
7943 let sym = self.peek().lexeme;
7944 self.advance();
7945 Ok(Some(self.te_id(sym)))
7946 }
7947
7948 fn try_parse_row_of(&mut self) -> ParseResult<Option<(&'a Expr<'a>, Symbol, i64, i64)>> {
7951 if !self.check_word("row") {
7952 return Ok(None);
7953 }
7954 let saved = self.backtrack_point();
7955 self.advance();
7956 let index = match self.parse_row_index() {
7957 Ok(Some(e)) => e,
7958 _ => {
7959 self.backtrack_to(&saved);
7960 return Ok(None);
7961 }
7962 };
7963 if !self.check_preposition_is("of") {
7964 self.backtrack_to(&saved);
7965 return Ok(None);
7966 }
7967 self.advance();
7968 let table = self.peek().lexeme;
7969 let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
7970 self.backtrack_to(&saved);
7971 return Ok(None);
7972 };
7973 self.advance();
7974 Ok(Some((index, table, rows, cols)))
7975 }
7976
7977 fn try_parse_tensor_rows(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
7981 if !(self.check_word("rows")
7982 && matches!(self.tokens.get(self.current + 2).map(|t| &t.kind), Some(TokenType::Preposition(sym)) if self.interner.resolve(*sym) == "of"))
7983 {
7984 return Ok(None);
7985 }
7986 let idx = self.tokens[self.current + 1].lexeme;
7987 let Some(&(ir, ic)) = self.tensor_shapes.get(&idx) else {
7988 return Ok(None);
7989 };
7990 self.advance();
7991 self.advance();
7992 self.advance();
7993 let idx_name = self.interner.resolve(idx).to_string();
7994 let table = self.peek().lexeme;
7995 let table_name = self.interner.resolve(table).to_string();
7996 let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
7997 return Err(ParseError {
7998 kind: ParseErrorKind::Custom(format!(
7999 "`rows {idx_name} of {table_name}` — {table_name} must be a declared tensor."
8000 )),
8001 span: self.current_span(),
8002 });
8003 };
8004 if !self.tensor_int.contains(&idx) {
8005 return Err(ParseError {
8006 kind: ParseErrorKind::Custom(format!(
8007 "`rows I of T` — the row indices {idx_name} must be a tensor of Int."
8008 )),
8009 span: self.current_span(),
8010 });
8011 }
8012 self.advance();
8013 let int = self.tensor_int.contains(&table);
8014 self.user_bound.insert(var);
8015 self.bind_tensor(var, (ir * ic, cols), int);
8016 Ok(Some(self.desugar_tensor_rows(
8017 var,
8018 idx,
8019 ir * ic,
8020 table,
8021 rows,
8022 cols,
8023 int,
8024 )))
8025 }
8026
8027 fn try_parse_tensor_row(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
8028 let Some((index, table, rows, cols)) = self.try_parse_row_of()? else {
8029 return Ok(None);
8030 };
8031 let int = self.tensor_int.contains(&table);
8032 self.user_bound.insert(var);
8033 self.bind_tensor(var, (1, cols), int);
8034 Ok(Some(
8035 self.desugar_tensor_row(var, index, table, rows, cols, int),
8036 ))
8037 }
8038
8039 fn try_parse_set_rows(&mut self) -> ParseResult<Option<Stmt<'a>>> {
8043 if !(self.check_word("rows") && self.peek_word_at(1, "from")) {
8044 return Ok(None);
8045 }
8046 let saved = self.backtrack_point();
8047 self.advance();
8048 self.advance();
8049 let index = match self.parse_row_index() {
8050 Ok(Some(e)) => e,
8051 _ => {
8052 self.backtrack_to(&saved);
8053 return Ok(None);
8054 }
8055 };
8056 if !self.check_preposition_is("of") {
8057 self.backtrack_to(&saved);
8058 return Ok(None);
8059 }
8060 self.advance();
8061 let table = self.peek().lexeme;
8062 let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
8063 self.backtrack_to(&saved);
8064 return Ok(None);
8065 };
8066 self.advance();
8067 let is_to = self.check(&TokenType::To)
8068 || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
8069 if !is_to {
8070 self.backtrack_to(&saved);
8071 return Ok(None);
8072 }
8073 self.advance();
8074 let source = self.peek().lexeme;
8075 let table_name = self.interner.resolve(table).to_string();
8076 let Some(&(sr, sc)) = self.tensor_shapes.get(&source) else {
8077 return Err(ParseError {
8078 kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to X` — X must be a declared tensor whose rows are {cols} wide.")),
8079 span: self.current_span(),
8080 });
8081 };
8082 let source_name = self.interner.resolve(source).to_string();
8083 if sc != cols {
8084 return Err(ParseError {
8085 kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — a row of {table_name} holds {cols} elements, and a row of {source_name} holds {sc}.")),
8086 span: self.current_span(),
8087 });
8088 }
8089 if sr < 1 || sr > rows {
8090 return Err(ParseError {
8091 kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — a block of {sr} rows cannot lie among {table_name}'s {rows}.")),
8092 span: self.current_span(),
8093 });
8094 }
8095 let (table_int, source_int) = (
8096 self.tensor_int.contains(&table),
8097 self.tensor_int.contains(&source),
8098 );
8099 if table_int != source_int {
8100 let kind = |int: bool| if int { "Int" } else { "Real" };
8101 return Err(ParseError {
8102 kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — {table_name} holds {} and {source_name} holds {}.", kind(table_int), kind(source_int))),
8103 span: self.current_span(),
8104 });
8105 }
8106 self.advance();
8107 Ok(Some(self.desugar_tensor_set_row(
8108 table, index, source, rows, cols, sr,
8109 )))
8110 }
8111
8112 fn try_parse_set_row(&mut self) -> ParseResult<Option<Stmt<'a>>> {
8116 if let Some(block) = self.try_parse_set_rows()? {
8117 return Ok(Some(block));
8118 }
8119 let saved = self.backtrack_point();
8120 let Some((index, table, rows, cols)) = self.try_parse_row_of()? else {
8121 return Ok(None);
8122 };
8123 let is_to = self.check(&TokenType::To)
8124 || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
8125 if !is_to {
8126 self.backtrack_to(&saved);
8127 return Ok(None);
8128 }
8129 self.advance();
8130 let source = self.peek().lexeme;
8131 let table_name = self.interner.resolve(table).to_string();
8132 let Some(&(sr, sc)) = self.tensor_shapes.get(&source) else {
8133 return Err(ParseError {
8134 kind: ParseErrorKind::Custom(format!("`Set row … of {table_name} to X` — X must be a declared tensor of {cols} elements.")),
8135 span: self.current_span(),
8136 });
8137 };
8138 let source_name = self.interner.resolve(source).to_string();
8139 if sr * sc != cols {
8140 return Err(ParseError {
8141 kind: ParseErrorKind::Custom(format!(
8142 "`Set row … of {table_name} to {source_name}` — a row of {table_name} holds {cols} elements, and {source_name} has {}.",
8143 sr * sc
8144 )),
8145 span: self.current_span(),
8146 });
8147 }
8148 let (table_int, source_int) = (
8149 self.tensor_int.contains(&table),
8150 self.tensor_int.contains(&source),
8151 );
8152 if table_int != source_int {
8153 let kind = |int: bool| if int { "Int" } else { "Real" };
8154 return Err(ParseError {
8155 kind: ParseErrorKind::Custom(format!(
8156 "`Set row … of {table_name} to {source_name}` — {table_name} holds {} and {source_name} holds {}.",
8157 kind(table_int),
8158 kind(source_int)
8159 )),
8160 span: self.current_span(),
8161 });
8162 }
8163 self.advance();
8164 Ok(Some(self.desugar_tensor_set_row(
8165 table, index, source, rows, cols, 1,
8166 )))
8167 }
8168
8169 fn checked_row_index(
8173 &mut self,
8174 index: &'a Expr<'a>,
8175 table: Symbol,
8176 rows: i64,
8177 ) -> (Symbol, [Stmt<'a>; 2]) {
8178 let message = format!(
8179 "row index out of range: {} has {rows} rows, numbered from 1",
8180 self.interner.resolve(table)
8181 );
8182 self.checked_first_row(index, rows, &message)
8183 }
8184
8185 fn checked_row_block(
8188 &mut self,
8189 index: &'a Expr<'a>,
8190 table: Symbol,
8191 rows: i64,
8192 count: i64,
8193 ) -> (Symbol, [Stmt<'a>; 2]) {
8194 let message = format!("row block out of range: {} has {rows} rows, numbered from 1, and a block of {count} rows must lie among them", self.interner.resolve(table));
8195 self.checked_first_row(index, rows - count + 1, &message)
8196 }
8197
8198 fn checked_first_row(
8200 &mut self,
8201 index: &'a Expr<'a>,
8202 last: i64,
8203 message: &str,
8204 ) -> (Symbol, [Stmt<'a>; 2]) {
8205 let at = self.fresh_desugar_temp("row_at");
8206 let in_range = self.te_bin(
8207 BinaryOpKind::And,
8208 self.te_bin(BinaryOpKind::GtEq, self.te_id(at), self.te_int(1)),
8209 self.te_bin(BinaryOpKind::LtEq, self.te_id(at), self.te_int(last)),
8210 );
8211 let message = self.interner.intern(message);
8212 let reason = self
8213 .ctx
8214 .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
8215 (
8216 at,
8217 [
8218 Stmt::Let {
8219 var: at,
8220 ty: None,
8221 value: index,
8222 mutable: false,
8223 },
8224 Stmt::RuntimeAssert {
8225 condition: in_range,
8226 hard: true,
8227 dim: None,
8228 reason: Some(reason),
8229 },
8230 ],
8231 )
8232 }
8233
8234 fn row_item(&self, at: Symbol, cols: i64, j: &'a Expr<'a>) -> &'a Expr<'a> {
8236 let row_base = self.te_bin(
8237 BinaryOpKind::Multiply,
8238 self.te_bin(BinaryOpKind::Subtract, self.te_id(at), self.te_int(1)),
8239 self.te_int(cols),
8240 );
8241 self.te_bin(
8242 BinaryOpKind::Add,
8243 self.te_bin(BinaryOpKind::Add, row_base, j),
8244 self.te_int(1),
8245 )
8246 }
8247
8248 fn desugar_tensor_row(
8251 &mut self,
8252 var: Symbol,
8253 index: &'a Expr<'a>,
8254 table: Symbol,
8255 rows: i64,
8256 cols: i64,
8257 int: bool,
8258 ) -> Stmt<'a> {
8259 let (at, check) = self.checked_row_index(index, table, rows);
8260 let seq_sym = self.interner.intern("Seq");
8261 let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
8262 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
8263 type_name: seq_sym,
8264 type_args: vec![TypeExpr::Named(elem_sym)],
8265 init_fields: vec![],
8266 });
8267 let j = self.fresh_desugar_temp("row_j");
8268 let item = self.row_item(at, cols, self.te_id(j));
8269 let loop_body = self.te_block(vec![
8270 Stmt::Push {
8271 value: self.te_index(self.te_id(table), item),
8272 collection: self.te_id(var),
8273 },
8274 Stmt::Set {
8275 target: j,
8276 value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8277 },
8278 ]);
8279 let [bind_at, assert_at] = check;
8280 let body = self.te_block(vec![
8281 bind_at,
8282 assert_at,
8283 Stmt::Let {
8284 var,
8285 ty: None,
8286 value: new_seq,
8287 mutable: true,
8288 },
8289 Stmt::Let {
8290 var: j,
8291 ty: None,
8292 value: self.te_int(0),
8293 mutable: true,
8294 },
8295 Stmt::While {
8296 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols)),
8297 body: loop_body,
8298 decreasing: None,
8299 },
8300 ]);
8301 Stmt::Splice { body }
8302 }
8303
8304 #[allow(clippy::too_many_arguments)]
8307 fn desugar_tensor_rows(
8308 &mut self,
8309 var: Symbol,
8310 idx: Symbol,
8311 n: i64,
8312 table: Symbol,
8313 rows: i64,
8314 cols: i64,
8315 int: bool,
8316 ) -> Stmt<'a> {
8317 let seq_sym = self.interner.intern("Seq");
8318 let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
8319 let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
8320 type_name: seq_sym,
8321 type_args: vec![TypeExpr::Named(elem_sym)],
8322 init_fields: vec![],
8323 });
8324 let k = self.fresh_desugar_temp("rows_k");
8325 let index = self.te_index(self.te_id(idx), self.te_id(k));
8326 let (at, check) = self.checked_row_index(index, table, rows);
8327 let j = self.fresh_desugar_temp("row_j");
8328 let item = self.row_item(at, cols, self.te_id(j));
8329 let copy = self.te_block(vec![
8330 Stmt::Push {
8331 value: self.te_index(self.te_id(table), item),
8332 collection: self.te_id(var),
8333 },
8334 Stmt::Set {
8335 target: j,
8336 value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8337 },
8338 ]);
8339 let [bind_at, assert_at] = check;
8340 let per_row = self.te_block(vec![
8341 bind_at,
8342 assert_at,
8343 Stmt::Let {
8344 var: j,
8345 ty: None,
8346 value: self.te_int(0),
8347 mutable: true,
8348 },
8349 Stmt::While {
8350 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols)),
8351 body: copy,
8352 decreasing: None,
8353 },
8354 Stmt::Set {
8355 target: k,
8356 value: self.te_bin(BinaryOpKind::Add, self.te_id(k), self.te_int(1)),
8357 },
8358 ]);
8359 let body = self.te_block(vec![
8360 Stmt::Let {
8361 var,
8362 ty: None,
8363 value: new_seq,
8364 mutable: true,
8365 },
8366 Stmt::Let {
8367 var: k,
8368 ty: None,
8369 value: self.te_int(1),
8370 mutable: true,
8371 },
8372 Stmt::While {
8373 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(k), self.te_int(n)),
8374 body: per_row,
8375 decreasing: None,
8376 },
8377 ]);
8378 Stmt::Splice { body }
8379 }
8380
8381 fn desugar_tensor_set_row(
8385 &mut self,
8386 table: Symbol,
8387 index: &'a Expr<'a>,
8388 source: Symbol,
8389 rows: i64,
8390 cols: i64,
8391 count: i64,
8392 ) -> Stmt<'a> {
8393 let (at, check) = if count == 1 {
8394 self.checked_row_index(index, table, rows)
8395 } else {
8396 self.checked_row_block(index, table, rows, count)
8397 };
8398 let j = self.fresh_desugar_temp("row_j");
8399 let item = self.row_item(at, cols, self.te_id(j));
8400 let loop_body = self.te_block(vec![
8401 Stmt::SetIndex {
8402 collection: self.te_id(table),
8403 index: item,
8404 value: self.te_index(
8405 self.te_id(source),
8406 self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8407 ),
8408 },
8409 Stmt::Set {
8410 target: j,
8411 value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8412 },
8413 ]);
8414 let [bind_at, assert_at] = check;
8415 let body = self.te_block(vec![
8416 bind_at,
8417 assert_at,
8418 Stmt::Let {
8419 var: j,
8420 ty: None,
8421 value: self.te_int(0),
8422 mutable: true,
8423 },
8424 Stmt::While {
8425 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols * count)),
8426 body: loop_body,
8427 decreasing: None,
8428 },
8429 ]);
8430 Stmt::Splice { body }
8431 }
8432
8433 fn desugar_tensor_argmax(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
8436 let best = self.fresh_desugar_temp("am_best");
8437 let at = self.fresh_desugar_temp("am_at");
8438 let i = self.fresh_desugar_temp("am_i");
8439 let then_block = self.te_block(vec![
8440 Stmt::Set {
8441 target: best,
8442 value: self.te_index(self.te_id(x), self.te_id(i)),
8443 },
8444 Stmt::Set {
8445 target: at,
8446 value: self.te_id(i),
8447 },
8448 ]);
8449 let loop_body = self.te_block(vec![
8450 Stmt::If {
8451 cond: self.te_bin(
8452 BinaryOpKind::Gt,
8453 self.te_index(self.te_id(x), self.te_id(i)),
8454 self.te_id(best),
8455 ),
8456 then_block,
8457 else_block: None,
8458 },
8459 Stmt::Set {
8460 target: i,
8461 value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
8462 },
8463 ]);
8464 let body = self.te_block(vec![
8465 Stmt::Let {
8466 var: best,
8467 ty: None,
8468 value: self.te_index(self.te_id(x), self.te_int(1)),
8469 mutable: true,
8470 },
8471 Stmt::Let {
8472 var: at,
8473 ty: None,
8474 value: self.te_int(1),
8475 mutable: true,
8476 },
8477 Stmt::Let {
8478 var: i,
8479 ty: None,
8480 value: self.te_int(2),
8481 mutable: true,
8482 },
8483 Stmt::While {
8484 cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
8485 body: loop_body,
8486 decreasing: None,
8487 },
8488 Stmt::Let {
8489 var,
8490 ty: None,
8491 value: self.te_id(at),
8492 mutable: false,
8493 },
8494 ]);
8495 Stmt::Splice { body }
8496 }
8497
8498 fn desugar_tensor_transpose(
8499 &mut self,
8500 var: Symbol,
8501 x: Symbol,
8502 rows: i64,
8503 cols: i64,
8504 int: bool,
8505 ) -> Stmt<'a> {
8506 let tj = self.fresh_desugar_temp("tp_j");
8507 let ti = self.fresh_desugar_temp("tp_i");
8508 let new_seq = self.new_tensor_seq(int);
8509 let idx = self.te_bin(
8511 BinaryOpKind::Add,
8512 self.te_bin(
8513 BinaryOpKind::Add,
8514 self.te_bin(BinaryOpKind::Multiply, self.te_id(ti), self.te_int(cols)),
8515 self.te_id(tj),
8516 ),
8517 self.te_int(1),
8518 );
8519 let inner_body = self.te_block(vec![
8520 Stmt::Push {
8521 value: self.te_index(self.te_id(x), idx),
8522 collection: self.te_id(var),
8523 },
8524 Stmt::Set {
8525 target: ti,
8526 value: self.te_bin(BinaryOpKind::Add, self.te_id(ti), self.te_int(1)),
8527 },
8528 ]);
8529 let inner = Stmt::While {
8530 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ti), self.te_int(rows)),
8531 body: inner_body,
8532 decreasing: None,
8533 };
8534 let outer_body = self.te_block(vec![
8535 Stmt::Let {
8536 var: ti,
8537 ty: None,
8538 value: self.te_int(0),
8539 mutable: true,
8540 },
8541 inner,
8542 Stmt::Set {
8543 target: tj,
8544 value: self.te_bin(BinaryOpKind::Add, self.te_id(tj), self.te_int(1)),
8545 },
8546 ]);
8547 let outer = Stmt::While {
8548 cond: self.te_bin(BinaryOpKind::Lt, self.te_id(tj), self.te_int(cols)),
8549 body: outer_body,
8550 decreasing: None,
8551 };
8552 let body = self.te_block(vec![
8553 Stmt::Let {
8554 var,
8555 ty: None,
8556 value: new_seq,
8557 mutable: true,
8558 },
8559 Stmt::Let {
8560 var: tj,
8561 ty: None,
8562 value: self.te_int(0),
8563 mutable: true,
8564 },
8565 outer,
8566 ]);
8567 Stmt::Splice { body }
8568 }
8569
8570 fn try_parse_tensor_unary(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
8573 if self.check_word("moe") {
8578 let saved = self.backtrack_point();
8579 self.advance();
8580 if !self.check_preposition_is("of") {
8581 self.backtrack_to(&saved);
8582 return Ok(None);
8583 }
8584 self.advance();
8585 let want = |s: &mut Self, w: &str| -> ParseResult<()> {
8586 if !s.check_word(w) {
8587 return Err(ParseError {
8588 kind: ParseErrorKind::Custom(format!(
8589 "`moe of X with router Wr gate Wg up Wu down Wd choosing K of E` — expected `{w}`."
8590 )),
8591 span: s.current_span(),
8592 });
8593 }
8594 s.advance();
8595 Ok(())
8596 };
8597 let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
8598 let sym = s.peek().lexeme;
8599 let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
8600 return Err(ParseError {
8601 kind: ParseErrorKind::Custom(format!(
8602 "`moe` {role} must be a declared tensor."
8603 )),
8604 span: s.current_span(),
8605 });
8606 };
8607 s.advance();
8608 Ok((sym, r, c))
8609 };
8610 let bias = |s: &mut Self, role: &str, shape: (i64, i64)| -> ParseResult<Option<Symbol>> {
8612 if !s.check_word("plus") {
8613 return Ok(None);
8614 }
8615 s.advance();
8616 let (b, r, c) = operand(s, role)?;
8617 if (r, c) != shape {
8618 return Err(ParseError {
8619 kind: ParseErrorKind::Custom(format!(
8620 "`moe … {role} plus B` — B must be {}×{}, and `{}` is {r}×{c}.",
8621 shape.0,
8622 shape.1,
8623 s.interner.resolve(b)
8624 )),
8625 span: s.current_span(),
8626 });
8627 }
8628 Ok(Some(b))
8629 };
8630 let (x, _xr, d) = operand(self, "input X")?;
8631 want(self, "with")?;
8632 want(self, "router")?;
8633 let (wr, _wrr, ne) = operand(self, "router Wr")?; let router_bias = bias(self, "router Wr", (1, ne))?;
8635 want(self, "gate")?;
8636 let (wg, wgr, _wgc) = operand(self, "gate Wg")?; let fd = if ne > 0 { wgr / ne } else { 0 };
8638 let gate_bias = bias(self, "gate Wg", (ne, fd))?;
8639 want(self, "up")?;
8640 let (wu, _wur, _wuc) = operand(self, "up Wu")?;
8641 let up_bias = bias(self, "up Wu", (ne, fd))?;
8642 want(self, "down")?;
8643 let (wd, _wdr, _wdc) = operand(self, "down Wd")?;
8644 let down_bias = bias(self, "down Wd", (ne, d))?;
8645 want(self, "choosing")?;
8646 let k = self.take_int_literal().ok_or_else(|| ParseError {
8647 kind: ParseErrorKind::Custom(
8648 "`moe … choosing K of E` — K must be an integer literal.".to_string(),
8649 ),
8650 span: self.current_span(),
8651 })?;
8652 if !self.check_preposition_is("of") {
8653 return Err(ParseError {
8654 kind: ParseErrorKind::Custom(
8655 "`moe … choosing K of E` — expected `of E`.".to_string(),
8656 ),
8657 span: self.current_span(),
8658 });
8659 }
8660 self.advance();
8661 let ne_lit = self.take_int_literal().ok_or_else(|| ParseError {
8662 kind: ParseErrorKind::Custom(
8663 "`moe … choosing K of E` — E must be an integer literal.".to_string(),
8664 ),
8665 span: self.current_span(),
8666 })?;
8667 let score = if self.check_word("by") {
8669 self.advance();
8670 if !self.check_word("sigmoid") {
8671 return Err(ParseError {
8672 kind: ParseErrorKind::Custom(
8673 "`moe … choosing K of E by sigmoid` — expected `sigmoid` after `by`.".to_string(),
8674 ),
8675 span: self.current_span(),
8676 });
8677 }
8678 self.advance();
8679 MoeScore::Sigmoid
8680 } else {
8681 MoeScore::Softmax
8682 };
8683 let selection_bias = if self.check_word("biased") {
8685 self.advance();
8686 if !self.check_word("by") {
8687 return Err(ParseError {
8688 kind: ParseErrorKind::Custom("`moe … biased by B` — expected `by` after `biased`.".to_string()),
8689 span: self.current_span(),
8690 });
8691 }
8692 self.advance();
8693 let (b, r, c) = operand(self, "selection bias B of `biased by B`")?;
8694 if (r, c) != (1, ne_lit) {
8695 return Err(ParseError {
8696 kind: ParseErrorKind::Custom(format!(
8697 "`moe … biased by B` — B must be 1×{ne_lit}, and `{}` is {r}×{c}.",
8698 self.interner.resolve(b)
8699 )),
8700 span: self.current_span(),
8701 });
8702 }
8703 Some(b)
8704 } else {
8705 None
8706 };
8707 let groups = if self.check_word("within") {
8709 self.advance();
8710 let clause = "`moe … within the top G of N groups`";
8711 for w in ["the", "top"] {
8712 if !self.check_word(w) {
8713 return Err(ParseError {
8714 kind: ParseErrorKind::Custom(format!("{clause} — expected `{w}`.")),
8715 span: self.current_span(),
8716 });
8717 }
8718 self.advance();
8719 }
8720 let kept = self.take_int_literal().ok_or_else(|| ParseError {
8721 kind: ParseErrorKind::Custom(format!("{clause} — G must be an integer literal.")),
8722 span: self.current_span(),
8723 })?;
8724 if !self.check_preposition_is("of") {
8725 return Err(ParseError {
8726 kind: ParseErrorKind::Custom(format!("{clause} — expected `of N groups`.")),
8727 span: self.current_span(),
8728 });
8729 }
8730 self.advance();
8731 let count = self.take_int_literal().ok_or_else(|| ParseError {
8732 kind: ParseErrorKind::Custom(format!("{clause} — N must be an integer literal.")),
8733 span: self.current_span(),
8734 })?;
8735 if !self.check_word("groups") {
8736 return Err(ParseError {
8737 kind: ParseErrorKind::Custom(format!("{clause} — expected `groups`.")),
8738 span: self.current_span(),
8739 });
8740 }
8741 self.advance();
8742 Some((kept, count))
8743 } else {
8744 None
8745 };
8746 let renormalize = !self.check_word("without");
8748 if !renormalize {
8749 self.advance();
8750 if !self.check_word("renormalizing") {
8751 return Err(ParseError {
8752 kind: ParseErrorKind::Custom(
8753 "`moe … choosing K of E without renormalizing` — expected `renormalizing` after `without`.".to_string(),
8754 ),
8755 span: self.current_span(),
8756 });
8757 }
8758 self.advance();
8759 }
8760 let scale = if self.check_word("scaled") {
8762 self.advance();
8763 if !self.check_word("by") {
8764 return Err(ParseError {
8765 kind: ParseErrorKind::Custom("`moe … scaled by S` — expected `by` after `scaled`.".to_string()),
8766 span: self.current_span(),
8767 });
8768 }
8769 self.advance();
8770 let v = match self.peek().kind {
8771 TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
8772 _ => None,
8773 };
8774 match v.filter(|v| v.is_finite() && *v > 0.0) {
8775 Some(v) => {
8776 self.advance();
8777 Some(v)
8778 }
8779 None => {
8780 return Err(ParseError {
8781 kind: ParseErrorKind::Custom("`moe … scaled by S` — S must be a positive number.".to_string()),
8782 span: self.current_span(),
8783 })
8784 }
8785 }
8786 } else {
8787 None
8788 };
8789 let weigh_inputs = self.check_word("weighting");
8791 if weigh_inputs {
8792 self.advance();
8793 for w in ["the", "inputs"] {
8794 if !self.check_word(w) {
8795 return Err(ParseError {
8796 kind: ParseErrorKind::Custom(format!(
8797 "`moe … weighting the inputs` — expected `{w}`."
8798 )),
8799 span: self.current_span(),
8800 });
8801 }
8802 self.advance();
8803 }
8804 }
8805 let act = self.take_moe_activation()?;
8806 if !(self.check(&TokenType::Period) || self.check(&TokenType::EOF)) {
8807 return Err(ParseError {
8808 kind: ParseErrorKind::Custom(format!(
8809 "`moe … choosing K of E` — `{}` is no clause here; the clauses come in this order: `by sigmoid`, \
8810 `biased by B`, `within the top G of N groups`, `without renormalizing`, `scaled by S`, \
8811 `weighting the inputs`, `using a swiglu clamped at L with slope A`.",
8812 self.interner.resolve(self.peek().lexeme)
8813 )),
8814 span: self.current_span(),
8815 });
8816 }
8817 if ne_lit != ne {
8818 return Err(ParseError {
8819 kind: ParseErrorKind::Custom(format!(
8820 "`moe … of {ne_lit}` disagrees with the router Wr's {ne} columns."
8821 )),
8822 span: self.current_span(),
8823 });
8824 }
8825 if ne == 0 || wgr % ne != 0 {
8826 return Err(ParseError {
8827 kind: ParseErrorKind::Custom(format!(
8828 "`moe` gate Wg rows ({wgr}) must be E·dff (a multiple of E={ne})."
8829 )),
8830 span: self.current_span(),
8831 });
8832 }
8833 let dff = wgr / ne;
8834 if !(1..=ne).contains(&k) {
8835 return Err(ParseError {
8836 kind: ParseErrorKind::Custom(format!(
8837 "`moe … choosing {k} of {ne}` — K must be in 1..=E."
8838 )),
8839 span: self.current_span(),
8840 });
8841 }
8842 if let Some((kept, count)) = groups {
8843 let why = if count < 1 || ne % count != 0 {
8844 Some(format!("N must divide the {ne} experts into groups of one size"))
8845 } else if !(1..=count).contains(&kept) {
8846 Some("G must be in 1..=N".to_string())
8847 } else if k > kept * (ne / count) {
8848 Some(format!("the top {kept} groups hold {} experts, fewer than the {k} chosen", kept * (ne / count)))
8849 } else {
8850 None
8851 };
8852 if let Some(why) = why {
8853 return Err(ParseError {
8854 kind: ParseErrorKind::Custom(format!("`moe … within the top {kept} of {count} groups` — {why}.")),
8855 span: self.current_span(),
8856 });
8857 }
8858 }
8859 self.user_bound.insert(var);
8860 self.bind_tensor(var, (1, d), false);
8861 return Ok(Some(self.desugar_tensor_moe(
8862 var,
8863 x,
8864 wr,
8865 wg,
8866 wu,
8867 wd,
8868 d,
8869 dff,
8870 ne,
8871 k,
8872 MoeOptions {
8873 router_bias,
8874 biases: [gate_bias, up_bias, down_bias],
8875 score,
8876 selection_bias,
8877 groups,
8878 renormalize,
8879 scale,
8880 weigh_inputs,
8881 act,
8882 },
8883 )));
8884 }
8885 if self.check_word("causal") && self.peek_word_at(1, "attention") {
8890 self.advance();
8891 self.advance();
8892 let want = |s: &mut Self, w: &str| -> ParseResult<()> {
8893 if !s.check_word(w) && !s.check_preposition_is(w) {
8894 return Err(ParseError {
8895 kind: ParseErrorKind::Custom(format!("`causal attention of Q over K and V with H heads KV groups from P` — expected `{w}`.")),
8896 span: s.current_span(),
8897 });
8898 }
8899 s.advance();
8900 Ok(())
8901 };
8902 let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
8903 let sym = s.peek().lexeme;
8904 let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
8905 return Err(ParseError {
8906 kind: ParseErrorKind::Custom(format!(
8907 "`causal attention` {role} must be a declared tensor."
8908 )),
8909 span: s.current_span(),
8910 });
8911 };
8912 s.advance();
8913 Ok((sym, r, c))
8914 };
8915 want(self, "of")?;
8916 let (q, qr, qc) = operand(self, "query block Q")?;
8917 want(self, "over")?;
8918 let (kc, kcr, kcc) = operand(self, "keys K")?;
8919 want(self, "and")?;
8920 let (vc, vr, vcc) = operand(self, "values V")?;
8921 want(self, "with")?;
8922 let h = self.take_int_literal().ok_or_else(|| ParseError {
8923 kind: ParseErrorKind::Custom(
8924 "`causal attention … with H heads` — H must be an integer literal.".to_string(),
8925 ),
8926 span: self.current_span(),
8927 })?;
8928 want(self, "heads")?;
8929 let kv = self.take_int_literal().ok_or_else(|| ParseError {
8930 kind: ParseErrorKind::Custom(
8931 "`causal attention … KV groups` — KV must be an integer literal.".to_string(),
8932 ),
8933 span: self.current_span(),
8934 })?;
8935 want(self, "groups")?;
8936 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
8937 return Err(ParseError { kind: ParseErrorKind::Custom("`causal attention … from P` — expected `from` and the first row's position.".to_string()), span: self.current_span() });
8938 }
8939 self.advance();
8940 let from = self.parse_row_index()?.ok_or_else(|| ParseError {
8941 kind: ParseErrorKind::Custom("`causal attention … from P` — P is a whole number, a name or a parenthesized expression.".to_string()),
8942 span: self.current_span(),
8943 })?;
8944 let window = self.take_window()?;
8945 let scale = self.take_attention_scale()?;
8946 let cap = self.take_cap("attention …")?;
8947 let sinks = self.take_sinks(h)?;
8948 if h <= 0 || qc % h != 0 {
8949 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal attention` query Q cols ({qc}) must be H·HD (a multiple of H={h}).")), span: self.current_span() });
8950 }
8951 let hd = qc / h;
8952 if kv <= 0 || h % kv != 0 || kcc != kv * hd || vr != kcr || vcc % kv != 0 || vcc == 0 {
8953 return Err(ParseError {
8954 kind: ParseErrorKind::Custom(format!("`causal attention` keys K must be L×(KV·HD) = L×{kv}·{hd} with KV dividing H={h}, and values V L×(KV·VD) — got {kcr}×{kcc} and {vr}×{vcc}.")),
8955 span: self.current_span(),
8956 });
8957 }
8958 let vd = vcc / kv;
8959 if qr < 1 {
8960 return Err(ParseError {
8961 kind: ParseErrorKind::Custom(
8962 "`causal attention` query block Q has no rows.".to_string(),
8963 ),
8964 span: self.current_span(),
8965 });
8966 }
8967 self.user_bound.insert(var);
8968 self.bind_tensor(var, (qr, h * vd), false);
8969 return Ok(Some(self.desugar_tensor_attention_block(
8970 var,
8971 q,
8972 kc,
8973 vc,
8974 AttentionShape { h, kv, hd, vd, l: kcr, rows: qr },
8975 Some(from),
8976 AttentionOptions { window, scale, cap, sinks },
8977 )));
8978 }
8979 if self.check_word("causal") {
8982 let saved = self.backtrack_point();
8983 self.advance();
8984 if !self.check_word("conv") {
8985 self.backtrack_to(&saved);
8986 return Ok(None);
8987 }
8988 self.advance();
8989 if !self.check_preposition_is("of") {
8990 self.backtrack_to(&saved);
8991 return Ok(None);
8992 }
8993 self.advance();
8994 let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
8995 if !sf.check_word(w) {
8996 return Err(ParseError {
8997 kind: ParseErrorKind::Custom(format!("`causal conv of X with weight WC state CS over T taps` — expected `{w}`.")),
8998 span: sf.current_span(),
8999 });
9000 }
9001 sf.advance();
9002 Ok(())
9003 };
9004 let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9005 let sym = sf.peek().lexeme;
9006 let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9007 return Err(ParseError {
9008 kind: ParseErrorKind::Custom(format!(
9009 "`causal conv` {role} must be a declared tensor."
9010 )),
9011 span: sf.current_span(),
9012 });
9013 };
9014 sf.advance();
9015 Ok((sym, r, c))
9016 };
9017 let (x, rows, chan) = operand(self, "input X")?;
9018 want(self, "with")?;
9019 want(self, "weight")?;
9020 let (wc, wcr, wcc) = operand(self, "weight WC")?;
9021 want(self, "state")?;
9022 let (cs, csr, csc) = operand(self, "state CS")?;
9023 want(self, "over")?;
9024 let taps = self.take_int_literal().ok_or_else(|| ParseError {
9025 kind: ParseErrorKind::Custom(
9026 "`causal conv … over T taps` — T must be an integer literal.".to_string(),
9027 ),
9028 span: self.current_span(),
9029 })?;
9030 want(self, "taps")?;
9031 if taps <= 0 {
9032 return Err(ParseError {
9033 kind: ParseErrorKind::Custom(format!(
9034 "`causal conv … over {taps} taps` — T must be positive."
9035 )),
9036 span: self.current_span(),
9037 });
9038 }
9039 if (wcr, wcc) != (taps, chan) {
9040 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal conv` weight WC must be {taps} by {chan} (taps by channels) — got {wcr} by {wcc}.")), span: self.current_span() });
9041 }
9042 if (csr, csc) != (taps, chan) {
9043 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal conv` state CS must be {taps} by {chan} (the ring of the last {taps} inputs) — got {csr} by {csc}.")), span: self.current_span() });
9044 }
9045 self.user_bound.insert(var);
9046 self.bind_tensor(var, (rows, chan), false);
9047 return Ok(Some(
9048 self.desugar_tensor_causal_conv(var, x, wc, cs, taps, chan, rows),
9049 ));
9050 }
9051 if self.check_word("full") {
9054 let saved = self.backtrack_point();
9055 self.advance();
9056 if !self.check_word("attention") {
9057 self.backtrack_to(&saved);
9058 return Ok(None);
9059 }
9060 self.advance();
9061 if !self.check_preposition_is("of") {
9062 self.backtrack_to(&saved);
9063 return Ok(None);
9064 }
9065 self.advance();
9066 let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9067 if !sf.check_word(w) {
9068 return Err(ParseError {
9069 kind: ParseErrorKind::Custom(format!(
9070 "`full attention of Q over K and V with H heads` — expected `{w}`."
9071 )),
9072 span: sf.current_span(),
9073 });
9074 }
9075 sf.advance();
9076 Ok(())
9077 };
9078 let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9079 let sym = sf.peek().lexeme;
9080 let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9081 return Err(ParseError {
9082 kind: ParseErrorKind::Custom(format!(
9083 "`full attention` {role} must be a declared tensor."
9084 )),
9085 span: sf.current_span(),
9086 });
9087 };
9088 sf.advance();
9089 Ok((sym, r, c))
9090 };
9091 let (q, qr, qc) = operand(self, "query Q")?;
9092 want(self, "over")?;
9093 let (kk, kr, kc) = operand(self, "keys K")?;
9094 want(self, "and")?;
9095 let (vv, vr, vc) = operand(self, "values V")?;
9096 want(self, "with")?;
9097 let h = self.take_int_literal().ok_or_else(|| ParseError {
9098 kind: ParseErrorKind::Custom(
9099 "`full attention … with H heads` — H must be an integer literal.".to_string(),
9100 ),
9101 span: self.current_span(),
9102 })?;
9103 want(self, "heads")?;
9104 if h <= 0 || qc % h != 0 {
9105 return Err(ParseError {
9106 kind: ParseErrorKind::Custom(format!(
9107 "`full attention` Q's {qc} columns must be H·HD (a multiple of H={h})."
9108 )),
9109 span: self.current_span(),
9110 });
9111 }
9112 if (kr, kc) != (qr, qc) || (vr, vc) != (qr, qc) {
9113 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`full attention` K and V must match Q's shape {qr} by {qc} — got {kr} by {kc} and {vr} by {vc}.")), span: self.current_span() });
9114 }
9115 self.user_bound.insert(var);
9116 self.bind_tensor(var, (qr, qc), false);
9117 return Ok(Some(self.desugar_tensor_full_attention(
9118 var,
9119 q,
9120 kk,
9121 vv,
9122 h,
9123 qc / h,
9124 qr,
9125 )));
9126 }
9127 if self.check_word("gated") {
9130 let saved = self.backtrack_point();
9131 self.advance();
9132 if !self.check_word("delta") {
9133 self.backtrack_to(&saved);
9134 return Ok(None);
9135 }
9136 self.advance();
9137 if !self.check_preposition_is("of") {
9138 self.backtrack_to(&saved);
9139 return Ok(None);
9140 }
9141 self.advance();
9142 let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9143 if !sf.check_word(w) {
9144 return Err(ParseError {
9145 kind: ParseErrorKind::Custom(format!(
9146 "`gated delta of Q over K and V with gate Z beta B decay A state S norm GN with HV heads HK groups` — expected `{w}`."
9147 )),
9148 span: sf.current_span(),
9149 });
9150 }
9151 sf.advance();
9152 Ok(())
9153 };
9154 let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9155 let sym = sf.peek().lexeme;
9156 let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9157 return Err(ParseError {
9158 kind: ParseErrorKind::Custom(format!(
9159 "`gated delta` {role} must be a declared tensor."
9160 )),
9161 span: sf.current_span(),
9162 });
9163 };
9164 sf.advance();
9165 Ok((sym, r, c))
9166 };
9167 let (q, qr, qc) = operand(self, "query Q")?;
9168 want(self, "over")?;
9169 let (kk, kr, kc) = operand(self, "keys K")?;
9170 want(self, "and")?;
9171 let (vv, vr, vc) = operand(self, "values V")?;
9172 want(self, "with")?;
9173 let sigmoid_gate = self.check_word("sigmoid");
9174 if sigmoid_gate {
9175 self.advance();
9176 }
9177 want(self, "gate")?;
9178 let (zz, zr, zc) = operand(self, "gate Z")?;
9179 want(self, "beta")?;
9180 let (bb, br, bc) = operand(self, "beta B")?;
9181 want(self, "decay")?;
9182 let (aa, ar, ac) = operand(self, "decay A")?;
9183 if [kr, vr, zr, br, ar].iter().any(|r| *r != qr) {
9185 return Err(ParseError {
9186 kind: ParseErrorKind::Custom(format!("`gated delta` Q, K, V, gate Z, beta B and decay A must have the same rows (one per token) — got {qr}, {kr}, {vr}, {zr}, {br} and {ar}.")),
9187 span: self.current_span(),
9188 });
9189 }
9190 want(self, "state")?;
9191 let (ss, sr, sc) = operand(self, "state S")?;
9192 want(self, "norm")?;
9193 let (gg, _gr, gc) = operand(self, "norm GN")?;
9194 want(self, "with")?;
9195 let hv = self.take_int_literal().ok_or_else(|| ParseError {
9196 kind: ParseErrorKind::Custom(
9197 "`gated delta … with HV heads` — HV must be an integer literal.".to_string(),
9198 ),
9199 span: self.current_span(),
9200 })?;
9201 want(self, "heads")?;
9202 let hk = self.take_int_literal().ok_or_else(|| ParseError {
9203 kind: ParseErrorKind::Custom(
9204 "`gated delta … HK groups` — HK must be an integer literal.".to_string(),
9205 ),
9206 span: self.current_span(),
9207 })?;
9208 want(self, "groups")?;
9209 if hk <= 0 || qc % hk != 0 {
9210 return Err(ParseError {
9211 kind: ParseErrorKind::Custom(format!(
9212 "`gated delta` Q's {qc} columns must be HK·D (a multiple of HK={hk})."
9213 )),
9214 span: self.current_span(),
9215 });
9216 }
9217 let d = qc / hk;
9218 if hv <= 0 || hv % hk != 0 {
9219 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta … {hv} heads {hk} groups` — HV must be a positive multiple of HK.")), span: self.current_span() });
9220 }
9221 if kc != qc {
9222 return Err(ParseError {
9223 kind: ParseErrorKind::Custom(format!(
9224 "`gated delta` keys K must match Q's {qc} columns — got {kc}."
9225 )),
9226 span: self.current_span(),
9227 });
9228 }
9229 if vc != hv * d || zc != hv * d {
9230 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` V and gate Z must be HV·D = {}·{d} = {} columns — got {vc} and {zc}.", hv, hv * d)), span: self.current_span() });
9231 }
9232 if bc != hv || ac != hv {
9233 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` beta B and decay A must be one value per head (HV={hv}) — got {bc} and {ac}.")), span: self.current_span() });
9234 }
9235 if (sr, sc) != (hv * d, d) {
9236 return Err(ParseError {
9237 kind: ParseErrorKind::Custom(format!(
9238 "`gated delta` state S must be HV·D by D = {} by {d} — got {sr} by {sc}.",
9239 hv * d
9240 )),
9241 span: self.current_span(),
9242 });
9243 }
9244 if gc != d {
9245 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` norm GN must have D={d} columns (shared across heads) — got {gc}.")), span: self.current_span() });
9246 }
9247 let eps = self.take_epsilon("gated delta")?;
9248 self.user_bound.insert(var);
9249 self.bind_tensor(var, (qr, hv * d), false);
9250 return Ok(Some(self.desugar_tensor_gated_delta(
9251 var, q, kk, vv, zz, bb, aa, ss, gg, hv, hk, d, qr, eps, sigmoid_gate,
9252 )));
9253 }
9254 if self.check_word("lut") && self.peek_word_at(1, "attention") {
9259 self.advance();
9260 self.advance();
9261 let form = "`lut attention of Q over K and V with H heads KV groups up to P with table E and scale M shifted right by S`";
9262 let refuse = |s: &Self, why: String| ParseError {
9263 kind: ParseErrorKind::Custom(format!("{form} — {why}")),
9264 span: s.current_span(),
9265 };
9266 let want = |s: &mut Self, w: &str| -> ParseResult<()> {
9267 if !s.check_word(w) {
9268 return Err(refuse(s, format!("expected `{w}`.")));
9269 }
9270 s.advance();
9271 Ok(())
9272 };
9273 let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9274 let sym = s.peek().lexeme;
9275 let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
9276 return Err(refuse(s, format!("{role} must be a declared tensor.")));
9277 };
9278 if !s.tensor_int.contains(&sym) {
9279 return Err(refuse(s, format!("{role} must be a tensor of Int.")));
9280 }
9281 s.advance();
9282 Ok((sym, r, c))
9283 };
9284 want(self, "of")?;
9285 let (q, qr, qc) = operand(self, "the query Q")?;
9286 want(self, "over")?;
9287 let (kc, kcr, kcc) = operand(self, "the keys K")?;
9288 want(self, "and")?;
9289 let (vc, vcr, vcc) = operand(self, "the values V")?;
9290 want(self, "with")?;
9291 let h = self
9292 .take_int_literal()
9293 .ok_or_else(|| refuse(self, "H must be an integer literal.".into()))?;
9294 want(self, "heads")?;
9295 let kv = self
9296 .take_int_literal()
9297 .ok_or_else(|| refuse(self, "KV must be an integer literal.".into()))?;
9298 want(self, "groups")?;
9299 let upto = if self.check_word("up") {
9300 self.advance();
9301 let is_to = self.check(&TokenType::To)
9302 || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
9303 if !is_to {
9304 return Err(refuse(self, "expected `to` after `up`.".into()));
9305 }
9306 self.advance();
9307 Some(self.parse_row_index()?.ok_or_else(|| {
9308 refuse(self, "P is a whole number, a name or a parenthesized expression.".into())
9309 })?)
9310 } else {
9311 None
9312 };
9313 want(self, "with")?;
9314 want(self, "table")?;
9315 let (table, tr, n) = operand(self, "the table E")?;
9316 want(self, "and")?;
9317 want(self, "scale")?;
9318 let m = self
9319 .take_signed_int_literal()
9320 .ok_or_else(|| refuse(self, "M must be an integer literal.".into()))?;
9321 want(self, "shifted")?;
9322 want(self, "right")?;
9323 want(self, "by")?;
9324 let s = self
9325 .take_int_literal()
9326 .filter(|s| (0..=63).contains(s))
9327 .ok_or_else(|| refuse(self, "S must be a whole number from 0 to 63.".into()))?;
9328 if qr != 1 {
9329 return Err(refuse(self, format!("Q is one query row, and it has {qr} rows.")));
9330 }
9331 if h <= 0 || qc % h != 0 {
9332 return Err(refuse(self, format!("Q's {qc} columns must be H·HD, a multiple of H = {h}.")));
9333 }
9334 let hd = qc / h;
9335 if kv <= 0 || h % kv != 0 || kcc != kv * hd {
9336 return Err(refuse(
9337 self,
9338 format!("K's {kcc} columns must be KV·HD = {kv}·{hd}, and H = {h} a multiple of KV."),
9339 ));
9340 }
9341 if (vcr, vcc) != (kcr, kcc) {
9342 return Err(refuse(self, format!("V is {vcr} by {vcc}, and K is {kcr} by {kcc}.")));
9343 }
9344 if tr != 1 || n < 1 {
9345 return Err(refuse(self, format!("the table E is one row of entries, and it is {tr} by {n}.")));
9346 }
9347 self.user_bound.insert(var);
9348 self.bind_tensor(var, (1, qc), true);
9349 return Ok(Some(self.desugar_tensor_lut_attention(
9350 var,
9351 q,
9352 kc,
9353 vc,
9354 table,
9355 (h, kv, hd, kcr, n),
9356 upto,
9357 (m, s),
9358 )));
9359 }
9360 if self.check_word("attention") {
9366 let saved = self.backtrack_point();
9367 self.advance();
9368 if !self.check_preposition_is("of") {
9369 self.backtrack_to(&saved);
9370 return Ok(None);
9371 }
9372 self.advance();
9373 let want = |s: &mut Self, w: &str| -> ParseResult<()> {
9374 if !s.check_word(w) {
9375 return Err(ParseError {
9376 kind: ParseErrorKind::Custom(format!("`attention of Q over K and V with H heads KV groups` — expected `{w}`.")),
9377 span: s.current_span(),
9378 });
9379 }
9380 s.advance();
9381 Ok(())
9382 };
9383 let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9384 let sym = s.peek().lexeme;
9385 let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
9386 return Err(ParseError {
9387 kind: ParseErrorKind::Custom(format!(
9388 "`attention` {role} must be a declared tensor."
9389 )),
9390 span: s.current_span(),
9391 });
9392 };
9393 s.advance();
9394 Ok((sym, r, c))
9395 };
9396 let (q, qr, qc) = operand(self, "query Q")?; if qr != 1 {
9398 return Err(ParseError {
9399 kind: ParseErrorKind::Custom(format!(
9400 "`attention of Q …` is one query row, and Q has {qr} rows — a block of query rows, each attending up to its own position, is `causal attention of Q over K and V with H heads KV groups from P`."
9401 )),
9402 span: self.current_span(),
9403 });
9404 }
9405 want(self, "over")?;
9406 let (kc, kcr, kcc) = operand(self, "keys K")?; want(self, "and")?;
9408 let (vc, vr, vcc) = operand(self, "values V")?;
9409 want(self, "with")?;
9410 let h = self.take_int_literal().ok_or_else(|| ParseError {
9411 kind: ParseErrorKind::Custom(
9412 "`attention … with H heads` — H must be an integer literal.".to_string(),
9413 ),
9414 span: self.current_span(),
9415 })?;
9416 want(self, "heads")?;
9417 let kv = self.take_int_literal().ok_or_else(|| ParseError {
9418 kind: ParseErrorKind::Custom(
9419 "`attention … KV groups` — KV must be an integer literal.".to_string(),
9420 ),
9421 span: self.current_span(),
9422 })?;
9423 want(self, "groups")?;
9424 let upto = if self.check_word("up") {
9425 self.advance();
9426 let is_to = self.check(&TokenType::To)
9427 || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
9428 if !is_to {
9429 return Err(ParseError {
9430 kind: ParseErrorKind::Custom(
9431 "`attention … up to P` — expected `to` after `up`.".to_string(),
9432 ),
9433 span: self.current_span(),
9434 });
9435 }
9436 self.advance();
9437 Some(self.parse_row_index()?.ok_or_else(|| ParseError {
9438 kind: ParseErrorKind::Custom("`attention … up to P` — P is a whole number, a name or a parenthesized expression.".to_string()),
9439 span: self.current_span(),
9440 })?)
9441 } else {
9442 None
9443 };
9444 let window = self.take_window()?;
9445 let scale = self.take_attention_scale()?;
9446 let cap = self.take_cap("attention …")?;
9447 let sinks = self.take_sinks(h)?;
9448 if h <= 0 || qc % h != 0 {
9449 return Err(ParseError {
9450 kind: ParseErrorKind::Custom(format!(
9451 "`attention` query Q cols ({qc}) must be H·HD (a multiple of H={h})."
9452 )),
9453 span: self.current_span(),
9454 });
9455 }
9456 let hd = qc / h;
9457 if kv <= 0 || kcc != kv * hd {
9458 return Err(ParseError {
9459 kind: ParseErrorKind::Custom(format!(
9460 "`attention` keys K cols ({kcc}) must be KV·HD = {kv}·{hd}."
9461 )),
9462 span: self.current_span(),
9463 });
9464 }
9465 if vr != kcr || vcc == 0 || vcc % kv != 0 {
9466 return Err(ParseError {
9467 kind: ParseErrorKind::Custom(format!(
9468 "`attention` values V must be L×(KV·VD) — K's {kcr} rows, and columns a multiple of KV={kv} — got {vr}×{vcc}."
9469 )),
9470 span: self.current_span(),
9471 });
9472 }
9473 let vd = vcc / kv;
9474 let l = kcr; self.user_bound.insert(var);
9476 self.bind_tensor(var, (1, h * vd), false);
9477 return Ok(Some(self.desugar_tensor_attention(
9478 var,
9479 q,
9480 kc,
9481 vc,
9482 AttentionShape { h, kv, hd, vd, l, rows: 1 },
9483 upto,
9484 AttentionOptions { window, scale, cap, sinks },
9485 )));
9486 }
9487 if let Some(stmt) = self.try_parse_tensor_row(var)? {
9490 return Ok(Some(stmt));
9491 }
9492 if let Some(stmt) = self.try_parse_tensor_rows(var)? {
9494 return Ok(Some(stmt));
9495 }
9496 if self.check_word("argmax") && self.peek_word_at(1, "of") {
9498 let saved = self.backtrack_point();
9499 self.advance();
9500 self.advance();
9501 let x = self.peek().lexeme;
9502 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9503 self.backtrack_to(&saved);
9504 return Ok(None);
9505 };
9506 self.advance();
9507 self.user_bound.insert(var);
9508 return Ok(Some(self.desugar_tensor_argmax(var, x, r * c)));
9509 }
9510 if self.check_word("transpose") {
9513 let saved = self.backtrack_point();
9514 self.advance();
9515 if !self.check_preposition_is("of") {
9516 self.backtrack_to(&saved);
9517 return Ok(None);
9518 }
9519 self.advance();
9520 let x = self.peek().lexeme;
9521 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9522 return Err(ParseError {
9523 kind: ParseErrorKind::Custom(
9524 "`transpose of X` needs a declared tensor X.".to_string(),
9525 ),
9526 span: self.current_span(),
9527 });
9528 };
9529 self.advance();
9530 self.user_bound.insert(var);
9531 let int = self.tensor_int.contains(&x);
9533 self.bind_tensor(var, (c, r), int);
9534 return Ok(Some(self.desugar_tensor_transpose(var, x, r, c, int)));
9535 }
9536 if self.check_word("rope") {
9542 let saved = self.backtrack_point();
9543 self.advance();
9544 if !self.check_preposition_is("of") {
9545 self.backtrack_to(&saved);
9546 return Ok(None);
9547 }
9548 self.advance();
9549 let q = self.peek().lexeme;
9550 let Some(&(qr, qc)) = self.tensor_shapes.get(&q) else {
9551 return Err(ParseError {
9552 kind: ParseErrorKind::Custom(
9553 "`rope of Q` needs a declared tensor Q.".to_string(),
9554 ),
9555 span: self.current_span(),
9556 });
9557 };
9558 self.advance();
9559 if !self.check_word("with") {
9560 return Err(ParseError {
9561 kind: ParseErrorKind::Custom(
9562 "`rope of Q with cos RC and sin RS`: expected `with`.".to_string(),
9563 ),
9564 span: self.current_span(),
9565 });
9566 }
9567 self.advance();
9568 if !self.check_word("cos") {
9569 return Err(ParseError {
9570 kind: ParseErrorKind::Custom(
9571 "`rope … with cos RC …`: expected `cos`.".to_string(),
9572 ),
9573 span: self.current_span(),
9574 });
9575 }
9576 self.advance();
9577 let rc = self.peek().lexeme;
9578 let Some(&(rcr, half)) = self.tensor_shapes.get(&rc) else {
9579 return Err(ParseError {
9580 kind: ParseErrorKind::Custom(
9581 "`rope … with cos RC`: RC must be a declared `R by half` tensor."
9582 .to_string(),
9583 ),
9584 span: self.current_span(),
9585 });
9586 };
9587 self.advance();
9588 if !self.check_word("and") {
9589 return Err(ParseError {
9590 kind: ParseErrorKind::Custom(
9591 "`rope … cos RC and sin RS`: expected `and`.".to_string(),
9592 ),
9593 span: self.current_span(),
9594 });
9595 }
9596 self.advance();
9597 if !self.check_word("sin") {
9598 return Err(ParseError {
9599 kind: ParseErrorKind::Custom(
9600 "`rope … and sin RS`: expected `sin`.".to_string(),
9601 ),
9602 span: self.current_span(),
9603 });
9604 }
9605 self.advance();
9606 let rs = self.peek().lexeme;
9607 let Some(&rs_shape) = self.tensor_shapes.get(&rs) else {
9608 return Err(ParseError {
9609 kind: ParseErrorKind::Custom(
9610 "`rope … and sin RS`: RS must be a declared tensor of RC's shape."
9611 .to_string(),
9612 ),
9613 span: self.current_span(),
9614 });
9615 };
9616 let (q_name, rc_name, rs_name) = (
9617 self.interner.resolve(q).to_string(),
9618 self.interner.resolve(rc).to_string(),
9619 self.interner.resolve(rs).to_string(),
9620 );
9621 if rs_shape != (rcr, half) {
9622 return Err(ParseError {
9623 kind: ParseErrorKind::Custom(format!("`rope … cos {rc_name} and sin {rs_name}` — {rs_name} must have {rc_name}'s shape ({rcr} by {half}), not {} by {}.", rs_shape.0, rs_shape.1)),
9624 span: self.current_span(),
9625 });
9626 }
9627 self.advance();
9628 let from = if self.check_word("from") && self.peek_word_at(1, "position") {
9632 self.advance();
9633 self.advance();
9634 let Some(p) = self.parse_row_index()? else {
9635 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rope of {q_name} … from position P` — P must be a whole number, a name or a parenthesized expression.")), span: self.current_span() });
9636 };
9637 if rcr < qr {
9638 return Err(ParseError {
9639 kind: ParseErrorKind::Custom(format!("`rope of {q_name} … from position` — a cache of {rcr} rows cannot hold the {qr} rows of {q_name}; {rc_name} has {rcr} rows.")),
9640 span: self.current_span(),
9641 });
9642 }
9643 Some((p, rcr as i64))
9644 } else {
9645 if rcr != 1 && rcr != qr {
9646 return Err(ParseError {
9647 kind: ParseErrorKind::Custom(format!("`rope of {q_name}` — the tables hold one row of angles per row of {q_name} ({qr} rows) or one row for every row; {rc_name} has {rcr} rows.")),
9648 span: self.current_span(),
9649 });
9650 }
9651 None
9652 };
9653 let hd = if self.check_word("across") {
9658 let saved_a = self.backtrack_point();
9659 self.advance();
9660 let Some(hcount) = self.take_int_literal() else {
9661 self.backtrack_to(&saved_a);
9662 return Err(ParseError {
9663 kind: ParseErrorKind::Custom(
9664 "`rope … across H heads` — H must be an integer literal.".to_string(),
9665 ),
9666 span: self.current_span(),
9667 });
9668 };
9669 if !self.check_word("heads") {
9670 return Err(ParseError {
9671 kind: ParseErrorKind::Custom(
9672 "`rope … across H heads`: expected `heads`.".to_string(),
9673 ),
9674 span: self.current_span(),
9675 });
9676 }
9677 self.advance();
9678 if hcount <= 0 || qc % hcount != 0 {
9679 return Err(ParseError {
9680 kind: ParseErrorKind::Custom(format!(
9681 "`rope … across {hcount} heads` — Q's {qc} columns must divide by H."
9682 )),
9683 span: self.current_span(),
9684 });
9685 }
9686 let hd = qc / hcount;
9687 if 2 * half > hd {
9688 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rope … across {hcount} heads` — the rotary slice is {} dims but head_dim is only {hd}.", 2 * half)), span: self.current_span() });
9689 }
9690 hd
9691 } else {
9692 if half <= 0 || qc % (2 * half) != 0 {
9693 return Err(ParseError {
9694 kind: ParseErrorKind::Custom(format!("`rope of {q_name}` — without `across H heads` a head is the tables' 2·{half} dims, and {q_name}'s {qc} columns are not whole heads.")),
9695 span: self.current_span(),
9696 });
9697 }
9698 2 * half
9699 };
9700 self.user_bound.insert(var);
9701 self.bind_tensor(var, (qr, qc), false);
9702 let per_row = (rcr > 1 || from.is_some()).then_some(qc / hd);
9703 return Ok(Some(self.desugar_tensor_rope(
9704 var,
9705 q,
9706 rc,
9707 rs,
9708 qr * qc,
9709 half,
9710 hd,
9711 per_row,
9712 from,
9713 )));
9714 }
9715 if self.check_word("relu") && self.peek_word_at(1, "of") {
9717 self.advance();
9718 self.advance();
9719 let x = self.peek().lexeme;
9720 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9721 return Err(ParseError {
9722 kind: ParseErrorKind::Custom(
9723 "`relu of X` needs a declared tensor X.".to_string(),
9724 ),
9725 span: self.current_span(),
9726 });
9727 };
9728 self.advance();
9729 let int = self.tensor_int.contains(&x);
9730 self.user_bound.insert(var);
9731 self.bind_tensor(var, (r, c), int);
9732 return Ok(Some(self.desugar_tensor_map(
9733 var,
9734 x,
9735 r * c,
9736 TensorElemMap::Relu,
9737 int,
9738 )));
9739 }
9740 if self.check_word("silu") {
9742 let saved = self.backtrack_point();
9743 self.advance();
9744 if !self.check_preposition_is("of") {
9745 self.backtrack_to(&saved);
9746 return Ok(None);
9747 }
9748 self.advance();
9749 let x = self.peek().lexeme;
9750 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9751 return Err(ParseError {
9752 kind: ParseErrorKind::Custom(
9753 "`silu of X` needs a declared tensor X.".to_string(),
9754 ),
9755 span: self.current_span(),
9756 });
9757 };
9758 self.advance();
9759 self.user_bound.insert(var);
9760 self.bind_tensor(var, (r, c), false);
9761 return Ok(Some(self.desugar_tensor_silu(var, x, r * c)));
9762 }
9763 if self.check_word("sigmoid") {
9765 let saved = self.backtrack_point();
9766 self.advance();
9767 if !self.check_preposition_is("of") {
9768 self.backtrack_to(&saved);
9769 return Ok(None);
9770 }
9771 self.advance();
9772 let x = self.peek().lexeme;
9773 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9774 return Err(ParseError {
9775 kind: ParseErrorKind::Custom(
9776 "`sigmoid of X` needs a declared tensor X.".to_string(),
9777 ),
9778 span: self.current_span(),
9779 });
9780 };
9781 self.advance();
9782 self.user_bound.insert(var);
9783 self.bind_tensor(var, (r, c), false);
9784 return Ok(Some(self.desugar_tensor_sigmoid(var, x, r * c)));
9785 }
9786 if self.check_word("gelu") {
9788 let saved = self.backtrack_point();
9789 self.advance();
9790 if !self.check_preposition_is("of") {
9791 self.backtrack_to(&saved);
9792 return Ok(None);
9793 }
9794 self.advance();
9795 let x = self.peek().lexeme;
9796 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9797 return Err(ParseError {
9798 kind: ParseErrorKind::Custom(
9799 "`gelu of X` needs a declared tensor X.".to_string(),
9800 ),
9801 span: self.current_span(),
9802 });
9803 };
9804 self.advance();
9805 self.user_bound.insert(var);
9806 self.bind_tensor(var, (r, c), false);
9807 return Ok(Some(self.desugar_tensor_gelu(var, x, r * c)));
9808 }
9809 if self.check_word("decay") {
9811 let saved = self.backtrack_point();
9812 self.advance();
9813 if !self.check_preposition_is("of") {
9814 self.backtrack_to(&saved);
9815 return Ok(None);
9816 }
9817 self.advance();
9818 let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9819 if !sf.check_word(w) {
9820 return Err(ParseError {
9821 kind: ParseErrorKind::Custom(format!(
9822 "`decay of Ar with bias Dt rate Alog` — expected `{w}`."
9823 )),
9824 span: sf.current_span(),
9825 });
9826 }
9827 sf.advance();
9828 Ok(())
9829 };
9830 let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64)> {
9831 let sym = sf.peek().lexeme;
9832 let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9833 return Err(ParseError {
9834 kind: ParseErrorKind::Custom(format!(
9835 "`decay` {role} must be a declared tensor."
9836 )),
9837 span: sf.current_span(),
9838 });
9839 };
9840 sf.advance();
9841 Ok((sym, r * c))
9842 };
9843 let ar_shape = self.tensor_shapes.get(&self.peek().lexeme).copied();
9844 let (ar, n) = operand(self, "input Ar")?;
9845 want(self, "with")?;
9846 want(self, "bias")?;
9847 let (dt, dn) = operand(self, "bias Dt")?;
9848 want(self, "rate")?;
9849 let (alog, an) = operand(self, "rate Alog")?;
9850 let heads = match ar_shape {
9852 Some((rows, cols)) if rows > 1 && dn == cols && an == cols => cols,
9853 _ => n,
9854 };
9855 if dn != heads || an != heads {
9856 return Err(ParseError {
9857 kind: ParseErrorKind::Custom(format!("`decay` bias and rate must be one value per head ({heads}) — got {dn} and {an}.")),
9858 span: self.current_span(),
9859 });
9860 }
9861 self.user_bound.insert(var);
9862 self.bind_tensor(var, ar_shape.filter(|_| heads < n).unwrap_or((1, n)), false);
9863 return Ok(Some(self.desugar_tensor_decay(var, ar, dt, alog, n, heads)));
9864 }
9865 if self.check_word("l2norm") {
9867 let saved = self.backtrack_point();
9868 self.advance();
9869 if !self.check_preposition_is("of") {
9870 self.backtrack_to(&saved);
9871 return Ok(None);
9872 }
9873 self.advance();
9874 let x = self.peek().lexeme;
9875 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9876 return Err(ParseError {
9877 kind: ParseErrorKind::Custom(
9878 "`l2norm of X` needs a declared tensor X.".to_string(),
9879 ),
9880 span: self.current_span(),
9881 });
9882 };
9883 self.advance();
9884 self.user_bound.insert(var);
9885 self.bind_tensor(var, (r, c), false);
9886 let (nr, nc) = if self.check_word("across") {
9889 self.advance();
9890 let Some(h) = self.take_int_literal() else {
9891 return Err(ParseError {
9892 kind: ParseErrorKind::Custom(
9893 "`l2norm … across H heads` — H must be an integer literal.".to_string(),
9894 ),
9895 span: self.current_span(),
9896 });
9897 };
9898 if !self.check_word("heads") {
9899 return Err(ParseError {
9900 kind: ParseErrorKind::Custom(
9901 "`l2norm … across H heads`: expected `heads`.".to_string(),
9902 ),
9903 span: self.current_span(),
9904 });
9905 }
9906 self.advance();
9907 if h <= 0 || (r * c) % h != 0 {
9908 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`l2norm … across {h} heads` — the tensor's {} entries must divide by H.", r * c)), span: self.current_span() });
9909 }
9910 (h, (r * c) / h)
9911 } else {
9912 (r, c)
9913 };
9914 let eps = self.take_epsilon("l2norm")?;
9915 self.bind_tensor(var, (r, c), false);
9916 return Ok(Some(self.desugar_tensor_l2norm(var, x, nr, nc, eps)));
9917 }
9918 if self.check_word("swiglu") {
9920 let saved = self.backtrack_point();
9921 self.advance();
9922 if !self.check_preposition_is("of") {
9923 self.backtrack_to(&saved);
9924 return Ok(None);
9925 }
9926 self.advance();
9927 let gate = self.peek().lexeme;
9928 let Some(&(gr, gc)) = self.tensor_shapes.get(&gate) else {
9929 return Err(ParseError {
9930 kind: ParseErrorKind::Custom(
9931 "`swiglu of Gate and Up` needs a declared tensor Gate.".to_string(),
9932 ),
9933 span: self.current_span(),
9934 });
9935 };
9936 self.advance(); if !self.check_word("and") {
9938 return Err(ParseError {
9939 kind: ParseErrorKind::Custom(
9940 "expected `and Up` in `swiglu of Gate and Up`.".to_string(),
9941 ),
9942 span: self.current_span(),
9943 });
9944 }
9945 self.advance(); let up = self.peek().lexeme;
9947 let Some(&(ur, uc)) = self.tensor_shapes.get(&up) else {
9948 return Err(ParseError {
9949 kind: ParseErrorKind::Custom(
9950 "`swiglu of Gate and Up` needs a declared tensor Up.".to_string(),
9951 ),
9952 span: self.current_span(),
9953 });
9954 };
9955 if (ur, uc) != (gr, gc) {
9956 return Err(ParseError {
9957 kind: ParseErrorKind::Custom(format!(
9958 "swiglu Up must match Gate's shape {gr} by {gc} — got {ur} by {uc}."
9959 )),
9960 span: self.current_span(),
9961 });
9962 }
9963 self.advance(); self.user_bound.insert(var);
9965 self.bind_tensor(var, (gr, gc), false);
9966 return Ok(Some(self.desugar_tensor_swiglu(var, gate, up, gr * gc)));
9967 }
9968 let kind = if self.check_word("softmax") {
9969 ReductionKind::Softmax
9970 } else if self.check_word("rmsnorm") {
9971 ReductionKind::Rmsnorm
9972 } else if self.check_word("layernorm") {
9973 ReductionKind::Layernorm
9974 } else {
9975 return Ok(None);
9976 };
9977 let saved = self.backtrack_point();
9978 self.advance(); if !self.check_preposition_is("of") {
9980 self.backtrack_to(&saved);
9981 return Ok(None);
9982 }
9983 self.advance(); let x = self.peek().lexeme;
9985 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9986 return Err(ParseError {
9987 kind: ParseErrorKind::Custom(format!(
9988 "`{}` needs a declared tensor — `Let Y be {} of X` where X is a tensor.",
9989 kind.word(),
9990 kind.word()
9991 )),
9992 span: self.current_span(),
9993 });
9994 };
9995 self.advance(); if matches!(kind, ReductionKind::Layernorm) {
9998 let want = |s: &mut Self, word: &str| -> ParseResult<()> {
9999 if !s.check_word(word) {
10000 return Err(ParseError {
10001 kind: ParseErrorKind::Custom(format!("layernorm needs `layernorm of X with weight W and bias B` — expected `{word}`.")),
10002 span: s.current_span(),
10003 });
10004 }
10005 s.advance();
10006 Ok(())
10007 };
10008 let vec_operand = |s: &mut Self, role: &str| -> ParseResult<Symbol> {
10009 let sym = s.peek().lexeme;
10010 let Some(&(vr, vc)) = s.tensor_shapes.get(&sym) else {
10011 return Err(ParseError {
10012 kind: ParseErrorKind::Custom(format!(
10013 "layernorm {role} must be a declared length-{c} vector."
10014 )),
10015 span: s.current_span(),
10016 });
10017 };
10018 if vr * vc != c {
10019 return Err(ParseError {
10020 kind: ParseErrorKind::Custom(format!(
10021 "layernorm {role} length must equal the column count {c} — got {}.",
10022 vr * vc
10023 )),
10024 span: s.current_span(),
10025 });
10026 }
10027 s.advance();
10028 Ok(sym)
10029 };
10030 want(self, "with")?;
10031 want(self, "weight")?;
10032 let w = vec_operand(self, "weight")?;
10033 want(self, "and")?;
10034 want(self, "bias")?;
10035 let b = vec_operand(self, "bias")?;
10036 let eps = self.take_epsilon("layernorm")?;
10037 self.user_bound.insert(var);
10038 self.bind_tensor(var, (r, c), false);
10039 return Ok(Some(self.desugar_tensor_layernorm(var, x, w, b, r, c, eps)));
10040 }
10041 if matches!(kind, ReductionKind::Rmsnorm)
10044 && (self.check_word("plus")
10045 || (self.check_word("with") && !self.check_word_at(self.current + 1, "epsilon")))
10046 {
10047 let residual = if self.check_word("plus") {
10049 self.advance(); let rz = self.peek().lexeme;
10051 let Some(&(rr, rc)) = self.tensor_shapes.get(&rz) else {
10052 return Err(ParseError {
10053 kind: ParseErrorKind::Custom("`rmsnorm of X plus R` needs a declared residual tensor R with X's shape.".to_string()),
10054 span: self.current_span(),
10055 });
10056 };
10057 if (rr, rc) != (r, c) {
10058 return Err(ParseError {
10059 kind: ParseErrorKind::Custom(format!("fused-add residual R must match X's shape {r} by {c} — got {rr} by {rc}.")),
10060 span: self.current_span(),
10061 });
10062 }
10063 self.advance(); Some(rz)
10065 } else {
10066 None
10067 };
10068 if !self.check_word("with") {
10070 return Err(ParseError {
10071 kind: ParseErrorKind::Custom(
10072 "rmsnorm here needs a weight: `rmsnorm of X [plus R] with weight W`."
10073 .to_string(),
10074 ),
10075 span: self.current_span(),
10076 });
10077 }
10078 self.advance(); if !self.check_word("weight") {
10080 return Err(ParseError {
10081 kind: ParseErrorKind::Custom(
10082 "expected `weight` after `with` in `rmsnorm of X with weight W`."
10083 .to_string(),
10084 ),
10085 span: self.current_span(),
10086 });
10087 }
10088 self.advance(); let w = self.peek().lexeme;
10090 let Some(&(wr, wc)) = self.tensor_shapes.get(&w) else {
10091 return Err(ParseError {
10092 kind: ParseErrorKind::Custom("`rmsnorm ... with weight W` needs a declared tensor W (a length-C weight vector).".to_string()),
10093 span: self.current_span(),
10094 });
10095 };
10096 self.advance(); let mut weight_rows = 1;
10103 let (nr, nc) = if self.check_word("in") && self.peek_word_at(2, "lanes") {
10104 self.advance();
10105 let Some(l) = self.take_int_literal() else {
10106 return Err(ParseError {
10107 kind: ParseErrorKind::Custom("`rmsnorm … in L lanes` — L must be an integer literal.".to_string()),
10108 span: self.current_span(),
10109 });
10110 };
10111 self.advance(); if l <= 0 || c % l != 0 {
10113 return Err(ParseError {
10114 kind: ParseErrorKind::Custom(format!("`rmsnorm … in {l} lanes` — the tensor's {c} columns must divide by L.")),
10115 span: self.current_span(),
10116 });
10117 }
10118 if wr * wc != c {
10119 return Err(ParseError {
10120 kind: ParseErrorKind::Custom(format!("`rmsnorm … in {l} lanes` — the weight holds every lane's: {c} values, got {}.", wr * wc)),
10121 span: self.current_span(),
10122 });
10123 }
10124 weight_rows = l;
10125 (r * l, c / l)
10126 } else if self.check_word("across") {
10127 let saved_h = self.backtrack_point();
10128 self.advance();
10129 let Some(h) = self.take_int_literal() else {
10130 self.backtrack_to(&saved_h);
10131 return Err(ParseError {
10132 kind: ParseErrorKind::Custom(
10133 "`rmsnorm … across H heads` — H must be an integer literal."
10134 .to_string(),
10135 ),
10136 span: self.current_span(),
10137 });
10138 };
10139 if !self.check_word("heads") {
10140 return Err(ParseError {
10141 kind: ParseErrorKind::Custom(
10142 "`rmsnorm … across H heads`: expected `heads`.".to_string(),
10143 ),
10144 span: self.current_span(),
10145 });
10146 }
10147 self.advance();
10148 if h <= 0 || (r * c) % h != 0 {
10149 return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rmsnorm … across {h} heads` — the tensor's {} entries must divide by H.", r * c)), span: self.current_span() });
10150 }
10151 (h, (r * c) / h)
10152 } else {
10153 (r, c)
10154 };
10155 if weight_rows == 1 && wr * wc != nc {
10156 return Err(ParseError {
10157 kind: ParseErrorKind::Custom(format!(
10158 "rmsnorm weight length must be {nc} (the per-head width) — got {}.",
10159 wr * wc
10160 )),
10161 span: self.current_span(),
10162 });
10163 }
10164 let eps = self.take_epsilon("rmsnorm")?;
10165 self.user_bound.insert(var);
10166 self.bind_tensor(var, (r, c), false);
10167 return Ok(Some(
10168 self.desugar_tensor_rmsnorm_weighted(var, x, residual, w, nr, nc, eps, weight_rows),
10169 ));
10170 }
10171 let total = r * c;
10172 let eps = if matches!(kind, ReductionKind::Rmsnorm) { self.take_epsilon("rmsnorm")? } else { 1e-6 };
10173 self.user_bound.insert(var);
10174 self.bind_tensor(var, (r, c), false);
10175 Ok(Some(match kind {
10176 ReductionKind::Rmsnorm => self.desugar_tensor_rmsnorm(var, x, total, eps),
10177 ReductionKind::Softmax if r > 1 => self.desugar_tensor_softmax_rows(var, x, r, c),
10179 ReductionKind::Softmax => self.desugar_tensor_softmax(var, x, total),
10180 ReductionKind::Layernorm => {
10181 unreachable!("layernorm is handled above (it requires weight + bias)")
10182 }
10183 }))
10184 }
10185
10186 fn try_parse_tensor_new(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10189 if !self.check_article() {
10190 return Ok(None);
10191 }
10192 let saved = self.backtrack_point();
10193 self.advance(); let Some(rows) = self.take_int_literal() else {
10195 self.backtrack_to(&saved);
10196 return Ok(None);
10197 };
10198 if !(self.check_word("by") || self.check_preposition_is("by")) {
10199 self.backtrack_to(&saved);
10200 return Ok(None);
10201 }
10202 self.advance(); let Some(cols) = self.take_int_literal() else {
10204 self.backtrack_to(&saved);
10205 return Ok(None);
10206 };
10207 if !self.check_word("tensor") {
10208 self.backtrack_to(&saved);
10209 return Ok(None);
10210 }
10211 self.advance(); if !self.check_preposition_is("of") {
10213 self.backtrack_to(&saved);
10214 return Ok(None);
10215 }
10216 self.advance(); let elem = self.interner.resolve(self.peek().lexeme).to_string();
10218 self.advance(); if rows <= 0 || cols <= 0 {
10220 return Err(ParseError {
10221 kind: ParseErrorKind::Custom(format!(
10222 "A tensor's dimensions must be positive — got {rows} by {cols}."
10223 )),
10224 span: self.current_span(),
10225 });
10226 }
10227 let is_float = matches!(elem.as_str(), "Real" | "Float");
10228 self.user_bound.insert(var);
10229 self.bind_tensor(var, (rows, cols), !is_float);
10230 Ok(Some(self.desugar_tensor_new(var, rows, cols, is_float)))
10231 }
10232
10233 fn try_parse_tensor_head_slice(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10238 let last = self.peek_word_at(1, "last");
10239 if !(self.check_article()
10240 && (self.peek_word_at(1, "first") || last)
10241 && matches!(self.tokens.get(self.current + 2).map(|t| &t.kind), Some(TokenType::Number(_)))
10242 && self.peek_word_at(3, "of")
10243 && self.peek_word_at(4, "every"))
10244 {
10245 return Ok(None);
10246 }
10247 let which = if last { "last" } else { "first" };
10248 self.advance();
10249 self.advance();
10250 let k = self.take_int_literal().unwrap_or(0);
10251 self.advance();
10252 self.advance();
10253 let Some(n) = self.take_int_literal() else {
10254 return Err(ParseError {
10255 kind: ParseErrorKind::Custom(format!("`the {which} K of every N of X` — N must be a whole number.")),
10256 span: self.current_span(),
10257 });
10258 };
10259 if !self.check_preposition_is("of") {
10260 return Err(ParseError {
10261 kind: ParseErrorKind::Custom(format!("`the {which} {k} of every {n} of X` — expected `of X`.")),
10262 span: self.current_span(),
10263 });
10264 }
10265 self.advance();
10266 let x = self.peek().lexeme;
10267 let Some(&(rows, cols)) = self.tensor_shapes.get(&x) else {
10268 return Err(ParseError {
10269 kind: ParseErrorKind::Custom(format!("`the {which} {k} of every {n} of X` — X must be a declared tensor.")),
10270 span: self.current_span(),
10271 });
10272 };
10273 self.advance();
10274 if k < 1 || n < 1 || k > n {
10275 return Err(ParseError {
10276 kind: ParseErrorKind::Custom(format!(
10277 "`the {which} {k} of every {n} of {}` — a part of {k} must fit in a group of {n}.",
10278 self.interner.resolve(x)
10279 )),
10280 span: self.current_span(),
10281 });
10282 }
10283 if cols % n != 0 {
10284 return Err(ParseError {
10285 kind: ParseErrorKind::Custom(format!(
10286 "`the {which} {k} of every {n} of {}` — its {cols} columns are not whole groups of every {n}.",
10287 self.interner.resolve(x)
10288 )),
10289 span: self.current_span(),
10290 });
10291 }
10292 let int = self.tensor_int.contains(&x);
10293 let from = if last { n - k } else { 0 };
10294 self.user_bound.insert(var);
10295 self.bind_tensor(var, (rows, cols / n * k), int);
10296 Ok(Some(self.desugar_tensor_head_slice(var, x, rows * cols / n, n, from, k, int)))
10297 }
10298
10299 fn take_signed_int_literal(&mut self) -> Option<i64> {
10301 if let Some(n) = self.take_int_literal() {
10302 return Some(n);
10303 }
10304 if self.check(&TokenType::Minus) {
10305 let saved = self.backtrack_point();
10306 self.advance();
10307 if let Some(n) = self.take_int_literal() {
10308 return Some(-n);
10309 }
10310 self.backtrack_to(&saved);
10311 }
10312 None
10313 }
10314
10315 fn try_parse_tensor_int_map(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10320 let x = self.peek().lexeme;
10321 let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
10322 return Ok(None);
10323 };
10324 let clamp = if self.peek_word_at(1, "times") {
10325 false
10326 } else if self.peek_word_at(1, "clamped") {
10327 true
10328 } else {
10329 return Ok(None);
10330 };
10331 let name = self.interner.resolve(x).to_string();
10332 let form = if clamp {
10333 "clamped between LO and HI"
10334 } else {
10335 "times M shifted right by S"
10336 };
10337 let refuse = |s: &Self, why: String| ParseError {
10338 kind: ParseErrorKind::Custom(why),
10339 span: s.current_span(),
10340 };
10341 if !self.tensor_int.contains(&x) {
10342 return Err(refuse(
10343 self,
10344 format!("`{name} {form}` is an integer form: `{name}` must be a tensor of Int."),
10345 ));
10346 }
10347 self.advance(); self.advance(); let want = |s: &mut Self, w: &str| -> ParseResult<()> {
10350 if !s.check_word(w) {
10351 return Err(refuse(s, format!("`{name} {form}` — expected `{w}`.")));
10352 }
10353 s.advance();
10354 Ok(())
10355 };
10356 let map = if clamp {
10357 want(self, "between")?;
10358 let lo = self.take_signed_int_literal().ok_or_else(|| {
10359 refuse(self, format!("`{name} {form}` — LO must be an integer literal."))
10360 })?;
10361 want(self, "and")?;
10362 let hi = self.take_signed_int_literal().ok_or_else(|| {
10363 refuse(self, format!("`{name} {form}` — HI must be an integer literal."))
10364 })?;
10365 if lo > hi {
10366 return Err(refuse(
10367 self,
10368 format!(
10369 "`{name} clamped between {lo} and {hi}` — nothing lies between {lo} and {hi}; the lower bound comes first."
10370 ),
10371 ));
10372 }
10373 TensorElemMap::Clamp { lo, hi }
10374 } else {
10375 let m = self.take_signed_int_literal().ok_or_else(|| {
10376 refuse(self, format!("`{name} {form}` — M must be an integer literal."))
10377 })?;
10378 want(self, "shifted")?;
10379 want(self, "right")?;
10380 want(self, "by")?;
10381 let s = self
10382 .take_int_literal()
10383 .filter(|s| (0..=63).contains(s))
10384 .ok_or_else(|| {
10385 refuse(self, format!("`{name} {form}` — S must be a whole number from 0 to 63."))
10386 })?;
10387 TensorElemMap::Requant { m, s }
10388 };
10389 self.user_bound.insert(var);
10390 self.bind_tensor(var, (r, c), true);
10391 Ok(Some(self.desugar_tensor_map(var, x, r * c, map, true)))
10392 }
10393
10394 fn try_parse_tensor_binop(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10397 if !self.tensor_shapes.contains_key(&self.peek().lexeme) {
10399 return Ok(None);
10400 }
10401 let saved = self.backtrack_point();
10402 let Ok(lhs) = self.parse_primary_expr() else {
10403 self.backtrack_to(&saved);
10404 return Ok(None);
10405 };
10406 if self.check_word("scaled") {
10408 let Some((x, (rows, cols))) = (match lhs {
10409 Expr::Identifier(x) => self.tensor_shapes.get(x).map(|&shape| (*x, shape)),
10410 _ => None,
10411 }) else {
10412 self.backtrack_to(&saved);
10413 return Ok(None);
10414 };
10415 self.advance();
10416 if !self.check_word("by") {
10417 return Err(ParseError {
10418 kind: ParseErrorKind::Custom("`X scaled by C` — expected `by` after `scaled`.".to_string()),
10419 span: self.current_span(),
10420 });
10421 }
10422 self.advance();
10423 let negative = self.check(&TokenType::Minus);
10424 if negative {
10425 self.advance();
10426 }
10427 let factor = match self.peek().kind {
10428 TokenType::Number(sym) => {
10429 self.interner.resolve(sym).replace('_', "").parse::<f64>().ok().map(|c| if negative { -c } else { c })
10430 }
10431 _ => None,
10432 };
10433 let Some(factor) = factor.filter(|c| c.is_finite()) else {
10434 return Err(ParseError {
10435 kind: ParseErrorKind::Custom("`X scaled by C` — C must be a number written out.".to_string()),
10436 span: self.current_span(),
10437 });
10438 };
10439 self.advance();
10440 self.user_bound.insert(var);
10441 self.bind_tensor(var, (rows, cols), false);
10442 return Ok(Some(self.desugar_tensor_scale(var, x, rows * cols, factor)));
10443 }
10444 if self.check_word("capped") {
10446 let Some((x, (rows, cols))) = (match lhs {
10447 Expr::Identifier(x) => self.tensor_shapes.get(x).map(|&shape| (*x, shape)),
10448 _ => None,
10449 }) else {
10450 self.backtrack_to(&saved);
10451 return Ok(None);
10452 };
10453 let cap = self.take_cap("X")?.expect("`capped` was seen");
10454 self.user_bound.insert(var);
10455 self.bind_tensor(var, (rows, cols), false);
10456 return Ok(Some(self.desugar_tensor_cap(var, x, rows * cols, cap)));
10457 }
10458 if self.check_word("joined") {
10461 let Some((a, (ra, ca))) = (match lhs {
10462 Expr::Identifier(a) => self.tensor_shapes.get(a).map(|&shape| (*a, shape)),
10463 _ => None,
10464 }) else {
10465 self.backtrack_to(&saved);
10466 return Ok(None);
10467 };
10468 self.advance();
10469 if !self.check_word("with") {
10470 return Err(ParseError {
10471 kind: ParseErrorKind::Custom("`A joined with B` — expected `with` after `joined`.".to_string()),
10472 span: self.current_span(),
10473 });
10474 }
10475 self.advance();
10476 let b = self.peek().lexeme;
10477 let Some(&(rb, cb)) = self.tensor_shapes.get(&b) else {
10478 return Err(ParseError {
10479 kind: ParseErrorKind::Custom("`A joined with B` — B must be a declared tensor.".to_string()),
10480 span: self.current_span(),
10481 });
10482 };
10483 self.advance();
10484 let heads = self.take_across_heads("A joined with B")?.unwrap_or(1);
10485 if ra != rb {
10486 return Err(ParseError {
10487 kind: ParseErrorKind::Custom(format!(
10488 "`A joined with B` — A and B must have the same rows, and `{}` is {ra}×{ca} and `{}` {rb}×{cb}.",
10489 self.interner.resolve(a),
10490 self.interner.resolve(b)
10491 )),
10492 span: self.current_span(),
10493 });
10494 }
10495 if ca % heads != 0 || cb % heads != 0 {
10496 return Err(ParseError {
10497 kind: ParseErrorKind::Custom(format!(
10498 "`A joined with B across {heads} heads` — A's {ca} columns and B's {cb} must each divide among the {heads} heads."
10499 )),
10500 span: self.current_span(),
10501 });
10502 }
10503 let int = self.tensor_int.contains(&a) && self.tensor_int.contains(&b);
10504 self.user_bound.insert(var);
10505 self.bind_tensor(var, (ra, ca + cb), int);
10506 return Ok(Some(self.desugar_tensor_join(var, a, b, ra, heads, ca / heads, cb / heads, int)));
10507 }
10508 let op = if self.check_word("matmul") {
10509 TensorBinop::Matmul
10510 } else if self.check_word("plus") {
10511 TensorBinop::Add
10512 } else if self.check_word("hadamard") {
10513 TensorBinop::Hadamard
10514 } else {
10515 self.backtrack_to(&saved);
10516 return Ok(None);
10517 };
10518 self.advance(); let rhs = self.parse_primary_expr()?;
10520 let heads = match op {
10522 TensorBinop::Matmul => self.take_across_heads("X matmul W")?,
10523 _ => None,
10524 };
10525 let (Expr::Identifier(a), Expr::Identifier(b)) = (lhs, rhs) else {
10526 return Err(ParseError {
10527 kind: ParseErrorKind::Custom(format!(
10528 "`{op}` takes two tensor variables — `Let C be A {op} B`.",
10529 op = op.word()
10530 )),
10531 span: self.current_span(),
10532 });
10533 };
10534 let (a, b) = (*a, *b);
10535 let int = self.tensor_int.contains(&a) && self.tensor_int.contains(&b);
10537 let (m, ka) = *self.tensor_shapes.get(&a).ok_or_else(|| ParseError {
10538 kind: ParseErrorKind::Custom(format!(
10539 "`{}`'s left operand `{}` is not a declared tensor.",
10540 op.word(),
10541 self.interner.resolve(a)
10542 )),
10543 span: self.current_span(),
10544 })?;
10545 let (kb, n) = *self.tensor_shapes.get(&b).ok_or_else(|| ParseError {
10546 kind: ParseErrorKind::Custom(format!(
10547 "`{}`'s right operand `{}` is not a declared tensor.",
10548 op.word(),
10549 self.interner.resolve(b)
10550 )),
10551 span: self.current_span(),
10552 })?;
10553 match op {
10554 TensorBinop::Matmul if heads.is_some_and(|h| h > 1) => {
10555 let h = heads.unwrap_or(1);
10556 if ka % h != 0 || kb != ka {
10557 return Err(ParseError {
10558 kind: ParseErrorKind::Custom(format!(
10559 "`X matmul W across {h} heads` — X's {ka} columns must divide among the {h} heads, and W must \
10560 have a row for each of them; W is {kb}×{n}."
10561 )),
10562 span: self.current_span(),
10563 });
10564 }
10565 self.user_bound.insert(var);
10566 self.bind_tensor(var, (m, h * n), int);
10567 Ok(Some(self.desugar_tensor_head_matmul(var, a, b, m, h, ka / h, n, int)))
10568 }
10569 TensorBinop::Matmul => {
10570 if ka != kb {
10571 return Err(ParseError {
10572 kind: ParseErrorKind::Custom(format!(
10573 "Shape mismatch in `matmul`: the left tensor is {m}×{ka} and the right is {kb}×{n} — \
10574 the inner dimensions ({ka} and {kb}) must agree."
10575 )),
10576 span: self.current_span(),
10577 });
10578 }
10579 self.user_bound.insert(var);
10580 self.bind_tensor(var, (m, n), int);
10581 Ok(Some(self.desugar_tensor_matmul(var, a, b, m, ka, n, int)))
10582 }
10583 TensorBinop::Add | TensorBinop::Hadamard => {
10584 let row = (kb == 1 && n == ka && m > 1).then_some(ka);
10586 if (m, ka) != (kb, n) && row.is_none() {
10587 return Err(ParseError {
10588 kind: ParseErrorKind::Custom(format!(
10589 "Shape mismatch in `{}`: the tensors are {m}×{ka} and {kb}×{n} — an elementwise op \
10590 needs identical shapes, or a right operand of one row as wide as the left's rows.",
10591 op.word()
10592 )),
10593 span: self.current_span(),
10594 });
10595 }
10596 let kind = if matches!(op, TensorBinop::Add) {
10597 BinaryOpKind::Add
10598 } else {
10599 BinaryOpKind::Multiply
10600 };
10601 self.user_bound.insert(var);
10602 self.bind_tensor(var, (m, ka), int);
10603 Ok(Some(self.desugar_tensor_elementwise(
10604 var,
10605 a,
10606 b,
10607 m * ka,
10608 kind,
10609 int,
10610 row,
10611 )))
10612 }
10613 }
10614 }
10615
10616 pub fn parse(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10633 let mut result = self.parse_prefix()?;
10634
10635 while self.check(&TokenType::While) {
10640 self.advance();
10641 let condition = if matches!(self.peek().kind, TokenType::Adjective(_))
10642 && matches!(
10643 self.tokens.get(self.current + 1).map(|t| &t.kind),
10644 Some(TokenType::Period) | Some(TokenType::EOF) | None
10645 ) {
10646 let adj = match self.advance().kind {
10647 TokenType::Adjective(a) => a,
10648 _ => unreachable!("guarded by the match above"),
10649 };
10650 let subject = Self::find_agent_term(result)
10651 .unwrap_or(Term::Constant(self.interner.intern("System")));
10652 &*self.ctx.exprs.alloc(LogicExpr::Predicate {
10653 name: adj,
10654 args: self.ctx.terms.alloc_slice([subject]),
10655 world: None,
10656 })
10657 } else {
10658 self.parse_sentence()?
10659 };
10660 if self.check(&TokenType::Period) {
10661 self.advance();
10662 }
10663 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
10664 left: condition,
10665 op: TokenType::Implies,
10666 right: result,
10667 });
10668 }
10669
10670 let at_any_time_from = if matches!(&self.peek().kind, TokenType::Preposition(p) if self.interner.resolve(*p).eq_ignore_ascii_case("at"))
10674 && matches!(
10675 self.tokens.get(self.current + 1).map(|t| &t.kind),
10676 Some(TokenType::Any)
10677 )
10678 && self
10679 .tokens
10680 .get(self.current + 2)
10681 .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("time"))
10682 .unwrap_or(false)
10683 {
10684 Some(3)
10686 } else if matches!(self.peek().kind, TokenType::Any)
10687 && self
10688 .tokens
10689 .get(self.current + 1)
10690 .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("time"))
10691 .unwrap_or(false)
10692 {
10693 Some(2)
10696 } else {
10697 None
10698 };
10699 if let Some(n) = at_any_time_from {
10700 for _ in 0..n {
10701 self.advance();
10702 }
10703 if self.check(&TokenType::Period) {
10704 self.advance();
10705 }
10706 result = self.ctx.exprs.alloc(LogicExpr::Temporal {
10707 operator: TemporalOperator::Always,
10708 body: result,
10709 });
10710 }
10711
10712 if !self.is_at_end() {
10715 return Err(ParseError {
10716 kind: crate::error::ParseErrorKind::TrailingTokens {
10717 found: self.peek().kind.clone(),
10718 },
10719 span: self.current_span(),
10720 });
10721 }
10722
10723 Ok(result)
10724 }
10725
10726 fn find_agent_term(expr: &'a LogicExpr<'a>) -> Option<Term<'a>> {
10729 match expr {
10730 LogicExpr::NeoEvent(data) => data
10731 .roles
10732 .iter()
10733 .find(|(role, _)| matches!(role, crate::ast::ThematicRole::Agent))
10734 .map(|(_, term)| term.clone()),
10735 LogicExpr::Modal { operand, .. } => Self::find_agent_term(operand),
10736 LogicExpr::UnaryOp { operand, .. } => Self::find_agent_term(operand),
10737 LogicExpr::Temporal { body, .. } => Self::find_agent_term(body),
10738 LogicExpr::Aspectual { body, .. } => Self::find_agent_term(body),
10739 LogicExpr::Quantifier { body, .. } => Self::find_agent_term(body),
10740 LogicExpr::BinaryOp { left, right, .. } => {
10741 Self::find_agent_term(left).or_else(|| Self::find_agent_term(right))
10742 }
10743 _ => None,
10744 }
10745 }
10746
10747 pub(super) fn parse_prefix(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10753 let mut result = self.parse_sentence()?;
10754
10755 while self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
10758 self.advance(); if !self.is_at_end() {
10760 let next = self.parse_sentence()?;
10761 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
10762 left: result,
10763 op: TokenType::And,
10764 right: next,
10765 });
10766 }
10767 }
10768
10769 Ok(result)
10770 }
10771
10772 pub fn parse_pragmatic(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10777 self.pragmatic = true;
10778 let scalar_trigger = matches!(
10781 self.peek().kind,
10782 TokenType::Some | TokenType::Most | TokenType::Many
10783 );
10784 let result = self.parse()?;
10785 if scalar_trigger {
10786 if let Some(strengthened) = self.scalar_implicature(result) {
10787 return Ok(strengthened);
10788 }
10789 }
10790 Ok(result)
10791 }
10792
10793 pub fn views(&self) -> &[crate::ast::view::ViewDef<'a>] {
10812 &self.views
10813 }
10814
10815 pub fn take_views(&mut self) -> Vec<crate::ast::view::ViewDef<'a>> {
10818 std::mem::take(&mut self.views)
10819 }
10820
10821 pub fn into_type_registry(self) -> TypeRegistry {
10826 self.type_registry.unwrap_or_default()
10827 }
10828
10829 pub fn lgx_findings(&self) -> &[crate::lgx::check::Finding] {
10833 &self.lgx_findings
10834 }
10835
10836 pub fn value_copy_findings(&self) -> &[crate::lint::Finding] {
10839 &self.value_copy_findings
10840 }
10841
10842 fn parse_given_fetch(&mut self) -> ParseResult<Option<Stmt<'a>>> {
10847 const METHODS: &[&str] = &["GET", "POST", "PUT", "PATCH", "DELETE"];
10848 let Some(method_word) = self
10849 .tokens
10850 .get(self.current + 2)
10851 .map(|t| self.interner.resolve(t.lexeme).to_ascii_uppercase())
10852 else {
10853 return Ok(None);
10854 };
10855 if !METHODS.contains(&method_word.as_str()) {
10856 return Ok(None);
10857 }
10858 self.advance(); self.advance(); let method = self.interner.intern(&method_word);
10861 self.advance(); let url = self.parse_imperative_expr()?;
10863 if !self.check_word("answers") {
10864 return Err(ParseError {
10865 kind: ParseErrorKind::Custom(
10866 "`Given that METHOD \"/url\" answers STATUS with BODY.` — say what the \
10867 request answers with, so the page has something to read."
10868 .to_string(),
10869 ),
10870 span: self.current_span(),
10871 });
10872 }
10873 self.advance(); let status = self.parse_imperative_expr()?;
10875 if !self.check_word("with") {
10876 return Err(ParseError {
10877 kind: ParseErrorKind::Custom(
10878 "`Given that METHOD \"/url\" answers STATUS with BODY.` — the status needs a \
10879 body after `with` (write `with \"\"` for an empty one)."
10880 .to_string(),
10881 ),
10882 span: self.current_span(),
10883 });
10884 }
10885 self.advance(); let body = self.parse_imperative_expr()?;
10887 Ok(Some(Stmt::GivenFetch {
10888 method,
10889 url,
10890 status,
10891 body,
10892 }))
10893 }
10894
10895 fn parse_journal_action_name(&mut self) -> ParseResult<Symbol> {
10907 let span = self.current_span();
10908 if let TokenType::StringLiteral(sym) = self.peek().kind {
10909 self.advance();
10910 self.journal_action_sites.push((sym, span));
10911 return Ok(sym);
10912 }
10913 match self.expect_identifier() {
10914 Ok(sym) => {
10915 self.journal_action_sites.push((sym, span));
10916 Ok(sym)
10917 }
10918 Err(_) => Err(ParseError {
10919 kind: ParseErrorKind::Custom(
10920 "An action is named here the way its `To <name>:` declares it — \
10921 `Replay these actions: add, remove.`, `the journal shows loaded then \
10922 failed`."
10923 .to_string(),
10924 ),
10925 span,
10926 }),
10927 }
10928 }
10929
10930 fn skip_optional_header_colon(&mut self) {
10938 let mut i = self.current;
10939 while let Some(tok) = self.tokens.get(i) {
10940 if matches!(tok.kind, TokenType::Newline) {
10941 break;
10942 }
10943 i += 1;
10944 }
10945 if i > self.current
10946 && matches!(
10947 self.tokens.get(i.wrapping_sub(1)).map(|t| &t.kind),
10948 Some(TokenType::Colon)
10949 )
10950 {
10951 self.tokens.remove(i - 1);
10952 }
10953 }
10954
10955 pub fn parse_program(&mut self) -> ParseResult<Vec<Stmt<'a>>> {
10956 let mut statements = Vec::new();
10957 self.stmt_spans.clear();
10958 self.nested_stmt_spans = NestedStmtSpans::default();
10959 self.moves.clear();
10960 self.declare_component_names();
10961 self.declare_function_names();
10962 let mut in_definition_block = false;
10963 let mut pending_opt_flags = OptimizationConfig::all_on();
10964
10965 if self.mode == ParserMode::Declarative {
10967 }
10971
10972 while !self.is_at_end() {
10973 if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
10975 self.release_moves_since(0);
10976 }
10977 if let Some(Token {
10979 kind: TokenType::BlockHeader { block_type },
10980 ..
10981 }) = self.tokens.get(self.current)
10982 {
10983 let block_type = *block_type;
10984 self.skip_optional_header_colon();
10985 match &block_type {
10986 BlockType::SuspectedTypo { found, suggestion } => {
10987 let found = self.interner.resolve(*found).to_string();
10988 let suggestion_str = self.interner.resolve(*suggestion);
10989 let mut canonical: String = suggestion_str.to_string();
10990 if let Some(c) = canonical.get_mut(0..1) {
10991 c.make_ascii_uppercase();
10992 }
10993 return Err(ParseError {
10994 kind: ParseErrorKind::ExpectedKeyword {
10995 keyword: format!(
10996 "a known header — `## {}` looks like a typo of `## {}` (if it is a prose heading, pick a name further from the code headers)",
10997 found, canonical
10998 ),
10999 },
11000 span: self.current_span(),
11001 });
11002 }
11003 BlockType::Definition => {
11004 in_definition_block = true;
11005 self.mode = ParserMode::Declarative;
11006 self.advance();
11007 continue;
11008 }
11009 BlockType::Main => {
11010 self.program_opt_flags = self.program_opt_flags.merged(&pending_opt_flags);
11013 pending_opt_flags = OptimizationConfig::all_on();
11014 in_definition_block = false;
11015 self.mode = ParserMode::Imperative;
11016 self.advance();
11017 continue;
11018 }
11019 BlockType::No => {
11020 self.advance(); if let Some(token) = self.tokens.get(self.current) {
11028 let word = self.interner.resolve(token.lexeme).to_lowercase();
11029 if word == "optimize" {
11030 pending_opt_flags = OptimizationConfig::all_off();
11031 } else if let Some(opt) = by_keyword(&word) {
11032 pending_opt_flags.set(opt, false);
11033 }
11034 self.advance(); }
11037 while self.check(&TokenType::Newline) {
11038 self.advance();
11039 }
11040 continue;
11041 }
11042 BlockType::Require => {
11043 self.advance(); if let Some(token) = self.tokens.get(self.current) {
11050 let word = self.interner.resolve(token.lexeme).to_lowercase();
11051 if let Some(opt) = by_keyword(&word) {
11052 self.required_opts.push(opt);
11053 }
11054 self.advance(); }
11057 while self.check(&TokenType::Newline) {
11058 self.advance();
11059 }
11060 continue;
11061 }
11062 BlockType::Tier => {
11063 self.advance(); let kw = self
11069 .tokens
11070 .get(self.current)
11071 .map(|t| self.interner.resolve(t.lexeme).to_lowercase());
11072 if kw.is_some() {
11073 self.advance(); }
11075 let val = self
11076 .tokens
11077 .get(self.current)
11078 .map(|t| self.interner.resolve(t.lexeme).to_lowercase());
11079 if val.is_some() {
11080 self.advance(); }
11082 if let (Some(kw), Some(val)) = (kw, val) {
11083 if let (Some(opt), Some(pin)) = (by_keyword(&kw), pin_from_str(&val)) {
11084 self.program_tier_pins.set(opt, pin);
11085 }
11086 }
11088 while self.check(&TokenType::Newline) {
11089 self.advance();
11090 }
11091 continue;
11092 }
11093 BlockType::Function => {
11094 in_definition_block = false;
11095 self.mode = ParserMode::Imperative;
11096 let header_tok = self.current;
11097 self.advance();
11098 let flags = std::mem::take(&mut pending_opt_flags);
11100 let func_def = self.parse_function_def_with_flags(flags)?;
11101 statements.push(func_def);
11102 self.record_stmt_span(header_tok);
11103 continue;
11104 }
11105 BlockType::Component => {
11106 in_definition_block = false;
11107 self.mode = ParserMode::Imperative;
11108 let header_tok = self.current;
11109 self.advance();
11110 let comp_def = self.parse_lgx_component_block()?;
11111 statements.push(comp_def);
11112 self.record_stmt_span(header_tok);
11113 continue;
11114 }
11115 BlockType::Page { path } => {
11116 in_definition_block = false;
11117 self.mode = ParserMode::Imperative;
11118 let header_tok = self.current;
11119 let path = *path;
11120 let def = self.parse_page_block(path)?;
11121 statements.push(def);
11122 self.record_stmt_span(header_tok);
11123 continue;
11124 }
11125 BlockType::Store => {
11126 in_definition_block = false;
11127 self.mode = ParserMode::Imperative;
11128 let header_tok = self.current;
11129 let def = self.parse_store_block()?;
11130 statements.push(def);
11131 self.record_stmt_span(header_tok);
11132 continue;
11133 }
11134 BlockType::Directive => {
11135 in_definition_block = false;
11136 self.mode = ParserMode::Imperative;
11137 let header_tok = self.current;
11138 let def = self.parse_directive_block()?;
11139 statements.push(def);
11140 self.record_stmt_span(header_tok);
11141 continue;
11142 }
11143 BlockType::Route { method, path } => {
11144 in_definition_block = false;
11145 self.mode = ParserMode::Imperative;
11146 let header_tok = self.current;
11147 let (method, path) = (*method, *path);
11148 let def = self.parse_route_block(method, path)?;
11149 statements.push(def);
11150 self.record_stmt_span(header_tok);
11151 continue;
11152 }
11153 BlockType::Middleware => {
11154 in_definition_block = false;
11155 self.mode = ParserMode::Imperative;
11156 let header_tok = self.current;
11157 let def = self.parse_middleware_block()?;
11158 statements.push(def);
11159 self.record_stmt_span(header_tok);
11160 continue;
11161 }
11162 BlockType::Redirect { from, to } => {
11163 in_definition_block = false;
11164 self.mode = ParserMode::Imperative;
11165 let header_tok = self.current;
11166 let (from, to) = (*from, *to);
11167 let def = self.parse_redirect_block(from, to)?;
11168 statements.push(def);
11169 self.record_stmt_span(header_tok);
11170 continue;
11171 }
11172 BlockType::ErrorPage { status } => {
11173 in_definition_block = false;
11174 self.mode = ParserMode::Imperative;
11175 let header_tok = self.current;
11176 let status = *status;
11177 let def = self.parse_error_page_block(status)?;
11178 statements.push(def);
11179 self.record_stmt_span(header_tok);
11180 continue;
11181 }
11182 BlockType::TypeDef => {
11183 self.advance();
11186 self.skip_type_def_content();
11187 continue;
11188 }
11189 BlockType::Session => {
11190 self.advance();
11198 self.session_declared = true;
11199 self.skip_type_def_content();
11200 continue;
11201 }
11202 BlockType::Policy => {
11203 in_definition_block = true; self.mode = ParserMode::Declarative;
11207 self.advance();
11208 continue;
11209 }
11210 BlockType::Hardware | BlockType::Property => {
11211 in_definition_block = true;
11214 self.mode = ParserMode::Declarative;
11215 self.advance();
11216 continue;
11217 }
11218 BlockType::Theorem => {
11219 in_definition_block = false;
11221 self.mode = ParserMode::Declarative;
11222 let header_tok = self.current;
11223 self.advance();
11224 let theorem = self.parse_theorem_block()?;
11225 statements.push(theorem);
11226 self.record_stmt_span(header_tok);
11227 continue;
11228 }
11229 BlockType::Define => {
11230 in_definition_block = false;
11232 self.mode = ParserMode::Declarative;
11233 let header_tok = self.current;
11234 self.advance();
11235 let definition = self.parse_define_block()?;
11236 statements.push(definition);
11237 self.record_stmt_span(header_tok);
11238 continue;
11239 }
11240 BlockType::Axiom => {
11241 in_definition_block = false;
11243 self.mode = ParserMode::Declarative;
11244 let header_tok = self.current;
11245 self.advance();
11246 let axiom = self.parse_axiom_block()?;
11247 statements.push(axiom);
11248 self.record_stmt_span(header_tok);
11249 continue;
11250 }
11251 BlockType::Theory => {
11252 in_definition_block = false;
11254 self.mode = ParserMode::Declarative;
11255 let header_tok = self.current;
11256 self.advance();
11257 let theory = self.parse_theory_block()?;
11258 statements.push(theory);
11259 self.record_stmt_span(header_tok);
11260 continue;
11261 }
11262 BlockType::Requires => {
11263 in_definition_block = false;
11264 self.mode = ParserMode::Declarative;
11265 let header_tok = self.current;
11266 self.advance();
11267 let deps = self.parse_requires_block()?;
11268 for _ in 0..deps.len() {
11269 self.record_stmt_span(header_tok);
11270 }
11271 statements.extend(deps);
11272 continue;
11273 }
11274 BlockType::Test { name } => {
11275 let name = *name;
11279 in_definition_block = false;
11280 self.mode = ParserMode::Imperative;
11281 let header_tok = self.current;
11282 self.advance();
11283 let test_def = self.parse_test_block(name)?;
11284 statements.push(test_def);
11285 self.record_stmt_span(header_tok);
11286 continue;
11287 }
11288 _ => {
11289 in_definition_block = false;
11291 self.mode = ParserMode::Declarative;
11292 self.advance();
11293 continue;
11294 }
11295 }
11296 }
11297
11298 if in_definition_block {
11300 self.advance();
11301 continue;
11302 }
11303
11304 if self.check(&TokenType::Indent)
11306 || self.check(&TokenType::Dedent)
11307 || self.check(&TokenType::Newline)
11308 {
11309 self.advance();
11310 continue;
11311 }
11312
11313 if self.mode == ParserMode::Imperative {
11315 if self.check_word("Accept") {
11319 self.parse_accept_contract()?;
11320 continue;
11321 }
11322 let start_tok = self.current;
11323 let stmt = self.parse_moving_statement()?;
11324 statements.push(stmt);
11325
11326 if self.check(&TokenType::Period) {
11327 self.advance();
11328 }
11329 self.record_stmt_span(start_tok);
11330 } else {
11331 self.advance();
11333 }
11334 }
11335
11336 self.program_opt_flags = self.program_opt_flags.merged(&pending_opt_flags);
11339
11340 if !self.fragment_mode {
11348 self.check_mounts_name_something()?;
11354 self.check_mounted_components_need_no_props(&statements)?;
11355
11356 self.mount_the_declared_page(&mut statements)?;
11361
11362 self.resolve_lgx(&mut statements)?;
11370
11371 self.lower_shared_stores(&mut statements);
11375 }
11376
11377 if !self.deferred_functions.is_empty() {
11383 let deferred = std::mem::take(&mut self.deferred_functions);
11384 let span = self
11385 .stmt_spans
11386 .first()
11387 .copied()
11388 .unwrap_or_else(|| self.current_span());
11389 let mut combined = deferred;
11390 for _ in 0..combined.len() {
11391 self.stmt_spans.insert(0, span);
11392 }
11393 combined.extend(statements);
11394 statements = combined;
11395 }
11396
11397 if !self.fragment_mode {
11401 self.check_lends_are_exclusive(&statements)?;
11402 }
11403
11404 debug_assert_eq!(
11405 self.stmt_spans.len(),
11406 statements.len(),
11407 "stmt_spans must stay aligned 1:1 with the statement list"
11408 );
11409
11410 crate::ast_depth::validate_program_depth(
11415 &statements,
11416 self.stmt_spans
11417 .first()
11418 .copied()
11419 .unwrap_or_else(|| self.current_span()),
11420 )?;
11421
11422 Ok(statements)
11423 }
11424
11425 fn declare_component_names(&mut self) {
11431 for (i, token) in self.tokens.iter().enumerate() {
11432 if !matches!(
11433 token.kind,
11434 TokenType::BlockHeader {
11435 block_type: BlockType::Component
11436 }
11437 ) {
11438 continue;
11439 }
11440 let Some(name) = self.tokens.get(i + 1) else {
11441 continue;
11442 };
11443 let legacy = self
11444 .tokens
11445 .get(i + 2)
11446 .is_some_and(|t| matches!(t.kind, TokenType::Colon));
11447 self.components.insert(name.lexeme);
11448 if !legacy {
11449 self.lgx_components.insert(name.lexeme);
11450 }
11451 }
11452 }
11453
11454 fn declare_function_names(&mut self) {
11461 for (i, token) in self.tokens.iter().enumerate() {
11462 if !matches!(
11463 token.kind,
11464 TokenType::BlockHeader {
11465 block_type: BlockType::Function
11466 }
11467 ) {
11468 continue;
11469 }
11470 let mut j = i + 1;
11471 if self
11472 .tokens
11473 .get(j)
11474 .is_some_and(|t| matches!(t.kind, TokenType::Native))
11475 {
11476 j += 1;
11477 }
11478 let Some(name) = self.tokens.get(j) else {
11479 continue;
11480 };
11481 self.declared_function_names.insert(name.lexeme);
11482 }
11483 }
11484
11485 pub(super) fn enter_recursion(&mut self) -> ParseResult<()> {
11489 self.recursion_depth += 1;
11490 let limit = crate::ast_depth::max_parse_recursion();
11491 if self.recursion_depth > limit {
11492 self.recursion_depth -= 1;
11493 return Err(ParseError {
11494 kind: ParseErrorKind::AstTooDeep {
11495 depth: limit + 1,
11496 max_depth: limit,
11497 },
11498 span: self.current_span(),
11499 });
11500 }
11501 Ok(())
11502 }
11503
11504 pub(super) fn leave_recursion(&mut self) {
11506 self.recursion_depth = self.recursion_depth.saturating_sub(1);
11507 }
11508
11509 pub fn program_opt_flags(&self) -> OptimizationConfig {
11513 self.program_opt_flags
11514 }
11515
11516 pub fn program_tier_pins(&self) -> PinSet {
11520 self.program_tier_pins
11521 }
11522
11523 pub fn required_optimizations(&self) -> Vec<Opt> {
11526 self.required_opts.clone()
11527 }
11528
11529 pub fn tensor_shapes(&self) -> &std::collections::HashMap<Symbol, (i64, i64)> {
11532 &self.tensor_shapes
11533 }
11534
11535 fn parse_statement(&mut self) -> ParseResult<Stmt<'a>> {
11540 self.enter_recursion()?;
11541 let start_tok = self.current;
11542 let result = self.parse_statement_inner();
11543 self.leave_recursion();
11544 if let Ok(stmt) = &result {
11545 if self.copy_lint_scope.is_some() {
11546 let span = self.span_from(start_tok);
11547 let Self {
11548 copy_lint_scope,
11549 interner,
11550 ..
11551 } = self;
11552 copy_lint_scope
11553 .as_mut()
11554 .unwrap()
11555 .observe(span, stmt, &**interner);
11556 }
11557 }
11558 result
11559 }
11560
11561 fn parse_statement_inner(&mut self) -> ParseResult<Stmt<'a>> {
11562 if self.declared_function_names.contains(&self.peek().lexeme)
11567 && self.next_is_argument_list()
11568 {
11569 return self.parse_bare_call_statement();
11570 }
11571 if self.lexeme_is(0, "Render")
11575 && (matches!(
11576 self.tokens.get(self.current + 1).map(|t| &t.kind),
11577 Some(TokenType::Colon)
11578 ) || self.lexeme_is(1, "into"))
11579 {
11580 return Err(ParseError {
11581 kind: ParseErrorKind::Custom(
11582 "`Render:` is retired — a view is a `View:` section of a `## Page` or \
11583 `## Component`, with `State:`/`Actions:`/`Style:` beside it."
11584 .to_string(),
11585 ),
11586 span: self.current_span(),
11587 });
11588 }
11589 if !self.in_component && self.next_is_section_header() {
11593 let word = self.interner.resolve(self.peek().lexeme).to_string();
11594 return Err(ParseError {
11595 kind: ParseErrorKind::Custom(format!(
11596 "`{word}:` is a component section, but there is no component here. Start one with \
11597 `## Component <Name>`, `## Page /<path>` or `## Store <Name>`, then its `State:`, \
11598 `Actions:`, `View:` and `Style:` sections follow."
11599 )),
11600 span: self.current_span(),
11601 });
11602 }
11603 let reloads_the_page =
11610 self.lexeme_is(0, "Reload") && self.lexeme_is(1, "the") && self.lexeme_is(2, "page");
11611 if self.in_component
11622 || reloads_the_page
11623 || crate::parser::component::UI_STATEMENT_OPENERS
11624 .iter()
11625 .any(|w| self.lexeme_is(0, w))
11626 || (self.lexeme_is(0, "Press") && !self.next_is_colon_like())
11627 || (self.lexeme_is(0, "Type") && !self.next_is_colon_like())
11628 {
11629 if let Some(stmt) = self.parse_ui_statement()? {
11630 return Ok(stmt);
11631 }
11632 }
11633 if self.lexeme_is(0, "Replay") && self.lexeme_is(1, "these") && self.lexeme_is(2, "actions")
11640 {
11641 for _ in 0..3 {
11642 self.advance();
11643 }
11644 if self.check(&TokenType::Colon) {
11645 self.advance();
11646 }
11647 let mut actions = vec![self.parse_journal_action_name()?];
11648 while self.check(&TokenType::Comma) || self.check_word("and") {
11649 self.advance();
11650 actions.push(self.parse_journal_action_name()?);
11651 }
11652 return Ok(Stmt::Replay { actions });
11653 }
11654 if self.lexeme_is(0, "Serve")
11657 && self.lexeme_is(1, "fetches")
11658 && self.lexeme_is(2, "with")
11659 && self.lexeme_is(3, "routes")
11660 {
11661 for _ in 0..4 {
11662 self.advance();
11663 }
11664 return Ok(Stmt::ServeFetches);
11665 }
11666 if self.lexeme_is(0, "Given") && self.lexeme_is(1, "that") {
11667 if let Some(stmt) = self.parse_given_fetch()? {
11668 return Ok(stmt);
11669 }
11670 }
11671 if self.at_limit_rule() {
11672 return self.parse_limit_rule();
11673 }
11674 if self.check(&TokenType::To) || self.check_preposition_is("to") {
11677 return self.parse_function_def();
11678 }
11679 if self.check(&TokenType::Let) {
11680 return self.parse_let_statement();
11681 }
11682 if self.check(&TokenType::Mut) {
11689 return self.parse_equals_assignment(true);
11690 }
11691 if self.peek_equals_assignment() {
11694 return self.parse_equals_assignment(false);
11695 }
11696 if self.peek_compound_assignment() {
11698 return self.parse_compound_assignment();
11699 }
11700 if self.check(&TokenType::Set) {
11701 return self.parse_set_statement();
11702 }
11703 if self.check_word("On")
11707 && self
11708 .tokens
11709 .get(self.current + 2)
11710 .is_some_and(|t| matches!(t.kind, TokenType::From))
11711 {
11712 return self.parse_on_statement();
11713 }
11714 if self.check(&TokenType::Return) {
11715 return self.parse_return_statement();
11716 }
11717 if self.check(&TokenType::Break) {
11718 return self.parse_break_statement();
11719 }
11720 if self.check(&TokenType::If) {
11721 return self.parse_if_statement();
11722 }
11723 if self.check(&TokenType::Assert) {
11724 return self.parse_assert_statement();
11725 }
11726 if self.check(&TokenType::Require) {
11727 return self.parse_require_statement();
11728 }
11729 if self.check_word("Assume") {
11730 return self.parse_assume_statement();
11731 }
11732 if self.check_word("Fail") && self.check_word_at(self.current + 1, "with") {
11733 return self.parse_fail_statement();
11734 }
11735 if self.check_word("Solve") {
11736 return self.parse_solve_statement();
11737 }
11738 if self.check_word("Minimize") {
11739 return self.parse_minimize_statement();
11740 }
11741 if self.check(&TokenType::Trust) {
11743 return self.parse_trust_statement();
11744 }
11745 if self.check(&TokenType::Expect) {
11747 return self.parse_expect_statement();
11748 }
11749 if self.check(&TokenType::Note) {
11750 return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Note);
11751 }
11752 if self.check(&TokenType::Warn) {
11753 return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Warn);
11754 }
11755 if self.check(&TokenType::Record) {
11756 return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Record);
11757 }
11758 if self.check(&TokenType::During) {
11759 return self.parse_during_statement();
11760 }
11761 if self.check(&TokenType::Advance) {
11762 return self.parse_advance_time_statement();
11763 }
11764 if self.check(&TokenType::Given) && self.mode == ParserMode::Imperative {
11765 return self.parse_given_file_statement();
11766 }
11767 if self.check(&TokenType::Skip) {
11768 return Err(ParseError {
11769 kind: ParseErrorKind::Custom(
11770 "`Skip this test because \"…\".` belongs in a test's leading \
11771 paragraph, before any other statement"
11772 .to_string(),
11773 ),
11774 span: self.current_span(),
11775 });
11776 }
11777 if self.check(&TokenType::Check) {
11779 return self.parse_check_statement();
11780 }
11781 if self.check(&TokenType::Listen) {
11783 return self.parse_listen_statement();
11784 }
11785 if self.check_word("Serve") {
11787 return self.parse_serve_statement();
11788 }
11789 if self.check(&TokenType::NetConnect) {
11790 return self.parse_connect_statement();
11791 }
11792 if self.check(&TokenType::Sleep) {
11793 return self.parse_sleep_statement();
11794 }
11795 if self.check_word("Run") {
11798 return self.parse_run_under_contract();
11799 }
11800 if self.check(&TokenType::Sync) {
11802 return self.parse_sync_statement();
11803 }
11804 if self.check(&TokenType::Mount) {
11806 return self.parse_mount_statement();
11807 }
11808 if self.check(&TokenType::While) {
11809 return self.parse_while_statement();
11810 }
11811 if self.check(&TokenType::Repeat) {
11812 return self.parse_repeat_statement();
11813 }
11814 if self.check(&TokenType::For)
11817 && self.in_test_block
11818 && self.peek_lexeme_lower(1).as_deref() == Some("all")
11819 {
11820 return self.parse_forall_statement();
11821 }
11822 if self.check(&TokenType::For) {
11824 return self.parse_for_statement();
11825 }
11826 if self.check(&TokenType::Call) {
11827 return self.parse_call_statement();
11828 }
11829 if self.check(&TokenType::Give) {
11830 return self.parse_give_statement();
11831 }
11832 if self.check(&TokenType::Show) {
11833 return self.parse_show_statement();
11834 }
11835 if self.check(&TokenType::Inspect) {
11837 return self.parse_inspect_statement();
11838 }
11839
11840 if self.check(&TokenType::Push) {
11842 return self.parse_push_statement();
11843 }
11844 if self.check(&TokenType::Pop) {
11845 return self.parse_pop_statement();
11846 }
11847 if self.check(&TokenType::Add) {
11849 return self.parse_add_statement();
11850 }
11851 if self.check(&TokenType::Remove) {
11852 return self.parse_remove_statement();
11853 }
11854
11855 if self.check(&TokenType::Inside) {
11857 return self.parse_zone_statement();
11858 }
11859
11860 if self.check(&TokenType::Attempt) {
11862 return self.parse_concurrent_block();
11863 }
11864 if self.check(&TokenType::Simultaneously) {
11865 return self.parse_parallel_block();
11866 }
11867
11868 if self.check_word("Reversibly") || self.check_word("Uncompute") {
11872 return self.parse_reversibly_block();
11873 }
11874
11875 if self.check(&TokenType::Read) {
11877 return self.parse_read_statement();
11878 }
11879 if self.check(&TokenType::Write) {
11880 return self.parse_write_statement();
11881 }
11882
11883 if self.check(&TokenType::Spawn) {
11885 return self.parse_spawn_statement();
11886 }
11887 if self.check_word("Stream") {
11888 return self.parse_stream_statement();
11889 }
11890 if self.check(&TokenType::Send) {
11891 if self.lookahead_contains_into() {
11893 return self.parse_send_pipe_statement();
11894 }
11895 return self.parse_send_statement();
11896 }
11897 if self.check(&TokenType::Await) {
11898 if self.lookahead_is_first_of() {
11900 return self.parse_select_statement();
11901 }
11902 if self.lookahead_is_task_await() {
11905 return self.parse_task_await_statement();
11906 }
11907 return self.parse_await_statement();
11908 }
11909 if self.check_word("Wait") {
11911 return self.parse_wait_statement();
11912 }
11913 if self.check_word("Close") {
11915 return self.parse_close_statement();
11916 }
11917
11918 if self.check(&TokenType::Merge) {
11920 return self.parse_merge_statement();
11921 }
11922 if self.check(&TokenType::Converge) {
11923 return self.parse_converge_statement();
11924 }
11925 if self.check(&TokenType::Swap) {
11926 return self.parse_swap_statement();
11927 }
11928 if self.check(&TokenType::Increase) {
11929 return self.parse_increase_statement();
11930 }
11931 if self.check(&TokenType::Decrease) {
11933 return self.parse_decrease_statement();
11934 }
11935 if self.check(&TokenType::Append) {
11936 return self.parse_append_statement();
11937 }
11938 if self.check(&TokenType::Resolve) {
11939 return self.parse_resolve_statement();
11940 }
11941
11942 if self.check(&TokenType::Launch) {
11944 return self.parse_launch_statement();
11945 }
11946 if self.check(&TokenType::Stop) {
11947 return self.parse_stop_statement();
11948 }
11949 if self.check(&TokenType::Try) {
11950 return self.parse_try_statement();
11951 }
11952 if self.check(&TokenType::Receive) {
11953 return self.parse_receive_pipe_statement();
11954 }
11955
11956 if self.check(&TokenType::Escape) {
11958 return self.parse_escape_statement();
11959 }
11960
11961 if self.next_is_argument_list() {
11964 return self.parse_bare_call_statement();
11965 }
11966
11967 if self
11973 .tokens
11974 .get(self.current + 1)
11975 .is_some_and(|t| matches!(t.kind, TokenType::Is))
11976 {
11977 if let Some(value_token) = self.tokens.get(self.current + 2) {
11978 if matches!(
11979 value_token.kind,
11980 TokenType::Number(_)
11981 | TokenType::StringLiteral(_)
11982 | TokenType::InterpolatedString(_)
11983 ) {
11984 let variable = self.interner.resolve(self.peek().lexeme).to_string();
11985 let value = self.interner.resolve(value_token.lexeme).to_string();
11986 let span = Span::new(self.peek().span.start, value_token.span.end);
11987 return Err(ParseError {
11988 kind: ParseErrorKind::IsValueEquality { variable, value },
11989 span,
11990 });
11991 }
11992 }
11993 }
11994
11995 Err(ParseError {
11996 kind: ParseErrorKind::ExpectedStatement,
11997 span: self.current_span(),
11998 })
11999 }
12000
12001 fn parse_if_statement(&mut self) -> ParseResult<Stmt<'a>> {
12002 self.advance(); let cond = self.parse_condition()?;
12006
12007 if self.check(&TokenType::Then) {
12009 self.advance();
12010 }
12011
12012 self.parse_if_branches(cond)
12013 }
12014
12015 fn parse_elif_as_if(&mut self) -> ParseResult<Stmt<'a>> {
12018 let cond = self.parse_condition()?;
12020 self.parse_if_branches(cond)
12021 }
12022
12023 fn parse_if_branches(&mut self, cond: &'a Expr<'a>) -> ParseResult<Stmt<'a>> {
12027 if !self.check(&TokenType::Colon) {
12029 return Err(ParseError {
12030 kind: ParseErrorKind::ExpectedKeyword {
12031 keyword: ":".to_string(),
12032 },
12033 span: self.current_span(),
12034 });
12035 }
12036 self.advance(); let then_block = self.read_branch_body()?;
12041
12042 let else_block = if self.check(&TokenType::Otherwise) || self.check(&TokenType::Else) {
12044 self.advance(); if self.check(&TokenType::If) {
12048 let link_tok = self.current;
12050 let nested_if = self.parse_if_statement()?;
12051 Some(self.read_link_block(nested_if, link_tok))
12052 } else {
12053 if !self.check(&TokenType::Colon) {
12055 return Err(ParseError {
12056 kind: ParseErrorKind::ExpectedKeyword {
12057 keyword: ":".to_string(),
12058 },
12059 span: self.current_span(),
12060 });
12061 }
12062 self.advance(); Some(self.read_branch_body()?)
12066 }
12067 } else if self.check(&TokenType::Elif) {
12068 let link_tok = self.current;
12070 self.advance(); let nested_if = self.parse_elif_as_if()?;
12073 Some(self.read_link_block(nested_if, link_tok))
12074 } else {
12075 None
12076 };
12077
12078 Ok(Stmt::If {
12079 cond,
12080 then_block,
12081 else_block,
12082 })
12083 }
12084
12085 fn parse_while_statement(&mut self) -> ParseResult<Stmt<'a>> {
12086 self.advance(); let cond = self.parse_condition()?;
12089
12090 let decreasing = if self.check(&TokenType::LParen) {
12092 self.advance(); if !self.check_word("decreasing") {
12096 return Err(ParseError {
12097 kind: ParseErrorKind::ExpectedKeyword {
12098 keyword: "decreasing".to_string(),
12099 },
12100 span: self.current_span(),
12101 });
12102 }
12103 self.advance(); let variant = self.parse_imperative_expr()?;
12106
12107 if !self.check(&TokenType::RParen) {
12108 return Err(ParseError {
12109 kind: ParseErrorKind::ExpectedKeyword {
12110 keyword: ")".to_string(),
12111 },
12112 span: self.current_span(),
12113 });
12114 }
12115 self.advance(); Some(variant)
12118 } else {
12119 None
12120 };
12121
12122 if !self.check(&TokenType::Colon) {
12123 return Err(ParseError {
12124 kind: ParseErrorKind::ExpectedKeyword {
12125 keyword: ":".to_string(),
12126 },
12127 span: self.current_span(),
12128 });
12129 }
12130 self.advance(); let body = self.read_indented_block()?;
12133
12134 Ok(Stmt::While {
12135 cond,
12136 body,
12137 decreasing,
12138 })
12139 }
12140
12141 fn parse_loop_pattern(&mut self) -> ParseResult<Pattern> {
12144 use crate::ast::stmt::Pattern;
12145
12146 if self.check(&TokenType::LParen) {
12148 self.advance(); let mut identifiers = Vec::new();
12151 loop {
12152 let id = self.expect_identifier()?;
12153 identifiers.push(id);
12154
12155 if self.check(&TokenType::Comma) {
12157 self.advance(); continue;
12159 }
12160 break;
12161 }
12162
12163 if !self.check(&TokenType::RParen) {
12165 return Err(ParseError {
12166 kind: ParseErrorKind::ExpectedKeyword {
12167 keyword: ")".to_string(),
12168 },
12169 span: self.current_span(),
12170 });
12171 }
12172 self.advance(); Ok(Pattern::Tuple(identifiers))
12175 } else {
12176 let id = self.expect_identifier()?;
12178 Ok(Pattern::Identifier(id))
12179 }
12180 }
12181
12182 fn parse_repeat_statement(&mut self) -> ParseResult<Stmt<'a>> {
12183 self.advance(); if self.check_word("forever") {
12187 self.advance(); if !self.check(&TokenType::Colon) {
12189 return Err(ParseError {
12190 kind: ParseErrorKind::ExpectedKeyword {
12191 keyword: ":".to_string(),
12192 },
12193 span: self.current_span(),
12194 });
12195 }
12196 self.advance(); let body = self.read_indented_block()?;
12198 let cond = self
12199 .ctx
12200 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(true)));
12201 return Ok(Stmt::While {
12202 cond,
12203 body,
12204 decreasing: None,
12205 });
12206 }
12207
12208 if self.check(&TokenType::For) {
12210 self.advance();
12211 }
12212 if self.check_word("each") {
12215 self.advance();
12216 }
12217 if let Some(stmt) = self.try_parse_every_natural_loop()? {
12218 return Ok(stmt);
12219 }
12220
12221 let pattern = self.parse_loop_pattern()?;
12223
12224 let iterable = if self.check(&TokenType::From) || self.check_preposition_is("from") {
12226 self.advance(); let start = self.parse_imperative_expr()?;
12228
12229 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
12231 return Err(ParseError {
12232 kind: ParseErrorKind::ExpectedKeyword {
12233 keyword: "to".to_string(),
12234 },
12235 span: self.current_span(),
12236 });
12237 }
12238 self.advance();
12239
12240 let end = self.parse_imperative_expr()?;
12241 self.ctx.alloc_imperative_expr(Expr::Range { start, end })
12242 } else if self.check(&TokenType::In) || self.check_preposition_is("in") {
12243 self.advance(); self.parse_imperative_expr()?
12245 } else {
12246 return Err(ParseError {
12247 kind: ParseErrorKind::ExpectedKeyword {
12248 keyword: "in or from".to_string(),
12249 },
12250 span: self.current_span(),
12251 });
12252 };
12253
12254 if !self.check(&TokenType::Colon) {
12256 return Err(ParseError {
12257 kind: ParseErrorKind::ExpectedKeyword {
12258 keyword: ":".to_string(),
12259 },
12260 span: self.current_span(),
12261 });
12262 }
12263 self.advance();
12264
12265 let body = self.read_indented_block()?;
12266
12267 Ok(Stmt::Repeat {
12268 pattern,
12269 iterable,
12270 body,
12271 })
12272 }
12273
12274 fn parse_for_statement(&mut self) -> ParseResult<Stmt<'a>> {
12277 self.advance(); if self.check_word("each") {
12280 self.advance();
12281 }
12282 if let Some(stmt) = self.try_parse_every_natural_loop()? {
12283 return Ok(stmt);
12284 }
12285
12286 let pattern = self.parse_loop_pattern()?;
12288
12289 let iterable = if self.check(&TokenType::From) || self.check_preposition_is("from") {
12291 self.advance(); let start = self.parse_imperative_expr()?;
12293
12294 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
12296 return Err(ParseError {
12297 kind: ParseErrorKind::ExpectedKeyword {
12298 keyword: "to".to_string(),
12299 },
12300 span: self.current_span(),
12301 });
12302 }
12303 self.advance();
12304
12305 let end = self.parse_imperative_expr()?;
12306 self.ctx.alloc_imperative_expr(Expr::Range { start, end })
12307 } else if self.check(&TokenType::In) || self.check_preposition_is("in") {
12308 self.advance(); self.parse_imperative_expr()?
12310 } else {
12311 return Err(ParseError {
12312 kind: ParseErrorKind::ExpectedKeyword {
12313 keyword: "in or from".to_string(),
12314 },
12315 span: self.current_span(),
12316 });
12317 };
12318
12319 if !self.check(&TokenType::Colon) {
12321 return Err(ParseError {
12322 kind: ParseErrorKind::ExpectedKeyword {
12323 keyword: ":".to_string(),
12324 },
12325 span: self.current_span(),
12326 });
12327 }
12328 self.advance();
12329
12330 let body = self.read_indented_block()?;
12331
12332 Ok(Stmt::Repeat {
12333 pattern,
12334 iterable,
12335 body,
12336 })
12337 }
12338
12339 fn parse_call_statement(&mut self) -> ParseResult<Stmt<'a>> {
12340 let start_tok = self.current;
12341 self.advance(); let function = if self.declared_function_names.contains(&self.peek().lexeme) {
12351 let s = self.peek().lexeme;
12352 self.advance();
12353 s
12354 } else {
12355 match &self.peek().kind {
12358 TokenType::Noun(sym) | TokenType::Adjective(sym) => {
12359 let s = *sym;
12360 self.advance();
12361 s
12362 }
12363 TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
12364 let s = self.peek().lexeme;
12366 self.advance();
12367 s
12368 }
12369 _ => {
12370 return Err(ParseError {
12371 kind: ParseErrorKind::ExpectedIdentifier,
12372 span: self.current_span(),
12373 });
12374 }
12375 }
12376 };
12377
12378 let args = if self.check_preposition_is("with") {
12380 self.advance(); self.parse_call_arguments()?
12382 } else {
12383 Vec::new()
12384 };
12385
12386 self.record_call_span(&args, start_tok);
12387 Ok(Stmt::Call { function, args })
12388 }
12389
12390 fn parse_call_arguments(&mut self) -> ParseResult<Vec<&'a Expr<'a>>> {
12391 let mut args = Vec::new();
12392
12393 let arg = self.parse_call_arg()?;
12395 args.push(arg);
12396
12397 while self.check(&TokenType::And) || self.check(&TokenType::Comma) {
12399 self.advance(); if self.check(&TokenType::RParen) {
12402 break;
12403 }
12404 let arg = self.parse_call_arg()?;
12405 args.push(arg);
12406 }
12407
12408 Ok(args)
12409 }
12410
12411 fn parse_call_arg(&mut self) -> ParseResult<&'a Expr<'a>> {
12412 if self.check(&TokenType::Give) {
12414 self.advance(); let value = self.parse_comparison()?;
12416 return Ok(self.ctx.alloc_imperative_expr(Expr::Give { value }));
12417 }
12418
12419 self.parse_comparison()
12422 }
12423
12424 #[inline(never)]
12432 fn parse_page_query_operand(&mut self) -> ParseResult<&'a Expr<'a>> {
12433 use crate::ast::{Expr, Literal};
12434 if self.check_word("the") && self.peek_word_at(1, "journal") {
12439 self.advance(); self.advance(); return Ok(self
12442 .ctx
12443 .alloc_imperative_expr(Expr::Dom(crate::ast::stmt::DomQuery::Journal)));
12444 }
12445 match self.peek().kind {
12446 TokenType::StringLiteral(sym) => {
12447 self.advance();
12448 Ok(self
12449 .ctx
12450 .alloc_imperative_expr(Expr::Literal(Literal::Text(sym))))
12451 }
12452 _ => {
12453 let name = self.expect_identifier()?;
12454 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(name));
12455 self.parse_field_access_chain(base)
12456 }
12457 }
12458 }
12459
12460 fn parse_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12461 let expr = self.parse_or_condition()?;
12464 if self.check(&TokenType::In) || self.check_preposition_is("in") {
12470 let zone_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
12473 Some(TokenType::StringLiteral(s)) => Some(*s),
12474 _ => None,
12475 };
12476 if let Some(zone_sym) = zone_sym {
12477 self.advance(); self.advance(); let zone = self
12480 .ctx
12481 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(zone_sym)));
12482 let func = self.interner.intern("in_zone");
12483 return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
12484 function: func,
12485 args: vec![expr, zone],
12486 }));
12487 }
12488 let unit_lexeme = self.tokens.get(self.current + 1).and_then(|t| {
12489 if matches!(
12490 t.kind,
12491 TokenType::Identifier | TokenType::Noun(_) | TokenType::Adjective(_)
12492 ) {
12493 Some(t.lexeme)
12494 } else {
12495 None
12496 }
12497 });
12498 if let Some(unit_lexeme) = unit_lexeme {
12499 if logicaffeine_base::quantity::units::by_name(self.interner.resolve(unit_lexeme))
12500 .is_some()
12501 {
12502 self.advance(); self.advance(); let unit_expr = self.ctx.alloc_imperative_expr(crate::ast::Expr::Literal(
12505 crate::ast::Literal::Text(unit_lexeme),
12506 ));
12507 let func = self.interner.intern("convert");
12508 return Ok(self.ctx.alloc_imperative_expr(crate::ast::Expr::Call {
12509 function: func,
12510 args: vec![expr, unit_expr],
12511 }));
12512 }
12513 }
12514 let code_lexeme = self.tokens.get(self.current + 1).and_then(|t| {
12518 if matches!(
12519 t.kind,
12520 TokenType::Identifier
12521 | TokenType::Noun(_)
12522 | TokenType::ProperName(_)
12523 | TokenType::Adjective(_)
12524 ) {
12525 Some(t.lexeme)
12526 } else {
12527 None
12528 }
12529 });
12530 if let Some(code_lexeme) = code_lexeme {
12531 if logicaffeine_base::money::currency::by_code(self.interner.resolve(code_lexeme))
12532 .is_some()
12533 {
12534 self.advance(); self.advance(); let code_expr = self.ctx.alloc_imperative_expr(crate::ast::Expr::Literal(
12537 crate::ast::Literal::Text(code_lexeme),
12538 ));
12539 let func = self.interner.intern("to_currency");
12540 return Ok(self.ctx.alloc_imperative_expr(crate::ast::Expr::Call {
12541 function: func,
12542 args: vec![expr, code_expr],
12543 }));
12544 }
12545 }
12546 }
12547 Ok(expr)
12548 }
12549
12550 fn parse_or_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12552 let mut left = self.parse_and_condition()?;
12553
12554 while self.check(&TokenType::Or) || self.check_word("or") {
12555 self.advance();
12556 let right = self.parse_and_condition()?;
12557 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12558 op: BinaryOpKind::Or,
12559 left,
12560 right,
12561 });
12562 }
12563
12564 Ok(left)
12565 }
12566
12567 fn parse_and_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12569 let mut left = self.parse_comparison()?;
12570
12571 while self.check(&TokenType::And) || self.check_word("and") {
12572 self.advance();
12573 let right = self.parse_comparison()?;
12574 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12575 op: BinaryOpKind::And,
12576 left,
12577 right,
12578 });
12579 }
12580
12581 Ok(left)
12582 }
12583
12584 fn in_introduces_conversion(&self) -> bool {
12589 match self.tokens.get(self.current + 1) {
12590 Some(t) => match &t.kind {
12591 TokenType::StringLiteral(_) => true,
12592 TokenType::Identifier
12593 | TokenType::Noun(_)
12594 | TokenType::ProperName(_)
12595 | TokenType::Adjective(_) => {
12596 if self.user_bound.contains(&t.lexeme) {
12601 return false;
12602 }
12603 let name = self.interner.resolve(t.lexeme);
12604 logicaffeine_base::quantity::units::by_name(name).is_some()
12605 || logicaffeine_base::money::currency::by_code(name).is_some()
12606 }
12607 _ => false,
12608 },
12609 None => false,
12610 }
12611 }
12612
12613 fn parse_comparison(&mut self) -> ParseResult<&'a Expr<'a>> {
12615 if self.check(&TokenType::Not) || self.check_word("not") {
12617 self.advance(); let operand = self.parse_comparison()?; return Ok(self.ctx.alloc_imperative_expr(Expr::Not { operand }));
12620 }
12621
12622 let left = self.parse_xor_expr()?;
12623
12624 if self.check(&TokenType::In) && !self.in_introduces_conversion() {
12634 self.advance(); let collection = self.parse_xor_expr()?;
12636 return Ok(self.ctx.alloc_imperative_expr(Expr::Contains {
12637 collection,
12638 value: left,
12639 }));
12640 }
12641 if (self.check(&TokenType::Not) || self.check_word("not"))
12642 && matches!(
12643 self.tokens.get(self.current + 1).map(|t| &t.kind),
12644 Some(TokenType::In)
12645 )
12646 {
12647 self.advance(); self.advance(); let collection = self.parse_xor_expr()?;
12650 let contains = self.ctx.alloc_imperative_expr(Expr::Contains {
12651 collection,
12652 value: left,
12653 });
12654 return Ok(self
12655 .ctx
12656 .alloc_imperative_expr(Expr::Not { operand: contains }));
12657 }
12658
12659 let op = if self.check(&TokenType::Equals) {
12661 self.advance();
12662 Some(BinaryOpKind::Eq)
12663 } else if self.check(&TokenType::Identity) {
12664 self.advance();
12666 Some(BinaryOpKind::Eq)
12667 } else if self.check_word("is") {
12668 let saved_pos = self.backtrack_point();
12670 self.advance(); if self.check_word("even") || self.check_word("odd") {
12677 let is_odd = self.check_word("odd");
12678 self.advance(); let two = self
12680 .ctx
12681 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(2)));
12682 let rem = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12683 op: BinaryOpKind::Modulo,
12684 left,
12685 right: two,
12686 });
12687 let want =
12688 self.ctx
12689 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(
12690 if is_odd { 1 } else { 0 },
12691 )));
12692 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12693 op: BinaryOpKind::Eq,
12694 left: rem,
12695 right: want,
12696 }));
12697 }
12698 if let Some(predicate) = self.try_parse_nonstandard_predicate(left)? {
12699 return Ok(predicate);
12700 }
12701 {
12713 let negated = self.check_word("not");
12714 let none_at = if negated {
12715 self.current + 1
12716 } else {
12717 self.current
12718 };
12719 let is_none_word = self.tokens.get(none_at).is_some_and(|t| {
12720 self.interner.resolve(t.lexeme).eq_ignore_ascii_case("none")
12721 && !self.user_bound.contains(&t.lexeme)
12722 });
12723 if is_none_word {
12724 if negated {
12725 self.advance(); }
12727 self.advance(); let none_lit = self.ctx.alloc_imperative_expr(Expr::OptionNone);
12729 let eq = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12730 op: BinaryOpKind::Eq,
12731 left,
12732 right: none_lit,
12733 });
12734 return Ok(if negated {
12735 self.ctx.alloc_imperative_expr(Expr::Not { operand: eq })
12736 } else {
12737 eq
12738 });
12739 }
12740 }
12741 if matches!(left, Expr::Literal(crate::ast::Literal::Text(_))) {
12752 let negated = self.check_word("not");
12753 let at = usize::from(negated);
12754 let state = if self.peek_word_at(at, "visible") {
12755 Some(0)
12756 } else if self.peek_word_at(at, "hidden") {
12757 Some(1)
12758 } else if self.peek_word_at(at, "disabled") {
12759 Some(2)
12760 } else if self.peek_word_at(at, "checked") {
12761 Some(3)
12762 } else if self.peek_word_at(at, "focused") {
12763 Some(4)
12764 } else {
12765 None
12766 };
12767 if let Some(state) = state {
12768 if negated {
12769 self.advance(); }
12771 self.advance(); self.check_selector_literal(left)?;
12776 use crate::ast::stmt::DomQuery;
12777 let query = match state {
12778 0 => DomQuery::Visible { selector: left },
12779 1 => DomQuery::Hidden { selector: left },
12780 2 => DomQuery::Disabled { selector: left },
12781 3 => DomQuery::Checked { selector: left },
12782 _ => DomQuery::Focused { selector: left },
12783 };
12784 let dom_expr = self.ctx.alloc_imperative_expr(Expr::Dom(query));
12785 return Ok(if negated {
12786 self.ctx
12787 .alloc_imperative_expr(Expr::Not { operand: dom_expr })
12788 } else {
12789 dom_expr
12790 });
12791 }
12792 }
12793 if self.check_word("divisible") {
12794 self.advance(); if self.check_word("by") || self.check_preposition_is("by") {
12796 self.advance(); let divisor = self.parse_xor_expr()?;
12798 let rem = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12799 op: BinaryOpKind::Modulo,
12800 left,
12801 right: divisor,
12802 });
12803 let zero = self
12804 .ctx
12805 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(0)));
12806 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12807 op: BinaryOpKind::Eq,
12808 left: rem,
12809 right: zero,
12810 }));
12811 }
12812 self.backtrack_to(&saved_pos);
12813 return Ok(left);
12814 }
12815 if self.check_word("between") {
12817 self.advance(); let lo = self.parse_xor_expr()?;
12819 if self.check_word("and") || self.check(&TokenType::And) {
12820 self.advance(); let hi = self.parse_xor_expr()?;
12822 let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12823 op: BinaryOpKind::LtEq,
12824 left: lo,
12825 right: left,
12826 });
12827 let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12828 op: BinaryOpKind::LtEq,
12829 left,
12830 right: hi,
12831 });
12832 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12833 op: BinaryOpKind::And,
12834 left: lower,
12835 right: upper,
12836 }));
12837 }
12838 self.backtrack_to(&saved_pos);
12839 return Ok(left);
12840 }
12841
12842 if self.check_word("greater") {
12843 self.advance(); if self.check_word("than") || self.check_preposition_is("than") {
12845 self.advance(); if self.consume_or_equal_to() {
12849 Some(BinaryOpKind::GtEq)
12850 } else {
12851 Some(BinaryOpKind::Gt)
12852 }
12853 } else {
12854 self.backtrack_to(&saved_pos);
12855 None
12856 }
12857 } else if self.check_word("less") {
12858 self.advance(); if self.check_word("than") || self.check_preposition_is("than") {
12860 self.advance(); if self.consume_or_equal_to() {
12863 Some(BinaryOpKind::LtEq)
12864 } else {
12865 Some(BinaryOpKind::Lt)
12866 }
12867 } else {
12868 self.backtrack_to(&saved_pos);
12869 None
12870 }
12871 } else if self.check_word("at") {
12872 self.advance(); if self.check_word("least") {
12874 self.advance(); Some(BinaryOpKind::GtEq)
12876 } else if self.check_word("most") {
12877 self.advance(); Some(BinaryOpKind::LtEq)
12879 } else {
12880 self.backtrack_to(&saved_pos);
12881 None
12882 }
12883 } else if self.check_word("before") {
12884 self.advance(); Some(BinaryOpKind::Lt)
12887 } else if self.check_word("after") {
12888 self.advance(); Some(BinaryOpKind::Gt)
12891 } else if self.check_word("not") || self.check(&TokenType::Not) {
12892 self.advance(); if self.check_word("equal") {
12900 let before_equal = self.backtrack_point();
12901 self.advance(); if self.check_preposition_is("to") || self.check_word("to") {
12903 self.advance(); } else {
12905 self.backtrack_to(&before_equal);
12907 }
12908 }
12909 Some(BinaryOpKind::NotEq)
12910 } else if self.check_word("equal") {
12911 self.advance(); if self.check_preposition_is("to") {
12914 self.advance(); Some(BinaryOpKind::Eq)
12916 } else {
12917 self.backtrack_to(&saved_pos);
12918 None
12919 }
12920 } else if self.check_word("approximately") {
12921 self.advance(); if self.check_word("equal") {
12926 self.advance(); if self.check_preposition_is("to") {
12928 self.advance(); }
12930 }
12931 let target_probe = self.backtrack_point();
12935 let target = match self.parse_xor_expr() {
12939 Ok(t) => Some(t),
12940 Err(_) => {
12941 self.backtrack_to(&target_probe);
12942 None
12943 }
12944 };
12945 if let Some(target) = target {
12946 if self.check_word("within") {
12947 self.advance(); let eps = self.parse_xor_expr()?;
12949 let lo = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12950 op: BinaryOpKind::Subtract,
12951 left: target,
12952 right: eps,
12953 });
12954 let hi = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12955 op: BinaryOpKind::Add,
12956 left: target,
12957 right: eps,
12958 });
12959 let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12960 op: BinaryOpKind::LtEq,
12961 left: lo,
12962 right: left,
12963 });
12964 let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12965 op: BinaryOpKind::LtEq,
12966 left,
12967 right: hi,
12968 });
12969 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12970 op: BinaryOpKind::And,
12971 left: lower,
12972 right: upper,
12973 }));
12974 }
12975 self.backtrack_to(&target_probe);
12978 }
12979 Some(BinaryOpKind::ApproxEq)
12980 } else if matches!(self.peek().kind, TokenType::Number(_))
12981 || (self.check(&TokenType::Minus)
12982 && self
12983 .tokens
12984 .get(self.current + 1)
12985 .is_some_and(|t| matches!(t.kind, TokenType::Number(_))))
12986 {
12987 Some(BinaryOpKind::Eq)
12993 } else if matches!(self.peek().kind, TokenType::StringLiteral(_)) {
12994 Some(BinaryOpKind::Eq)
12997 } else if matches!(
12998 self.peek().kind,
12999 TokenType::Identifier
13000 | TokenType::Noun(_)
13001 | TokenType::Adjective(_)
13002 | TokenType::ProperName(_)
13003 | TokenType::Verb { .. }
13004 | TokenType::Particle(_)
13005 | TokenType::Ambiguous { .. }
13006 | TokenType::Pronoun { .. }
13007 ) && (self.user_bound.contains(&self.peek().lexeme)
13008 || self
13009 .type_registry
13010 .as_ref()
13011 .is_some_and(|r| r.is_variant(self.peek().lexeme)))
13012 {
13013 Some(BinaryOpKind::Eq)
13022 } else {
13023 self.backtrack_to(&saved_pos);
13024 None
13025 }
13026 } else if self.check(&TokenType::Lt) {
13027 self.advance();
13028 Some(BinaryOpKind::Lt)
13029 } else if self.check(&TokenType::Gt) {
13030 self.advance();
13031 Some(BinaryOpKind::Gt)
13032 } else if self.check(&TokenType::LtEq) {
13033 self.advance();
13034 Some(BinaryOpKind::LtEq)
13035 } else if self.check(&TokenType::GtEq) {
13036 self.advance();
13037 Some(BinaryOpKind::GtEq)
13038 } else if self.check(&TokenType::EqEq) || self.check(&TokenType::Assign) {
13039 self.advance();
13040 Some(BinaryOpKind::Eq)
13041 } else if self.check(&TokenType::NotEq) {
13042 self.advance();
13043 Some(BinaryOpKind::NotEq)
13044 } else {
13045 None
13046 };
13047
13048 if let Some(op) = op {
13049 let right = self.parse_xor_expr()?;
13050 let first = self
13051 .ctx
13052 .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
13053 if let Some(op2) = self.try_symbolic_comparison_op() {
13057 let hi = self.parse_xor_expr()?;
13058 let second = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13059 op: op2,
13060 left: right,
13061 right: hi,
13062 });
13063 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13064 op: BinaryOpKind::And,
13065 left: first,
13066 right: second,
13067 }));
13068 }
13069 Ok(first)
13070 } else {
13071 Ok(left)
13072 }
13073 }
13074
13075 fn try_symbolic_comparison_op(&mut self) -> Option<BinaryOpKind> {
13079 let op = if self.check(&TokenType::LtEq) {
13080 BinaryOpKind::LtEq
13081 } else if self.check(&TokenType::GtEq) {
13082 BinaryOpKind::GtEq
13083 } else if self.check(&TokenType::Lt) {
13084 BinaryOpKind::Lt
13085 } else if self.check(&TokenType::Gt) {
13086 BinaryOpKind::Gt
13087 } else if self.check(&TokenType::EqEq) {
13088 BinaryOpKind::Eq
13089 } else if self.check(&TokenType::NotEq) {
13090 BinaryOpKind::NotEq
13091 } else {
13092 return None;
13093 };
13094 self.advance();
13095 Some(op)
13096 }
13097
13098 fn parse_destructuring_let(&mut self, mutable: bool) -> ParseResult<Stmt<'a>> {
13102 self.advance(); let mut binders = Vec::new();
13104 loop {
13105 binders.push(self.expect_identifier()?);
13106 if self.check(&TokenType::Comma) {
13107 self.advance(); continue;
13109 }
13110 break;
13111 }
13112 if !self.check(&TokenType::RParen) {
13113 return Err(ParseError {
13114 kind: ParseErrorKind::ExpectedKeyword {
13115 keyword: ")".to_string(),
13116 },
13117 span: self.current_span(),
13118 });
13119 }
13120 self.advance(); if !self.check(&TokenType::Be) && !self.check(&TokenType::Assign) {
13122 return Err(ParseError {
13123 kind: ParseErrorKind::ExpectedKeyword {
13124 keyword: "be or =".to_string(),
13125 },
13126 span: self.current_span(),
13127 });
13128 }
13129 self.advance(); let value = self.parse_imperative_expr()?;
13131
13132 let tmp = self.fresh_desugar_temp("destructure");
13133 let tmp_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(tmp));
13134 let mut body: Vec<Stmt<'a>> = Vec::with_capacity(binders.len() + 1);
13135 body.push(Stmt::Let {
13136 var: tmp,
13137 ty: None,
13138 value,
13139 mutable: false,
13140 });
13141 for (i, binder) in binders.iter().enumerate() {
13142 let idx = self
13143 .ctx
13144 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number((i + 1) as i64)));
13145 let read = self.ctx.alloc_imperative_expr(Expr::Index {
13146 collection: tmp_ref,
13147 index: idx,
13148 });
13149 body.push(Stmt::Let {
13150 var: *binder,
13151 ty: None,
13152 value: read,
13153 mutable,
13154 });
13155 }
13156 let body = self.alloc_block(body);
13157 Ok(Stmt::Splice { body })
13158 }
13159
13160 fn parse_let_statement(&mut self) -> ParseResult<Stmt<'a>> {
13161 self.advance(); let mutable = if self.check_mutable_keyword() {
13165 self.advance();
13166 true
13167 } else {
13168 false
13169 };
13170
13171 if self.check(&TokenType::LParen) {
13175 return self.parse_destructuring_let(mutable);
13176 }
13177
13178 let var = self.expect_identifier()?;
13180
13181 let ty = if self.check(&TokenType::Colon) {
13183 self.advance(); let type_expr = self.parse_type_expression()?;
13185 Some(self.ctx.alloc_type_expr(type_expr))
13186 } else {
13187 None
13188 };
13189
13190 if !self.check(&TokenType::Be) && !self.check(&TokenType::Assign) {
13192 return Err(ParseError {
13193 kind: ParseErrorKind::ExpectedKeyword {
13194 keyword: "be or =".to_string(),
13195 },
13196 span: self.current_span(),
13197 });
13198 }
13199 self.advance(); if let Some(stmt) = self.try_parse_tensor_new(var)? {
13206 return Ok(stmt);
13207 }
13208 if let Some(stmt) = self.try_parse_tensor_int_map(var)? {
13209 return Ok(stmt);
13210 }
13211 if let Some(stmt) = self.try_parse_tensor_binop(var)? {
13212 return Ok(stmt);
13213 }
13214 if let Some(stmt) = self.try_parse_tensor_head_slice(var)? {
13215 return Ok(stmt);
13216 }
13217 if let Some(stmt) = self.try_parse_tensor_unary(var)? {
13218 return Ok(stmt);
13219 }
13220 if let Some((binder, list, condition)) = self.try_parse_every_where()? {
13223 let (kept, mut body) = self.desugar_keep_where(binder, list, condition, true);
13224 let value = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
13225 body.push(Stmt::Let {
13226 var,
13227 ty,
13228 value,
13229 mutable,
13230 });
13231 let body = self.alloc_block(body);
13232 return Ok(Stmt::Splice { body });
13233 }
13234
13235 if self.check_word("mounted") {
13237 self.advance(); if !self.check(&TokenType::At) && !self.check_preposition_is("at") {
13239 return Err(ParseError {
13240 kind: ParseErrorKind::ExpectedKeyword {
13241 keyword: "at".to_string(),
13242 },
13243 span: self.current_span(),
13244 });
13245 }
13246 self.advance(); let path = self.parse_imperative_expr()?;
13248 return Ok(Stmt::Mount { var, path });
13249 }
13250
13251 if self.check_article() {
13253 let saved_pos = self.backtrack_point();
13254 self.advance(); if let TokenType::Noun(sym) | TokenType::ProperName(sym) = self.peek().kind {
13258 let word = self.interner.resolve(sym).to_lowercase();
13259 if word == "peeragent" {
13260 self.advance(); if self.check(&TokenType::At) || self.check_preposition_is("at") {
13264 self.advance(); let address = self.parse_imperative_expr()?;
13268
13269 return Ok(Stmt::LetPeerAgent { var, address });
13270 }
13271 }
13272 }
13273 self.backtrack_to(&saved_pos);
13275 }
13276
13277 if self.check_article() {
13281 let saved_pos = self.backtrack_point();
13282 self.advance(); if self.check(&TokenType::New) {
13284 self.advance(); }
13286
13287 if self.check(&TokenType::Pipe) {
13288 self.advance(); if !self.check_word("of") {
13292 return Err(ParseError {
13293 kind: ParseErrorKind::ExpectedKeyword {
13294 keyword: "of".to_string(),
13295 },
13296 span: self.current_span(),
13297 });
13298 }
13299 self.advance(); let element_type = self.expect_identifier()?;
13303
13304 return Ok(Stmt::CreatePipe {
13307 var,
13308 element_type,
13309 capacity: None,
13310 });
13311 }
13312 self.backtrack_to(&saved_pos);
13314 }
13315
13316 if self.check(&TokenType::Launch)
13320 && matches!(
13321 self.tokens.get(self.current + 1).map(|t| &t.kind),
13322 Some(TokenType::Article(_))
13323 )
13324 {
13325 self.advance(); if !self.check_article() {
13329 return Err(ParseError {
13330 kind: ParseErrorKind::ExpectedKeyword {
13331 keyword: "a".to_string(),
13332 },
13333 span: self.current_span(),
13334 });
13335 }
13336 self.advance();
13337
13338 if !self.check(&TokenType::Task) {
13340 return Err(ParseError {
13341 kind: ParseErrorKind::ExpectedKeyword {
13342 keyword: "task".to_string(),
13343 },
13344 span: self.current_span(),
13345 });
13346 }
13347 self.advance();
13348
13349 if !self.check(&TokenType::To) && !self.check_word("to") {
13351 return Err(ParseError {
13352 kind: ParseErrorKind::ExpectedKeyword {
13353 keyword: "to".to_string(),
13354 },
13355 span: self.current_span(),
13356 });
13357 }
13358 self.advance();
13359
13360 let function = self.parse_task_function()?;
13361
13362 let args = if self.check_word("with") {
13364 self.advance();
13365 self.parse_call_arguments()?
13366 } else {
13367 vec![]
13368 };
13369
13370 return Ok(Stmt::LaunchTaskWithHandle {
13371 handle: var,
13372 function,
13373 args,
13374 });
13375 }
13376
13377 let value_tok = self.current;
13378 let value = match self.try_parse_infinitesimal(var)? {
13379 Some(value) => value,
13380 None => self.parse_imperative_expr()?,
13381 };
13382
13383 let value = match &ty {
13386 Some(declared_ty) => {
13387 self.check_declared_type(declared_ty, value, self.span_since(value_tok))?;
13388 self.widen_literal_to_declared(declared_ty, value)
13389 }
13390 None => value,
13391 };
13392
13393 let value = if self.check_word("with") {
13395 let saved = self.backtrack_point();
13396 self.advance(); if self.check_word("capacity") {
13398 self.advance(); let cap_expr = self.parse_imperative_expr()?;
13400 self.ctx.alloc_imperative_expr(Expr::WithCapacity {
13401 value,
13402 capacity: cap_expr,
13403 })
13404 } else {
13405 self.backtrack_to(&saved); value
13407 }
13408 } else {
13409 value
13410 };
13411
13412 self.world_state.drs.introduce_referent(
13414 var,
13415 var,
13416 crate::drs::Gender::Unknown,
13417 crate::drs::Number::Singular,
13418 );
13419
13420 self.user_bound.insert(var);
13421 Ok(Stmt::Let {
13422 var,
13423 ty,
13424 value,
13425 mutable,
13426 })
13427 }
13428
13429 fn check_mutable_keyword(&self) -> bool {
13430 if matches!(self.peek().kind, TokenType::Mut) {
13432 return true;
13433 }
13434 if let TokenType::Noun(sym) | TokenType::Adjective(sym) = self.peek().kind {
13436 let word = self.interner.resolve(sym).to_lowercase();
13437 word == "mutable" || word == "mut"
13438 } else {
13439 false
13440 }
13441 }
13442
13443 pub(super) fn widen_literal_to_declared(
13459 &self,
13460 declared: &TypeExpr<'_>,
13461 value: &'a Expr<'a>,
13462 ) -> &'a Expr<'a> {
13463 let (TypeExpr::Primitive(sym) | TypeExpr::Named(sym)) = declared else {
13464 return value;
13465 };
13466 let name = self.interner.resolve(*sym);
13467 if !name.eq_ignore_ascii_case("Float") && !name.eq_ignore_ascii_case("Real") {
13468 return value;
13469 }
13470 match value {
13471 Expr::Literal(Literal::Number(n)) => self
13472 .ctx
13473 .alloc_imperative_expr(Expr::Literal(Literal::Float(*n as f64))),
13474 _ => value,
13475 }
13476 }
13477
13478 pub(super) fn check_declared_type(
13481 &self,
13482 declared: &TypeExpr<'_>,
13483 value: &Expr<'_>,
13484 value_span: Span,
13485 ) -> ParseResult<()> {
13486 let Some(inferred) = self.infer_literal_type(value) else {
13487 return Ok(());
13488 };
13489 if self.check_type_compatibility(declared, inferred) {
13490 return Ok(());
13491 }
13492 let expected = match declared {
13493 TypeExpr::Primitive(sym) | TypeExpr::Named(sym) => {
13494 self.interner.resolve(*sym).to_string()
13495 }
13496 _ => "unknown".to_string(),
13497 };
13498 Err(ParseError {
13499 kind: ParseErrorKind::TypeMismatch {
13500 expected,
13501 found: inferred.to_string(),
13502 },
13503 span: value_span,
13504 })
13505 }
13506
13507 fn infer_literal_type(&self, expr: &Expr<'_>) -> Option<&'static str> {
13508 match expr {
13509 Expr::Literal(lit) => match lit {
13510 crate::ast::Literal::Number(_) => Some("Int"),
13511 crate::ast::Literal::Float(_) => Some("Real"),
13512 crate::ast::Literal::Text(_) => Some("Text"),
13513 crate::ast::Literal::Boolean(_) => Some("Bool"),
13514 crate::ast::Literal::Nothing => Some("Unit"),
13515 crate::ast::Literal::Char(_) => Some("Char"),
13516 crate::ast::Literal::Duration(_) => Some("Duration"),
13517 crate::ast::Literal::Date(_) => Some("Date"),
13518 crate::ast::Literal::Moment(_) => Some("Moment"),
13519 crate::ast::Literal::Span { .. } => Some("Span"),
13520 crate::ast::Literal::Time(_) => Some("Time"),
13521 },
13522 _ => None, }
13524 }
13525
13526 fn check_type_compatibility(&self, declared: &TypeExpr<'_>, inferred: &str) -> bool {
13528 match declared {
13529 TypeExpr::Primitive(sym) | TypeExpr::Named(sym) => {
13530 let declared_name = self.interner.resolve(*sym);
13531 declared_name.eq_ignore_ascii_case(inferred)
13535 || (declared_name.eq_ignore_ascii_case("Nat") && inferred == "Int")
13536 || (declared_name.eq_ignore_ascii_case("Byte") && inferred == "Int")
13537 || (declared_name.eq_ignore_ascii_case("Rational") && inferred == "Int")
13538 || (declared_name.eq_ignore_ascii_case("Float") && inferred == "Real")
13539 || (declared_name.eq_ignore_ascii_case("Float") && inferred == "Int")
13545 || (declared_name.eq_ignore_ascii_case("Real") && inferred == "Int")
13546 || (inferred == "Int"
13550 && ["Algebraic", "ExactReal", "Hyperreal", "Hypernatural", "Ordinal"]
13551 .iter()
13552 .any(|t| declared_name.eq_ignore_ascii_case(t)))
13553 }
13554 _ => true, }
13556 }
13557
13558 fn peek_compound_assignment(&self) -> bool {
13569 let is_identifier = matches!(
13570 self.peek().kind,
13571 TokenType::Noun(_)
13572 | TokenType::ProperName(_)
13573 | TokenType::Identifier
13574 | TokenType::Adjective(_)
13575 | TokenType::Verb { .. }
13576 | TokenType::Particle(_)
13577 | TokenType::Ambiguous { .. }
13578 | TokenType::Pronoun { .. }
13579 );
13580 if !is_identifier {
13581 return false;
13582 }
13583 let mut offset = 1;
13584 while self.current + offset < self.tokens.len() {
13585 match &self.tokens[self.current + offset].kind {
13586 TokenType::PlusEq
13587 | TokenType::MinusEq
13588 | TokenType::StarEq
13589 | TokenType::SlashEq
13590 | TokenType::PercentEq => return true,
13591 TokenType::Period | TokenType::Newline | TokenType::EOF => return false,
13592 _ => offset += 1,
13593 }
13594 }
13595 false
13596 }
13597
13598 fn parse_compound_assignment(&mut self) -> ParseResult<Stmt<'a>> {
13602 use crate::ast::Expr;
13603 let target_from = self.current;
13604 let target = self.parse_imperative_expr()?;
13605 self.note_write(
13606 target,
13607 target_from,
13608 self.current,
13609 component::WriteVerb::Compound,
13610 );
13611 let op = match self.peek().kind {
13612 TokenType::PlusEq => BinaryOpKind::Add,
13613 TokenType::MinusEq => BinaryOpKind::Subtract,
13614 TokenType::StarEq => BinaryOpKind::Multiply,
13615 TokenType::SlashEq => BinaryOpKind::Divide,
13616 TokenType::PercentEq => BinaryOpKind::Modulo,
13617 _ => {
13618 return Err(ParseError {
13619 kind: ParseErrorKind::ExpectedKeyword {
13620 keyword: "+= -= *= /= %=".to_string(),
13621 },
13622 span: self.current_span(),
13623 });
13624 }
13625 };
13626 self.advance(); let rhs = self.parse_imperative_expr()?;
13628 let combined = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13629 op,
13630 left: target,
13631 right: rhs,
13632 });
13633 match target {
13636 Expr::Identifier(sym) => Ok(Stmt::Set {
13637 target: *sym,
13638 value: combined,
13639 }),
13640 Expr::FieldAccess { object, field } => Ok(Stmt::SetField {
13641 object,
13642 field: *field,
13643 value: combined,
13644 }),
13645 Expr::Index { collection, index } => {
13646 Ok(self.desugar_place_set_index(collection, index, combined))
13647 }
13648 _ => Err(ParseError {
13649 kind: ParseErrorKind::ExpectedIdentifier,
13650 span: self.current_span(),
13651 }),
13652 }
13653 }
13654
13655 fn peek_equals_assignment(&self) -> bool {
13656 let is_identifier = matches!(
13660 self.peek().kind,
13661 TokenType::Noun(_)
13662 | TokenType::ProperName(_)
13663 | TokenType::Identifier
13664 | TokenType::Adjective(_)
13665 | TokenType::Verb { .. }
13666 | TokenType::Particle(_)
13667 | TokenType::Ambiguous { .. }
13668 | TokenType::Pronoun { .. }
13669 );
13670 if !is_identifier {
13671 return false;
13672 }
13673
13674 if self.current + 1 >= self.tokens.len() {
13676 return false;
13677 }
13678
13679 let next = &self.tokens[self.current + 1].kind;
13680
13681 if matches!(next, TokenType::Assign) {
13683 return true;
13684 }
13685
13686 if matches!(next, TokenType::Colon) {
13689 let mut offset = 2;
13690 while self.current + offset < self.tokens.len() {
13691 let tok = &self.tokens[self.current + offset].kind;
13692 if matches!(tok, TokenType::Assign) {
13693 return true;
13694 }
13695 if matches!(tok, TokenType::Period | TokenType::Newline | TokenType::EOF) {
13696 return false;
13697 }
13698 offset += 1;
13699 }
13700 }
13701
13702 false
13703 }
13704
13705 fn parse_equals_assignment(&mut self, explicit_mutable: bool) -> ParseResult<Stmt<'a>> {
13707 if explicit_mutable {
13709 self.advance(); }
13711
13712 let var = self.expect_identifier()?;
13714
13715 let ty = if self.check(&TokenType::Colon) {
13717 self.advance(); let type_expr = self.parse_type_expression()?;
13719 Some(self.ctx.alloc_type_expr(type_expr))
13720 } else {
13721 None
13722 };
13723
13724 if !self.check(&TokenType::Assign) {
13726 return Err(ParseError {
13727 kind: ParseErrorKind::ExpectedKeyword {
13728 keyword: "=".to_string(),
13729 },
13730 span: self.current_span(),
13731 });
13732 }
13733 self.advance(); let value = self.parse_imperative_expr()?;
13737
13738 let value = if self.check_word("with") {
13740 let saved = self.backtrack_point();
13741 self.advance(); if self.check_word("capacity") {
13743 self.advance(); let cap_expr = self.parse_imperative_expr()?;
13745 self.ctx.alloc_imperative_expr(Expr::WithCapacity {
13746 value,
13747 capacity: cap_expr,
13748 })
13749 } else {
13750 self.backtrack_to(&saved); value
13752 }
13753 } else {
13754 value
13755 };
13756
13757 if !explicit_mutable && ty.is_none() && self.user_bound.contains(&var) {
13763 return Ok(Stmt::Set { target: var, value });
13764 }
13765
13766 self.world_state.drs.introduce_referent(
13768 var,
13769 var,
13770 crate::drs::Gender::Unknown,
13771 crate::drs::Number::Singular,
13772 );
13773
13774 self.user_bound.insert(var);
13775 Ok(Stmt::Let {
13776 var,
13777 ty,
13778 value,
13779 mutable: explicit_mutable,
13780 })
13781 }
13782
13783 fn parse_set_statement(&mut self) -> ParseResult<Stmt<'a>> {
13784 use crate::ast::Expr;
13785 self.advance(); self.eat_article_before_possessive();
13792
13793 if let Some(stmt) = self.try_parse_set_row()? {
13797 return Ok(stmt);
13798 }
13799
13800 if self.check_word("the") && self.peek_word_at(1, "text") && self.peek_word_at(2, "of") {
13802 self.advance(); self.advance(); self.advance(); let element = self.parse_imperative_expr()?;
13806 let is_to = self.check(&TokenType::To)
13807 || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
13808 if !is_to {
13809 return Err(ParseError {
13810 kind: ParseErrorKind::ExpectedKeyword {
13811 keyword: "to".to_string(),
13812 },
13813 span: self.current_span(),
13814 });
13815 }
13816 self.advance(); let value = self.parse_imperative_expr()?;
13818 let function = self.interner.intern("setText");
13819 return Ok(Stmt::Call {
13820 function,
13821 args: vec![element, value],
13822 });
13823 }
13824
13825 let target_from = self.current;
13827 let target_expr = self.parse_imperative_expr()?;
13828 let target_to = self.current;
13829
13830 let target_expr = if self.check(&TokenType::At) {
13832 self.advance(); let key = self.parse_imperative_expr()?;
13834 self.ctx.alloc_imperative_expr(Expr::Index {
13835 collection: target_expr,
13836 index: key,
13837 })
13838 } else {
13839 target_expr
13840 };
13841
13842 let is_to = self.check(&TokenType::To)
13844 || matches!(
13845 &self.peek().kind,
13846 TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to"
13847 );
13848 if !is_to {
13849 return Err(ParseError {
13850 kind: ParseErrorKind::ExpectedKeyword {
13851 keyword: "to".to_string(),
13852 },
13853 span: self.current_span(),
13854 });
13855 }
13856 self.advance(); let (gathered, value) = match self.try_parse_every_where()? {
13861 Some((binder, list, condition)) => {
13862 let (kept, body) = self.desugar_keep_where(binder, list, condition, true);
13863 (body, self.ctx.alloc_imperative_expr(Expr::Identifier(kept)))
13864 }
13865 None => (Vec::new(), self.parse_imperative_expr()?),
13866 };
13867
13868 let assign = match target_expr {
13871 Expr::FieldAccess { object, field } => {
13872 self.note_write(
13873 target_expr,
13874 target_from,
13875 target_to,
13876 component::WriteVerb::SetField,
13877 );
13878 Stmt::SetField {
13879 object: *object,
13880 field: *field,
13881 value,
13882 }
13883 }
13884 Expr::Identifier(target) => {
13885 self.note_write(
13886 target_expr,
13887 target_from,
13888 target_to,
13889 component::WriteVerb::Set,
13890 );
13891 Stmt::Set {
13892 target: *target,
13893 value,
13894 }
13895 }
13896 Expr::Index { collection, index } => {
13897 self.note_write(
13898 target_expr,
13899 target_from,
13900 target_to,
13901 component::WriteVerb::SetItem,
13902 );
13903 self.desugar_place_set_index(*collection, *index, value)
13904 }
13905 _ => {
13906 return Err(ParseError {
13907 kind: ParseErrorKind::ExpectedIdentifier,
13908 span: self.current_span(),
13909 })
13910 }
13911 };
13912 if gathered.is_empty() {
13913 return Ok(assign);
13914 }
13915 let mut body = gathered;
13916 body.push(assign);
13917 let body = self.alloc_block(body);
13918 Ok(Stmt::Splice { body })
13919 }
13920
13921 fn parse_return_statement(&mut self) -> ParseResult<Stmt<'a>> {
13922 self.advance(); if self.check(&TokenType::Period) || self.is_at_end() {
13926 return Ok(Stmt::Return { value: None });
13927 }
13928
13929 let value = self.parse_condition()?;
13933 if self.check(&TokenType::If) {
13935 self.advance(); let cond = self.parse_condition()?;
13937 let ret = Stmt::Return { value: Some(value) };
13938 let then_block = self.alloc_block(vec![ret]);
13939 return Ok(Stmt::If {
13940 cond,
13941 then_block,
13942 else_block: None,
13943 });
13944 }
13945 Ok(Stmt::Return { value: Some(value) })
13946 }
13947
13948 fn parse_break_statement(&mut self) -> ParseResult<Stmt<'a>> {
13949 self.advance(); Ok(Stmt::Break)
13951 }
13952
13953 fn parse_assert_statement(&mut self) -> ParseResult<Stmt<'a>> {
13954 self.advance(); if self.check(&TokenType::That)
13958 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
13959 {
13960 self.advance();
13961 }
13962
13963 let condition = self.parse_condition()?;
13966 let reason = self.parse_contract_reason()?;
13967
13968 Ok(Stmt::RuntimeAssert {
13969 condition,
13970 hard: false,
13971 dim: None,
13972 reason,
13973 })
13974 }
13975
13976 fn parse_contract_reason(&mut self) -> ParseResult<Option<&'a Expr<'a>>> {
13979 if self.check(&TokenType::Comma)
13980 && matches!(
13981 self.tokens.get(self.current + 1).map(|t| &t.kind),
13982 Some(TokenType::Otherwise)
13983 )
13984 {
13985 self.advance();
13986 self.advance();
13987 return Ok(Some(self.parse_imperative_expr()?));
13988 }
13989 Ok(None)
13990 }
13991
13992 fn parse_fail_statement(&mut self) -> ParseResult<Stmt<'a>> {
13994 self.advance(); self.advance(); let reason = self.parse_imperative_expr()?;
13997 Ok(Stmt::Fail { reason })
13998 }
13999
14000 fn parse_require_statement(&mut self) -> ParseResult<Stmt<'a>> {
14004 self.advance(); if self.check(&TokenType::That)
14008 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14009 {
14010 self.advance();
14011 }
14012
14013 let condition = self.parse_condition()?;
14014 let (condition, dim) = self.fold_contract_dimension(condition)?;
14015 let reason = self.parse_contract_reason()?;
14016 Ok(Stmt::RuntimeAssert {
14017 condition,
14018 hard: true,
14019 dim,
14020 reason,
14021 })
14022 }
14023
14024 fn parse_assume_statement(&mut self) -> ParseResult<Stmt<'a>> {
14029 self.advance(); if self.check(&TokenType::That)
14033 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14034 {
14035 self.advance();
14036 }
14037
14038 let condition = self.parse_condition()?;
14039 let (condition, dim) = self.fold_contract_dimension(condition)?;
14040 let reason = self.parse_contract_reason()?;
14041 Ok(Stmt::RuntimeAssert {
14042 condition,
14043 hard: true,
14044 dim,
14045 reason,
14046 })
14047 }
14048
14049 fn parse_solve_statement(&mut self) -> ParseResult<Stmt<'a>> {
14055 self.advance(); let err = |msg: &str| ParseError {
14057 kind: ParseErrorKind::Custom(msg.to_string()),
14058 span: Span::default(),
14059 };
14060 let lhs = self.parse_xor_expr()?;
14061 if !self.check_word("equals") {
14062 return Err(err(
14063 "a Solve statement reads `Solve M times x equals v for x.` — I expected `equals` after the product",
14064 ));
14065 }
14066 self.advance(); let rhs = self.parse_xor_expr()?;
14068 if !self.check_word("for") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14069 return Err(err("a Solve statement ends `… for x.` — name the unknown"));
14070 }
14071 self.advance(); let binder = match self.peek().kind {
14073 TokenType::Identifier | TokenType::Noun(_) => {
14074 let sym = self.peek().lexeme;
14075 self.advance();
14076 sym
14077 }
14078 _ => return Err(err("a Solve statement ends `… for x.` — name the unknown")),
14079 };
14080 let Expr::BinaryOp {
14082 op: BinaryOpKind::Multiply,
14083 left: matrix,
14084 right: unknown,
14085 } = lhs
14086 else {
14087 return Err(err(
14088 "a Solve statement reads `Solve M times x equals v for x.` — the left side must be the product `M times x`",
14089 ));
14090 };
14091 let Expr::Identifier(x) = unknown else {
14092 return Err(err("the unknown in `M times x` must be a plain name"));
14093 };
14094 if *x != binder {
14095 return Err(err(
14096 "the unknown in `M times x` and the `for` binder must be the SAME name",
14097 ));
14098 }
14099 let func = self.interner.intern("solveLinear");
14100 let call = self.ctx.alloc_imperative_expr(Expr::Call {
14101 function: func,
14102 args: vec![matrix, rhs],
14103 });
14104 Ok(Stmt::Let {
14105 var: binder,
14106 ty: None,
14107 value: call,
14108 mutable: false,
14109 })
14110 }
14111
14112 fn parse_minimize_statement(&mut self) -> ParseResult<Stmt<'a>> {
14117 self.advance(); let err = |msg: &str| ParseError {
14119 kind: ParseErrorKind::Custom(msg.to_string()),
14120 span: Span::default(),
14121 };
14122 if matches!(self.peek().kind, TokenType::Article(_)) {
14123 self.advance(); }
14125 if !self.check_word("quadratic") {
14126 return Err(err(
14127 "a Minimize statement reads `Minimize the quadratic Q with linear b over x.`",
14128 ));
14129 }
14130 self.advance(); let q = self.parse_xor_expr()?;
14132 if !self.check_word("with") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14133 return Err(err("I expected `with linear b` after the quadratic"));
14134 }
14135 self.advance(); if !self.check_word("linear") {
14137 return Err(err("I expected `with linear b` after the quadratic"));
14138 }
14139 self.advance(); let b = self.parse_xor_expr()?;
14141 if !self.check_word("over") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14142 return Err(err(
14143 "a Minimize statement ends `… over x.` — name the unknown",
14144 ));
14145 }
14146 self.advance(); let binder = match self.peek().kind {
14148 TokenType::Identifier | TokenType::Noun(_) => {
14149 let sym = self.peek().lexeme;
14150 self.advance();
14151 sym
14152 }
14153 _ => {
14154 return Err(err(
14155 "a Minimize statement ends `… over x.` — name the unknown",
14156 ))
14157 }
14158 };
14159 let func = self.interner.intern("minimizeQuadratic");
14160 let call = self.ctx.alloc_imperative_expr(Expr::Call {
14161 function: func,
14162 args: vec![q, b],
14163 });
14164 Ok(Stmt::Let {
14165 var: binder,
14166 ty: None,
14167 value: call,
14168 mutable: false,
14169 })
14170 }
14171
14172 fn fold_contract_dimension(
14186 &mut self,
14187 condition: &'a Expr<'a>,
14188 ) -> ParseResult<(
14189 &'a Expr<'a>,
14190 Option<logicaffeine_base::dimension::Dimension>,
14191 )> {
14192 use logicaffeine_base::numeric::{rational_from_f64_exact, Decimal, Rational};
14193 use logicaffeine_base::quantity::{units, Unit};
14194
14195 enum CBound {
14196 Bare(Rational),
14197 Stated(Rational, Unit, String),
14198 Opaque,
14199 }
14200
14201 let Expr::BinaryOp {
14202 op: BinaryOpKind::And,
14203 left: lower,
14204 right: upper,
14205 } = condition
14206 else {
14207 return Ok((condition, None));
14208 };
14209 let Expr::BinaryOp {
14210 op: BinaryOpKind::LtEq,
14211 left: lo,
14212 right: v1,
14213 } = lower
14214 else {
14215 return Ok((condition, None));
14216 };
14217 let Expr::BinaryOp {
14218 op: BinaryOpKind::LtEq,
14219 left: v2,
14220 right: hi,
14221 } = upper
14222 else {
14223 return Ok((condition, None));
14224 };
14225 let (Expr::Identifier(a), Expr::Identifier(b)) = (v1, v2) else {
14226 return Ok((condition, None));
14227 };
14228 if a != b {
14229 return Ok((condition, None));
14230 }
14231
14232 let classify = |parser: &Self, e: &Expr<'a>| -> CBound {
14233 let (neg, e) = match e {
14235 Expr::BinaryOp {
14236 op: BinaryOpKind::Subtract,
14237 left,
14238 right,
14239 } => match left {
14240 Expr::Literal(Literal::Number(0)) => (true, *right),
14241 Expr::Literal(Literal::Float(z)) if *z == 0.0 => (true, *right),
14242 _ => (false, e),
14243 },
14244 _ => (false, e),
14245 };
14246 let signed = |r: Rational| if neg { r.negated() } else { r };
14247 match e {
14248 Expr::Literal(Literal::Number(n)) => CBound::Bare(signed(Rational::from_i64(*n))),
14249 Expr::Literal(Literal::Float(f)) => match rational_from_f64_exact(*f) {
14250 Some(r) => CBound::Bare(signed(r)),
14251 None => CBound::Opaque,
14252 },
14253 Expr::Call { function, args }
14254 if parser.interner.resolve(*function) == "quantity" && args.len() == 2 =>
14255 {
14256 let Expr::Literal(Literal::Text(unit_sym)) = args[1] else {
14257 return CBound::Opaque;
14258 };
14259 let word = parser.interner.resolve(*unit_sym).to_string();
14260 let Some(unit) = units::by_name(&word) else {
14261 return CBound::Opaque;
14262 };
14263 let mag = match args[0] {
14264 Expr::Literal(Literal::Number(n)) => Some(Rational::from_i64(*n)),
14265 Expr::Call {
14266 function: df,
14267 args: dargs,
14268 } if parser.interner.resolve(*df) == "decimal" && dargs.len() == 1 => {
14269 match dargs[0] {
14270 Expr::Literal(Literal::Text(t)) => {
14271 Decimal::parse(parser.interner.resolve(*t))
14272 .map(|d| d.to_rational())
14273 }
14274 _ => None,
14275 }
14276 }
14277 _ => None,
14278 };
14279 match mag {
14280 Some(m) => CBound::Stated(signed(m), unit, word),
14281 None => CBound::Opaque,
14282 }
14283 }
14284 _ => CBound::Opaque,
14285 }
14286 };
14287
14288 let (blo, bhi) = (classify(self, lo), classify(self, hi));
14289 if !matches!(blo, CBound::Stated(..)) && !matches!(bhi, CBound::Stated(..)) {
14291 return Ok((condition, None));
14292 }
14293 let err = |msg: String| ParseError {
14294 kind: ParseErrorKind::Custom(msg),
14295 span: Span::default(),
14296 };
14297 let dim_err = |wl: &str, wh: &str| {
14298 err(format!(
14299 "this contract bounds one variable with `{wl}` and `{wh}` — two different \
14300 dimensions in one range is analytically meaningless"
14301 ))
14302 };
14303 let (si_lo, si_hi, unit) = match (blo, bhi) {
14304 (CBound::Stated(ml, ul, wl), CBound::Stated(mh, uh, wh)) => {
14305 if ul.dimension != uh.dimension {
14306 return Err(dim_err(&wl, &wh));
14307 }
14308 let l = ml.mul(&ul.scale).add(&ul.offset);
14309 let h = mh.mul(&uh.scale).add(&uh.offset);
14310 (l, h, ul)
14311 }
14312 (CBound::Bare(ml), CBound::Stated(mh, uh, _)) => {
14314 let l = ml.mul(&uh.scale).add(&uh.offset);
14315 let h = mh.mul(&uh.scale).add(&uh.offset);
14316 (l, h, uh)
14317 }
14318 (CBound::Stated(ml, ul, _), CBound::Bare(mh)) => {
14319 let l = ml.mul(&ul.scale).add(&ul.offset);
14320 let h = mh.mul(&ul.scale).add(&ul.offset);
14321 (l, h, ul)
14322 }
14323 (CBound::Opaque, _) | (_, CBound::Opaque) => {
14324 return Err(err(
14325 "a dimensioned range contract needs literal bounds — the unit fold cannot \
14326 preserve a computed bound"
14327 .to_string(),
14328 ));
14329 }
14330 (CBound::Bare(_), CBound::Bare(_)) => unreachable!("guarded above"),
14331 };
14332 let var = self.ctx.alloc_imperative_expr(Expr::Identifier(*a));
14333 let var2 = self.ctx.alloc_imperative_expr(Expr::Identifier(*a));
14334 let flo = self
14335 .ctx
14336 .alloc_imperative_expr(Expr::Literal(Literal::Float(si_lo.to_f64())));
14337 let fhi = self
14338 .ctx
14339 .alloc_imperative_expr(Expr::Literal(Literal::Float(si_hi.to_f64())));
14340 let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14341 op: BinaryOpKind::LtEq,
14342 left: flo,
14343 right: var,
14344 });
14345 let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14346 op: BinaryOpKind::LtEq,
14347 left: var2,
14348 right: fhi,
14349 });
14350 let folded = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14351 op: BinaryOpKind::And,
14352 left: lower,
14353 right: upper,
14354 });
14355 Ok((folded, Some(unit.dimension)))
14356 }
14357
14358 fn parse_trust_statement(&mut self) -> ParseResult<Stmt<'a>> {
14361 self.advance(); if self.check(&TokenType::That)
14365 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14366 {
14367 self.advance();
14368 }
14369
14370 let saved_mode = self.mode;
14372 self.mode = ParserMode::Declarative;
14373
14374 let proposition = self.parse_prefix()?;
14378
14379 self.mode = saved_mode;
14381
14382 if !self.check(&TokenType::Because) {
14384 return Err(ParseError {
14385 kind: ParseErrorKind::UnexpectedToken {
14386 expected: TokenType::Because,
14387 found: self.peek().kind.clone(),
14388 },
14389 span: self.current_span(),
14390 });
14391 }
14392 self.advance(); let justification = match &self.peek().kind {
14396 TokenType::StringLiteral(sym) => {
14397 let s = *sym;
14398 self.advance();
14399 s
14400 }
14401 _ => {
14402 return Err(ParseError {
14403 kind: ParseErrorKind::UnexpectedToken {
14404 expected: TokenType::StringLiteral(self.interner.intern("")),
14405 found: self.peek().kind.clone(),
14406 },
14407 span: self.current_span(),
14408 });
14409 }
14410 };
14411
14412 Ok(Stmt::Trust {
14413 proposition,
14414 justification,
14415 })
14416 }
14417
14418 fn peek_lexeme_lower(&self, k: usize) -> Option<String> {
14421 self.tokens
14422 .get(self.current + k)
14423 .map(|t| self.interner.resolve(t.lexeme).to_lowercase())
14424 }
14425
14426 fn parse_test_block(&mut self, name: Symbol) -> ParseResult<Stmt<'a>> {
14438 use crate::ast::stmt::TestCapabilities;
14439
14440 let name_str = self.interner.resolve(name).to_string();
14441 if name_str.is_empty() {
14442 return Err(ParseError {
14443 kind: ParseErrorKind::Custom(
14444 "a test needs a prose name: `## Test: <what this test shows>`".to_string(),
14445 ),
14446 span: self.current_span(),
14447 });
14448 }
14449 if !self.test_names.insert(name_str.clone()) {
14450 return Err(ParseError {
14451 kind: ParseErrorKind::Custom(format!(
14452 "duplicate test name \"{name_str}\" — a test's prose name is its identity; \
14453 give each ## Test its own sentence"
14454 )),
14455 span: self.current_span(),
14456 });
14457 }
14458
14459 let mut skip: Option<Symbol> = None;
14461 let mut capabilities = TestCapabilities::default();
14462 let mut budget: Option<u64> = None;
14463 loop {
14464 if self.check(&TokenType::Newline) || self.check(&TokenType::Indent) {
14465 self.advance();
14466 continue;
14467 }
14468 if self.check(&TokenType::Skip) {
14470 self.advance(); while self
14473 .peek_lexeme_lower(0)
14474 .is_some_and(|w| w == "this" || w == "test")
14475 {
14476 self.advance();
14477 }
14478 if !self.check(&TokenType::Because)
14479 && self.peek_lexeme_lower(0).as_deref() != Some("because")
14480 {
14481 return Err(ParseError {
14482 kind: ParseErrorKind::Custom(
14483 "a skip needs its reason: `Skip this test because \"<why>\".` \
14484 — an unexplained skip is a silent debt"
14485 .to_string(),
14486 ),
14487 span: self.current_span(),
14488 });
14489 }
14490 self.advance(); let TokenType::StringLiteral(reason) = self.peek().kind else {
14492 return Err(ParseError {
14493 kind: ParseErrorKind::Custom(
14494 "the skip reason is a quoted sentence: \
14495 `Skip this test because \"the flag has not shipped\".`"
14496 .to_string(),
14497 ),
14498 span: self.current_span(),
14499 });
14500 };
14501 self.advance();
14502 if self.check(&TokenType::Period) {
14503 self.advance();
14504 }
14505 skip = Some(reason);
14506 continue;
14507 }
14508 if self.peek_lexeme_lower(0).as_deref() == Some("this")
14510 && self.peek_lexeme_lower(1).as_deref() == Some("test")
14511 && self.peek_lexeme_lower(2).as_deref() == Some("may")
14512 {
14513 self.advance(); self.advance(); self.advance(); match self.peek_lexeme_lower(0).as_deref() {
14517 Some("use") => {
14518 self.advance(); while self
14521 .peek_lexeme_lower(0)
14522 .is_some_and(|w| w == "the" || w == "real")
14523 {
14524 self.advance();
14525 }
14526 match self.peek_lexeme_lower(0).as_deref() {
14527 Some("filesystem") => capabilities.filesystem = true,
14528 Some("network") => capabilities.network = true,
14529 Some("environment") => capabilities.environment = true,
14530 Some("clock") => capabilities.clock = true,
14531 other => {
14532 return Err(ParseError {
14533 kind: ParseErrorKind::Custom(format!(
14534 "unknown capability {:?} — a test may use the real \
14535 filesystem, network, environment, or clock",
14536 other.unwrap_or_default()
14537 )),
14538 span: self.current_span(),
14539 });
14540 }
14541 }
14542 self.advance(); }
14544 Some("run") => {
14545 self.advance(); while self
14548 .peek_lexeme_lower(0)
14549 .is_some_and(|w| w == "up" || w == "to")
14550 {
14551 self.advance();
14552 }
14553 let lexeme = self
14554 .peek_lexeme_lower(0)
14555 .unwrap_or_default()
14556 .replace('_', "");
14557 let Ok(steps) = lexeme.parse::<u64>() else {
14558 return Err(ParseError {
14559 kind: ParseErrorKind::Custom(
14560 "the op budget is a number: \
14561 `This test may run up to 5000000 steps.`"
14562 .to_string(),
14563 ),
14564 span: self.current_span(),
14565 });
14566 };
14567 self.advance(); if self.peek_lexeme_lower(0).as_deref() == Some("steps") {
14569 self.advance();
14570 }
14571 budget = Some(steps);
14572 }
14573 _ => {
14574 return Err(ParseError {
14575 kind: ParseErrorKind::Custom(
14576 "a test grant is `This test may use the real <resource>.` \
14577 or `This test may run up to <N> steps.`"
14578 .to_string(),
14579 ),
14580 span: self.current_span(),
14581 });
14582 }
14583 }
14584 if self.check(&TokenType::Period) {
14585 self.advance();
14586 }
14587 continue;
14588 }
14589 break;
14590 }
14591
14592 let saved_in_test = self.in_test_block;
14594 self.in_test_block = true;
14595 let mut body_stmts = StmtList::default();
14596 let mut cases: Option<crate::ast::stmt::CaseTable<'a>> = None;
14597 let result = (|| -> ParseResult<()> {
14598 while !self.is_at_end() {
14599 if self.check(&TokenType::Newline)
14600 || self.check(&TokenType::Indent)
14601 || self.check(&TokenType::Dedent)
14602 {
14603 self.advance();
14604 continue;
14605 }
14606 if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
14607 break;
14608 }
14609 if self.peek_lexeme_lower(0).as_deref() == Some("for")
14613 && self.peek_lexeme_lower(1).as_deref() == Some("each")
14614 && self.peek_lexeme_lower(2).as_deref() == Some("of")
14615 {
14616 if cases.is_some() {
14617 return Err(ParseError {
14618 kind: ParseErrorKind::Custom(
14619 "a test carries one case table — merge the rows".to_string(),
14620 ),
14621 span: self.current_span(),
14622 });
14623 }
14624 cases = Some(self.parse_case_table()?);
14625 continue;
14626 }
14627 self.read_block_statement(&mut body_stmts)?;
14628 }
14629 Ok(())
14630 })();
14631 self.in_test_block = saved_in_test;
14632 result?;
14633
14634 let body = self.alloc_read_block(body_stmts);
14635 Ok(Stmt::TestDef {
14636 name,
14637 skip,
14638 capabilities,
14639 budget,
14640 cases,
14641 body,
14642 })
14643 }
14644
14645 fn at_table_line_end(&self) -> bool {
14647 matches!(
14648 self.peek().kind,
14649 TokenType::Newline | TokenType::Indent | TokenType::Dedent | TokenType::EOF
14650 ) || matches!(self.peek().kind, TokenType::BlockHeader { .. })
14651 }
14652
14653 fn parse_case_table(&mut self) -> ParseResult<crate::ast::stmt::CaseTable<'a>> {
14658 use crate::ast::stmt::CaseTable;
14659
14660 for expected in ["for", "each", "of", "these", "cases"] {
14662 if self.peek_lexeme_lower(0).as_deref() == Some(expected) {
14663 self.advance();
14664 }
14665 }
14666 if self.check(&TokenType::Colon) {
14667 self.advance();
14668 }
14669 while matches!(
14671 self.peek().kind,
14672 TokenType::Newline | TokenType::Indent | TokenType::Dedent
14673 ) {
14674 self.advance();
14675 }
14676 if !self.check(&TokenType::VBar) {
14677 return Err(ParseError {
14678 kind: ParseErrorKind::Custom(
14679 "a case table follows: a markdown table whose header row names \
14680 the columns (`| a | b | total |`)"
14681 .to_string(),
14682 ),
14683 span: self.current_span(),
14684 });
14685 }
14686
14687 let mut columns: Vec<Symbol> = Vec::new();
14689 self.advance(); while !self.at_table_line_end() {
14691 let mut cell_words: Vec<String> = Vec::new();
14692 while !self.check(&TokenType::VBar) && !self.at_table_line_end() {
14693 cell_words.push(self.interner.resolve(self.peek().lexeme).to_string());
14694 self.advance();
14695 }
14696 if !cell_words.is_empty() {
14697 columns.push(self.interner.intern(&cell_words.join(" ")));
14698 }
14699 if self.check(&TokenType::VBar) {
14700 self.advance();
14701 }
14702 }
14703 if columns.is_empty() {
14704 return Err(ParseError {
14705 kind: ParseErrorKind::Custom(
14706 "the case table's header row names its columns: `| a | b | total |`"
14707 .to_string(),
14708 ),
14709 span: self.current_span(),
14710 });
14711 }
14712 for column in &columns {
14716 self.user_bound.insert(*column);
14717 }
14718
14719 let mut rows: Vec<Vec<&'a Expr<'a>>> = Vec::new();
14721 let mut separator_checked = false;
14722 loop {
14723 while matches!(
14724 self.peek().kind,
14725 TokenType::Newline | TokenType::Indent | TokenType::Dedent
14726 ) {
14727 self.advance();
14728 }
14729 if !self.check(&TokenType::VBar) {
14730 break; }
14732 if !separator_checked {
14735 separator_checked = true;
14736 let mut k = 0;
14737 let mut only_dashes = true;
14738 loop {
14739 let Some(tok) = self.tokens.get(self.current + k) else {
14740 break;
14741 };
14742 match &tok.kind {
14743 TokenType::VBar | TokenType::Minus => k += 1,
14744 TokenType::Newline
14745 | TokenType::Indent
14746 | TokenType::Dedent
14747 | TokenType::EOF => break,
14748 _ => {
14749 let lex = self.interner.resolve(tok.lexeme);
14751 if !lex.is_empty() && lex.chars().all(|c| c == '-') {
14752 k += 1;
14753 } else {
14754 only_dashes = false;
14755 break;
14756 }
14757 }
14758 }
14759 }
14760 if only_dashes {
14761 for _ in 0..k {
14762 self.advance();
14763 }
14764 continue;
14765 }
14766 }
14767 self.advance(); let mut row: Vec<&'a Expr<'a>> = Vec::new();
14772 while !self.at_table_line_end() {
14773 let cell = self.parse_bitxor_expr()?;
14774 row.push(cell);
14775 if self.check(&TokenType::VBar) {
14776 self.advance();
14777 }
14778 }
14779 if row.len() != columns.len() {
14780 return Err(ParseError {
14781 kind: ParseErrorKind::Custom(format!(
14782 "this case row has {} cells but the header names {} columns",
14783 row.len(),
14784 columns.len()
14785 )),
14786 span: self.current_span(),
14787 });
14788 }
14789 rows.push(row);
14790 }
14791 if rows.is_empty() {
14792 return Err(ParseError {
14793 kind: ParseErrorKind::Custom(
14794 "the case table needs at least one data row under the header".to_string(),
14795 ),
14796 span: self.current_span(),
14797 });
14798 }
14799 Ok(CaseTable { columns, rows })
14800 }
14801
14802 fn parse_expect_statement(&mut self) -> ParseResult<Stmt<'a>> {
14809 use crate::ast::stmt::ExpectedOutput;
14810
14811 let gate_span = self.current_span();
14812 self.advance(); if !self.in_test_block {
14814 return Err(ParseError {
14815 kind: ParseErrorKind::Custom(
14816 "`Expect` lives in `## Test` blocks — for a runtime guard in ordinary \
14817 code, use `Require that <condition>.`"
14818 .to_string(),
14819 ),
14820 span: gate_span,
14821 });
14822 }
14823
14824 if self.peek_lexeme_lower(0).as_deref() == Some("output") {
14826 self.advance(); if self.peek_lexeme_lower(0).as_deref() == Some("as") {
14828 self.advance(); if self.peek_lexeme_lower(0).as_deref() == Some("in") {
14830 self.advance(); }
14832 let TokenType::StringLiteral(path) = self.peek().kind else {
14833 return Err(ParseError {
14834 kind: ParseErrorKind::Custom(
14835 "the golden file is a quoted path: \
14836 `Expect output as in \"golden/report.txt\".`"
14837 .to_string(),
14838 ),
14839 span: self.current_span(),
14840 });
14841 };
14842 self.advance();
14843 return Ok(Stmt::ExpectOutput {
14844 expected: ExpectedOutput::File(path),
14845 });
14846 }
14847 if self.check(&TokenType::Colon) {
14848 self.advance();
14849 }
14850 while self.check(&TokenType::Newline) {
14853 self.advance();
14854 }
14855 let mut depth = 0usize;
14856 while self.check(&TokenType::Indent) {
14857 depth += 1;
14858 self.advance();
14859 }
14860 let mut lines: Vec<String> = Vec::new();
14861 let mut line: Vec<String> = Vec::new();
14862 while depth > 0 && !self.is_at_end() {
14863 match self.peek().kind {
14864 TokenType::Indent => {
14865 depth += 1;
14866 self.advance();
14867 }
14868 TokenType::Dedent => {
14869 depth -= 1;
14870 self.advance();
14871 }
14872 TokenType::Newline => {
14873 if !line.is_empty() {
14874 lines.push(line.join(" "));
14875 line.clear();
14876 }
14877 self.advance();
14878 }
14879 TokenType::EOF => break,
14880 _ => {
14881 line.push(self.interner.resolve(self.peek().lexeme).to_string());
14882 self.advance();
14883 }
14884 }
14885 }
14886 if !line.is_empty() {
14887 lines.push(line.join(" "));
14888 }
14889 let text = self.interner.intern(&lines.join("\n"));
14890 return Ok(Stmt::ExpectOutput {
14891 expected: ExpectedOutput::Inline(text),
14892 });
14893 }
14894
14895 if self.peek_lexeme_lower(0).as_deref() == Some("this") {
14897 self.advance(); while self
14899 .peek_lexeme_lower(0)
14900 .is_some_and(|w| w == "to" || w == "fail")
14901 {
14902 self.advance();
14903 }
14904 let mut message: Option<Symbol> = None;
14905 if self.peek_lexeme_lower(0).as_deref() == Some("with") {
14906 self.advance(); let TokenType::StringLiteral(msg) = self.peek().kind else {
14908 return Err(ParseError {
14909 kind: ParseErrorKind::Custom(
14910 "the expected failure is a quoted message: \
14911 `Expect this to fail with \"Division by zero\":`"
14912 .to_string(),
14913 ),
14914 span: self.current_span(),
14915 });
14916 };
14917 self.advance();
14918 message = Some(msg);
14919 }
14920 if self.check(&TokenType::Colon) {
14921 self.advance();
14922 }
14923 let body = self.parse_indented_block()?;
14924 return Ok(Stmt::ExpectFail { message, body });
14925 }
14926
14927 if self.check(&TokenType::That)
14929 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14930 {
14931 self.advance();
14932 }
14933 let condition = self.parse_condition()?;
14934 Ok(Stmt::Expect { condition })
14935 }
14936
14937 fn parse_advance_time_statement(&mut self) -> ParseResult<Stmt<'a>> {
14940 let gate_span = self.current_span();
14941 self.advance(); if !self.in_test_block {
14943 return Err(ParseError {
14944 kind: ParseErrorKind::Custom(
14945 "`Advance time` moves a test's virtual clock — it lives in \
14946 `## Test` blocks only"
14947 .to_string(),
14948 ),
14949 span: gate_span,
14950 });
14951 }
14952 if self.peek_lexeme_lower(0).as_deref() == Some("time") {
14953 self.advance();
14954 }
14955 if self.peek_lexeme_lower(0).as_deref() == Some("by") {
14956 self.advance();
14957 }
14958 let amount = self.parse_xor_expr()?;
14959 let unit_ms: i64 = match self.peek_lexeme_lower(0).as_deref() {
14960 Some("millisecond") | Some("milliseconds") | Some("ms") => {
14961 self.advance();
14962 1
14963 }
14964 Some("second") | Some("seconds") => {
14965 self.advance();
14966 1000
14967 }
14968 Some("minute") | Some("minutes") => {
14969 self.advance();
14970 60_000
14971 }
14972 _ => 1, };
14974 let millis = if unit_ms == 1 {
14975 match amount {
14986 Expr::Literal(Literal::Duration(ns)) => {
14987 self.ctx
14988 .alloc_imperative_expr(Expr::Literal(Literal::Number(
14989 (*ns).max(0) / 1_000_000,
14990 )))
14991 }
14992 other => other,
14993 }
14994 } else {
14995 let factor = self
14996 .ctx
14997 .alloc_imperative_expr(Expr::Literal(Literal::Number(unit_ms)));
14998 self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14999 op: BinaryOpKind::Multiply,
15000 left: amount,
15001 right: factor,
15002 })
15003 };
15004 Ok(Stmt::AdvanceTime { millis })
15005 }
15006
15007 fn parse_given_file_statement(&mut self) -> ParseResult<Stmt<'a>> {
15010 let gate_span = self.current_span();
15011 self.advance(); if !self.in_test_block {
15013 return Err(ParseError {
15014 kind: ParseErrorKind::Custom(
15015 "`Given a file …` seeds a test's sealed filesystem — it lives in \
15016 `## Test` blocks only"
15017 .to_string(),
15018 ),
15019 span: gate_span,
15020 });
15021 }
15022 while self
15023 .peek_lexeme_lower(0)
15024 .is_some_and(|w| w == "a" || w == "an" || w == "file")
15025 {
15026 self.advance();
15027 }
15028 let TokenType::StringLiteral(path) = self.peek().kind else {
15029 return Err(ParseError {
15030 kind: ParseErrorKind::Custom(
15031 "the fixture path is a quoted string: \
15032 `Given a file \"data.txt\" containing \"hello\".`"
15033 .to_string(),
15034 ),
15035 span: self.current_span(),
15036 });
15037 };
15038 self.advance();
15039 if self.peek_lexeme_lower(0).as_deref() == Some("containing") {
15040 self.advance();
15041 }
15042 let TokenType::StringLiteral(content) = self.peek().kind else {
15043 return Err(ParseError {
15044 kind: ParseErrorKind::Custom(
15045 "the fixture content is a quoted string: \
15046 `Given a file \"data.txt\" containing \"hello\".`"
15047 .to_string(),
15048 ),
15049 span: self.current_span(),
15050 });
15051 };
15052 self.advance();
15053 Ok(Stmt::GivenFile { path, content })
15054 }
15055
15056 fn next_is_argument_list(&self) -> bool {
15058 self.tokens
15059 .get(self.current + 1)
15060 .is_some_and(|t| matches!(t.kind, TokenType::LParen))
15061 }
15062
15063 fn parse_bare_call_statement(&mut self) -> ParseResult<Stmt<'a>> {
15065 let function = self.peek().lexeme;
15066 self.advance();
15067 let expr = self.parse_call_expr(function)?;
15068 if let Expr::Call { function, args } = expr {
15069 return Ok(Stmt::Call {
15070 function: *function,
15071 args: args.clone(),
15072 });
15073 }
15074 Err(ParseError {
15079 kind: ParseErrorKind::ExpectedStatement,
15080 span: self.current_span(),
15081 })
15082 }
15083
15084 fn parse_observe_statement(
15088 &mut self,
15089 category: crate::ast::stmt::ObsCategory,
15090 ) -> ParseResult<Stmt<'a>> {
15091 self.advance(); let mut sample: Option<u64> = None;
15095 if self.peek_lexeme_lower(0).as_deref() == Some("once") {
15096 self.advance(); while self
15098 .peek_lexeme_lower(0)
15099 .is_some_and(|w| w == "in" || w == "every")
15100 {
15101 self.advance();
15102 }
15103 let n = self
15104 .peek_lexeme_lower(0)
15105 .unwrap_or_default()
15106 .replace('_', "");
15107 if let Ok(rate) = n.parse::<u64>() {
15108 self.advance(); if self.peek_lexeme_lower(0).as_deref() == Some("times") {
15110 self.advance();
15111 }
15112 sample = Some(rate.max(1));
15113 }
15114 }
15115
15116 if self.check(&TokenType::That)
15117 || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
15118 {
15119 self.advance();
15120 }
15121 let mut words: Vec<String> = Vec::new();
15123 while let Some(tok) = self.tokens.get(self.current) {
15124 if self.peek_lexeme_lower(0).as_deref() == Some("with") {
15125 break;
15126 }
15127 match tok.kind {
15128 TokenType::Period
15129 | TokenType::Newline
15130 | TokenType::Indent
15131 | TokenType::Dedent
15132 | TokenType::Colon
15133 | TokenType::EOF
15134 | TokenType::BlockHeader { .. } => break,
15135 _ => {
15136 words.push(self.interner.resolve(tok.lexeme).to_string());
15137 self.advance();
15138 }
15139 }
15140 }
15141 let message = self.interner.intern(words.join(" ").trim());
15142
15143 let mut payloads: Vec<(Symbol, &'a Expr<'a>)> = Vec::new();
15145 if self.peek_lexeme_lower(0).as_deref() == Some("with") {
15146 self.advance(); loop {
15148 let expr = self.parse_xor_expr()?;
15149 let name = match expr {
15150 Expr::Identifier(s) => *s,
15151 other => self
15152 .interner
15153 .intern(&crate::printer::render_expr(other, self.interner)),
15154 };
15155 payloads.push((name, expr));
15156 if self.check_word("and") || self.check(&TokenType::And) {
15157 self.advance();
15158 } else {
15159 break;
15160 }
15161 }
15162 }
15163 Ok(Stmt::Observe {
15164 category,
15165 message,
15166 payloads,
15167 sample,
15168 })
15169 }
15170
15171 fn parse_during_statement(&mut self) -> ParseResult<Stmt<'a>> {
15173 self.advance(); let mut words: Vec<String> = Vec::new();
15175 while let Some(tok) = self.tokens.get(self.current) {
15176 match tok.kind {
15177 TokenType::Colon | TokenType::Newline | TokenType::Indent | TokenType::EOF => break,
15178 TokenType::StringLiteral(s) => {
15179 words.push(self.interner.resolve(s).to_string());
15180 self.advance();
15181 }
15182 _ => {
15183 words.push(self.interner.resolve(tok.lexeme).to_string());
15184 self.advance();
15185 }
15186 }
15187 }
15188 let label = self.interner.intern(words.join(" ").trim());
15189 if self.check(&TokenType::Colon) {
15190 self.advance();
15191 }
15192 while self.check(&TokenType::Newline) {
15194 self.advance();
15195 }
15196 let body = self.parse_indented_block()?;
15197 Ok(Stmt::During { label, body })
15198 }
15199
15200 fn parse_forall_statement(&mut self) -> ParseResult<Stmt<'a>> {
15204 use crate::ast::stmt::PropDomain;
15205 self.advance(); self.advance(); let domain = match self.peek_lexeme_lower(0).as_deref() {
15210 Some("integers") | Some("integer") => PropDomain::Integers,
15211 Some("naturals") | Some("natural") => PropDomain::Naturals,
15212 Some("booleans") | Some("boolean") | Some("bools") | Some("bool") => {
15213 PropDomain::Booleans
15214 }
15215 other => {
15216 return Err(ParseError {
15217 kind: ParseErrorKind::Custom(format!(
15218 "a property quantifies over a domain — `integers`, `naturals`, or \
15219 `booleans` (got {:?})",
15220 other.unwrap_or_default()
15221 )),
15222 span: self.current_span(),
15223 });
15224 }
15225 };
15226 self.advance(); let mut vars: Vec<Symbol> = Vec::new();
15231 loop {
15232 let name = self.expect_identifier()?;
15233 self.user_bound.insert(name);
15234 vars.push(name);
15235 if self.check_word("and")
15236 || self.check(&TokenType::And)
15237 || self.check(&TokenType::Comma)
15238 {
15239 self.advance();
15240 } else {
15241 break;
15242 }
15243 }
15244 if vars.is_empty() {
15245 return Err(ParseError {
15246 kind: ParseErrorKind::Custom(
15247 "a property needs at least one bound variable: `For all integers n:`"
15248 .to_string(),
15249 ),
15250 span: self.current_span(),
15251 });
15252 }
15253
15254 let constraint = if self.check_word("where") {
15256 self.advance(); Some(self.parse_condition()?)
15258 } else {
15259 None
15260 };
15261
15262 if self.check(&TokenType::Colon) {
15263 self.advance();
15264 }
15265 while self.check(&TokenType::Newline) {
15266 self.advance();
15267 }
15268 let body = self.parse_indented_block()?;
15269 Ok(Stmt::ForAll {
15270 vars,
15271 domain,
15272 constraint,
15273 body,
15274 })
15275 }
15276
15277 fn parse_check_statement(&mut self) -> ParseResult<Stmt<'a>> {
15281 let start_span = self.current_span();
15282 self.advance(); if self.check(&TokenType::That) {
15286 self.advance();
15287 }
15288
15289 if matches!(self.peek().kind, TokenType::Article(_)) {
15291 self.advance();
15292 }
15293
15294 let subject = match &self.peek().kind {
15296 TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15297 let s = *sym;
15298 self.advance();
15299 s
15300 }
15301 _ => {
15302 let tok = self.peek();
15304 let s = tok.lexeme;
15305 self.advance();
15306 s
15307 }
15308 };
15309
15310 let is_capability;
15312 let predicate;
15313 let object;
15314
15315 if self.check(&TokenType::Is) || self.check(&TokenType::Are) {
15316 is_capability = false;
15318 self.advance(); predicate = match &self.peek().kind {
15322 TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15323 let s = *sym;
15324 self.advance();
15325 s
15326 }
15327 _ => {
15328 let tok = self.peek();
15329 let s = tok.lexeme;
15330 self.advance();
15331 s
15332 }
15333 };
15334 object = None;
15335 } else if self.check(&TokenType::Can) {
15336 is_capability = true;
15338 self.advance(); predicate = match &self.peek().kind {
15342 TokenType::Verb { lemma, .. } => {
15343 let s = *lemma;
15344 self.advance();
15345 s
15346 }
15347 TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15348 let s = *sym;
15349 self.advance();
15350 s
15351 }
15352 _ => {
15353 let tok = self.peek();
15354 let s = tok.lexeme;
15355 self.advance();
15356 s
15357 }
15358 };
15359
15360 if matches!(self.peek().kind, TokenType::Article(_)) {
15362 self.advance();
15363 }
15364
15365 let obj = match &self.peek().kind {
15367 TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15368 let s = *sym;
15369 self.advance();
15370 s
15371 }
15372 _ => {
15373 let tok = self.peek();
15374 let s = tok.lexeme;
15375 self.advance();
15376 s
15377 }
15378 };
15379 object = Some(obj);
15380 } else {
15381 return Err(ParseError {
15382 kind: ParseErrorKind::ExpectedKeyword {
15383 keyword: "is/can".to_string(),
15384 },
15385 span: self.current_span(),
15386 });
15387 }
15388
15389 let source_text = if is_capability {
15391 let obj_name = self.interner.resolve(object.unwrap());
15392 let pred_name = self.interner.resolve(predicate);
15393 let subj_name = self.interner.resolve(subject);
15394 format!("{} can {} the {}", subj_name, pred_name, obj_name)
15395 } else {
15396 let pred_name = self.interner.resolve(predicate);
15397 let subj_name = self.interner.resolve(subject);
15398 format!("{} is {}", subj_name, pred_name)
15399 };
15400
15401 Ok(Stmt::Check {
15402 subject,
15403 predicate,
15404 is_capability,
15405 object,
15406 source_text,
15407 span: start_span,
15408 })
15409 }
15410
15411 fn read_word_string(&mut self) -> String {
15413 let s = self.interner.resolve(self.peek().lexeme).to_string();
15414 self.advance();
15415 s
15416 }
15417
15418 fn expect_keyword(&mut self, word: &str) -> ParseResult<()> {
15420 if self.check_word(word) {
15421 self.advance();
15422 Ok(())
15423 } else {
15424 Err(ParseError {
15425 kind: ParseErrorKind::Custom(format!("expected `{}` here", word)),
15426 span: self.current_span(),
15427 })
15428 }
15429 }
15430
15431 fn read_int_literal(&mut self) -> ParseResult<i64> {
15433 if let TokenType::Number(sym) = &self.peek().kind {
15434 let sym = *sym;
15435 self.advance();
15436 self.interner
15437 .resolve(sym)
15438 .parse::<i64>()
15439 .map_err(|_| ParseError {
15440 kind: ParseErrorKind::Custom("expected an integer bound".to_string()),
15441 span: self.current_span(),
15442 })
15443 } else {
15444 Err(ParseError {
15445 kind: ParseErrorKind::Custom("expected an integer bound".to_string()),
15446 span: self.current_span(),
15447 })
15448 }
15449 }
15450
15451 fn parse_accept_contract(&mut self) -> ParseResult<()> {
15455 self.advance(); self.expect_keyword("computed")?;
15457 let name = self.read_word_string();
15458 self.expect_keyword("where")?;
15459 let _param = self.read_word_string(); self.expect_keyword("is")?;
15461 if self.check_word("a") || self.check_word("an") {
15462 self.advance();
15463 }
15464 self.expect_keyword("Int")?;
15465 self.expect_keyword("from")?;
15466 let lo = self.read_int_literal()?;
15467 if self.check(&TokenType::To) || self.check_word("to") {
15469 self.advance();
15470 } else {
15471 return Err(ParseError {
15472 kind: ParseErrorKind::Custom(
15473 "expected `to` between the contract bounds".to_string(),
15474 ),
15475 span: self.current_span(),
15476 });
15477 }
15478 let hi = self.read_int_literal()?;
15479 if self.check(&TokenType::Period) {
15480 self.advance(); }
15482 self.contracts.insert(name, (lo, hi));
15483 Ok(())
15484 }
15485
15486 fn parse_run_under_contract(&mut self) -> ParseResult<Stmt<'a>> {
15490 self.advance(); let f_expr = self.parse_imperative_expr()?;
15492 self.expect_keyword("on")?;
15493 let arg_expr = self.parse_imperative_expr()?;
15494 self.expect_keyword("under")?;
15495 let name = self.read_word_string();
15496 let (lo, hi) = *self.contracts.get(&name).ok_or_else(|| ParseError {
15497 kind: ParseErrorKind::Custom(format!(
15498 "unknown acceptance contract `{name}` — declare it with `Accept computed {name} where …`"
15499 )),
15500 span: self.current_span(),
15501 })?;
15502 self.expect_keyword("into")?;
15503 let var = self.peek().lexeme;
15504 self.advance();
15505 let lo_e = self
15506 .ctx
15507 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(lo)));
15508 let hi_e = self
15509 .ctx
15510 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(hi)));
15511 let run_sym = self.interner.intern("run_accepted");
15512 let call = self.ctx.alloc_imperative_expr(Expr::Call {
15513 function: run_sym,
15514 args: vec![f_expr, arg_expr, lo_e, hi_e],
15515 });
15516 Ok(Stmt::Let {
15517 var,
15518 ty: None,
15519 value: call,
15520 mutable: false,
15521 })
15522 }
15523
15524 fn parse_listen_statement(&mut self) -> ParseResult<Stmt<'a>> {
15527 self.advance(); if !self.check_preposition_is("on") {
15531 return Err(ParseError {
15532 kind: ParseErrorKind::ExpectedKeyword {
15533 keyword: "on".to_string(),
15534 },
15535 span: self.current_span(),
15536 });
15537 }
15538 self.advance(); let address = self.parse_imperative_expr()?;
15542
15543 let secure = self.parse_secure_clause()?;
15544 Ok(Stmt::Listen { address, secure })
15545 }
15546
15547 fn parse_serve_statement(&mut self) -> ParseResult<Stmt<'a>> {
15550 self.advance(); if !self.check_preposition_is("on") && !self.check_word("on") {
15554 return Err(ParseError {
15555 kind: ParseErrorKind::ExpectedKeyword {
15556 keyword: "on".to_string(),
15557 },
15558 span: self.current_span(),
15559 });
15560 }
15561 self.advance(); if !self.check_word("port") {
15565 return Err(ParseError {
15566 kind: ParseErrorKind::ExpectedKeyword {
15567 keyword: "port".to_string(),
15568 },
15569 span: self.current_span(),
15570 });
15571 }
15572 self.advance(); let port = self.parse_imperative_expr()?;
15576
15577 if !self.check_preposition_is("with") && !self.check_word("with") {
15586 let handler = self.ctx.alloc_imperative_expr(Expr::Identifier(
15587 self.interner
15588 .intern(crate::parser::lgx_lower::ROUTE_HANDLER),
15589 ));
15590 return Ok(Stmt::Serve { port, handler });
15591 }
15592 self.advance(); let handler = self.parse_imperative_expr()?;
15596
15597 Ok(Stmt::Serve { port, handler })
15598 }
15599
15600 fn parse_on_statement(&mut self) -> ParseResult<Stmt<'a>> {
15606 use crate::ast::stmt::ClosureBody;
15607 use crate::ast::{Expr, Literal, TypeExpr};
15608
15609 self.advance(); let event_sym = self.peek().lexeme;
15611 self.advance(); if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
15614 return Err(ParseError {
15615 kind: ParseErrorKind::ExpectedKeyword {
15616 keyword: "from".to_string(),
15617 },
15618 span: self.current_span(),
15619 });
15620 }
15621 self.advance(); let source = self.parse_imperative_expr()?;
15624
15625 if !self.check(&TokenType::Colon) {
15626 return Err(ParseError {
15627 kind: ParseErrorKind::ExpectedKeyword {
15628 keyword: ":".to_string(),
15629 },
15630 span: self.current_span(),
15631 });
15632 }
15633 self.advance(); let mut block_stmts = StmtList::default();
15637 if !self.check(&TokenType::Indent) {
15638 self.read_block_statement(&mut block_stmts)?;
15639 } else {
15640 self.advance(); while !self.check(&TokenType::Dedent) && !self.is_at_end() {
15642 if self.check(&TokenType::Newline) {
15643 self.advance();
15644 continue;
15645 }
15646 self.read_block_statement(&mut block_stmts)?;
15647 }
15648 if self.check(&TokenType::Dedent) {
15649 self.advance();
15650 }
15651 }
15652 let block = self.alloc_read_block(block_stmts);
15653 let int_ty = self
15655 .ctx
15656 .alloc_type_expr(TypeExpr::Primitive(self.interner.intern("Int")));
15657 let param = self.interner.intern("event");
15658 let closure = self.ctx.alloc_imperative_expr(Expr::Closure {
15659 params: vec![(param, int_ty)],
15660 body: ClosureBody::Block(block),
15661 return_type: None,
15662 });
15663 let event_name = self
15664 .ctx
15665 .alloc_imperative_expr(Expr::Literal(Literal::Text(event_sym)));
15666 let add_listener = self.interner.intern("addEventListener");
15667 Ok(Stmt::Call {
15668 function: add_listener,
15669 args: vec![source, event_name, closure],
15670 })
15671 }
15672
15673 fn parse_connect_statement(&mut self) -> ParseResult<Stmt<'a>> {
15676 self.advance(); if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
15680 return Err(ParseError {
15681 kind: ParseErrorKind::ExpectedKeyword {
15682 keyword: "to".to_string(),
15683 },
15684 span: self.current_span(),
15685 });
15686 }
15687 self.advance(); let address = self.parse_imperative_expr()?;
15691
15692 let secure = self.parse_secure_clause()?;
15693 Ok(Stmt::ConnectTo { address, secure })
15694 }
15695
15696 fn parse_secure_clause(&mut self) -> ParseResult<Option<crate::ast::SecurePad<'a>>> {
15700 if !(self.check_word("with") && self.peek_word_at(1, "pad")) {
15702 return Ok(None);
15703 }
15704 self.advance(); self.advance(); let pad = self.parse_imperative_expr()?;
15707 if !self.check_word("as") {
15708 return Err(ParseError {
15709 kind: ParseErrorKind::ExpectedKeyword {
15710 keyword: "as".to_string(),
15711 },
15712 span: self.current_span(),
15713 });
15714 }
15715 self.advance(); let role = if self.check_word("initiator") {
15717 self.advance();
15718 crate::ast::SecureRole::Initiator
15719 } else if self.check_word("responder") {
15720 self.advance();
15721 crate::ast::SecureRole::Responder
15722 } else {
15723 return Err(ParseError {
15724 kind: ParseErrorKind::ExpectedKeyword {
15725 keyword: "initiator or responder".to_string(),
15726 },
15727 span: self.current_span(),
15728 });
15729 };
15730 Ok(Some(crate::ast::SecurePad { pad, role }))
15731 }
15732
15733 fn parse_sleep_statement(&mut self) -> ParseResult<Stmt<'a>> {
15736 self.advance(); let milliseconds = self.parse_imperative_expr()?;
15740
15741 Ok(Stmt::Sleep { milliseconds })
15742 }
15743
15744 fn parse_sync_statement(&mut self) -> ParseResult<Stmt<'a>> {
15747 self.advance(); let var = match &self.tokens[self.current].kind {
15752 TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
15753 let s = *sym;
15754 self.advance();
15755 s
15756 }
15757 TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
15758 let s = self.tokens[self.current].lexeme;
15759 self.advance();
15760 s
15761 }
15762 _ => {
15763 return Err(ParseError {
15764 kind: ParseErrorKind::ExpectedKeyword {
15765 keyword: "variable name".to_string(),
15766 },
15767 span: self.current_span(),
15768 });
15769 }
15770 };
15771
15772 if !self.check_preposition_is("on") {
15774 return Err(ParseError {
15775 kind: ParseErrorKind::ExpectedKeyword {
15776 keyword: "on".to_string(),
15777 },
15778 span: self.current_span(),
15779 });
15780 }
15781 self.advance(); let topic = self.parse_imperative_expr()?;
15785
15786 Ok(Stmt::Sync { var, topic })
15787 }
15788
15789 fn parse_mount_statement(&mut self) -> ParseResult<Stmt<'a>> {
15793 self.advance(); let name = self.tokens[self.current].lexeme;
15802 let capitalised = self
15803 .interner
15804 .resolve(name)
15805 .chars()
15806 .next()
15807 .is_some_and(char::is_uppercase);
15808 let no_target = matches!(
15809 self.tokens.get(self.current + 1).map(|t| &t.kind),
15810 Some(TokenType::Period)
15811 );
15812 let selector_target = matches!(
15813 self.tokens.get(self.current + 1).map(|t| &t.kind),
15814 Some(TokenType::At)
15815 ) && matches!(
15816 self.tokens.get(self.current + 2).map(|t| &t.kind),
15817 Some(TokenType::StringLiteral(s)) if crate::lgx::html::is_selector_shaped(self.interner.resolve(*s))
15818 );
15819 if self.components.contains(&name) || capitalised || no_target || selector_target {
15820 return self.parse_ui_mount_statement();
15821 }
15822
15823 let var = match &self.tokens[self.current].kind {
15826 TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
15827 let s = *sym;
15828 self.advance();
15829 s
15830 }
15831 TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
15832 let s = self.tokens[self.current].lexeme;
15833 self.advance();
15834 s
15835 }
15836 _ => {
15837 return Err(ParseError {
15838 kind: ParseErrorKind::ExpectedKeyword {
15839 keyword: "variable name".to_string(),
15840 },
15841 span: self.current_span(),
15842 });
15843 }
15844 };
15845
15846 if !self.check(&TokenType::At) {
15848 return Err(ParseError {
15849 kind: ParseErrorKind::ExpectedKeyword {
15850 keyword: "at".to_string(),
15851 },
15852 span: self.current_span(),
15853 });
15854 }
15855 self.advance(); let path = self.parse_imperative_expr()?;
15859
15860 Ok(Stmt::Mount { var, path })
15861 }
15862
15863 fn parse_ui_mount_statement(&mut self) -> ParseResult<Stmt<'a>> {
15869 use crate::ast::{BinaryOpKind, Expr, Literal};
15870
15871 let comp_sym = self.tokens[self.current].lexeme;
15872 let target = match (
15873 self.tokens.get(self.current + 1).map(|t| &t.kind),
15874 self.tokens.get(self.current + 2).map(|t| &t.kind),
15875 ) {
15876 (Some(TokenType::At), Some(TokenType::StringLiteral(s))) => {
15877 Some(self.interner.resolve(*s).to_string())
15878 }
15879 _ => None,
15880 };
15881 self.mount_sites.push(MountSite {
15882 name: comp_sym,
15883 span: self.current_span(),
15884 is_root: !self.in_test_block,
15885 target,
15886 });
15887 self.advance(); if self.lgx_components.contains(&comp_sym) {
15891 let selector = if self.check(&TokenType::At) {
15892 self.advance();
15893 Some(self.parse_imperative_expr()?)
15894 } else {
15895 None
15896 };
15897 return Ok(Stmt::Ui(crate::ast::stmt::UiStmt::Mount {
15898 component: comp_sym,
15899 selector,
15900 }));
15901 }
15902 let sel_str = if self.check(&TokenType::At) {
15906 self.advance(); let sel_expr = self.parse_imperative_expr()?;
15908 match sel_expr {
15909 Expr::Literal(Literal::Text(s)) => self.interner.resolve(*s).to_string(),
15910 _ => {
15911 return Err(ParseError {
15912 kind: ParseErrorKind::Custom(
15913 "Mount's selector must be a string literal, e.g. `Mount App at \"#sidebar\"` \
15914 — or just `Mount App` for the default `#root`."
15915 .to_string(),
15916 ),
15917 span: self.current_span(),
15918 });
15919 }
15920 }
15921 } else {
15922 "#root".to_string()
15923 };
15924 let id_str = sel_str.strip_prefix('#').unwrap_or(&sel_str).to_string();
15925
15926 let render_entry = self.interner.intern("render");
15927 let render_name = self
15928 .interner
15929 .intern(&format!("__render_{}", self.interner.resolve(comp_sym)));
15930 let query = self.interner.intern("querySelector");
15931 let sel_sym = self.interner.intern(&sel_str);
15932
15933 let mut render_body: Vec<Stmt<'a>> = Vec::new();
15935 let begin = self.interner.intern("beginRender");
15936 render_body.push(Stmt::Call {
15937 function: begin,
15938 args: vec![],
15939 });
15940 let root_sym = self.interner.intern("__root");
15941 let sel_lit = self
15942 .ctx
15943 .alloc_imperative_expr(Expr::Literal(Literal::Text(sel_sym)));
15944 let qs_call = self.ctx.alloc_imperative_expr(Expr::Call {
15945 function: query,
15946 args: vec![sel_lit],
15947 });
15948 render_body.push(Stmt::Let {
15949 var: root_sym,
15950 ty: None,
15951 value: qs_call,
15952 mutable: false,
15953 });
15954 let root_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(root_sym));
15955 let clear = self.interner.intern("clearChildren");
15956 render_body.push(Stmt::Call {
15957 function: clear,
15958 args: vec![root_ref],
15959 });
15960 let enter = self.interner.intern("enterComponent");
15961 let key_lit = self
15962 .ctx
15963 .alloc_imperative_expr(Expr::Literal(Literal::Text(comp_sym)));
15964 render_body.push(Stmt::Call {
15965 function: enter,
15966 args: vec![key_lit],
15967 });
15968 render_body.push(Stmt::Call {
15969 function: render_name,
15970 args: vec![root_ref],
15971 });
15972 let exit = self.interner.intern("exitComponent");
15973 render_body.push(Stmt::Call {
15974 function: exit,
15975 args: vec![],
15976 });
15977 let render_block = self.alloc_block(render_body);
15978 self.deferred_functions.push(Stmt::FunctionDef {
15979 name: render_entry,
15980 generics: Vec::new(),
15981 params: Vec::new(),
15982 body: render_block,
15983 return_type: None,
15984 is_native: false,
15985 native_path: None,
15986 is_exported: false,
15987 export_target: None,
15988 opt_flags: OptimizationConfig::all_on(),
15989 });
15990
15991 let mut mount_body: Vec<Stmt<'a>> = Vec::new();
15993 let sel_lit2 = self
15995 .ctx
15996 .alloc_imperative_expr(Expr::Literal(Literal::Text(sel_sym)));
15997 let qs_call2 = self.ctx.alloc_imperative_expr(Expr::Call {
15998 function: query,
15999 args: vec![sel_lit2],
16000 });
16001 let zero = self
16002 .ctx
16003 .alloc_imperative_expr(Expr::Literal(Literal::Number(0)));
16004 let cond = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
16005 op: BinaryOpKind::Lt,
16006 left: qs_call2,
16007 right: zero,
16008 });
16009 let mut then_body: Vec<Stmt<'a>> = Vec::new();
16010 let create = self.interner.intern("createElement");
16011 let div_lit = self
16012 .ctx
16013 .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern("div"))));
16014 let create_call = self.ctx.alloc_imperative_expr(Expr::Call {
16015 function: create,
16016 args: vec![div_lit],
16017 });
16018 let mount_sym = self.interner.intern("__mount");
16019 then_body.push(Stmt::Let {
16020 var: mount_sym,
16021 ty: None,
16022 value: create_call,
16023 mutable: false,
16024 });
16025 let mount_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(mount_sym));
16026 let set_attr = self.interner.intern("setAttribute");
16027 let id_key = self
16028 .ctx
16029 .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern("id"))));
16030 let id_val = self
16031 .ctx
16032 .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern(&id_str))));
16033 then_body.push(Stmt::Call {
16034 function: set_attr,
16035 args: vec![mount_ref, id_key, id_val],
16036 });
16037 let body_fn = self.interner.intern("body");
16038 let body_call = self.ctx.alloc_imperative_expr(Expr::Call {
16039 function: body_fn,
16040 args: vec![],
16041 });
16042 let append = self.interner.intern("appendChild");
16043 then_body.push(Stmt::Call {
16044 function: append,
16045 args: vec![body_call, mount_ref],
16046 });
16047 let then_block = self.alloc_block(then_body);
16048 mount_body.push(Stmt::If {
16049 cond,
16050 then_block,
16051 else_block: None,
16052 });
16053 mount_body.push(Stmt::Call {
16054 function: render_entry,
16055 args: vec![],
16056 });
16057 let mount_block = self.alloc_block(mount_body);
16058 Ok(Stmt::Splice { body: mount_block })
16059 }
16060
16061 fn lookahead_contains_into(&self) -> bool {
16067 for i in self.current..std::cmp::min(self.current + 5, self.tokens.len()) {
16068 if matches!(self.tokens[i].kind, TokenType::Into) {
16069 return true;
16070 }
16071 }
16072 false
16073 }
16074
16075 fn lookahead_is_first_of(&self) -> bool {
16077 self.current + 3 < self.tokens.len()
16079 && matches!(self.tokens.get(self.current + 1), Some(t) if matches!(t.kind, TokenType::Article(_)))
16080 && self
16081 .tokens
16082 .get(self.current + 2)
16083 .map(|t| self.interner.resolve(t.lexeme).to_lowercase() == "first")
16084 .unwrap_or(false)
16085 }
16086
16087 fn parse_launch_statement(&mut self) -> ParseResult<Stmt<'a>> {
16090 self.advance(); if !self.check_article() {
16094 return Err(ParseError {
16095 kind: ParseErrorKind::ExpectedKeyword {
16096 keyword: "a".to_string(),
16097 },
16098 span: self.current_span(),
16099 });
16100 }
16101 self.advance();
16102
16103 if !self.check(&TokenType::Task) {
16105 return Err(ParseError {
16106 kind: ParseErrorKind::ExpectedKeyword {
16107 keyword: "task".to_string(),
16108 },
16109 span: self.current_span(),
16110 });
16111 }
16112 self.advance();
16113
16114 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16116 return Err(ParseError {
16117 kind: ParseErrorKind::ExpectedKeyword {
16118 keyword: "to".to_string(),
16119 },
16120 span: self.current_span(),
16121 });
16122 }
16123 self.advance();
16124
16125 let function = self.parse_task_function()?;
16126
16127 let args = if self.check(&TokenType::LParen) {
16129 self.parse_call_arguments()?
16130 } else if self.check_word("with") {
16131 self.advance(); self.parse_call_arguments()?
16133 } else {
16134 Vec::new()
16135 };
16136
16137 Ok(Stmt::LaunchTask { function, args })
16138 }
16139
16140 fn parse_task_function(&mut self) -> ParseResult<Symbol> {
16144 if self.declared_function_names.contains(&self.peek().lexeme) {
16145 let name = self.peek().lexeme;
16146 self.advance();
16147 return Ok(name);
16148 }
16149 self.expect_identifier()
16150 }
16151
16152 fn parse_send_pipe_statement(&mut self) -> ParseResult<Stmt<'a>> {
16155 self.advance(); let value = self.parse_imperative_expr()?;
16159
16160 if !self.check(&TokenType::Into) {
16162 return Err(ParseError {
16163 kind: ParseErrorKind::ExpectedKeyword {
16164 keyword: "into".to_string(),
16165 },
16166 span: self.current_span(),
16167 });
16168 }
16169 self.advance();
16170
16171 let pipe = self.parse_imperative_expr()?;
16173
16174 Ok(Stmt::SendPipe { value, pipe })
16175 }
16176
16177 fn parse_receive_pipe_statement(&mut self) -> ParseResult<Stmt<'a>> {
16180 self.advance(); let var = self.expect_identifier()?;
16184
16185 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16187 return Err(ParseError {
16188 kind: ParseErrorKind::ExpectedKeyword {
16189 keyword: "from".to_string(),
16190 },
16191 span: self.current_span(),
16192 });
16193 }
16194 self.advance();
16195
16196 let pipe = self.parse_imperative_expr()?;
16198
16199 Ok(Stmt::ReceivePipe { var, pipe })
16200 }
16201
16202 fn parse_try_statement(&mut self) -> ParseResult<Stmt<'a>> {
16205 self.advance(); if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16209 return Err(ParseError {
16210 kind: ParseErrorKind::ExpectedKeyword {
16211 keyword: "to".to_string(),
16212 },
16213 span: self.current_span(),
16214 });
16215 }
16216 self.advance();
16217
16218 if self.check(&TokenType::Send) {
16220 self.advance(); let value = self.parse_imperative_expr()?;
16222
16223 if !self.check(&TokenType::Into) {
16224 return Err(ParseError {
16225 kind: ParseErrorKind::ExpectedKeyword {
16226 keyword: "into".to_string(),
16227 },
16228 span: self.current_span(),
16229 });
16230 }
16231 self.advance();
16232
16233 let pipe = self.parse_imperative_expr()?;
16234 Ok(Stmt::TrySendPipe {
16235 value,
16236 pipe,
16237 result: None,
16238 })
16239 } else if self.check(&TokenType::Receive) {
16240 self.advance(); let var = self.expect_identifier()?;
16243
16244 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16245 return Err(ParseError {
16246 kind: ParseErrorKind::ExpectedKeyword {
16247 keyword: "from".to_string(),
16248 },
16249 span: self.current_span(),
16250 });
16251 }
16252 self.advance();
16253
16254 let pipe = self.parse_imperative_expr()?;
16255 Ok(Stmt::TryReceivePipe { var, pipe })
16256 } else {
16257 Err(ParseError {
16258 kind: ParseErrorKind::ExpectedKeyword {
16259 keyword: "send or receive".to_string(),
16260 },
16261 span: self.current_span(),
16262 })
16263 }
16264 }
16265
16266 fn parse_escape_body(
16269 &mut self,
16270 ) -> ParseResult<(
16271 crate::intern::Symbol,
16272 crate::intern::Symbol,
16273 crate::token::Span,
16274 )> {
16275 let start_span = self.current_span();
16276 self.advance(); if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16280 return Err(ParseError {
16281 kind: ParseErrorKind::Custom(
16282 "Expected 'to' after 'Escape'. Syntax: Escape to Rust:".to_string(),
16283 ),
16284 span: self.current_span(),
16285 });
16286 }
16287 self.advance(); let language = match &self.peek().kind {
16291 TokenType::ProperName(sym) => {
16292 let s = *sym;
16293 self.advance();
16294 s
16295 }
16296 TokenType::Noun(sym) | TokenType::Adjective(sym) => {
16297 let s = *sym;
16298 self.advance();
16299 s
16300 }
16301 _ => {
16302 return Err(ParseError {
16303 kind: ParseErrorKind::Custom(
16304 "Expected language name after 'Escape to'. Currently only 'Rust' is supported.".to_string()
16305 ),
16306 span: self.current_span(),
16307 });
16308 }
16309 };
16310
16311 if !language.is(self.interner, "Rust") {
16313 let lang_str = self.interner.resolve(language);
16314 return Err(ParseError {
16315 kind: ParseErrorKind::Custom(format!(
16316 "Unsupported escape target '{}'. Only 'Rust' is supported.",
16317 lang_str
16318 )),
16319 span: self.current_span(),
16320 });
16321 }
16322
16323 if !self.check(&TokenType::Colon) {
16325 return Err(ParseError {
16326 kind: ParseErrorKind::Custom(
16327 "Expected ':' after 'Escape to Rust'. Syntax: Escape to Rust:".to_string(),
16328 ),
16329 span: self.current_span(),
16330 });
16331 }
16332 self.advance(); if !self.check(&TokenType::Indent) {
16336 return Err(ParseError {
16337 kind: ParseErrorKind::Custom(
16338 "Expected indented block after 'Escape to Rust:'.".to_string(),
16339 ),
16340 span: self.current_span(),
16341 });
16342 }
16343 self.advance(); let code = match &self.peek().kind {
16347 TokenType::EscapeBlock(sym) => {
16348 let s = *sym;
16349 self.advance();
16350 s
16351 }
16352 _ => {
16353 return Err(ParseError {
16354 kind: ParseErrorKind::Custom(
16355 "Escape block body is empty or malformed.".to_string(),
16356 ),
16357 span: self.current_span(),
16358 });
16359 }
16360 };
16361
16362 if self.check(&TokenType::Dedent) {
16364 self.advance();
16365 }
16366
16367 let end_span = self.previous().span;
16368 Ok((
16369 language,
16370 code,
16371 crate::token::Span::new(start_span.start, end_span.end),
16372 ))
16373 }
16374
16375 fn parse_escape_statement(&mut self) -> ParseResult<Stmt<'a>> {
16377 let (language, code, span) = self.parse_escape_body()?;
16378 Ok(Stmt::Escape {
16379 language,
16380 code,
16381 span,
16382 })
16383 }
16384
16385 fn parse_escape_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
16388 let (language, code, _span) = self.parse_escape_body()?;
16389 Ok(self
16390 .ctx
16391 .alloc_imperative_expr(Expr::Escape { language, code }))
16392 }
16393
16394 fn parse_requires_block(&mut self) -> ParseResult<Vec<Stmt<'a>>> {
16397 let mut deps = Vec::new();
16398
16399 loop {
16400 if self.is_at_end() {
16402 break;
16403 }
16404 if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
16405 break;
16406 }
16407
16408 if self.check(&TokenType::Indent)
16410 || self.check(&TokenType::Dedent)
16411 || self.check(&TokenType::Newline)
16412 {
16413 self.advance();
16414 continue;
16415 }
16416
16417 if matches!(self.peek().kind, TokenType::Article(_)) {
16419 let dep = self.parse_require_line()?;
16420 deps.push(dep);
16421 continue;
16422 }
16423
16424 self.advance();
16426 }
16427
16428 Ok(deps)
16429 }
16430
16431 fn parse_require_line(&mut self) -> ParseResult<Stmt<'a>> {
16434 let start_span = self.current_span();
16435
16436 if !matches!(self.peek().kind, TokenType::Article(_)) {
16438 return Err(crate::error::ParseError {
16439 kind: crate::error::ParseErrorKind::Custom(
16440 "Expected 'The' to begin a dependency declaration.".to_string(),
16441 ),
16442 span: self.current_span(),
16443 });
16444 }
16445 self.advance(); let crate_name = if let TokenType::StringLiteral(sym) = self.peek().kind {
16449 let s = sym;
16450 self.advance();
16451 s
16452 } else {
16453 return Err(crate::error::ParseError {
16454 kind: crate::error::ParseErrorKind::Custom(
16455 "Expected a string literal for the crate name, e.g. \"serde\".".to_string(),
16456 ),
16457 span: self.current_span(),
16458 });
16459 };
16460
16461 if !self.check_word("crate") {
16463 return Err(crate::error::ParseError {
16464 kind: crate::error::ParseErrorKind::Custom(
16465 "Expected the word 'crate' after the crate name.".to_string(),
16466 ),
16467 span: self.current_span(),
16468 });
16469 }
16470 self.advance(); if !self.check_word("version") {
16474 return Err(crate::error::ParseError {
16475 kind: crate::error::ParseErrorKind::Custom(
16476 "Expected 'version' after 'crate'.".to_string(),
16477 ),
16478 span: self.current_span(),
16479 });
16480 }
16481 self.advance(); let version = if let TokenType::StringLiteral(sym) = self.peek().kind {
16485 let s = sym;
16486 self.advance();
16487 s
16488 } else {
16489 return Err(crate::error::ParseError {
16490 kind: crate::error::ParseErrorKind::Custom(
16491 "Expected a string literal for the version, e.g. \"1.0\".".to_string(),
16492 ),
16493 span: self.current_span(),
16494 });
16495 };
16496
16497 let mut features = Vec::new();
16499 if self.check_preposition_is("with") {
16500 self.advance(); if !self.check_word("features") {
16504 return Err(crate::error::ParseError {
16505 kind: crate::error::ParseErrorKind::Custom(
16506 "Expected 'features' after 'with'.".to_string(),
16507 ),
16508 span: self.current_span(),
16509 });
16510 }
16511 self.advance(); if let TokenType::StringLiteral(sym) = self.peek().kind {
16515 features.push(sym);
16516 self.advance();
16517 } else {
16518 return Err(crate::error::ParseError {
16519 kind: crate::error::ParseErrorKind::Custom(
16520 "Expected a string literal for a feature name.".to_string(),
16521 ),
16522 span: self.current_span(),
16523 });
16524 }
16525
16526 while self.check(&TokenType::And) {
16528 self.advance(); if let TokenType::StringLiteral(sym) = self.peek().kind {
16530 features.push(sym);
16531 self.advance();
16532 } else {
16533 return Err(crate::error::ParseError {
16534 kind: crate::error::ParseErrorKind::Custom(
16535 "Expected a string literal for a feature name after 'and'.".to_string(),
16536 ),
16537 span: self.current_span(),
16538 });
16539 }
16540 }
16541 }
16542
16543 if self.check(&TokenType::For) {
16545 self.advance(); while !self.check(&TokenType::Period)
16547 && !self.check(&TokenType::EOF)
16548 && !self.check(&TokenType::Newline)
16549 && !matches!(self.peek().kind, TokenType::BlockHeader { .. })
16550 {
16551 self.advance();
16552 }
16553 }
16554
16555 if self.check(&TokenType::Period) {
16557 self.advance();
16558 }
16559
16560 let end_span = self.previous().span;
16561
16562 Ok(Stmt::Require {
16563 crate_name,
16564 version,
16565 features,
16566 span: crate::token::Span::new(start_span.start, end_span.end),
16567 })
16568 }
16569
16570 fn parse_stop_statement(&mut self) -> ParseResult<Stmt<'a>> {
16573 self.advance(); let handle = self.parse_imperative_expr()?;
16576
16577 Ok(Stmt::StopTask { handle })
16578 }
16579
16580 fn lookahead_is_task_await(&self) -> bool {
16585 if self.check_word_at(self.current + 1, "response")
16588 || self.check_word_at(self.current + 1, "view")
16589 || self.check_word_at(self.current + 1, "viewed")
16590 || self.check_word_at(self.current + 1, "stream")
16591 {
16592 return false;
16593 }
16594 for i in (self.current + 1)..std::cmp::min(self.current + 5, self.tokens.len()) {
16596 let t = &self.tokens[i];
16597 if matches!(t.kind, TokenType::From)
16598 || self.interner.resolve(t.lexeme).eq_ignore_ascii_case("from")
16599 {
16600 return false;
16601 }
16602 }
16603 true
16604 }
16605
16606 fn check_word_at(&self, idx: usize, word: &str) -> bool {
16608 self.tokens
16609 .get(idx)
16610 .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case(word))
16611 .unwrap_or(false)
16612 }
16613
16614 fn parse_task_await_statement(&mut self) -> ParseResult<Stmt<'a>> {
16616 self.advance(); self.finish_task_await()
16618 }
16619
16620 fn parse_wait_statement(&mut self) -> ParseResult<Stmt<'a>> {
16623 self.advance(); if self.check(&TokenType::For) || self.check_preposition_is("for") || self.check_word("for")
16625 {
16626 self.advance(); }
16628 self.finish_task_await()
16629 }
16630
16631 fn finish_task_await(&mut self) -> ParseResult<Stmt<'a>> {
16634 let handle = self.parse_imperative_expr()?;
16635 let result = if self.check(&TokenType::Into) || self.check_word("into") {
16636 self.advance(); Some(self.expect_identifier()?)
16638 } else {
16639 None
16640 };
16641 Ok(Stmt::AwaitTask { handle, result })
16642 }
16643
16644 fn parse_close_statement(&mut self) -> ParseResult<Stmt<'a>> {
16646 self.advance(); if self.check_article() {
16649 self.advance();
16650 }
16651 if self.check(&TokenType::Pipe) || self.check_word("channel") {
16652 self.advance();
16653 }
16654 let channel = self.parse_imperative_expr()?;
16655 Ok(Stmt::CloseChannel { channel })
16656 }
16657
16658 fn parse_select_statement(&mut self) -> ParseResult<Stmt<'a>> {
16666 use crate::ast::stmt::SelectBranch;
16667
16668 self.advance(); if !self.check_article() {
16672 return Err(ParseError {
16673 kind: ParseErrorKind::ExpectedKeyword {
16674 keyword: "the".to_string(),
16675 },
16676 span: self.current_span(),
16677 });
16678 }
16679 self.advance();
16680
16681 if !self.check_word("first") {
16683 return Err(ParseError {
16684 kind: ParseErrorKind::ExpectedKeyword {
16685 keyword: "first".to_string(),
16686 },
16687 span: self.current_span(),
16688 });
16689 }
16690 self.advance();
16691
16692 if !self.check_preposition_is("of") {
16694 return Err(ParseError {
16695 kind: ParseErrorKind::ExpectedKeyword {
16696 keyword: "of".to_string(),
16697 },
16698 span: self.current_span(),
16699 });
16700 }
16701 self.advance();
16702
16703 if !self.check(&TokenType::Colon) {
16705 return Err(ParseError {
16706 kind: ParseErrorKind::ExpectedKeyword {
16707 keyword: ":".to_string(),
16708 },
16709 span: self.current_span(),
16710 });
16711 }
16712 self.advance();
16713
16714 if !self.check(&TokenType::Indent) {
16716 return Err(ParseError {
16717 kind: ParseErrorKind::ExpectedStatement,
16718 span: self.current_span(),
16719 });
16720 }
16721 self.advance();
16722
16723 let mut branches = Vec::new();
16725 while !self.check(&TokenType::Dedent) && !self.is_at_end() {
16726 let branch = self.parse_select_branch()?;
16727 branches.push(branch);
16728 }
16729
16730 if self.check(&TokenType::Dedent) {
16732 self.advance();
16733 }
16734
16735 Ok(Stmt::Select { branches })
16736 }
16737
16738 fn parse_select_branch(&mut self) -> ParseResult<crate::ast::stmt::SelectBranch<'a>> {
16740 use crate::ast::stmt::SelectBranch;
16741
16742 if self.check(&TokenType::Receive) {
16743 self.advance(); let var = match &self.tokens[self.current].kind {
16746 TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
16747 let s = *sym;
16748 self.advance();
16749 s
16750 }
16751 _ => {
16752 return Err(ParseError {
16753 kind: ParseErrorKind::ExpectedKeyword {
16754 keyword: "variable name".to_string(),
16755 },
16756 span: self.current_span(),
16757 });
16758 }
16759 };
16760
16761 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16762 return Err(ParseError {
16763 kind: ParseErrorKind::ExpectedKeyword {
16764 keyword: "from".to_string(),
16765 },
16766 span: self.current_span(),
16767 });
16768 }
16769 self.advance();
16770
16771 let pipe = self.parse_imperative_expr()?;
16772
16773 if !self.check(&TokenType::Colon) {
16775 return Err(ParseError {
16776 kind: ParseErrorKind::ExpectedKeyword {
16777 keyword: ":".to_string(),
16778 },
16779 span: self.current_span(),
16780 });
16781 }
16782 self.advance();
16783
16784 let body = self.parse_indented_block()?;
16786
16787 Ok(SelectBranch::Receive { var, pipe, body })
16788 } else if self.check_word("after") {
16789 self.advance(); let milliseconds = self.parse_imperative_expr()?;
16792
16793 if self.check_word("seconds") || self.check_word("milliseconds") {
16795 self.advance();
16796 }
16797
16798 if !self.check(&TokenType::Colon) {
16800 return Err(ParseError {
16801 kind: ParseErrorKind::ExpectedKeyword {
16802 keyword: ":".to_string(),
16803 },
16804 span: self.current_span(),
16805 });
16806 }
16807 self.advance();
16808
16809 let body = self.parse_indented_block()?;
16811
16812 Ok(SelectBranch::Timeout { milliseconds, body })
16813 } else {
16814 Err(ParseError {
16815 kind: ParseErrorKind::ExpectedKeyword {
16816 keyword: "Receive or After".to_string(),
16817 },
16818 span: self.current_span(),
16819 })
16820 }
16821 }
16822
16823 fn parse_indented_block(&mut self) -> ParseResult<crate::ast::stmt::Block<'a>> {
16827 self.enter_recursion()?;
16828 let result = self.parse_indented_block_inner();
16829 self.leave_recursion();
16830 result
16831 }
16832
16833 fn parse_indented_block_inner(&mut self) -> ParseResult<crate::ast::stmt::Block<'a>> {
16834 if !self.check(&TokenType::Indent) {
16836 return Err(ParseError {
16837 kind: ParseErrorKind::ExpectedStatement,
16838 span: self.current_span(),
16839 });
16840 }
16841 self.advance();
16842
16843 let list = self.read_scoped(|parser| {
16844 let mut stmts = StmtList::default();
16845 while !parser.check(&TokenType::Dedent) && !parser.is_at_end() {
16846 let start_tok = parser.current;
16847 let mut read = StmtList::default();
16848 parser.read_block_statement(&mut read)?;
16849 if let Some((component, action)) = parser.site_tracking {
16857 let span = parser.span_from(start_tok);
16858 let id = parser.failure_sites.len() as u32;
16859 parser.failure_sites.push(crate::ast::stmt::FailureSite {
16860 span,
16861 component,
16862 action,
16863 });
16864 stmts.wrote(Stmt::Site(id));
16865 }
16866 stmts.append(read);
16867 }
16868
16869 if parser.check(&TokenType::Dedent) {
16871 parser.advance();
16872 }
16873 Ok(stmts)
16874 })?;
16875
16876 Ok(self.alloc_read_block(list))
16877 }
16878
16879 fn parse_give_statement(&mut self) -> ParseResult<Stmt<'a>> {
16880 self.advance(); let object = self.parse_imperative_expr()?;
16884
16885 if !self.check_to_preposition() {
16887 return Err(ParseError {
16888 kind: ParseErrorKind::ExpectedKeyword {
16889 keyword: "to".to_string(),
16890 },
16891 span: self.current_span(),
16892 });
16893 }
16894 self.advance(); let recipient = self.parse_imperative_expr()?;
16898
16899 Ok(Stmt::Give { object, recipient })
16902 }
16903
16904 fn parse_show_statement(&mut self) -> ParseResult<Stmt<'a>> {
16905 self.advance(); let object = self.parse_condition()?;
16910
16911 let recipient = if self.check_to_preposition() {
16915 self.advance(); if self.check_article() {
16920 self.advance(); }
16922 if self.check(&TokenType::Console) {
16923 self.advance(); let show_sym = self.interner.intern("show");
16925 self.ctx.alloc_imperative_expr(Expr::Identifier(show_sym))
16926 } else {
16927 self.parse_imperative_expr()?
16929 }
16930 } else {
16931 let show_sym = self.interner.intern("show");
16933 self.ctx.alloc_imperative_expr(Expr::Identifier(show_sym))
16934 };
16935
16936 if let Expr::Identifier(sym) = object {
16938 self.world_state
16939 .set_ownership_by_var(*sym, crate::drs::OwnershipState::Borrowed);
16940 }
16941
16942 Ok(Stmt::Show { object, recipient })
16943 }
16944
16945 fn parse_push_statement(&mut self) -> ParseResult<Stmt<'a>> {
16948 self.advance(); let mut values = vec![self.parse_imperative_expr()?];
16953 while self.check(&TokenType::Comma) {
16954 self.advance(); values.push(self.parse_imperative_expr()?);
16956 }
16957
16958 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16960 return Err(ParseError {
16961 kind: ParseErrorKind::ExpectedKeyword {
16962 keyword: "to".to_string(),
16963 },
16964 span: self.current_span(),
16965 });
16966 }
16967 self.advance(); let collection_from = self.current;
16971 let collection = self.parse_imperative_expr()?;
16972 self.note_write(
16973 collection,
16974 collection_from,
16975 self.current,
16976 component::WriteVerb::Push,
16977 );
16978
16979 if values.len() == 1 {
16980 Ok(self.desugar_place_push(values[0], collection))
16981 } else {
16982 let mut pushes = Vec::with_capacity(values.len());
16983 for value in values {
16984 pushes.push(self.desugar_place_push(value, collection));
16985 }
16986 let body = self.alloc_block(pushes);
16987 Ok(Stmt::Splice { body })
16988 }
16989 }
16990
16991 fn parse_pop_statement(&mut self) -> ParseResult<Stmt<'a>> {
16994 self.advance(); if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16998 return Err(ParseError {
16999 kind: ParseErrorKind::ExpectedKeyword {
17000 keyword: "from".to_string(),
17001 },
17002 span: self.current_span(),
17003 });
17004 }
17005 self.advance(); let collection_from = self.current;
17009 let collection = self.parse_imperative_expr()?;
17010 self.note_write(
17011 collection,
17012 collection_from,
17013 self.current,
17014 component::WriteVerb::Pop,
17015 );
17016
17017 let into = if self.check(&TokenType::Into) || self.check_preposition_is("into") {
17019 self.advance(); if let TokenType::Noun(sym) | TokenType::ProperName(sym) = &self.peek().kind {
17023 let sym = *sym;
17024 self.advance();
17025 Some(sym)
17026 } else if let Some(token) = self.tokens.get(self.current) {
17027 let sym = token.lexeme;
17029 self.advance();
17030 Some(sym)
17031 } else {
17032 return Err(ParseError {
17033 kind: ParseErrorKind::ExpectedIdentifier,
17034 span: self.current_span(),
17035 });
17036 }
17037 } else {
17038 None
17039 };
17040
17041 Ok(Stmt::Pop { collection, into })
17042 }
17043
17044 fn parse_add_statement(&mut self) -> ParseResult<Stmt<'a>> {
17047 self.advance(); let value = self.parse_imperative_expr()?;
17051
17052 if !self.check_preposition_is("to") && !self.check(&TokenType::To) {
17054 return Err(ParseError {
17055 kind: ParseErrorKind::ExpectedKeyword {
17056 keyword: "to".to_string(),
17057 },
17058 span: self.current_span(),
17059 });
17060 }
17061 self.advance(); let collection_from = self.current;
17065 let collection = self.parse_imperative_expr()?;
17066 self.note_write(
17067 collection,
17068 collection_from,
17069 self.current,
17070 component::WriteVerb::Add,
17071 );
17072
17073 Ok(Stmt::Add { value, collection })
17074 }
17075
17076 fn parse_remove_statement(&mut self) -> ParseResult<Stmt<'a>> {
17079 self.advance(); if self.check_word("every") && self.check_word_at(self.current + 2, "from") {
17085 let saved = self.backtrack_point();
17086 self.advance(); if let Ok(binder) = self.expect_identifier() {
17088 self.advance(); let list_from = self.current;
17090 let list = self.parse_imperative_expr()?;
17091 let list_to = self.current;
17092 self.expect_keyword("where")?;
17093 let condition = self.parse_condition()?;
17094 let (kept, mut body) = self.desugar_keep_where(binder, list, condition, false);
17095 let kept_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
17096 let write_back = match list {
17097 Expr::Identifier(target) => {
17098 self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17099 Stmt::Set { target: *target, value: kept_ref }
17100 }
17101 Expr::FieldAccess { object, field } => {
17102 self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17103 Stmt::SetField { object: *object, field: *field, value: kept_ref }
17104 }
17105 Expr::Index { collection, index } => {
17106 self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17107 self.desugar_place_set_index(*collection, *index, kept_ref)
17108 }
17109 _ => {
17110 return Err(ParseError {
17111 kind: ParseErrorKind::Custom(
17112 "`Remove every … from` takes items out of a list it can write back to — \
17113 a name, a field (`the cart's items`) or an item (`item 2 of lists`)."
17114 .to_string(),
17115 ),
17116 span: self.tokens[list_from].span,
17117 })
17118 }
17119 };
17120 body.push(write_back);
17121 let body = self.alloc_block(body);
17122 return Ok(Stmt::Splice { body });
17123 }
17124 self.backtrack_to(&saved);
17125 }
17126
17127 let value = self.parse_imperative_expr()?;
17129
17130 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17132 return Err(ParseError {
17133 kind: ParseErrorKind::ExpectedKeyword {
17134 keyword: "from".to_string(),
17135 },
17136 span: self.current_span(),
17137 });
17138 }
17139 self.advance(); let collection_from = self.current;
17143 let collection = self.parse_imperative_expr()?;
17144 self.note_write(
17145 collection,
17146 collection_from,
17147 self.current,
17148 component::WriteVerb::Remove,
17149 );
17150
17151 Ok(Stmt::Remove { value, collection })
17152 }
17153
17154 fn parse_read_statement(&mut self) -> ParseResult<Stmt<'a>> {
17158 self.advance(); let var = self.expect_identifier()?;
17162
17163 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17165 return Err(ParseError {
17166 kind: ParseErrorKind::ExpectedKeyword {
17167 keyword: "from".to_string(),
17168 },
17169 span: self.current_span(),
17170 });
17171 }
17172 self.advance(); if self.check_article() {
17176 self.advance();
17177 }
17178
17179 let source = if self.check(&TokenType::Console) {
17181 self.advance(); ReadSource::Console
17183 } else if self.check(&TokenType::File) {
17184 self.advance(); let path = self.parse_imperative_expr()?;
17186 ReadSource::File(path)
17187 } else {
17188 return Err(ParseError {
17189 kind: ParseErrorKind::ExpectedKeyword {
17190 keyword: "console or file".to_string(),
17191 },
17192 span: self.current_span(),
17193 });
17194 };
17195
17196 Ok(Stmt::ReadFrom { var, source })
17197 }
17198
17199 fn parse_write_statement(&mut self) -> ParseResult<Stmt<'a>> {
17202 self.advance(); let content = self.parse_imperative_expr()?;
17206
17207 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
17209 return Err(ParseError {
17210 kind: ParseErrorKind::ExpectedKeyword {
17211 keyword: "to".to_string(),
17212 },
17213 span: self.current_span(),
17214 });
17215 }
17216 self.advance(); if !self.check(&TokenType::File) {
17220 return Err(ParseError {
17221 kind: ParseErrorKind::ExpectedKeyword {
17222 keyword: "file".to_string(),
17223 },
17224 span: self.current_span(),
17225 });
17226 }
17227 self.advance(); let path = self.parse_imperative_expr()?;
17231
17232 Ok(Stmt::WriteFile { content, path })
17233 }
17234
17235 fn parse_zone_statement(&mut self) -> ParseResult<Stmt<'a>> {
17241 self.advance(); if self.check_article() {
17245 self.advance();
17246 }
17247
17248 if self.check(&TokenType::New) {
17250 self.advance();
17251 }
17252
17253 if !self.check(&TokenType::Zone) {
17255 return Err(ParseError {
17256 kind: ParseErrorKind::ExpectedKeyword {
17257 keyword: "zone".to_string(),
17258 },
17259 span: self.current_span(),
17260 });
17261 }
17262 self.advance(); if !self.check(&TokenType::Called) {
17266 return Err(ParseError {
17267 kind: ParseErrorKind::ExpectedKeyword {
17268 keyword: "called".to_string(),
17269 },
17270 span: self.current_span(),
17271 });
17272 }
17273 self.advance(); let name = match &self.peek().kind {
17277 TokenType::StringLiteral(sym) => {
17278 let s = *sym;
17279 self.advance();
17280 s
17281 }
17282 TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
17283 let s = *sym;
17284 self.advance();
17285 s
17286 }
17287 _ => {
17288 let token = self.peek().clone();
17290 self.advance();
17291 token.lexeme
17292 }
17293 };
17294
17295 let mut capacity = None;
17296 let mut source_file = None;
17297
17298 if self.check(&TokenType::Mapped) {
17300 self.advance(); if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17304 return Err(ParseError {
17305 kind: ParseErrorKind::ExpectedKeyword {
17306 keyword: "from".to_string(),
17307 },
17308 span: self.current_span(),
17309 });
17310 }
17311 self.advance(); if let TokenType::StringLiteral(path) = &self.peek().kind {
17316 let p = *path;
17317 self.advance();
17318 source_file = Some(crate::ast::stmt::ZoneSource::Literal(p));
17319 } else {
17320 let var = self.expect_identifier()?;
17321 source_file = Some(crate::ast::stmt::ZoneSource::Variable(var));
17322 }
17323 }
17324 else if self.check_of_preposition() {
17326 self.advance(); if !self.check(&TokenType::Size) {
17330 return Err(ParseError {
17331 kind: ParseErrorKind::ExpectedKeyword {
17332 keyword: "size".to_string(),
17333 },
17334 span: self.current_span(),
17335 });
17336 }
17337 self.advance(); let size_value = match &self.peek().kind {
17341 TokenType::Number(sym) => {
17342 let num_str = self.interner.resolve(*sym).to_string();
17343 let val = self.parse_i64_numeral(&num_str)? as usize;
17344 self.advance();
17345 val
17346 }
17347 TokenType::Cardinal(n) => {
17348 let val = *n as usize;
17349 self.advance();
17350 val
17351 }
17352 _ => {
17353 return Err(ParseError {
17354 kind: ParseErrorKind::ExpectedNumber,
17355 span: self.current_span(),
17356 });
17357 }
17358 };
17359
17360 let unit_multiplier = self.parse_size_unit()?;
17362 capacity = Some(size_value * unit_multiplier);
17363 }
17364
17365 if !self.check(&TokenType::Colon) {
17367 return Err(ParseError {
17368 kind: ParseErrorKind::ExpectedKeyword {
17369 keyword: ":".to_string(),
17370 },
17371 span: self.current_span(),
17372 });
17373 }
17374 self.advance(); let body = self.read_indented_block()?;
17377
17378 Ok(Stmt::Zone {
17379 name,
17380 capacity,
17381 source_file,
17382 body,
17383 })
17384 }
17385
17386 fn parse_size_unit(&mut self) -> ParseResult<usize> {
17388 let token = self.peek().clone();
17389 let unit_str = self.interner.resolve(token.lexeme).to_uppercase();
17390 self.advance();
17391
17392 match unit_str.as_str() {
17393 "B" | "BYTES" | "BYTE" => Ok(1),
17394 "KB" | "KILOBYTE" | "KILOBYTES" => Ok(1024),
17395 "MB" | "MEGABYTE" | "MEGABYTES" => Ok(1024 * 1024),
17396 "GB" | "GIGABYTE" | "GIGABYTES" => Ok(1024 * 1024 * 1024),
17397 _ => Err(ParseError {
17398 kind: ParseErrorKind::ExpectedKeyword {
17399 keyword: "size unit (B, KB, MB, GB)".to_string(),
17400 },
17401 span: token.span,
17402 }),
17403 }
17404 }
17405
17406 fn parse_concurrent_block(&mut self) -> ParseResult<Stmt<'a>> {
17415 self.advance(); if !self.check(&TokenType::All) {
17419 return Err(ParseError {
17420 kind: ParseErrorKind::ExpectedKeyword {
17421 keyword: "all".to_string(),
17422 },
17423 span: self.current_span(),
17424 });
17425 }
17426 self.advance(); if !self.check_of_preposition() {
17430 return Err(ParseError {
17431 kind: ParseErrorKind::ExpectedKeyword {
17432 keyword: "of".to_string(),
17433 },
17434 span: self.current_span(),
17435 });
17436 }
17437 self.advance(); if !self.check_article() {
17441 return Err(ParseError {
17442 kind: ParseErrorKind::ExpectedKeyword {
17443 keyword: "the".to_string(),
17444 },
17445 span: self.current_span(),
17446 });
17447 }
17448 self.advance(); if !self.check(&TokenType::Following) {
17452 return Err(ParseError {
17453 kind: ParseErrorKind::ExpectedKeyword {
17454 keyword: "following".to_string(),
17455 },
17456 span: self.current_span(),
17457 });
17458 }
17459 self.advance(); if !self.check(&TokenType::Colon) {
17463 return Err(ParseError {
17464 kind: ParseErrorKind::ExpectedKeyword {
17465 keyword: ":".to_string(),
17466 },
17467 span: self.current_span(),
17468 });
17469 }
17470 self.advance(); let tasks = self.read_indented_block()?;
17473
17474 Ok(Stmt::Concurrent { tasks })
17475 }
17476
17477 fn parse_parallel_block(&mut self) -> ParseResult<Stmt<'a>> {
17486 self.advance(); if !self.check(&TokenType::Colon) {
17490 return Err(ParseError {
17491 kind: ParseErrorKind::ExpectedKeyword {
17492 keyword: ":".to_string(),
17493 },
17494 span: self.current_span(),
17495 });
17496 }
17497 self.advance(); let tasks = self.read_indented_block()?;
17500
17501 Ok(Stmt::Parallel { tasks })
17502 }
17503
17504 fn parse_reversibly_block(&mut self) -> ParseResult<Stmt<'a>> {
17516 self.advance(); if !self.check(&TokenType::Colon) {
17520 return Err(ParseError {
17521 kind: ParseErrorKind::ExpectedKeyword {
17522 keyword: ":".to_string(),
17523 },
17524 span: self.current_span(),
17525 });
17526 }
17527 self.advance(); while self.check(&TokenType::Newline) {
17531 self.advance();
17532 }
17533
17534 let body = self.read_indented_block()?;
17535
17536 Ok(Stmt::Reversibly { body })
17537 }
17538
17539 fn parse_inspect_statement(&mut self) -> ParseResult<Stmt<'a>> {
17546 self.advance(); let target = self.parse_imperative_expr()?;
17550
17551 if !self.check(&TokenType::Colon) {
17553 return Err(ParseError {
17554 kind: ParseErrorKind::ExpectedKeyword {
17555 keyword: ":".to_string(),
17556 },
17557 span: self.current_span(),
17558 });
17559 }
17560 self.advance(); if !self.check(&TokenType::Indent) {
17564 return Err(ParseError {
17565 kind: ParseErrorKind::ExpectedStatement,
17566 span: self.current_span(),
17567 });
17568 }
17569 self.advance(); let mut arms = Vec::new();
17572 let mut has_otherwise = false;
17573 let mut established_enum: Option<(Symbol, Symbol)> = None;
17574
17575 while !self.check(&TokenType::Dedent) && !self.is_at_end() {
17577 if self.check(&TokenType::Otherwise) {
17578 self.advance(); if !self.check(&TokenType::Colon) {
17582 return Err(ParseError {
17583 kind: ParseErrorKind::ExpectedKeyword {
17584 keyword: ":".to_string(),
17585 },
17586 span: self.current_span(),
17587 });
17588 }
17589 self.advance(); let body = self.read_branch_body()?;
17593
17594 arms.push(MatchArm {
17595 enum_name: None,
17596 variant: None,
17597 bindings: vec![],
17598 body,
17599 });
17600 has_otherwise = true;
17601 break;
17602 }
17603
17604 if self.check(&TokenType::If) {
17605 let arm_span = self.peek().span;
17607 let arm = self.parse_match_arm(established_enum.map(|(e, _)| e))?;
17608 self.check_inspect_arm_enum(&arm, arm_span, &mut established_enum)?;
17609 arms.push(arm);
17610 } else if self.check(&TokenType::When) || self.check_word("When") {
17611 let arm_span = self.peek().span;
17613 let arm = self.parse_when_arm(established_enum.map(|(e, _)| e))?;
17614 self.check_inspect_arm_enum(&arm, arm_span, &mut established_enum)?;
17615 arms.push(arm);
17616 } else if self.check(&TokenType::Newline) {
17617 self.advance();
17619 } else {
17620 self.advance();
17622 }
17623 }
17624
17625 if self.check(&TokenType::Dedent) {
17627 self.advance();
17628 }
17629
17630 Ok(Stmt::Inspect {
17631 target,
17632 arms,
17633 has_otherwise,
17634 })
17635 }
17636
17637 fn check_inspect_arm_enum(
17658 &self,
17659 arm: &MatchArm<'a>,
17660 arm_span: Span,
17661 established: &mut Option<(Symbol, Symbol)>,
17662 ) -> ParseResult<()> {
17663 let Some(variant) = arm.variant else {
17664 return Ok(());
17665 };
17666 let (expected_enum, first_variant) = match (*established, arm.enum_name) {
17667 (None, None) => return Ok(()),
17668 (None, Some(found_enum)) => {
17669 *established = Some((found_enum, variant));
17670 return Ok(());
17671 }
17672 (Some((expected_enum, _)), Some(found_enum)) if expected_enum == found_enum => {
17673 return Ok(())
17674 }
17675 (Some(pair), _) => pair,
17676 };
17677 let expected_str = self.interner.resolve(expected_enum).to_string();
17678 let variant_str = self.interner.resolve(variant).to_string();
17679 let first_variant_str = self.interner.resolve(first_variant).to_string();
17680 let suggestion = self
17681 .type_registry
17682 .as_ref()
17683 .and_then(|r| match r.get(expected_enum) {
17684 Some(crate::analysis::TypeDef::Enum { variants, .. }) => {
17685 let names: Vec<&str> = variants
17686 .iter()
17687 .map(|v| self.interner.resolve(v.name))
17688 .collect();
17689 crate::suggest::find_similar(&variant_str, &names, 2)
17690 }
17691 _ => None,
17692 });
17693 let hint = suggestion
17694 .map(|s| format!(" Did you mean '{s}'?"))
17695 .unwrap_or_default();
17696 let belongs_to = match arm.enum_name {
17697 Some(found_enum) => format!(
17698 "is a variant of '{}', not",
17699 self.interner.resolve(found_enum)
17700 ),
17701 None => "is not a variant of".to_string(),
17702 };
17703 Err(ParseError {
17704 kind: ParseErrorKind::Custom(format!(
17705 "'{variant_str}' {belongs_to} '{expected_str}' — every arm of this Inspect must \
17706 match variants of the SAME type ('{expected_str}', established by its \
17707 '{first_variant_str}' arm).{hint}"
17708 )),
17709 span: arm_span,
17710 })
17711 }
17712
17713 fn resolve_arm_variant(
17724 &self,
17725 variant: Symbol,
17726 established: Option<Symbol>,
17727 ) -> (Option<Symbol>, Vec<Symbol>) {
17728 use crate::analysis::TypeDef;
17729 if let Some(enum_name) = established {
17730 if let Some(TypeDef::Enum { variants, .. }) =
17731 self.type_registry.as_ref().and_then(|r| r.get(enum_name))
17732 {
17733 if let Some(vdef) = variants.iter().find(|v| v.name == variant) {
17734 return (
17735 Some(enum_name),
17736 vdef.fields.iter().map(|f| f.name).collect(),
17737 );
17738 }
17739 }
17740 }
17741 self.type_registry
17742 .as_ref()
17743 .and_then(|r| {
17744 r.find_variant(variant).map(|(enum_name, vdef)| {
17745 (
17746 Some(enum_name),
17747 vdef.fields.iter().map(|f| f.name).collect(),
17748 )
17749 })
17750 })
17751 .unwrap_or((None, vec![]))
17752 }
17753
17754 fn parse_match_arm(&mut self, established: Option<Symbol>) -> ParseResult<MatchArm<'a>> {
17756 self.advance(); if !self.check_word("it") {
17760 return Err(ParseError {
17761 kind: ParseErrorKind::ExpectedKeyword {
17762 keyword: "it".to_string(),
17763 },
17764 span: self.current_span(),
17765 });
17766 }
17767 self.advance(); if !self.check(&TokenType::Is) {
17771 return Err(ParseError {
17772 kind: ParseErrorKind::ExpectedKeyword {
17773 keyword: "is".to_string(),
17774 },
17775 span: self.current_span(),
17776 });
17777 }
17778 self.advance(); if self.check_article() {
17782 self.advance();
17783 }
17784
17785 let variant_raw = self.peek().lexeme;
17790 self.expect_identifier()?;
17791 let variant = variant_raw;
17792
17793 let (enum_name, _fields) = self.resolve_arm_variant(variant, established);
17795
17796 let bindings = if self.check(&TokenType::LParen) {
17798 self.parse_pattern_bindings()?
17799 } else {
17800 vec![]
17801 };
17802
17803 if !self.check(&TokenType::Colon) {
17805 return Err(ParseError {
17806 kind: ParseErrorKind::ExpectedKeyword {
17807 keyword: ":".to_string(),
17808 },
17809 span: self.current_span(),
17810 });
17811 }
17812 self.advance(); let body = self.read_indented_block()?;
17815
17816 Ok(MatchArm {
17817 enum_name,
17818 variant: Some(variant),
17819 bindings,
17820 body,
17821 })
17822 }
17823
17824 fn parse_when_arm(&mut self, established: Option<Symbol>) -> ParseResult<MatchArm<'a>> {
17826 self.advance(); let variant_raw = self.peek().lexeme;
17831 self.expect_identifier()?;
17832 let variant = variant_raw;
17833
17834 let (enum_name, variant_fields) = self.resolve_arm_variant(variant, established);
17838
17839 let bindings = if self.check(&TokenType::LParen) {
17841 let raw_bindings = self.parse_when_bindings()?;
17842 raw_bindings
17844 .into_iter()
17845 .enumerate()
17846 .map(|(i, binding)| {
17847 let field = variant_fields.get(i).copied().unwrap_or(binding);
17848 (field, binding)
17849 })
17850 .collect()
17851 } else {
17852 vec![]
17853 };
17854
17855 if !self.check(&TokenType::Colon) {
17857 return Err(ParseError {
17858 kind: ParseErrorKind::ExpectedKeyword {
17859 keyword: ":".to_string(),
17860 },
17861 span: self.current_span(),
17862 });
17863 }
17864 self.advance(); let body = self.read_branch_body()?;
17868
17869 Ok(MatchArm {
17870 enum_name,
17871 variant: Some(variant),
17872 bindings,
17873 body,
17874 })
17875 }
17876
17877 fn parse_when_bindings(&mut self) -> ParseResult<Vec<Symbol>> {
17879 self.advance(); let mut bindings = Vec::new();
17881
17882 loop {
17883 let binding = self.expect_identifier()?;
17884 bindings.push(binding);
17885
17886 if !self.check(&TokenType::Comma) {
17887 break;
17888 }
17889 self.advance(); }
17891
17892 if !self.check(&TokenType::RParen) {
17893 return Err(ParseError {
17894 kind: ParseErrorKind::ExpectedKeyword {
17895 keyword: ")".to_string(),
17896 },
17897 span: self.current_span(),
17898 });
17899 }
17900 self.advance(); Ok(bindings)
17903 }
17904
17905 fn parse_pattern_bindings(&mut self) -> ParseResult<Vec<(Symbol, Symbol)>> {
17907 self.advance(); let mut bindings = Vec::new();
17909
17910 loop {
17911 let field = self.expect_identifier()?;
17912 let binding = if self.check(&TokenType::Colon) {
17913 self.advance(); self.expect_identifier()?
17915 } else {
17916 field };
17918 bindings.push((field, binding));
17919
17920 if !self.check(&TokenType::Comma) {
17921 break;
17922 }
17923 self.advance(); }
17925
17926 if !self.check(&TokenType::RParen) {
17927 return Err(ParseError {
17928 kind: ParseErrorKind::ExpectedKeyword {
17929 keyword: ")".to_string(),
17930 },
17931 span: self.current_span(),
17932 });
17933 }
17934 self.advance(); Ok(bindings)
17937 }
17938
17939 fn constructor_field_names(
17958 &self,
17959 type_name: Symbol,
17960 enum_hint: Option<Symbol>,
17961 ) -> Option<(Vec<Symbol>, bool)> {
17962 use crate::analysis::TypeDef;
17963 let registry = self.type_registry.as_ref()?;
17964 if let Some(enum_name) = enum_hint {
17965 if let Some(vdef) = registry.variant_in(enum_name, type_name) {
17966 return Some((vdef.fields.iter().map(|f| f.name).collect(), true));
17967 }
17968 }
17969 if let Some((_, vdef)) = registry.find_variant(type_name) {
17970 return Some((vdef.fields.iter().map(|f| f.name).collect(), true));
17971 }
17972 match registry.get(type_name) {
17973 Some(TypeDef::Struct { fields, .. }) => {
17974 Some((fields.iter().map(|f| f.name).collect(), false))
17975 }
17976 _ => None,
17977 }
17978 }
17979
17980 fn peek_field_name_at(&self, offset: usize) -> Option<Symbol> {
17984 let token = self.tokens.get(self.current + offset)?;
17985 if matches!(
17986 token.kind,
17987 TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
17988 ) {
17989 return None;
17990 }
17991 Some(token.lexeme)
17992 }
17993
17994 fn nested_constructor_naming(&self, value: &Expr<'a>, field: Symbol) -> Option<Symbol> {
17999 use crate::ast::Expr;
18000 let (inner_type, enum_hint) = match value {
18001 Expr::New { type_name, .. } => (*type_name, None),
18002 Expr::NewVariant {
18003 enum_name, variant, ..
18004 } => (*variant, Some(*enum_name)),
18005 _ => return None,
18006 };
18007 self.constructor_field_names(inner_type, enum_hint)?
18008 .0
18009 .contains(&field)
18010 .then_some(inner_type)
18011 }
18012
18013 fn any_enclosing_frame_wants(&self, frame_index: usize, field: Symbol) -> bool {
18021 self.constructor_frames[..frame_index]
18022 .iter()
18023 .any(|(ty, enum_hint, assigned)| {
18024 self.constructor_field_names(*ty, *enum_hint)
18025 .is_some_and(|(names, _)| names.contains(&field) && !assigned.contains(&field))
18026 })
18027 }
18028
18029 fn parse_constructor_fields(
18042 &mut self,
18043 type_name: Symbol,
18044 enum_hint: Option<Symbol>,
18045 ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18046 let with_span = self.peek().span;
18047 self.advance(); let frame_index = self.constructor_frames.len();
18050 self.constructor_frames
18051 .push((type_name, enum_hint, Vec::new()));
18052 let result =
18053 self.parse_constructor_fields_body(type_name, enum_hint, with_span, frame_index);
18054 self.constructor_frames.truncate(frame_index);
18055 result
18056 }
18057
18058 fn parse_constructor_fields_body(
18059 &mut self,
18060 type_name: Symbol,
18061 enum_hint: Option<Symbol>,
18062 with_span: Span,
18063 frame_index: usize,
18064 ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18065 use crate::ast::Expr;
18066 let mut fields: Vec<(Symbol, &'a Expr<'a>)> = Vec::new();
18067 let declared = self.constructor_field_names(type_name, enum_hint);
18068
18069 loop {
18070 let field_span = self.peek().span;
18071 let field_name = self.expect_identifier()?;
18072
18073 if fields.iter().any(|(n, _)| *n == field_name) {
18074 let field_str = self.interner.resolve(field_name);
18075 return Err(ParseError {
18076 kind: ParseErrorKind::Custom(format!(
18077 "'{}' names the field '{field_str}' twice in the same construction — a \
18078 field is assigned once. Remove one of the two '{field_str}' clauses.",
18079 self.interner.resolve(type_name)
18080 )),
18081 span: field_span,
18082 });
18083 }
18084
18085 let value = self.parse_comparison()?;
18088
18089 fields.push((field_name, value));
18090 self.constructor_frames[frame_index].2.push(field_name);
18091
18092 if self.check(&TokenType::And) {
18099 let continues_this_constructor = match &declared {
18100 Some((names, _)) => self.peek_field_name_at(1).is_some_and(|next| {
18101 if !names.contains(&next) {
18102 return false;
18103 }
18104 let already_assigned = fields.iter().any(|(n, _)| *n == next);
18105 !already_assigned || !self.any_enclosing_frame_wants(frame_index, next)
18106 }),
18107 None => true,
18108 };
18109 if continues_this_constructor {
18110 self.advance(); continue;
18112 }
18113 }
18114 break;
18115 }
18116
18117 if let Some((names, true)) = &declared {
18120 let missing: Vec<Symbol> = names
18121 .iter()
18122 .copied()
18123 .filter(|f| !fields.iter().any(|(n, _)| n == f))
18124 .collect();
18125 if !missing.is_empty() {
18126 let type_str = self.interner.resolve(type_name);
18127 let parts: Vec<String> = missing
18128 .iter()
18129 .map(|f| {
18130 let field_str = self.interner.resolve(*f);
18131 match fields.iter().find_map(|(_, v)| self.nested_constructor_naming(v, *f)) {
18132 Some(inner_type) => {
18133 let inner_str = self.interner.resolve(inner_type);
18134 format!(
18135 "'{field_str}' — a nested 'a new {inner_str}' assigned to another \
18136 field above also declares a field named '{field_str}' and may have \
18137 consumed the 'and {field_str} …' meant for '{type_str}' here; wrap \
18138 it in parentheses, '(a new {inner_str} with …)', to keep the two apart"
18139 )
18140 }
18141 None => format!("'{field_str}'"),
18142 }
18143 })
18144 .collect();
18145 let plural = if missing.len() == 1 {
18146 "field"
18147 } else {
18148 "fields"
18149 };
18150 return Err(ParseError {
18151 kind: ParseErrorKind::Custom(format!(
18152 "'{type_str}' needs a value for its {plural}: {}. Every field a constructor \
18153 declares must be given one, here and at every other 'a new {type_str}'.",
18154 parts.join("; ")
18155 )),
18156 span: with_span,
18157 });
18158 }
18159 }
18160
18161 Ok(fields)
18162 }
18163
18164 fn parse_variant_constructor_fields(
18168 &mut self,
18169 type_name: Symbol,
18170 enum_hint: Option<Symbol>,
18171 ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18172 self.parse_constructor_fields(type_name, enum_hint)
18173 }
18174
18175 fn option_form_of(
18185 &self,
18186 type_name: Symbol,
18187 type_args: &[TypeExpr],
18188 init_fields: &[(Symbol, &'a Expr<'a>)],
18189 ) -> Option<Expr<'a>> {
18190 if !type_args.is_empty() {
18191 return None;
18192 }
18193 match self.interner.resolve(type_name) {
18194 "Some"
18195 if init_fields.len() == 1 && self.interner.resolve(init_fields[0].0) == "value" =>
18196 {
18197 Some(Expr::OptionSome {
18198 value: init_fields[0].1,
18199 })
18200 }
18201 "None" if init_fields.is_empty() => Some(Expr::OptionNone),
18202 _ => None,
18203 }
18204 }
18205
18206 fn parse_struct_init_fields(
18209 &mut self,
18210 type_name: Symbol,
18211 ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18212 self.parse_constructor_fields(type_name, None)
18213 }
18214
18215 fn parse_generic_type_args(&mut self, type_name: Symbol) -> ParseResult<Vec<TypeExpr<'a>>> {
18219 if !self.is_generic_type(type_name) {
18221 return Ok(vec![]);
18222 }
18223
18224 if !self.check_preposition_is("of") {
18226 return Ok(vec![]); }
18228 self.advance(); let mut type_args = Vec::new();
18231 loop {
18232 let type_arg = self.parse_type_expression()?;
18234 type_args.push(type_arg);
18235
18236 if self.check(&TokenType::And) || self.check_to_preposition() {
18238 self.advance(); continue;
18240 }
18241 break;
18242 }
18243
18244 Ok(type_args)
18245 }
18246
18247 fn skip_type_def_content(&mut self) {
18251 while !self.is_at_end() {
18252 if matches!(
18254 self.tokens.get(self.current),
18255 Some(Token {
18256 kind: TokenType::BlockHeader { .. },
18257 ..
18258 })
18259 ) {
18260 break;
18261 }
18262 self.advance();
18263 }
18264 }
18265
18266 fn parse_theorem_block(&mut self) -> ParseResult<Stmt<'a>> {
18274 use crate::ast::theorem::{ProofStrategy, TheoremBlock};
18275
18276 self.skip_whitespace_tokens();
18278
18279 if self.check(&TokenType::Colon) {
18284 self.advance();
18285 }
18286
18287 self.skip_whitespace_tokens();
18289
18290 let name = if let Some(token) = self.tokens.get(self.current) {
18292 match &token.kind {
18293 TokenType::Noun(_)
18294 | TokenType::ProperName(_)
18295 | TokenType::Verb { .. }
18296 | TokenType::Adjective(_) => {
18297 let name = self.interner.resolve(token.lexeme).to_string();
18298 self.advance();
18299 name
18300 }
18301 _ => {
18302 let lexeme = self.interner.resolve(token.lexeme);
18304 if !lexeme.is_empty()
18305 && lexeme
18306 .chars()
18307 .next()
18308 .map(|c| c.is_alphanumeric())
18309 .unwrap_or(false)
18310 {
18311 let name = lexeme.to_string();
18312 self.advance();
18313 name
18314 } else {
18315 "Anonymous".to_string()
18316 }
18317 }
18318 }
18319 } else {
18320 "Anonymous".to_string()
18321 };
18322
18323 self.skip_whitespace_tokens();
18324
18325 if self.check(&TokenType::Period) {
18327 self.advance();
18328 }
18329
18330 self.skip_whitespace_tokens();
18331
18332 let mut premises = Vec::new();
18335 let mut premise_names: Vec<Option<String>> = Vec::new();
18336 while self.check(&TokenType::Given) {
18337 self.advance(); self.skip_whitespace_tokens();
18340
18341 let mut this_name: Option<String> = None;
18344 if self.check(&TokenType::LParen) {
18345 self.advance();
18346 self.skip_whitespace_tokens();
18347 if let Some(token) = self.tokens.get(self.current) {
18348 if !matches!(token.kind, TokenType::RParen) {
18349 this_name = Some(self.interner.resolve(token.lexeme).to_string());
18350 self.advance();
18351 }
18352 }
18353 self.skip_whitespace_tokens();
18354 if self.check(&TokenType::RParen) {
18355 self.advance();
18356 }
18357 self.skip_whitespace_tokens();
18358 }
18359 premise_names.push(this_name);
18360
18361 if self.check(&TokenType::Colon) {
18363 self.advance();
18364 }
18365
18366 self.skip_whitespace_tokens();
18367
18368 let scalar_trigger = self.pragmatic
18372 && matches!(
18373 self.peek().kind,
18374 TokenType::Some | TokenType::Most | TokenType::Many
18375 );
18376 let mut premise_expr = match self.try_parse_optative()? {
18381 Some(opt) => opt,
18382 None => self.parse_sentence()?,
18383 };
18384 if scalar_trigger {
18385 if let Some(strengthened) = self.scalar_implicature(premise_expr) {
18386 premise_expr = strengthened;
18387 }
18388 }
18389 premises.push(premise_expr);
18390
18391 self.world_state.end_sentence();
18394
18395 if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18398 self.advance();
18399 }
18400
18401 self.skip_whitespace_tokens();
18402 }
18403
18404 if let Some(conflict) = premise_label_conflict(&premise_names) {
18407 return Err(ParseError {
18408 kind: ParseErrorKind::Custom(conflict),
18409 span: self.current_span(),
18410 });
18411 }
18412
18413 let goal = if self.check(&TokenType::Prove) {
18415 self.advance(); if self.check(&TokenType::Colon) {
18418 self.advance();
18419 }
18420
18421 self.skip_whitespace_tokens();
18422
18423 let goal_expr = match self.try_parse_optative()? {
18426 Some(opt) => opt,
18427 None => self.parse_sentence()?,
18428 };
18429
18430 if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18431 self.advance();
18432 }
18433
18434 goal_expr
18435 } else {
18436 return Err(ParseError {
18437 kind: ParseErrorKind::ExpectedKeyword {
18438 keyword: "Prove".to_string(),
18439 },
18440 span: self.current_span(),
18441 });
18442 };
18443
18444 self.skip_whitespace_tokens();
18445
18446 if self.stray_premise_after_goal() {
18457 return Err(ParseError {
18458 kind: ParseErrorKind::Custom(
18459 "a `Given:` line appears BELOW the `Prove:` line. A theorem's premises \
18460 are read above its goal, so a `Given:` written after `Prove:` is not a \
18461 premise at all — the theorem would be built without it and then fail to \
18462 prove, which reads as a defect in the proof rather than in the layout. \
18463 Move every `Given:` line above the `Prove:` line."
18464 .to_string(),
18465 ),
18466 span: self.current_span(),
18467 });
18468 }
18469
18470 let at_proof = self.check(&TokenType::BlockHeader {
18474 block_type: crate::token::BlockType::Proof,
18475 }) || self.tokens.get(self.current).is_some_and(|t| {
18476 self.interner
18477 .resolve(t.lexeme)
18478 .eq_ignore_ascii_case("proof")
18479 });
18480 let strategy = if at_proof {
18481 self.advance();
18482 self.skip_whitespace_tokens();
18483
18484 if self.check(&TokenType::Colon) {
18485 self.advance();
18486 }
18487
18488 self.skip_whitespace_tokens();
18489
18490 if self.check(&TokenType::Auto) {
18491 self.advance();
18492 ProofStrategy::Auto
18493 } else {
18494 let mut parts: Vec<String> = Vec::new();
18500 while !self.is_at_end()
18501 && !matches!(
18502 self.tokens[self.current].kind,
18503 TokenType::BlockHeader { .. }
18504 )
18505 && !self.check(&TokenType::Dedent)
18506 {
18507 let lexeme = self.tokens[self.current].lexeme;
18508 parts.push(self.interner.resolve(lexeme).to_string());
18509 self.advance();
18510 }
18511 let script = parts.join(" ");
18512 if script.trim().is_empty() {
18513 ProofStrategy::Auto
18514 } else if let Some(variable) = proof_line_is_induction_on(&parts) {
18515 ProofStrategy::Induction(variable)
18516 } else if let Some(rule) = proof_line_names_one_rule(&parts) {
18517 ProofStrategy::ByRule(rule)
18518 } else {
18519 ProofStrategy::Script(lower_rule_applications(&parts).unwrap_or(script))
18520 }
18521 }
18522 } else {
18523 ProofStrategy::Auto
18525 };
18526
18527 if self.check(&TokenType::Period) {
18529 self.advance();
18530 }
18531
18532 let theorem = TheoremBlock {
18533 name,
18534 premises,
18535 premise_names,
18536 goal,
18537 strategy,
18538 };
18539
18540 Ok(Stmt::Theorem(theorem))
18541 }
18542
18543 fn parse_define_block(&mut self) -> ParseResult<Stmt<'a>> {
18550 use crate::ast::definition::DefinitionBlock;
18551
18552 self.skip_whitespace_tokens();
18553
18554 if self.check(&TokenType::Colon) {
18557 self.advance();
18558 self.skip_whitespace_tokens();
18559 if let Some(token) = self.tokens.get(self.current) {
18560 if matches!(
18561 token.kind,
18562 TokenType::Noun(_)
18563 | TokenType::ProperName(_)
18564 | TokenType::Adjective(_)
18565 | TokenType::Verb { .. }
18566 ) {
18567 self.advance();
18568 }
18569 }
18570 if self.check(&TokenType::Period) {
18571 self.advance();
18572 }
18573 }
18574
18575 self.skip_whitespace_tokens();
18576
18577 let sentence = self.parse_sentence()?;
18579 if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18580 self.advance();
18581 }
18582
18583 let (definiendum, definiens) = match sentence {
18585 LogicExpr::BinaryOp {
18586 left,
18587 op: TokenType::Iff,
18588 right,
18589 } => (*left, *right),
18590 _ => {
18591 return Err(ParseError {
18592 kind: ParseErrorKind::Custom(
18593 "a `## Define` body must be a biconditional: \
18594 `<subject> is <predicate> if and only if <definiens>`"
18595 .to_string(),
18596 ),
18597 span: self.current_span(),
18598 });
18599 }
18600 };
18601
18602 let (name, params) = match definiendum {
18605 LogicExpr::Predicate { name, args, .. } => {
18606 let pname = self.interner.resolve(*name).to_string();
18607 let params: Vec<Symbol> = args
18608 .iter()
18609 .filter_map(|t| match t {
18610 Term::Constant(s) | Term::Variable(s) => Some(*s),
18611 _ => None,
18612 })
18613 .collect();
18614 (pname, params)
18615 }
18616 _ => {
18617 return Err(ParseError {
18618 kind: ParseErrorKind::Custom(
18619 "the left side of a `## Define` biconditional must be a \
18620 predicate applied to its parameters (e.g. `x is a bachelor`)"
18621 .to_string(),
18622 ),
18623 span: self.current_span(),
18624 });
18625 }
18626 };
18627
18628 Ok(Stmt::Definition(DefinitionBlock {
18629 name,
18630 params,
18631 definiendum,
18632 definiens,
18633 }))
18634 }
18635
18636 fn parse_axiom_block(&mut self) -> ParseResult<Stmt<'a>> {
18642 use crate::ast::axiom::AxiomBlock;
18643 self.skip_whitespace_tokens();
18644 let name = self.read_block_name();
18645 self.skip_whitespace_tokens();
18646 if self.check(&TokenType::Colon) {
18647 self.advance();
18648 }
18649 let formula = self.collect_block_text();
18650 Ok(Stmt::Axiom(AxiomBlock { name, formula }))
18651 }
18652
18653 fn parse_theory_block(&mut self) -> ParseResult<Stmt<'a>> {
18657 use crate::ast::axiom::TheoryBlock;
18658 self.skip_whitespace_tokens();
18659 let name = self.read_block_name();
18660 self.skip_whitespace_tokens();
18661 if self.check(&TokenType::Colon) {
18662 self.advance();
18663 }
18664 let body = self.collect_block_text();
18665 Ok(Stmt::Theory(TheoryBlock { name, body }))
18666 }
18667
18668 fn read_block_name(&mut self) -> String {
18671 if let Some(token) = self.tokens.get(self.current) {
18672 if matches!(
18673 token.kind,
18674 TokenType::Noun(_)
18675 | TokenType::ProperName(_)
18676 | TokenType::Adjective(_)
18677 | TokenType::Verb { .. }
18678 ) {
18679 let name = self.interner.resolve(token.lexeme).to_string();
18680 self.advance();
18681 return name;
18682 }
18683 }
18684 "Anonymous".to_string()
18685 }
18686
18687 fn collect_block_text(&mut self) -> String {
18693 let mut parts: Vec<String> = Vec::new();
18694 while !self.is_at_end()
18695 && !matches!(
18696 self.tokens[self.current].kind,
18697 TokenType::BlockHeader { .. }
18698 )
18699 {
18700 match &self.tokens[self.current].kind {
18701 TokenType::Newline | TokenType::Indent | TokenType::Dedent => {}
18702 _ => {
18703 let lexeme = self.tokens[self.current].lexeme;
18704 parts.push(self.interner.resolve(lexeme).to_string());
18705 }
18706 }
18707 self.advance();
18708 }
18709 parts.join(" ")
18710 }
18711
18712 fn stray_premise_after_goal(&self) -> bool {
18720 let mut index = self.current;
18721 while let Some(token) = self.tokens.get(index) {
18722 match &token.kind {
18723 TokenType::BlockHeader { .. } | TokenType::EOF => return false,
18724 TokenType::Given if self.premise_colon_follows(index + 1) => return true,
18725 _ => {}
18726 }
18727 if self
18728 .interner
18729 .resolve(token.lexeme)
18730 .eq_ignore_ascii_case("proof")
18731 {
18732 return false;
18733 }
18734 index += 1;
18735 }
18736 false
18737 }
18738
18739 fn premise_colon_follows(&self, start: usize) -> bool {
18742 let mut index = start;
18743 let skip_layout = |index: &mut usize| {
18744 while matches!(
18745 self.tokens.get(*index).map(|t| &t.kind),
18746 Some(TokenType::Newline) | Some(TokenType::Indent) | Some(TokenType::Dedent)
18747 ) {
18748 *index += 1;
18749 }
18750 };
18751 skip_layout(&mut index);
18752 if matches!(
18753 self.tokens.get(index).map(|t| &t.kind),
18754 Some(TokenType::LParen)
18755 ) {
18756 while let Some(token) = self.tokens.get(index) {
18757 index += 1;
18758 if matches!(token.kind, TokenType::RParen) {
18759 break;
18760 }
18761 }
18762 skip_layout(&mut index);
18763 }
18764 matches!(
18765 self.tokens.get(index).map(|t| &t.kind),
18766 Some(TokenType::Colon)
18767 )
18768 }
18769
18770 fn skip_whitespace_tokens(&mut self) {
18771 while self.check(&TokenType::Newline)
18772 || self.check(&TokenType::Indent)
18773 || self.check(&TokenType::Dedent)
18774 {
18775 self.advance();
18776 }
18777 }
18778
18779 fn insert_ensures_before_returns(
18788 &mut self,
18789 stmts: StmtList<'a>,
18790 checks: &[&'a Expr<'a>],
18791 ) -> StmtList<'a> {
18792 let mut out = StmtList::default();
18793 for (mut s, span) in stmts {
18794 if matches!(s, Stmt::Return { .. }) {
18795 for &c in checks {
18796 out.wrote(Stmt::RuntimeAssert {
18797 condition: c,
18798 hard: true,
18799 dim: None,
18800 reason: None,
18801 });
18802 }
18803 out.carry(s, span);
18804 continue;
18805 }
18806 match &mut s {
18809 Stmt::If {
18810 then_block,
18811 else_block,
18812 ..
18813 } => {
18814 *then_block = self.guard_returns_in(then_block, checks);
18815 if let Some(eb) = else_block {
18816 *eb = self.guard_returns_in(eb, checks);
18817 }
18818 }
18819 Stmt::While { body, .. } | Stmt::Repeat { body, .. } | Stmt::Zone { body, .. } => {
18820 *body = self.guard_returns_in(body, checks);
18821 }
18822 Stmt::Inspect { arms, .. } => {
18823 for arm in arms.iter_mut() {
18824 arm.body = self.guard_returns_in(arm.body, checks);
18825 }
18826 }
18827 Stmt::Select { branches } => {
18828 for br in branches.iter_mut() {
18831 match br {
18832 crate::ast::stmt::SelectBranch::Receive { body, .. }
18833 | crate::ast::stmt::SelectBranch::Timeout { body, .. } => {
18834 *body = self.guard_returns_in(body, checks);
18835 }
18836 }
18837 }
18838 }
18839 _ => {}
18842 }
18843 out.carry(s, span);
18844 }
18845 out
18846 }
18847
18848 fn guard_returns_in(&mut self, block: &[Stmt<'a>], checks: &[&'a Expr<'a>]) -> &'a [Stmt<'a>] {
18850 let list = self.carried(block);
18851 let guarded = self.insert_ensures_before_returns(list, checks);
18852 self.alloc_read_block(guarded)
18853 }
18854
18855 fn alloc_block(&self, stmts: Vec<Stmt<'a>>) -> &'a [Stmt<'a>] {
18859 self.ctx
18860 .stmts
18861 .expect("imperative arenas not initialized")
18862 .alloc_slice(stmts.into_iter())
18863 }
18864
18865 fn alloc_read_block(&mut self, list: StmtList<'a>) -> &'a [Stmt<'a>] {
18868 let StmtList { stmts, spans } = list;
18869 let block = self.alloc_block(stmts);
18870 for (stmt, span) in block.iter().zip(spans) {
18871 if let Some(span) = span {
18872 self.nested_stmt_spans.0.insert(StmtId::of(stmt), span);
18873 }
18874 }
18875 block
18876 }
18877
18878 fn read_block_statement(&mut self, into: &mut StmtList<'a>) -> ParseResult<()> {
18882 let start = self.current;
18883 let stmt = self.parse_moving_statement()?;
18884 if self.check(&TokenType::Period) {
18885 self.advance();
18886 }
18887 into.read(stmt, self.span_since(start));
18888 Ok(())
18889 }
18890
18891 fn parse_moving_statement(&mut self) -> ParseResult<Stmt<'a>> {
18895 let mark = self.moves.len();
18896 let stmt = self.parse_statement()?;
18897 self.release_moves_since(mark);
18898 if let Stmt::Give {
18899 object: Expr::Identifier(sym),
18900 ..
18901 } = &stmt
18902 {
18903 let prior = self
18904 .world_state
18905 .get_ownership_by_var(*sym)
18906 .unwrap_or_default();
18907 self.moves.push((*sym, prior));
18908 self.world_state
18909 .set_ownership_by_var(*sym, crate::drs::OwnershipState::Moved);
18910 }
18911 Ok(stmt)
18912 }
18913
18914 fn release_moves_since(&mut self, mark: usize) {
18919 while self.moves.len() > mark {
18920 let (sym, prior) = self.moves.pop().expect("more moves than the mark");
18921 self.world_state.set_ownership_by_var(sym, prior);
18922 }
18923 }
18924
18925 fn read_scoped<T>(&mut self, read: impl FnOnce(&mut Self) -> ParseResult<T>) -> ParseResult<T> {
18930 let mark = self.moves.len();
18931 let block = read(self);
18932 self.release_moves_since(mark);
18933 block
18934 }
18935
18936 fn read_indented_block(&mut self) -> ParseResult<&'a [Stmt<'a>]> {
18941 if !self.check(&TokenType::Indent) {
18942 return Err(ParseError {
18943 kind: ParseErrorKind::ExpectedStatement,
18944 span: self.current_span(),
18945 });
18946 }
18947 self.advance(); let list = self.read_indented_statements()?;
18949 Ok(self.alloc_read_block(list))
18950 }
18951
18952 fn read_indented_statements(&mut self) -> ParseResult<StmtList<'a>> {
18955 self.read_scoped(|parser| {
18956 let mut list = StmtList::default();
18957 while !parser.check(&TokenType::Dedent) && !parser.is_at_end() {
18958 parser.read_block_statement(&mut list)?;
18959 }
18960 if parser.check(&TokenType::Dedent) {
18961 parser.advance();
18962 }
18963 Ok(list)
18964 })
18965 }
18966
18967 fn read_branch_body(&mut self) -> ParseResult<&'a [Stmt<'a>]> {
18970 let list = if self.check(&TokenType::Indent) {
18971 self.advance(); self.read_indented_statements()?
18973 } else {
18974 self.read_scoped(|parser| {
18975 let mut one = StmtList::default();
18976 parser.read_block_statement(&mut one)?;
18977 Ok(one)
18978 })?
18979 };
18980 Ok(self.alloc_read_block(list))
18981 }
18982
18983 fn read_link_block(&mut self, stmt: Stmt<'a>, start_tok: usize) -> &'a [Stmt<'a>] {
18986 let mut link = StmtList::default();
18987 link.read(stmt, self.span_since(start_tok));
18988 self.alloc_read_block(link)
18989 }
18990
18991 fn carried(&self, block: &[Stmt<'a>]) -> StmtList<'a> {
18994 let mut list = StmtList::default();
18995 for stmt in block {
18996 list.carry(stmt.clone(), self.nested_stmt_spans.get(StmtId::of(stmt)));
18997 }
18998 list
18999 }
19000
19001 fn parse_function_def(&mut self) -> ParseResult<Stmt<'a>> {
19006 self.parse_function_def_with_flags(OptimizationConfig::all_on())
19007 }
19008
19009 fn parse_function_def_with_flags(
19011 &mut self,
19012 opt_flags: OptimizationConfig,
19013 ) -> ParseResult<Stmt<'a>> {
19014 if self.check(&TokenType::To) || self.check_preposition_is("to") {
19016 self.advance();
19017 }
19018
19019 let mut is_native = if self.check(&TokenType::Native) {
19021 self.advance(); true
19023 } else {
19024 false
19025 };
19026
19027 let name = self.expect_identifier()?;
19029
19030 let mut parsed_generics: Vec<Symbol> = Vec::new();
19032 if self.check_preposition_is("of") {
19033 self.advance(); loop {
19035 if !self.check(&TokenType::LBracket) {
19036 return Err(ParseError {
19037 kind: ParseErrorKind::Custom(
19038 "Expected '[TypeParam]' after 'of' in generic function".to_string(),
19039 ),
19040 span: self.current_span(),
19041 });
19042 }
19043 self.advance(); let type_param = self.expect_identifier()?;
19045 parsed_generics.push(type_param);
19046 if !self.check(&TokenType::RBracket) {
19047 return Err(ParseError {
19048 kind: ParseErrorKind::Custom(
19049 "Expected ']' after type parameter name".to_string(),
19050 ),
19051 span: self.current_span(),
19052 });
19053 }
19054 self.advance(); if self.check_word("and") {
19056 self.advance(); } else {
19058 break;
19059 }
19060 }
19061 }
19062
19063 let mut params = Vec::new();
19065 while self.check(&TokenType::LParen) {
19066 self.advance(); if self.check(&TokenType::RParen) {
19070 self.advance(); break;
19072 }
19073
19074 loop {
19076 let param_name = self.expect_identifier()?;
19077
19078 if !self.check(&TokenType::Colon) {
19080 return Err(ParseError {
19081 kind: ParseErrorKind::ExpectedKeyword {
19082 keyword: ":".to_string(),
19083 },
19084 span: self.current_span(),
19085 });
19086 }
19087 self.advance(); let param_is_mutable = self.check_mutable_keyword();
19093 if param_is_mutable {
19094 self.advance(); }
19096
19097 let param_type_expr = self.parse_type_expression()?;
19099 let param_type_inner = self.ctx.alloc_type_expr(param_type_expr);
19100 let param_type = if param_is_mutable {
19101 self.ctx.alloc_type_expr(TypeExpr::Mutable {
19102 inner: param_type_inner,
19103 })
19104 } else {
19105 param_type_inner
19106 };
19107
19108 params.push((param_name, param_type));
19109
19110 if self.check(&TokenType::Comma) {
19112 self.advance(); continue;
19114 }
19115 break;
19116 }
19117
19118 if !self.check(&TokenType::RParen) {
19120 return Err(ParseError {
19121 kind: ParseErrorKind::ExpectedKeyword {
19122 keyword: ")".to_string(),
19123 },
19124 span: self.current_span(),
19125 });
19126 }
19127 self.advance(); if self.check_word("and") || self.check_preposition() || self.check(&TokenType::From) {
19132 self.advance();
19133 }
19134 }
19135
19136 let return_type = if self.check(&TokenType::Arrow) {
19138 self.advance(); let ret_type_expr = self.parse_type_expression()?;
19140 Some(self.ctx.alloc_type_expr(ret_type_expr))
19141 } else {
19142 None
19143 };
19144
19145 let mut native_path: Option<Symbol> = None;
19147 let mut is_exported = false;
19148 let mut export_target: Option<Symbol> = None;
19149
19150 if self.check_word("is") {
19151 self.advance(); if self.check(&TokenType::Native) {
19153 self.advance(); is_native = true;
19156 if let TokenType::StringLiteral(sym) = self.peek().kind {
19157 native_path = Some(sym);
19158 self.advance(); } else {
19160 return Err(ParseError {
19161 kind: ParseErrorKind::Custom(
19162 "Expected a string literal for native function path (e.g., is native \"reqwest::blocking::get\")".to_string()
19163 ),
19164 span: self.current_span(),
19165 });
19166 }
19167 } else if self.check_word("exported") {
19168 self.advance(); is_exported = true;
19171 if self.check_word("for") {
19172 self.advance(); let target_sym = if self.check(&TokenType::Native) {
19175 self.advance();
19176 self.interner.intern("native")
19177 } else {
19178 self.expect_identifier()?
19179 };
19180 let target_str = self.interner.resolve(target_sym);
19181 if !target_str.eq_ignore_ascii_case("c")
19182 && !target_str.eq_ignore_ascii_case("wasm")
19183 && !target_str.eq_ignore_ascii_case("native")
19184 {
19185 return Err(ParseError {
19186 kind: ParseErrorKind::Custom(
19187 format!("Unsupported export target \"{}\". Supported targets are \"c\", \"wasm\", and \"native\".", target_str)
19188 ),
19189 span: self.current_span(),
19190 });
19191 }
19192 export_target = Some(target_sym);
19193 }
19194 }
19195 }
19196
19197 if is_native {
19199 if self.check(&TokenType::Period) {
19201 self.advance();
19202 }
19203 if self.check(&TokenType::Newline) {
19204 self.advance();
19205 }
19206
19207 let empty_body = self.alloc_block(Vec::new());
19209
19210 return Ok(Stmt::FunctionDef {
19211 name,
19212 generics: parsed_generics,
19213 params,
19214 body: empty_body,
19215 return_type,
19216 is_native: true,
19217 native_path,
19218 is_exported: false,
19219 export_target: None,
19220 opt_flags,
19221 });
19222 }
19223
19224 if !self.check(&TokenType::Colon) {
19226 return Err(ParseError {
19227 kind: ParseErrorKind::ExpectedKeyword {
19228 keyword: ":".to_string(),
19229 },
19230 span: self.current_span(),
19231 });
19232 }
19233 self.advance(); if self.returns_a_view(return_type) {
19240 if let Some(raw) = self.consume_view_body() {
19241 let span = self.current_span();
19242 let view = self.view_from_raw(raw, None, span)?;
19243 let value = self.ctx.alloc_imperative_expr(Expr::View(Box::new(view)));
19244 let body = self.alloc_block(vec![Stmt::Return { value: Some(value) }]);
19245 return Ok(Stmt::FunctionDef {
19246 name,
19247 generics: parsed_generics,
19248 params,
19249 body,
19250 return_type,
19251 is_native: false,
19252 native_path: None,
19253 is_exported: false,
19254 export_target: None,
19255 opt_flags,
19256 });
19257 }
19258 }
19259
19260 if !self.check(&TokenType::Indent) {
19262 return Err(ParseError {
19263 kind: ParseErrorKind::ExpectedStatement,
19264 span: self.current_span(),
19265 });
19266 }
19267 self.advance(); let saved_user_bound = std::mem::take(&mut self.user_bound);
19275 for (param_name, _) in ¶ms {
19276 self.user_bound.insert(*param_name);
19277 }
19278
19279 let outer_copy_lint_scope = self
19283 .copy_lint_scope
19284 .replace(crate::lint::CopyLintScope::new());
19285
19286 let mut body_stmts = StmtList::default();
19288 let mut requires_checks: Vec<&'a Expr<'a>> = Vec::new();
19293 let mut ensures_checks: Vec<&'a Expr<'a>> = Vec::new();
19294 while !self.check(&TokenType::Dedent) && !self.is_at_end() {
19295 if self.check(&TokenType::Newline) {
19297 self.advance();
19298 continue;
19299 }
19300 if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
19302 break;
19303 }
19304 if self.check(&TokenType::Requires) {
19305 self.advance(); requires_checks.push(self.parse_condition()?);
19307 if self.check(&TokenType::Period) {
19308 self.advance();
19309 }
19310 continue;
19311 }
19312 if self.check(&TokenType::Ensures) {
19313 self.advance(); ensures_checks.push(self.parse_condition()?);
19315 if self.check(&TokenType::Period) {
19316 self.advance();
19317 }
19318 continue;
19319 }
19320 self.read_block_statement(&mut body_stmts)?;
19321 }
19322
19323 if self.check(&TokenType::Dedent) {
19325 self.advance();
19326 }
19327
19328 self.user_bound = saved_user_bound;
19330 if let Some(scope) = std::mem::replace(&mut self.copy_lint_scope, outer_copy_lint_scope) {
19331 self.value_copy_findings.extend(scope.finish(self.interner));
19332 }
19333
19334 if !ensures_checks.is_empty() {
19337 body_stmts = self.insert_ensures_before_returns(body_stmts, &ensures_checks);
19338 if !matches!(body_stmts.last(), Some(Stmt::Return { .. })) {
19339 for &c in &ensures_checks {
19340 body_stmts.wrote(Stmt::RuntimeAssert {
19341 condition: c,
19342 hard: true,
19343 dim: None,
19344 reason: None,
19345 });
19346 }
19347 }
19348 }
19349 if !requires_checks.is_empty() {
19351 let mut prefixed = StmtList::default();
19352 for &c in &requires_checks {
19353 prefixed.wrote(Stmt::RuntimeAssert {
19354 condition: c,
19355 hard: true,
19356 dim: None,
19357 reason: None,
19358 });
19359 }
19360 prefixed.append(body_stmts);
19361 body_stmts = prefixed;
19362 }
19363
19364 let body = self.alloc_read_block(body_stmts);
19366
19367 Ok(Stmt::FunctionDef {
19368 name,
19369 generics: parsed_generics,
19370 params,
19371 body,
19372 return_type,
19373 is_native: false,
19374 native_path: None,
19375 is_exported,
19376 export_target,
19377 opt_flags,
19378 })
19379 }
19380
19381 fn parse_primary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
19387 self.enter_recursion()?;
19388 self.eat_article_before_possessive();
19389 let result = self.parse_primary_expr_inner();
19390 self.leave_recursion();
19391 result
19392 }
19393
19394 fn check_selector_literal(&mut self, selector: &'a Expr<'a>) -> ParseResult<()> {
19406 let Expr::Literal(Literal::Text(sym)) = selector else {
19407 return Ok(());
19408 };
19409 let text = self.interner.resolve(*sym).to_string();
19410 match crate::lgx::selector::unsupported(&text) {
19411 Some(why) => Err(ParseError {
19412 kind: ParseErrorKind::Custom(why),
19413 span: self.current_span(),
19414 }),
19415 None => Ok(()),
19416 }
19417 }
19418
19419 fn eat_article_before_possessive(&mut self) {
19435 if self.check_word("the")
19436 && self
19437 .tokens
19438 .get(self.current + 2)
19439 .is_some_and(|t| matches!(t.kind, TokenType::Possessive))
19440 {
19441 self.advance();
19442 }
19443 }
19444
19445 fn parse_primary_expr_inner(&mut self) -> ParseResult<&'a Expr<'a>> {
19446 use crate::ast::{Expr, Literal};
19447
19448 if self.check_word("count") && self.peek_word_at(1, "of") {
19451 let start = self.current_span().start;
19452 let end = self.tokens[self.current + 1].span.end;
19453 let operand = self
19454 .tokens
19455 .get(self.current + 2)
19456 .map(|t| self.interner.resolve(t.lexeme).to_string())
19457 .filter(|w| {
19458 w.chars()
19459 .next()
19460 .is_some_and(|c| c.is_alphabetic() || c == '_')
19461 })
19462 .unwrap_or_else(|| "xs".to_string());
19463 return Err(ParseError {
19464 kind: ParseErrorKind::Custom(format!(
19465 "`count of {operand}` is not an expression — did you mean `length of {operand}`? \
19466 The number of items a collection holds is its `length of`; `the count of \
19467 \".row\"` (with `the`, and a selector) asks how many page elements match."
19468 )),
19469 span: Span::new(start, end),
19470 });
19471 }
19472
19473 if self.check_word("the") && self.peek_word_at(2, "of") {
19478 let page = if self.peek_word_at(1, "text") {
19484 Some("text")
19485 } else if self.peek_word_at(1, "count") {
19486 Some("count")
19487 } else if self.peek_word_at(1, "value") {
19488 Some("value")
19489 } else {
19490 None
19491 };
19492 if let Some(noun) = page {
19493 self.advance(); self.advance(); self.advance(); let selector = self.parse_page_query_operand()?;
19497 self.check_selector_literal(selector)?;
19498 let is_page = matches!(selector, Expr::Literal(Literal::Text(_)) | Expr::Dom(_));
19504 if noun == "count" && !is_page {
19505 return Ok(self.ctx.alloc_imperative_expr(Expr::Length {
19506 collection: selector,
19507 }));
19508 }
19509 let query = match noun {
19510 "text" => crate::ast::stmt::DomQuery::Text { selector },
19511 "count" => crate::ast::stmt::DomQuery::Count { selector },
19512 _ => crate::ast::stmt::DomQuery::Value { selector },
19513 };
19514 return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19515 }
19516 let op = if self.peek_word_at(1, "sum") {
19517 Some(BinaryOpKind::Add)
19518 } else if self.peek_word_at(1, "product") {
19519 Some(BinaryOpKind::Multiply)
19520 } else if self.peek_word_at(1, "difference") {
19521 Some(BinaryOpKind::Subtract)
19522 } else if self.peek_word_at(1, "quotient") {
19523 Some(BinaryOpKind::Divide)
19524 } else if self.peek_word_at(1, "remainder") {
19525 Some(BinaryOpKind::Modulo)
19526 } else {
19527 None
19528 };
19529 if let Some(op) = op {
19530 self.advance(); self.advance(); self.advance(); let a = self.parse_additive_expr()?;
19534 if self.check(&TokenType::And) || self.check_word("and") {
19535 self.advance(); } else {
19537 return Err(ParseError {
19538 kind: ParseErrorKind::ExpectedKeyword {
19539 keyword: "and".to_string(),
19540 },
19541 span: self.current_span(),
19542 });
19543 }
19544 let b = self.parse_additive_expr()?;
19545 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
19546 op,
19547 left: a,
19548 right: b,
19549 }));
19550 }
19551 }
19552 if self.check_word("html") && self.peek_word_at(1, "of") {
19556 self.advance(); self.advance(); let name = self.expect_identifier()?;
19559 let f = self
19560 .interner
19561 .intern(&crate::parser::lgx_lower::html_fn_name(
19562 self.interner.resolve(name),
19563 ));
19564 return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19565 function: f,
19566 args: Vec::new(),
19567 }));
19568 }
19569 if self.check_word("the")
19570 && self.peek_word_at(1, "standard")
19571 && self.peek_word_at(2, "part")
19572 && self.peek_word_at(3, "of")
19573 {
19574 return self.parse_standard_part();
19575 }
19576 if let Some(real) = self.try_parse_defined_real()? {
19577 return Ok(real);
19578 }
19579 if self.check_word("the") {
19581 if self.peek_word_at(1, "journal") && self.peek_word_at(2, "shows") {
19586 self.advance(); self.advance(); self.advance(); let first = self.parse_journal_action_name()?;
19590 if !self.check_word("then") {
19591 return Err(ParseError {
19592 kind: ParseErrorKind::ExpectedKeyword {
19593 keyword: "then".to_string(),
19594 },
19595 span: self.current_span(),
19596 });
19597 }
19598 self.advance(); let second = self.parse_journal_action_name()?;
19600 let query = crate::ast::stmt::DomQuery::JournalShows { first, second };
19601 return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19602 }
19603 let bare = if self.peek_word_at(1, "title") {
19604 Some(crate::ast::stmt::DomQuery::Title)
19605 } else if self.peek_word_at(1, "URL") {
19606 Some(crate::ast::stmt::DomQuery::Url)
19607 } else if self.peek_word_at(1, "journal") {
19608 Some(crate::ast::stmt::DomQuery::Journal)
19609 } else {
19610 None
19611 };
19612 if let Some(query) = bare {
19613 self.advance(); self.advance(); return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19616 }
19617 }
19618
19619 if matches!(self.peek().kind, TokenType::Identifier | TokenType::Noun(_))
19624 && self
19625 .interner
19626 .resolve(self.peek().lexeme)
19627 .eq_ignore_ascii_case("timestamp")
19628 {
19629 let str_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19630 Some(TokenType::StringLiteral(s)) => Some(*s),
19631 _ => None,
19632 };
19633 if let Some(s) = str_sym {
19634 self.advance(); self.advance(); let text = self
19637 .ctx
19638 .alloc_imperative_expr(Expr::Literal(Literal::Text(s)));
19639 let func = self.interner.intern("parse_timestamp");
19640 return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19641 function: func,
19642 args: vec![text],
19643 }));
19644 }
19645 }
19646
19647 if matches!(self.peek().kind, TokenType::Identifier | TokenType::Noun(_))
19651 && self
19652 .interner
19653 .resolve(self.peek().lexeme)
19654 .eq_ignore_ascii_case("uuid")
19655 {
19656 let str_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19657 Some(TokenType::StringLiteral(s)) => Some(*s),
19658 _ => None,
19659 };
19660 if let Some(s) = str_sym {
19661 self.advance(); self.advance(); let text = self
19664 .ctx
19665 .alloc_imperative_expr(Expr::Literal(Literal::Text(s)));
19666 let func = self.interner.intern("uuid");
19667 return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19668 function: func,
19669 args: vec![text],
19670 }));
19671 }
19672 }
19673
19674 let token = self.peek().clone();
19675 match &token.kind {
19676 TokenType::New => {
19680 let next_starts_type = matches!(
19683 self.tokens.get(self.current + 1).map(|t| &t.kind),
19684 Some(TokenType::Noun(_)
19685 | TokenType::ProperName(_)
19686 | TokenType::Adjective(_)
19687 | TokenType::Identifier
19688 | TokenType::Verb { .. }
19689 | TokenType::Tally
19690 | TokenType::SharedSet
19691 | TokenType::SharedSequence
19692 | TokenType::CollaborativeSequence
19693 | TokenType::Pipe
19694 | TokenType::Task)
19695 );
19696 if !next_starts_type {
19697 let sym = token.lexeme;
19698 self.advance();
19699 if self.check(&TokenType::LParen) {
19700 return self.parse_call_expr(sym);
19701 }
19702 self.verify_identifier_access(sym, token.span)?;
19703 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19704 return self.parse_field_access_chain(base);
19705 }
19706 self.advance(); let type_name_span = self.current_span();
19708 let base_type_name = self.expect_identifier()?;
19709
19710 let type_name = match self.resolve_construction_target(base_type_name, type_name_span)? {
19713 ConstructionTarget::Variant { enum_name, variant } => {
19714 let fields = if self.check_word("with") {
19715 self.parse_variant_constructor_fields(variant, Some(enum_name))?
19716 } else {
19717 vec![]
19718 };
19719 let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
19720 enum_name,
19721 variant,
19722 fields,
19723 });
19724 return self.parse_field_access_chain(base);
19725 }
19726 ConstructionTarget::Type(type_name) => type_name,
19727 };
19728
19729 let type_args = self.parse_generic_type_args(type_name)?;
19731
19732 let init_fields = if self.check_word("with") && !self.peek_word_at(1, "capacity") {
19735 self.parse_struct_init_fields(type_name)?
19736 } else {
19737 vec![]
19738 };
19739
19740 if let Some(expr) = self.option_form_of(type_name, &type_args, &init_fields) {
19757 let base = self.ctx.alloc_imperative_expr(expr);
19758 return self.parse_field_access_chain(base);
19759 }
19760
19761 let base = self.ctx.alloc_imperative_expr(Expr::New { type_name, type_args, init_fields });
19762 return self.parse_field_access_chain(base);
19763 }
19764
19765 TokenType::Article(_) => {
19769 let comp_lex = self.tokens.get(self.current + 1).map(|t| t.lexeme);
19773 let of_lex = self.tokens.get(self.current + 2).map(|t| t.lexeme);
19774 if let (Some(cl), Some(ol)) = (comp_lex, of_lex) {
19775 let extractor = match self.interner.resolve(cl).to_ascii_lowercase().as_str() {
19776 "year" => Some("year_of"),
19777 "month" => Some("month_of"),
19778 "day" => Some("day_of"),
19779 "weekday" => Some("weekday_of"),
19780 "hour" => Some("hour_of"),
19781 "minute" => Some("minute_of"),
19782 "second" => Some("second_of"),
19783 "date" => Some("date_of"),
19784 "time" => Some("time_of"),
19785 "week" => Some("week_of"),
19786 "quarter" => Some("quarter_of"),
19787 _ => None,
19788 };
19789 if let Some(extractor) = extractor {
19790 if self.interner.resolve(ol).eq_ignore_ascii_case("of") {
19791 self.advance(); self.advance(); self.advance(); let mut operand = self.parse_primary_expr()?;
19795 if self.check(&TokenType::In) || self.check_preposition_is("in") {
19800 let zone_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19801 Some(TokenType::StringLiteral(z)) => Some(*z),
19802 _ => None,
19803 };
19804 if let Some(z) = zone_sym {
19805 self.advance(); self.advance(); let zone = self
19808 .ctx
19809 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(z)));
19810 let local = self.interner.intern("local_instant");
19811 operand = self.ctx.alloc_imperative_expr(Expr::Call {
19812 function: local,
19813 args: vec![operand, zone],
19814 });
19815 }
19816 }
19817 let func = self.interner.intern(extractor);
19818 return Ok(self
19819 .ctx
19820 .alloc_imperative_expr(Expr::Call { function: func, args: vec![operand] }));
19821 }
19822 }
19823 let unit_lexeme = self.interner.resolve(cl).to_ascii_lowercase();
19827 let per_unit: Option<i64> = match unit_lexeme.as_str() {
19828 "second" | "seconds" => Some(1),
19829 "minute" | "minutes" => Some(60),
19830 "hour" | "hours" => Some(3600),
19831 "day" | "days" => Some(86400),
19832 "week" | "weeks" => Some(604800),
19833 _ => None,
19834 };
19835 let cal_func: Option<&str> = match unit_lexeme.as_str() {
19836 "month" | "months" => Some("months_between"),
19837 "year" | "years" => Some("years_between"),
19838 _ => None,
19839 };
19840 if (per_unit.is_some() || cal_func.is_some())
19841 && self.interner.resolve(ol).eq_ignore_ascii_case("between")
19842 {
19843 self.advance(); self.advance(); self.advance(); let a = self.parse_primary_expr()?;
19847 if !(self.check(&TokenType::And) || self.check_word("and")) {
19848 return Err(ParseError {
19849 kind: ParseErrorKind::ExpectedKeyword { keyword: "and".to_string() },
19850 span: self.current_span(),
19851 });
19852 }
19853 self.advance(); let b = self.parse_primary_expr()?;
19855 if let Some(cal) = cal_func {
19856 let func = self.interner.intern(cal);
19857 return Ok(self
19858 .ctx
19859 .alloc_imperative_expr(Expr::Call { function: func, args: vec![a, b] }));
19860 }
19861 let per_unit = per_unit.unwrap();
19862 let func = self.interner.intern("seconds_between");
19863 let secs = self.ctx.alloc_imperative_expr(Expr::Call { function: func, args: vec![a, b] });
19864 return Ok(if per_unit == 1 {
19865 secs
19866 } else {
19867 let d = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(per_unit)));
19868 self.ctx.alloc_imperative_expr(Expr::BinaryOp {
19869 op: BinaryOpKind::Divide,
19870 left: secs,
19871 right: d,
19872 })
19873 });
19874 }
19875 }
19876 if let Some(next) = self.tokens.get(self.current + 1) {
19879 if matches!(next.kind, TokenType::Manifest) {
19880 self.advance(); return self.parse_primary_expr();
19883 }
19884 if matches!(next.kind, TokenType::Chunk) {
19885 self.advance(); return self.parse_primary_expr();
19888 }
19889 if matches!(next.kind, TokenType::Length) {
19890 self.advance(); return self.parse_primary_expr();
19892 }
19893 }
19894 if let Some(next) = self.tokens.get(self.current + 1) {
19896 if matches!(next.kind, TokenType::New) {
19897 self.advance(); self.advance(); let type_name_span = self.current_span();
19900 let base_type_name = self.expect_identifier()?;
19901
19902 let type_name = match self.resolve_construction_target(base_type_name, type_name_span)? {
19904 ConstructionTarget::Variant { enum_name, variant } => {
19905 let fields = if self.check_word("with") {
19906 self.parse_variant_constructor_fields(variant, Some(enum_name))?
19907 } else {
19908 vec![]
19909 };
19910 let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
19911 enum_name,
19912 variant,
19913 fields,
19914 });
19915 return self.parse_field_access_chain(base);
19916 }
19917 ConstructionTarget::Type(type_name) => type_name,
19918 };
19919
19920 let type_args = self.parse_generic_type_args(type_name)?;
19922
19923 let init_fields = if self.check_word("with") && !self.peek_word_at(1, "capacity") {
19926 self.parse_struct_init_fields(type_name)?
19927 } else {
19928 vec![]
19929 };
19930
19931 if let Some(expr) = self.option_form_of(type_name, &type_args, &init_fields) {
19936 let base = self.ctx.alloc_imperative_expr(expr);
19937 return self.parse_field_access_chain(base);
19938 }
19939
19940 let base = self.ctx.alloc_imperative_expr(Expr::New { type_name, type_args, init_fields });
19941 return self.parse_field_access_chain(base);
19942 }
19943 }
19944 let sym = token.lexeme;
19946 self.advance();
19947 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19948 return self.parse_field_access_chain(base);
19949 }
19950
19951 TokenType::Swap => {
19955 let sym = token.lexeme;
19956 self.advance();
19957 if self.check(&TokenType::LParen) {
19958 return self.parse_call_expr(sym);
19959 }
19960 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19961 return self.parse_field_access_chain(base);
19962 }
19963
19964 TokenType::Adjective(_)
19971 if self.user_bound.contains(&token.lexeme)
19972 && !matches!(
19973 self.tokens.get(self.current + 1).map(|t| &t.kind),
19974 Some(TokenType::LParen)
19975 ) => {
19976 let sym = token.lexeme;
19977 self.advance();
19978 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19979 return self.parse_field_access_chain(base);
19980 }
19981
19982 TokenType::Item if !self.item_opens_an_index() => {
19987 let sym = self.peek().lexeme;
19988 self.advance();
19989 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19990 return self.parse_field_access_chain(base);
19991 }
19992 TokenType::Item => {
19993 self.advance(); let index = if let TokenType::Number(sym) = &self.peek().kind {
19997 let sym = *sym;
19999 self.advance();
20000 self.parse_index_numeral(sym)?
20001 } else if self.check(&TokenType::Minus)
20002 && matches!(
20003 self.tokens.get(self.current + 1).map(|t| &t.kind),
20004 Some(TokenType::Number(_))
20005 )
20006 {
20007 self.advance(); let TokenType::Number(sym) = self.peek().kind else { unreachable!() };
20011 self.advance();
20012 let num_str = self.interner.resolve(sym).to_string();
20013 let n = self.parse_i64_numeral_signed(&num_str, true).map_err(|_| ParseError {
20014 kind: ParseErrorKind::ExpectedKeyword {
20015 keyword: format!("a numeric index (got '{}')", num_str),
20016 },
20017 span: self.current_span(),
20018 })?;
20019 self.ctx
20020 .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(n)))
20021 } else if self.check(&TokenType::LParen) {
20022 self.advance(); let first = self.parse_imperative_expr()?;
20026 let inner = if self.check(&TokenType::Comma) {
20027 let mut items = vec![first];
20028 while self.check(&TokenType::Comma) {
20029 self.advance(); items.push(self.parse_imperative_expr()?);
20031 }
20032 self.ctx.alloc_imperative_expr(Expr::Tuple(items))
20033 } else {
20034 first
20035 };
20036 if !self.check(&TokenType::RParen) {
20037 return Err(ParseError {
20038 kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20039 span: self.current_span(),
20040 });
20041 }
20042 self.advance(); inner
20044 } else if let TokenType::StringLiteral(sym) = self.peek().kind {
20045 let sym = sym;
20047 self.advance();
20048 self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(sym)))
20049 } else if self.check(&TokenType::LBracket) {
20050 return Err(ParseError {
20055 kind: ParseErrorKind::Custom(
20056 "A List cannot be a map key: keys must be immutable, and a live \
20057 List can change after insertion — the map would lose the entry. \
20058 Which fixed value identifies this entry? Use an Int or Text key, \
20059 or a tuple like ('a', 1) for a compound key."
20060 .to_string(),
20061 ),
20062 span: self.peek().span,
20063 });
20064 } else if !self.check_preposition_is("of") {
20065 let word = self.interner.resolve(self.peek().lexeme);
20067 if word == "true" {
20068 self.advance();
20069 self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(true)))
20070 } else if word == "false" {
20071 self.advance();
20072 self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(false)))
20073 } else {
20074 let sym = self.peek().lexeme;
20076 self.advance();
20077 self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20078 }
20079 } else {
20080 return Err(ParseError {
20081 kind: ParseErrorKind::ExpectedExpression,
20082 span: self.current_span(),
20083 });
20084 };
20085
20086 if !self.check_preposition_is("of") {
20088 return Err(ParseError {
20089 kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20090 span: self.current_span(),
20091 });
20092 }
20093 self.advance(); let collection = self.parse_primary_expr()?;
20098
20099 Ok(self.ctx.alloc_imperative_expr(Expr::Index {
20100 collection,
20101 index,
20102 }))
20103 }
20104
20105 TokenType::Items => {
20108 let is_slice_syntax = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
20113 Some(TokenType::Number(_) | TokenType::LParen | TokenType::Length) => true,
20114 Some(_) => self.peek_word_at(2, "through"),
20115 None => false,
20116 };
20117
20118 if !is_slice_syntax {
20119 let sym = token.lexeme;
20121 self.advance();
20122 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20123 return self.parse_field_access_chain(base);
20124 }
20125
20126 self.advance(); let start = if let TokenType::Number(sym) = &self.peek().kind {
20130 let sym = *sym;
20132 self.advance();
20133 self.parse_index_numeral(sym)?
20134 } else if self.check(&TokenType::LParen) {
20135 self.advance(); let inner = self.parse_imperative_expr()?;
20138 if !self.check(&TokenType::RParen) {
20139 return Err(ParseError {
20140 kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20141 span: self.current_span(),
20142 });
20143 }
20144 self.advance(); inner
20146 } else if self.check(&TokenType::Length) {
20147 self.advance(); if self.check_preposition_is("of") {
20150 self.advance(); let target = self.parse_primary_expr()?;
20152 self.ctx.alloc_imperative_expr(Expr::Length { collection: target })
20153 } else {
20154 let sym = self.tokens[self.current - 1].lexeme;
20156 self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20157 }
20158 } else if !self.check_preposition_is("through") {
20159 let sym = self.peek().lexeme;
20161 self.advance();
20162 self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20163 } else {
20164 return Err(ParseError {
20165 kind: ParseErrorKind::ExpectedExpression,
20166 span: self.current_span(),
20167 });
20168 };
20169
20170 if !self.check_preposition_is("through") {
20172 return Err(ParseError {
20173 kind: ParseErrorKind::ExpectedKeyword { keyword: "through".to_string() },
20174 span: self.current_span(),
20175 });
20176 }
20177 self.advance(); let end = if let TokenType::Number(sym) = &self.peek().kind {
20181 let sym = *sym;
20183 self.advance();
20184 self.parse_index_numeral(sym)?
20185 } else if self.check(&TokenType::LParen) {
20186 self.advance(); let inner = self.parse_imperative_expr()?;
20189 if !self.check(&TokenType::RParen) {
20190 return Err(ParseError {
20191 kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20192 span: self.current_span(),
20193 });
20194 }
20195 self.advance(); inner
20197 } else if self.check(&TokenType::Length) {
20198 self.advance(); if self.check_preposition_is("of") {
20201 self.advance(); let target = self.parse_primary_expr()?;
20203 self.ctx.alloc_imperative_expr(Expr::Length { collection: target })
20204 } else {
20205 let sym = self.tokens[self.current - 1].lexeme;
20207 self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20208 }
20209 } else if !self.check_preposition_is("of") {
20210 let sym = self.peek().lexeme;
20212 self.advance();
20213 self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20214 } else {
20215 return Err(ParseError {
20216 kind: ParseErrorKind::ExpectedExpression,
20217 span: self.current_span(),
20218 });
20219 };
20220
20221 let collection = if self.check_preposition_is("of") {
20224 self.advance(); self.parse_imperative_expr()?
20226 } else {
20227 let items_sym = self.interner.intern("items");
20230 self.ctx.alloc_imperative_expr(Expr::Identifier(items_sym))
20231 };
20232
20233 Ok(self.ctx.alloc_imperative_expr(Expr::Slice {
20234 collection,
20235 start,
20236 end,
20237 }))
20238 }
20239
20240 TokenType::LBracket => {
20242 self.advance(); let mut items = Vec::new();
20245 if !self.check(&TokenType::RBracket) {
20246 loop {
20247 items.push(self.parse_imperative_expr()?);
20248 if !self.check(&TokenType::Comma) {
20249 break;
20250 }
20251 self.advance(); if self.check(&TokenType::RBracket) {
20254 break;
20255 }
20256 }
20257 }
20258
20259 if !self.check(&TokenType::RBracket) {
20260 return Err(ParseError {
20261 kind: ParseErrorKind::ExpectedKeyword { keyword: "]".to_string() },
20262 span: self.current_span(),
20263 });
20264 }
20265 self.advance(); if items.is_empty() && self.check_word("of") {
20269 self.advance(); let type_name = self.expect_identifier()?;
20271 let seq_sym = self.interner.intern("Seq");
20273 return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20274 type_name: seq_sym,
20275 type_args: vec![TypeExpr::Named(type_name)],
20276 init_fields: vec![],
20277 }));
20278 }
20279
20280 Ok(self.ctx.alloc_imperative_expr(Expr::List(items)))
20281 }
20282
20283 TokenType::LBrace => {
20290 self.advance(); if self.check(&TokenType::RBrace) {
20293 self.advance(); if !self.check_word("of") {
20295 return Err(ParseError {
20296 kind: ParseErrorKind::ExpectedKeyword {
20297 keyword: "of — an empty {} needs its element type: `{} of Int` (set) or `{} of Text to Int` (map)".to_string(),
20298 },
20299 span: self.current_span(),
20300 });
20301 }
20302 self.advance(); let first_ty = self.expect_identifier()?;
20304 if self.check(&TokenType::To) || self.check_preposition_is("to") {
20305 self.advance(); let val_ty = self.expect_identifier()?;
20307 let map_sym = self.interner.intern("Map");
20308 return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20309 type_name: map_sym,
20310 type_args: vec![TypeExpr::Named(first_ty), TypeExpr::Named(val_ty)],
20311 init_fields: vec![],
20312 }));
20313 }
20314 let set_sym = self.interner.intern("Set");
20315 return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20316 type_name: set_sym,
20317 type_args: vec![TypeExpr::Named(first_ty)],
20318 init_fields: vec![],
20319 }));
20320 }
20321
20322 let first = self.parse_imperative_expr()?;
20323 if self.check(&TokenType::Colon) {
20324 self.advance(); let mut args = vec![first, self.parse_imperative_expr()?];
20328 while self.check(&TokenType::Comma) {
20329 self.advance(); if self.check(&TokenType::RBrace) {
20332 break;
20333 }
20334 args.push(self.parse_imperative_expr()?);
20335 if !self.check(&TokenType::Colon) {
20336 return Err(ParseError {
20337 kind: ParseErrorKind::ExpectedKeyword { keyword: ":".to_string() },
20338 span: self.current_span(),
20339 });
20340 }
20341 self.advance(); args.push(self.parse_imperative_expr()?);
20343 }
20344 if !self.check(&TokenType::RBrace) {
20345 return Err(ParseError {
20346 kind: ParseErrorKind::ExpectedKeyword { keyword: "}".to_string() },
20347 span: self.current_span(),
20348 });
20349 }
20350 self.advance(); let map_of = self.interner.intern("mapOf");
20352 return Ok(self.ctx.alloc_imperative_expr(Expr::Call { function: map_of, args }));
20353 }
20354
20355 let mut args = vec![first];
20357 while self.check(&TokenType::Comma) {
20358 self.advance(); if self.check(&TokenType::RBrace) {
20361 break;
20362 }
20363 args.push(self.parse_imperative_expr()?);
20364 }
20365 if !self.check(&TokenType::RBrace) {
20366 return Err(ParseError {
20367 kind: ParseErrorKind::ExpectedKeyword { keyword: "}".to_string() },
20368 span: self.current_span(),
20369 });
20370 }
20371 self.advance(); let set_of = self.interner.intern("setOf");
20373 Ok(self.ctx.alloc_imperative_expr(Expr::Call { function: set_of, args }))
20374 }
20375
20376 TokenType::MoneyLiteral { amount, currency } => {
20380 let amount = *amount;
20381 let currency = *currency;
20382 let num_str = self.interner.resolve(amount).to_string();
20383 self.advance();
20384 let num_expr = if num_str.contains('.') {
20385 let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(amount)));
20386 let dec = self.interner.intern("decimal");
20387 self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20388 } else {
20389 let num = self.parse_i64_numeral(&num_str)?;
20390 self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20391 };
20392 let code_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(currency)));
20393 let func = self.interner.intern("money");
20394 return Ok(self
20395 .ctx
20396 .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, code_expr] }));
20397 }
20398
20399 TokenType::Number(sym) => {
20400 let num_str = self.interner.resolve(*sym).to_string();
20401 self.advance();
20402
20403 if let TokenType::CalendarUnit(unit) = self.peek().kind {
20405 return self.parse_span_literal_from_num(&num_str);
20406 }
20407
20408 if matches!(
20414 self.peek().kind,
20415 TokenType::Identifier | TokenType::Noun(_) | TokenType::Adjective(_)
20416 ) {
20417 let unit_lexeme = self.peek().lexeme;
20418 let is_unit =
20419 logicaffeine_base::quantity::units::by_name(self.interner.resolve(unit_lexeme)).is_some();
20420 if is_unit {
20421 self.advance(); let num_expr = if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20423 let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym)));
20424 let dec = self.interner.intern("decimal");
20425 self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20426 } else {
20427 let num = self.parse_i64_numeral(&num_str)?;
20428 self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20429 };
20430 let unit_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(unit_lexeme)));
20431 let func = self.interner.intern("quantity");
20432 return Ok(self
20433 .ctx
20434 .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, unit_expr] }));
20435 }
20436 }
20437
20438 if matches!(
20443 self.peek().kind,
20444 TokenType::Identifier
20445 | TokenType::Noun(_)
20446 | TokenType::ProperName(_)
20447 | TokenType::Adjective(_)
20448 ) {
20449 let code_lexeme = self.peek().lexeme;
20450 if logicaffeine_base::money::currency::by_code(self.interner.resolve(code_lexeme)).is_some() {
20451 self.advance(); let num_expr = if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20453 let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym)));
20454 let dec = self.interner.intern("decimal");
20455 self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20456 } else {
20457 let num = self.parse_i64_numeral(&num_str)?;
20458 self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20459 };
20460 let code_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(code_lexeme)));
20461 let func = self.interner.intern("money");
20462 return Ok(self
20463 .ctx
20464 .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, code_expr] }));
20465 }
20466 }
20467
20468 if Self::is_radix_numeral(&num_str) {
20472 let num = self.parse_i64_numeral(&num_str)?;
20473 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num))))
20474 } else if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20475 let num = num_str.replace('_', "").parse::<f64>().map_err(|_| ParseError {
20476 kind: ParseErrorKind::ExpectedKeyword {
20477 keyword: format!("a Float literal (got `{}`)", num_str),
20478 },
20479 span: self.current_span(),
20480 })?;
20481 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(num))))
20482 } else {
20483 let num = self.parse_i64_numeral(&num_str)?;
20484 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num))))
20485 }
20486 }
20487
20488 TokenType::StringLiteral(sym) => {
20490 self.advance();
20491 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym))))
20492 }
20493
20494 TokenType::InterpolatedString(sym) => {
20496 let raw = self.interner.resolve(*sym).to_string();
20497 let span = self.advance().span;
20498 let parts = self.parse_interpolation_parts(&raw, span)?;
20499 Ok(self.ctx.alloc_imperative_expr(Expr::InterpolatedString(parts)))
20500 }
20501
20502 TokenType::CharLiteral(sym) => {
20504 let char_str = self.interner.resolve(*sym);
20505 let ch = char_str.chars().next().unwrap_or('\0');
20506 self.advance();
20507 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Char(ch))))
20508 }
20509
20510 TokenType::DurationLiteral { nanos, .. } => {
20512 let nanos = *nanos;
20513 self.advance();
20514 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Duration(nanos))))
20515 }
20516
20517 TokenType::DateLiteral { days } => {
20520 let days = *days;
20521 self.advance();
20522
20523 if self.check(&TokenType::At) {
20525 self.advance(); if let TokenType::TimeLiteral { nanos_from_midnight } = self.peek().kind {
20529 let time_nanos = nanos_from_midnight;
20530 self.advance(); let moment_nanos = (days as i64) * 86_400_000_000_000 + time_nanos;
20534 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Moment(moment_nanos))));
20535 } else {
20536 return Err(ParseError {
20537 kind: ParseErrorKind::ExpectedExpression,
20538 span: self.current_span(),
20539 });
20540 }
20541 }
20542
20543 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Date(days))))
20544 }
20545
20546 TokenType::TimeLiteral { nanos_from_midnight } => {
20548 let nanos = *nanos_from_midnight;
20549 self.advance();
20550 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Time(nanos))))
20551 }
20552
20553 TokenType::Nothing => {
20557 if self.user_bound.contains(&token.lexeme) {
20558 return self.shadowed_literal_word_expr(token.lexeme);
20559 }
20560 self.advance();
20561 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)))
20562 }
20563
20564 TokenType::Some => {
20569 if self.user_bound.contains(&token.lexeme) {
20570 return self.shadowed_literal_word_expr(token.lexeme);
20571 }
20572 self.advance(); let value = self.parse_imperative_expr()?;
20574 Ok(self.ctx.alloc_imperative_expr(Expr::OptionSome { value }))
20575 }
20576
20577 TokenType::Length => {
20579 let func_name = self.peek().lexeme;
20580
20581 if self.tokens.get(self.current + 1)
20586 .map(|t| matches!(t.kind, TokenType::LParen))
20587 .unwrap_or(false)
20588 {
20589 let builtin = !self.declared_function_names.contains(&func_name);
20590 self.advance(); let call = self.parse_call_expr(func_name)?;
20592 if let Expr::Call { args, .. } = call {
20593 if let (true, [collection]) = (builtin, args.as_slice()) {
20594 return Ok(self.ctx.alloc_imperative_expr(Expr::Length { collection: *collection }));
20595 }
20596 }
20597 return Ok(call);
20598 }
20599
20600 self.advance(); if !self.check_preposition_is("of") {
20604 return Err(ParseError {
20605 kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20606 span: self.current_span(),
20607 });
20608 }
20609 self.advance(); let collection = self.parse_primary_expr()?;
20614 Ok(self.ctx.alloc_imperative_expr(Expr::Length { collection }))
20615 }
20616
20617 TokenType::Copy => {
20619 let func_name = self.peek().lexeme;
20620
20621 if self.tokens.get(self.current + 1)
20624 .map(|t| matches!(t.kind, TokenType::LParen))
20625 .unwrap_or(false)
20626 {
20627 let builtin = !self.declared_function_names.contains(&func_name);
20628 self.advance(); let call = self.parse_call_expr(func_name)?;
20630 if let Expr::Call { args, .. } = call {
20631 if let (true, [expr]) = (builtin, args.as_slice()) {
20632 return Ok(self.ctx.alloc_imperative_expr(Expr::Copy { expr: *expr }));
20633 }
20634 }
20635 return Ok(call);
20636 }
20637
20638 self.advance(); if !self.check_preposition_is("of") {
20642 return Err(ParseError {
20643 kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20644 span: self.current_span(),
20645 });
20646 }
20647 self.advance(); let expr = self.parse_imperative_expr()?;
20650 Ok(self.ctx.alloc_imperative_expr(Expr::Copy { expr }))
20651 }
20652
20653 TokenType::Manifest => {
20655 self.advance(); if !self.check_preposition_is("of") {
20659 return Err(ParseError {
20660 kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20661 span: self.current_span(),
20662 });
20663 }
20664 self.advance(); let zone = self.parse_imperative_expr()?;
20667 Ok(self.ctx.alloc_imperative_expr(Expr::ManifestOf { zone }))
20668 }
20669
20670 TokenType::Chunk => {
20672 self.advance(); if !self.check(&TokenType::At) {
20676 return Err(ParseError {
20677 kind: ParseErrorKind::ExpectedKeyword { keyword: "at".to_string() },
20678 span: self.current_span(),
20679 });
20680 }
20681 self.advance(); let index = self.parse_xor_expr()?;
20690
20691 if !self.check_preposition_is("in") && !self.check(&TokenType::In) {
20693 return Err(ParseError {
20694 kind: ParseErrorKind::ExpectedKeyword { keyword: "in".to_string() },
20695 span: self.current_span(),
20696 });
20697 }
20698 self.advance(); let zone = self.parse_imperative_expr()?;
20701 Ok(self.ctx.alloc_imperative_expr(Expr::ChunkAt { index, zone }))
20702 }
20703
20704 TokenType::Verb { lemma, .. } => {
20708 let word = self.interner.resolve(*lemma).to_lowercase();
20709 if word == "empty" && !self.user_bound.contains(&token.lexeme) {
20710 self.advance();
20711 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)));
20712 }
20713 let sym = token.lexeme;
20715 self.advance();
20716 if self.check(&TokenType::LParen) {
20717 return self.parse_call_expr(sym);
20718 }
20719 self.verify_identifier_access(sym, token.span)?;
20721 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20722 self.parse_field_access_chain(base)
20723 }
20724
20725 TokenType::TemporalAdverb(_) | TokenType::ScopalAdverb(_) | TokenType::Adverb(_) => {
20727 let sym = token.lexeme;
20728 self.advance();
20729 if self.check(&TokenType::LParen) {
20730 return self.parse_call_expr(sym);
20731 }
20732 self.verify_identifier_access(sym, token.span)?;
20734 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20735 self.parse_field_access_chain(base)
20736 }
20737
20738 TokenType::Read | TokenType::Write | TokenType::File | TokenType::Console |
20746 TokenType::Add | TokenType::Remove |
20747 TokenType::Size | TokenType::Zone | TokenType::Inside | TokenType::Called |
20748 TokenType::Launch | TokenType::Task | TokenType::Pipe | TokenType::Stop |
20749 TokenType::Receive | TokenType::Send | TokenType::Spawn | TokenType::Await |
20750 TokenType::Attempt | TokenType::Following | TokenType::Listen |
20751 TokenType::NetConnect | TokenType::Sync | TokenType::Mount |
20752 TokenType::Shifted | TokenType::Union | TokenType::Intersection |
20753 TokenType::Contains | TokenType::Through | TokenType::Into |
20754 TokenType::Push | TokenType::Pop | TokenType::Try | TokenType::Inspect |
20755 TokenType::Check | TokenType::Superlative(_) | TokenType::Comparative(_) |
20756 TokenType::Performative(_) |
20757 TokenType::Expect | TokenType::Advance | TokenType::Skip | TokenType::Given |
20764 TokenType::Note | TokenType::Warn | TokenType::Record | TokenType::During |
20765 TokenType::Sleep |
20766 TokenType::Focus(_) | TokenType::Particle(_) | TokenType::CalendarUnit(_) => {
20767 let sym = token.lexeme;
20768 self.advance();
20769 if self.check(&TokenType::LParen) {
20770 return self.parse_call_expr(sym);
20771 }
20772 self.verify_identifier_access(sym, token.span)?;
20774 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20775 self.parse_field_access_chain(base)
20776 }
20777
20778 TokenType::Noun(sym) | TokenType::ProperName(sym) | TokenType::Adjective(sym) => {
20781 let sym = *sym;
20782 let word = self.interner.resolve(sym);
20783
20784 if word == "true" {
20786 self.advance();
20787 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Boolean(true))));
20788 }
20789 if word == "false" {
20790 self.advance();
20791 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Boolean(false))));
20792 }
20793 if word == "infinity" && !self.user_bound.contains(&sym) {
20796 self.advance();
20797 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(f64::INFINITY))));
20798 }
20799 if word == "nan" && !self.user_bound.contains(&sym) {
20800 self.advance();
20801 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(f64::NAN))));
20802 }
20803
20804 if word == "empty" && !self.user_bound.contains(&sym) {
20807 self.advance();
20808 return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)));
20809 }
20810
20811 if word == "none" && !self.user_bound.contains(&sym) {
20815 self.advance();
20816 return Ok(self.ctx.alloc_imperative_expr(Expr::OptionNone));
20817 }
20818
20819 let sym_span = self.current_span();
20821 self.advance();
20822
20823 if self.check(&TokenType::LParen) {
20825 return self.parse_call_expr(sym);
20826 }
20827
20828 if self.check(&TokenType::From) && !self.bare_word_from_is_a_qualifier(sym) {
20837 self.verify_identifier_access(sym, sym_span)?;
20838 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20839 return self.parse_field_access_chain(base);
20840 }
20841
20842 match self.resolve_construction_target(sym, sym_span)? {
20846 ConstructionTarget::Variant { enum_name, variant } => {
20847 let fields = if self.check_word("with") {
20848 self.parse_variant_constructor_fields(variant, Some(enum_name))?
20849 } else {
20850 vec![]
20851 };
20852 let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
20853 enum_name,
20854 variant,
20855 fields,
20856 });
20857 return self.parse_field_access_chain(base);
20858 }
20859 ConstructionTarget::Type(name) => {
20860 self.verify_identifier_access(name, sym_span)?;
20862 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(name));
20863 self.parse_field_access_chain(base)
20865 }
20866 }
20867 }
20868
20869 TokenType::Pronoun { .. } => {
20871 let sym = token.lexeme;
20872 self.advance();
20873 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20874 self.parse_field_access_chain(base)
20876 }
20877
20878 TokenType::Merge | TokenType::Increase => {
20880 let sym = token.lexeme;
20881 self.advance();
20882
20883 if self.check(&TokenType::LParen) {
20885 return self.parse_call_expr(sym);
20886 }
20887
20888 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20889 self.parse_field_access_chain(base)
20890 }
20891
20892 TokenType::Escape => {
20896 if self.tokens.get(self.current + 1).map_or(false, |t|
20897 matches!(t.kind, TokenType::To) || {
20898 if let TokenType::Preposition(sym) = t.kind {
20899 sym.is(self.interner, "to")
20900 } else {
20901 false
20902 }
20903 }
20904 ) {
20905 return self.parse_escape_expr();
20906 }
20907 let sym = token.lexeme;
20909 self.advance();
20910 if self.check(&TokenType::LParen) {
20911 return self.parse_call_expr(sym);
20912 }
20913 self.verify_identifier_access(sym, token.span)?;
20914 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20915 self.parse_field_access_chain(base)
20916 }
20917
20918 TokenType::Values | TokenType::Both | TokenType::Either | TokenType::Combined | TokenType::Followed | TokenType::Shared | TokenType::Preposition(_) | TokenType::All => { let sym = token.lexeme;
20930 self.advance();
20931
20932 if self.check(&TokenType::LParen) {
20934 return self.parse_call_expr(sym);
20935 }
20936
20937 self.verify_identifier_access(sym, token.span)?;
20938 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20939 self.parse_field_access_chain(base)
20940 }
20941
20942 TokenType::Ambiguous { primary, alternatives } => {
20944 let sym = token.lexeme;
20947
20948 let is_identifier_token = match &**primary {
20950 TokenType::Noun(_) | TokenType::Adjective(_) | TokenType::ProperName(_) |
20951 TokenType::Verb { .. } => true,
20952 _ => alternatives.iter().any(|t| matches!(t,
20953 TokenType::Noun(_) | TokenType::Adjective(_) | TokenType::ProperName(_) |
20954 TokenType::Verb { .. }
20955 ))
20956 };
20957
20958 if is_identifier_token {
20959 self.advance();
20960
20961 if self.check(&TokenType::LParen) {
20963 return self.parse_call_expr(sym);
20964 }
20965
20966 self.verify_identifier_access(sym, token.span)?;
20967 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20968 self.parse_field_access_chain(base)
20970 } else {
20971 Err(ParseError {
20972 kind: ParseErrorKind::ExpectedExpression,
20973 span: self.current_span(),
20974 })
20975 }
20976 }
20977
20978 TokenType::LParen => {
20980 if let Some(closure) = self.try_parse(|p| p.parse_closure_expr()) {
20983 return Ok(closure);
20984 }
20985
20986 self.advance(); let first = self.parse_imperative_expr()?;
20989
20990 if self.check(&TokenType::Comma) {
20992 let mut items = vec![first];
20994 while self.check(&TokenType::Comma) {
20995 self.advance(); items.push(self.parse_imperative_expr()?);
20997 }
20998
20999 if !self.check(&TokenType::RParen) {
21000 return Err(ParseError {
21001 kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
21002 span: self.current_span(),
21003 });
21004 }
21005 self.advance(); let base = self.ctx.alloc_imperative_expr(Expr::Tuple(items));
21008 self.parse_field_access_chain(base)
21009 } else {
21010 if !self.check(&TokenType::RParen) {
21012 return Err(ParseError {
21013 kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
21014 span: self.current_span(),
21015 });
21016 }
21017 self.advance(); self.parse_field_access_chain(first)
21021 }
21022 }
21023
21024 TokenType::Call => {
21026 let start_tok = self.current;
21027 self.advance(); let function = match &self.peek().kind {
21029 TokenType::Noun(sym) | TokenType::Adjective(sym) => {
21030 let s = *sym;
21031 self.advance();
21032 s
21033 }
21034 TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
21035 let s = self.peek().lexeme;
21036 self.advance();
21037 s
21038 }
21039 _ => {
21040 return Err(ParseError {
21041 kind: ParseErrorKind::ExpectedIdentifier,
21042 span: self.current_span(),
21043 });
21044 }
21045 };
21046 let args = if self.check_preposition_is("with") {
21047 self.advance(); self.parse_call_arguments()?
21049 } else {
21050 Vec::new()
21051 };
21052 self.record_call_span(&args, start_tok);
21053 Ok(self.ctx.alloc_imperative_expr(Expr::Call { function, args }))
21054 }
21055
21056 _ if self.token_is_word_identifier(&token) => {
21062 let sym = token.lexeme;
21063 self.advance();
21064 if self.check(&TokenType::LParen) {
21066 self.parse_call_expr(sym)
21067 } else {
21068 Ok(self.ctx.alloc_imperative_expr(Expr::Identifier(sym)))
21069 }
21070 }
21071
21072 _ => {
21073 Err(ParseError {
21074 kind: ParseErrorKind::ExpectedExpression,
21075 span: self.current_span(),
21076 })
21077 }
21078 }
21079 }
21080
21081 fn parse_closure_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21084 use crate::ast::stmt::ClosureBody;
21085
21086 if !self.check(&TokenType::LParen) {
21088 return Err(ParseError {
21089 kind: ParseErrorKind::ExpectedExpression,
21090 span: self.current_span(),
21091 });
21092 }
21093 self.advance(); let mut params = Vec::new();
21097 if !self.check(&TokenType::RParen) {
21098 let name = self.expect_identifier()?;
21100 if !self.check(&TokenType::Colon) {
21101 return Err(ParseError {
21102 kind: ParseErrorKind::ExpectedKeyword {
21103 keyword: ":".to_string(),
21104 },
21105 span: self.current_span(),
21106 });
21107 }
21108 self.advance(); let ty = self.parse_type_expression()?;
21110 let ty_ref = self.ctx.alloc_type_expr(ty);
21111 params.push((name, ty_ref));
21112
21113 while self.check(&TokenType::Comma) {
21115 self.advance(); let name = self.expect_identifier()?;
21117 if !self.check(&TokenType::Colon) {
21118 return Err(ParseError {
21119 kind: ParseErrorKind::ExpectedKeyword {
21120 keyword: ":".to_string(),
21121 },
21122 span: self.current_span(),
21123 });
21124 }
21125 self.advance(); let ty = self.parse_type_expression()?;
21127 let ty_ref = self.ctx.alloc_type_expr(ty);
21128 params.push((name, ty_ref));
21129 }
21130 }
21131
21132 if !self.check(&TokenType::RParen) {
21134 return Err(ParseError {
21135 kind: ParseErrorKind::ExpectedKeyword {
21136 keyword: ")".to_string(),
21137 },
21138 span: self.current_span(),
21139 });
21140 }
21141 self.advance(); if !self.check(&TokenType::Arrow) {
21145 return Err(ParseError {
21146 kind: ParseErrorKind::ExpectedKeyword {
21147 keyword: "->".to_string(),
21148 },
21149 span: self.current_span(),
21150 });
21151 }
21152 self.advance(); let body = if self.check(&TokenType::Colon) {
21156 self.advance(); ClosureBody::Block(self.read_indented_block()?)
21158 } else {
21159 let expr = self.parse_condition()?;
21161 ClosureBody::Expression(expr)
21162 };
21163
21164 Ok(self.ctx.alloc_imperative_expr(Expr::Closure {
21165 params,
21166 body,
21167 return_type: None,
21168 }))
21169 }
21170
21171 fn parse_imperative_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21175 self.parse_condition()
21176 }
21177
21178 fn parse_xor_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21182 let mut left = self.parse_bitxor_expr()?;
21183
21184 while self.check(&TokenType::VBar) {
21185 self.advance(); let right = self.parse_bitxor_expr()?;
21187 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21188 op: BinaryOpKind::BitOr,
21189 left,
21190 right,
21191 });
21192 }
21193
21194 Ok(left)
21195 }
21196
21197 fn parse_bitxor_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21199 let mut left = self.parse_bitand_expr()?;
21200
21201 while self.check(&TokenType::Xor) || self.check(&TokenType::Caret) {
21202 self.advance(); let right = self.parse_bitand_expr()?;
21204 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21205 op: BinaryOpKind::BitXor,
21206 left,
21207 right,
21208 });
21209 }
21210
21211 Ok(left)
21212 }
21213
21214 fn parse_bitand_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21216 let mut left = self.parse_additive_expr()?;
21217
21218 while self.check(&TokenType::Amp) {
21219 self.advance(); let right = self.parse_additive_expr()?;
21221 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21222 op: BinaryOpKind::BitAnd,
21223 left,
21224 right,
21225 });
21226 }
21227
21228 Ok(left)
21229 }
21230
21231 fn parse_additive_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21233 let mut left = self.parse_shift_expr()?;
21234
21235 loop {
21236 match &self.peek().kind {
21237 TokenType::Plus => {
21238 self.advance();
21239 let right = self.parse_shift_expr()?;
21240 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21241 op: BinaryOpKind::Add,
21242 left,
21243 right,
21244 });
21245 }
21246 TokenType::Minus => {
21247 self.advance();
21248 let right = self.parse_shift_expr()?;
21249 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21250 op: BinaryOpKind::Subtract,
21251 left,
21252 right,
21253 });
21254 }
21255 TokenType::Combined => {
21257 self.advance(); if !self.check_preposition_is("with") {
21260 return Err(ParseError {
21261 kind: ParseErrorKind::ExpectedKeyword {
21262 keyword: "with".to_string(),
21263 },
21264 span: self.current_span(),
21265 });
21266 }
21267 self.advance(); let right = self.parse_shift_expr()?;
21269 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21270 op: BinaryOpKind::Concat,
21271 left,
21272 right,
21273 });
21274 }
21275 TokenType::Followed => {
21277 self.advance(); if !self.check_preposition_is("by") {
21279 return Err(ParseError {
21280 kind: ParseErrorKind::ExpectedKeyword {
21281 keyword: "by".to_string(),
21282 },
21283 span: self.current_span(),
21284 });
21285 }
21286 self.advance(); let right = self.parse_shift_expr()?;
21288 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21289 op: BinaryOpKind::SeqConcat,
21290 left,
21291 right,
21292 });
21293 }
21294 TokenType::Union => {
21296 self.advance(); let right = self.parse_shift_expr()?;
21298 left = self.ctx.alloc_imperative_expr(Expr::Union { left, right });
21299 }
21300 TokenType::Intersection => {
21301 self.advance(); let right = self.parse_shift_expr()?;
21303 left = self
21304 .ctx
21305 .alloc_imperative_expr(Expr::Intersection { left, right });
21306 }
21307 TokenType::Contains => {
21309 self.advance(); let value = self.parse_shift_expr()?;
21311 left = self.ctx.alloc_imperative_expr(Expr::Contains {
21312 collection: left,
21313 value,
21314 });
21315 }
21316 _ if self.check_word("without") => {
21320 self.advance(); let right = self.parse_shift_expr()?;
21322 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21323 op: BinaryOpKind::Subtract,
21324 left,
21325 right,
21326 });
21327 }
21328 _ if self.check_word("copies") && self.peek_word_at(1, "of") => {
21333 self.advance(); self.advance(); let element = self.parse_shift_expr()?;
21336 let repeat_sym = self.interner.intern("repeatSeq");
21337 left = self.ctx.alloc_imperative_expr(Expr::Call {
21338 function: repeat_sym,
21339 args: vec![element, left],
21340 });
21341 }
21342 _ if self.check_op_word("plus") => {
21345 self.advance(); let right = self.parse_shift_expr()?;
21347 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21348 op: BinaryOpKind::Add,
21349 left,
21350 right,
21351 });
21352 }
21353 _ if self.check_op_word("minus") => {
21354 self.advance(); let right = self.parse_shift_expr()?;
21356 left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21357 op: BinaryOpKind::Subtract,
21358 left,
21359 right,
21360 });
21361 }
21362 _ => break,
21363 }
21364 }
21365
21366 Ok(left)
21367 }
21368
21369 fn parse_shift_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21372 let mut left = self.parse_multiplicative_expr()?;
21373
21374 loop {
21375 if !self.check(&TokenType::Shifted) {
21376 break;
21377 }
21378 self.advance(); let is_left = self.check_word("left");
21381 if is_left {
21382 self.advance(); } else if self.check_word("right") {
21384 self.advance(); } else {
21386 return Err(ParseError {
21387 kind: ParseErrorKind::ExpectedKeyword {
21388 keyword: "left or right".to_string(),
21389 },
21390 span: self.current_span(),
21391 });
21392 }
21393
21394 if !self.check_preposition_is("by") && !self.check_word("by") {
21396 return Err(ParseError {
21397 kind: ParseErrorKind::ExpectedKeyword {
21398 keyword: "by".to_string(),
21399 },
21400 span: self.current_span(),
21401 });
21402 }
21403 self.advance(); let right = self.parse_multiplicative_expr()?;
21406 let op = if is_left {
21407 BinaryOpKind::Shl
21408 } else {
21409 BinaryOpKind::Shr
21410 };
21411 left = self
21412 .ctx
21413 .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
21414 }
21415
21416 Ok(left)
21417 }
21418
21419 fn parse_unary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21421 use crate::ast::{Expr, Literal};
21422
21423 if self.check(&TokenType::Tilde) {
21424 self.advance(); let operand = self.parse_unary_expr()?;
21426 let neg_one = self
21429 .ctx
21430 .alloc_imperative_expr(Expr::Literal(Literal::Number(-1)));
21431 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21432 op: BinaryOpKind::BitXor,
21433 left: operand,
21434 right: neg_one,
21435 }));
21436 }
21437 if self.check(&TokenType::Minus) {
21438 if let Some(TokenType::Number(sym)) =
21443 self.tokens.get(self.current + 1).map(|t| t.kind.clone())
21444 {
21445 self.advance(); self.advance(); let num_str = self.interner.resolve(sym).to_string();
21448 if !Self::is_radix_numeral(&num_str)
21449 && (num_str.contains('.') || num_str.contains('e') || num_str.contains('E'))
21450 {
21451 let f = num_str
21452 .replace('_', "")
21453 .parse::<f64>()
21454 .map_err(|_| ParseError {
21455 kind: ParseErrorKind::ExpectedKeyword {
21456 keyword: format!("a Float literal (got `-{}`)", num_str),
21457 },
21458 span: self.current_span(),
21459 })?;
21460 return Ok(self
21461 .ctx
21462 .alloc_imperative_expr(Expr::Literal(Literal::Float(-f))));
21463 }
21464 let n = self.parse_i64_numeral_signed(&num_str, true)?;
21465 return Ok(self
21466 .ctx
21467 .alloc_imperative_expr(Expr::Literal(Literal::Number(n))));
21468 }
21469 if self.tokens.get(self.current + 1).is_some_and(|t| {
21470 self.interner.resolve(t.lexeme) == "infinity"
21471 && !self.user_bound.contains(&t.lexeme)
21472 }) {
21473 self.advance(); self.advance(); return Ok(self
21476 .ctx
21477 .alloc_imperative_expr(Expr::Literal(Literal::Float(f64::NEG_INFINITY))));
21478 }
21479 self.advance(); let operand = self.parse_unary_expr()?; return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21483 op: BinaryOpKind::Subtract,
21484 left: self
21485 .ctx
21486 .alloc_imperative_expr(Expr::Literal(Literal::Number(0))),
21487 right: operand,
21488 }));
21489 }
21490 self.parse_primary_expr()
21491 }
21492
21493 fn parse_power_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21498 let base = self.parse_unary_expr()?;
21499 let symbolic = self.check(&TokenType::StarStar);
21501 let spoken = (self.check_to_preposition() || self.check_word("to"))
21502 && self.peek_word_at(1, "the")
21503 && self.peek_word_at(2, "power")
21504 && self.peek_word_at(3, "of");
21505 if symbolic || spoken {
21506 if symbolic {
21507 self.advance(); } else {
21509 self.advance(); self.advance(); self.advance(); self.advance(); }
21514 let exp = self.parse_power_expr()?;
21515 return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21516 op: BinaryOpKind::Pow,
21517 left: base,
21518 right: exp,
21519 }));
21520 }
21521 Ok(base)
21522 }
21523
21524 fn parse_multiplicative_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21525 let mut left = self.parse_power_expr()?;
21526
21527 loop {
21528 let op = match &self.peek().kind {
21529 TokenType::Star => {
21530 self.advance();
21531 BinaryOpKind::Multiply
21532 }
21533 TokenType::Slash => {
21534 self.advance();
21535 BinaryOpKind::Divide
21536 }
21537 TokenType::SlashSlash => {
21538 self.advance();
21539 BinaryOpKind::FloorDivide
21540 }
21541 TokenType::Percent => {
21542 self.advance();
21543 BinaryOpKind::Modulo
21544 }
21545 _ if self.check_op_word("times") => {
21549 self.advance(); BinaryOpKind::Multiply
21551 }
21552 _ if self.check_op_word("divided") && self.peek_word_at(1, "by") => {
21553 self.advance(); self.advance(); BinaryOpKind::Divide
21556 }
21557 _ if self.check_op_word("modulo") || self.check_op_word("mod") => {
21558 self.advance(); BinaryOpKind::Modulo
21560 }
21561 _ => break,
21562 };
21563 let right = self.parse_power_expr()?;
21564 left = self
21565 .ctx
21566 .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
21567 }
21568
21569 Ok(left)
21570 }
21571
21572 fn try_parse_binary_op(&mut self) -> Option<BinaryOpKind> {
21574 match &self.peek().kind {
21575 TokenType::Plus => {
21576 self.advance();
21577 Some(BinaryOpKind::Add)
21578 }
21579 TokenType::Minus => {
21580 self.advance();
21581 Some(BinaryOpKind::Subtract)
21582 }
21583 TokenType::Star => {
21584 self.advance();
21585 Some(BinaryOpKind::Multiply)
21586 }
21587 TokenType::Slash => {
21588 self.advance();
21589 Some(BinaryOpKind::Divide)
21590 }
21591 _ => None,
21592 }
21593 }
21594
21595 fn parse_interpolation_parts(
21602 &mut self,
21603 raw: &str,
21604 span: Span,
21605 ) -> ParseResult<Vec<crate::ast::stmt::StringPart<'a>>> {
21606 use crate::ast::stmt::StringPart;
21607
21608 let mut parts = Vec::new();
21609 let chars: Vec<char> = raw.chars().collect();
21610 let mut i = 0;
21611 let mut literal_buf = String::new();
21612
21613 while i < chars.len() {
21614 match chars[i] {
21615 '{' if i + 1 < chars.len() && chars[i + 1] == '{' => {
21616 literal_buf.push('{');
21618 i += 2;
21619 }
21620 '{' => {
21621 if !literal_buf.is_empty() {
21623 let sym = self.interner.intern(&literal_buf);
21624 parts.push(StringPart::Literal(sym));
21625 literal_buf.clear();
21626 }
21627
21628 let start = i + 1;
21630 let mut depth = 1;
21631 let mut j = start;
21632 while j < chars.len() && depth > 0 {
21633 if chars[j] == '{' {
21634 depth += 1;
21635 }
21636 if chars[j] == '}' {
21637 depth -= 1;
21638 }
21639 if depth > 0 {
21640 j += 1;
21641 }
21642 }
21643 if depth != 0 {
21644 return Err(ParseError {
21645 kind: crate::error::ParseErrorKind::Custom(
21646 "Unclosed interpolation brace in string — a `{` opens a hole that \
21647 nothing closes. Add the `}`, or, if you meant the character \
21648 itself, double it: `{{{{` prints `{{`."
21649 .to_string(),
21650 ),
21651 span,
21652 });
21653 }
21654
21655 let hole_content: String = chars[start..j].iter().collect();
21656
21657 let (hole_after_debug, is_debug) = {
21662 if let Some(eq_pos) = hole_content.rfind('=') {
21663 let before_eq = hole_content[..eq_pos].trim();
21664 let is_double_eq = eq_pos > 0
21667 && hole_content.as_bytes().get(eq_pos - 1) == Some(&b'=');
21668 let is_preceded_by_comparison = eq_pos > 0
21669 && matches!(
21670 hole_content.as_bytes().get(eq_pos - 1),
21671 Some(b'!' | b'<' | b'>')
21672 );
21673 if !is_double_eq
21674 && !is_preceded_by_comparison
21675 && !before_eq.is_empty()
21676 && before_eq.chars().all(|c| c.is_alphanumeric() || c == '_')
21677 {
21678 (
21679 hole_content[..eq_pos].to_string()
21680 + &hole_content[eq_pos + 1..],
21681 true,
21682 )
21683 } else {
21684 (hole_content.clone(), false)
21685 }
21686 } else {
21687 (hole_content.clone(), false)
21688 }
21689 };
21690
21691 let (expr_str, format_spec) =
21693 if let Some(colon_pos) = hole_after_debug.rfind(':') {
21694 let before = &hole_after_debug[..colon_pos];
21695 let after = &hole_after_debug[colon_pos + 1..];
21696 if !after.is_empty()
21699 && (after.starts_with('.')
21700 || after.starts_with('<')
21701 || after.starts_with('>')
21702 || after.starts_with('^')
21703 || after == "$"
21704 || after.chars().next().map_or(false, |c| c.is_ascii_digit()))
21705 {
21706 let valid = if after == "$" {
21708 true
21709 } else if after.starts_with('.') {
21710 after[1..].parse::<usize>().is_ok()
21711 } else if after.starts_with('<')
21712 || after.starts_with('>')
21713 || after.starts_with('^')
21714 {
21715 after[1..].parse::<usize>().is_ok()
21716 } else {
21717 after.parse::<usize>().is_ok()
21718 };
21719 if !valid {
21720 return Err(ParseError {
21721 kind: crate::error::ParseErrorKind::Custom(format!(
21722 "Invalid format specifier `{}` in interpolation hole",
21723 after
21724 )),
21725 span,
21726 });
21727 }
21728 (before.to_string(), Some(after.to_string()))
21729 } else {
21730 (hole_after_debug.clone(), None)
21731 }
21732 } else {
21733 (hole_after_debug.clone(), None)
21734 };
21735
21736 let expr_source = expr_str.trim().to_string();
21738 if expr_source.is_empty() {
21739 return Err(ParseError {
21740 kind: crate::error::ParseErrorKind::Custom(
21741 "Empty interpolation hole in string — `{}` asks me to print a \
21742 value and names none. Put the value inside the braces, or, if you \
21743 meant the two characters themselves (an empty JSON object, a CSS \
21744 rule), double them: write `{{{{}}}}` to get `{}`."
21745 .to_string(),
21746 ),
21747 span,
21748 });
21749 }
21750
21751 let sub_expr = {
21754 let mut sub_lexer = match self.mode {
21767 ParserMode::Imperative => {
21768 crate::lexer::Lexer::new_imperative(&expr_source, self.interner)
21769 }
21770 ParserMode::Declarative => {
21771 crate::lexer::Lexer::new(&expr_source, self.interner)
21772 }
21773 };
21774 let sub_tokens = sub_lexer.tokenize();
21775
21776 let saved_tokens = std::mem::replace(&mut self.tokens, sub_tokens);
21778 let saved_current = self.backtrack_point();
21779 self.current = 0;
21780
21781 let result = self.parse_primary_or_binary_expr();
21782
21783 let unread = match &result {
21796 Ok(_) if !self.is_at_end() => {
21797 let at = self.peek().span.start.min(expr_source.len());
21798 let read = expr_source.get(..at).unwrap_or("").trim().to_string();
21799 let rest = expr_source.get(at..).unwrap_or("").trim().to_string();
21800 Some((read, rest))
21801 }
21802 _ => None,
21803 };
21804
21805 self.tokens = saved_tokens;
21807 self.backtrack_to(&saved_current);
21808
21809 if let Some((read, rest)) = unread {
21810 return Err(ParseError {
21811 kind: crate::error::ParseErrorKind::Custom(format!(
21812 "I read `{read}` in this interpolation hole and then found \
21813 `{rest}`, which is not part of it. A hole holds ONE value, so \
21814 everything between the braces has to be that one value. If \
21815 you meant the braces themselves — a JSON object, a CSS rule — \
21816 double them: `{{{{` and `}}}}` print `{{` and `}}`."
21817 )),
21818 span,
21819 });
21820 }
21821
21822 result.map_err(|e| ParseError { span, ..e })?
21823 };
21824
21825 if format_spec.is_some() && matches!(sub_expr, Expr::Literal(Literal::Text(_)))
21837 {
21838 return Err(ParseError {
21839 kind: crate::error::ParseErrorKind::Custom(format!(
21840 "`{{{hole_after_debug}}}` reads as a text literal padded to a \
21841 width — a value you could write out directly, so this is almost \
21842 always a `:` that was meant literally. If you meant a JSON \
21843 object, a CSS rule, or anything else containing braces, double \
21844 them: `{{{{` and `}}}}` print `{{` and `}}`."
21845 )),
21846 span,
21847 });
21848 }
21849
21850 let format_sym = format_spec.map(|s| self.interner.intern(&s));
21851 parts.push(StringPart::Expr {
21852 value: sub_expr,
21853 format_spec: format_sym,
21854 debug: is_debug,
21855 });
21856
21857 i = j + 1; }
21859 '}' if i + 1 < chars.len() && chars[i + 1] == '}' => {
21860 literal_buf.push('}');
21862 i += 2;
21863 }
21864 _ => {
21865 literal_buf.push(chars[i]);
21866 i += 1;
21867 }
21868 }
21869 }
21870
21871 if !literal_buf.is_empty() {
21873 let sym = self.interner.intern(&literal_buf);
21874 parts.push(StringPart::Literal(sym));
21875 }
21876
21877 Ok(parts)
21878 }
21879
21880 fn parse_primary_or_binary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21882 self.parse_imperative_expr()
21883 }
21884
21885 fn parse_span_literal_from_num(&mut self, first_num_str: &str) -> ParseResult<&'a Expr<'a>> {
21888 use crate::ast::Literal;
21889 use crate::token::CalendarUnit;
21890
21891 let first_num = self.parse_i64_numeral(first_num_str)?;
21892
21893 let unit = match self.peek().kind {
21895 TokenType::CalendarUnit(u) => u,
21896 _ => {
21897 return Err(ParseError {
21898 kind: ParseErrorKind::ExpectedKeyword {
21899 keyword: "calendar unit (day, week, month, year)".to_string(),
21900 },
21901 span: self.current_span(),
21902 });
21903 }
21904 };
21905 self.advance(); let mut total_months: i64 = 0;
21913 let mut total_days: i64 = 0;
21914 let mut total_nanos: i64 = 0;
21915 let mut saw_calendar = false;
21916 let mut saw_clock = false;
21917
21918 fn apply(
21919 unit: CalendarUnit,
21920 n: i64,
21921 months: &mut i64,
21922 days: &mut i64,
21923 nanos: &mut i64,
21924 cal: &mut bool,
21925 clk: &mut bool,
21926 ) {
21927 match unit {
21928 CalendarUnit::Day => {
21929 *days += n;
21930 *cal = true;
21931 }
21932 CalendarUnit::Week => {
21933 *days += n * 7;
21934 *cal = true;
21935 }
21936 CalendarUnit::Month => {
21937 *months += n;
21938 *cal = true;
21939 }
21940 CalendarUnit::Year => {
21941 *months += n * 12;
21942 *cal = true;
21943 }
21944 CalendarUnit::Hour => {
21945 *nanos += n * 3_600_000_000_000;
21946 *clk = true;
21947 }
21948 CalendarUnit::Minute => {
21949 *nanos += n * 60_000_000_000;
21950 *clk = true;
21951 }
21952 CalendarUnit::Second => {
21953 *nanos += n * 1_000_000_000;
21954 *clk = true;
21955 }
21956 CalendarUnit::Millisecond => {
21957 *nanos += n * 1_000_000;
21958 *clk = true;
21959 }
21960 CalendarUnit::Microsecond => {
21961 *nanos += n * 1_000;
21962 *clk = true;
21963 }
21964 CalendarUnit::Nanosecond => {
21965 *nanos += n;
21966 *clk = true;
21967 }
21968 }
21969 }
21970 apply(
21971 unit,
21972 first_num,
21973 &mut total_months,
21974 &mut total_days,
21975 &mut total_nanos,
21976 &mut saw_calendar,
21977 &mut saw_clock,
21978 );
21979
21980 while self.check(&TokenType::And) {
21982 self.advance(); let next_num = match &self.peek().kind {
21986 TokenType::Number(sym) => {
21987 let num_str = self.interner.resolve(*sym).to_string();
21988 self.advance();
21989 self.parse_i64_numeral(&num_str)?
21990 }
21991 _ => break, };
21993
21994 let next_unit = match self.peek().kind {
21996 TokenType::CalendarUnit(u) => {
21997 self.advance();
21998 u
21999 }
22000 _ => break, };
22002
22003 apply(
22004 next_unit,
22005 next_num,
22006 &mut total_months,
22007 &mut total_days,
22008 &mut total_nanos,
22009 &mut saw_calendar,
22010 &mut saw_clock,
22011 );
22012 }
22013
22014 if saw_clock && !saw_calendar {
22015 Ok(self
22016 .ctx
22017 .alloc_imperative_expr(Expr::Literal(Literal::Duration(total_nanos))))
22018 } else {
22019 Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Span {
22020 months: total_months as i32,
22021 days: total_days as i32,
22022 })))
22023 }
22024 }
22025
22026 #[inline(never)]
22032 fn shadowed_literal_word_expr(&mut self, sym: Symbol) -> ParseResult<&'a Expr<'a>> {
22033 use crate::ast::Expr;
22034 let span = self.advance().span;
22035 if self.check(&TokenType::LParen) {
22036 return self.parse_call_expr(sym);
22037 }
22038 self.verify_identifier_access(sym, span)?;
22039 let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
22040 self.parse_field_access_chain(base)
22041 }
22042
22043 fn parse_call_expr(&mut self, function: Symbol) -> ParseResult<&'a Expr<'a>> {
22045 use crate::ast::Expr;
22046
22047 let start_tok = self.current.saturating_sub(1);
22049 self.advance(); let mut args = Vec::new();
22052 if !self.check(&TokenType::RParen) {
22053 loop {
22054 args.push(self.parse_imperative_expr()?);
22055 if !self.check(&TokenType::Comma) {
22056 break;
22057 }
22058 self.advance(); }
22060 }
22061
22062 if !self.check(&TokenType::RParen) {
22063 return Err(ParseError {
22064 kind: ParseErrorKind::ExpectedKeyword {
22065 keyword: ")".to_string(),
22066 },
22067 span: self.current_span(),
22068 });
22069 }
22070 self.advance(); self.record_call_span(&args, start_tok);
22076 let call = self
22077 .ctx
22078 .alloc_imperative_expr(Expr::Call { function, args });
22079 self.parse_field_access_chain(call)
22080 }
22081
22082 fn parse_field_access_chain(&mut self, base: &'a Expr<'a>) -> ParseResult<&'a Expr<'a>> {
22085 use crate::ast::Expr;
22086
22087 let mut result = base;
22088
22089 loop {
22091 if self.check(&TokenType::Dot) {
22092 self.advance(); let start_tok = self.current;
22098 let name = self.expect_identifier()?;
22099 if self.check(&TokenType::LParen) {
22100 self.advance(); let mut args = vec![result]; if !self.check(&TokenType::RParen) {
22103 loop {
22104 args.push(self.parse_imperative_expr()?);
22105 if !self.check(&TokenType::Comma) {
22106 break;
22107 }
22108 self.advance(); }
22110 }
22111 if !self.check(&TokenType::RParen) {
22112 return Err(ParseError {
22113 kind: ParseErrorKind::ExpectedKeyword {
22114 keyword: ")".to_string(),
22115 },
22116 span: self.current_span(),
22117 });
22118 }
22119 self.advance(); self.record_call_span(&args, start_tok);
22121 result = self.ctx.alloc_imperative_expr(Expr::Call {
22122 function: name,
22123 args,
22124 });
22125 } else {
22126 result = self.ctx.alloc_imperative_expr(Expr::FieldAccess {
22127 object: result,
22128 field: name,
22129 });
22130 }
22131 } else if self.check(&TokenType::Possessive) {
22132 self.advance(); let field = self.expect_identifier()?;
22135 result = self.ctx.alloc_imperative_expr(Expr::FieldAccess {
22136 object: result,
22137 field,
22138 });
22139 } else if self.check(&TokenType::LBracket) {
22140 self.advance(); let index = self.parse_imperative_expr()?;
22143
22144 if !self.check(&TokenType::RBracket) {
22145 return Err(ParseError {
22146 kind: ParseErrorKind::ExpectedKeyword {
22147 keyword: "]".to_string(),
22148 },
22149 span: self.current_span(),
22150 });
22151 }
22152 self.advance(); result = self.ctx.alloc_imperative_expr(Expr::Index {
22155 collection: result,
22156 index,
22157 });
22158 } else {
22159 break;
22160 }
22161 }
22162
22163 Ok(result)
22164 }
22165
22166 fn verify_identifier_access(&self, sym: Symbol, span: crate::token::Span) -> ParseResult<()> {
22170 if self.mode != ParserMode::Imperative {
22171 return Ok(());
22172 }
22173
22174 if let Some(crate::drs::OwnershipState::Moved) = self.world_state.get_ownership_by_var(sym)
22176 {
22177 return Err(ParseError {
22178 kind: ParseErrorKind::UseAfterMove {
22179 name: self.interner.resolve(sym).to_string(),
22180 },
22181 span,
22182 });
22183 }
22184
22185 Ok(())
22186 }
22187
22188 fn expect_identifier(&mut self) -> ParseResult<Symbol> {
22189 let token = self.peek().clone();
22190 match &token.kind {
22191 TokenType::Noun(sym) | TokenType::ProperName(sym) | TokenType::Adjective(sym) => {
22193 self.advance();
22194 Ok(*sym)
22195 }
22196 TokenType::Verb { .. } => {
22199 let sym = token.lexeme;
22200 self.advance();
22201 Ok(sym)
22202 }
22203 TokenType::Article(_) => {
22205 let sym = token.lexeme;
22206 self.advance();
22207 Ok(sym)
22208 }
22209 TokenType::Pronoun { .. } | TokenType::Items | TokenType::Values | TokenType::Item | TokenType::Nothing | TokenType::TemporalAdverb(_) |
22217 TokenType::ScopalAdverb(_) |
22218 TokenType::Adverb(_) |
22219 TokenType::Read |
22221 TokenType::Write |
22222 TokenType::File |
22223 TokenType::Console |
22224 TokenType::Merge |
22226 TokenType::Increase |
22227 TokenType::Decrease |
22228 TokenType::Sleep |
22231 TokenType::Expect |
22235 TokenType::Advance |
22236 TokenType::Skip |
22237 TokenType::Given |
22238 TokenType::Note |
22241 TokenType::Warn |
22242 TokenType::Record |
22243 TokenType::During |
22244 TokenType::Swap |
22247 TokenType::Tally |
22249 TokenType::SharedSet |
22250 TokenType::SharedSequence |
22251 TokenType::CollaborativeSequence |
22252 TokenType::Add |
22255 TokenType::Remove |
22256 TokenType::First |
22257 TokenType::Both | TokenType::Either | TokenType::Combined | TokenType::Followed | TokenType::Shared | TokenType::All | TokenType::Some | TokenType::CalendarUnit(_) |
22269 TokenType::Focus(_) |
22271 TokenType::Particle(_) |
22273 TokenType::Preposition(_) |
22275 TokenType::Size | TokenType::Zone | TokenType::Inside | TokenType::Called | TokenType::Launch | TokenType::Task | TokenType::Pipe | TokenType::Stop | TokenType::Receive | TokenType::Send | TokenType::Spawn | TokenType::Await | TokenType::Attempt | TokenType::Following | TokenType::Listen | TokenType::NetConnect | TokenType::Sync | TokenType::Mount | TokenType::Shifted | TokenType::Union | TokenType::Intersection | TokenType::Contains | TokenType::Through | TokenType::Into | TokenType::Push | TokenType::Pop | TokenType::Try | TokenType::New | TokenType::Inspect | TokenType::Check | TokenType::Superlative(_) | TokenType::Comparative(_) | TokenType::Performative(_) | TokenType::Escape => {
22313 let sym = token.lexeme;
22315 self.advance();
22316 Ok(sym)
22317 }
22318 TokenType::Ambiguous { .. } => {
22319 let sym = token.lexeme;
22322 self.advance();
22323 Ok(sym)
22324 }
22325 _ if self.token_is_word_identifier(&token) => {
22333 let sym = token.lexeme;
22334 self.advance();
22335 Ok(sym)
22336 }
22337 _ => Err(ParseError {
22338 kind: ParseErrorKind::ExpectedIdentifier,
22339 span: self.current_span(),
22340 }),
22341 }
22342 }
22343
22344 fn token_is_word_identifier(&self, token: &crate::token::Token) -> bool {
22350 match &token.kind {
22351 TokenType::Number(_)
22353 | TokenType::StringLiteral(_)
22354 | TokenType::InterpolatedString(_)
22355 | TokenType::CharLiteral(_)
22356 | TokenType::EOF
22358 | TokenType::Colon
22359 | TokenType::Indent
22360 | TokenType::Dedent
22361 | TokenType::Newline => false,
22362 _ => {
22363 let s = self.interner.resolve(token.lexeme);
22364 let mut chars = s.chars();
22365 matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_')
22366 && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
22367 }
22368 }
22369 }
22370
22371 fn consume_content_word_for_relative(&mut self) -> ParseResult<Symbol> {
22372 let t = self.advance().clone();
22373 match t.kind {
22374 TokenType::Noun(s) | TokenType::Adjective(s) => Ok(s),
22375 TokenType::ProperName(s) => Ok(s),
22376 TokenType::Verb { lemma, .. } => Ok(lemma),
22377 other => Err(ParseError {
22378 kind: ParseErrorKind::ExpectedContentWord { found: other },
22379 span: self.current_span(),
22380 }),
22381 }
22382 }
22383
22384 fn check_modal(&self) -> bool {
22385 matches!(
22386 self.peek().kind,
22387 TokenType::Must
22388 | TokenType::Shall
22389 | TokenType::Should
22390 | TokenType::Can
22391 | TokenType::May
22392 | TokenType::Cannot
22393 | TokenType::Could
22394 | TokenType::Would
22395 | TokenType::Might
22396 )
22397 }
22398
22399 fn possessive_np_head_follows(&self) -> bool {
22403 let mut i = self.current + 1;
22404 while let Some(token) = self.tokens.get(i) {
22405 match &token.kind {
22406 TokenType::Adjective(_) => i += 1,
22407 TokenType::Noun(_) => return true,
22408 TokenType::Ambiguous {
22411 primary,
22412 alternatives,
22413 } => {
22414 return matches!(**primary, TokenType::Noun(_))
22415 || alternatives.iter().any(|t| matches!(t, TokenType::Noun(_)));
22416 }
22417 _ => return false,
22418 }
22419 }
22420 false
22421 }
22422
22423 fn check_pronoun(&self) -> bool {
22424 match &self.peek().kind {
22425 TokenType::Pronoun { case, .. } => {
22426 if matches!(case, Case::Possessive) {
22427 if self.noun_priority_mode {
22430 return false;
22431 }
22432 if self.possessive_np_head_follows() {
22438 return false;
22439 }
22440 }
22441 true
22442 }
22443 TokenType::Ambiguous {
22444 primary,
22445 alternatives,
22446 } => {
22447 let has_possessive = matches!(
22448 **primary,
22449 TokenType::Pronoun {
22450 case: Case::Possessive,
22451 ..
22452 }
22453 ) || alternatives.iter().any(|t| {
22454 matches!(
22455 t,
22456 TokenType::Pronoun {
22457 case: Case::Possessive,
22458 ..
22459 }
22460 )
22461 });
22462 if self.noun_priority_mode && has_possessive {
22464 return false;
22465 }
22466 if has_possessive && self.possessive_np_head_follows() {
22470 return false;
22471 }
22472 matches!(**primary, TokenType::Pronoun { .. })
22473 || alternatives
22474 .iter()
22475 .any(|t| matches!(t, TokenType::Pronoun { .. }))
22476 }
22477 _ => false,
22478 }
22479 }
22480
22481 fn peek_heads_reduced_relative_participle(&self) -> bool {
22488 let idx = if matches!(
22489 self.peek().kind,
22490 TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
22491 ) && self
22492 .tokens
22493 .get(self.current + 1)
22494 .map_or(false, |t| self.kind_is_verb(&t.kind))
22495 {
22496 self.current + 1
22497 } else {
22498 self.current
22499 };
22500 let Some(t) = self.tokens.get(idx) else {
22501 return false;
22502 };
22503 if !self.kind_is_verb(&t.kind) {
22504 return false;
22505 }
22506 if let TokenType::Verb { lemma, .. } = &t.kind {
22509 return !matches!(
22510 self.interner.resolve(*lemma).to_lowercase().as_str(),
22511 "have" | "be" | "do"
22512 );
22513 }
22514 true
22515 }
22516
22517 fn has_comma_before_clause_end(&self) -> bool {
22522 for i in self.current..self.tokens.len() {
22523 match &self.tokens[i].kind {
22524 TokenType::Period | TokenType::EOF => return false,
22525 TokenType::Comma => return true,
22526 _ => {}
22527 }
22528 }
22529 false
22530 }
22531
22532 fn try_consume_reduced_relative(
22547 &mut self,
22548 term: Term<'a>,
22549 ) -> ParseResult<Option<&'a LogicExpr<'a>>> {
22550 if !self.peek_heads_reduced_relative_participle() {
22551 return Ok(None);
22552 }
22553 let mut preds: Vec<&'a LogicExpr<'a>> = Vec::new();
22554 if matches!(
22556 self.peek().kind,
22557 TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
22558 ) {
22559 let adv = match self.peek().kind {
22560 TokenType::Adverb(s) | TokenType::TemporalAdverb(s) => s,
22561 _ => unreachable!(),
22562 };
22563 self.advance();
22564 preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22565 name: adv,
22566 args: self.ctx.terms.alloc_slice([term]),
22567 world: None,
22568 }));
22569 }
22570 let (red_verb, _t, _a, _c) = self.consume_verb_with_metadata();
22571 preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22572 name: red_verb,
22573 args: self.ctx.terms.alloc_slice([term]),
22574 world: None,
22575 }));
22576 loop {
22580 let prep = if self.check_preposition() {
22581 match self.advance().kind {
22582 TokenType::Preposition(s) => s,
22583 _ => break,
22584 }
22585 } else if self.check(&TokenType::To)
22586 && matches!(
22587 self.tokens.get(self.current + 1).map(|t| &t.kind),
22588 Some(TokenType::Article(_))
22589 | Some(TokenType::Noun(_))
22590 | Some(TokenType::ProperName(_))
22591 )
22592 {
22593 self.advance();
22594 self.interner.intern("To")
22595 } else {
22596 break;
22597 };
22598 if self.check_number() {
22599 let m = self.parse_measure_phrase()?;
22600 preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22601 name: prep,
22602 args: self.ctx.terms.alloc_slice([term, *m]),
22603 world: None,
22604 }));
22605 } else if self.check_article() || self.check_content_word() {
22606 let obj = self.parse_noun_phrase(true)?;
22607 preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22608 name: prep,
22609 args: self.ctx.terms.alloc_slice([term, Term::Constant(obj.noun)]),
22610 world: None,
22611 }));
22612 } else {
22613 break;
22614 }
22615 }
22616 let mut result = preds[0];
22617 for p in &preds[1..] {
22618 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22619 left: result,
22620 op: TokenType::And,
22621 right: p,
22622 });
22623 }
22624 Ok(Some(result))
22625 }
22626
22627 fn parse_list_member_np(
22640 &mut self,
22641 np: &crate::ast::NounPhrase<'a>,
22642 bare_definite_distinct: bool,
22643 ) -> ParseResult<(Term<'a>, Option<Symbol>, Option<&'a LogicExpr<'a>>)> {
22644 let has_rel = self.check(&TokenType::Who)
22645 || self.check(&TokenType::That)
22646 || self.check(&TokenType::Where)
22647 || self.check(&TokenType::Whose);
22648 let is_desc = has_rel
22649 || (bare_definite_distinct && np.definiteness.is_some())
22650 || !np.adjectives.is_empty()
22651 || np.possessor.is_some()
22652 || !np.pps.is_empty();
22653 if is_desc {
22654 let var = self.next_var_name();
22655 let term = Term::Variable(var);
22656 let mut r = self.nominal_predication(term, np);
22657 if let Some(poss) = np.possessor {
22658 let possesses = self.interner.intern("Possesses");
22659 let pr = self.ctx.exprs.alloc(LogicExpr::Predicate {
22660 name: possesses,
22661 args: self
22662 .ctx
22663 .terms
22664 .alloc_slice([Term::Constant(poss.noun), term]),
22665 world: None,
22666 });
22667 r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22668 left: r,
22669 op: TokenType::And,
22670 right: pr,
22671 });
22672 }
22673 for pp in np.pps {
22674 let pp_sub = self.substitute_pp_self_term(pp, term);
22675 r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22676 left: r,
22677 op: TokenType::And,
22678 right: pp_sub,
22679 });
22680 }
22681 if let Some(rc) = self.try_attach_relative(term)? {
22683 r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22684 left: r,
22685 op: TokenType::And,
22686 right: rc,
22687 });
22688 }
22689 if let Some(rr) = self.try_consume_reduced_relative(term)? {
22693 r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22694 left: r,
22695 op: TokenType::And,
22696 right: rr,
22697 });
22698 }
22699 Ok((term, Some(var), Some(r)))
22700 } else {
22701 Ok((Term::Constant(np.noun), None, None))
22702 }
22703 }
22704
22705 fn finish_enumerated_identity(
22711 &mut self,
22712 subject: &crate::ast::NounPhrase<'a>,
22713 is_negated: bool,
22714 first: (Term<'a>, Option<Symbol>, Option<&'a LogicExpr<'a>>),
22715 ) -> ParseResult<&'a LogicExpr<'a>> {
22716 let mut members: Vec<Term<'a>> = Vec::new();
22717 let mut member_vars: Vec<Symbol> = Vec::new();
22718 let mut member_restrictors: Vec<&'a LogicExpr<'a>> = Vec::new();
22719 let (t0, v0, r0) = first;
22720 members.push(t0);
22721 if let Some(v) = v0 {
22722 member_vars.push(v);
22723 }
22724 if let Some(r) = r0 {
22725 member_restrictors.push(r);
22726 }
22727 while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
22728 while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
22730 self.advance();
22731 }
22732 if !(self.check_article() || self.check_content_word()) {
22733 break;
22734 }
22735 let item = self.parse_noun_phrase(true)?;
22736 let (t, v, r) = self.parse_list_member_np(&item, false)?;
22737 members.push(t);
22738 if let Some(vv) = v {
22739 member_vars.push(vv);
22740 }
22741 if let Some(rr) = r {
22742 member_restrictors.push(rr);
22743 }
22744 }
22745 let group = Term::Group(self.ctx.terms.alloc_slice(members));
22746 let identity: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Identity {
22747 left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
22748 right: self.ctx.terms.alloc(group),
22749 });
22750 let mut result = if is_negated {
22751 &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
22752 op: TokenType::Not,
22753 operand: identity,
22754 })
22755 } else {
22756 identity
22757 };
22758 for rc in member_restrictors {
22759 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22760 left: result,
22761 op: TokenType::And,
22762 right: rc,
22763 });
22764 }
22765 for v in member_vars.into_iter().rev() {
22766 result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
22767 kind: QuantifierKind::Existential,
22768 variable: v,
22769 body: result,
22770 island_id: self.current_island,
22771 });
22772 }
22773 self.wrap_with_definiteness_full(subject, result)
22774 }
22775
22776 fn try_parse_list_subject(
22782 &mut self,
22783 first: &crate::ast::NounPhrase<'a>,
22784 ) -> ParseResult<Option<&'a LogicExpr<'a>>> {
22785 let cp = self.checkpoint();
22786 let mut member_terms: Vec<Term<'a>> = Vec::new();
22793 let mut member_vars: Vec<Symbol> = Vec::new();
22794 let mut member_restrictors: Vec<&'a LogicExpr<'a>> = Vec::new();
22795 macro_rules! add_member {
22796 ($np:expr) => {{
22797 let np = $np;
22798 match self.parse_list_member_np(&np, true) {
22799 Ok((term, var, restrictor)) => {
22800 member_terms.push(term);
22801 if let Some(v) = var {
22802 member_vars.push(v);
22803 }
22804 if let Some(r) = restrictor {
22805 member_restrictors.push(r);
22806 }
22807 }
22808 Err(_) => {
22809 self.restore(cp);
22810 return Ok(None);
22811 }
22812 }
22813 }};
22814 }
22815 add_member!(*first);
22816 if !self.check(&TokenType::Comma) && !self.check(&TokenType::And) {
22823 self.restore(cp);
22824 return Ok(None);
22825 }
22826 let mut saw_and = false;
22827 loop {
22828 let mut advanced = false;
22829 if self.check(&TokenType::Comma) {
22830 self.advance();
22831 advanced = true;
22832 }
22833 if self.check(&TokenType::And) {
22834 self.advance();
22835 saw_and = true;
22836 advanced = true;
22837 }
22838 if !advanced {
22839 break;
22840 }
22841 if !(self.check_article() || self.check_content_word()) {
22842 break;
22843 }
22844 let np = match self.parse_noun_phrase(true) {
22845 Ok(np) => np,
22846 Err(_) => {
22847 self.restore(cp);
22848 return Ok(None);
22849 }
22850 };
22851 add_member!(np);
22852 }
22853
22854 if !saw_and || member_terms.len() < 2 {
22861 self.restore(cp);
22862 return Ok(None);
22863 }
22864
22865 let copula_past = self.check(&TokenType::Were);
22866 if !(self.check(&TokenType::Are)
22867 || self.check(&TokenType::Were)
22868 || self.check(&TokenType::Is))
22869 {
22870 self.restore(cp);
22871 return Ok(None);
22872 }
22873 self.advance(); if self.check(&TokenType::All) {
22876 self.advance();
22877 }
22878
22879 if matches!(self.peek().kind, TokenType::Cardinal(_))
22883 && self
22884 .tokens
22885 .get(self.current + 1)
22886 .map(|t| {
22887 self.interner
22888 .resolve(t.lexeme)
22889 .eq_ignore_ascii_case("different")
22890 })
22891 .unwrap_or(false)
22892 {
22893 self.advance();
22894 }
22895
22896 let mut conjuncts: Vec<&'a LogicExpr<'a>> = member_restrictors;
22900
22901 let is_different = self
22902 .interner
22903 .resolve(self.peek().lexeme)
22904 .eq_ignore_ascii_case("different");
22905 if is_different {
22906 self.advance(); if self.check_content_word() {
22909 let type_noun = self.consume_content_word()?;
22910 for t in &member_terms {
22911 conjuncts.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22912 name: type_noun,
22913 args: self.ctx.terms.alloc_slice([*t]),
22914 world: None,
22915 }));
22916 if let Term::Constant(item) = t {
22923 self.drs.register_item_category(*item, type_noun);
22924 }
22925 }
22926 }
22927 for i in 0..member_terms.len() {
22930 for j in (i + 1)..member_terms.len() {
22931 let id = self.ctx.exprs.alloc(LogicExpr::Identity {
22932 left: self.ctx.terms.alloc(member_terms[i]),
22933 right: self.ctx.terms.alloc(member_terms[j]),
22934 });
22935 conjuncts.push(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
22936 op: TokenType::Not,
22937 operand: id,
22938 }));
22939 }
22940 }
22941 } else if self.check_article() || self.check_content_word() {
22942 let pred_np = self.parse_noun_phrase(true)?;
22944 for t in &member_terms {
22945 conjuncts.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22946 name: pred_np.noun,
22947 args: self.ctx.terms.alloc_slice([*t]),
22948 world: None,
22949 }));
22950 }
22951 } else {
22952 self.restore(cp);
22953 return Ok(None);
22954 }
22955
22956 if conjuncts.is_empty() {
22957 self.restore(cp);
22958 return Ok(None);
22959 }
22960 let mut result = conjuncts[0];
22961 for c in &conjuncts[1..] {
22962 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22963 left: result,
22964 op: TokenType::And,
22965 right: c,
22966 });
22967 }
22968 if copula_past {
22969 result = self.ctx.exprs.alloc(LogicExpr::Temporal {
22970 operator: TemporalOperator::Past,
22971 body: result,
22972 });
22973 }
22974 for &var in member_vars.iter().rev() {
22977 result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
22978 kind: QuantifierKind::Existential,
22979 variable: var,
22980 body: result,
22981 island_id: self.current_island,
22982 });
22983 }
22984 Ok(Some(result))
22985 }
22986
22987 fn parse_atom(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
22995 self.enter_recursion()?;
22996 let result = self.parse_atom_inner();
22997 self.leave_recursion();
22998 result
22999 }
23000
23001 fn parse_atom_inner(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
23002 self.pending_subject_restriction = None;
23006
23007 if self.check_focus() {
23009 return self.parse_focus();
23010 }
23011
23012 if self.check_measure() {
23014 return self.parse_measure();
23015 }
23016
23017 if let TokenType::Verb {
23022 lemma,
23023 aspect: Aspect::Progressive,
23024 time: Time::None,
23025 ..
23026 } = self.peek().kind
23027 {
23028 let noun_next = matches!(
23029 self.tokens.get(self.current + 1).map(|t| t.kind.clone()),
23030 Some(TokenType::Noun(_))
23031 );
23032 let finite_after = matches!(
23033 self.tokens.get(self.current + 2).map(|t| t.kind.clone()),
23034 Some(
23035 TokenType::Can
23036 | TokenType::Could
23037 | TokenType::May
23038 | TokenType::Must
23039 | TokenType::Should
23040 | TokenType::Would
23041 | TokenType::Is
23042 | TokenType::Are
23043 )
23044 );
23045 if noun_next && finite_after {
23046 self.advance(); let noun_sym = match self.advance().kind {
23048 TokenType::Noun(n) => n,
23049 _ => unreachable!("guarded by noun_next"),
23050 };
23051
23052 let event_var = self.get_event_var();
23053 let activity = self
23054 .ctx
23055 .exprs
23056 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23057 event_var,
23058 verb: lemma,
23059 roles: self
23060 .ctx
23061 .roles
23062 .alloc_slice(vec![(ThematicRole::Theme, Term::Constant(noun_sym))]),
23063 modifiers: self.ctx.syms.alloc_slice(vec![]),
23064 suppress_existential: false,
23065 world: None,
23066 })));
23067
23068 let modal_tok = if matches!(
23069 self.peek().kind,
23070 TokenType::Can
23071 | TokenType::Could
23072 | TokenType::May
23073 | TokenType::Must
23074 | TokenType::Should
23075 | TokenType::Would
23076 ) {
23077 Some(self.advance().kind.clone())
23078 } else {
23079 None
23080 };
23081 if matches!(
23083 self.peek().kind,
23084 TokenType::Is | TokenType::Are | TokenType::Be
23085 ) {
23086 self.advance();
23087 } else if matches!(self.peek().kind, TokenType::Verb { .. })
23088 && self
23089 .interner
23090 .resolve(self.peek().lexeme)
23091 .eq_ignore_ascii_case("be")
23092 {
23093 self.advance();
23094 }
23095
23096 if let TokenType::Adjective(adj) = self.peek().kind {
23097 self.advance();
23098 let pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23099 name: adj,
23100 args: self.ctx.terms.alloc_slice([Term::Proposition(activity)]),
23101 world: None,
23102 });
23103 let result = match modal_tok {
23104 Some(tok) => {
23105 let force = match tok {
23106 TokenType::Must => 1.0,
23107 TokenType::Should | TokenType::Would => 0.6,
23108 TokenType::Could => 0.4,
23109 _ => 0.5,
23110 };
23111 &*self.ctx.exprs.alloc(LogicExpr::Modal {
23112 vector: crate::ast::ModalVector::new(
23113 crate::ast::ModalDomain::Alethic,
23114 force,
23115 crate::ast::ModalFlavor::Root,
23116 ),
23117 operand: pred,
23118 })
23119 }
23120 None => pred,
23121 };
23122 return Ok(result);
23123 }
23124 return Err(ParseError {
23125 kind: ParseErrorKind::ExpectedContentWord {
23126 found: self.peek().kind.clone(),
23127 },
23128 span: self.current_span(),
23129 });
23130 }
23131 }
23132
23133 if self.check_quantifier() {
23134 self.advance();
23135 return self.parse_quantified();
23136 }
23137
23138 if self.check_npi_quantifier() {
23139 return self.parse_npi_quantified();
23140 }
23141
23142 if self.check_temporal_npi() {
23143 return self.parse_temporal_npi();
23144 }
23145
23146 if self.match_token(&[TokenType::LParen]) {
23147 let expr = self.parse_sentence()?;
23148 self.consume(TokenType::RParen)?;
23149 return Ok(expr);
23150 }
23151
23152 if self.check_pronoun() {
23154 let token = self.advance().clone();
23155 let (gender, number) = match &token.kind {
23156 TokenType::Pronoun { gender, number, .. } => (*gender, *number),
23157 TokenType::Ambiguous {
23158 primary,
23159 alternatives,
23160 } => {
23161 if let TokenType::Pronoun { gender, number, .. } = **primary {
23162 (gender, number)
23163 } else {
23164 alternatives
23165 .iter()
23166 .find_map(|t| {
23167 if let TokenType::Pronoun { gender, number, .. } = t {
23168 Some((*gender, *number))
23169 } else {
23170 None
23171 }
23172 })
23173 .unwrap_or((Gender::Unknown, Number::Singular))
23174 }
23175 }
23176 _ => (Gender::Unknown, Number::Singular),
23177 };
23178
23179 let token_text = self.interner.resolve(token.lexeme);
23180
23181 if token_text.eq_ignore_ascii_case("it") && self.check_verb() {
23184 if let TokenType::Verb { lemma, time, .. } = &self.peek().kind {
23185 let lemma_str = self.interner.resolve(*lemma);
23186 if Lexer::is_weather_verb(lemma_str) {
23187 let verb = *lemma;
23188 let verb_time = *time;
23189 self.advance(); let event_var = self.get_event_var();
23192 let suppress_existential = self.drs.in_conditional_antecedent();
23193 if suppress_existential {
23194 let event_class = self.interner.intern("Event");
23195 self.drs.introduce_event_referent(event_var, event_class);
23196 }
23197 let neo_event =
23198 self.ctx
23199 .exprs
23200 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23201 event_var,
23202 verb,
23203 roles: self.ctx.roles.alloc_slice(vec![]), modifiers: self.ctx.syms.alloc_slice(vec![]),
23205 suppress_existential,
23206 world: None,
23207 })));
23208
23209 return Ok(match verb_time {
23210 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
23211 operator: TemporalOperator::Past,
23212 body: neo_event,
23213 }),
23214 Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
23215 operator: TemporalOperator::Future,
23216 body: neo_event,
23217 }),
23218 _ => neo_event,
23219 });
23220 }
23221 }
23222 }
23223
23224 let resolved = if token_text.eq_ignore_ascii_case("i") {
23226 ResolvedPronoun::Constant(self.interner.intern("Speaker"))
23227 } else if token_text.eq_ignore_ascii_case("you") {
23228 ResolvedPronoun::Constant(self.interner.intern("Addressee"))
23229 } else {
23230 self.resolve_pronoun(gender, number)?
23232 };
23233
23234 if self
23236 .interner
23237 .resolve(self.peek().lexeme)
23238 .eq_ignore_ascii_case("hereby")
23239 {
23240 self.advance();
23241 }
23242
23243 if self.check_performative() {
23245 if let TokenType::Performative(act) = self.advance().kind.clone() {
23246 let sym = match resolved {
23247 ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
23248 };
23249
23250 if self.check(&TokenType::To) {
23254 self.advance(); if !self.is_at_end() && !self.check(&TokenType::Period) {
23257 let lexeme = self.interner.resolve(self.peek().lexeme).to_string();
23262 let verb_name = lexeme
23263 .chars()
23264 .next()
23265 .map(|c| c.to_uppercase().collect::<String>() + &lexeme[1..])
23266 .unwrap_or(lexeme);
23267 let infinitive_verb = self.interner.intern(&verb_name);
23268 self.advance(); let content = self.performative_predicate(infinitive_verb, sym);
23270 return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23271 performer: sym,
23272 act_type: act,
23273 content,
23274 }));
23275 }
23276 }
23277
23278 if self.check(&TokenType::That) {
23280 self.advance();
23281 let content = self.parse_sentence()?;
23282 return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23283 performer: sym,
23284 act_type: act,
23285 content,
23286 }));
23287 }
23288
23289 let at_end = self.check(&TokenType::Period) || self.is_at_end();
23295 let lone_object_then_end = matches!(
23296 self.peek().kind,
23297 TokenType::Pronoun { .. } | TokenType::ProperName(_)
23298 ) && self.current + 1 < self.tokens.len()
23299 && matches!(
23300 self.tokens[self.current + 1].kind,
23301 TokenType::Period | TokenType::EOF
23302 );
23303 if at_end || lone_object_then_end {
23304 let content = self.performative_predicate(act, sym);
23305 return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23306 performer: sym,
23307 act_type: act,
23308 content,
23309 }));
23310 }
23311
23312 let content = self.parse_sentence()?;
23315 return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23316 performer: sym,
23317 act_type: act,
23318 content,
23319 }));
23320 }
23321 }
23322
23323 return match resolved {
23326 ResolvedPronoun::Variable(sym) => self.parse_predicate_with_subject_as_var(sym),
23327 ResolvedPronoun::Constant(sym) => self.parse_predicate_with_subject(sym),
23328 };
23329 }
23330
23331 let _had_both = self.match_token(&[TokenType::Both]);
23334
23335 let mut subject = self.parse_noun_phrase(true)?;
23336
23337 if self.check(&TokenType::All)
23342 || self.check(&TokenType::Both)
23343 || self
23344 .interner
23345 .resolve(self.peek().lexeme)
23346 .eq_ignore_ascii_case("each")
23347 {
23348 self.advance();
23349 self.collective_mode = false; self.distributive_marker = true;
23351 }
23352
23353 if subject.definiteness == Some(Definiteness::Indefinite)
23359 || subject.definiteness == Some(Definiteness::Distal)
23360 {
23361 let gender = Self::infer_noun_gender(self.interner.resolve(subject.noun));
23362 let number = if Self::is_plural_noun(self.interner.resolve(subject.noun)) {
23363 Number::Plural
23364 } else {
23365 Number::Singular
23366 };
23367 self.drs
23369 .introduce_referent(subject.noun, subject.noun, gender, number);
23370 }
23371
23372 if self.check(&TokenType::And) {
23374 match self.try_parse_plural_subject(&subject) {
23375 Ok(Some(result)) => return Ok(result),
23376 Ok(None) => {} Err(e) => return Err(e), }
23379 }
23380
23381 if self.check_scopal_adverb() {
23383 return self.parse_scopal_adverb(&subject);
23384 }
23385
23386 if self.check(&TokenType::Comma)
23390 && matches!(
23391 self.tokens.get(self.current + 1).map(|t| &t.kind),
23392 Some(TokenType::Who) | Some(TokenType::That)
23393 )
23394 {
23395 self.advance(); self.advance(); let rc = self.parse_relative_clause(subject.noun)?;
23399 if self.check(&TokenType::Comma) {
23400 self.advance(); }
23402 let main = self.parse_predicate_with_subject(subject.noun)?;
23404 let conj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23405 left: main,
23406 op: TokenType::And,
23407 right: rc,
23408 });
23409 return self.wrap_with_definiteness_full(&subject, conj);
23410 }
23411
23412 if self.check(&TokenType::Comma)
23419 || self.check(&TokenType::Who)
23420 || self.check(&TokenType::That)
23421 || self.check(&TokenType::And)
23428 || (self.peek_heads_reduced_relative_participle() && self.has_comma_before_clause_end())
23435 {
23436 if let Some(result) = self.try_parse_list_subject(&subject)? {
23437 return Ok(result);
23438 }
23439 }
23440
23441 if self.check(&TokenType::Comma) {
23443 let saved_pos = self.backtrack_point();
23444 self.advance(); if self.check_pronoun() {
23448 let topic_attempt = self.try_parse(|p| {
23449 let token = p.peek().clone();
23450 let pronoun_features = match &token.kind {
23451 TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
23452 TokenType::Ambiguous {
23453 primary,
23454 alternatives,
23455 } => {
23456 if let TokenType::Pronoun { gender, number, .. } = **primary {
23457 Some((gender, number))
23458 } else {
23459 alternatives.iter().find_map(|t| {
23460 if let TokenType::Pronoun { gender, number, .. } = t {
23461 Some((*gender, *number))
23462 } else {
23463 None
23464 }
23465 })
23466 }
23467 }
23468 _ => None,
23469 };
23470
23471 if let Some((gender, number)) = pronoun_features {
23472 p.advance(); let resolved = p.resolve_pronoun(gender, number)?;
23474 let resolved_term = match resolved {
23475 ResolvedPronoun::Variable(s) => Term::Variable(s),
23476 ResolvedPronoun::Constant(s) => Term::Constant(s),
23477 };
23478
23479 if p.check_verb() {
23480 let verb = p.consume_verb();
23481 let predicate = p.ctx.exprs.alloc(LogicExpr::Predicate {
23482 name: verb,
23483 args: p
23484 .ctx
23485 .terms
23486 .alloc_slice([resolved_term, Term::Constant(subject.noun)]),
23487 world: None,
23488 });
23489 p.wrap_with_definiteness_full(&subject, predicate)
23490 } else {
23491 Err(ParseError {
23492 kind: ParseErrorKind::ExpectedVerb {
23493 found: p.peek().kind.clone(),
23494 },
23495 span: p.current_span(),
23496 })
23497 }
23498 } else {
23499 Err(ParseError {
23500 kind: ParseErrorKind::ExpectedContentWord { found: token.kind },
23501 span: p.current_span(),
23502 })
23503 }
23504 });
23505
23506 if let Some(result) = topic_attempt {
23507 return Ok(result);
23508 }
23509 }
23510
23511 if self.check_content_word() {
23513 let topic_attempt = self.try_parse(|p| {
23514 let real_subject = p.parse_noun_phrase(true)?;
23515 if p.check_verb() {
23516 let verb = p.consume_verb();
23517 let predicate = p.ctx.exprs.alloc(LogicExpr::Predicate {
23518 name: verb,
23519 args: p.ctx.terms.alloc_slice([
23520 Term::Constant(real_subject.noun),
23521 Term::Constant(subject.noun),
23522 ]),
23523 world: None,
23524 });
23525 p.wrap_with_definiteness_full(&subject, predicate)
23526 } else {
23527 Err(ParseError {
23528 kind: ParseErrorKind::ExpectedVerb {
23529 found: p.peek().kind.clone(),
23530 },
23531 span: p.current_span(),
23532 })
23533 }
23534 });
23535
23536 if let Some(result) = topic_attempt {
23537 return Ok(result);
23538 }
23539 }
23540
23541 self.backtrack_to(&saved_pos);
23543 }
23544
23545 let mut relative_clause: Option<(Symbol, &'a LogicExpr<'a>)> = None;
23547 if self.check(&TokenType::That) || self.check(&TokenType::Who) {
23548 self.advance();
23549 let var_name = self.next_var_name();
23550 let rel_pred = self.parse_relative_clause(var_name)?;
23551 relative_clause = Some((var_name, rel_pred));
23552 } else if self.check(&TokenType::Where) {
23553 let var_name = self.next_var_name();
23560 let rel_pred = self.parse_where_relative(var_name)?;
23561 relative_clause = Some((var_name, rel_pred));
23562 } else if matches!(self.peek().kind, TokenType::Article(_))
23563 && self.is_contact_clause_pattern()
23564 {
23565 let var_name = self.next_var_name();
23568 let rel_pred = self.parse_relative_clause(var_name)?;
23569 relative_clause = Some((var_name, rel_pred));
23570 }
23571
23572 if let Some((var_name, rel_clause)) = relative_clause {
23574 if self.check_presup_trigger()
23579 && !self.is_followed_by_np_object()
23580 && self.is_followed_by_gerund()
23581 {
23582 let presup_kind = self.consume_presup_trigger();
23583 let main_pred = self.parse_presupposition_for_term(
23584 Term::Variable(var_name),
23585 presup_kind,
23586 false,
23587 )?;
23588 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23589 name: subject.noun,
23590 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23591 world: None,
23592 });
23593 let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23594 left: type_pred,
23595 op: TokenType::And,
23596 right: rel_clause,
23597 });
23598 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23599 left: inner,
23600 op: TokenType::And,
23601 right: main_pred,
23602 });
23603 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23604 kind: QuantifierKind::Existential,
23605 variable: var_name,
23606 body,
23607 island_id: self.current_island,
23608 }));
23609 }
23610 if self.check_verb() {
23611 let (verb, verb_time, _, _) = self.consume_verb_with_metadata();
23612 let var_term = Term::Variable(var_name);
23613
23614 let main_pred = if self.at_clause_boundary() {
23618 let event_var = self.get_event_var();
23619 let suppress_existential = self.drs.in_conditional_antecedent();
23620 let mut modifiers = vec![];
23621 if verb_time == Time::Past {
23622 modifiers.push(self.interner.intern("Past"));
23623 }
23624 &*self
23625 .ctx
23626 .exprs
23627 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23628 event_var,
23629 verb,
23630 roles: self
23631 .ctx
23632 .roles
23633 .alloc_slice(vec![(ThematicRole::Agent, var_term)]),
23634 modifiers: self.ctx.syms.alloc_slice(modifiers),
23635 suppress_existential,
23636 world: None,
23637 })))
23638 } else {
23639 self.current -= 1; self.parse_predicate_with_subject_as_var(var_name)?
23641 };
23642
23643 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23644 name: subject.noun,
23645 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23646 world: None,
23647 });
23648
23649 let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23650 left: type_pred,
23651 op: TokenType::And,
23652 right: rel_clause,
23653 });
23654
23655 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23656 left: inner,
23657 op: TokenType::And,
23658 right: main_pred,
23659 });
23660
23661 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23662 kind: QuantifierKind::Existential,
23663 variable: var_name,
23664 body,
23665 island_id: self.current_island,
23666 }));
23667 }
23668
23669 if self.is_at_end() || self.check(&TokenType::Period) || self.check(&TokenType::Comma) {
23672 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23673 name: subject.noun,
23674 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23675 world: None,
23676 });
23677
23678 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23679 left: type_pred,
23680 op: TokenType::And,
23681 right: rel_clause,
23682 });
23683
23684 let uniqueness_body = if subject.definiteness == Some(Definiteness::Definite) {
23686 let y_var = self.next_var_name();
23687 let type_pred_y = self.ctx.exprs.alloc(LogicExpr::Predicate {
23688 name: subject.noun,
23689 args: self.ctx.terms.alloc_slice([Term::Variable(y_var)]),
23690 world: None,
23691 });
23692 let identity = self.ctx.exprs.alloc(LogicExpr::Identity {
23693 left: self.ctx.terms.alloc(Term::Variable(y_var)),
23694 right: self.ctx.terms.alloc(Term::Variable(var_name)),
23695 });
23696 let uniqueness_cond = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23697 left: type_pred_y,
23698 op: TokenType::Implies,
23699 right: identity,
23700 });
23701 let uniqueness = self.ctx.exprs.alloc(LogicExpr::Quantifier {
23702 kind: QuantifierKind::Universal,
23703 variable: y_var,
23704 body: uniqueness_cond,
23705 island_id: self.current_island,
23706 });
23707 self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23708 left: body,
23709 op: TokenType::And,
23710 right: uniqueness,
23711 })
23712 } else {
23713 body
23714 };
23715
23716 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23717 kind: QuantifierKind::Existential,
23718 variable: var_name,
23719 body: uniqueness_body,
23720 island_id: self.current_island,
23721 }));
23722 }
23723
23724 relative_clause = Some((var_name, rel_clause));
23726 }
23727
23728 if self.check(&TokenType::Identity) {
23730 self.advance();
23731 let right = self.consume_content_word()?;
23732 return Ok(self.ctx.exprs.alloc(LogicExpr::Identity {
23733 left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
23734 right: self.ctx.terms.alloc(Term::Constant(right)),
23735 }));
23736 }
23737
23738 if self.check_modal() {
23739 if let Some((var_name, rel_clause)) = relative_clause {
23740 let modal_pred = self.parse_aspect_chain_with_term(Term::Variable(var_name))?;
23741
23742 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23743 name: subject.noun,
23744 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23745 world: None,
23746 });
23747
23748 let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23749 left: type_pred,
23750 op: TokenType::And,
23751 right: rel_clause,
23752 });
23753
23754 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23755 left: inner,
23756 op: TokenType::And,
23757 right: modal_pred,
23758 });
23759
23760 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23761 kind: QuantifierKind::Existential,
23762 variable: var_name,
23763 body,
23764 island_id: self.current_island,
23765 }));
23766 }
23767
23768 let modal_pred = self.parse_aspect_chain(subject.noun)?;
23769 return self.wrap_with_definiteness_full(&subject, modal_pred);
23770 }
23771
23772 let leading_adverb = if matches!(
23791 self.peek().kind,
23792 TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
23793 ) && self
23794 .tokens
23795 .get(self.current + 1)
23796 .map_or(false, |t| self.kind_is_verb(&t.kind))
23797 {
23798 match self.peek().kind {
23799 TokenType::Adverb(s) | TokenType::TemporalAdverb(s) => Some(s),
23800 _ => None,
23801 }
23802 } else {
23803 None
23804 };
23805 let head_verb_idx = if leading_adverb.is_some() {
23806 self.current + 1
23807 } else {
23808 self.current
23809 };
23810 let head_verb_is_aux = if let Some(TokenType::Verb { lemma, .. }) =
23811 self.tokens.get(head_verb_idx).map(|t| &t.kind)
23812 {
23813 matches!(
23814 self.interner.resolve(*lemma).to_lowercase().as_str(),
23815 "have" | "be" | "do"
23816 )
23817 } else {
23818 false
23819 };
23820 let heads_reduced_rel = self
23821 .tokens
23822 .get(head_verb_idx)
23823 .map_or(false, |t| self.kind_is_verb(&t.kind));
23824 if subject.definiteness.is_some()
23825 && heads_reduced_rel
23826 && self.pending_time.is_none()
23827 && !head_verb_is_aux
23828 {
23829 if let Some(restrs) = self.try_parse(|p| {
23830 let placeholder = p.interner.intern("_PP_SELF_");
23831 let mut preds: Vec<&'a LogicExpr<'a>> = Vec::new();
23832 if let Some(adv) = leading_adverb {
23833 p.advance();
23834 preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23835 name: adv,
23836 args: p.ctx.terms.alloc_slice([Term::Variable(placeholder)]),
23837 world: None,
23838 }));
23839 }
23840 let (red_verb, _t, _a, _c) = p.consume_verb_with_metadata();
23841 preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23842 name: red_verb,
23843 args: p.ctx.terms.alloc_slice([Term::Variable(placeholder)]),
23844 world: None,
23845 }));
23846 loop {
23849 let prep = if p.check_preposition() {
23850 match p.advance().kind {
23851 TokenType::Preposition(s) => s,
23852 _ => break,
23853 }
23854 } else if p.check(&TokenType::To)
23855 && matches!(
23856 p.tokens.get(p.current + 1).map(|t| &t.kind),
23857 Some(TokenType::Article(_))
23858 | Some(TokenType::Noun(_))
23859 | Some(TokenType::ProperName(_))
23860 )
23861 {
23862 p.advance(); p.interner.intern("To")
23864 } else {
23865 break;
23866 };
23867 if p.check_number() {
23868 let m = p.parse_measure_phrase()?;
23869 preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23870 name: prep,
23871 args: p.ctx.terms.alloc_slice([Term::Variable(placeholder), *m]),
23872 world: None,
23873 }));
23874 } else if p.check_article() || p.check_content_word() {
23875 let obj = p.parse_noun_phrase(true)?;
23876 preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23877 name: prep,
23878 args: p.ctx.terms.alloc_slice([
23879 Term::Variable(placeholder),
23880 Term::Constant(obj.noun),
23881 ]),
23882 world: None,
23883 }));
23884 } else {
23885 break;
23886 }
23887 }
23888 let matrix_finite_verb = matches!(
23899 p.peek().kind,
23900 TokenType::Verb { aspect, .. } if !matches!(aspect, Aspect::Progressive)
23901 );
23902 if !(p.check(&TokenType::Is)
23903 || p.check(&TokenType::Are)
23904 || p.check(&TokenType::Was)
23905 || p.check(&TokenType::Were)
23906 || p.check(&TokenType::Does)
23907 || p.check(&TokenType::Do)
23908 || p.check(&TokenType::Had)
23909 || matrix_finite_verb)
23910 {
23911 return Err(ParseError {
23912 kind: ParseErrorKind::ExpectedCopula,
23913 span: p.current_span(),
23914 });
23915 }
23916 Ok(preds)
23917 }) {
23918 let mut pps: Vec<&'a LogicExpr<'a>> = subject.pps.to_vec();
23919 pps.extend(restrs);
23920 subject = crate::ast::NounPhrase {
23921 pps: self.ctx.pps.alloc_slice(pps),
23922 ..subject
23923 };
23924 }
23925 }
23926
23927 if (self.check(&TokenType::Are) || self.check(&TokenType::Is))
23931 && subject.definiteness.is_none()
23932 && subject.possessor.is_none()
23933 {
23934 let noun_str = self.interner.resolve(subject.noun);
23935 let lower = noun_str.to_lowercase();
23936 let is_known_plural = Self::is_plural_noun(noun_str)
23937 && (crate::lexicon::is_common_noun(&lower)
23938 || matches!(
23939 crate::lexicon::analyze_word(&lower),
23940 Some(crate::lexicon::WordAnalysis::Noun(_))
23941 ));
23942 let nominal_or_adj_follows = matches!(
23943 self.tokens.get(self.current + 1).map(|t| &t.kind),
23944 Some(TokenType::Noun(_))
23945 | Some(TokenType::Adjective(_))
23946 | Some(TokenType::Article(_))
23947 );
23948 if is_known_plural && nominal_or_adj_follows {
23949 self.advance(); let negated = self.check(&TokenType::Not);
23951 if negated {
23952 self.advance();
23953 }
23954 if self.check_article() {
23955 self.advance();
23956 }
23957 let var_name = self.next_var_name();
23958 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23959 name: subject.noun,
23960 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23961 world: None,
23962 });
23963 let pred_name = match self.peek().kind {
23964 TokenType::Noun(s) | TokenType::Adjective(s) => {
23965 self.advance();
23966 s
23967 }
23968 _ => self.consume_content_word()?,
23969 };
23970 let mut nucleus: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
23971 name: pred_name,
23972 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23973 world: None,
23974 });
23975 if negated {
23976 nucleus = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
23977 op: TokenType::Not,
23978 operand: nucleus,
23979 });
23980 }
23981 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23982 left: type_pred,
23983 op: TokenType::Implies,
23984 right: nucleus,
23985 });
23986 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23987 kind: QuantifierKind::Generic,
23988 variable: var_name,
23989 body,
23990 island_id: self.current_island,
23991 }));
23992 }
23993 }
23994
23995 if let Some((rc_var, rc_pred)) = relative_clause {
24001 if self.check(&TokenType::Is)
24002 || self.check(&TokenType::Are)
24003 || self.check(&TokenType::Was)
24004 || self.check(&TokenType::Were)
24005 {
24006 let restr =
24007 self.substitute_variable_with_constant(rc_pred, rc_var, subject.noun)?;
24008 self.pending_subject_restriction = Some((subject.noun, restr));
24009 }
24010 }
24011
24012 if self.check(&TokenType::Is)
24013 || self.check(&TokenType::Are)
24014 || self.check(&TokenType::Was)
24015 || self.check(&TokenType::Were)
24016 {
24017 let copula_time = if self.check(&TokenType::Was) || self.check(&TokenType::Were) {
24018 Time::Past
24019 } else {
24020 Time::Present
24021 };
24022 self.advance();
24023
24024 let is_negated = self.check(&TokenType::Not);
24026 if is_negated {
24027 self.advance(); }
24029
24030 let mut copula_temporal = None;
24032 if !is_negated {
24033 if self.check(&TokenType::Never) {
24034 self.advance();
24035 copula_temporal = Some(CopulaTemporal::Never);
24036 } else if let TokenType::Adverb(sym)
24037 | TokenType::ScopalAdverb(sym)
24038 | TokenType::TemporalAdverb(sym) = &self.peek().kind
24039 {
24040 let resolved = self.interner.resolve(*sym).to_string();
24041 if resolved == "Always" || resolved == "always" {
24042 self.advance();
24043 copula_temporal = Some(CopulaTemporal::Always);
24044 } else if resolved == "Eventually" || resolved == "eventually" {
24045 self.advance();
24046 copula_temporal = Some(CopulaTemporal::Eventually);
24047 }
24048 }
24049 }
24050
24051 if matches!(self.peek().kind, TokenType::Cardinal(1)) {
24055 let of_follows = matches!(
24056 self.tokens.get(self.current + 1).map(|t| &t.kind),
24057 Some(TokenType::Preposition(p)) if self.interner.resolve(*p).eq_ignore_ascii_case("of")
24058 );
24059 let article_then_noun = matches!(
24060 self.tokens.get(self.current + 2).map(|t| &t.kind),
24061 Some(TokenType::Article(_))
24062 ) && matches!(
24063 self.tokens.get(self.current + 3).map(|t| &t.kind),
24064 Some(TokenType::Noun(_))
24065 );
24066 if of_follows && article_then_noun {
24067 self.advance(); self.advance(); self.advance(); let noun_sym = match self.advance().kind {
24071 TokenType::Noun(s) => s,
24072 _ => unreachable!("guarded by article_then_noun"),
24073 };
24074 let noun_str = self.interner.resolve(noun_sym).to_string();
24075 let singular = Self::singularize_noun(&noun_str);
24076 let name_sym = self.interner.intern(&singular);
24077 let mut result: &'a LogicExpr<'a> =
24078 self.ctx.exprs.alloc(LogicExpr::Predicate {
24079 name: name_sym,
24080 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24081 world: None,
24082 });
24083 if is_negated {
24084 result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24085 op: TokenType::Not,
24086 operand: result,
24087 });
24088 }
24089 return self.wrap_with_definiteness_full(&subject, result);
24090 }
24091 }
24092
24093 if matches!(
24100 self.tokens.get(self.current + 1).map(|t| &t.kind),
24101 Some(TokenType::Comparative(_))
24102 ) && lexicon::is_degree_adverb(
24103 &self.interner.resolve(self.peek().lexeme).to_lowercase(),
24104 ) {
24105 self.advance(); }
24107
24108 if self.check_number() {
24111 let measure = self.parse_measure_phrase()?;
24112
24113 if self.check_comparative() {
24115 return self.parse_comparative(&subject, copula_time, Some(measure));
24116 }
24117
24118 if self.check_content_word() {
24120 let adj = self.consume_content_word()?;
24121 let result = self.ctx.exprs.alloc(LogicExpr::Predicate {
24122 name: adj,
24123 args: self
24124 .ctx
24125 .terms
24126 .alloc_slice([Term::Constant(subject.noun), *measure]),
24127 world: None,
24128 });
24129 return self.wrap_with_definiteness_full(&subject, result);
24130 }
24131
24132 if self.check(&TokenType::Period) || self.is_at_end() {
24135 if self.mode == ParserMode::Imperative {
24137 let variable = self.interner.resolve(subject.noun).to_string();
24138 let value = if let Term::Value { kind, .. } = measure {
24139 format!("{:?}", kind)
24140 } else {
24141 "value".to_string()
24142 };
24143 return Err(ParseError {
24144 kind: ParseErrorKind::IsValueEquality { variable, value },
24145 span: self.current_span(),
24146 });
24147 }
24148 let result = self.ctx.exprs.alloc(LogicExpr::Identity {
24149 left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24150 right: measure,
24151 });
24152 return self.wrap_with_definiteness_full(&subject, result);
24153 }
24154 }
24155
24156 if self
24158 .interner
24159 .resolve(self.peek().lexeme)
24160 .eq_ignore_ascii_case("as")
24161 {
24162 return self.parse_equative(&subject);
24163 }
24164
24165 if self.check_comparative() {
24167 return self.parse_comparative(&subject, copula_time, None);
24168 }
24169
24170 if self.check(&TokenType::Period) || self.is_at_end() {
24172 let var = self.next_var_name();
24173 let body = self.ctx.exprs.alloc(LogicExpr::Identity {
24174 left: self.ctx.terms.alloc(Term::Variable(var)),
24175 right: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24176 });
24177 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
24178 kind: QuantifierKind::Existential,
24179 variable: var,
24180 body,
24181 island_id: self.current_island,
24182 }));
24183 }
24184
24185 if self.check(&TokenType::Article(Definiteness::Definite)) {
24187 let saved_pos = self.backtrack_point();
24188 self.advance();
24189 if self.check_superlative() {
24190 return self.parse_superlative(&subject);
24191 }
24192 self.backtrack_to(&saved_pos);
24193 }
24194
24195 if self.check_article() {
24197 let saved_ctx = self.nominal_np_context;
24201 self.nominal_np_context = true;
24202 let predicate_np_result = self.parse_noun_phrase(true);
24203 self.nominal_np_context = saved_ctx;
24204 let predicate_np = predicate_np_result?;
24205 let predicate_noun = predicate_np.noun;
24206
24207 if self.check(&TokenType::Who) || self.check(&TokenType::That) {
24218 let rel_cp = self.checkpoint();
24219 if let Ok(first) = self.parse_list_member_np(&predicate_np, false) {
24220 if self.check(&TokenType::Comma) {
24221 return self.finish_enumerated_identity(&subject, is_negated, first);
24222 }
24223 }
24224 self.restore(rel_cp);
24225 }
24226 if self.check(&TokenType::Comma) {
24227 let first = self.parse_list_member_np(&predicate_np, false)?;
24228 return self.finish_enumerated_identity(&subject, is_negated, first);
24229 }
24230
24231 if self.event_reading_mode {
24234 let noun_str = self.interner.resolve(predicate_noun);
24235 if let Some(base_verb) = lexicon::lookup_agentive_noun(noun_str) {
24236 let event_adj = predicate_np.adjectives.iter().find(|adj| {
24238 lexicon::is_event_modifier_adjective(self.interner.resolve(**adj))
24239 });
24240
24241 if let Some(&adj_sym) = event_adj {
24242 let verb_sym = self.interner.intern(base_verb);
24244 let event_var = self.get_event_var();
24245
24246 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24247 name: verb_sym,
24248 args: self.ctx.terms.alloc_slice([Term::Variable(event_var)]),
24249 world: None,
24250 });
24251
24252 let agent_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24253 name: self.interner.intern("Agent"),
24254 args: self.ctx.terms.alloc_slice([
24255 Term::Variable(event_var),
24256 Term::Constant(subject.noun),
24257 ]),
24258 world: None,
24259 });
24260
24261 let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24262 name: adj_sym,
24263 args: self.ctx.terms.alloc_slice([Term::Variable(event_var)]),
24264 world: None,
24265 });
24266
24267 let verb_agent = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24269 left: verb_pred,
24270 op: TokenType::And,
24271 right: agent_pred,
24272 });
24273
24274 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24276 left: verb_agent,
24277 op: TokenType::And,
24278 right: adj_pred,
24279 });
24280
24281 let event_reading = self.ctx.exprs.alloc(LogicExpr::Quantifier {
24283 kind: QuantifierKind::Existential,
24284 variable: event_var,
24285 body,
24286 island_id: self.current_island,
24287 });
24288
24289 return self.wrap_with_definiteness_full(&subject, event_reading);
24290 }
24291 }
24292 }
24293
24294 let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
24295 let predicate_sort = lexicon::lookup_sort(self.interner.resolve(predicate_noun));
24296
24297 if let (Some(s_sort), Some(p_sort)) = (subject_sort, predicate_sort) {
24298 if !s_sort.is_compatible_with(p_sort) && !p_sort.is_compatible_with(s_sort) {
24299 let metaphor: &'a LogicExpr<'a> =
24300 self.ctx.exprs.alloc(LogicExpr::Metaphor {
24301 tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24302 vehicle: self.ctx.terms.alloc(Term::Constant(predicate_noun)),
24303 });
24304 let metaphor = if is_negated {
24305 &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24306 op: TokenType::Not,
24307 operand: metaphor,
24308 })
24309 } else {
24310 metaphor
24311 };
24312 return self.wrap_with_definiteness_full(&subject, metaphor);
24313 }
24314 }
24315
24316 let mut predicates: Vec<&'a LogicExpr<'a>> = Vec::new();
24319
24320 for &adj_sym in predicate_np.adjectives {
24322 let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24323 name: adj_sym,
24324 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24325 world: None,
24326 });
24327 predicates.push(adj_pred);
24328 }
24329
24330 let noun_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24332 name: predicate_noun,
24333 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24334 world: None,
24335 });
24336 predicates.push(noun_pred);
24337
24338 if let Some(possessor) = predicate_np.possessor {
24345 let poss_pred =
24346 self.possessor_predication(possessor, Term::Constant(subject.noun));
24347 predicates.push(poss_pred);
24348 }
24349
24350 for pp in predicate_np.pps {
24358 let pp_sub = self.substitute_pp_self_term(pp, Term::Constant(subject.noun));
24359 predicates.push(pp_sub);
24360 }
24361
24362 let mut result = if predicates.len() == 1 {
24364 predicates[0]
24365 } else {
24366 let mut combined = predicates[0];
24367 for pred in &predicates[1..] {
24368 combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24369 left: combined,
24370 op: TokenType::And,
24371 right: *pred,
24372 });
24373 }
24374 combined
24375 };
24376
24377 let mut result = result;
24384 if let Some(rc) = self.try_attach_relative(Term::Constant(subject.noun))? {
24385 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24386 left: result,
24387 op: TokenType::And,
24388 right: rc,
24389 });
24390 }
24391
24392 loop {
24398 if !self.check(&TokenType::And) {
24399 break;
24400 }
24401 let save = self.backtrack_point();
24402 self.advance(); let next_is_boundary = matches!(
24408 self.tokens.get(self.current + 1).map(|t| &t.kind),
24409 Some(TokenType::Period)
24410 | Some(TokenType::Comma)
24411 | Some(TokenType::And)
24412 | Some(TokenType::EOF)
24413 | Some(TokenType::Exclamation)
24414 | None
24415 );
24416 let conjunct: Option<&'a LogicExpr<'a>> = match self.peek().kind {
24417 TokenType::Adjective(a) | TokenType::NonIntersectiveAdjective(a) => {
24418 self.advance();
24419 Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
24420 name: a,
24421 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24422 world: None,
24423 }))
24424 }
24425 TokenType::Noun(a) if next_is_boundary => {
24426 self.advance();
24427 Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
24428 name: a,
24429 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24430 world: None,
24431 }))
24432 }
24433 _ if self.check_article() => match self.parse_noun_phrase(true) {
24434 Ok(np) => {
24435 Some(self.nominal_predication(Term::Constant(subject.noun), &np))
24436 }
24437 Err(_) => None,
24438 },
24439 _ => None,
24440 };
24441 match conjunct {
24442 Some(c) => {
24443 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24444 left: result,
24445 op: TokenType::And,
24446 right: c,
24447 });
24448 }
24449 None => {
24450 self.backtrack_to(&save);
24451 break;
24452 }
24453 }
24454 }
24455
24456 let result = if is_negated {
24458 &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24459 op: TokenType::Not,
24460 operand: result,
24461 })
24462 } else {
24463 result
24464 };
24465
24466 return self.wrap_with_definiteness_full(&subject, result);
24472 }
24473
24474 let prefer_adjective = if let TokenType::Ambiguous {
24477 primary,
24478 alternatives,
24479 } = &self.peek().kind
24480 {
24481 let is_simple_verb = if let TokenType::Verb { aspect, .. } = **primary {
24482 aspect == Aspect::Simple
24483 } else {
24484 false
24485 };
24486 let has_adj_alt = alternatives
24487 .iter()
24488 .any(|t| matches!(t, TokenType::Adjective(_)));
24489 is_simple_verb && has_adj_alt
24490 } else {
24491 false
24492 };
24493
24494 if !prefer_adjective && self.check_verb() {
24495 let (verb, _verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
24496
24497 if verb_class.is_stative() && verb_aspect == Aspect::Progressive {
24499 return Err(ParseError {
24500 kind: ParseErrorKind::StativeProgressiveConflict,
24501 span: self.current_span(),
24502 });
24503 }
24504
24505 let mut goal_args: Vec<Term<'a>> = Vec::new();
24508 while self.check_to_preposition() {
24509 self.advance(); let goal = self.parse_noun_phrase(true)?;
24511 goal_args.push(self.noun_phrase_to_term(&goal));
24512 }
24513
24514 if self.check_by_preposition() {
24516 self.advance(); let agent = self.parse_noun_phrase(true)?;
24518
24519 let (agent_term, agent_restr) = self.possessor_entity(&agent);
24524 let mut args = vec![agent_term, self.noun_phrase_to_term(&subject)];
24525 args.extend(goal_args);
24526
24527 let predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24528 name: verb,
24529 args: self.ctx.terms.alloc_slice(args),
24530 world: None,
24531 });
24532 let predicate = self.wrap_in_possessor_entity(agent_restr, predicate);
24533
24534 let with_time = if copula_time == Time::Past {
24535 self.ctx.exprs.alloc(LogicExpr::Temporal {
24536 operator: TemporalOperator::Past,
24537 body: predicate,
24538 })
24539 } else {
24540 predicate
24541 };
24542
24543 return self.wrap_with_definiteness_full(&subject, with_time);
24544 }
24545
24546 let subject_is_common_noun = {
24557 let n = self.interner.resolve(subject.noun);
24558 lexicon::lookup_noun_db(n).is_some()
24559 || (n.ends_with('s')
24560 && lexicon::lookup_noun_db(&n[..n.len() - 1]).is_some())
24561 };
24562 if copula_time == Time::Past
24563 && verb_aspect == Aspect::Simple
24564 && subject.definiteness != Some(Definiteness::Definite)
24565 && subject_is_common_noun
24566 {
24567 let var_name = self.next_var_name();
24569 let predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24570 name: verb,
24571 args: self
24572 .ctx
24573 .terms
24574 .alloc_slice([Term::Variable(var_name), Term::Constant(subject.noun)]),
24575 world: None,
24576 });
24577
24578 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24579 name: subject.noun,
24580 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
24581 world: None,
24582 });
24583
24584 let temporal = self.ctx.exprs.alloc(LogicExpr::Temporal {
24585 operator: TemporalOperator::Past,
24586 body: predicate,
24587 });
24588
24589 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24590 left: type_pred,
24591 op: TokenType::And,
24592 right: temporal,
24593 });
24594
24595 let result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
24596 kind: QuantifierKind::Existential,
24597 variable: var_name,
24598 body,
24599 island_id: self.current_island,
24600 });
24601
24602 if is_negated {
24604 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24605 op: TokenType::Not,
24606 operand: result,
24607 }));
24608 }
24609 return Ok(result);
24610 }
24611
24612 let verb_str = self.interner.resolve(verb).to_lowercase();
24615 let subject_term = if lexicon::is_intensional_predicate(&verb_str) {
24616 Term::Intension(subject.noun)
24617 } else {
24618 Term::Constant(subject.noun)
24619 };
24620
24621 let mut predicate: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
24622 name: verb,
24623 args: self.ctx.terms.alloc_slice([subject_term]),
24624 world: None,
24625 });
24626
24627 if verb_aspect == Aspect::Progressive
24633 && !self.check_preposition()
24634 && (self.check_article() || self.check_number() || self.check_content_word())
24635 {
24636 let obj_term = if self.check_number() {
24637 *self.parse_measure_phrase()?
24638 } else {
24639 let obj_np = self.parse_noun_phrase(false)?;
24640 self.noun_phrase_to_term(&obj_np)
24641 };
24642 predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24643 name: verb,
24644 args: self.ctx.terms.alloc_slice([subject_term, obj_term]),
24645 world: None,
24646 });
24647 }
24648
24649 while self.check_preposition() {
24654 let prep = match self.advance().kind {
24655 TokenType::Preposition(s) => s,
24656 _ => break,
24657 };
24658 let pp = if self.check_number() {
24659 let m = self.parse_measure_phrase()?;
24660 self.ctx.exprs.alloc(LogicExpr::Predicate {
24661 name: prep,
24662 args: self.ctx.terms.alloc_slice([subject_term, *m]),
24663 world: None,
24664 })
24665 } else if self.check_content_word()
24666 || matches!(self.peek().kind, TokenType::Article(_))
24667 {
24668 let obj = self.parse_noun_phrase(true)?;
24669 self.ctx.exprs.alloc(LogicExpr::Predicate {
24670 name: prep,
24671 args: self
24672 .ctx
24673 .terms
24674 .alloc_slice([subject_term, Term::Constant(obj.noun)]),
24675 world: None,
24676 })
24677 } else {
24678 break;
24679 };
24680 predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24681 left: predicate,
24682 op: TokenType::And,
24683 right: pp,
24684 });
24685 }
24686
24687 if let Some(constraint) = self.parse_temporal_offset_constraint(subject_term)? {
24692 predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24693 left: predicate,
24694 op: TokenType::And,
24695 right: constraint,
24696 });
24697 }
24698 else if let Some(constraint) =
24702 self.parse_bare_temporal_constraint(subject_term)?
24703 {
24704 predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24705 left: predicate,
24706 op: TokenType::And,
24707 right: constraint,
24708 });
24709 }
24710
24711 let adverbs = self.collect_adverbs();
24714 let predicate = if adverbs.is_empty() {
24715 predicate
24716 } else {
24717 self.ctx.exprs.alloc(LogicExpr::Event {
24718 predicate,
24719 adverbs: self.ctx.syms.alloc_slice(adverbs),
24720 })
24721 };
24722
24723 let with_aspect = if verb_aspect == Aspect::Progressive {
24724 let operator = if verb_class == VerbClass::Semelfactive {
24726 AspectOperator::Iterative
24727 } else {
24728 AspectOperator::Progressive
24729 };
24730 self.ctx.exprs.alloc(LogicExpr::Aspectual {
24731 operator,
24732 body: predicate,
24733 })
24734 } else {
24735 predicate
24736 };
24737
24738 let with_time = if copula_time == Time::Past {
24739 self.ctx.exprs.alloc(LogicExpr::Temporal {
24740 operator: TemporalOperator::Past,
24741 body: with_aspect,
24742 })
24743 } else {
24744 with_aspect
24745 };
24746
24747 let final_expr = if is_negated {
24748 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24749 op: TokenType::Not,
24750 operand: with_time,
24751 })
24752 } else {
24753 with_time
24754 };
24755
24756 if subject.definiteness == Some(Definiteness::Definite)
24763 && subject.adjectives.is_empty()
24764 && subject.pps.is_empty()
24765 && subject.possessor.is_none()
24766 {
24767 return Ok(final_expr);
24768 }
24769
24770 return self.wrap_with_definiteness_full(&subject, final_expr);
24771 }
24772
24773 if let Some((var_name, rel_clause)) = relative_clause {
24778 let var_term = Term::Variable(var_name);
24779
24780 let main_pred: &'a LogicExpr<'a> = if self.check(&TokenType::Either) {
24781 self.advance();
24782 let np1 = self.parse_noun_phrase(true)?;
24783 let p1 = self.nominal_predication_with_pps(var_term, &np1);
24784 let p1 = self.conjoin_trailing_relative(p1, var_term)?;
24785 if self.check(&TokenType::Or) {
24786 self.advance();
24787 let np2 = self.parse_noun_phrase(true)?;
24788 let p2 = self.nominal_predication_with_pps(var_term, &np2);
24789 let p2 = self.conjoin_trailing_relative(p2, var_term)?;
24790 self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24791 left: p1,
24792 op: TokenType::Or,
24793 right: p2,
24794 })
24795 } else {
24796 p1
24797 }
24798 } else if matches!(self.peek().kind, TokenType::ProperName(_))
24799 && matches!(
24800 self.tokens.get(self.current + 1).map(|t| &t.kind),
24801 Some(TokenType::Possessive)
24802 )
24803 {
24804 let np = self.parse_noun_phrase(true)?;
24806 self.nominal_predication_with_pps(var_term, &np)
24807 } else if let TokenType::ProperName(pname) = self.peek().kind {
24808 self.advance();
24809 self.ctx.exprs.alloc(LogicExpr::Identity {
24810 left: self.ctx.terms.alloc(var_term),
24811 right: self.ctx.terms.alloc(Term::Constant(pname)),
24812 })
24813 } else if self.check_article() {
24814 let np = self.parse_noun_phrase(true)?;
24815 let pred = self.nominal_predication_with_pps(var_term, &np);
24816 self.conjoin_trailing_relative(pred, var_term)?
24817 } else if self.check_preposition() && !self.check_by_preposition() {
24818 let prep_sym = match self.advance().kind {
24822 TokenType::Preposition(s) => s,
24823 _ => unreachable!("guarded by check_preposition()"),
24824 };
24825 if self.check_number() {
24826 let measure = self.parse_measure_phrase()?;
24827 self.ctx.exprs.alloc(LogicExpr::Predicate {
24828 name: prep_sym,
24829 args: self.ctx.terms.alloc_slice([var_term, *measure]),
24830 world: None,
24831 })
24832 } else {
24833 let obj = self.parse_noun_phrase(true)?;
24834 self.ctx.exprs.alloc(LogicExpr::Predicate {
24835 name: prep_sym,
24836 args: self
24837 .ctx
24838 .terms
24839 .alloc_slice([var_term, Term::Constant(obj.noun)]),
24840 world: None,
24841 })
24842 }
24843 } else {
24844 let pred_word = self.consume_content_word()?;
24845 self.ctx.exprs.alloc(LogicExpr::Predicate {
24846 name: pred_word,
24847 args: self.ctx.terms.alloc_slice([var_term]),
24848 world: None,
24849 })
24850 };
24851 let main_pred = if is_negated {
24852 &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24853 op: TokenType::Not,
24854 operand: main_pred,
24855 })
24856 } else {
24857 main_pred
24858 };
24859
24860 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24861 name: subject.noun,
24862 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
24863 world: None,
24864 });
24865
24866 let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24867 left: type_pred,
24868 op: TokenType::And,
24869 right: rel_clause,
24870 });
24871
24872 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24873 left: inner,
24874 op: TokenType::And,
24875 right: main_pred,
24876 });
24877
24878 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
24879 kind: QuantifierKind::Existential,
24880 variable: var_name,
24881 body,
24882 island_id: self.current_island,
24883 }));
24884 }
24885
24886 if let TokenType::ProperName(predicate_name) = self.peek().kind {
24894 let next_is_possessive = matches!(
24895 self.tokens.get(self.current + 1).map(|t| &t.kind),
24896 Some(TokenType::Possessive)
24897 );
24898 let elided = matches!(
24899 self.tokens.get(self.current + 2).map(|t| &t.kind),
24900 Some(TokenType::Period)
24901 | Some(TokenType::EOF)
24902 | Some(TokenType::Comma)
24903 | Some(TokenType::And)
24904 | Some(TokenType::Or)
24905 | None
24906 );
24907 if next_is_possessive && elided {
24908 self.advance(); self.advance(); let poss = self.ctx.exprs.alloc(LogicExpr::Predicate {
24911 name: self.interner.intern("Possesses"),
24912 args: self.ctx.terms.alloc_slice([
24913 Term::Constant(predicate_name),
24914 Term::Constant(subject.noun),
24915 ]),
24916 world: None,
24917 });
24918 let result = if is_negated {
24919 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24920 op: TokenType::Not,
24921 operand: poss,
24922 })
24923 } else {
24924 poss
24925 };
24926 return self.wrap_with_definiteness_full(&subject, result);
24927 }
24928 }
24929
24930 if matches!(self.peek().kind, TokenType::ProperName(_))
24934 && matches!(
24935 self.tokens.get(self.current + 1).map(|t| &t.kind),
24936 Some(TokenType::Possessive)
24937 )
24938 {
24939 let np = self.parse_noun_phrase(true)?;
24940 let pred = self.nominal_predication_with_pps(Term::Constant(subject.noun), &np);
24941 let result = if is_negated {
24942 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24943 op: TokenType::Not,
24944 operand: pred,
24945 })
24946 } else {
24947 pred
24948 };
24949 return self.wrap_with_definiteness_full(&subject, result);
24950 }
24951
24952 if let TokenType::ProperName(predicate_name) = self.peek().kind {
24955 self.advance(); let predicate_name = self.absorb_multiword_proper_name(predicate_name);
24960
24961 if self.check(&TokenType::Comma) {
24968 let mut members: Vec<Term<'a>> = vec![Term::Constant(predicate_name)];
24969 let mut member_rels: Vec<&'a LogicExpr<'a>> = Vec::new();
24970 while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
24971 while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
24972 self.advance();
24973 }
24974 if let TokenType::ProperName(pn) = self.peek().kind {
24975 self.advance();
24976 let pn = self.absorb_multiword_proper_name(pn);
24977 members.push(Term::Constant(pn));
24978 } else if self.check_article() || self.check_content_word() {
24979 let item = self.parse_noun_phrase(true)?;
24980 members.push(Term::Constant(item.noun));
24981 if let Some(rc) = self.try_attach_relative(Term::Constant(item.noun))? {
24982 member_rels.push(rc);
24983 }
24984 } else {
24985 break;
24986 }
24987 }
24988 let group = Term::Group(self.ctx.terms.alloc_slice(members));
24989 let identity: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Identity {
24990 left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24991 right: self.ctx.terms.alloc(group),
24992 });
24993 let mut result = if is_negated {
24994 &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24995 op: TokenType::Not,
24996 operand: identity,
24997 })
24998 } else {
24999 identity
25000 };
25001 for rc in member_rels {
25002 result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25003 left: result,
25004 op: TokenType::And,
25005 right: rc,
25006 });
25007 }
25008 return self.wrap_with_definiteness_full(&subject, result);
25009 }
25010
25011 let identity = self.ctx.exprs.alloc(LogicExpr::Identity {
25012 left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25013 right: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25014 });
25015 let result = if is_negated {
25016 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25017 op: TokenType::Not,
25018 operand: identity,
25019 })
25020 } else {
25021 identity
25022 };
25023 return self.wrap_with_definiteness_full(&subject, result);
25024 }
25025
25026 if self.check(&TokenType::Either) {
25029 self.advance(); let parse_disjunct = |p: &mut Self| -> ParseResult<&'a LogicExpr<'a>> {
25034 let saved_ctx = p.nominal_np_context;
25037 p.nominal_np_context = true;
25038 let np_result = p.parse_noun_phrase(true);
25039 p.nominal_np_context = saved_ctx;
25040 let np = np_result?;
25041 let mut pred: &'a LogicExpr<'a> =
25047 p.nominal_predication_with_pps(Term::Constant(subject.noun), &np);
25048 if let Some(rel) = p.try_attach_relative(Term::Constant(subject.noun))? {
25049 pred = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
25050 left: pred,
25051 op: TokenType::And,
25052 right: rel,
25053 });
25054 }
25055 if let Some(rr) =
25059 p.try_consume_reduced_relative(Term::Constant(subject.noun))?
25060 {
25061 pred = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
25062 left: pred,
25063 op: TokenType::And,
25064 right: rr,
25065 });
25066 }
25067 Ok(pred)
25068 };
25069 let pred1 = parse_disjunct(self)?;
25070 if self.check(&TokenType::Or) {
25071 self.advance(); let pred2 = parse_disjunct(self)?;
25073 let disj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25074 left: pred1,
25075 op: TokenType::Or,
25076 right: pred2,
25077 });
25078 let with_time = if copula_time == Time::Past {
25079 self.ctx.exprs.alloc(LogicExpr::Temporal {
25080 operator: TemporalOperator::Past,
25081 body: disj,
25082 })
25083 } else {
25084 disj
25085 };
25086 let result = if is_negated {
25087 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25088 op: TokenType::Not,
25089 operand: with_time,
25090 })
25091 } else {
25092 with_time
25093 };
25094 return self.wrap_with_definiteness_full(&subject, result);
25097 }
25098 }
25099
25100 if self.check_preposition() && !self.check_by_preposition() {
25103 let prep_token = self.advance().clone();
25104 let prep_sym = match prep_token.kind {
25105 TokenType::Preposition(s) => s,
25106 _ => unreachable!("guarded by check_preposition()"),
25107 };
25108 let base = if self.check_number() {
25112 let m = self.parse_measure_phrase()?;
25113 self.ctx.exprs.alloc(LogicExpr::Predicate {
25114 name: prep_sym,
25115 args: self
25116 .ctx
25117 .terms
25118 .alloc_slice([Term::Constant(subject.noun), *m]),
25119 world: None,
25120 })
25121 } else {
25122 let saved_ctx = self.nominal_np_context;
25126 self.nominal_np_context = true;
25127 let pp_obj_result = self.parse_noun_phrase(true);
25128 self.nominal_np_context = saved_ctx;
25129 let pp_obj = pp_obj_result?;
25130 let pred: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25131 name: prep_sym,
25132 args: self.ctx.terms.alloc_slice([
25133 Term::Constant(subject.noun),
25134 Term::Constant(pp_obj.noun),
25135 ]),
25136 world: None,
25137 });
25138 self.attach_pp_object_modifiers(pred, &pp_obj)
25142 };
25143 let mut base: &'a LogicExpr<'a> = base;
25148 while self.check(&TokenType::Or) {
25149 let cp = self.checkpoint();
25150 self.advance(); let disj_prep = if self.check_preposition() && !self.check_by_preposition() {
25152 match self.advance().kind {
25153 TokenType::Preposition(s) => s,
25154 _ => prep_sym,
25155 }
25156 } else {
25157 prep_sym
25158 };
25159 if !(self.check_content_word()
25160 || self.check_number()
25161 || matches!(self.peek().kind, TokenType::Article(_)))
25162 {
25163 self.restore(cp);
25164 break;
25165 }
25166 let saved_ctx = self.nominal_np_context;
25167 self.nominal_np_context = true;
25168 let disj_obj_res = self.parse_noun_phrase(true);
25169 self.nominal_np_context = saved_ctx;
25170 let disj_obj = disj_obj_res?;
25171 let disj_pred: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25172 name: disj_prep,
25173 args: self.ctx.terms.alloc_slice([
25174 Term::Constant(subject.noun),
25175 Term::Constant(disj_obj.noun),
25176 ]),
25177 world: None,
25178 });
25179 let disj_pred = self.attach_pp_object_modifiers(disj_pred, &disj_obj);
25180 base = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25181 left: base,
25182 op: TokenType::Or,
25183 right: disj_pred,
25184 });
25185 }
25186 let with_time = if copula_time == Time::Past {
25187 self.ctx.exprs.alloc(LogicExpr::Temporal {
25188 operator: TemporalOperator::Past,
25189 body: base,
25190 })
25191 } else {
25192 base
25193 };
25194 let result = if is_negated {
25195 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25196 op: TokenType::Not,
25197 operand: with_time,
25198 })
25199 } else {
25200 with_time
25201 };
25202 return self.wrap_with_definiteness_full(&subject, result);
25205 }
25206
25207 if let Some(base) =
25211 self.copula_temporal_adverb_complement(Term::Constant(subject.noun))?
25212 {
25213 let with_time = if copula_time == Time::Past {
25214 self.ctx.exprs.alloc(LogicExpr::Temporal {
25215 operator: TemporalOperator::Past,
25216 body: base,
25217 })
25218 } else {
25219 base
25220 };
25221 let result = if is_negated {
25222 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25223 op: TokenType::Not,
25224 operand: with_time,
25225 })
25226 } else {
25227 with_time
25228 };
25229 return self.wrap_with_definiteness_full(&subject, result);
25230 }
25231
25232 let predicate_name = self.consume_content_word()?;
25235
25236 {
25242 let mut coord_adjs: Vec<Symbol> = vec![predicate_name];
25243 while self.check(&TokenType::And) {
25244 let cp = self.checkpoint();
25245 self.advance(); if let TokenType::Adjective(a) = self.peek().kind {
25247 if matches!(
25252 self.tokens.get(self.current + 1).map(|t| &t.kind),
25253 Some(TokenType::Is)
25254 | Some(TokenType::Are)
25255 | Some(TokenType::Was)
25256 | Some(TokenType::Were)
25257 | Some(TokenType::Verb { .. })
25258 ) {
25259 self.restore(cp);
25260 break;
25261 }
25262 self.advance();
25263 coord_adjs.push(a);
25264 } else {
25265 self.restore(cp); break;
25267 }
25268 }
25269 if coord_adjs.len() > 1 {
25270 let mut conj: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25271 name: coord_adjs[0],
25272 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25273 world: None,
25274 });
25275 for &a in &coord_adjs[1..] {
25276 let p = self.ctx.exprs.alloc(LogicExpr::Predicate {
25277 name: a,
25278 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25279 world: None,
25280 });
25281 conj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25282 left: conj,
25283 op: TokenType::And,
25284 right: p,
25285 });
25286 }
25287 let conj = if is_negated {
25288 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25289 op: TokenType::Not,
25290 operand: conj,
25291 })
25292 } else {
25293 conj
25294 };
25295 let conj = if copula_time == Time::Past {
25296 self.ctx.exprs.alloc(LogicExpr::Temporal {
25297 operator: TemporalOperator::Past,
25298 body: conj,
25299 })
25300 } else {
25301 conj
25302 };
25303 return self.wrap_with_definiteness_full(&subject, conj);
25304 }
25305 }
25306
25307 if self.check_number() {
25313 let measure = self.parse_measure_phrase()?;
25314 let pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25315 name: predicate_name,
25316 args: self
25317 .ctx
25318 .terms
25319 .alloc_slice([Term::Constant(subject.noun), *measure]),
25320 world: None,
25321 });
25322 let pred = if is_negated {
25323 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25324 op: TokenType::Not,
25325 operand: pred,
25326 })
25327 } else {
25328 pred
25329 };
25330 let pred = if copula_time == Time::Past {
25331 self.ctx.exprs.alloc(LogicExpr::Temporal {
25332 operator: TemporalOperator::Past,
25333 body: pred,
25334 })
25335 } else {
25336 pred
25337 };
25338 return self.wrap_with_definiteness_full(&subject, pred);
25339 }
25340
25341 let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
25343 let predicate_str = self.interner.resolve(predicate_name);
25344
25345 if let Some(s_sort) = subject_sort {
25347 if !crate::ontology::check_sort_compatibility(predicate_str, s_sort) {
25348 let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
25349 tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25350 vehicle: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25351 });
25352 return self.wrap_with_definiteness_full(&subject, metaphor);
25353 }
25354 }
25355
25356 let predicate_sort = lexicon::lookup_sort(predicate_str);
25358 if let (Some(s_sort), Some(p_sort)) = (subject_sort, predicate_sort) {
25359 if s_sort != p_sort
25360 && !s_sort.is_compatible_with(p_sort)
25361 && !p_sort.is_compatible_with(s_sort)
25362 {
25363 let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
25364 tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25365 vehicle: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25366 });
25367 return self.wrap_with_definiteness_full(&subject, metaphor);
25368 }
25369 }
25370
25371 let predicate = if self.pragmatic
25375 && crate::lexicon::is_gradable_adjective(
25376 &self.interner.resolve(predicate_name).to_lowercase(),
25377 ) {
25378 let d = self.next_var_name();
25379 let theta = self.interner.intern("θ_C");
25380 let adj_d = self.ctx.exprs.alloc(LogicExpr::Predicate {
25381 name: predicate_name,
25382 args: self
25383 .ctx
25384 .terms
25385 .alloc_slice([Term::Constant(subject.noun), Term::Variable(d)]),
25386 world: None,
25387 });
25388 let exceeds = self.ctx.exprs.alloc(LogicExpr::Predicate {
25389 name: self.interner.intern(">"),
25390 args: self
25391 .ctx
25392 .terms
25393 .alloc_slice([Term::Variable(d), Term::Constant(theta)]),
25394 world: None,
25395 });
25396 let mut body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25397 left: adj_d,
25398 op: TokenType::And,
25399 right: exceeds,
25400 });
25401 if crate::lexicon::is_vague_adjective(
25404 &self.interner.resolve(predicate_name).to_lowercase(),
25405 ) {
25406 let borderline = self.ctx.exprs.alloc(LogicExpr::Predicate {
25407 name: self.interner.intern("Borderline"),
25408 args: self.ctx.terms.alloc_slice([Term::Constant(theta)]),
25409 world: None,
25410 });
25411 body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25412 left: body,
25413 op: TokenType::And,
25414 right: borderline,
25415 });
25416 }
25417 self.ctx.exprs.alloc(LogicExpr::Quantifier {
25418 kind: QuantifierKind::Existential,
25419 variable: d,
25420 body,
25421 island_id: self.current_island,
25422 })
25423 } else {
25424 self.ctx.exprs.alloc(LogicExpr::Predicate {
25425 name: predicate_name,
25426 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25427 world: None,
25428 })
25429 };
25430
25431 let result = if is_negated {
25433 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25434 op: TokenType::Not,
25435 operand: predicate,
25436 })
25437 } else {
25438 predicate
25439 };
25440
25441 let result = match copula_temporal {
25443 Some(CopulaTemporal::Always) => self.ctx.exprs.alloc(LogicExpr::Temporal {
25444 operator: TemporalOperator::Always,
25445 body: result,
25446 }),
25447 Some(CopulaTemporal::Never) => {
25448 let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25449 op: TokenType::Not,
25450 operand: predicate,
25451 });
25452 self.ctx.exprs.alloc(LogicExpr::Temporal {
25453 operator: TemporalOperator::Always,
25454 body: negated,
25455 })
25456 }
25457 Some(CopulaTemporal::Eventually) => self.ctx.exprs.alloc(LogicExpr::Temporal {
25458 operator: TemporalOperator::Eventually,
25459 body: result,
25460 }),
25461 None => result,
25462 };
25463
25464 return self.wrap_with_definiteness_full(&subject, result);
25468 }
25469
25470 if self.check_auxiliary_as_main_verb() {
25476 let subject_term = self.noun_phrase_to_term(&subject);
25477 let result = self.parse_do_as_main_verb(subject_term)?;
25478 return self.wrap_with_definiteness_full(&subject, result);
25479 }
25480
25481 if self.check_auxiliary() && self.is_true_auxiliary_usage() {
25485 let aux_time = if let TokenType::Auxiliary(time) = self.advance().kind {
25486 time
25487 } else {
25488 Time::None
25489 };
25490 self.pending_time = Some(aux_time);
25491
25492 if self.match_token(&[TokenType::Not]) {
25494 self.negative_depth += 1;
25495
25496 if self.check(&TokenType::Ever) {
25498 self.advance();
25499 }
25500
25501 if self.check_presup_trigger()
25505 && !self.is_followed_by_np_object()
25506 && self.is_followed_by_gerund()
25507 {
25508 let presup_kind = self.consume_presup_trigger();
25509 self.negative_depth -= 1; return self.parse_presupposition(&subject, presup_kind, true);
25511 }
25512
25513 if let TokenType::Performative(_) = self.peek().kind {
25517 let lemma = self
25518 .interner
25519 .intern(&self.interner.resolve(self.peek().lexeme).to_lowercase());
25520 self.tokens[self.current].kind = TokenType::Verb {
25521 lemma,
25522 time: Time::None,
25523 aspect: Aspect::Simple,
25524 class: crate::lexicon::VerbClass::Activity,
25525 };
25526 }
25527 if self.check_verb() || self.check(&TokenType::Do) {
25529 let (verb, verb_time, verb_aspect, verb_class) = if self.check(&TokenType::Do) {
25530 self.advance(); (
25532 self.interner.intern("Do"),
25533 Time::None,
25534 Aspect::Simple,
25535 crate::lexicon::VerbClass::Activity,
25536 )
25537 } else {
25538 self.consume_verb_with_metadata()
25539 };
25540 let subject_term = self.noun_phrase_to_term(&subject);
25541
25542 if self.check_npi_object() {
25544 let npi_token = self.advance().kind.clone();
25545 let obj_var = self.next_var_name();
25546
25547 let restriction_name = match npi_token {
25548 TokenType::Anything => "Thing",
25549 TokenType::Anyone => "Person",
25550 _ => "Thing",
25551 };
25552
25553 let restriction_sym = self.interner.intern(restriction_name);
25554 let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25555 name: restriction_sym,
25556 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
25557 world: None,
25558 });
25559
25560 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25561 name: verb,
25562 args: self
25563 .ctx
25564 .terms
25565 .alloc_slice([subject_term.clone(), Term::Variable(obj_var)]),
25566 world: None,
25567 });
25568
25569 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25570 left: obj_restriction,
25571 op: TokenType::And,
25572 right: verb_pred,
25573 });
25574
25575 let quantified = self.ctx.exprs.alloc(LogicExpr::Quantifier {
25576 kind: QuantifierKind::Existential,
25577 variable: obj_var,
25578 body,
25579 island_id: self.current_island,
25580 });
25581
25582 let effective_time = self.pending_time.take().unwrap_or(Time::None);
25583 let with_time = match effective_time {
25584 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25585 operator: TemporalOperator::Past,
25586 body: quantified,
25587 }),
25588 Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25589 operator: TemporalOperator::Future,
25590 body: quantified,
25591 }),
25592 _ => quantified,
25593 };
25594
25595 self.negative_depth -= 1;
25596 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25597 op: TokenType::Not,
25598 operand: with_time,
25599 }));
25600 }
25601
25602 if self.check_quantifier() {
25604 let quantifier_token = self.advance().kind.clone();
25605 let object_np = self.parse_noun_phrase(false)?;
25606 let obj_var = self.next_var_name();
25607
25608 let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25609 name: object_np.noun,
25610 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
25611 world: None,
25612 });
25613
25614 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25615 name: verb,
25616 args: self
25617 .ctx
25618 .terms
25619 .alloc_slice([subject_term.clone(), Term::Variable(obj_var)]),
25620 world: None,
25621 });
25622
25623 let quantified =
25624 self.quantify_argument(&quantifier_token, obj_var, obj_restriction, verb_pred);
25625
25626 let effective_time = self.pending_time.take().unwrap_or(Time::None);
25627 let with_time = match effective_time {
25628 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25629 operator: TemporalOperator::Past,
25630 body: quantified,
25631 }),
25632 Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25633 operator: TemporalOperator::Future,
25634 body: quantified,
25635 }),
25636 _ => quantified,
25637 };
25638
25639 self.negative_depth -= 1;
25640 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25641 op: TokenType::Not,
25642 operand: with_time,
25643 }));
25644 }
25645
25646 if self.check_pronoun() {
25652 let pronoun_token = self.advance();
25653 let pronoun_sym = pronoun_token.lexeme;
25654 let roles = vec![
25655 (ThematicRole::Agent, subject_term.clone()),
25656 (ThematicRole::Theme, Term::Constant(pronoun_sym)),
25657 ];
25658 let effective_time = self.pending_time.take().unwrap_or(Time::None);
25659 let mut modifiers: Vec<Symbol> = vec![];
25660 match effective_time {
25661 Time::Past => modifiers.push(self.interner.intern("Past")),
25662 Time::Future => modifiers.push(self.interner.intern("Future")),
25663 _ => {}
25664 }
25665 let event_var = self.get_event_var();
25666 let suppress_existential = self.drs.in_conditional_antecedent();
25667 let neo_event =
25668 self.ctx
25669 .exprs
25670 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25671 event_var,
25672 verb,
25673 roles: self.ctx.roles.alloc_slice(roles),
25674 modifiers: self.ctx.syms.alloc_slice(modifiers),
25675 suppress_existential,
25676 world: None,
25677 })));
25678 self.negative_depth -= 1;
25679 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25680 op: TokenType::Not,
25681 operand: neo_event,
25682 }));
25683 }
25684
25685 let vp = self.build_verb_vp(
25692 subject.noun,
25693 subject_term,
25694 false,
25695 verb,
25696 verb_time,
25697 verb_aspect,
25698 verb_class,
25699 )?;
25700
25701 self.negative_depth -= 1;
25702 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25703 op: TokenType::Not,
25704 operand: vp,
25705 }));
25706 }
25707
25708 self.negative_depth -= 1;
25709 }
25710 }
25712
25713 if self.check_presup_trigger()
25719 && !self.is_followed_by_np_object()
25720 && self.is_followed_by_gerund()
25721 {
25722 let presup_kind = self.consume_presup_trigger();
25723 return self.parse_presupposition(&subject, presup_kind, false);
25724 }
25725
25726 let noun_str = self.interner.resolve(subject.noun);
25728 let is_known_plural = {
25731 let lower = noun_str.to_lowercase();
25732 Self::is_plural_noun(noun_str)
25733 && (crate::lexicon::is_common_noun(&lower)
25734 || matches!(
25735 crate::lexicon::analyze_word(&lower),
25736 Some(crate::lexicon::WordAnalysis::Noun(_))
25737 ))
25738 };
25739 let generic_do_not = matches!(self.peek().kind, TokenType::Does | TokenType::Do)
25743 && matches!(
25744 self.tokens.get(self.current + 1).map(|t| &t.kind),
25745 Some(TokenType::Not)
25746 );
25747 let is_bare_plural = subject.definiteness.is_none()
25748 && subject.possessor.is_none()
25749 && is_known_plural
25750 && (self.check_verb() || generic_do_not);
25751
25752 if is_bare_plural && generic_do_not {
25753 self.advance(); self.advance(); let var_name = self.next_var_name();
25759 let (verb, _t, _a, _) = self.consume_verb_with_metadata();
25760 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25761 name: subject.noun,
25762 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25763 world: None,
25764 });
25765 let event_var = self.get_event_var();
25766 let verb_pred = self
25767 .ctx
25768 .exprs
25769 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25770 event_var,
25771 verb,
25772 roles: self
25773 .ctx
25774 .roles
25775 .alloc_slice(vec![(ThematicRole::Agent, Term::Variable(var_name))]),
25776 modifiers: self.ctx.syms.alloc_slice(vec![]),
25777 suppress_existential: false,
25778 world: None,
25779 })));
25780 let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25781 op: TokenType::Not,
25782 operand: verb_pred,
25783 });
25784 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25785 left: type_pred,
25786 op: TokenType::Implies,
25787 right: negated,
25788 });
25789 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
25790 kind: QuantifierKind::Generic,
25791 variable: var_name,
25792 body,
25793 island_id: self.current_island,
25794 }));
25795 }
25796
25797 if is_bare_plural {
25798 let var_name = self.next_var_name();
25799 let (verb, verb_time, verb_aspect, _) = self.consume_verb_with_metadata();
25800
25801 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25802 name: subject.noun,
25803 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25804 world: None,
25805 });
25806
25807 let mut roles = vec![(ThematicRole::Agent, Term::Variable(var_name))];
25811 if self.check_content_word() {
25812 let object = self.parse_noun_phrase(false)?;
25813 roles.push((ThematicRole::Theme, self.noun_phrase_to_term(&object)));
25814 }
25815
25816 let manner_adverbs = self.collect_adverbs();
25820
25821 let event_var = self.get_event_var();
25822 let verb_pred = self
25823 .ctx
25824 .exprs
25825 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25826 event_var,
25827 verb,
25828 roles: self.ctx.roles.alloc_slice(roles),
25829 modifiers: self.ctx.syms.alloc_slice(manner_adverbs),
25830 suppress_existential: false,
25831 world: None,
25832 })));
25833
25834 let effective_time = self.pending_time.take().unwrap_or(verb_time);
25835 let with_time = match effective_time {
25836 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25837 operator: TemporalOperator::Past,
25838 body: verb_pred,
25839 }),
25840 Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25841 operator: TemporalOperator::Future,
25842 body: verb_pred,
25843 }),
25844 _ => verb_pred,
25845 };
25846
25847 let with_aspect = if verb_aspect == Aspect::Progressive {
25848 self.ctx.exprs.alloc(LogicExpr::Aspectual {
25849 operator: AspectOperator::Progressive,
25850 body: with_time,
25851 })
25852 } else {
25853 with_time
25854 };
25855
25856 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25857 left: type_pred,
25858 op: TokenType::Implies,
25859 right: with_aspect,
25860 });
25861
25862 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
25863 kind: QuantifierKind::Generic,
25864 variable: var_name,
25865 body,
25866 island_id: self.current_island,
25867 }));
25868 }
25869
25870 if self.check(&TokenType::Does) || self.check(&TokenType::Do) {
25872 self.advance(); let is_negated = self.match_token(&[TokenType::Not]);
25874
25875 if self.check_presup_trigger()
25878 && !self.is_followed_by_np_object()
25879 && self.is_followed_by_gerund()
25880 {
25881 let presup_kind = self.consume_presup_trigger();
25882 return self.parse_presupposition(&subject, presup_kind, is_negated);
25883 }
25884
25885 if self.check_verb() {
25886 let (verb, verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
25887 let verb_lemma = self.interner.resolve(verb).to_lowercase();
25888
25889 if self.check_wh_word() {
25891 let wh_token = self.advance().kind.clone();
25892 let is_who = matches!(wh_token, TokenType::Who);
25893 let is_what = matches!(wh_token, TokenType::What);
25894
25895 let is_sluicing = self.is_at_end()
25896 || self.check(&TokenType::Period)
25897 || self.check(&TokenType::Comma);
25898
25899 if is_sluicing {
25900 if let Some(template) = self.last_event_template.clone() {
25901 let wh_var = self.next_var_name();
25902 let subject_term = self.noun_phrase_to_term(&subject);
25903
25904 let roles: Vec<_> = if is_who {
25905 std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
25906 .chain(template.non_agent_roles.iter().cloned())
25907 .collect()
25908 } else if is_what {
25909 vec![
25910 (ThematicRole::Agent, subject_term.clone()),
25911 (ThematicRole::Theme, Term::Variable(wh_var)),
25912 ]
25913 } else {
25914 std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
25915 .chain(template.non_agent_roles.iter().cloned())
25916 .collect()
25917 };
25918
25919 let event_var = self.get_event_var();
25920 let suppress_existential = self.drs.in_conditional_antecedent();
25921 if suppress_existential {
25922 let event_class = self.interner.intern("Event");
25923 self.drs.introduce_event_referent(event_var, event_class);
25924 }
25925 let reconstructed =
25926 self.ctx
25927 .exprs
25928 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25929 event_var,
25930 verb: template.verb,
25931 roles: self.ctx.roles.alloc_slice(roles),
25932 modifiers: self
25933 .ctx
25934 .syms
25935 .alloc_slice(template.modifiers.clone()),
25936 suppress_existential,
25937 world: None,
25938 })));
25939
25940 let question = self.ctx.exprs.alloc(LogicExpr::Question {
25941 wh_variable: wh_var,
25942 body: reconstructed,
25943 });
25944
25945 let know_event_var = self.get_event_var();
25946 let suppress_existential2 = self.drs.in_conditional_antecedent();
25947 if suppress_existential2 {
25948 let event_class = self.interner.intern("Event");
25949 self.drs
25950 .introduce_event_referent(know_event_var, event_class);
25951 }
25952 let know_event =
25953 self.ctx
25954 .exprs
25955 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25956 event_var: know_event_var,
25957 verb,
25958 roles: self.ctx.roles.alloc_slice(vec![
25959 (ThematicRole::Agent, subject_term),
25960 (ThematicRole::Theme, Term::Proposition(question)),
25961 ]),
25962 modifiers: self.ctx.syms.alloc_slice(vec![]),
25963 suppress_existential: suppress_existential2,
25964 world: None,
25965 })));
25966
25967 let result = if is_negated {
25968 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25969 op: TokenType::Not,
25970 operand: know_event,
25971 })
25972 } else {
25973 know_event
25974 };
25975
25976 return self.wrap_with_definiteness_full(&subject, result);
25977 }
25978 }
25979 }
25980
25981 if verb_lemma == "exist" && is_negated {
25983 let var_name = self.next_var_name();
25985 let restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25986 name: subject.noun,
25987 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25988 world: None,
25989 });
25990 let exists = self.ctx.exprs.alloc(LogicExpr::Quantifier {
25991 kind: QuantifierKind::Existential,
25992 variable: var_name,
25993 body: restriction,
25994 island_id: self.current_island,
25995 });
25996 return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25997 op: TokenType::Not,
25998 operand: exists,
25999 }));
26000 }
26001
26002 let subject_term = self.noun_phrase_to_term(&subject);
26005 let modifiers: Vec<Symbol> = vec![];
26006
26007 if self.check(&TokenType::Reflexive) {
26009 self.advance();
26010 let roles = vec![
26011 (ThematicRole::Agent, subject_term.clone()),
26012 (ThematicRole::Theme, subject_term),
26013 ];
26014 let event_var = self.get_event_var();
26015 let suppress_existential = self.drs.in_conditional_antecedent();
26016 if suppress_existential {
26017 let event_class = self.interner.intern("Event");
26018 self.drs.introduce_event_referent(event_var, event_class);
26019 }
26020 let neo_event =
26021 self.ctx
26022 .exprs
26023 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26024 event_var,
26025 verb,
26026 roles: self.ctx.roles.alloc_slice(roles),
26027 modifiers: self.ctx.syms.alloc_slice(modifiers),
26028 suppress_existential,
26029 world: None,
26030 })));
26031
26032 let result = if is_negated {
26033 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26034 op: TokenType::Not,
26035 operand: neo_event,
26036 })
26037 } else {
26038 neo_event
26039 };
26040 return self.wrap_with_definiteness_full(&subject, result);
26041 }
26042
26043 if self.check_npi_quantifier() || self.check_quantifier() || self.check_article() {
26045 let (obj_quantifier, was_definite_article) = if self.check_npi_quantifier() {
26046 let tok = self.advance().kind.clone();
26048 (Some(tok), false)
26049 } else if self.check_quantifier() {
26050 (Some(self.advance().kind.clone()), false)
26051 } else {
26052 let art = self.advance().kind.clone();
26053 if let TokenType::Article(def) = art {
26054 if def == Definiteness::Indefinite {
26055 (Some(TokenType::Some), false)
26056 } else {
26057 (None, true)
26058 }
26059 } else {
26060 (None, false)
26061 }
26062 };
26063
26064 let object_np = self.parse_noun_phrase(false)?;
26065 let obj_var = self.next_var_name();
26066
26067 let mut type_pred: &'a LogicExpr<'a> =
26068 self.ctx.exprs.alloc(LogicExpr::Predicate {
26069 name: object_np.noun,
26070 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
26071 world: None,
26072 });
26073 for &adj in object_np.adjectives {
26077 let adj_pred = self.adjective_restriction(adj, obj_var, object_np.noun);
26078 type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26079 left: type_pred,
26080 op: TokenType::And,
26081 right: adj_pred,
26082 });
26083 }
26084 for pp in object_np.pps {
26085 let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
26086 type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26087 left: type_pred,
26088 op: TokenType::And,
26089 right: pp_sub,
26090 });
26091 }
26092
26093 let obj_restriction =
26095 if self.check(&TokenType::That) || self.check(&TokenType::Who) {
26096 self.advance();
26097 let rel_clause = self.parse_relative_clause(obj_var)?;
26098 self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26099 left: type_pred,
26100 op: TokenType::And,
26101 right: rel_clause,
26102 })
26103 } else {
26104 type_pred
26105 };
26106
26107 let event_var = self.get_event_var();
26108 let suppress_existential = self.drs.in_conditional_antecedent();
26109 if suppress_existential {
26110 let event_class = self.interner.intern("Event");
26111 self.drs.introduce_event_referent(event_var, event_class);
26112 }
26113
26114 let roles = vec![
26115 (ThematicRole::Agent, subject_term),
26116 (ThematicRole::Theme, Term::Variable(obj_var)),
26117 ];
26118
26119 let neo_event =
26120 self.ctx
26121 .exprs
26122 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26123 event_var,
26124 verb,
26125 roles: self.ctx.roles.alloc_slice(roles),
26126 modifiers: self.ctx.syms.alloc_slice(modifiers),
26127 suppress_existential,
26128 world: None,
26129 })));
26130
26131 let obj_determiner = obj_quantifier.clone().unwrap_or(TokenType::Some);
26134 let obj_quantified =
26135 self.quantify_argument(&obj_determiner, obj_var, obj_restriction, neo_event);
26136
26137 let result = if is_negated && matches!(obj_quantifier, Some(TokenType::Any)) {
26139 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26140 op: TokenType::Not,
26141 operand: obj_quantified,
26142 })
26143 } else if is_negated {
26144 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26146 op: TokenType::Not,
26147 operand: obj_quantified,
26148 })
26149 } else {
26150 obj_quantified
26151 };
26152
26153 return self.wrap_with_definiteness_full(&subject, result);
26154 }
26155
26156 if is_negated {
26163 self.negative_depth += 1;
26164 }
26165 let vp = self.build_verb_vp(
26166 subject.noun,
26167 subject_term,
26168 false,
26169 verb,
26170 verb_time,
26171 verb_aspect,
26172 verb_class,
26173 )?;
26174 if is_negated {
26175 self.negative_depth -= 1;
26176 }
26177 let result = if is_negated {
26181 self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26182 op: TokenType::Not,
26183 operand: vp,
26184 })
26185 } else {
26186 vp
26187 };
26188 return Ok(result);
26189 }
26190 }
26191
26192 let is_perfect_aux = if self.check_verb() {
26198 let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
26199 word == "has" || word == "have" || word == "had"
26200 } else {
26201 false
26202 };
26203 if subject.definiteness == Some(Definiteness::Definite)
26204 && self.check_verb()
26205 && self.pending_time.is_none()
26206 && !is_perfect_aux
26207 {
26208 let saved_pos = self.backtrack_point();
26209
26210 if let Some(garden_path_result) = self.try_parse(|p| {
26212 let (modifier_verb, _modifier_time, _, _) = p.consume_verb_with_metadata();
26213
26214 let mut pp_mods: Vec<&'a LogicExpr<'a>> = Vec::new();
26216 while p.check_preposition() {
26217 let prep = if let TokenType::Preposition(prep) = p.advance().kind {
26218 prep
26219 } else {
26220 break;
26221 };
26222 if p.check_article() || p.check_content_word() {
26223 let pp_obj = p.parse_noun_phrase(false)?;
26224 let pp_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26225 name: prep,
26226 args: p.ctx.terms.alloc_slice([
26227 Term::Variable(p.interner.intern("x")),
26228 Term::Constant(pp_obj.noun),
26229 ]),
26230 world: None,
26231 });
26232 pp_mods.push(pp_pred);
26233 }
26234 }
26235
26236 if !p.check_verb() {
26238 return Err(ParseError {
26239 kind: ParseErrorKind::ExpectedVerb {
26240 found: p.peek().kind.clone(),
26241 },
26242 span: p.current_span(),
26243 });
26244 }
26245
26246 let (main_verb, main_time, _, _) = p.consume_verb_with_metadata();
26247
26248 let main_complement = if let TokenType::Adjective(a) = p.peek().kind {
26251 p.advance();
26252 Some(a)
26253 } else {
26254 None
26255 };
26256
26257 if !p.at_clause_boundary() {
26260 return Err(ParseError {
26261 kind: ParseErrorKind::TrailingTokens {
26262 found: p.peek().kind.clone(),
26263 },
26264 span: p.current_span(),
26265 });
26266 }
26267
26268 let var = p.interner.intern("x");
26270
26271 let type_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26273 name: subject.noun,
26274 args: p.ctx.terms.alloc_slice([Term::Variable(var)]),
26275 world: None,
26276 });
26277
26278 let mod_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26280 name: modifier_verb,
26281 args: p.ctx.terms.alloc_slice([Term::Variable(var)]),
26282 world: None,
26283 });
26284
26285 let main_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26287 name: main_verb,
26288 args: match main_complement {
26289 Some(adj) => p
26290 .ctx
26291 .terms
26292 .alloc_slice([Term::Variable(var), Term::Constant(adj)]),
26293 None => p.ctx.terms.alloc_slice([Term::Variable(var)]),
26294 },
26295 world: None,
26296 });
26297
26298 let mut body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26300 left: type_pred,
26301 op: TokenType::And,
26302 right: mod_pred,
26303 });
26304
26305 for pp in pp_mods {
26307 body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26308 left: body,
26309 op: TokenType::And,
26310 right: pp,
26311 });
26312 }
26313
26314 body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26316 left: body,
26317 op: TokenType::And,
26318 right: main_pred,
26319 });
26320
26321 let with_time = match main_time {
26323 Time::Past => p.ctx.exprs.alloc(LogicExpr::Temporal {
26324 operator: TemporalOperator::Past,
26325 body,
26326 }),
26327 Time::Future => p.ctx.exprs.alloc(LogicExpr::Temporal {
26328 operator: TemporalOperator::Future,
26329 body,
26330 }),
26331 _ => body,
26332 };
26333
26334 Ok(p.ctx.exprs.alloc(LogicExpr::Quantifier {
26336 kind: QuantifierKind::Existential,
26337 variable: var,
26338 body: with_time,
26339 island_id: p.current_island,
26340 }))
26341 }) {
26342 return Ok(garden_path_result);
26343 }
26344
26345 self.backtrack_to(&saved_pos);
26347 }
26348
26349 if self.check_modal() {
26350 return self.parse_aspect_chain(subject.noun);
26351 }
26352
26353 if self.check_content_word() {
26355 let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
26356 if word == "has" || word == "have" || word == "had" {
26357 let is_perfect_aspect = if self.current + 1 < self.tokens.len() {
26359 let next_token = &self.tokens[self.current + 1].kind;
26360 matches!(next_token, TokenType::Verb { .. } | TokenType::Not)
26361 && !matches!(next_token, TokenType::Number(_))
26362 } else {
26363 false
26364 };
26365 if is_perfect_aspect {
26366 return self.parse_aspect_chain(subject.noun);
26367 }
26368 }
26370 }
26371
26372 if self.check(&TokenType::Had) {
26377 let next_is_participle = matches!(
26378 self.tokens.get(self.current + 1).map(|t| &t.kind),
26379 Some(TokenType::Verb { .. })
26380 );
26381 if next_is_participle {
26382 return self.parse_aspect_chain(subject.noun);
26383 }
26384 let have_lemma = self.interner.intern("Have");
26385 self.tokens[self.current].kind = TokenType::Verb {
26386 lemma: have_lemma,
26387 time: Time::Past,
26388 aspect: Aspect::Simple,
26389 class: VerbClass::State,
26390 };
26391 }
26393
26394 if self.check(&TokenType::Never) {
26396 self.advance();
26397 let verb = self.consume_verb();
26398 let subject_term = self.noun_phrase_to_term(&subject);
26399
26400 let mut object_quant: Option<(QuantifierKind, Symbol, Symbol)> = None;
26404 let mut object_term: Option<Term<'a>> = None;
26405 if let TokenType::Cardinal(n) = self.peek().kind {
26406 self.advance();
26407 let np = self.parse_noun_phrase(false)?;
26408 let var = self.next_var_name();
26409 object_quant = Some((QuantifierKind::Cardinal(n), var, np.noun));
26410 object_term = Some(Term::Variable(var));
26411 } else if matches!(self.peek().kind, TokenType::All | TokenType::Some) {
26412 let universal = matches!(self.advance().kind, TokenType::All);
26413 let np = self.parse_noun_phrase(false)?;
26414 let var = self.next_var_name();
26415 let kind = if universal {
26416 QuantifierKind::Universal
26417 } else {
26418 QuantifierKind::Existential
26419 };
26420 object_quant = Some((kind, var, np.noun));
26421 object_term = Some(Term::Variable(var));
26422 } else if self.check_content_word() || self.check_article() {
26423 let np = self.parse_noun_phrase(false)?;
26424 object_term = Some(self.noun_phrase_to_term(&np));
26425 }
26426
26427 if object_term.is_none() {
26428 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26429 name: verb,
26430 args: self.ctx.terms.alloc_slice([subject_term]),
26431 world: None,
26432 });
26433 let result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26434 op: TokenType::Not,
26435 operand: verb_pred,
26436 });
26437 return self.wrap_with_definiteness_full(&subject, result);
26438 }
26439
26440 let mut roles = vec![(crate::ast::ThematicRole::Agent, subject_term)];
26441 if let Some(t) = object_term {
26442 roles.push((crate::ast::ThematicRole::Theme, t));
26443 }
26444 let modifiers = self.collect_adverbs();
26445 let event_var = self.get_event_var();
26446 let suppress_existential = self.drs.in_conditional_antecedent();
26447 let event = self
26448 .ctx
26449 .exprs
26450 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26451 event_var,
26452 verb,
26453 roles: self.ctx.roles.alloc_slice(roles),
26454 modifiers: self.ctx.syms.alloc_slice(modifiers),
26455 suppress_existential,
26456 world: None,
26457 })));
26458 let mut result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26459 op: TokenType::Not,
26460 operand: event,
26461 });
26462 if let Some((kind, var, noun)) = object_quant {
26463 let restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
26464 name: noun,
26465 args: self.ctx.terms.alloc_slice([Term::Variable(var)]),
26466 world: None,
26467 });
26468 let connective = if matches!(kind, QuantifierKind::Universal) {
26469 TokenType::Implies
26470 } else {
26471 TokenType::And
26472 };
26473 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26474 left: restriction,
26475 op: connective,
26476 right: result,
26477 });
26478 result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
26479 kind,
26480 variable: var,
26481 body,
26482 island_id: self.current_island,
26483 });
26484 }
26485 return self.wrap_with_definiteness_full(&subject, result);
26486 }
26487
26488 if let TokenType::Adverb(sym)
26494 | TokenType::ScopalAdverb(sym)
26495 | TokenType::TemporalAdverb(sym) = self.peek().kind
26496 {
26497 let w = self.interner.resolve(sym).to_lowercase();
26500 if crate::lexicon::is_quantificational_adverb(&w)
26501 && self.current + 1 < self.tokens.len()
26502 && matches!(self.tokens[self.current + 1].kind, TokenType::Verb { .. })
26503 {
26504 self.advance();
26505 }
26506 }
26507
26508 if self.check_verb() {
26509 let (mut verb, verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
26510
26511 let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
26513 let verb_str = self.interner.resolve(verb);
26514 if let Some(s_sort) = subject_sort {
26515 if !crate::ontology::check_sort_compatibility(verb_str, s_sort) {
26516 let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
26517 tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
26518 vehicle: self.ctx.terms.alloc(Term::Constant(verb)),
26519 });
26520 return self.wrap_with_definiteness_full(&subject, metaphor);
26521 }
26522 }
26523
26524 if let Some(cmp) =
26529 self.try_arithmetic_comparative(verb, Term::Constant(subject.noun), verb_time)?
26530 {
26531 return self.wrap_with_definiteness_full(&subject, cmp);
26532 }
26533
26534 if let Some(off) =
26536 self.try_temporal_offset(verb, Term::Constant(subject.noun), verb_time)?
26537 {
26538 return self.wrap_with_definiteness_full(&subject, off);
26539 }
26540
26541 if let Some(off) =
26543 self.try_positional_offset(verb, Term::Constant(subject.noun), verb_time)?
26544 {
26545 return self.wrap_with_definiteness_full(&subject, off);
26546 }
26547
26548 let is_raising =
26559 crate::lexicon::is_raising_verb(&self.interner.resolve(verb).to_lowercase());
26560 let next_is_to_be = self.check(&TokenType::To)
26561 && self.current + 1 < self.tokens.len()
26562 && matches!(self.tokens[self.current + 1].kind, TokenType::Be);
26563 let raising_with_complement = is_raising
26564 && (matches!(self.peek().kind, TokenType::Adjective(_))
26565 || next_is_to_be
26566 || (self.check_content_word() && !self.check(&TokenType::To)));
26567 if raising_with_complement {
26568 if self.check(&TokenType::To) {
26570 self.advance();
26571 if self.check(&TokenType::Be) {
26572 self.advance();
26573 }
26574 }
26575 let complement = if let TokenType::Adjective(adj) = self.peek().kind {
26576 self.advance();
26577 self.ctx.exprs.alloc(LogicExpr::Predicate {
26578 name: adj,
26579 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
26580 world: None,
26581 })
26582 } else if self.check_content_word() {
26583 self.parse_predicate_with_subject(subject.noun)?
26584 } else {
26585 self.ctx
26586 .exprs
26587 .alloc(LogicExpr::Atom(self.interner.intern("?")))
26588 };
26589 let evidential = self.ctx.exprs.alloc(LogicExpr::Modal {
26590 vector: crate::ast::ModalVector::new(
26591 crate::ast::ModalDomain::Alethic,
26592 1.0,
26593 crate::ast::ModalFlavor::Evidential,
26594 )
26595 .with_base(verb),
26596 operand: complement,
26597 });
26598 return self.wrap_with_definiteness_full(&subject, evidential);
26599 }
26600
26601 if crate::lexicon::is_perception_verb(&self.interner.resolve(verb).to_lowercase()) {
26608 let is_verb_capable = |kind: &TokenType| match kind {
26609 TokenType::Verb { .. } => true,
26610 TokenType::Ambiguous {
26611 primary,
26612 alternatives,
26613 } => {
26614 matches!(**primary, TokenType::Verb { .. })
26615 || alternatives
26616 .iter()
26617 .any(|t| matches!(t, TokenType::Verb { .. }))
26618 }
26619 _ => false,
26620 };
26621 let mut vp_idx = None;
26622 let mut i = self.current;
26623 while i < self.tokens.len() {
26624 if matches!(
26627 self.tokens[i].kind,
26628 TokenType::Period
26629 | TokenType::EOF
26630 | TokenType::Comma
26631 | TokenType::And
26632 | TokenType::Or
26633 ) {
26634 break;
26635 }
26636 let lex = self.interner.resolve(self.tokens[i].lexeme).to_lowercase();
26639 if is_verb_capable(&self.tokens[i].kind)
26640 || crate::lexicon::lookup_verb_db(&lex).is_some()
26641 {
26642 vp_idx = Some(i);
26643 }
26644 i += 1;
26645 }
26646 if let Some(vp_i) = vp_idx {
26647 if vp_i > self.current {
26649 let subj_kind = self.tokens[vp_i - 1].kind.clone();
26650 let psubj = match subj_kind {
26651 TokenType::Noun(n) | TokenType::ProperName(n) => Some(n),
26652 TokenType::Article(_)
26654 | TokenType::Preposition(_)
26655 | TokenType::Period
26656 | TokenType::Comma => None,
26657 TokenType::Adjective(_)
26663 | TokenType::NonIntersectiveAdjective(_)
26664 | TokenType::Cardinal(_)
26665 | TokenType::Number(_)
26666 | TokenType::AtLeast(_)
26667 | TokenType::AtMost(_) => None,
26668 _ => {
26671 let lx = self
26672 .interner
26673 .resolve(self.tokens[vp_i - 1].lexeme)
26674 .to_lowercase();
26675 let cap = lx
26676 .chars()
26677 .next()
26678 .map(|c| c.to_uppercase().collect::<String>() + &lx[1..])
26679 .unwrap_or(lx);
26680 Some(self.interner.intern(&cap))
26681 }
26682 };
26683 if let Some(psubj) = psubj {
26684 let vlex = self
26685 .interner
26686 .resolve(self.tokens[vp_i].lexeme)
26687 .to_lowercase();
26688 let vname = vlex
26689 .chars()
26690 .next()
26691 .map(|c| c.to_uppercase().collect::<String>() + &vlex[1..])
26692 .unwrap_or(vlex);
26693 let inner_verb = self.interner.intern(&vname);
26694 self.current = vp_i + 1; let perceived = self.ctx.exprs.alloc(LogicExpr::Predicate {
26696 name: inner_verb,
26697 args: self.ctx.terms.alloc_slice([Term::Constant(psubj)]),
26698 world: None,
26699 });
26700 let perceived_advs = self.collect_adverbs();
26703 let perceived = if perceived_advs.is_empty() {
26704 perceived
26705 } else {
26706 self.ctx.exprs.alloc(LogicExpr::Event {
26707 predicate: perceived,
26708 adverbs: self.ctx.syms.alloc_slice(perceived_advs),
26709 })
26710 };
26711 let event_var = self.get_event_var();
26712 let roles = vec![
26713 (ThematicRole::Agent, Term::Constant(subject.noun)),
26714 (ThematicRole::Theme, Term::Proposition(perceived)),
26715 ];
26716 let mut modifiers: Vec<Symbol> = Vec::new();
26717 match verb_time {
26718 Time::Past => modifiers.push(self.interner.intern("Past")),
26719 Time::Future => modifiers.push(self.interner.intern("Future")),
26720 _ => {}
26721 }
26722 let neo =
26723 self.ctx
26724 .exprs
26725 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26726 event_var,
26727 verb,
26728 roles: self.ctx.roles.alloc_slice(roles),
26729 modifiers: self.ctx.syms.alloc_slice(modifiers),
26730 suppress_existential: false,
26731 world: None,
26732 })));
26733 return self.wrap_with_definiteness_full(&subject, neo);
26734 }
26735 }
26736 }
26737 }
26738
26739 if self.is_control_verb(verb)
26747 && (self.check_to()
26748 || lexicon::is_object_control_verb(&self.interner.resolve(verb).to_lowercase()))
26749 {
26750 return self.parse_control_structure(&subject, verb, verb_time);
26751 }
26752
26753 if let Some((var_name, rel_clause)) = relative_clause {
26755 let with_time = if self.at_clause_boundary() {
26759 let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26760 name: verb,
26761 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
26762 world: None,
26763 });
26764
26765 let effective_time = self.pending_time.take().unwrap_or(verb_time);
26766 match effective_time {
26767 Time::Past => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
26768 operator: TemporalOperator::Past,
26769 body: main_pred,
26770 }),
26771 Time::Future => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
26772 operator: TemporalOperator::Future,
26773 body: main_pred,
26774 }),
26775 _ => main_pred,
26776 }
26777 } else {
26778 self.current -= 1; self.parse_predicate_with_subject_as_var(var_name)?
26780 };
26781
26782 let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26784 name: subject.noun,
26785 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
26786 world: None,
26787 });
26788
26789 let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26790 left: type_pred,
26791 op: TokenType::And,
26792 right: rel_clause,
26793 });
26794
26795 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26796 left: inner,
26797 op: TokenType::And,
26798 right: with_time,
26799 });
26800
26801 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
26802 kind: QuantifierKind::Existential,
26803 variable: var_name,
26804 body,
26805 island_id: self.current_island,
26806 }));
26807 }
26808
26809 let subject_term = self.noun_phrase_to_term(&subject);
26810 let mut args = vec![subject_term.clone()];
26811
26812 let unknown = self.interner.intern("?");
26813
26814 if self.check_wh_word() {
26816 let wh_token = self.advance().kind.clone();
26817
26818 let is_who = matches!(wh_token, TokenType::Who);
26820 let is_what = matches!(wh_token, TokenType::What);
26821
26822 let is_sluicing = self.is_at_end()
26824 || self.check(&TokenType::Period)
26825 || self.check(&TokenType::Comma);
26826
26827 if is_sluicing {
26828 if let Some(template) = self.last_event_template.clone() {
26830 let wh_var = self.next_var_name();
26831
26832 let roles: Vec<_> = if is_who {
26834 std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
26836 .chain(template.non_agent_roles.iter().cloned())
26837 .collect()
26838 } else if is_what {
26839 vec![
26841 (ThematicRole::Agent, subject_term.clone()),
26842 (ThematicRole::Theme, Term::Variable(wh_var)),
26843 ]
26844 } else {
26845 std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
26847 .chain(template.non_agent_roles.iter().cloned())
26848 .collect()
26849 };
26850
26851 let event_var = self.get_event_var();
26852 let suppress_existential = self.drs.in_conditional_antecedent();
26853 if suppress_existential {
26854 let event_class = self.interner.intern("Event");
26855 self.drs.introduce_event_referent(event_var, event_class);
26856 }
26857 let reconstructed =
26858 self.ctx
26859 .exprs
26860 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26861 event_var,
26862 verb: template.verb,
26863 roles: self.ctx.roles.alloc_slice(roles),
26864 modifiers: self
26865 .ctx
26866 .syms
26867 .alloc_slice(template.modifiers.clone()),
26868 suppress_existential,
26869 world: None,
26870 })));
26871
26872 let question = self.ctx.exprs.alloc(LogicExpr::Question {
26873 wh_variable: wh_var,
26874 body: reconstructed,
26875 });
26876
26877 let know_event_var = self.get_event_var();
26879 let suppress_existential2 = self.drs.in_conditional_antecedent();
26880 if suppress_existential2 {
26881 let event_class = self.interner.intern("Event");
26882 self.drs
26883 .introduce_event_referent(know_event_var, event_class);
26884 }
26885 let know_event =
26886 self.ctx
26887 .exprs
26888 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26889 event_var: know_event_var,
26890 verb,
26891 roles: self.ctx.roles.alloc_slice(vec![
26892 (ThematicRole::Agent, subject_term),
26893 (ThematicRole::Theme, Term::Proposition(question)),
26894 ]),
26895 modifiers: self.ctx.syms.alloc_slice(vec![]),
26896 suppress_existential: suppress_existential2,
26897 world: None,
26898 })));
26899
26900 return self.wrap_with_definiteness_full(&subject, know_event);
26901 }
26902 }
26903
26904 let embedded = self.parse_embedded_wh_clause()?;
26906 let question = self.ctx.exprs.alloc(LogicExpr::Question {
26907 wh_variable: self.interner.intern("x"),
26908 body: embedded,
26909 });
26910
26911 let know_event_var = self.get_event_var();
26913 let suppress_existential = self.drs.in_conditional_antecedent();
26914 if suppress_existential {
26915 let event_class = self.interner.intern("Event");
26916 self.drs
26917 .introduce_event_referent(know_event_var, event_class);
26918 }
26919 let know_event =
26920 self.ctx
26921 .exprs
26922 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26923 event_var: know_event_var,
26924 verb,
26925 roles: self.ctx.roles.alloc_slice(vec![
26926 (ThematicRole::Agent, subject_term),
26927 (ThematicRole::Theme, Term::Proposition(question)),
26928 ]),
26929 modifiers: self.ctx.syms.alloc_slice(vec![]),
26930 suppress_existential,
26931 world: None,
26932 })));
26933
26934 return self.wrap_with_definiteness_full(&subject, know_event);
26935 }
26936
26937 if crate::lexicon::is_opaque_verb(&self.interner.resolve(verb).to_lowercase()) {
26944 let mut i = self.current;
26945 if i < self.tokens.len() && matches!(self.tokens[i].kind, TokenType::That) {
26946 i += 1;
26947 }
26948 let subj_ok = i < self.tokens.len()
26949 && matches!(
26950 self.tokens[i].kind,
26951 TokenType::ProperName(_) | TokenType::Pronoun { .. }
26952 );
26953 let verb_follows = subj_ok
26954 && i + 1 < self.tokens.len()
26955 && (matches!(
26956 self.tokens[i + 1].kind,
26957 TokenType::Verb { .. } | TokenType::Auxiliary(_)
26958 )
26959 || crate::lexicon::lookup_irregular_verb(
26963 &self
26964 .interner
26965 .resolve(self.tokens[i + 1].lexeme)
26966 .to_lowercase(),
26967 )
26968 .is_some());
26969 if verb_follows {
26970 if self.check(&TokenType::That) {
26971 self.advance();
26972 }
26973 let embedded_subject = match self.peek().kind {
26974 TokenType::ProperName(s) => {
26975 self.advance();
26976 s
26977 }
26978 TokenType::Pronoun { gender, number, .. } => {
26979 self.advance();
26980 match self.resolve_pronoun(gender, number)? {
26981 ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
26982 }
26983 }
26984 _ => unreachable!("guarded by subj_ok"),
26985 };
26986 let embedded_pred = self.parse_predicate_with_subject(embedded_subject)?;
26987 let subject_term_for_event = self.coerce_agent(&subject);
26988 let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26989 name: verb,
26990 args: self.ctx.terms.alloc_slice([
26991 subject_term_for_event,
26992 Term::Proposition(embedded_pred),
26993 ]),
26994 world: None,
26995 });
26996 let effective_time = self.pending_time.take().unwrap_or(verb_time);
26997 let with_time = match effective_time {
26998 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
26999 operator: TemporalOperator::Past,
27000 body: main_pred,
27001 }),
27002 Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
27003 operator: TemporalOperator::Future,
27004 body: main_pred,
27005 }),
27006 _ => main_pred,
27007 };
27008 return self.wrap_with_definiteness_full(&subject, with_time);
27009 }
27010 }
27011
27012 let mut object_term: Option<Term<'a>> = None;
27013 let mut object_adjectives: &[Symbol] = &[];
27018 let mut object_desc_pps: &[&'a LogicExpr<'a>] = &[];
27019 let mut secondary_adj: Option<Symbol> = None;
27023 let secondary_role = |verb_lemma: &str| -> ThematicRole {
27026 if crate::lexicon::is_resultative_verb(verb_lemma) {
27027 ThematicRole::Result
27028 } else {
27029 ThematicRole::Depictive
27030 }
27031 };
27032 let mut second_object_term: Option<Term<'a>> = None;
27033 let mut object_superlative: Option<(Symbol, Symbol)> = None; if self.check(&TokenType::Reflexive) {
27035 self.advance();
27036 let term = self.noun_phrase_to_term(&subject);
27037 object_term = Some(term.clone());
27038 args.push(term);
27039
27040 if let TokenType::Particle(particle_sym) = self.peek().kind {
27042 let verb_str = self.interner.resolve(verb).to_lowercase();
27043 let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27044 if let Some((phrasal_lemma, _class)) =
27045 crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27046 {
27047 self.advance();
27048 verb = self.interner.intern(phrasal_lemma);
27049 }
27050 }
27051 } else if self.check_pronoun() {
27052 let token = self.advance().clone();
27053 let pronoun_analysis = match &token.kind {
27057 TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
27058 TokenType::Ambiguous { primary, .. } => match primary.as_ref() {
27059 TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
27060 _ => None,
27061 },
27062 _ => None,
27063 };
27064 if let Some((gender, number)) = pronoun_analysis {
27065 let plex = self.interner.resolve(token.lexeme).to_lowercase();
27068 let deictic_role = match plex.as_str() {
27069 "you" | "yourself" => Some("Addressee"),
27070 "i" | "me" | "myself" => Some("Speaker"),
27071 _ => None,
27072 };
27073 if let Some(role) = deictic_role {
27074 let term = Term::Constant(self.interner.intern(role));
27075 object_term = Some(term.clone());
27076 args.push(term);
27077 } else {
27079 let resolved = self.resolve_pronoun(gender, number)?;
27080 let resolved_sym = match resolved {
27081 ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
27082 };
27083 let term = if resolved_sym == subject.noun {
27088 let deictic = match (gender, number) {
27089 (Gender::Male, Number::Singular) => "Him",
27090 (Gender::Female, Number::Singular) => "Her",
27091 (Gender::Neuter, Number::Singular) => "It",
27092 _ => "Them",
27093 };
27094 let sym = self.interner.intern(deictic);
27095 self.drs.introduce_referent_with_source(
27096 sym,
27097 sym,
27098 gender,
27099 number,
27100 crate::drs::ReferentSource::ProperName,
27101 );
27102 Term::Constant(sym)
27103 } else {
27104 match resolved {
27105 ResolvedPronoun::Variable(s) => Term::Variable(s),
27106 ResolvedPronoun::Constant(s) => Term::Constant(s),
27107 }
27108 };
27109 object_term = Some(term.clone());
27110 args.push(term);
27111
27112 if let TokenType::Particle(particle_sym) = self.peek().kind {
27114 let verb_str = self.interner.resolve(verb).to_lowercase();
27115 let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27116 if let Some((phrasal_lemma, _class)) =
27117 crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27118 {
27119 self.advance();
27120 verb = self.interner.intern(phrasal_lemma);
27121 }
27122 }
27123 } }
27125 } else if self.check(&TokenType::Either) {
27126 self.advance(); let np1 = self.parse_noun_phrase(true)?;
27129 if self.check(&TokenType::Or) {
27130 self.advance(); let np2 = self.parse_noun_phrase(true)?;
27132
27133 let effective_time = self.pending_time.take().unwrap_or(verb_time);
27134 let subject_term1 = self.coerce_agent(&subject);
27135 let subject_term2 = self.coerce_agent(&subject);
27136 let suppress_existential = self.drs.in_conditional_antecedent();
27137
27138 let past_sym = self.interner.intern("Past");
27139 let future_sym = self.interner.intern("Future");
27140 let mut mods1 = vec![];
27141 let mut mods2 = vec![];
27142 match effective_time {
27143 Time::Past => {
27144 mods1.push(past_sym);
27145 mods2.push(past_sym);
27146 }
27147 Time::Future => {
27148 mods1.push(future_sym);
27149 mods2.push(future_sym);
27150 }
27151 _ => {}
27152 }
27153
27154 let event_var1 = self.get_event_var();
27155 let event_var2 = self.get_event_var();
27156
27157 let neo1 = self
27158 .ctx
27159 .exprs
27160 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27161 event_var: event_var1,
27162 verb,
27163 roles: self.ctx.roles.alloc_slice(vec![
27164 (ThematicRole::Agent, subject_term1),
27165 (ThematicRole::Theme, Term::Constant(np1.noun)),
27166 ]),
27167 modifiers: self.ctx.syms.alloc_slice(mods1),
27168 suppress_existential,
27169 world: None,
27170 })));
27171 let neo2 = self
27172 .ctx
27173 .exprs
27174 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27175 event_var: event_var2,
27176 verb,
27177 roles: self.ctx.roles.alloc_slice(vec![
27178 (ThematicRole::Agent, subject_term2),
27179 (ThematicRole::Theme, Term::Constant(np2.noun)),
27180 ]),
27181 modifiers: self.ctx.syms.alloc_slice(mods2),
27182 suppress_existential,
27183 world: None,
27184 })));
27185
27186 let placeholder = self.interner.intern("_PP_SELF_");
27190 let possesses = self.interner.intern("Possesses");
27191 let mut augment = |p: &mut Self,
27192 neo: &'a LogicExpr<'a>,
27193 np: &crate::ast::NounPhrase<'a>|
27194 -> &'a LogicExpr<'a> {
27195 let obj = Term::Constant(np.noun);
27196 let mut e = neo;
27197 if let Some(possessor) = np.possessor {
27198 let poss = p.ctx.exprs.alloc(LogicExpr::Predicate {
27199 name: possesses,
27200 args: p
27201 .ctx
27202 .terms
27203 .alloc_slice([Term::Constant(possessor.noun), obj]),
27204 world: None,
27205 });
27206 e = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
27207 left: e,
27208 op: TokenType::And,
27209 right: poss,
27210 });
27211 }
27212 for pp in np.pps {
27213 let pp_sub = match pp {
27214 LogicExpr::Predicate { name, args, world } => {
27215 let new_args: Vec<Term<'a>> = args
27216 .iter()
27217 .map(|a| match a {
27218 Term::Variable(v) if *v == placeholder => obj,
27219 other => *other,
27220 })
27221 .collect();
27222 p.ctx.exprs.alloc(LogicExpr::Predicate {
27223 name: *name,
27224 args: p.ctx.terms.alloc_slice(new_args),
27225 world: *world,
27226 })
27227 }
27228 other => *other,
27229 };
27230 e = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
27231 left: e,
27232 op: TokenType::And,
27233 right: pp_sub,
27234 });
27235 }
27236 e
27237 };
27238 let neo1 = augment(self, neo1, &np1);
27239 let neo2 = augment(self, neo2, &np2);
27240 let disj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27241 left: neo1,
27242 op: TokenType::Or,
27243 right: neo2,
27244 });
27245 return self.wrap_with_definiteness_full(&subject, disj);
27246 }
27247 return Err(ParseError {
27248 kind: ParseErrorKind::UnexpectedToken {
27249 expected: TokenType::Or,
27250 found: self.peek().kind.clone(),
27251 },
27252 span: self.current_span(),
27253 });
27254 } else if self.counting_np_lookahead().is_some()
27255 || self.check_quantifier()
27256 || self.check_article()
27257 || self.check_possessive_pronoun()
27258 {
27259 let (obj_quantifier, was_definite_article) =
27261 if let Some(n) = self.counting_np_lookahead() {
27262 self.advance();
27269 (Some(TokenType::Cardinal(n)), false)
27270 } else if self.check_possessive_pronoun() {
27271 (None, true)
27274 } else if self.check_quantifier() {
27275 (Some(self.advance().kind.clone()), false)
27276 } else {
27277 let art = self.advance().kind.clone();
27278 if let TokenType::Article(def) = art {
27279 if def == Definiteness::Indefinite {
27280 (Some(TokenType::Some), false)
27281 } else {
27282 (None, true) }
27284 } else {
27285 (None, false)
27286 }
27287 };
27288
27289 self.nominal_np_context = true;
27292 let object_np_result = self.parse_noun_phrase(false);
27293 self.nominal_np_context = false;
27294 let object_np = object_np_result?;
27295
27296 if let Some(adj) = object_np.superlative {
27298 object_superlative = Some((adj, object_np.noun));
27299 }
27300
27301 if let TokenType::Adjective(a) = self.peek().kind {
27303 self.advance();
27304 secondary_adj = Some(a);
27305 }
27306
27307 if let TokenType::Particle(particle_sym) = self.peek().kind {
27309 let verb_str = self.interner.resolve(verb).to_lowercase();
27310 let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27311 if let Some((phrasal_lemma, _class)) =
27312 crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27313 {
27314 self.advance(); verb = self.interner.intern(phrasal_lemma);
27316 }
27317 }
27318
27319 if let Some(obj_q) = obj_quantifier {
27320 let verb_str = self.interner.resolve(verb).to_lowercase();
27324 let is_opaque = lexicon::lookup_verb_db(&verb_str)
27325 .map(|meta| meta.features.contains(&lexicon::Feature::Opaque))
27326 .unwrap_or(false);
27327
27328 if is_opaque && self.check_verb() {
27332 let emb_var = self.next_var_name();
27333 let emb_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
27334 name: object_np.noun,
27335 args: self.ctx.terms.alloc_slice([Term::Variable(emb_var)]),
27336 world: None,
27337 });
27338 let emb_vp = self.parse_predicate_with_subject_as_var(emb_var)?;
27339 let embedded =
27340 self.quantify_argument(&obj_q, emb_var, emb_restriction, emb_vp);
27341 let subject_term_for_event = self.coerce_agent(&subject);
27342 let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27343 name: verb,
27344 args: self
27345 .ctx
27346 .terms
27347 .alloc_slice([subject_term_for_event, Term::Proposition(embedded)]),
27348 world: None,
27349 });
27350 let effective_time = self.pending_time.take().unwrap_or(verb_time);
27351 let with_time = match effective_time {
27352 Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
27353 operator: TemporalOperator::Past,
27354 body: main_pred,
27355 }),
27356 _ => main_pred,
27357 };
27358 return self.wrap_with_definiteness_full(&subject, with_time);
27359 }
27360
27361 if is_opaque && matches!(obj_q, TokenType::Some) {
27362 let intension_term = Term::Intension(object_np.noun);
27364
27365 let event_var = self.get_event_var();
27367 let mut modifiers = self.collect_adverbs();
27368 let effective_time = self.pending_time.take().unwrap_or(verb_time);
27369 match effective_time {
27370 Time::Past => modifiers.push(self.interner.intern("Past")),
27371 Time::Future => modifiers.push(self.interner.intern("Future")),
27372 _ => {}
27373 }
27374
27375 let subject_term_for_event = self.coerce_agent(&subject);
27376 let roles = vec![
27377 (ThematicRole::Agent, subject_term_for_event),
27378 (ThematicRole::Theme, intension_term),
27379 ];
27380
27381 let suppress_existential = self.drs.in_conditional_antecedent();
27382 if suppress_existential {
27383 let event_class = self.interner.intern("Event");
27384 self.drs.introduce_event_referent(event_var, event_class);
27385 }
27386 let neo_event =
27387 self.ctx
27388 .exprs
27389 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27390 event_var,
27391 verb,
27392 roles: self.ctx.roles.alloc_slice(roles),
27393 modifiers: self.ctx.syms.alloc_slice(modifiers),
27394 suppress_existential,
27395 world: None,
27396 })));
27397
27398 return self.wrap_with_definiteness_full(&subject, neo_event);
27399 }
27400
27401 let obj_var = self.next_var_name();
27402
27403 let obj_gender = Self::infer_noun_gender(self.interner.resolve(object_np.noun));
27405 let obj_number = if Self::is_plural_noun(self.interner.resolve(object_np.noun))
27406 {
27407 Number::Plural
27408 } else {
27409 Number::Singular
27410 };
27411 if object_np.definiteness == Some(Definiteness::Definite) {
27413 self.drs.introduce_referent_with_source(
27414 obj_var,
27415 object_np.noun,
27416 obj_gender,
27417 obj_number,
27418 ReferentSource::MainClause,
27419 );
27420 } else {
27421 self.drs.introduce_referent(
27422 obj_var,
27423 object_np.noun,
27424 obj_gender,
27425 obj_number,
27426 );
27427 }
27428
27429 let mut type_pred: &'a LogicExpr<'a> =
27430 self.ctx.exprs.alloc(LogicExpr::Predicate {
27431 name: object_np.noun,
27432 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27433 world: None,
27434 });
27435
27436 if crate::lexicon::is_mass_noun(
27441 &self.interner.resolve(object_np.noun).to_lowercase(),
27442 ) {
27443 let portion_sym = self.interner.intern("Portion");
27444 let portion_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27445 name: portion_sym,
27446 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27447 world: None,
27448 });
27449 type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27450 left: portion_pred,
27451 op: TokenType::And,
27452 right: type_pred,
27453 });
27454 }
27455
27456 for &adj in object_np.adjectives {
27460 let adj_pred = self.adjective_restriction(adj, obj_var, object_np.noun);
27461 type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27462 left: type_pred,
27463 op: TokenType::And,
27464 right: adj_pred,
27465 });
27466 }
27467 for pp in object_np.pps {
27468 let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
27469 type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27470 left: type_pred,
27471 op: TokenType::And,
27472 right: pp_sub,
27473 });
27474 }
27475
27476 let obj_restriction =
27477 if self.check(&TokenType::That) || self.check(&TokenType::Who) {
27478 self.advance();
27479 let rel_clause = self.parse_relative_clause(obj_var)?;
27480 self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27481 left: type_pred,
27482 op: TokenType::And,
27483 right: rel_clause,
27484 })
27485 } else {
27486 type_pred
27487 };
27488
27489 let mut recipient: Option<Term<'a>> = None;
27493 let mut control_infinitive: Option<Symbol> = None;
27494 if self.check_to_marker() {
27495 let after_to = self.tokens.get(self.current + 1).map(|t| t.kind.clone());
27496 match after_to {
27497 Some(TokenType::Verb { lemma, .. }) => {
27498 self.advance(); self.advance(); control_infinitive = Some(lemma);
27501 }
27502 Some(TokenType::Noun(word))
27503 if crate::lexicon::lookup_verb_db(
27504 &self.interner.resolve(word).to_lowercase(),
27505 )
27506 .is_some() =>
27507 {
27508 let lemma_str = crate::lexicon::lookup_verb_db(
27509 &self.interner.resolve(word).to_lowercase(),
27510 )
27511 .map(|m| m.lemma)
27512 .unwrap();
27513 self.advance(); self.advance(); control_infinitive = Some(self.interner.intern(lemma_str));
27516 }
27517 Some(kind)
27518 if Lexer::is_ditransitive_verb(&self.interner.resolve(verb))
27519 && matches!(
27520 kind,
27521 TokenType::ProperName(_)
27522 | TokenType::Noun(_)
27523 | TokenType::Article(_)
27524 ) =>
27525 {
27526 self.advance(); let r_np = self.parse_noun_phrase(false)?;
27528 recipient = Some(Term::Constant(r_np.noun));
27529 }
27530 _ => {}
27531 }
27532 }
27533
27534 let event_var = self.get_event_var();
27535 let mut modifiers = self.collect_adverbs();
27536 let effective_time = self.pending_time.take().unwrap_or(verb_time);
27537 match effective_time {
27538 Time::Past => modifiers.push(self.interner.intern("Past")),
27539 Time::Future => modifiers.push(self.interner.intern("Future")),
27540 _ => {}
27541 }
27542
27543 let subject_term_for_event = self.coerce_agent(&subject);
27544 let mut roles = vec![
27545 (ThematicRole::Agent, subject_term_for_event),
27546 (ThematicRole::Theme, Term::Variable(obj_var)),
27547 ];
27548 if let Some(r) = recipient {
27549 roles.push((ThematicRole::Recipient, r));
27550 }
27551
27552 if let Some(adj) = secondary_adj {
27554 let sec_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27555 name: adj,
27556 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27557 world: None,
27558 });
27559 let role = secondary_role(&self.interner.resolve(verb).to_lowercase());
27560 roles.push((role, Term::Proposition(sec_pred)));
27561 }
27562
27563 let template_roles = vec![
27566 (ThematicRole::Agent, subject_term_for_event),
27567 (ThematicRole::Theme, Term::Constant(object_np.noun)),
27568 ];
27569 self.capture_event_template(verb, &template_roles, &modifiers);
27570
27571 let suppress_existential = self.drs.in_conditional_antecedent();
27572 if suppress_existential {
27573 let event_class = self.interner.intern("Event");
27574 self.drs.introduce_event_referent(event_var, event_class);
27575 }
27576 let neo_event =
27577 if let Some(inf) = control_infinitive {
27578 let inf_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27579 name: inf,
27580 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27581 world: None,
27582 });
27583 let control = self.ctx.exprs.alloc(LogicExpr::Control {
27584 verb,
27585 subject: self.ctx.terms.alloc(subject_term_for_event),
27586 object: Some(&*self.ctx.terms.alloc(Term::Variable(obj_var))),
27587 infinitive: inf_pred,
27588 });
27589 match effective_time {
27590 Time::Past => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
27591 operator: TemporalOperator::Past,
27592 body: control,
27593 }),
27594 Time::Future => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
27595 operator: TemporalOperator::Future,
27596 body: control,
27597 }),
27598 _ => control,
27599 }
27600 } else {
27601 let plain = &*self.ctx.exprs.alloc(LogicExpr::NeoEvent(Box::new(
27602 NeoEventData {
27603 event_var,
27604 verb,
27605 roles: self.ctx.roles.alloc_slice(roles),
27606 modifiers: self.ctx.syms.alloc_slice(modifiers),
27607 suppress_existential,
27608 world: None,
27609 },
27610 )));
27611 self.attach_trailing_event_pps(plain, event_var)?
27616 };
27617
27618 let obj_quantified =
27619 self.quantify_argument(&obj_q, obj_var, obj_restriction, neo_event);
27620
27621 let obj_quantified = {
27628 let subj = self.coerce_agent(&subject);
27629 let temporal = match self.parse_temporal_offset_constraint(subj)? {
27636 Some(off) => Some(off),
27637 None => self.parse_bare_temporal_constraint(subj)?,
27638 };
27639 match temporal {
27640 Some(t) => self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27641 left: obj_quantified,
27642 op: TokenType::And,
27643 right: t,
27644 }),
27645 None => obj_quantified,
27646 }
27647 };
27648 return self.wrap_with_definiteness_full(&subject, obj_quantified);
27650 } else {
27651 if was_definite_article {
27656 let obj_gender =
27657 Self::infer_noun_gender(self.interner.resolve(object_np.noun));
27658 let obj_number =
27659 if Self::is_plural_noun(self.interner.resolve(object_np.noun)) {
27660 Number::Plural
27661 } else {
27662 Number::Singular
27663 };
27664 self.drs.introduce_referent_with_source(
27666 object_np.noun,
27667 object_np.noun,
27668 obj_gender,
27669 obj_number,
27670 ReferentSource::MainClause,
27671 );
27672 }
27673
27674 let term = self.noun_phrase_to_term(&object_np);
27675 object_term = Some(term.clone());
27676 object_adjectives = object_np.adjectives;
27677 object_desc_pps = object_np.pps;
27678 args.push(term);
27679
27680 let verb_str = self.interner.resolve(verb);
27687 if Lexer::is_ditransitive_verb(verb_str)
27688 && (self.check_content_word() || self.check_article())
27689 {
27690 let second_np = self.parse_noun_phrase(false)?;
27691 let second_term = Term::Constant(second_np.noun);
27692 second_object_term = Some(second_term);
27693 args.push(second_term);
27694 }
27695 }
27696 } else if self.check_focus() {
27697 let focus_kind = if let TokenType::Focus(k) = self.advance().kind {
27698 k
27699 } else {
27700 FocusKind::Only
27701 };
27702
27703 let event_var = self.get_event_var();
27704 let mut modifiers = self.collect_adverbs();
27705 let effective_time = self.pending_time.take().unwrap_or(verb_time);
27706 match effective_time {
27707 Time::Past => modifiers.push(self.interner.intern("Past")),
27708 Time::Future => modifiers.push(self.interner.intern("Future")),
27709 _ => {}
27710 }
27711
27712 let subject_term_for_event = self.coerce_agent(&subject);
27713
27714 if self.check_preposition() {
27715 let prep_token = self.advance().clone();
27716 let prep_name = if let TokenType::Preposition(sym) = prep_token.kind {
27717 sym
27718 } else {
27719 self.interner.intern("to")
27720 };
27721 let pp_obj = self.parse_noun_phrase(false)?;
27722 let pp_obj_term = Term::Constant(pp_obj.noun);
27723
27724 let roles = vec![(ThematicRole::Agent, subject_term_for_event)];
27725 let suppress_existential = self.drs.in_conditional_antecedent();
27726 if suppress_existential {
27727 let event_class = self.interner.intern("Event");
27728 self.drs.introduce_event_referent(event_var, event_class);
27729 }
27730 let neo_event =
27731 self.ctx
27732 .exprs
27733 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27734 event_var,
27735 verb,
27736 roles: self.ctx.roles.alloc_slice(roles),
27737 modifiers: self.ctx.syms.alloc_slice(modifiers),
27738 suppress_existential,
27739 world: None,
27740 })));
27741
27742 let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27743 name: prep_name,
27744 args: self
27745 .ctx
27746 .terms
27747 .alloc_slice([Term::Variable(event_var), pp_obj_term]),
27748 world: None,
27749 });
27750
27751 let with_pp = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27752 left: neo_event,
27753 op: TokenType::And,
27754 right: pp_pred,
27755 });
27756
27757 let focused_ref = self.ctx.terms.alloc(pp_obj_term);
27758 return Ok(self.ctx.exprs.alloc(LogicExpr::Focus {
27759 kind: focus_kind,
27760 focused: focused_ref,
27761 scope: with_pp,
27762 }));
27763 }
27764
27765 let focused_np = self.parse_noun_phrase(false)?;
27766 let focused_term = self.noun_phrase_to_term(&focused_np);
27767 args.push(focused_term.clone());
27768
27769 let roles = vec![
27770 (ThematicRole::Agent, subject_term_for_event),
27771 (ThematicRole::Theme, focused_term.clone()),
27772 ];
27773
27774 let suppress_existential = self.drs.in_conditional_antecedent();
27775 if suppress_existential {
27776 let event_class = self.interner.intern("Event");
27777 self.drs.introduce_event_referent(event_var, event_class);
27778 }
27779 let neo_event = self
27780 .ctx
27781 .exprs
27782 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27783 event_var,
27784 verb,
27785 roles: self.ctx.roles.alloc_slice(roles),
27786 modifiers: self.ctx.syms.alloc_slice(modifiers),
27787 suppress_existential,
27788 world: None,
27789 })));
27790
27791 let focused_ref = self.ctx.terms.alloc(focused_term);
27792 return Ok(self.ctx.exprs.alloc(LogicExpr::Focus {
27793 kind: focus_kind,
27794 focused: focused_ref,
27795 scope: neo_event,
27796 }));
27797 } else if self.check_number() {
27798 let measure = self.parse_measure_phrase()?;
27800
27801 if self.check_content_word() && !self.check_article() {
27805 let noun_sym = self.consume_content_word()?;
27806 let count_term = *measure;
27808 object_term = Some(count_term.clone());
27809 args.push(count_term);
27810 second_object_term = Some(Term::Constant(noun_sym));
27811 args.push(Term::Constant(noun_sym));
27812 } else {
27813 object_term = Some(*measure);
27815 args.push(*measure);
27816 }
27817 } else if self.check_content_word() || self.check_article() {
27818 let object = self.parse_noun_phrase(false)?;
27819 if let Some(adj) = object.superlative {
27820 object_superlative = Some((adj, object.noun));
27821 }
27822
27823 if self.check(&TokenType::Respectively) {
27827 self.advance(); }
27829
27830 let term = self.noun_phrase_to_term(&object);
27831 object_term = Some(term.clone());
27832 args.push(term.clone());
27833
27834 if let TokenType::Particle(particle_sym) = self.peek().kind {
27836 let verb_str = self.interner.resolve(verb).to_lowercase();
27837 let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27838 if let Some((phrasal_lemma, _class)) =
27839 crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27840 {
27841 self.advance(); verb = self.interner.intern(phrasal_lemma);
27843 }
27844 }
27845
27846 if self.check_number() {
27848 let measure = self.parse_measure_phrase()?;
27849 second_object_term = Some(*measure);
27850 args.push(*measure);
27851 }
27852 else {
27854 let verb_str = self.interner.resolve(verb);
27855 if Lexer::is_ditransitive_verb(verb_str)
27856 && (self.check_content_word() || self.check_article())
27857 {
27858 let second_np = self.parse_noun_phrase(false)?;
27859 let second_term = self.noun_phrase_to_term(&second_np);
27860 second_object_term = Some(second_term.clone());
27861 args.push(second_term);
27862 }
27863 }
27864 }
27865
27866 let mut pp_predicates: Vec<&'a LogicExpr<'a>> = Vec::new();
27867
27868 if matches!(object_term, Some(Term::Value { .. })) {
27873 let rate = self.check_preposition_is("per")
27874 || (matches!(
27875 self.peek().kind,
27876 TokenType::Article(Definiteness::Indefinite)
27877 ) && matches!(
27878 self.tokens.get(self.current + 1).map(|t| &t.kind),
27879 Some(TokenType::CalendarUnit(_)) | Some(TokenType::Noun(_))
27880 ));
27881 if rate {
27882 self.advance(); let unit_lexeme = self.peek().lexeme;
27884 self.advance(); let unit_cap = {
27886 let s = self.interner.resolve(unit_lexeme).to_string();
27887 let mut c = s.chars();
27888 match c.next() {
27889 Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
27890 None => s,
27891 }
27892 };
27893 let event_sym = self.get_event_var();
27894 pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27895 name: self.interner.intern("Per"),
27896 args: self.ctx.terms.alloc_slice([
27897 Term::Variable(event_sym),
27898 Term::Constant(self.interner.intern(&unit_cap)),
27899 ]),
27900 world: None,
27901 }));
27902 }
27903 }
27904
27905 while self.check_preposition() || self.check_to() {
27906 if self.check_preposition_is("within")
27908 && self.current + 1 < self.tokens.len()
27909 && matches!(
27910 self.tokens[self.current + 1].kind,
27911 TokenType::Cardinal(_) | TokenType::Number(_)
27912 )
27913 {
27914 break;
27915 }
27916 let prep_token = self.advance().clone();
27917 let prep_name = if let TokenType::Preposition(sym) = prep_token.kind {
27918 sym
27919 } else if matches!(prep_token.kind, TokenType::To) {
27920 self.interner.intern("To")
27921 } else {
27922 continue;
27923 };
27924
27925 let pp_obj_term = if self.check(&TokenType::Reflexive) {
27926 self.advance();
27927 self.noun_phrase_to_term(&subject)
27928 } else if self.check_pronoun() {
27929 let token = self.advance().clone();
27930 if let TokenType::Pronoun { gender, number, .. } = token.kind {
27931 let resolved = self.resolve_pronoun(gender, number)?;
27932 match resolved {
27933 ResolvedPronoun::Variable(s) => Term::Variable(s),
27934 ResolvedPronoun::Constant(s) => Term::Constant(s),
27935 }
27936 } else {
27937 continue;
27938 }
27939 } else if self.check_content_word() || self.check_article() {
27940 let prep_obj = self.parse_noun_phrase(false)?;
27941 self.noun_phrase_to_term(&prep_obj)
27942 } else if self.check_number() {
27943 *self.parse_measure_phrase()?
27945 } else if self.at_clause_boundary()
27946 && crate::lexicon::is_particle(&self.interner.resolve(prep_name).to_lowercase())
27947 {
27948 let event_sym = self.get_event_var();
27952 pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27953 name: prep_name,
27954 args: self.ctx.terms.alloc_slice([Term::Variable(event_sym)]),
27955 world: None,
27956 }));
27957 continue;
27958 } else {
27959 self.current -= 1;
27963 break;
27964 };
27965
27966 if self.pp_attach_to_noun {
27967 if let Some(ref obj) = object_term {
27968 let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27970 name: prep_name,
27971 args: self.ctx.terms.alloc_slice([obj.clone(), pp_obj_term]),
27972 world: None,
27973 });
27974 pp_predicates.push(pp_pred);
27975 } else {
27976 args.push(pp_obj_term);
27977 }
27978 } else {
27979 let event_sym = self.get_event_var();
27981 let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27982 name: prep_name,
27983 args: self
27984 .ctx
27985 .terms
27986 .alloc_slice([Term::Variable(event_sym), pp_obj_term]),
27987 world: None,
27988 });
27989 pp_predicates.push(pp_pred);
27990 }
27991
27992 for adv in self.collect_adverbs() {
27996 let event_sym = self.get_event_var();
27997 pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27998 name: adv,
27999 args: self.ctx.terms.alloc_slice([Term::Variable(event_sym)]),
28000 world: None,
28001 }));
28002 }
28003 }
28004
28005 if self.check(&TokenType::That) || self.check(&TokenType::Who) {
28007 self.advance();
28008 let rel_var = self.next_var_name();
28009 let rel_pred = self.parse_relative_clause(rel_var)?;
28010 pp_predicates.push(rel_pred);
28011 }
28012
28013 {
28024 let subj = self.coerce_agent(&subject);
28025 let off = match self.parse_temporal_offset_constraint(subj)? {
28026 Some(o) => Some(o),
28027 None => self.parse_bare_temporal_constraint(subj)?,
28028 };
28029 if let Some(off) = off {
28030 pp_predicates.push(off);
28031 }
28032 }
28033
28034 let mut modifiers = self.collect_adverbs();
28036
28037 let effective_time = self.pending_time.take().unwrap_or(verb_time);
28039 match effective_time {
28040 Time::Past => modifiers.push(self.interner.intern("Past")),
28041 Time::Future => modifiers.push(self.interner.intern("Future")),
28042 _ => {}
28043 }
28044
28045 if verb_aspect == Aspect::Progressive {
28047 modifiers.push(self.interner.intern("Progressive"));
28048 } else if verb_aspect == Aspect::Perfect {
28049 modifiers.push(self.interner.intern("Perfect"));
28050 }
28051
28052 let mut roles: Vec<(ThematicRole, Term<'a>)> = Vec::new();
28054
28055 let verb_str_for_check = self.interner.resolve(verb).to_lowercase();
28057 let is_unaccusative = crate::lexicon::lookup_verb_db(&verb_str_for_check)
28058 .map(|meta| {
28059 meta.features
28060 .contains(&crate::lexicon::Feature::Unaccusative)
28061 })
28062 .unwrap_or(false);
28063
28064 let has_object = object_term.is_some() || second_object_term.is_some();
28066 let subject_role = if is_unaccusative && !has_object {
28067 ThematicRole::Theme
28068 } else {
28069 ThematicRole::Agent
28070 };
28071
28072 roles.push((subject_role, subject_term));
28073 if let Some(second_obj) = second_object_term {
28074 if let Some(first_obj) = object_term {
28076 roles.push((ThematicRole::Recipient, first_obj));
28077 }
28078 roles.push((ThematicRole::Theme, second_obj));
28079 } else if let Some(obj) = object_term {
28080 roles.push((ThematicRole::Theme, obj));
28082 }
28083
28084 if let (Some(adj), Some(obj)) = (secondary_adj, object_term) {
28087 let sec_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28088 name: adj,
28089 args: self.ctx.terms.alloc_slice([obj]),
28090 world: None,
28091 });
28092 let role = secondary_role(&self.interner.resolve(verb).to_lowercase());
28093 roles.push((role, Term::Proposition(sec_pred)));
28094 }
28095
28096 let event_var = self.get_event_var();
28098
28099 self.capture_event_template(verb, &roles, &modifiers);
28101
28102 let suppress_existential = self.drs.in_conditional_antecedent();
28104 if suppress_existential {
28105 let event_class = self.interner.intern("Event");
28106 self.drs.introduce_event_referent(event_var, event_class);
28107 }
28108 let with_pps = self.event_with_conditions(
28109 NeoEventData {
28110 event_var,
28111 verb,
28112 roles: self.ctx.roles.alloc_slice(roles),
28113 modifiers: self.ctx.syms.alloc_slice(modifiers),
28114 suppress_existential,
28115 world: None,
28116 },
28117 pp_predicates,
28118 );
28119
28120 let with_pps = if let Some(obj_term) = object_term {
28124 let placeholder = self.interner.intern("_PP_SELF_");
28125 let mut combined = with_pps;
28126 for &adj in object_adjectives {
28127 let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28128 name: adj,
28129 args: self.ctx.terms.alloc_slice([obj_term]),
28130 world: None,
28131 });
28132 combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28133 left: combined,
28134 op: TokenType::And,
28135 right: adj_pred,
28136 });
28137 }
28138 for pp in object_desc_pps {
28139 if let LogicExpr::Predicate { name, args, world } = pp {
28140 let new_args: Vec<Term<'a>> = args
28141 .iter()
28142 .map(|a| match a {
28143 Term::Variable(v) if *v == placeholder => obj_term,
28144 other => *other,
28145 })
28146 .collect();
28147 let sub = self.ctx.exprs.alloc(LogicExpr::Predicate {
28148 name: *name,
28149 args: self.ctx.terms.alloc_slice(new_args),
28150 world: *world,
28151 });
28152 combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28153 left: combined,
28154 op: TokenType::And,
28155 right: sub,
28156 });
28157 }
28158 }
28159 combined
28160 } else {
28161 with_pps
28162 };
28163
28164 let with_aspect = if verb_aspect == Aspect::Progressive {
28166 if verb_class == crate::lexicon::VerbClass::Semelfactive {
28168 self.ctx.exprs.alloc(LogicExpr::Aspectual {
28169 operator: AspectOperator::Iterative,
28170 body: with_pps,
28171 })
28172 } else {
28173 self.ctx.exprs.alloc(LogicExpr::Aspectual {
28175 operator: AspectOperator::Progressive,
28176 body: with_pps,
28177 })
28178 }
28179 } else if verb_aspect == Aspect::Perfect {
28180 self.ctx.exprs.alloc(LogicExpr::Aspectual {
28181 operator: AspectOperator::Perfect,
28182 body: with_pps,
28183 })
28184 } else if effective_time == Time::Present && verb_aspect == Aspect::Simple {
28185 if !verb_class.is_stative() {
28187 self.ctx.exprs.alloc(LogicExpr::Aspectual {
28188 operator: AspectOperator::Habitual,
28189 body: with_pps,
28190 })
28191 } else {
28192 with_pps
28194 }
28195 } else {
28196 with_pps
28197 };
28198
28199 let with_adverbs = with_aspect;
28200
28201 let with_temporal = if self.check_temporal_adverb() {
28203 let anchor = if let TokenType::TemporalAdverb(adv) = self.advance().kind.clone() {
28204 adv
28205 } else {
28206 panic!("Expected temporal adverb");
28207 };
28208 self.ctx.exprs.alloc(LogicExpr::TemporalAnchor {
28209 anchor,
28210 body: with_adverbs,
28211 })
28212 } else {
28213 with_adverbs
28214 };
28215
28216 let wrapped = self.wrap_with_definiteness_full(&subject, with_temporal)?;
28217
28218 if let Some((adj, noun)) = object_superlative {
28220 let superlative_expr = self.ctx.exprs.alloc(LogicExpr::Superlative {
28221 adjective: adj,
28222 subject: self.ctx.terms.alloc(Term::Constant(noun)),
28223 domain: noun,
28224 });
28225 return Ok(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28226 left: wrapped,
28227 op: TokenType::And,
28228 right: superlative_expr,
28229 }));
28230 }
28231
28232 return Ok(wrapped);
28233 }
28234
28235 Ok(self.ctx.exprs.alloc(LogicExpr::Atom(subject.noun)))
28236 }
28237
28238 fn check_preposition(&self) -> bool {
28239 matches!(self.peek().kind, TokenType::Preposition(_))
28240 }
28241
28242 fn check_by_preposition(&self) -> bool {
28243 if let TokenType::Preposition(p) = self.peek().kind {
28244 p.is(self.interner, "by")
28245 } else {
28246 false
28247 }
28248 }
28249
28250 fn check_preposition_is(&self, word: &str) -> bool {
28251 if let TokenType::Preposition(p) = self.peek().kind {
28252 p.is(self.interner, word)
28253 } else {
28254 false
28255 }
28256 }
28257
28258 fn consume_or_equal_to(&mut self) -> bool {
28264 if self.peek_word_at(0, "or") && self.peek_word_at(1, "equal") && self.peek_word_at(2, "to")
28265 {
28266 self.advance(); self.advance(); self.advance(); true
28270 } else {
28271 false
28272 }
28273 }
28274
28275 fn next_is_colon_like(&self) -> bool {
28279 self.tokens.get(self.current + 1).is_some_and(|t| {
28280 matches!(
28281 t.kind,
28282 TokenType::Colon | TokenType::Period | TokenType::Newline
28283 )
28284 })
28285 }
28286
28287 fn item_opens_an_index(&self) -> bool {
28291 let mut i = self.current + 1;
28295 let end = (self.current + 24).min(self.tokens.len());
28296 let mut depth = 0i32;
28297 while i < end {
28298 let tok = &self.tokens[i];
28299 match tok.kind {
28300 TokenType::LParen | TokenType::LBracket => depth += 1,
28301 TokenType::RParen | TokenType::RBracket => {
28302 if depth == 0 {
28305 return false;
28306 }
28307 depth -= 1;
28308 }
28309 TokenType::Period | TokenType::Newline if depth == 0 => return false,
28312 TokenType::Comma | TokenType::Colon if depth == 0 => return false,
28313 _ if depth == 0 && self.interner.resolve(tok.lexeme).eq_ignore_ascii_case("of") => {
28314 return true
28315 }
28316 _ => {}
28317 }
28318 i += 1;
28319 }
28320 false
28321 }
28322
28323 fn next_is_section_header(&self) -> bool {
28325 component::SECTION_WORDS
28326 .iter()
28327 .any(|w| self.lexeme_is(0, w))
28328 && self
28329 .tokens
28330 .get(self.current + 1)
28331 .is_some_and(|t| matches!(t.kind, TokenType::Colon))
28332 }
28333
28334 fn check_word(&self, word: &str) -> bool {
28335 let token = self.peek();
28336 if matches!(
28340 token.kind,
28341 TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
28342 ) {
28343 return false;
28344 }
28345 let lexeme = self.interner.resolve(token.lexeme);
28346 lexeme.eq_ignore_ascii_case(word)
28347 }
28348
28349 fn check_op_word(&self, word: &str) -> bool {
28354 self.check_word(word) && !self.user_bound.contains(&self.peek().lexeme)
28355 }
28356
28357 fn peek_word_at(&self, offset: usize, word: &str) -> bool {
28358 if self.current + offset >= self.tokens.len() {
28359 return false;
28360 }
28361 let token = &self.tokens[self.current + offset];
28362 if matches!(
28363 token.kind,
28364 TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
28365 ) {
28366 return false;
28367 }
28368 let lexeme = self.interner.resolve(token.lexeme);
28369 lexeme.eq_ignore_ascii_case(word)
28370 }
28371
28372 fn check_to_preposition(&self) -> bool {
28373 match self.peek().kind {
28374 TokenType::To => true,
28375 TokenType::Preposition(p) => p.is(self.interner, "to"),
28376 _ => false,
28377 }
28378 }
28379
28380 fn check_content_word(&self) -> bool {
28381 match &self.peek().kind {
28382 TokenType::Noun(_)
28383 | TokenType::Adjective(_)
28384 | TokenType::NonIntersectiveAdjective(_)
28385 | TokenType::Verb { .. }
28386 | TokenType::ProperName(_)
28387 | TokenType::Article(_)
28388 | TokenType::StringLiteral(_)
28390 | TokenType::May
28393 | TokenType::Number(_)
28396 | TokenType::Performative(_) => true,
28397 TokenType::Item | TokenType::Items if self.mode == ParserMode::Declarative => true,
28403 TokenType::CalendarUnit(_) if self.mode == ParserMode::Declarative => true,
28407 TokenType::Ambiguous { primary, alternatives } => {
28408 Self::is_content_word_type(primary)
28409 || alternatives.iter().any(Self::is_content_word_type)
28410 }
28411 _ => false,
28412 }
28413 }
28414
28415 fn is_content_word_type(t: &TokenType) -> bool {
28416 matches!(
28417 t,
28418 TokenType::Noun(_)
28419 | TokenType::Adjective(_)
28420 | TokenType::NonIntersectiveAdjective(_)
28421 | TokenType::Verb { .. }
28422 | TokenType::ProperName(_)
28423 | TokenType::Article(_)
28424 | TokenType::StringLiteral(_)
28425 | TokenType::Performative(_)
28426 )
28427 }
28428
28429 pub(super) fn try_wrap_bounded_delay(&mut self, expr: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
28431 if !self.check_preposition_is("within") {
28432 return expr;
28433 }
28434 let has_number = if self.current + 1 < self.tokens.len() {
28435 matches!(
28436 self.tokens[self.current + 1].kind,
28437 TokenType::Cardinal(_) | TokenType::Number(_)
28438 )
28439 } else {
28440 false
28441 };
28442 if !has_number {
28443 return expr;
28444 }
28445 self.advance(); let bound = match self.advance().kind {
28447 TokenType::Cardinal(n) => n,
28448 TokenType::Number(sym) => {
28449 let n_str = self.interner.resolve(sym);
28450 n_str.parse::<u32>().unwrap_or(1)
28451 }
28452 _ => 1,
28453 };
28454 if self.check_content_word() {
28456 let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
28457 if word == "cycle" || word == "cycles" {
28458 self.advance();
28459 }
28460 }
28461 self.ctx.exprs.alloc(LogicExpr::Temporal {
28462 operator: TemporalOperator::BoundedEventually(bound),
28463 body: expr,
28464 })
28465 }
28466
28467 pub(super) fn pp_is_cycle_temporal(&self) -> bool {
28473 let prep = match &self.peek().kind {
28474 TokenType::Preposition(s) => self.interner.resolve(*s).to_lowercase(),
28475 _ => return false,
28476 };
28477 if !matches!(prep.as_str(), "in" | "within" | "after") {
28478 return false;
28479 }
28480 let mut i = self.current + 1;
28481 let end = (self.current + 5).min(self.tokens.len());
28482 while i < end {
28483 match &self.tokens[i].kind {
28484 TokenType::Period | TokenType::Comma | TokenType::EOF => return false,
28485 _ => {
28486 let w = self.interner.resolve(self.tokens[i].lexeme).to_lowercase();
28487 if matches!(w.as_str(), "cycle" | "cycles" | "tick" | "ticks") {
28488 return true;
28489 }
28490 }
28491 }
28492 i += 1;
28493 }
28494 false
28495 }
28496
28497 pub(super) fn try_wrap_next_cycle(&mut self, expr: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
28500 let bare_next_cycle = self
28502 .interner
28503 .resolve(self.peek().lexeme)
28504 .eq_ignore_ascii_case("next")
28505 && self
28506 .tokens
28507 .get(self.current + 1)
28508 .map(|t| {
28509 self.interner
28510 .resolve(t.lexeme)
28511 .eq_ignore_ascii_case("cycle")
28512 })
28513 .unwrap_or(false);
28514 if bare_next_cycle {
28515 self.advance(); self.advance(); return self.ctx.exprs.alloc(LogicExpr::Temporal {
28518 operator: TemporalOperator::Next,
28519 body: expr,
28520 });
28521 }
28522 if !self.check_preposition_is("in") {
28523 return expr;
28524 }
28525 let mut i = self.current + 1;
28527 if matches!(
28528 self.tokens.get(i).map(|t| &t.kind),
28529 Some(TokenType::Article(_))
28530 ) {
28531 i += 1;
28532 }
28533 let next_matches = self
28534 .tokens
28535 .get(i)
28536 .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("next"))
28537 .unwrap_or(false);
28538 let cycle_matches = self
28539 .tokens
28540 .get(i + 1)
28541 .map(|t| {
28542 self.interner
28543 .resolve(t.lexeme)
28544 .eq_ignore_ascii_case("cycle")
28545 })
28546 .unwrap_or(false);
28547 if !next_matches || !cycle_matches {
28548 return expr;
28549 }
28550 while self.current <= i + 1 {
28551 self.advance();
28552 }
28553 self.ctx.exprs.alloc(LogicExpr::Temporal {
28554 operator: TemporalOperator::Next,
28555 body: expr,
28556 })
28557 }
28558
28559 fn check_verb(&self) -> bool {
28560 self.kind_is_verb(&self.peek().kind)
28561 }
28562
28563 fn kind_is_verb(&self, kind: &TokenType) -> bool {
28568 match kind {
28569 TokenType::Verb { .. } => true,
28570 TokenType::Ambiguous {
28571 primary,
28572 alternatives,
28573 } => {
28574 if self.noun_priority_mode {
28575 return false;
28576 }
28577 matches!(**primary, TokenType::Verb { .. })
28578 || alternatives
28579 .iter()
28580 .any(|t| matches!(t, TokenType::Verb { .. }))
28581 }
28582 _ => false,
28583 }
28584 }
28585
28586 fn check_adverb(&self) -> bool {
28587 matches!(self.peek().kind, TokenType::Adverb(_))
28588 }
28589
28590 fn check_performative(&self) -> bool {
28591 matches!(self.peek().kind, TokenType::Performative(_))
28592 }
28593
28594 fn try_performative_object(&mut self) -> Option<Term<'a>> {
28599 match self.peek().kind {
28600 TokenType::Pronoun { .. } => {
28601 let text = self.interner.resolve(self.peek().lexeme).to_lowercase();
28602 let sym = match text.as_str() {
28603 "you" => self.interner.intern("Addressee"),
28604 "me" | "myself" => self.interner.intern("Speaker"),
28605 other => {
28606 let cap = other
28607 .chars()
28608 .next()
28609 .map(|c| c.to_uppercase().collect::<String>() + &other[1..])
28610 .unwrap_or_default();
28611 self.interner.intern(&cap)
28612 }
28613 };
28614 self.advance();
28615 Some(Term::Constant(sym))
28616 }
28617 TokenType::ProperName(s) => {
28618 self.advance();
28619 Some(Term::Constant(s))
28620 }
28621 _ => None,
28622 }
28623 }
28624
28625 fn performative_predicate(&mut self, verb: Symbol, subject: Symbol) -> &'a LogicExpr<'a> {
28630 if let Some(obj) = self.try_performative_object() {
28631 self.ctx.exprs.alloc(LogicExpr::Predicate {
28632 name: verb,
28633 args: self.ctx.terms.alloc_slice([Term::Constant(subject), obj]),
28634 world: None,
28635 })
28636 } else {
28637 self.ctx.exprs.alloc(LogicExpr::Predicate {
28638 name: verb,
28639 args: self.ctx.terms.alloc_slice([Term::Constant(subject)]),
28640 world: None,
28641 })
28642 }
28643 }
28644
28645 fn collect_adverbs(&mut self) -> Vec<Symbol> {
28646 let mut adverbs = Vec::new();
28647 loop {
28648 if self.check_adverb() {
28649 if let TokenType::Adverb(adv) = self.advance().kind.clone() {
28650 adverbs.push(adv);
28651 }
28652 if self.check(&TokenType::And) {
28654 self.advance();
28655 }
28656 continue;
28657 }
28658 if let TokenType::Particle(p) = self.peek().kind {
28663 if matches!(
28664 self.tokens.get(self.current + 1).map(|t| &t.kind),
28665 Some(TokenType::Period) | Some(TokenType::EOF) | None
28666 ) {
28667 self.advance();
28668 let raw = self.interner.resolve(p).to_string();
28669 let mut capitalized = raw;
28670 if let Some(first) = capitalized.get_mut(0..1) {
28671 first.make_ascii_uppercase();
28672 }
28673 adverbs.push(self.interner.intern(&capitalized));
28674 continue;
28675 }
28676 }
28677 break;
28678 }
28679 adverbs
28680 }
28681
28682 fn check_auxiliary(&self) -> bool {
28683 matches!(self.peek().kind, TokenType::Auxiliary(_))
28684 }
28685
28686 fn is_true_auxiliary_usage(&self) -> bool {
28693 if self.current + 1 >= self.tokens.len() {
28694 return false;
28695 }
28696
28697 let next_token = &self.tokens[self.current + 1].kind;
28698
28699 if matches!(next_token, TokenType::Not) {
28701 return true;
28702 }
28703
28704 if matches!(next_token, TokenType::Verb { .. }) {
28706 return true;
28707 }
28708
28709 if matches!(
28711 next_token,
28712 TokenType::Pronoun { .. }
28713 | TokenType::Article(_)
28714 | TokenType::Noun(_)
28715 | TokenType::ProperName(_)
28716 ) {
28717 return false;
28718 }
28719
28720 true
28722 }
28723
28724 fn check_auxiliary_as_main_verb(&self) -> bool {
28727 if let TokenType::Auxiliary(Time::Past) = self.peek().kind {
28728 if self.current + 1 < self.tokens.len() {
28730 let next = &self.tokens[self.current + 1].kind;
28731 matches!(
28732 next,
28733 TokenType::Pronoun { .. }
28734 | TokenType::Article(_)
28735 | TokenType::Noun(_)
28736 | TokenType::ProperName(_)
28737 | TokenType::Number(_)
28740 )
28741 } else {
28742 false
28743 }
28744 } else {
28745 false
28746 }
28747 }
28748
28749 fn parse_do_as_main_verb(&mut self, subject_term: Term<'a>) -> ParseResult<&'a LogicExpr<'a>> {
28752 let aux_token = self.advance();
28754 let verb_time = if let TokenType::Auxiliary(time) = aux_token.kind {
28755 time
28756 } else {
28757 Time::Past
28758 };
28759
28760 let verb = self.interner.intern("Do");
28762
28763 let mut object_cardinal: Option<(u32, crate::intern::Symbol, &'a LogicExpr<'a>)> = None;
28764
28765 let object_term = if let TokenType::Pronoun { .. } = self.peek().kind {
28767 self.advance();
28769 let it_sym = self.interner.intern("it");
28772 Term::Constant(it_sym)
28773 } else if let Some(n) = self.counting_np_lookahead() {
28774 self.advance(); let obj_var = self.next_var_name();
28778 self.nominal_np_context = true;
28779 let obj_np_result = self.parse_noun_phrase(false);
28780 self.nominal_np_context = false;
28781 let obj_np = obj_np_result?;
28782 let mut restr: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
28783 name: obj_np.noun,
28784 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
28785 world: None,
28786 });
28787 for &adj in obj_np.adjectives {
28788 let adj_pred = self.adjective_restriction(adj, obj_var, obj_np.noun);
28789 restr = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28790 left: restr,
28791 op: TokenType::And,
28792 right: adj_pred,
28793 });
28794 }
28795 for pp in obj_np.pps {
28796 let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
28797 restr = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28798 left: restr,
28799 op: TokenType::And,
28800 right: pp_sub,
28801 });
28802 }
28803 object_cardinal = Some((n, obj_var, restr));
28804 Term::Variable(obj_var)
28805 } else if self.check_number() {
28806 *self.parse_measure_phrase()?
28809 } else {
28810 let object = self.parse_noun_phrase(false)?;
28811 self.noun_phrase_to_term(&object)
28812 };
28813
28814 let event_var = self.get_event_var();
28816 let suppress_existential = self.drs.in_conditional_antecedent();
28817
28818 let mut modifiers = Vec::new();
28819 if verb_time == Time::Past {
28820 modifiers.push(self.interner.intern("Past"));
28821 } else if verb_time == Time::Future {
28822 modifiers.push(self.interner.intern("Future"));
28823 }
28824
28825 let neo_event = self
28826 .ctx
28827 .exprs
28828 .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
28829 event_var,
28830 verb,
28831 roles: self.ctx.roles.alloc_slice(vec![
28832 (ThematicRole::Agent, subject_term),
28833 (ThematicRole::Theme, object_term),
28834 ]),
28835 modifiers: self.ctx.syms.alloc_slice(modifiers),
28836 suppress_existential,
28837 world: None,
28838 })));
28839
28840 if let Some((n, obj_var, restr)) = object_cardinal {
28843 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28844 left: restr,
28845 op: TokenType::And,
28846 right: neo_event,
28847 });
28848 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
28849 kind: QuantifierKind::Cardinal(n),
28850 variable: obj_var,
28851 body,
28852 island_id: self.current_island,
28853 }));
28854 }
28855
28856 Ok(neo_event)
28857 }
28858
28859 fn check_to(&self) -> bool {
28860 matches!(self.peek().kind, TokenType::To)
28861 }
28862
28863 pub(super) fn check_to_marker(&self) -> bool {
28867 self.check_to() || self.check_preposition_is("to")
28868 }
28869
28870 fn has_modal_subordination_ahead(&self) -> bool {
28874 for i in self.current..self.tokens.len() {
28877 match &self.tokens[i].kind {
28878 TokenType::Would | TokenType::Could | TokenType::Should | TokenType::Might => {
28879 return true;
28880 }
28881 TokenType::Period | TokenType::EOF => break,
28883 _ => {}
28884 }
28885 }
28886 false
28887 }
28888
28889 fn consume_verb(&mut self) -> Symbol {
28890 let t = self.advance().clone();
28891 match t.kind {
28892 TokenType::Verb { lemma, .. } => lemma,
28893 TokenType::Ambiguous { primary, .. } => match *primary {
28894 TokenType::Verb { lemma, .. } => lemma,
28895 _ => panic!("Expected verb in Ambiguous primary, got {:?}", primary),
28896 },
28897 _ => panic!("Expected verb, got {:?}", t.kind),
28898 }
28899 }
28900
28901 fn consume_verb_with_metadata(&mut self) -> (Symbol, Time, Aspect, VerbClass) {
28902 let t = self.advance().clone();
28903 match t.kind {
28904 TokenType::Verb {
28905 lemma,
28906 time,
28907 aspect,
28908 class,
28909 } => (lemma, time, aspect, class),
28910 TokenType::Ambiguous { primary, .. } => match *primary {
28911 TokenType::Verb {
28912 lemma,
28913 time,
28914 aspect,
28915 class,
28916 } => (lemma, time, aspect, class),
28917 _ => panic!("Expected verb in Ambiguous primary, got {:?}", primary),
28918 },
28919 _ => panic!("Expected verb, got {:?}", t.kind),
28920 }
28921 }
28922
28923 fn match_token(&mut self, types: &[TokenType]) -> bool {
28924 for t in types {
28925 if self.check(t) {
28926 self.advance();
28927 return true;
28928 }
28929 }
28930 false
28931 }
28932
28933 fn check_quantifier(&self) -> bool {
28934 matches!(self.peek().kind, TokenType::Any)
28935 || QuantifierKind::of_determiner(&self.peek().kind).is_some()
28936 }
28937
28938 fn check_npi_quantifier(&self) -> bool {
28939 matches!(
28940 self.peek().kind,
28941 TokenType::Nobody | TokenType::Nothing | TokenType::NoOne
28942 )
28943 }
28944
28945 fn check_npi_object(&self) -> bool {
28946 matches!(self.peek().kind, TokenType::Anything | TokenType::Anyone)
28947 }
28948
28949 fn check_temporal_npi(&self) -> bool {
28950 matches!(self.peek().kind, TokenType::Ever | TokenType::Never)
28951 }
28952
28953 fn parse_npi_quantified(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
28954 let npi_token = self.advance().kind.clone();
28955 let var_name = self.next_var_name();
28956
28957 let (restriction_name, is_person) = match npi_token {
28958 TokenType::Nobody | TokenType::NoOne => ("Person", true),
28959 TokenType::Nothing => ("Thing", false),
28960 _ => ("Thing", false),
28961 };
28962
28963 let restriction_sym = self.interner.intern(restriction_name);
28964 let subject_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28965 name: restriction_sym,
28966 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
28967 world: None,
28968 });
28969
28970 self.negative_depth += 1;
28971
28972 let verb = self.consume_verb();
28973
28974 if self.check_npi_object() {
28975 let obj_npi_token = self.advance().kind.clone();
28976 let obj_var = self.next_var_name();
28977
28978 let obj_restriction_name = match obj_npi_token {
28979 TokenType::Anything => "Thing",
28980 TokenType::Anyone => "Person",
28981 _ => "Thing",
28982 };
28983
28984 let obj_restriction_sym = self.interner.intern(obj_restriction_name);
28985 let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
28986 name: obj_restriction_sym,
28987 args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
28988 world: None,
28989 });
28990
28991 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28992 name: verb,
28993 args: self
28994 .ctx
28995 .terms
28996 .alloc_slice([Term::Variable(var_name), Term::Variable(obj_var)]),
28997 world: None,
28998 });
28999
29000 let verb_and_obj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29001 left: obj_restriction,
29002 op: TokenType::And,
29003 right: verb_pred,
29004 });
29005
29006 let inner_existential = self.ctx.exprs.alloc(LogicExpr::Quantifier {
29007 kind: crate::ast::QuantifierKind::Existential,
29008 variable: obj_var,
29009 body: verb_and_obj,
29010 island_id: self.current_island,
29011 });
29012
29013 self.negative_depth -= 1;
29014
29015 let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29016 op: TokenType::Not,
29017 operand: inner_existential,
29018 });
29019
29020 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29021 left: subject_pred,
29022 op: TokenType::Implies,
29023 right: negated,
29024 });
29025
29026 return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
29027 kind: crate::ast::QuantifierKind::Universal,
29028 variable: var_name,
29029 body,
29030 island_id: self.current_island,
29031 }));
29032 }
29033
29034 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
29035 name: verb,
29036 args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
29037 world: None,
29038 });
29039
29040 self.negative_depth -= 1;
29041
29042 let negated_verb = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29043 op: TokenType::Not,
29044 operand: verb_pred,
29045 });
29046
29047 let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29048 left: subject_pred,
29049 op: TokenType::Implies,
29050 right: negated_verb,
29051 });
29052
29053 Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
29054 kind: crate::ast::QuantifierKind::Universal,
29055 variable: var_name,
29056 body,
29057 island_id: self.current_island,
29058 }))
29059 }
29060
29061 fn parse_temporal_npi(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
29062 let npi_token = self.advance().kind.clone();
29063 let is_never = matches!(npi_token, TokenType::Never);
29064
29065 let subject = self.parse_noun_phrase(true)?;
29066
29067 if is_never {
29068 self.negative_depth += 1;
29069 }
29070
29071 let verb = self.consume_verb();
29072 let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
29073 name: verb,
29074 args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
29075 world: None,
29076 });
29077
29078 if is_never {
29079 self.negative_depth -= 1;
29080 Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29081 op: TokenType::Not,
29082 operand: verb_pred,
29083 }))
29084 } else {
29085 Ok(verb_pred)
29086 }
29087 }
29088
29089 fn check(&self, kind: &TokenType) -> bool {
29090 if self.is_at_end() {
29091 return false;
29092 }
29093 std::mem::discriminant(&self.peek().kind) == std::mem::discriminant(kind)
29094 }
29095
29096 fn check_any(&self, kinds: &[TokenType]) -> bool {
29097 if self.is_at_end() {
29098 return false;
29099 }
29100 let current = std::mem::discriminant(&self.peek().kind);
29101 kinds.iter().any(|k| std::mem::discriminant(k) == current)
29102 }
29103
29104 fn check_article(&self) -> bool {
29105 matches!(self.peek().kind, TokenType::Article(_))
29106 }
29107
29108 fn advance(&mut self) -> &Token {
29109 if !self.is_at_end() {
29110 self.current += 1;
29111 }
29112 self.previous()
29113 }
29114
29115 fn is_at_end(&self) -> bool {
29116 self.peek().kind == TokenType::EOF
29117 }
29118
29119 fn peek(&self) -> &Token {
29120 self.tokens
29123 .get(self.current)
29124 .unwrap_or_else(|| self.tokens.last().expect("stream ends with EOF"))
29125 }
29126
29127 fn peek_next_is_string_literal(&self) -> bool {
29130 self.tokens
29131 .get(self.current + 1)
29132 .map(|t| matches!(t.kind, TokenType::StringLiteral(_)))
29133 .unwrap_or(false)
29134 }
29135
29136 fn previous(&self) -> &Token {
29137 let idx = self.current.saturating_sub(1);
29143 self.tokens
29144 .get(idx)
29145 .unwrap_or_else(|| self.tokens.last().expect("stream ends with EOF"))
29146 }
29147
29148 fn current_span(&self) -> crate::token::Span {
29149 self.peek().span
29150 }
29151
29152 fn consume(&mut self, kind: TokenType) -> ParseResult<&Token> {
29153 if self.check(&kind) {
29154 Ok(self.advance())
29155 } else {
29156 Err(ParseError {
29157 kind: ParseErrorKind::UnexpectedToken {
29158 expected: kind,
29159 found: self.peek().kind.clone(),
29160 },
29161 span: self.current_span(),
29162 })
29163 }
29164 }
29165
29166 pub(super) fn absorb_multiword_proper_name(&mut self, first: Symbol) -> Symbol {
29173 let mut full = self.interner.resolve(first).to_string();
29174 loop {
29175 let next = self.peek();
29176 let is_content = matches!(
29177 next.kind,
29178 TokenType::ProperName(_)
29179 | TokenType::Noun(_)
29180 | TokenType::Adjective(_)
29181 | TokenType::Verb { .. }
29182 | TokenType::Ambiguous { .. }
29183 );
29184 let capitalized = self
29185 .interner
29186 .resolve(next.lexeme)
29187 .chars()
29188 .next()
29189 .map_or(false, |c| c.is_ascii_uppercase());
29190 if is_content && capitalized {
29191 full.push('_');
29192 full.push_str(&self.interner.resolve(self.peek().lexeme));
29193 self.advance();
29194 } else {
29195 break;
29196 }
29197 }
29198 self.interner.intern(&full)
29199 }
29200
29201 fn consume_content_word(&mut self) -> ParseResult<Symbol> {
29202 let t = self.advance().clone();
29203 match t.kind {
29204 TokenType::Noun(s)
29205 | TokenType::Adjective(s)
29206 | TokenType::NonIntersectiveAdjective(s) => Ok(s),
29207 TokenType::Item | TokenType::Items if self.mode == ParserMode::Declarative => {
29210 let lex = self.interner.resolve(t.lexeme);
29211 let mut chars = lex.chars();
29212 let cap = match chars.next() {
29213 Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
29214 None => String::new(),
29215 };
29216 Ok(self.interner.intern(&cap))
29217 }
29218 TokenType::StringLiteral(s) => {
29220 let s_str = self.interner.resolve(s);
29221 let gender = Self::infer_gender(s_str);
29222 self.drs.introduce_proper_name(s, s, gender);
29223 Ok(s)
29224 }
29225 TokenType::Article(_) => Ok(t.lexeme),
29227 TokenType::May => Ok(self.interner.intern("May")),
29229 TokenType::Number(s) => Ok(s),
29231 TokenType::CalendarUnit(u) => {
29237 let lemma = match u {
29238 crate::token::CalendarUnit::Second => "Second",
29239 crate::token::CalendarUnit::Minute => "Minute",
29240 crate::token::CalendarUnit::Hour => "Hour",
29241 crate::token::CalendarUnit::Day => "Day",
29242 crate::token::CalendarUnit::Week => "Week",
29243 crate::token::CalendarUnit::Month => "Month",
29244 crate::token::CalendarUnit::Year => "Year",
29245 crate::token::CalendarUnit::Millisecond => "Millisecond",
29246 crate::token::CalendarUnit::Microsecond => "Microsecond",
29247 crate::token::CalendarUnit::Nanosecond => "Nanosecond",
29248 };
29249 Ok(self.interner.intern(lemma))
29250 }
29251 TokenType::ProperName(s) => {
29252 if self.mode == ParserMode::Imperative {
29254 if !self.drs.has_referent_by_variable(s) {
29255 return Err(ParseError {
29256 kind: ParseErrorKind::UndefinedVariable {
29257 name: self.interner.resolve(s).to_string(),
29258 },
29259 span: t.span,
29260 });
29261 }
29262 return Ok(s);
29263 }
29264
29265 let s_str = self.interner.resolve(s);
29267 let gender = Self::infer_gender(s_str);
29268
29269 self.drs.introduce_proper_name(s, s, gender);
29271
29272 Ok(s)
29273 }
29274 TokenType::Verb { lemma, .. } => Ok(lemma),
29275 TokenType::Performative(s) => Ok(s),
29276 TokenType::Ambiguous { primary, .. } => {
29277 match *primary {
29278 TokenType::Noun(s)
29279 | TokenType::Adjective(s)
29280 | TokenType::NonIntersectiveAdjective(s) => Ok(s),
29281 TokenType::Verb { lemma, .. } => Ok(lemma),
29282 TokenType::Performative(s) => Ok(s),
29283 TokenType::ProperName(s) => {
29284 if self.mode == ParserMode::Imperative {
29286 if !self.drs.has_referent_by_variable(s) {
29287 return Err(ParseError {
29288 kind: ParseErrorKind::UndefinedVariable {
29289 name: self.interner.resolve(s).to_string(),
29290 },
29291 span: t.span,
29292 });
29293 }
29294 return Ok(s);
29295 }
29296 let s_str = self.interner.resolve(s);
29298 let gender = Self::infer_gender(s_str);
29299 self.drs.introduce_proper_name(s, s, gender);
29300 Ok(s)
29301 }
29302 _ => Err(ParseError {
29303 kind: ParseErrorKind::ExpectedContentWord { found: *primary },
29304 span: self.current_span(),
29305 }),
29306 }
29307 }
29308 other => Err(ParseError {
29309 kind: ParseErrorKind::ExpectedContentWord { found: other },
29310 span: self.current_span(),
29311 }),
29312 }
29313 }
29314
29315 fn consume_copula(&mut self) -> ParseResult<()> {
29316 if self.match_token(&[
29317 TokenType::Is,
29318 TokenType::Are,
29319 TokenType::Was,
29320 TokenType::Were,
29321 ]) {
29322 Ok(())
29323 } else {
29324 Err(ParseError {
29325 kind: ParseErrorKind::ExpectedCopula,
29326 span: self.current_span(),
29327 })
29328 }
29329 }
29330
29331 fn check_comparative(&self) -> bool {
29332 matches!(self.peek().kind, TokenType::Comparative(_))
29333 }
29334
29335 fn is_contact_clause_pattern(&self) -> bool {
29336 let mut pos = self.current;
29339
29340 if pos < self.tokens.len() && matches!(self.tokens[pos].kind, TokenType::Article(_)) {
29342 pos += 1;
29343 } else {
29344 return false;
29345 }
29346
29347 while pos < self.tokens.len() && matches!(self.tokens[pos].kind, TokenType::Adjective(_)) {
29349 pos += 1;
29350 }
29351
29352 if pos < self.tokens.len()
29354 && matches!(
29355 self.tokens[pos].kind,
29356 TokenType::Noun(_) | TokenType::ProperName(_) | TokenType::Adjective(_)
29357 )
29358 {
29359 pos += 1;
29360 } else {
29361 return false;
29362 }
29363
29364 pos < self.tokens.len()
29366 && matches!(
29367 self.tokens[pos].kind,
29368 TokenType::Verb { .. } | TokenType::Article(_)
29369 )
29370 }
29371
29372 fn check_superlative(&self) -> bool {
29373 matches!(self.peek().kind, TokenType::Superlative(_))
29374 }
29375
29376 fn check_scopal_adverb(&self) -> bool {
29377 matches!(self.peek().kind, TokenType::ScopalAdverb(_))
29378 }
29379
29380 fn check_temporal_adverb(&self) -> bool {
29381 matches!(self.peek().kind, TokenType::TemporalAdverb(_))
29382 }
29383
29384 fn check_non_intersective_adjective(&self) -> bool {
29385 matches!(self.peek().kind, TokenType::NonIntersectiveAdjective(_))
29386 }
29387
29388 fn check_focus(&self) -> bool {
29389 matches!(self.peek().kind, TokenType::Focus(_))
29390 }
29391
29392 fn check_measure(&self) -> bool {
29393 matches!(self.peek().kind, TokenType::Measure(_))
29394 }
29395
29396 fn check_presup_trigger(&self) -> bool {
29397 match &self.peek().kind {
29398 TokenType::PresupTrigger(_) => true,
29399 TokenType::Verb { lemma, .. } => {
29400 let s = self.interner.resolve(*lemma).to_lowercase();
29401 crate::lexicon::lookup_presup_trigger(&s).is_some()
29402 }
29403 TokenType::Ambiguous { primary, .. } => match primary.as_ref() {
29404 TokenType::Verb { lemma, .. } => {
29405 let s = self.interner.resolve(*lemma).to_lowercase();
29406 crate::lexicon::lookup_presup_trigger(&s).is_some()
29407 }
29408 _ => false,
29409 },
29410 _ => false,
29411 }
29412 }
29413
29414 fn consume_presup_trigger(&mut self) -> crate::token::PresupKind {
29415 let lemma = match self.advance().kind.clone() {
29416 TokenType::PresupTrigger(kind) => return kind,
29417 TokenType::Verb { lemma, .. } => lemma,
29418 TokenType::Ambiguous { primary, .. } => match *primary {
29419 TokenType::Verb { lemma, .. } => lemma,
29420 other => unreachable!("guarded by check_presup_trigger, got {other:?}"),
29421 },
29422 other => unreachable!("guarded by check_presup_trigger, got {other:?}"),
29423 };
29424 let s = self.interner.resolve(lemma).to_lowercase();
29425 crate::lexicon::lookup_presup_trigger(&s)
29426 .expect("Lexicon mismatch: Verb flagged as trigger but lookup failed")
29427 }
29428
29429 fn is_followed_by_np_object(&self) -> bool {
29430 if self.current + 1 >= self.tokens.len() {
29431 return false;
29432 }
29433 let next = &self.tokens[self.current + 1].kind;
29434 matches!(
29435 next,
29436 TokenType::ProperName(_)
29437 | TokenType::Article(_)
29438 | TokenType::Noun(_)
29439 | TokenType::Pronoun { .. }
29440 | TokenType::Reflexive
29441 | TokenType::Who
29442 | TokenType::What
29443 | TokenType::Where
29444 | TokenType::When
29445 | TokenType::Why
29446 )
29447 }
29448
29449 fn is_followed_by_gerund(&self) -> bool {
29450 if self.current + 1 >= self.tokens.len() {
29451 return false;
29452 }
29453 matches!(self.tokens[self.current + 1].kind, TokenType::Verb { .. })
29454 }
29455
29456 fn parse_spawn_statement(&mut self) -> ParseResult<Stmt<'a>> {
29462 self.advance(); if !self.check_article() {
29466 return Err(ParseError {
29467 kind: ParseErrorKind::ExpectedKeyword {
29468 keyword: "a/an".to_string(),
29469 },
29470 span: self.current_span(),
29471 });
29472 }
29473 self.advance(); let agent_type = match &self.tokens[self.current].kind {
29477 TokenType::Noun(sym) | TokenType::ProperName(sym) => {
29478 let s = *sym;
29479 self.advance();
29480 s
29481 }
29482 _ => {
29483 return Err(ParseError {
29484 kind: ParseErrorKind::ExpectedKeyword {
29485 keyword: "agent type".to_string(),
29486 },
29487 span: self.current_span(),
29488 });
29489 }
29490 };
29491
29492 if !self.check(&TokenType::Called) {
29494 return Err(ParseError {
29495 kind: ParseErrorKind::ExpectedKeyword {
29496 keyword: "called".to_string(),
29497 },
29498 span: self.current_span(),
29499 });
29500 }
29501 self.advance(); let name = if let TokenType::StringLiteral(sym) = &self.tokens[self.current].kind {
29505 let s = *sym;
29506 self.advance();
29507 s
29508 } else {
29509 return Err(ParseError {
29510 kind: ParseErrorKind::ExpectedKeyword {
29511 keyword: "agent name".to_string(),
29512 },
29513 span: self.current_span(),
29514 });
29515 };
29516
29517 Ok(Stmt::Spawn { agent_type, name })
29518 }
29519
29520 fn parse_send_statement(&mut self) -> ParseResult<Stmt<'a>> {
29522 self.advance(); let mut cached = false;
29527 let mut unchecked = false;
29528 let mut compression = None;
29529 let mut layout = None;
29530 let mut shared = false;
29531 let mut computed = false;
29532 let mut indexed = false;
29533 let mut deduped = false;
29534 let mut unreliable = false;
29535 loop {
29536 if !cached && self.check_word("cached") {
29537 self.advance();
29538 cached = true;
29539 } else if !shared && (self.check_word("shared") || self.check_word("known")) {
29540 self.advance();
29541 shared = true;
29542 } else if !indexed && (self.check_word("indexed") || self.check_word("addressable")) {
29543 self.advance();
29544 indexed = true;
29545 } else if !deduped && (self.check_word("deduped") || self.check_word("shared_refs")) {
29546 self.advance();
29547 deduped = true;
29548 } else if !computed && self.check_word("computed") {
29549 self.advance();
29550 computed = true;
29551 } else if !unchecked && self.check_word("unchecked") {
29552 self.advance();
29553 unchecked = true;
29554 } else if !unreliable && self.check_word("unreliably") {
29555 self.advance();
29556 unreliable = true;
29557 } else if layout.is_none() && (self.check_word("fast") || self.check_word("quickly")) {
29558 self.advance();
29559 layout = Some(SendLayout::Fast);
29560 } else if layout.is_none() && (self.check_word("compact") || self.check_word("small")) {
29561 self.advance();
29562 layout = Some(SendLayout::Compact);
29563 } else if layout.is_none() && self.check_word("packed") {
29564 self.advance();
29565 layout = Some(SendLayout::Packed);
29566 } else if layout.is_none() && (self.check_word("smallest") || self.check_word("best")) {
29567 self.advance();
29568 layout = Some(SendLayout::Smallest);
29569 } else if layout.is_none() && (self.check_word("redundant") || self.check_word("tough"))
29570 {
29571 self.advance();
29572 layout = Some(SendLayout::Redundant);
29573 } else if compression.is_none() && self.check_word("compressed") {
29574 self.advance();
29575 let codec = if self.check_word("with") {
29576 self.advance();
29577 let name = self
29578 .interner
29579 .resolve(self.peek().lexeme)
29580 .to_ascii_lowercase();
29581 let c = match name.as_str() {
29582 "deflate" => CompressionCodec::Deflate,
29583 "lz4" => CompressionCodec::Lz4,
29584 "zstd" => CompressionCodec::Zstd,
29585 _ => {
29586 return Err(ParseError {
29587 kind: ParseErrorKind::ExpectedKeyword {
29588 keyword: "a codec (deflate, lz4, or zstd)".to_string(),
29589 },
29590 span: self.current_span(),
29591 })
29592 }
29593 };
29594 self.advance(); c
29596 } else {
29597 CompressionCodec::Deflate
29598 };
29599 compression = Some(codec);
29600 } else {
29601 break;
29602 }
29603 }
29604
29605 let message = self.parse_imperative_expr()?;
29607
29608 if !self.check_preposition_is("to") {
29610 return Err(ParseError {
29611 kind: ParseErrorKind::ExpectedKeyword {
29612 keyword: "to".to_string(),
29613 },
29614 span: self.current_span(),
29615 });
29616 }
29617 self.advance(); let destination = self.parse_imperative_expr()?;
29621
29622 Ok(Stmt::SendMessage {
29623 message,
29624 destination,
29625 compression,
29626 cached,
29627 unchecked,
29628 layout,
29629 shared,
29630 computed,
29631 indexed,
29632 deduped,
29633 unreliable,
29634 })
29635 }
29636
29637 fn parse_await_statement(&mut self) -> ParseResult<Stmt<'a>> {
29639 self.advance(); let mut view = false;
29644 let mut stream = false;
29645 if self.check_word("view") || self.check_word("viewed") {
29646 view = true;
29647 self.advance();
29648 }
29649 if self.check_word("stream") {
29650 stream = true;
29651 self.advance();
29652 }
29653
29654 if self.check_word("response") {
29656 self.advance();
29657 }
29658
29659 if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
29661 return Err(ParseError {
29662 kind: ParseErrorKind::ExpectedKeyword {
29663 keyword: "from".to_string(),
29664 },
29665 span: self.current_span(),
29666 });
29667 }
29668 self.advance(); let source = self.parse_imperative_expr()?;
29672
29673 if !self.check_word("into") {
29675 return Err(ParseError {
29676 kind: ParseErrorKind::ExpectedKeyword {
29677 keyword: "into".to_string(),
29678 },
29679 span: self.current_span(),
29680 });
29681 }
29682 self.advance(); let into = self.expect_identifier()?;
29689
29690 Ok(Stmt::AwaitMessage {
29691 source,
29692 into,
29693 view,
29694 stream,
29695 })
29696 }
29697
29698 fn parse_stream_statement(&mut self) -> ParseResult<Stmt<'a>> {
29704 self.advance(); let values = self.parse_imperative_expr()?;
29706 if self.check(&TokenType::Into) || self.check_word("into") {
29708 self.advance();
29709 let pipe = self.parse_imperative_expr()?;
29710 return Ok(Stmt::SendPipe {
29711 value: values,
29712 pipe,
29713 });
29714 }
29715 if self.check(&TokenType::To) || self.check_preposition_is("to") || self.check_word("to") {
29717 self.advance();
29718 let destination = self.parse_imperative_expr()?;
29719 return Ok(Stmt::StreamMessage {
29720 values,
29721 destination,
29722 });
29723 }
29724 Err(ParseError {
29725 kind: ParseErrorKind::ExpectedKeyword {
29726 keyword: "to (relay) or into (pipe)".to_string(),
29727 },
29728 span: self.current_span(),
29729 })
29730 }
29731
29732 fn parse_merge_statement(&mut self) -> ParseResult<Stmt<'a>> {
29738 self.parse_crdt_join_like(false)
29739 }
29740
29741 fn parse_converge_statement(&mut self) -> ParseResult<Stmt<'a>> {
29744 self.parse_crdt_join_like(true)
29745 }
29746
29747 fn parse_crdt_join_like(&mut self, is_join: bool) -> ParseResult<Stmt<'a>> {
29750 self.advance(); let source = self.parse_imperative_expr()?;
29754
29755 if !self.check_word("into") {
29757 return Err(ParseError {
29758 kind: ParseErrorKind::ExpectedKeyword {
29759 keyword: "into".to_string(),
29760 },
29761 span: self.current_span(),
29762 });
29763 }
29764 self.advance(); let target = self.parse_imperative_expr()?;
29768
29769 Ok(if is_join {
29770 Stmt::JoinCrdt { source, target }
29771 } else {
29772 Stmt::MergeCrdt { source, target }
29773 })
29774 }
29775
29776 fn parse_swap_statement(&mut self) -> ParseResult<Stmt<'a>> {
29779 self.advance(); let a_span = self.current_span();
29781 let a = self.expect_identifier()?;
29782 self.note_write_at(a, a_span, component::WriteVerb::Swap);
29783 if !self.check(&TokenType::And) {
29784 return Err(ParseError {
29785 kind: ParseErrorKind::ExpectedKeyword {
29786 keyword: "and".to_string(),
29787 },
29788 span: self.current_span(),
29789 });
29790 }
29791 self.advance(); let b_span = self.current_span();
29793 let b = self.expect_identifier()?;
29794 self.note_write_at(b, b_span, component::WriteVerb::Swap);
29795 Ok(Stmt::Swap { a, b })
29796 }
29797
29798 fn parse_step_statement(&mut self, increase: bool) -> ParseResult<Stmt<'a>> {
29804 self.advance(); let target_from = self.current;
29807 let expr = self.parse_imperative_expr()?;
29808 self.note_write(
29809 expr,
29810 target_from,
29811 self.current,
29812 if increase {
29813 component::WriteVerb::Increase
29814 } else {
29815 component::WriteVerb::Decrease
29816 },
29817 );
29818
29819 let field_target = match expr {
29822 Expr::FieldAccess { object, field } => Some((*object, *field)),
29823 Expr::Identifier(_) => None,
29824 _ => {
29825 return Err(ParseError {
29826 kind: ParseErrorKind::ExpectedKeyword {
29827 keyword: "a name or a field access (e.g., 'count' or 'x's count')"
29828 .to_string(),
29829 },
29830 span: self.current_span(),
29831 })
29832 }
29833 };
29834
29835 if !self.check_preposition_is("by") {
29836 return Err(ParseError {
29837 kind: ParseErrorKind::ExpectedKeyword {
29838 keyword: "by".to_string(),
29839 },
29840 span: self.current_span(),
29841 });
29842 }
29843 self.advance(); let amount = self.parse_imperative_expr()?;
29846
29847 if let Some((object, field)) = field_target {
29848 return Ok(if increase {
29849 Stmt::IncreaseCrdt {
29850 object,
29851 field,
29852 amount,
29853 }
29854 } else {
29855 Stmt::DecreaseCrdt {
29856 object,
29857 field,
29858 amount,
29859 }
29860 });
29861 }
29862
29863 let Expr::Identifier(target) = expr else {
29864 unreachable!("classified as a plain name above")
29865 };
29866 let left = self.ctx.alloc_imperative_expr(Expr::Identifier(*target));
29867 let op = if increase {
29868 BinaryOpKind::Add
29869 } else {
29870 BinaryOpKind::Subtract
29871 };
29872 let value = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
29873 op,
29874 left,
29875 right: amount,
29876 });
29877 Ok(Stmt::Set {
29878 target: *target,
29879 value,
29880 })
29881 }
29882
29883 fn parse_increase_statement(&mut self) -> ParseResult<Stmt<'a>> {
29884 self.parse_step_statement(true)
29885 }
29886
29887 fn parse_decrease_statement(&mut self) -> ParseResult<Stmt<'a>> {
29888 self.parse_step_statement(false)
29889 }
29890
29891 fn parse_append_statement(&mut self) -> ParseResult<Stmt<'a>> {
29893 self.advance(); let value = self.parse_imperative_expr()?;
29897
29898 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
29900 return Err(ParseError {
29901 kind: ParseErrorKind::ExpectedKeyword {
29902 keyword: "to".to_string(),
29903 },
29904 span: self.current_span(),
29905 });
29906 }
29907 self.advance(); let sequence = self.parse_imperative_expr()?;
29911
29912 Ok(Stmt::AppendToSequence { sequence, value })
29913 }
29914
29915 fn parse_resolve_statement(&mut self) -> ParseResult<Stmt<'a>> {
29917 self.advance(); let expr = self.parse_imperative_expr()?;
29921
29922 let (object, field) = if let Expr::FieldAccess { object, field } = expr {
29924 (object, field)
29925 } else {
29926 return Err(ParseError {
29927 kind: ParseErrorKind::ExpectedKeyword {
29928 keyword: "field access (e.g., 'x's title')".to_string(),
29929 },
29930 span: self.current_span(),
29931 });
29932 };
29933
29934 if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
29936 return Err(ParseError {
29937 kind: ParseErrorKind::ExpectedKeyword {
29938 keyword: "to".to_string(),
29939 },
29940 span: self.current_span(),
29941 });
29942 }
29943 self.advance(); let value = self.parse_imperative_expr()?;
29947
29948 Ok(Stmt::ResolveConflict {
29949 object,
29950 field: *field,
29951 value,
29952 })
29953 }
29954}
29955
29956fn proof_line_names_one_rule(parts: &[String]) -> Option<String> {
29971 let mut words = parts.iter().filter(|p| !p.trim().is_empty());
29972 let head = words.next()?;
29973 match (words.next(), words.next()) {
29974 (None, _) => {}
29975 (Some(p), None) if p == "." => {}
29976 _ => return None,
29977 }
29978 let mut chars = head.chars();
29979 if !chars.next().is_some_and(|c| c.is_alphabetic() || c == '_') {
29980 return None;
29981 }
29982 if !chars.all(|c| c.is_alphanumeric() || c == '_') {
29983 return None;
29984 }
29985 Some(head.clone())
29986}
29987
29988fn premise_label_conflict(names: &[Option<String>]) -> Option<String> {
30012 for (i, label) in names.iter().enumerate() {
30015 let Some(label) = label else { continue };
30016 let positional = label.len() > 2
30017 && label.starts_with("hp")
30018 && label[2..].bytes().all(|b| b.is_ascii_digit());
30019 if positional && *label != format!("hp{i}") {
30020 return Some(format!(
30021 "premise {i} is labelled `{label}`, and `hp<n>` is the POSITIONAL name every \
30022 `Proof:` script already has for premise n — so `{label}` would cite THIS \
30023 premise while naming another. Choose a label outside the `hp<n>` namespace, \
30024 or write `hp{i}`, which is what this premise is called"
30025 ));
30026 }
30027 }
30028 let effective: Vec<String> = names
30030 .iter()
30031 .enumerate()
30032 .map(|(i, n)| n.clone().unwrap_or_else(|| format!("hp{i}")))
30033 .collect();
30034 for (i, name) in effective.iter().enumerate() {
30035 if let Some(j) = effective[..i].iter().position(|earlier| earlier == name) {
30036 return Some(format!(
30037 "premises {j} and {i} are both named `{name}` — a `Proof:` script citing \
30038 `{name}` would silently take premise {j} and never see premise {i}. Give \
30039 them different `Given (<label>):` names"
30040 ));
30041 }
30042 }
30043 None
30044}
30045
30046fn names_a_tactic(word: &str) -> bool {
30054 match logicaffeine_proof::tactic_script::parse_script(word) {
30055 Ok(_) => true,
30056 Err(e) => !e.to_string().contains("unknown tactic"),
30057 }
30058}
30059
30060fn is_proof_word(word: &str) -> bool {
30062 let mut chars = word.chars();
30063 chars.next().is_some_and(|c| c.is_alphabetic() || c == '_')
30064 && chars.all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
30065}
30066
30067fn lower_rule_applications(parts: &[String]) -> Option<String> {
30090 let mut out: Vec<String> = Vec::with_capacity(parts.len());
30091 let mut changed = false;
30092 let mut at_step_start = true;
30093 let mut i = 0;
30094 while i < parts.len() {
30095 let word = parts[i].trim();
30096 if word.is_empty() {
30097 out.push(parts[i].clone());
30098 i += 1;
30099 continue;
30100 }
30101 if at_step_start && word.eq_ignore_ascii_case("by") {
30102 if let Some(j) = (i + 1..parts.len()).find(|k| !parts[*k].trim().is_empty()) {
30103 let rule = parts[j].trim();
30104 if logicaffeine_proof::InferenceRule::from_name(rule).is_some()
30105 && !names_a_tactic(rule)
30106 {
30107 out.push("by_rule".to_string());
30108 out.push(rule.to_string());
30109 changed = true;
30110 at_step_start = false;
30111 i = j + 1;
30112 continue;
30113 }
30114 }
30115 }
30116 at_step_start = word == "." || word == ";";
30117 out.push(parts[i].clone());
30118 i += 1;
30119 }
30120 changed.then(|| out.join(" "))
30121}
30122
30123fn proof_line_is_induction_on(parts: &[String]) -> Option<String> {
30137 let mut words = parts.iter().map(|p| p.trim()).filter(|p| !p.is_empty());
30138 if !words.next()?.eq_ignore_ascii_case("by") {
30139 return None;
30140 }
30141 if !words.next()?.eq_ignore_ascii_case("induction") {
30142 return None;
30143 }
30144 if !words.next()?.eq_ignore_ascii_case("on") {
30145 return None;
30146 }
30147 let variable = words.next()?;
30148 if !is_proof_word(variable) {
30149 return None;
30150 }
30151 match (words.next(), words.next()) {
30152 (None, _) => {}
30153 (Some("."), None) => {}
30154 _ => return None,
30155 }
30156 Some(variable.to_string())
30157}