numbat_syntax.nbt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: Speed = 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) -> Speed = len / dur # Two parameters
  45. fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
  46. # 6. Dimension definitions
  47. dimension Fame # A new base dimension
  48. dimension Deceleration = Length / Time^2 # A new derived dimension
  49. # 7. Unit definitions
  50. @aliases(quorks) # Optional aliases-decorator
  51. unit quork = 0.35 meter # A new derived unit
  52. @metric_prefixes # Optional decorator to allow 'milliclonk', etc.
  53. @aliases(ck: short) # short aliases can be used with short prefixes (mck)
  54. unit clonk: Time = 0.2 seconds # Optional type annotation
  55. @metric_prefixes
  56. @aliases(wh: short)
  57. unit warhol: Fame # New base unit for the "Fame" dimension
  58. unit thing # New base unit with automatically generated
  59. # base dimension "Thing"
  60. # 8. Conditionals
  61. fn step(x: Scalar) -> Scalar = # The construct 'if <cond> then <expr> else <expr>'
  62. if x < 0 # is an expression, not a statement. It can span
  63. then 0 # multiple lines.
  64. else 1
  65. # 9. Procedures
  66. print(2 kilowarhol) # Print the value of an expression
  67. assert_eq(1 ft, 12 in) # Assert that two quantities are equal
  68. assert_eq(1 yd, 1 m, 10 cm) # Assert that two quantities are equal, up to
  69. # the given precision
  70. type(2 m/s) # Print the type of an expression