|
2 | 2 |
|
3 | 3 | use std::fmt;
|
4 | 4 |
|
5 |
| -use crate::env::{Index, Level}; |
| 5 | +use crate::env::{EnvLen, Index, Level}; |
6 | 6 | use crate::source::{Span, StringId};
|
| 7 | +use scoped_arena::Scope; |
7 | 8 |
|
8 | 9 | pub mod binary;
|
9 | 10 | pub mod pretty;
|
@@ -205,6 +206,10 @@ pub enum Term<'arena> {
|
205 | 206 | }
|
206 | 207 |
|
207 | 208 | impl<'arena> Term<'arena> {
|
| 209 | + pub fn error(span: impl Into<Span>) -> Self { |
| 210 | + Self::Prim(span.into(), Prim::ReportedError) |
| 211 | + } |
| 212 | + |
208 | 213 | /// Get the source span of the term.
|
209 | 214 | pub fn span(&self) -> Span {
|
210 | 215 | match self {
|
@@ -279,6 +284,153 @@ impl<'arena> Term<'arena> {
|
279 | 284 | pub fn is_error(&self) -> bool {
|
280 | 285 | matches!(self, Term::Prim(_, Prim::ReportedError))
|
281 | 286 | }
|
| 287 | + |
| 288 | + // TODO: Add a new `Weaken` variant to `core::Term` instead of eagerly traversing the term? |
| 289 | + // See [Andras Kovacs’ staged language](https://github.com/AndrasKovacs/staged/blob/9e381eb162f44912d70fb843c4ca6567b0d1683a/demo/Syntax.hs#L52) for an example |
| 290 | + pub fn shift(&self, scope: &'arena Scope<'arena>, amount: EnvLen) -> Term<'arena> { |
| 291 | + self.shift_inner(scope, Index::last(), amount) |
| 292 | + } |
| 293 | + |
| 294 | + /// Increment all `LocalVar`s greater than or equal to `min` by `amount` |
| 295 | + fn shift_inner( |
| 296 | + &self, |
| 297 | + scope: &'arena Scope<'arena>, |
| 298 | + mut min: Index, |
| 299 | + amount: EnvLen, |
| 300 | + ) -> Term<'arena> { |
| 301 | + // Skip traversing and rebuilding the term if it would make no change. Increases sharing. |
| 302 | + if amount == EnvLen::new() { |
| 303 | + return self.clone(); |
| 304 | + } |
| 305 | + |
| 306 | + match self { |
| 307 | + Term::LocalVar(span, var) if *var >= min => Term::LocalVar(*span, *var + amount), |
| 308 | + Term::LocalVar(..) |
| 309 | + | Term::ItemVar(..) |
| 310 | + | Term::MetaVar(..) |
| 311 | + | Term::InsertedMeta(..) |
| 312 | + | Term::Prim(..) |
| 313 | + | Term::ConstLit(..) |
| 314 | + | Term::Universe(..) => self.clone(), |
| 315 | + Term::Ann(span, expr, r#type) => Term::Ann( |
| 316 | + *span, |
| 317 | + scope.to_scope(expr.shift_inner(scope, min, amount)), |
| 318 | + scope.to_scope(r#type.shift_inner(scope, min, amount)), |
| 319 | + ), |
| 320 | + Term::Let(span, name, def_type, def_expr, body) => Term::Let( |
| 321 | + *span, |
| 322 | + *name, |
| 323 | + scope.to_scope(def_type.shift_inner(scope, min, amount)), |
| 324 | + scope.to_scope(def_expr.shift_inner(scope, min, amount)), |
| 325 | + scope.to_scope(body.shift_inner(scope, min.prev(), amount)), |
| 326 | + ), |
| 327 | + Term::FunType(span, plicity, name, input, output) => Term::FunType( |
| 328 | + *span, |
| 329 | + *plicity, |
| 330 | + *name, |
| 331 | + scope.to_scope(input.shift_inner(scope, min, amount)), |
| 332 | + scope.to_scope(output.shift_inner(scope, min.prev(), amount)), |
| 333 | + ), |
| 334 | + Term::FunLit(span, plicity, name, body) => Term::FunLit( |
| 335 | + *span, |
| 336 | + *plicity, |
| 337 | + *name, |
| 338 | + scope.to_scope(body.shift_inner(scope, min.prev(), amount)), |
| 339 | + ), |
| 340 | + Term::FunApp(span, plicity, fun, arg) => Term::FunApp( |
| 341 | + *span, |
| 342 | + *plicity, |
| 343 | + scope.to_scope(fun.shift_inner(scope, min, amount)), |
| 344 | + scope.to_scope(arg.shift_inner(scope, min, amount)), |
| 345 | + ), |
| 346 | + Term::RecordType(span, labels, types) => Term::RecordType( |
| 347 | + *span, |
| 348 | + labels, |
| 349 | + scope.to_scope_from_iter(types.iter().map(|r#type| { |
| 350 | + let ret = r#type.shift_inner(scope, min, amount); |
| 351 | + min = min.prev(); |
| 352 | + ret |
| 353 | + })), |
| 354 | + ), |
| 355 | + Term::RecordLit(span, labels, exprs) => Term::RecordLit( |
| 356 | + *span, |
| 357 | + labels, |
| 358 | + scope.to_scope_from_iter( |
| 359 | + exprs |
| 360 | + .iter() |
| 361 | + .map(|expr| expr.shift_inner(scope, min, amount)), |
| 362 | + ), |
| 363 | + ), |
| 364 | + Term::RecordProj(span, head, label) => Term::RecordProj( |
| 365 | + *span, |
| 366 | + scope.to_scope(head.shift_inner(scope, min, amount)), |
| 367 | + *label, |
| 368 | + ), |
| 369 | + Term::ArrayLit(span, terms) => Term::ArrayLit( |
| 370 | + *span, |
| 371 | + scope.to_scope_from_iter( |
| 372 | + terms |
| 373 | + .iter() |
| 374 | + .map(|term| term.shift_inner(scope, min, amount)), |
| 375 | + ), |
| 376 | + ), |
| 377 | + Term::FormatRecord(span, labels, terms) => Term::FormatRecord( |
| 378 | + *span, |
| 379 | + labels, |
| 380 | + scope.to_scope_from_iter(terms.iter().map(|term| { |
| 381 | + let ret = term.shift_inner(scope, min, amount); |
| 382 | + min = min.prev(); |
| 383 | + ret |
| 384 | + })), |
| 385 | + ), |
| 386 | + Term::FormatCond(span, name, format, pred) => Term::FormatCond( |
| 387 | + *span, |
| 388 | + *name, |
| 389 | + scope.to_scope(format.shift_inner(scope, min, amount)), |
| 390 | + scope.to_scope(pred.shift_inner(scope, min.prev(), amount)), |
| 391 | + ), |
| 392 | + Term::FormatOverlap(span, labels, terms) => Term::FormatOverlap( |
| 393 | + *span, |
| 394 | + labels, |
| 395 | + scope.to_scope_from_iter(terms.iter().map(|term| { |
| 396 | + let ret = term.shift_inner(scope, min, amount); |
| 397 | + min = min.prev(); |
| 398 | + ret |
| 399 | + })), |
| 400 | + ), |
| 401 | + Term::ConstMatch(span, scrut, branches, default) => Term::ConstMatch( |
| 402 | + *span, |
| 403 | + scope.to_scope(scrut.shift_inner(scope, min, amount)), |
| 404 | + scope.to_scope_from_iter( |
| 405 | + branches |
| 406 | + .iter() |
| 407 | + .map(|(r#const, term)| (*r#const, term.shift_inner(scope, min, amount))), |
| 408 | + ), |
| 409 | + default.map(|(name, term)| { |
| 410 | + ( |
| 411 | + name, |
| 412 | + scope.to_scope(term.shift_inner(scope, min.prev(), amount)) as &_, |
| 413 | + ) |
| 414 | + }), |
| 415 | + ), |
| 416 | + } |
| 417 | + } |
| 418 | + |
| 419 | + /// Returns `true` if `self` can be evaluated in a single step. |
| 420 | + /// Used as a heuristic to prevent increase in runtime when expanding pattern matches |
| 421 | + pub fn is_atomic(&self) -> bool { |
| 422 | + match self { |
| 423 | + Term::ItemVar(_, _) |
| 424 | + | Term::LocalVar(_, _) |
| 425 | + | Term::MetaVar(_, _) |
| 426 | + | Term::InsertedMeta(_, _, _) |
| 427 | + | Term::Universe(_) |
| 428 | + | Term::Prim(_, _) |
| 429 | + | Term::ConstLit(_, _) => true, |
| 430 | + Term::RecordProj(_, head, _) => head.is_atomic(), |
| 431 | + _ => false, |
| 432 | + } |
| 433 | + } |
282 | 434 | }
|
283 | 435 |
|
284 | 436 | macro_rules! def_prims {
|
@@ -599,6 +751,21 @@ pub enum Const {
|
599 | 751 | Ref(usize),
|
600 | 752 | }
|
601 | 753 |
|
| 754 | +impl Const { |
| 755 | + /// Return the number of inhabitants of `self`. |
| 756 | + /// `None` represents infinity |
| 757 | + pub fn num_inhabitants(&self) -> Option<u128> { |
| 758 | + match self { |
| 759 | + Const::Bool(_) => Some(2), |
| 760 | + Const::U8(_, _) | Const::S8(_) => Some(1 << 8), |
| 761 | + Const::U16(_, _) | Const::S16(_) => Some(1 << 16), |
| 762 | + Const::U32(_, _) | Const::S32(_) => Some(1 << 32), |
| 763 | + Const::U64(_, _) | Const::S64(_) => Some(1 << 64), |
| 764 | + Const::F32(_) | Const::F64(_) | Const::Pos(_) | Const::Ref(_) => None, |
| 765 | + } |
| 766 | + } |
| 767 | +} |
| 768 | + |
602 | 769 | impl PartialEq for Const {
|
603 | 770 | fn eq(&self, other: &Self) -> bool {
|
604 | 771 | match (*self, *other) {
|
|
0 commit comments