numbat_syntax.nbt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # 3. Simple expressions
  20. 3 + (4 - 3) # Addition and subtraction
  21. 1920 / 16 * 9 # Multiplication, division
  22. 1920 ÷ 16 × 9 # Unicode-style, '·' is also multiplication
  23. 2 pi # Whitespace is implicit multiplication
  24. meter per second # 'per' keyword can be used for division
  25. 2^3 # Exponentiation
  26. 2**3 # Python-style
  27. 2³ # Unicode exponents
  28. 2^-3 # Negative exponents
  29. mod(17, 4) # Modulo
  30. 3 in -> cm # Unit conversion, can also be → or ➞
  31. 3 in to cm # Unit conversion with the 'to' keyword
  32. cos(pi/3 + pi) # Call mathematical functions
  33. pi/3 + pi // cos # Same, 'arg // f' is equivalent to 'f(arg)'
  34. # The '//' operator has the lowest precedence
  35. # which makes it very useful for interactive
  36. # terminals (press up-arrow, and add '// f')
  37. # 4. Constants
  38. let n = 4 # Simple numerical constant
  39. let q1 = 2 m/s # Right hand side can be any expression
  40. let q2: Velocity = 2 m/s # With optional type annotation
  41. let q3: Length / Time = 2 m/s # more complex type annotation
  42. # 5. Function definitions
  43. fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
  44. fn speed(len: Length, dur: Time) -> Velocity = len / dur # Two parameters
  45. fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
  46. fn is_non_negative(x: Scalar) -> Bool = x ≥ 0 # Returns a bool
  47. # 6. Dimension definitions
  48. dimension Fame # A new base dimension
  49. dimension Deceleration = Length / Time^2 # A new derived dimension
  50. # 7. Unit definitions
  51. @aliases(quorks) # Optional aliases-decorator
  52. unit quork = 0.35 meter # A new derived unit
  53. @metric_prefixes # Optional decorator to allow 'milliclonk', etc.
  54. @aliases(ck: short) # short aliases can be used with short prefixes (mck)
  55. unit clonk: Time = 0.2 seconds # Optional type annotation
  56. @metric_prefixes
  57. @aliases(wh: short)
  58. unit warhol: Fame # New base unit for the "Fame" dimension
  59. unit thing # New base unit with automatically generated
  60. # base dimension "Thing"
  61. # 8. Conditionals
  62. fn bump(x: Scalar) -> Scalar = # The construct 'if <cond> then <expr> else <expr>'
  63. if x >= 0 && x <= 1 # is an expression, not a statement. It can span
  64. then 1 # multiple lines.
  65. else 0
  66. # 9. Procedures
  67. print(2 kilowarhol) # Print the value of an expression
  68. print("hello world") # Print a message
  69. print("value of pi = {pi}") # String interpolation
  70. print("sqrt(10) = {sqrt(10)}") # Expressions in string interpolation
  71. assert(1 yard < 1 meter) # Assertion
  72. assert_eq(1 ft, 12 in) # Assert that two quantities are equal
  73. assert_eq(1 yd, 1 m, 10 cm) # Assert that two quantities are equal, up to
  74. # the given precision
  75. type(2 m/s) # Print the type of an expression