numbat_syntax.nbt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # This is a line comment. It can span over
  2. # multiple lines
  3. # 1. Imports
  4. use prelude # This is not necessary. The 'prelude'
  5. # module will always be loaded upon startup
  6. use units::stoney # Load a specific module
  7. # 2. Numbers
  8. 12345 # integer notation
  9. 12_345 # optional decimal separators
  10. 0.234 # floating point notation
  11. .234 # without the leading zero
  12. 1.234e15 # scientific notation
  13. 1.234e+15
  14. 1e-9
  15. 1.0e-9
  16. 0x2A # hexadecimal
  17. 0o52 # octal
  18. 0b101010 # binary
  19. NaN # Not a number
  20. inf # Infinity
  21. # 3. Simple expressions
  22. 3 + (4 - 3) # Addition and subtraction
  23. 1920 / 16 * 9 # Multiplication, division
  24. 1920 ÷ 16 × 9 # Unicode-style, '·' is also multiplication
  25. 2 pi # Whitespace is implicit multiplication
  26. meter per second # 'per' keyword can be used for division
  27. 2^3 # Exponentiation
  28. 2**3 # Python-style
  29. 2³ # Unicode exponents
  30. 2^-3 # Negative exponents
  31. mod(17, 4) # Modulo
  32. 3 in -> cm # Unit conversion, can also be → or ➞
  33. 3 in to cm # Unit conversion with the 'to' keyword
  34. cos(pi/3 + pi) # Call mathematical functions
  35. pi/3 + pi |> cos # Same, 'arg |> f' is equivalent to 'f(arg)'
  36. # The '|>' operator has the lowest precedence
  37. # which makes it very useful for interactive
  38. # terminals (press up-arrow, and add '|> f')
  39. # 4. Constants
  40. let n = 4 # Simple numerical constant
  41. let q1 = 2 m/s # Right hand side can be any expression
  42. let q2: Velocity = 2 m/s # With optional type annotation
  43. let q3: Length / Time = 2 m/s # more complex type annotation
  44. # 5. Function definitions
  45. fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
  46. fn speed(len: Length, dur: Time) -> Velocity = len / dur # Two parameters
  47. fn my_sqrt<T: Dim>(q: T^2) -> T = q^(1/2) # A generic function
  48. fn is_non_negative(x: Scalar) -> Bool = x ≥ 0 # Returns a bool
  49. fn power_4(x: Scalar) = z # A function with local variables
  50. where y = x * x
  51. and z = y * y
  52. # 6. Dimension definitions
  53. dimension Fame # A new base dimension
  54. dimension Deceleration = Length / Time^2 # A new derived dimension
  55. # 7. Unit definitions
  56. @aliases(quorks) # Optional aliases-decorator
  57. unit quork = 0.35 meter # A new derived unit
  58. @metric_prefixes # Optional decorator to allow 'milliclonk', etc.
  59. @aliases(ck: short) # short aliases can be used with short prefixes (mck)
  60. unit clonk: Time = 0.2 seconds # Optional type annotation
  61. @metric_prefixes
  62. @aliases(wh: short)
  63. unit warhol: Fame # New base unit for the "Fame" dimension
  64. unit thing # New base unit with automatically generated
  65. # base dimension "Thing"
  66. # 8. Conditionals
  67. fn bump(x: Scalar) -> Scalar = # The construct 'if <cond> then <expr> else <expr>'
  68. if x >= 0 && x <= 1 # is an expression, not a statement. It can span
  69. then 1 # multiple lines.
  70. else 0
  71. # 9. Procedures
  72. print(2 kilowarhol) # Print the value of an expression
  73. print("hello world") # Print a message
  74. print("value of pi = {pi}") # String interpolation
  75. print("sqrt(10) = {sqrt(10)}") # Expressions in string interpolation
  76. print("value of π ≈ {π:.3}") # Format specifiers
  77. assert(1 yard < 1 meter) # Assertion
  78. assert_eq(1 ft, 12 in) # Assert that two quantities are equal
  79. assert_eq(1 yd, 1 m, 10 cm) # Assert that two quantities are equal, up to
  80. # the given precision
  81. type(2 m/s) # Print the type of an expression
  82. # 10. Structs
  83. struct Element { # Define a struct
  84. name: String,
  85. atomic_number: Scalar,
  86. density: MassDensity,
  87. }
  88. let hydrogen = Element { # Instantiate it
  89. name: "Hydrogen",
  90. atomic_number: 1,
  91. density: 0.08988 g/L,
  92. }
  93. hydrogen.density # Access the field of a struct