| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- # This is a line comment. It can span over
- # multiple lines
- # 1. Imports
- use prelude # This is not necessary. The 'prelude'
- # module will always be loaded upon startup
- use units::stoney # Load a specific module
- # 2. Numbers
- 12345 # integer notation
- 12_345 # optional decimal separators
- 0.234 # floating point notation
- .234 # without the leading zero
- 1.234e15 # scientific notation
- 1.234e+15
- 1e-9
- 1.0e-9
- 0x2A # hexadecimal
- 0o52 # octal
- 0b101010 # binary
- # 3. Simple expressions
- 3 + (4 - 3) # Addition and subtraction
- 1920 / 16 * 9 # Multiplication, division
- 1920 ÷ 16 × 9 # Unicode-style, '·' is also multiplication
- 2 pi # Whitespace is implicit multiplication
- meter per second # 'per' keyword can be used for division
- 2^3 # Exponentiation
- 2**3 # Python-style
- 2³ # Unicode exponents
- 2^-3 # Negative exponents
- mod(17, 4) # Modulo
- 3 in -> cm # Unit conversion, can also be → or ➞
- 3 in to cm # Unit conversion with the 'to' keyword
- cos(pi/3 + pi) # Call mathematical functions
- pi/3 + pi // cos # Same, 'arg // f' is equivalent to 'f(arg)'
- # The '//' operator has the lowest precedence
- # which makes it very useful for interactive
- # terminals (press up-arrow, and add '// f')
- # 4. Constants
- let n = 4 # Simple numerical constant
- let q1 = 2 m/s # Right hand side can be any expression
- let q2: Speed = 2 m/s # With optional type annotation
- let q3: Length / Time = 2 m/s # more complex type annotation
- # 5. Function definitions
- fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
- fn speed(len: Length, dur: Time) -> Speed = len / dur # Two parameters
- fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
- # 6. Dimension definitions
- dimension Fame # A new base dimension
- dimension Deceleration = Length / Time^2 # A new derived dimension
- # 7. Unit definitions
- @aliases(quorks) # Optional aliases-decorator
- unit quork = 0.35 meter # A new derived unit
- @metric_prefixes # Optional decorator to allow 'milliclonk', etc.
- @aliases(ck: short) # short aliases can be used with short prefixes (mck)
- unit clonk: Time = 0.2 seconds # Optional type annotation
- @metric_prefixes
- @aliases(wh: short)
- unit warhol: Fame # New base unit for the "Fame" dimension
- unit thing # New base unit with automatically generated
- # base dimension "Thing"
- # 8. Conditionals
- fn step(x: Scalar) -> Scalar = # The construct 'if <cond> then <expr> else <expr>'
- if x < 0 # is an expression, not a statement. It can span
- then 0 # multiple lines.
- else 1
- # 9. Procedures
- print(2 kilowarhol) # Print the value of an expression
- assert_eq(1 ft, 12 in) # Assert that two quantities are equal
- assert_eq(1 yd, 1 m, 10 cm) # Assert that two quantities are equal, up to
- # the given precision
- type(2 m/s) # Print the type of an expression
|