Add TraceIfFalse untyped expression

The goal is to handle this without bothering the code generation down the line. That is, we can handle it when transforming from the untyped AST to the typed one. That's why there's no 'TraceIfFalse' constructor in the typed AST. It has disappeared during type-check.
This commit is contained in:
KtorZ 2023-02-16 13:50:50 +01:00 committed by Lucas
parent 6ce62115f7
commit 60390fe4f0
3 changed files with 18 additions and 1 deletions

View File

@ -382,6 +382,11 @@ pub enum UntypedExpr {
text: Box<Self>, text: Box<Self>,
}, },
TraceIfFalse {
location: Span,
value: Box<Self>,
},
When { When {
location: Span, location: Span,
subjects: Vec<Self>, subjects: Vec<Self>,
@ -510,7 +515,8 @@ impl UntypedExpr {
match self { match self {
Self::PipeLine { expressions, .. } => expressions.last().location(), Self::PipeLine { expressions, .. } => expressions.last().location(),
Self::Trace { then, .. } => then.location(), Self::Trace { then, .. } => then.location(),
Self::Fn { location, .. } Self::TraceIfFalse { location, .. }
| Self::Fn { location, .. }
| Self::Var { location, .. } | Self::Var { location, .. }
| Self::Int { location, .. } | Self::Int { location, .. }
| Self::ErrorTerm { location, .. } | Self::ErrorTerm { location, .. }

View File

@ -800,6 +800,8 @@ impl<'comments> Formatter<'comments> {
} }
UntypedExpr::ErrorTerm { .. } => "error".to_doc(), UntypedExpr::ErrorTerm { .. } => "error".to_doc(),
UntypedExpr::TraceIfFalse { value, .. } => self.trace_if_false(value),
}; };
commented(document, comments) commented(document, comments)
@ -814,6 +816,10 @@ impl<'comments> Formatter<'comments> {
} }
} }
pub fn trace_if_false<'a>(&mut self, value: &'a UntypedExpr) -> Document<'a> {
docvec![self.wrap_unary_op(value), "?"]
}
pub fn trace<'a>( pub fn trace<'a>(
&mut self, &mut self,
kind: &'a TraceKind, kind: &'a TraceKind,

View File

@ -225,6 +225,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
| UntypedExpr::TupleIndex { .. } | UntypedExpr::TupleIndex { .. }
| UntypedExpr::UnOp { .. } | UntypedExpr::UnOp { .. }
| UntypedExpr::Var { .. } | UntypedExpr::Var { .. }
| UntypedExpr::TraceIfFalse { .. }
| UntypedExpr::When { .. } => Ok(()), | UntypedExpr::When { .. } => Ok(()),
} }
} }
@ -361,6 +362,10 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
value, value,
op, op,
} => self.infer_un_op(location, value, op), } => self.infer_un_op(location, value, op),
UntypedExpr::TraceIfFalse { value, location } => {
self.infer_trace_if_false(*value, location)
}
} }
} }