Bläddra i källkod

Change bool => Bool, str => String

David Peter 2 år sedan
förälder
incheckning
3b90678ed3

+ 1 - 1
book/src/example-numbat_syntax.md

@@ -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
 

+ 3 - 3
examples/booleans.nbt

@@ -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)))

+ 1 - 1
examples/numbat_syntax.nbt

@@ -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
 

+ 1 - 1
numbat/modules/core/error.nbt

@@ -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

+ 6 - 6
numbat/modules/core/strings.nbt

@@ -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)
@@ -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 ""

+ 1 - 1
numbat/modules/units/currencies.nbt

@@ -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")

+ 2 - 2
numbat/src/ast.rs

@@ -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"),
         }
     }
 }

+ 2 - 2
numbat/src/keywords.rs

@@ -17,10 +17,10 @@ pub const KEYWORDS: &[&str] = &[
     "if",
     "then",
     "else",
-    "bool",
+    "Bool",
     "true",
     "false",
-    "str",
+    "String",
     // decorators
     "metric_prefixes",
     "binary_prefixes",

+ 2 - 2
numbat/src/parser.rs

@@ -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 ) ?
@@ -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(

+ 3 - 3
numbat/src/tokenizer.rs

@@ -92,7 +92,7 @@ pub enum TokenKind {
     Then,
     Else,
 
-    Str,
+    String,
 
     Long,
     Short,
@@ -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
         });

+ 8 - 8
numbat/src/typechecker.rs

@@ -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"),
@@ -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
         ));
     }
@@ -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!(
@@ -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!(
@@ -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(..)
         ));
     }

+ 4 - 4
numbat/src/typed_ast.rs

@@ -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"),
         }
     }
 }
@@ -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"),
         }
     }
 }

+ 1 - 1
numbat/src/value.rs

@@ -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");
         }
     }
 }