Skip to content

Commit

Permalink
Change bool => Bool, str => String
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp authored and David Peter committed Oct 8, 2023
1 parent 3e65abf commit 3b90678
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion book/src/example-numbat_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ let q3: Length / Time = 2 m/s # more complex type annotation
fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
fn speed(len: Length, dur: Time) -> Speed = len / dur # Two parameters
fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
fn is_non_negative(x: Scalar) -> bool = x ≥ 0 # Returns a bool
fn is_non_negative(x: Scalar) -> Bool = x ≥ 0 # Returns a bool
# 6. Dimension definitions
Expand Down
6 changes: 3 additions & 3 deletions examples/booleans.nbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn not(a: bool) = if a then false else true
fn and(a: bool, b: bool) = if a then b else false
fn or(a: bool, b: bool) = if a then true else b
fn not(a: Bool) = if a then false else true
fn and(a: Bool, b: Bool) = if a then b else false
fn or(a: Bool, b: Bool) = if a then true else b

assert(not(false))
assert(not(not(true)))
Expand Down
2 changes: 1 addition & 1 deletion examples/numbat_syntax.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let q3: Length / Time = 2 m/s # more complex type annotation
fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
fn speed(len: Length, dur: Time) -> Speed = len / dur # Two parameters
fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
fn is_non_negative(x: Scalar) -> bool = x ≥ 0 # Returns a bool
fn is_non_negative(x: Scalar) -> Bool = x ≥ 0 # Returns a bool

# 6. Dimension definitions

Expand Down
2 changes: 1 addition & 1 deletion numbat/modules/core/error.nbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 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: str) -> 1
fn error(message: String) -> 1
12 changes: 6 additions & 6 deletions numbat/modules/core/strings.nbt
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use core::scalar

fn str_length(s: str) -> Scalar
fn str_length(s: String) -> Scalar

fn str_slice(s: str, start: Scalar, end: Scalar) -> str
fn str_slice(s: String, start: Scalar, end: Scalar) -> String

fn str_append(a: str, b: str) -> str = "{a}{b}"
fn str_append(a: String, b: String) -> String = "{a}{b}"

fn str_contains(haystack: str, needle: str) -> bool =
fn str_contains(haystack: String, needle: String) -> Bool =
if str_length(haystack) == 0
then false
else if str_slice(haystack, 0, str_length(needle)) == needle
then true
else str_contains(str_slice(haystack, 1, str_length(haystack)), needle)

fn str_replace(s: str, pattern: str, replacement: str) -> str =
fn str_replace(s: String, pattern: String, replacement: String) -> String =
if pattern == ""
then s
else if str_contains(s, pattern)
Expand All @@ -22,7 +22,7 @@ fn str_replace(s: str, pattern: str, replacement: str) -> str =
else str_append(str_slice(s, 0, 1), str_replace(str_slice(s, 1, str_length(s)), pattern, replacement))
else s

fn str_repeat(a: str, n: Scalar) -> str =
fn str_repeat(a: String, n: Scalar) -> String =
if n > 0
then str_append(a, str_repeat(a, n - 1))
else ""
2 changes: 1 addition & 1 deletion numbat/modules/units/currencies.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use units::currency
# the identifiers below is. For the Web version, we asynchronously load exchange rates and then
# pull in this module.

fn exchange_rate(currency: str) -> Scalar
fn exchange_rate(currency: String) -> Scalar

@aliases(dollars, USD, $: short)
unit dollar: Money = EUR / exchange_rate("USD")
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ impl PrettyPrint for TypeAnnotation {
fn pretty_print(&self) -> Markup {
match self {
TypeAnnotation::DimensionExpression(d) => d.pretty_print(),
TypeAnnotation::Bool(_) => m::type_identifier("bool"),
TypeAnnotation::String(_) => m::type_identifier("str"),
TypeAnnotation::Bool(_) => m::type_identifier("Bool"),
TypeAnnotation::String(_) => m::type_identifier("String"),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub const KEYWORDS: &[&str] = &[
"if",
"then",
"else",
"bool",
"Bool",
"true",
"false",
"str",
"String",
// decorators
"metric_prefixes",
"binary_prefixes",
Expand Down
4 changes: 2 additions & 2 deletions numbat/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//!
//! decorator ::= "@" ( "metric_prefixes" | "binary_prefixes" | ( "aliases(" list_of_aliases ")" ) )
//!
//! type_annotation ::= "bool" | "str" | dimension_expr
//! type_annotation ::= "Bool" | "String" | dimension_expr
//! dimension_expr ::= dim_factor
//! dim_factor ::= dim_power ( (multiply | divide) dim_power ) *
//! dim_power ::= dim_primary ( power dim_exponent | unicode_exponent ) ?
Expand Down Expand Up @@ -1152,7 +1152,7 @@ impl<'a> Parser<'a> {
fn type_annotation(&mut self) -> Result<TypeAnnotation> {
if let Some(token) = self.match_exact(TokenKind::Bool) {
Ok(TypeAnnotation::Bool(token.span))
} else if let Some(token) = self.match_exact(TokenKind::Str) {
} else if let Some(token) = self.match_exact(TokenKind::String) {
Ok(TypeAnnotation::String(token.span))
} else {
Ok(TypeAnnotation::DimensionExpression(
Expand Down
6 changes: 3 additions & 3 deletions numbat/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub enum TokenKind {
Then,
Else,

Str,
String,

Long,
Short,
Expand Down Expand Up @@ -336,13 +336,13 @@ impl Tokenizer {
m.insert("assert", TokenKind::ProcedureAssert);
m.insert("assert_eq", TokenKind::ProcedureAssertEq);
m.insert("type", TokenKind::ProcedureType);
m.insert("bool", TokenKind::Bool);
m.insert("Bool", TokenKind::Bool);
m.insert("true", TokenKind::True);
m.insert("false", TokenKind::False);
m.insert("if", TokenKind::If);
m.insert("then", TokenKind::Then);
m.insert("else", TokenKind::Else);
m.insert("str", TokenKind::Str);
m.insert("String", TokenKind::String);
// Keep this list in sync with keywords::KEYWORDS!
m
});
Expand Down
16 changes: 8 additions & 8 deletions numbat/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,8 +1471,8 @@ mod tests {

assert_successful_typecheck("let x: A = c / b");

assert_successful_typecheck("let x: bool = true");
assert_successful_typecheck("let x: str = \"hello\"");
assert_successful_typecheck("let x: Bool = true");
assert_successful_typecheck("let x: String = \"hello\"");

assert!(matches!(
get_typecheck_error("let x: A = b"),
Expand All @@ -1487,11 +1487,11 @@ mod tests {
TypeCheckError::IncompatibleTypesInAnnotation(_, _, annotated_type, _, actual_type, _) if annotated_type == Type::Dimension(type_a()) && actual_type == Type::String
));
assert!(matches!(
get_typecheck_error("let x: bool = a"),
get_typecheck_error("let x: Bool = a"),
TypeCheckError::IncompatibleTypesInAnnotation(_, _, annotated_type, _, actual_type, _) if annotated_type == Type::Boolean && actual_type == Type::Dimension(type_a())
));
assert!(matches!(
get_typecheck_error("let x: str = true"),
get_typecheck_error("let x: String = true"),
TypeCheckError::IncompatibleTypesInAnnotation(_, _, annotated_type, _, actual_type, _) if annotated_type == Type::String && actual_type == Type::Boolean
));
}
Expand Down Expand Up @@ -1760,7 +1760,7 @@ mod tests {
#[test]
fn non_dtype_return_types() {
assert!(matches!(
get_typecheck_error("fn f() -> str = 1"),
get_typecheck_error("fn f() -> String = 1"),
TypeCheckError::IncompatibleTypesInAnnotation(..)
));
assert!(matches!(
Expand All @@ -1769,7 +1769,7 @@ mod tests {
));

assert!(matches!(
get_typecheck_error("fn f() -> bool = 1"),
get_typecheck_error("fn f() -> Bool = 1"),
TypeCheckError::IncompatibleTypesInAnnotation(..)
));
assert!(matches!(
Expand All @@ -1778,11 +1778,11 @@ mod tests {
));

assert!(matches!(
get_typecheck_error("fn f() -> str = true"),
get_typecheck_error("fn f() -> String = true"),
TypeCheckError::IncompatibleTypesInAnnotation(..)
));
assert!(matches!(
get_typecheck_error("fn f() -> bool = \"test\""),
get_typecheck_error("fn f() -> Bool = \"test\""),
TypeCheckError::IncompatibleTypesInAnnotation(..)
));
}
Expand Down
8 changes: 4 additions & 4 deletions numbat/src/typed_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ impl std::fmt::Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Dimension(d) => d.fmt(f),
Type::Boolean => write!(f, "bool"),
Type::String => write!(f, "str"),
Type::Boolean => write!(f, "Bool"),
Type::String => write!(f, "String"),
}
}
}
Expand All @@ -61,8 +61,8 @@ impl PrettyPrint for Type {
fn pretty_print(&self) -> Markup {
match self {
Type::Dimension(d) => d.pretty_print(),
Type::Boolean => m::keyword("bool"),
Type::String => m::keyword("str"),
Type::Boolean => m::keyword("Bool"),
Type::String => m::keyword("String"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion numbat/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Value {
if let Value::String(s) = self {
s
} else {
panic!("Expected value to be a bool");
panic!("Expected value to be a string");
}
}
}
Expand Down

0 comments on commit 3b90678

Please sign in to comment.