numbat_syntax.nbt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 physics::temperature_conversion # 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. mod(17, 4) # Modulo
  29. 3 in -> cm # Unit conversion, can also be → or ➞
  30. 3 in to cm # Unit conversion with the 'to' keyword
  31. cos(pi/3 + pi) # Call mathematical functions
  32. pi/3 + pi // cos # equivalent, useful in REPL
  33. # 4. Variable definitions
  34. let x = 4 # Simple numerical variable
  35. let y1 = 2 m/s # Right hand side can be any expression
  36. let y2: Speed = 2 m/s # With optional type annotation
  37. let y3: Length / Time = 2 m/s # more complex type annotation
  38. # 5. Function definitions
  39. fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
  40. fn speed(len: Length, dur: Time) -> Speed = len / dur # Two parameters
  41. fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
  42. # 6. Dimension definitions
  43. dimension Fame # A new base dimension
  44. dimension Deceleration = Length / Time^2 # A new derived dimension
  45. # 7. Unit definitions
  46. @aliases(quorks) # Optional aliases-decorator
  47. unit quork = 0.35 meter # A new derived unit
  48. @metric_prefixes # Optional decorator to allow 'milliclonk', etc.
  49. @aliases(ck: short) # short aliases can be used with short prefixes (mck)
  50. unit clonk: Time = 0.2 seconds # Optional type annotation
  51. @metric_prefixes
  52. @aliases(wh: short)
  53. unit warhol: Fame # New base unit for phys. dimension "Fame"
  54. unit thing # New base unit with automatically generated
  55. # base dimension "Thing"
  56. # 8. Procedures
  57. print(2 kilowarhol) # Print a quantity
  58. assert_eq(1 ft, 12 in) # Assert that two quantities are equal