From c8fe1c2fdb8d3124fbd03ea3ee399f8bfc76a516 Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Mon, 7 Oct 2024 15:19:03 +0200 Subject: [PATCH] Define untyped echo --- compiler-core/src/ast/untyped.rs | 6 ++++++ compiler-core/src/ast_folder.rs | 24 ++++++++++++++++++++++++ compiler-core/src/call_graph.rs | 9 +++++++++ compiler-core/src/format.rs | 12 ++++++++++++ compiler-core/src/type_/expression.rs | 23 +++++++++++++++++++++++ 5 files changed, 74 insertions(+) diff --git a/compiler-core/src/ast/untyped.rs b/compiler-core/src/ast/untyped.rs index f5407ffee98..2cea0218506 100644 --- a/compiler-core/src/ast/untyped.rs +++ b/compiler-core/src/ast/untyped.rs @@ -106,6 +106,11 @@ pub enum UntypedExpr { message: Option>, }, + Echo { + location: SrcSpan, + expression: Option>, + }, + BitArray { location: SrcSpan, segments: Vec, @@ -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, .. } diff --git a/compiler-core/src/ast_folder.rs b/compiler-core/src/ast_folder.rs index 1f731403dfd..07ddf7d4008 100644 --- a/compiler-core/src/ast_folder.rs +++ b/compiler-core/src/ast_folder.rs @@ -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), @@ -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, @@ -783,6 +796,17 @@ pub trait UntypedExprFolder: TypeAstFolder + UntypedConstantFolder + PatternFold } } + fn fold_echo( + &mut self, + location: SrcSpan, + expression: Option>, + ) -> UntypedExpr { + UntypedExpr::Echo { + location, + expression, + } + } + fn fold_panic(&mut self, location: SrcSpan, message: Option>) -> UntypedExpr { UntypedExpr::Panic { location, message } } diff --git a/compiler-core/src/call_graph.rs b/compiler-core/src/call_graph.rs index 91944ec1a3b..d61881e6e70 100644 --- a/compiler-core/src/call_graph.rs +++ b/compiler-core/src/call_graph.rs @@ -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); diff --git a/compiler-core/src/format.rs b/compiler-core/src/format.rs index 1c8e249d757..f5e1bf8f0be 100644 --- a/compiler-core/src/format.rs +++ b/compiler-core/src/format.rs @@ -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), @@ -1178,6 +1188,7 @@ impl<'comments> Formatter<'comments> { | UntypedExpr::Tuple { .. } | UntypedExpr::TupleIndex { .. } | UntypedExpr::Todo { .. } + | UntypedExpr::Echo { .. } | UntypedExpr::Panic { .. } | UntypedExpr::BitArray { .. } | UntypedExpr::RecordUpdate { .. } @@ -2446,6 +2457,7 @@ impl<'comments> Formatter<'comments> { | UntypedExpr::TupleIndex { .. } | UntypedExpr::Todo { .. } | UntypedExpr::Panic { .. } + | UntypedExpr::Echo { .. } | UntypedExpr::BitArray { .. } | UntypedExpr::RecordUpdate { .. } | UntypedExpr::NegateBool { .. } diff --git a/compiler-core/src/type_/expression.rs b/compiler-core/src/type_/expression.rs index 0785f72710e..56ac8c77220 100644 --- a/compiler-core/src/type_/expression.rs +++ b/compiler-core/src/type_/expression.rs @@ -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 { @@ -465,6 +470,24 @@ impl<'a, 'b> ExprTyper<'a, 'b> { }) } + fn infer_echo( + &mut self, + location: SrcSpan, + expression: Option>, + ) -> Result { + 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,