1use crate::ast::{
15 AspectOperator, Expr, LogicExpr, ModalVector, NounPhrase, QuantifierKind, Stmt,
16 TemporalOperator, Term, ThematicRole, TypeExpr, VoiceOperator,
17};
18use crate::token::TokenType;
19use logicaffeine_base::Arena;
20use logicaffeine_base::Symbol;
21
22#[derive(Clone, Copy)]
34pub struct AstContext<'a> {
35 pub exprs: &'a Arena<LogicExpr<'a>>,
37 pub terms: &'a Arena<Term<'a>>,
39 pub nps: &'a Arena<NounPhrase<'a>>,
41 pub syms: &'a Arena<Symbol>,
43 pub roles: &'a Arena<(ThematicRole, Term<'a>)>,
45 pub pps: &'a Arena<&'a LogicExpr<'a>>,
47 pub stmts: Option<&'a Arena<Stmt<'a>>>,
49 pub imperative_exprs: Option<&'a Arena<Expr<'a>>>,
51 pub type_exprs: Option<&'a Arena<TypeExpr<'a>>>,
53}
54
55impl<'a> AstContext<'a> {
56 pub fn new(
58 exprs: &'a Arena<LogicExpr<'a>>,
59 terms: &'a Arena<Term<'a>>,
60 nps: &'a Arena<NounPhrase<'a>>,
61 syms: &'a Arena<Symbol>,
62 roles: &'a Arena<(ThematicRole, Term<'a>)>,
63 pps: &'a Arena<&'a LogicExpr<'a>>,
64 ) -> Self {
65 AstContext {
66 exprs,
67 terms,
68 nps,
69 syms,
70 roles,
71 pps,
72 stmts: None,
73 imperative_exprs: None,
74 type_exprs: None,
75 }
76 }
77
78 pub fn with_imperative(
80 exprs: &'a Arena<LogicExpr<'a>>,
81 terms: &'a Arena<Term<'a>>,
82 nps: &'a Arena<NounPhrase<'a>>,
83 syms: &'a Arena<Symbol>,
84 roles: &'a Arena<(ThematicRole, Term<'a>)>,
85 pps: &'a Arena<&'a LogicExpr<'a>>,
86 stmts: &'a Arena<Stmt<'a>>,
87 imperative_exprs: &'a Arena<Expr<'a>>,
88 ) -> Self {
89 AstContext {
90 exprs,
91 terms,
92 nps,
93 syms,
94 roles,
95 pps,
96 stmts: Some(stmts),
97 imperative_exprs: Some(imperative_exprs),
98 type_exprs: None,
99 }
100 }
101
102 pub fn with_types(
104 exprs: &'a Arena<LogicExpr<'a>>,
105 terms: &'a Arena<Term<'a>>,
106 nps: &'a Arena<NounPhrase<'a>>,
107 syms: &'a Arena<Symbol>,
108 roles: &'a Arena<(ThematicRole, Term<'a>)>,
109 pps: &'a Arena<&'a LogicExpr<'a>>,
110 stmts: &'a Arena<Stmt<'a>>,
111 imperative_exprs: &'a Arena<Expr<'a>>,
112 type_exprs: &'a Arena<TypeExpr<'a>>,
113 ) -> Self {
114 AstContext {
115 exprs,
116 terms,
117 nps,
118 syms,
119 roles,
120 pps,
121 stmts: Some(stmts),
122 imperative_exprs: Some(imperative_exprs),
123 type_exprs: Some(type_exprs),
124 }
125 }
126
127 pub fn alloc_stmt(&self, stmt: Stmt<'a>) -> &'a Stmt<'a> {
132 self.stmts
133 .expect("imperative arenas not initialized")
134 .alloc(stmt)
135 }
136
137 pub fn alloc_imperative_expr(&self, expr: Expr<'a>) -> &'a Expr<'a> {
142 self.imperative_exprs
143 .expect("imperative arenas not initialized")
144 .alloc(expr)
145 }
146
147 pub fn alloc_type_expr(&self, ty: TypeExpr<'a>) -> &'a TypeExpr<'a> {
152 self.type_exprs
153 .expect("type_exprs arena not initialized")
154 .alloc(ty)
155 }
156
157 pub fn alloc_type_exprs<I>(&self, types: I) -> &'a [TypeExpr<'a>]
162 where
163 I: IntoIterator<Item = TypeExpr<'a>>,
164 I::IntoIter: ExactSizeIterator,
165 {
166 self.type_exprs
167 .expect("type_exprs arena not initialized")
168 .alloc_slice(types)
169 }
170
171 pub fn alloc_expr(&self, expr: LogicExpr<'a>) -> &'a LogicExpr<'a> {
173 self.exprs.alloc(expr)
174 }
175
176 pub fn alloc_term(&self, term: Term<'a>) -> &'a Term<'a> {
178 self.terms.alloc(term)
179 }
180
181 pub fn alloc_terms<I>(&self, terms: I) -> &'a [Term<'a>]
183 where
184 I: IntoIterator<Item = Term<'a>>,
185 I::IntoIter: ExactSizeIterator,
186 {
187 self.terms.alloc_slice(terms)
188 }
189
190 pub fn alloc_np(&self, np: NounPhrase<'a>) -> &'a NounPhrase<'a> {
192 self.nps.alloc(np)
193 }
194
195 pub fn alloc_syms<I>(&self, syms: I) -> &'a [Symbol]
197 where
198 I: IntoIterator<Item = Symbol>,
199 I::IntoIter: ExactSizeIterator,
200 {
201 self.syms.alloc_slice(syms)
202 }
203
204 pub fn alloc_roles<I>(&self, roles: I) -> &'a [(ThematicRole, Term<'a>)]
206 where
207 I: IntoIterator<Item = (ThematicRole, Term<'a>)>,
208 I::IntoIter: ExactSizeIterator,
209 {
210 self.roles.alloc_slice(roles)
211 }
212
213 pub fn alloc_pps<I>(&self, pps: I) -> &'a [&'a LogicExpr<'a>]
215 where
216 I: IntoIterator<Item = &'a LogicExpr<'a>>,
217 I::IntoIter: ExactSizeIterator,
218 {
219 self.pps.alloc_slice(pps)
220 }
221
222 pub fn predicate(&self, name: Symbol, args: &'a [Term<'a>]) -> &'a LogicExpr<'a> {
224 self.exprs.alloc(LogicExpr::Predicate {
225 name,
226 args,
227 world: None,
228 })
229 }
230
231 #[inline(always)]
233 pub fn binary(
234 &self,
235 left: &'a LogicExpr<'a>,
236 op: TokenType,
237 right: &'a LogicExpr<'a>,
238 ) -> &'a LogicExpr<'a> {
239 self.exprs.alloc(LogicExpr::BinaryOp { left, op, right })
240 }
241
242 #[inline(always)]
244 pub fn unary(&self, op: TokenType, operand: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
245 self.exprs.alloc(LogicExpr::UnaryOp { op, operand })
246 }
247
248 #[inline(always)]
250 pub fn quantifier(
251 &self,
252 kind: QuantifierKind,
253 variable: Symbol,
254 body: &'a LogicExpr<'a>,
255 island_id: u32,
256 ) -> &'a LogicExpr<'a> {
257 self.exprs.alloc(LogicExpr::Quantifier {
258 kind,
259 variable,
260 body,
261 island_id,
262 })
263 }
264
265 #[inline(always)]
267 pub fn temporal(
268 &self,
269 operator: TemporalOperator,
270 body: &'a LogicExpr<'a>,
271 ) -> &'a LogicExpr<'a> {
272 self.exprs.alloc(LogicExpr::Temporal { operator, body })
273 }
274
275 #[inline(always)]
277 pub fn aspectual(
278 &self,
279 operator: AspectOperator,
280 body: &'a LogicExpr<'a>,
281 ) -> &'a LogicExpr<'a> {
282 self.exprs.alloc(LogicExpr::Aspectual { operator, body })
283 }
284
285 #[inline(always)]
287 pub fn voice(&self, operator: VoiceOperator, body: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
288 self.exprs.alloc(LogicExpr::Voice { operator, body })
289 }
290
291 #[inline(always)]
293 pub fn modal(&self, vector: ModalVector, operand: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
294 self.exprs.alloc(LogicExpr::Modal { vector, operand })
295 }
296}
297
298#[cfg(test)]
299#[path = "../../logicaffeine_tests/unit/logicaffeine_language/arena_ctx/tests.rs"]
300mod tests;