|
1 | 1 | //! Core language.
|
2 | 2 |
|
3 |
| -use crate::env::{Index, Level}; |
4 |
| -use crate::source::{Span, StringId}; |
| 3 | +use scoped_arena::Scope; |
| 4 | + |
| 5 | +use crate::env::{EnvLen, Index, Level}; |
| 6 | +use crate::source::{ByteRange, Span, StringId}; |
5 | 7 |
|
6 | 8 | pub mod binary;
|
7 | 9 | pub mod prim;
|
@@ -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,176 @@ 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 | + |
| 393 | +/// Simple patterns that have had some initial elaboration performed on them |
| 394 | +#[derive(Debug, Clone)] |
| 395 | +pub enum CheckedPattern<'arena> { |
| 396 | + /// Error sentinel |
| 397 | + ReportedError(ByteRange), |
| 398 | + /// Placeholder patterns that match everything |
| 399 | + Placeholder(ByteRange), |
| 400 | + /// Pattern that binds local variable |
| 401 | + Binder(ByteRange, StringId), |
| 402 | + /// Constant literals |
| 403 | + ConstLit(ByteRange, Const), |
| 404 | + /// Record literals |
| 405 | + RecordLit(ByteRange, &'arena [StringId], &'arena [Self]), |
| 406 | +} |
| 407 | + |
| 408 | +impl<'arena> CheckedPattern<'arena> { |
| 409 | + pub fn range(&self) -> ByteRange { |
| 410 | + match self { |
| 411 | + CheckedPattern::ReportedError(range) |
| 412 | + | CheckedPattern::Placeholder(range) |
| 413 | + | CheckedPattern::Binder(range, _) |
| 414 | + | CheckedPattern::ConstLit(range, _) |
| 415 | + | CheckedPattern::RecordLit(range, _, _) => *range, |
| 416 | + } |
| 417 | + } |
| 418 | + |
| 419 | + pub fn name(&self) -> Option<StringId> { |
| 420 | + match self { |
| 421 | + CheckedPattern::Binder(_, name) => Some(*name), |
| 422 | + _ => None, |
| 423 | + } |
| 424 | + } |
| 425 | + |
| 426 | + pub fn is_infalliable(&self) -> bool { |
| 427 | + match self { |
| 428 | + CheckedPattern::ReportedError(_) |
| 429 | + | CheckedPattern::Placeholder(_) |
| 430 | + | CheckedPattern::Binder(_, _) => true, |
| 431 | + CheckedPattern::ConstLit(_, _) | CheckedPattern::RecordLit(_, _, _) => false, |
| 432 | + } |
| 433 | + } |
258 | 434 | }
|
259 | 435 |
|
260 | 436 | macro_rules! def_prims {
|
@@ -571,6 +747,18 @@ pub enum Const {
|
571 | 747 | Pos(usize),
|
572 | 748 | Ref(usize),
|
573 | 749 | }
|
| 750 | +impl Const { |
| 751 | + pub fn num_variants(&self) -> Option<u128> { |
| 752 | + match self { |
| 753 | + Const::Bool(_) => Some(2), |
| 754 | + Const::U8(_, _) | Const::S8(_) => Some(u8::MAX as u128 + 1), |
| 755 | + Const::U16(_, _) | Const::S16(_) => Some(u16::MAX as u128 + 1), |
| 756 | + Const::U32(_, _) | Const::S32(_) => Some(u32::MAX as u128 + 1), |
| 757 | + Const::U64(_, _) | Const::S64(_) => Some(u64::MAX as u128 + 1), |
| 758 | + Const::F32(_) | Const::F64(_) | Const::Pos(_) | Const::Ref(_) => None, |
| 759 | + } |
| 760 | + } |
| 761 | +} |
574 | 762 |
|
575 | 763 | impl PartialEq for Const {
|
576 | 764 | fn eq(&self, other: &Self) -> bool {
|
|
0 commit comments