|
1 | 1 | //! Core language.
|
2 | 2 |
|
3 |
| -use crate::env::{Index, Level}; |
| 3 | +use scoped_arena::Scope; |
| 4 | + |
| 5 | +use crate::env::{EnvLen, Index, Level}; |
4 | 6 | use crate::source::{Span, StringId};
|
5 | 7 |
|
6 | 8 | pub mod binary;
|
@@ -185,6 +187,10 @@ pub enum Term<'arena> {
|
185 | 187 | }
|
186 | 188 |
|
187 | 189 | impl<'arena> Term<'arena> {
|
| 190 | + pub fn error(span: Span) -> Self { |
| 191 | + Self::Prim(span, Prim::ReportedError) |
| 192 | + } |
| 193 | + |
188 | 194 | /// Get the source span of the term.
|
189 | 195 | pub fn span(&self) -> Span {
|
190 | 196 | match self {
|
@@ -255,6 +261,149 @@ impl<'arena> Term<'arena> {
|
255 | 261 | }
|
256 | 262 | }
|
257 | 263 | }
|
| 264 | + |
| 265 | + // TODO: Add a new `Weaken` variant to `core::Term` instead of eagerly traversing the term? |
| 266 | + pub fn shift(&self, scope: &'arena Scope<'arena>, amount: EnvLen) -> Term<'arena> { |
| 267 | + self.shift_inner(scope, Index::last(), amount) |
| 268 | + } |
| 269 | + |
| 270 | + /// Increment all `LocalVar`s greater than or equal to `min` by `amount` |
| 271 | + fn shift_inner( |
| 272 | + &self, |
| 273 | + scope: &'arena Scope<'arena>, |
| 274 | + mut min: Index, |
| 275 | + amount: EnvLen, |
| 276 | + ) -> Term<'arena> { |
| 277 | + // Skip traversing and rebuilding the term if it would make no change. Increases sharing. |
| 278 | + if amount == EnvLen::new() { |
| 279 | + return self.clone(); |
| 280 | + } |
| 281 | + |
| 282 | + match self { |
| 283 | + Term::LocalVar(span, var) if *var >= min => Term::LocalVar(*span, *var + amount), |
| 284 | + Term::LocalVar(..) |
| 285 | + | Term::ItemVar(..) |
| 286 | + | Term::MetaVar(..) |
| 287 | + | Term::InsertedMeta(..) |
| 288 | + | Term::Prim(..) |
| 289 | + | Term::ConstLit(..) |
| 290 | + | Term::Universe(..) => self.clone(), |
| 291 | + Term::Ann(span, expr, r#type) => Term::Ann( |
| 292 | + *span, |
| 293 | + scope.to_scope(expr.shift_inner(scope, min, amount)), |
| 294 | + scope.to_scope(r#type.shift_inner(scope, min, amount)), |
| 295 | + ), |
| 296 | + Term::Let(span, name, def_type, def_expr, body) => Term::Let( |
| 297 | + *span, |
| 298 | + *name, |
| 299 | + scope.to_scope(def_type.shift_inner(scope, min, amount)), |
| 300 | + scope.to_scope(def_expr.shift_inner(scope, min, amount)), |
| 301 | + scope.to_scope(body.shift_inner(scope, min.prev(), amount)), |
| 302 | + ), |
| 303 | + Term::FunType(span, name, input, output) => Term::FunType( |
| 304 | + *span, |
| 305 | + *name, |
| 306 | + scope.to_scope(input.shift_inner(scope, min, amount)), |
| 307 | + scope.to_scope(output.shift_inner(scope, min.prev(), amount)), |
| 308 | + ), |
| 309 | + Term::FunLit(span, name, body) => Term::FunLit( |
| 310 | + *span, |
| 311 | + *name, |
| 312 | + scope.to_scope(body.shift_inner(scope, min.prev(), amount)), |
| 313 | + ), |
| 314 | + Term::FunApp(span, fun, arg) => Term::FunApp( |
| 315 | + *span, |
| 316 | + scope.to_scope(fun.shift_inner(scope, min, amount)), |
| 317 | + scope.to_scope(arg.shift_inner(scope, min, amount)), |
| 318 | + ), |
| 319 | + Term::RecordType(span, labels, types) => Term::RecordType( |
| 320 | + *span, |
| 321 | + labels, |
| 322 | + scope.to_scope_from_iter(types.iter().map(|r#type| { |
| 323 | + let ret = r#type.shift_inner(scope, min, amount); |
| 324 | + min = min.prev(); |
| 325 | + ret |
| 326 | + })), |
| 327 | + ), |
| 328 | + Term::RecordLit(span, labels, exprs) => Term::RecordLit( |
| 329 | + *span, |
| 330 | + labels, |
| 331 | + scope.to_scope_from_iter( |
| 332 | + exprs |
| 333 | + .iter() |
| 334 | + .map(|expr| expr.shift_inner(scope, min, amount)), |
| 335 | + ), |
| 336 | + ), |
| 337 | + Term::RecordProj(span, head, label) => Term::RecordProj( |
| 338 | + *span, |
| 339 | + scope.to_scope(head.shift_inner(scope, min, amount)), |
| 340 | + *label, |
| 341 | + ), |
| 342 | + Term::ArrayLit(span, terms) => Term::ArrayLit( |
| 343 | + *span, |
| 344 | + scope.to_scope_from_iter( |
| 345 | + terms |
| 346 | + .iter() |
| 347 | + .map(|term| term.shift_inner(scope, min, amount)), |
| 348 | + ), |
| 349 | + ), |
| 350 | + Term::FormatRecord(span, labels, terms) => Term::FormatRecord( |
| 351 | + *span, |
| 352 | + labels, |
| 353 | + scope.to_scope_from_iter(terms.iter().map(|term| { |
| 354 | + let ret = term.shift_inner(scope, min, amount); |
| 355 | + min = min.prev(); |
| 356 | + ret |
| 357 | + })), |
| 358 | + ), |
| 359 | + Term::FormatCond(span, name, format, pred) => Term::FormatCond( |
| 360 | + *span, |
| 361 | + *name, |
| 362 | + scope.to_scope(format.shift_inner(scope, min, amount)), |
| 363 | + scope.to_scope(pred.shift_inner(scope, min.prev(), amount)), |
| 364 | + ), |
| 365 | + Term::FormatOverlap(span, labels, terms) => Term::FormatOverlap( |
| 366 | + *span, |
| 367 | + labels, |
| 368 | + scope.to_scope_from_iter(terms.iter().map(|term| { |
| 369 | + let ret = term.shift_inner(scope, min, amount); |
| 370 | + min = min.prev(); |
| 371 | + ret |
| 372 | + })), |
| 373 | + ), |
| 374 | + Term::ConstMatch(span, scrut, branches, default) => Term::ConstMatch( |
| 375 | + *span, |
| 376 | + scope.to_scope(scrut.shift_inner(scope, min, amount)), |
| 377 | + scope.to_scope_from_iter( |
| 378 | + branches |
| 379 | + .iter() |
| 380 | + .map(|(r#const, term)| (*r#const, term.shift_inner(scope, min, amount))), |
| 381 | + ), |
| 382 | + default.map(|(name, term)| { |
| 383 | + ( |
| 384 | + name, |
| 385 | + scope.to_scope(term.shift_inner(scope, min.prev(), amount)) as &_, |
| 386 | + ) |
| 387 | + }), |
| 388 | + ), |
| 389 | + } |
| 390 | + } |
| 391 | + |
| 392 | + /// Returns `true` if `self` can be evaluated in a single step. |
| 393 | + /// Used as a heuristic to prevent increase in runtime when expanding pattern matches |
| 394 | + pub fn is_atomic(&self) -> bool { |
| 395 | + match self { |
| 396 | + Term::ItemVar(_, _) |
| 397 | + | Term::LocalVar(_, _) |
| 398 | + | Term::MetaVar(_, _) |
| 399 | + | Term::InsertedMeta(_, _, _) |
| 400 | + | Term::Universe(_) |
| 401 | + | Term::Prim(_, _) |
| 402 | + | Term::ConstLit(_, _) => true, |
| 403 | + Term::RecordProj(_, head, _) => head.is_atomic(), |
| 404 | + _ => false, |
| 405 | + } |
| 406 | + } |
258 | 407 | }
|
259 | 408 |
|
260 | 409 | macro_rules! def_prims {
|
@@ -572,6 +721,21 @@ pub enum Const {
|
572 | 721 | Ref(usize),
|
573 | 722 | }
|
574 | 723 |
|
| 724 | +impl Const { |
| 725 | + /// Return the number of inhabitants of `self`. |
| 726 | + /// `None` represents infinity |
| 727 | + pub fn num_inhabitants(&self) -> Option<u128> { |
| 728 | + match self { |
| 729 | + Const::Bool(_) => Some(2), |
| 730 | + Const::U8(_, _) | Const::S8(_) => Some(1 << 8), |
| 731 | + Const::U16(_, _) | Const::S16(_) => Some(1 << 16), |
| 732 | + Const::U32(_, _) | Const::S32(_) => Some(1 << 32), |
| 733 | + Const::U64(_, _) | Const::S64(_) => Some(1 << 64), |
| 734 | + Const::F32(_) | Const::F64(_) | Const::Pos(_) | Const::Ref(_) => None, |
| 735 | + } |
| 736 | + } |
| 737 | +} |
| 738 | + |
575 | 739 | impl PartialEq for Const {
|
576 | 740 | fn eq(&self, other: &Self) -> bool {
|
577 | 741 | match (*self, *other) {
|
|
0 commit comments