Skip to content

Commit

Permalink
Define untyped echo
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 7, 2024
1 parent 4aa05d0 commit c8fe1c2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions compiler-core/src/ast/untyped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ pub enum UntypedExpr {
message: Option<Box<Self>>,
},

Echo {
location: SrcSpan,
expression: Option<Box<Self>>,
},

BitArray {
location: SrcSpan,
segments: Vec<UntypedExprBitArraySegment>,
Expand Down Expand Up @@ -148,6 +153,7 @@ impl UntypedExpr {
| Self::Var { location, .. }
| Self::Int { location, .. }
| Self::Todo { location, .. }
| Self::Echo { location, .. }
| Self::Case { location, .. }
| Self::Call { location, .. }
| Self::List { location, .. }
Expand Down
24 changes: 24 additions & 0 deletions compiler-core/src/ast_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
message,
} => self.fold_todo(kind, location, message),

UntypedExpr::Echo {
location,
expression,
} => self.fold_echo(location, expression),

UntypedExpr::Panic { location, message } => self.fold_panic(location, message),

UntypedExpr::BitArray { location, segments } => self.fold_bit_array(location, segments),
Expand Down Expand Up @@ -357,6 +362,14 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
message: message.map(|msg_expr| Box::new(self.fold_expr(*msg_expr))),
},

UntypedExpr::Echo {
location,
expression,
} => UntypedExpr::Echo {
location,
expression: expression.map(|expression| Box::new(self.fold_expr(*expression))),
},

UntypedExpr::Block {
location,
statements,
Expand Down Expand Up @@ -783,6 +796,17 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold
}
}

fn fold_echo(
&mut self,
location: SrcSpan,
expression: Option<Box<UntypedExpr>>,
) -> UntypedExpr {
UntypedExpr::Echo {
location,
expression,
}
}

fn fold_panic(&mut self, location: SrcSpan, message: Option<Box<UntypedExpr>>) -> UntypedExpr {
UntypedExpr::Panic { location, message }
}
Expand Down
9 changes: 9 additions & 0 deletions compiler-core/src/call_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ impl<'a> CallGraphBuilder<'a> {
}
}

UntypedExpr::Echo {
expression,
location: _,
} => {
if let Some(expression) = expression {
self.expression(expression);
}
}

// Aha! A variable is being referenced.
UntypedExpr::Var { name, .. } => {
self.referenced(name);
Expand Down
12 changes: 12 additions & 0 deletions compiler-core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,16 @@ impl<'comments> Formatter<'comments> {
message: Some(l), ..
} => docvec!["todo as ", self.expr(l)],

UntypedExpr::Echo {
expression: None,
location: _,
} => "echo".to_doc(),

UntypedExpr::Echo {
expression: Some(e),
location: _,
} => docvec!["echo ", self.expr(e)],

UntypedExpr::PipeLine { expressions, .. } => self.pipeline(expressions, false),

UntypedExpr::Int { value, .. } => self.int(value),
Expand Down Expand Up @@ -1178,6 +1188,7 @@ impl<'comments> Formatter<'comments> {
| UntypedExpr::Tuple { .. }
| UntypedExpr::TupleIndex { .. }
| UntypedExpr::Todo { .. }
| UntypedExpr::Echo { .. }
| UntypedExpr::Panic { .. }
| UntypedExpr::BitArray { .. }
| UntypedExpr::RecordUpdate { .. }
Expand Down Expand Up @@ -2446,6 +2457,7 @@ impl<'comments> Formatter<'comments> {
| UntypedExpr::TupleIndex { .. }
| UntypedExpr::Todo { .. }
| UntypedExpr::Panic { .. }
| UntypedExpr::Echo { .. }
| UntypedExpr::BitArray { .. }
| UntypedExpr::RecordUpdate { .. }
| UntypedExpr::NegateBool { .. }
Expand Down
23 changes: 23 additions & 0 deletions compiler-core/src/type_/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location, message, ..
} => self.infer_panic(location, message),

UntypedExpr::Echo {
location,
expression,
} => self.infer_echo(location, expression),

UntypedExpr::Var { location, name, .. } => self.infer_var(name, location),

UntypedExpr::Int {
Expand Down Expand Up @@ -465,6 +470,24 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
})
}

fn infer_echo(
&mut self,
location: SrcSpan,
expression: Option<Box<UntypedExpr>>,
) -> Result<TypedExpr, Error> {
if let Some(expression) = expression {
let typed_expression = self.infer(*expression);
Ok(todo!("typed expr ECHO"))
} else {
todo!(
"
echo with no expression after it
should only be in a pipeline
"
)
}
}

pub(crate) fn warn_for_unreachable_code(
&mut self,
location: SrcSpan,
Expand Down

0 comments on commit c8fe1c2

Please sign in to comment.