Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 'Never' type (!) #420

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions numbat/modules/core/error.nbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# TODO: Ideally, this function should have a '-> !' return type such that
# it can be used everywhere. Not just instead of a Scalar.
fn error(message: String) -> 1
# Throw a user-defined error
fn error(message: String) -> !
3 changes: 2 additions & 1 deletion numbat/modules/core/strings.nbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::scalar
use core::functions
use core::error

fn str_length(s: String) -> Scalar

Expand Down Expand Up @@ -44,7 +45,7 @@ fn _hex_digit(x: Scalar) -> String =

fn _digit_in_base(x: Scalar, base: Scalar) -> String =
if base < 2 || base > 16
then "?" # TODO: better error handling once we can specify the return type of 'error(msg)' as '!' (see error.nbt).
then error("base must be between 2 and 16")
else if mod(x, 16) < 10 then chr(48 + mod(x, 16)) else chr(97 + mod(x, 16) - 10)

fn _number_in_base(x: Scalar, b: Scalar) -> String =
Expand Down
4 changes: 4 additions & 0 deletions numbat/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub(crate) use scalar;

#[derive(Debug, Clone, PartialEq)]
pub enum TypeAnnotation {
Never(Span),
DimensionExpression(DimensionExpression),
Bool(Span),
String(Span),
Expand All @@ -227,6 +228,7 @@ pub enum TypeAnnotation {
impl TypeAnnotation {
pub fn full_span(&self) -> Span {
match self {
TypeAnnotation::Never(span) => *span,
TypeAnnotation::DimensionExpression(d) => d.full_span(),
TypeAnnotation::Bool(span) => *span,
TypeAnnotation::String(span) => *span,
Expand All @@ -239,6 +241,7 @@ impl TypeAnnotation {
impl PrettyPrint for TypeAnnotation {
fn pretty_print(&self) -> Markup {
match self {
TypeAnnotation::Never(_) => m::type_identifier("!"),
TypeAnnotation::DimensionExpression(d) => d.pretty_print(),
TypeAnnotation::Bool(_) => m::type_identifier("Bool"),
TypeAnnotation::String(_) => m::type_identifier("String"),
Expand Down Expand Up @@ -384,6 +387,7 @@ pub trait ReplaceSpans {
impl ReplaceSpans for TypeAnnotation {
fn replace_spans(&self) -> Self {
match self {
TypeAnnotation::Never(_) => TypeAnnotation::Never(Span::dummy()),
TypeAnnotation::DimensionExpression(d) => {
TypeAnnotation::DimensionExpression(d.replace_spans())
}
Expand Down
4 changes: 3 additions & 1 deletion numbat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,9 @@ impl<'a> Parser<'a> {
}

fn type_annotation(&mut self) -> Result<TypeAnnotation> {
if let Some(token) = self.match_exact(TokenKind::Bool) {
if let Some(token) = self.match_exact(TokenKind::ExclamationMark) {
Ok(TypeAnnotation::Never(token.span))
} else if let Some(token) = self.match_exact(TokenKind::Bool) {
Ok(TypeAnnotation::Bool(token.span))
} else if let Some(token) = self.match_exact(TokenKind::String) {
Ok(TypeAnnotation::String(token.span))
Expand Down
23 changes: 21 additions & 2 deletions numbat/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ impl TypeChecker {
let checked_expr = self.check_expression(expr)?;
let type_ = checked_expr.get_type();
match (&type_, op) {
(Type::Never, _) => {}
(Type::Dimension(dtype), ast::UnaryOperator::Factorial) => {
if !dtype.is_scalar() {
return Err(TypeCheckError::NonScalarFactorialArgument(
Expand Down Expand Up @@ -730,7 +731,15 @@ impl TypeChecker {
let lhs_checked = self.check_expression(lhs)?;
let rhs_checked = self.check_expression(rhs)?;

if let Type::Fn(parameter_types, return_type) = rhs_checked.get_type() {
if lhs_checked.get_type().is_never() || rhs_checked.get_type().is_never() {
return Ok(typed_ast::Expression::BinaryOperator(
*span_op,
*op,
Box::new(lhs_checked),
Box::new(rhs_checked),
Type::Never,
));
} else if let Type::Fn(parameter_types, return_type) = rhs_checked.get_type() {
// make sure that there is just one paramter (return arity error otherwise)
if parameter_types.len() != 1 {
return Err(TypeCheckError::WrongArity {
Expand Down Expand Up @@ -1046,7 +1055,16 @@ impl TypeChecker {
let then_type = then.get_type();
let else_type = else_.get_type();

if then_type != else_type {
if then_type.is_never() || else_type.is_never() {
// This case is fine. We use the type of the *other* branch in those cases.
// For example:
//
// if <some precondition>
// then X
// else error("please make sure <some precondition> is met")
//
// Here, we simply use the type of `X` as the type of the whole expression.
} else if then_type != else_type {
return Err(TypeCheckError::IncompatibleTypesInCondition(
*span,
then_type,
Expand Down Expand Up @@ -1610,6 +1628,7 @@ impl TypeChecker {

fn type_from_annotation(&self, annotation: &TypeAnnotation) -> Result<Type> {
match annotation {
TypeAnnotation::Never(_) => Ok(Type::Never),
TypeAnnotation::DimensionExpression(dexpr) => self
.registry
.get_base_representation(dexpr)
Expand Down
15 changes: 14 additions & 1 deletion numbat/src/typed_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl DType {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
Never,
Dimension(DType),
Boolean,
String,
Expand All @@ -64,6 +65,7 @@ pub enum Type {
impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Never => write!(f, "!"),
Type::Dimension(d) => d.fmt(f),
Type::Boolean => write!(f, "Bool"),
Type::String => write!(f, "String"),
Expand All @@ -82,6 +84,7 @@ impl std::fmt::Display for Type {
impl PrettyPrint for Type {
fn pretty_print(&self) -> Markup {
match self {
Type::Never => m::keyword("!"),
Type::Dimension(d) => d.pretty_print(),
Type::Boolean => m::keyword("Bool"),
Type::String => m::keyword("String"),
Expand Down Expand Up @@ -117,6 +120,10 @@ impl Type {
Type::Dimension(DType::unity())
}

pub fn is_never(&self) -> bool {
matches!(self, Type::Never)
}

pub fn is_dtype(&self) -> bool {
matches!(self, Type::Dimension(..))
}
Expand Down Expand Up @@ -276,7 +283,13 @@ impl Expression {
Expression::FunctionCall(_, _, _, _, type_) => type_.clone(),
Expression::CallableCall(_, _, _, type_) => type_.clone(),
Expression::Boolean(_, _) => Type::Boolean,
Expression::Condition(_, _, then, _) => then.get_type(),
Expression::Condition(_, _, then_, else_) => {
if then_.get_type().is_never() {
else_.get_type()
} else {
then_.get_type()
}
}
Expression::String(_, _) => Type::String,
}
}
Expand Down
11 changes: 11 additions & 0 deletions numbat/tests/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,14 @@ fn test_datetime_runtime_errors() {
"Error in datetime format",
)
}

#[test]
fn test_user_errors() {
expect_failure("error(\"test\")", "User error: test");

// Make sure that the never type (!) can be used in all contexts
expect_failure("- error(\"test\")", "User error: test");
expect_failure("1 + error(\"test\")", "User error: test");
expect_failure("1 m + error(\"test\")", "User error: test");
expect_failure("if 3 < 2 then 2 m else error(\"test\")", "User error: test");
}
Loading