Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Oct 25, 2024
1 parent 97c05b1 commit f1ac07a
Showing 1 changed file with 49 additions and 49 deletions.
98 changes: 49 additions & 49 deletions core/src/bytecode/ast/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,25 +615,25 @@ impl From<&term::NAryOp> for PrimOp {
/// value. We initially used `From` directly, but this causes annoying inference issue around auto
/// deref and blanket implementations of `From`/`Into`. It's just simpler and more explicit to have
/// a separate trait for this conversion as well.
pub trait FromAst<'a, T> {
fn from_ast(ast: &'a T) -> Self;
pub trait FromAst<T> {
fn from_ast(ast: &T) -> Self;
}

pub trait ToMainline<'a, T> {
fn to_mainline(&'a self) -> T;
pub trait ToMainline<T> {
fn to_mainline(&self) -> T;
}

impl<'a, S, T> ToMainline<'a, T> for S
impl<S, T> ToMainline<T> for S
where
T: FromAst<'a, S>,
T: FromAst<S>,
{
fn to_mainline(&'a self) -> T {
fn to_mainline(&self) -> T {
T::from_ast(self)
}
}

impl<'ast, 'a> FromAst<'a, Pattern<'ast>> for mline_pat::Pattern {
fn from_ast(pattern: &'a Pattern<'ast>) -> Self {
impl<'ast> FromAst<Pattern<'ast>> for mline_pat::Pattern {
fn from_ast(pattern: &Pattern<'ast>) -> Self {
mline_pat::Pattern {
data: (&pattern.data).to_mainline(),
alias: pattern.alias,
Expand All @@ -642,8 +642,8 @@ impl<'ast, 'a> FromAst<'a, Pattern<'ast>> for mline_pat::Pattern {
}
}

impl<'ast, 'a> FromAst<'a, PatternData<'ast>> for mline_pat::PatternData {
fn from_ast(ast: &'a PatternData<'ast>) -> Self {
impl<'ast> FromAst<PatternData<'ast>> for mline_pat::PatternData {
fn from_ast(ast: &PatternData<'ast>) -> Self {
match ast {
PatternData::Wildcard => mline_pat::PatternData::Wildcard,
PatternData::Any(id) => mline_pat::PatternData::Any(*id),
Expand All @@ -656,8 +656,8 @@ impl<'ast, 'a> FromAst<'a, PatternData<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, RecordPattern<'ast>> for mline_pat::PatternData {
fn from_ast(record_pat: &'a RecordPattern<'ast>) -> Self {
impl<'ast> FromAst<RecordPattern<'ast>> for mline_pat::PatternData {
fn from_ast(record_pat: &RecordPattern<'ast>) -> Self {
let patterns = record_pat
.patterns
.iter()
Expand All @@ -678,8 +678,8 @@ impl<'ast, 'a> FromAst<'a, RecordPattern<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, FieldPattern<'ast>> for mline_pat::FieldPattern {
fn from_ast(field_pat: &'a FieldPattern<'ast>) -> Self {
impl<'ast> FromAst<FieldPattern<'ast>> for mline_pat::FieldPattern {
fn from_ast(field_pat: &FieldPattern<'ast>) -> Self {
let pattern = (&field_pat.pattern).to_mainline();

let default = field_pat.default.as_ref().map(|term| term.to_mainline());
Expand All @@ -696,8 +696,8 @@ impl<'ast, 'a> FromAst<'a, FieldPattern<'ast>> for mline_pat::FieldPattern {
}
}

impl<'ast, 'a> FromAst<'a, ArrayPattern<'ast>> for mline_pat::PatternData {
fn from_ast(array_pat: &'a ArrayPattern<'ast>) -> Self {
impl<'ast> FromAst<ArrayPattern<'ast>> for mline_pat::PatternData {
fn from_ast(array_pat: &ArrayPattern<'ast>) -> Self {
let patterns = array_pat
.patterns
.iter()
Expand All @@ -718,8 +718,8 @@ impl<'ast, 'a> FromAst<'a, ArrayPattern<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, EnumPattern<'ast>> for mline_pat::PatternData {
fn from_ast(enum_pat: &'a EnumPattern<'ast>) -> Self {
impl<'ast> FromAst<EnumPattern<'ast>> for mline_pat::PatternData {
fn from_ast(enum_pat: &EnumPattern<'ast>) -> Self {
let pattern = enum_pat
.pattern
.as_ref()
Expand All @@ -733,8 +733,8 @@ impl<'ast, 'a> FromAst<'a, EnumPattern<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, ConstantPattern<'ast>> for mline_pat::PatternData {
fn from_ast(pattern: &'a ConstantPattern<'ast>) -> Self {
impl<'ast> FromAst<ConstantPattern<'ast>> for mline_pat::PatternData {
fn from_ast(pattern: &ConstantPattern<'ast>) -> Self {
let data = match pattern.data {
ConstantPatternData::Bool(b) => mline_pat::ConstantPatternData::Bool(b),
ConstantPatternData::Number(n) => mline_pat::ConstantPatternData::Number(n.clone()),
Expand All @@ -749,8 +749,8 @@ impl<'ast, 'a> FromAst<'a, ConstantPattern<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, OrPattern<'ast>> for mline_pat::PatternData {
fn from_ast(pattern: &'a OrPattern<'ast>) -> Self {
impl<'ast> FromAst<OrPattern<'ast>> for mline_pat::PatternData {
fn from_ast(pattern: &OrPattern<'ast>) -> Self {
let patterns = pattern
.patterns
.iter()
Expand All @@ -764,8 +764,8 @@ impl<'ast, 'a> FromAst<'a, OrPattern<'ast>> for mline_pat::PatternData {
}
}

impl<'ast, 'a> FromAst<'a, Annotation<'ast>> for term::TypeAnnotation {
fn from_ast(annot: &'a Annotation<'ast>) -> Self {
impl<'ast> FromAst<Annotation<'ast>> for term::TypeAnnotation {
fn from_ast(annot: &Annotation<'ast>) -> Self {
let typ = annot.typ.as_ref().map(ToMainline::to_mainline);

let contracts = annot
Expand All @@ -778,8 +778,8 @@ impl<'ast, 'a> FromAst<'a, Annotation<'ast>> for term::TypeAnnotation {
}
}

impl<'ast, 'a> FromAst<'a, record::Field<'ast>> for term::record::Field {
fn from_ast(field: &'a record::Field<'ast>) -> Self {
impl<'ast> FromAst<record::Field<'ast>> for term::record::Field {
fn from_ast(field: &record::Field<'ast>) -> Self {
term::record::Field {
value: field.value.as_ref().map(|term| term.to_mainline()),
metadata: field.metadata.to_mainline(),
Expand All @@ -788,8 +788,8 @@ impl<'ast, 'a> FromAst<'a, record::Field<'ast>> for term::record::Field {
}
}

impl<'ast, 'a> FromAst<'a, record::FieldMetadata<'ast>> for term::record::FieldMetadata {
fn from_ast(metadata: &'a record::FieldMetadata<'ast>) -> Self {
impl<'ast> FromAst<record::FieldMetadata<'ast>> for term::record::FieldMetadata {
fn from_ast(metadata: &record::FieldMetadata<'ast>) -> Self {
let doc = metadata.doc.as_ref().map(|doc| String::from(&**doc));

term::record::FieldMetadata {
Expand All @@ -802,17 +802,17 @@ impl<'ast, 'a> FromAst<'a, record::FieldMetadata<'ast>> for term::record::FieldM
}
}

impl<'ast, 'a> FromAst<'a, Type<'ast>> for mline_type::Type {
fn from_ast(typ: &'a Type<'ast>) -> Self {
impl<'ast> FromAst<Type<'ast>> for mline_type::Type {
fn from_ast(typ: &Type<'ast>) -> Self {
mline_type::Type {
typ: typ.typ.to_mainline(),
pos: typ.pos,
}
}
}

impl<'ast, 'a> FromAst<'a, TypeUnr<'ast>> for MainlineTypeUnr {
fn from_ast(typ: &'a TypeUnr<'ast>) -> Self {
impl<'ast> FromAst<TypeUnr<'ast>> for MainlineTypeUnr {
fn from_ast(typ: &TypeUnr<'ast>) -> Self {
typ.clone().map(
|typ| Box::new(typ.to_mainline()),
|rrows| rrows.to_mainline(),
Expand All @@ -822,38 +822,38 @@ impl<'ast, 'a> FromAst<'a, TypeUnr<'ast>> for MainlineTypeUnr {
}
}

impl<'ast, 'a> FromAst<'a, RecordRows<'ast>> for mline_type::RecordRows {
fn from_ast(rrows: &'a RecordRows<'ast>) -> Self {
impl<'ast> FromAst<RecordRows<'ast>> for mline_type::RecordRows {
fn from_ast(rrows: &RecordRows<'ast>) -> Self {
mline_type::RecordRows(rrows.0.to_mainline())
}
}

impl<'ast, 'a> FromAst<'a, EnumRows<'ast>> for mline_type::EnumRows {
fn from_ast(erows: &'a EnumRows<'ast>) -> Self {
impl<'ast> FromAst<EnumRows<'ast>> for mline_type::EnumRows {
fn from_ast(erows: &EnumRows<'ast>) -> Self {
mline_type::EnumRows(erows.0.to_mainline())
}
}

impl<'ast, 'a> FromAst<'a, EnumRowsUnr<'ast>> for MainlineEnumRowsUnr {
fn from_ast(erows: &'a EnumRowsUnr<'ast>) -> Self {
impl<'ast> FromAst<EnumRowsUnr<'ast>> for MainlineEnumRowsUnr {
fn from_ast(erows: &EnumRowsUnr<'ast>) -> Self {
erows.clone().map(
|typ| Box::new(typ.to_mainline()),
|erows| Box::new(erows.to_mainline()),
)
}
}

impl<'ast, 'a> FromAst<'a, RecordRowsUnr<'ast>> for MainlineRecordRowsUnr {
fn from_ast(rrows: &'a RecordRowsUnr<'ast>) -> Self {
impl<'ast> FromAst<RecordRowsUnr<'ast>> for MainlineRecordRowsUnr {
fn from_ast(rrows: &RecordRowsUnr<'ast>) -> Self {
rrows.clone().map(
|typ| Box::new(typ.to_mainline()),
|rrows| Box::new(rrows.to_mainline()),
)
}
}

impl<'ast, 'a> FromAst<'a, Type<'ast>> for term::LabeledType {
fn from_ast(typ: &'a Type<'ast>) -> Self {
impl<'ast> FromAst<Type<'ast>> for term::LabeledType {
fn from_ast(typ: &Type<'ast>) -> Self {
let typ: mline_type::Type = typ.to_mainline();
// We expect the new AST node to always have a position set. In fact we should
// probably switch to `RawSpan` instead of `TermPos` everywhere; but let's do that
Expand All @@ -871,8 +871,8 @@ impl<'ast, 'a> FromAst<'a, Type<'ast>> for term::LabeledType {
}
}

impl<'ast, 'a> FromAst<'a, MatchBranch<'ast>> for term::MatchBranch {
fn from_ast(branch: &'a MatchBranch<'ast>) -> Self {
impl<'ast> FromAst<MatchBranch<'ast>> for term::MatchBranch {
fn from_ast(branch: &MatchBranch<'ast>) -> Self {
term::MatchBranch {
pattern: branch.pattern.to_mainline(),
guard: branch.guard.map(|ast| ast.to_mainline()),
Expand All @@ -881,8 +881,8 @@ impl<'ast, 'a> FromAst<'a, MatchBranch<'ast>> for term::MatchBranch {
}
}

impl<'ast, 'a> FromAst<'a, Node<'ast>> for term::Term {
fn from_ast(node: &'a Node<'ast>) -> Self {
impl<'ast> FromAst<Node<'ast>> for term::Term {
fn from_ast(node: &Node<'ast>) -> Self {
use term::Term;

match node {
Expand Down Expand Up @@ -1023,8 +1023,8 @@ impl<'ast, 'a> FromAst<'a, Node<'ast>> for term::Term {
}
}

impl<'ast, 'a> FromAst<'a, Ast<'ast>> for term::RichTerm {
fn from_ast(ast: &'a Ast<'ast>) -> Self {
impl<'ast> FromAst<Ast<'ast>> for term::RichTerm {
fn from_ast(ast: &Ast<'ast>) -> Self {
term::RichTerm::new(ast.node.to_mainline(), ast.pos.clone())
}
}

0 comments on commit f1ac07a

Please sign in to comment.