numbat_syntax.nbt 4.3 KB

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