Skip to content

Commit

Permalink
Add types to the bytecode AST
Browse files Browse the repository at this point in the history
The previous commit introducing the new AST used the "old" type
representation from mainline Nickel. Now that `typ::Type` has been made
more generic, we can use a better representation that can be arena
allocated as well and can contain contracts that are themselves
represented in the new AST.

This commit integrates this new type representation and add
corresponding translation functions from the mainline implementation.
  • Loading branch information
yannham committed Oct 25, 2024
1 parent aaa04ab commit 7de7b8e
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 24 deletions.
78 changes: 72 additions & 6 deletions core/src/bytecode/ast/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! [crate::bytecode::ast].

use super::{primop::PrimOp, *};
use crate::term;
use crate::{term, typ as mainline_type};

/// Convert from the mainline Nickel representation to the new AST representation. This trait is
/// mostly `From` with an additional argument for the allocator.
Expand Down Expand Up @@ -142,11 +142,14 @@ impl<'ast> FromMainline<'ast, term::pattern::OrPattern> for PatternData<'ast> {

impl<'ast> FromMainline<'ast, term::TypeAnnotation> for Annotation<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, annot: &term::TypeAnnotation) -> Self {
let typ = annot.typ.as_ref().map(|typ| typ.typ.clone());
let typ = annot.typ.as_ref().map(|typ| typ.typ.to_ast(alloc));

let contracts = alloc
.type_arena
.alloc_extend(annot.contracts.iter().map(|contract| contract.typ.clone()));
let contracts = alloc.types(
annot
.contracts
.iter()
.map(|contract| contract.typ.to_ast(alloc)),
);

Annotation { typ, contracts }
}
Expand Down Expand Up @@ -175,6 +178,69 @@ impl<'ast> FromMainline<'ast, term::record::FieldMetadata> for record::FieldMeta
}
}

impl<'ast> FromMainline<'ast, mainline_type::Type> for Type<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, mainline: &mainline_type::Type) -> Self {
Type {
typ: mainline.typ.to_ast(alloc),
pos: mainline.pos,
}
}
}

type MainlineTypeUnr = mainline_type::TypeF<
Box<mainline_type::Type>,
mainline_type::RecordRows,
mainline_type::EnumRows,
term::RichTerm,
>;

impl<'ast> FromMainline<'ast, MainlineTypeUnr> for TypeUnr<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, typ: &MainlineTypeUnr) -> Self {
typ.clone().map(
|typ| &*alloc.generic_arena.alloc((*typ).to_ast(alloc)),
|rrows| rrows.to_ast(alloc),
|erows| erows.to_ast(alloc),
|ctr| ctr.to_ast(alloc),
)
}
}

impl<'ast> FromMainline<'ast, mainline_type::RecordRows> for RecordRows<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, rrows: &mainline_type::RecordRows) -> Self {
RecordRows(rrows.0.to_ast(alloc))
}
}

impl<'ast> FromMainline<'ast, mainline_type::EnumRows> for EnumRows<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, erows: &mainline_type::EnumRows) -> Self {
EnumRows(erows.0.to_ast(alloc))
}
}

type MainlineEnumRowsUnr =
mainline_type::EnumRowsF<Box<mainline_type::Type>, Box<mainline_type::EnumRows>>;

impl<'ast> FromMainline<'ast, MainlineEnumRowsUnr> for EnumRowsUnr<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, erows: &MainlineEnumRowsUnr) -> Self {
erows.clone().map(
|typ| &*alloc.generic_arena.alloc((*typ).to_ast(alloc)),
|erows| &*alloc.generic_arena.alloc((*erows).to_ast(alloc)),
)
}
}

type MainlineRecordRowsUnr =
mainline_type::RecordRowsF<Box<mainline_type::Type>, Box<mainline_type::RecordRows>>;

impl<'ast> FromMainline<'ast, MainlineRecordRowsUnr> for RecordRowsUnr<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, rrows: &MainlineRecordRowsUnr) -> Self {
rrows.clone().map(
|typ| &*alloc.generic_arena.alloc((*typ).to_ast(alloc)),
|rrows| &*alloc.generic_arena.alloc((*rrows).to_ast(alloc)),
)
}
}

impl<'ast> FromMainline<'ast, term::Term> for &'ast Node<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, term: &term::Term) -> Self {
use term::Term;
Expand Down Expand Up @@ -346,7 +412,7 @@ impl<'ast> FromMainline<'ast, term::Term> for &'ast Node<'ast> {
}
Term::Import { path, format } => alloc.import(path.clone(), *format),
Term::ResolvedImport(_) => panic!("didn't expect a resolved import at parsing stage"),
Term::Type { typ, .. } => alloc.typ(typ.clone()),
Term::Type { typ, .. } => alloc.typ_move(typ.to_ast(alloc)),
Term::CustomContract(_) => panic!("didn't expect a custom contract at parsing stage"),
Term::ParseError(error) => alloc.parse_error(error.clone()),
Term::RuntimeError(_) => panic!("didn't expect a runtime error at parsing stage"),
Expand Down
63 changes: 45 additions & 18 deletions core/src/bytecode/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ use std::{ffi::OsString, rc};
use pattern::Pattern;
use record::Record;

use crate::{
cache::InputFormat, error::ParseError, identifier::LocIdent, position::TermPos, typ::Type,
};
use crate::{cache::InputFormat, error::ParseError, identifier::LocIdent, position::TermPos};

// For now, we reuse those types from the term module.
pub use crate::term::{Number, StrChunk};
Expand All @@ -27,9 +25,11 @@ pub mod compat;
pub mod pattern;
pub mod primop;
pub mod record;
pub mod typ;

use pattern::*;
use primop::PrimOp;
use typ::*;

/// A Nickel AST. Contains a root node and a span.
///
Expand Down Expand Up @@ -146,7 +146,7 @@ pub enum Node<'ast> {
/// A type in term position, such as in `let my_contract = Number -> Number in ...`.
///
/// During evaluation, this will get turned into a contract.
Type(&'ast Type),
Type(&'ast Type<'ast>),

/// A term that couldn't be parsed properly. Used by the LSP to handle partially valid
/// programs.
Expand Down Expand Up @@ -190,10 +190,10 @@ pub struct Match<'ast> {
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Annotation<'ast> {
/// The type annotation (using `:`).
pub typ: Option<Type>,
pub typ: Option<Type<'ast>>,

/// The contract annotations (using `|`).
pub contracts: &'ast [Type],
pub contracts: &'ast [Type<'ast>],
}

impl<'ast> Annotation<'ast> {
Expand All @@ -211,13 +211,14 @@ impl<'ast> Annotation<'ast> {
/// Returns a string representation of the contracts (without the static type annotation) as a
/// comma-separated list.
pub fn contracts_to_string(&self) -> Option<String> {
(!self.contracts.is_empty()).then(|| {
self.contracts
.iter()
.map(|typ| format!("{typ}"))
.collect::<Vec<_>>()
.join(",")
})
todo!("requires pretty printing first")
//(!self.contracts.is_empty()).then(|| {
// self.contracts
// .iter()
// .map(|typ| format!("{typ}"))
// .collect::<Vec<_>>()
// .join(",")
//})
}

/// Returns `true` if this annotation is empty, i.e. hold neither a type annotation nor
Expand All @@ -234,15 +235,14 @@ impl<'ast> Annotation<'ast> {
/// The most popular choice for arena is the `bumpalo` crate, which is a fast bump allocator that
/// can handle heterogeneous data. However, it doesn't support destructors, which is a problem
/// because some of the nodes in the AST owns heap allocated data and needs to be de-allocated
/// (`Number`, `Type` and parse errors currently).
/// (numbers and parse errors currently).
///
/// Another choice is `typed-arena` and derivatives, which do run destructors, but can only store
/// one type of values. As the number of types that need to be dropped is relatively small, we use
/// a general `bumpalo` arena by default, and specialized typed arenas for stuff that need to be
/// dropped.
pub struct AstAlloc {
generic_arena: Bump,
type_arena: typed_arena::Arena<Type>,
number_arena: typed_arena::Arena<Number>,
error_arena: typed_arena::Arena<ParseError>,
}
Expand All @@ -252,7 +252,6 @@ impl AstAlloc {
pub fn new() -> Self {
Self {
generic_arena: Bump::new(),
type_arena: typed_arena::Arena::new(),
number_arena: typed_arena::Arena::new(),
error_arena: typed_arena::Arena::new(),
}
Expand Down Expand Up @@ -409,11 +408,39 @@ impl AstAlloc {
self.generic_arena.alloc(Node::Import { path, format })
}

pub fn typ(&self, typ: Type) -> &Node<'_> {
let typ = self.type_arena.alloc(typ);
pub fn typ<'ast>(&'ast self, typ: TypeUnr<'ast>, pos: TermPos) -> &'ast Node<'ast> {
let type_full = self.generic_arena.alloc(Type { typ, pos });
self.generic_arena.alloc(Node::Type(type_full))
}

/// As opposed to [Self::typ], this method takes an already constructed type and move it into
/// the arena, instead of taking each constituent separately.
pub fn typ_move<'ast>(&'ast self, typ: Type<'ast>) -> &'ast Node<'ast> {
let typ = self.generic_arena.alloc(typ);
self.generic_arena.alloc(Node::Type(typ))
}

pub fn typ_unr_move<'ast>(&'ast self, typ: TypeUnr<'ast>, pos: TermPos) -> &'ast Node<'ast> {
let type_full = self.generic_arena.alloc(Type { typ, pos });
self.generic_arena.alloc(Node::Type(type_full))
}

pub fn types<'ast, I>(&'ast self, types: I) -> &'ast [Type<'ast>]
where
I: IntoIterator<Item = Type<'ast>>,
I::IntoIter: ExactSizeIterator,
{
self.generic_arena.alloc_slice_fill_iter(types)
}

pub fn enum_rows<'ast>(&'ast self, erows: EnumRowsUnr<'ast>) -> &'ast EnumRows<'ast> {
self.generic_arena.alloc(EnumRows(erows))
}

pub fn record_rows<'ast>(&'ast self, rrows: RecordRowsUnr<'ast>) -> &'ast RecordRows<'ast> {
self.generic_arena.alloc(RecordRows(rrows))
}

pub fn parse_error(&self, error: ParseError) -> &Node<'_> {
let error = self.error_arena.alloc(error);
self.generic_arena.alloc(Node::ParseError(error))
Expand Down
33 changes: 33 additions & 0 deletions core/src/bytecode/ast/typ.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Representation of Nickel types in the AST.

use super::{Ast, TermPos};
use crate::typ as mainline_typ;
pub use mainline_typ::{EnumRowF, EnumRowsF, RecordRowF, RecordRowsF, TypeF};

/// The recursive unrolling of a type, that is when we "peel off" the top-level layer to find the actual
/// structure represented by an instantiation of `TypeF`.
pub type TypeUnr<'ast> = TypeF<&'ast Type<'ast>, RecordRows<'ast>, EnumRows<'ast>, Ast<'ast>>;

/// The recursive unrolling of a enum rows.
pub type EnumRowsUnr<'ast> = EnumRowsF<&'ast Type<'ast>, &'ast EnumRows<'ast>>;

/// The recursive unrolling of record rows.
pub type RecordRowsUnr<'ast> = RecordRowsF<&'ast Type<'ast>, &'ast RecordRows<'ast>>;

/// Concrete, recursive definition for an enum row.
pub type EnumRow<'ast> = EnumRowF<&'ast Type<'ast>>;
/// Concrete, recursive definition for enum rows.
#[derive(Clone, PartialEq, Debug)]
pub struct EnumRows<'ast>(pub EnumRowsUnr<'ast>);
/// Concrete, recursive definition for a record row.
pub type RecordRow<'ast> = RecordRowF<&'ast Type<'ast>>;
#[derive(Clone, PartialEq, Debug)]
/// Concrete, recursive definition for record rows.
pub struct RecordRows<'ast>(pub RecordRowsUnr<'ast>);

/// Concrete, recursive type for a Nickel type.
#[derive(Clone, PartialEq, Debug)]
pub struct Type<'ast> {
pub typ: TypeUnr<'ast>,
pub pos: TermPos,
}

0 comments on commit 7de7b8e

Please sign in to comment.