Basic Constructs

As Hush aims to be simple language, only standard control flow constructs and operations are supported.

Comments

Comments start with a #:

  1. # This is a comment.

Variables

In Hush, all variables must be declared. This slightly increases verbosity, but makes scope rules way more reasonable, and will prevent you from ever debugging why the value of the collcetion variable is nil (note the typo).

  1. let x # Introduces the variable in the local scope
  2. let pi = 3.14 # Syntax sugar for assignment
  3. let string = "hello!"
  4. let byte = 'a', # this is a single byte, not a string. Note the single quotes.
  5. let array = [ 1, 2, 3, 4 ]
  6. let dictionary = @[
  7. key: pi,
  8. items: array,
  9. nested: @[ one: 1, two: 2 ],
  10. ]

Identifiers must start with a letter or underscore, and may contain further letters, digits and underscores.

Operators

Hush provides standard arithmetic, logical, relational and indexing operators.

  • Arithmetic +, -, *, /: int or float; %: int only. These operators do not promote ints to float, and will panic with mismatching types. You may explicitly convert your ints to float prior to applying the operators.
  • Logical and, or, not: bool only. Logical operators are short-circuiting.
  • Relational ==, !=: all types; <, <=, >, >=: int, float, byte, string only.
  • String concatenation ++: string only.
  • Indexing [], .: array, dict, string, error only. Attempts to access an index out of bounds will result in a panic. Additionally, dicts and errors may be indexed with the dot . operator, as long as the key is a valid identifier.
  1. let array = [
  2. 1 + 2,
  3. 3.0 * std.float(4), # explicit conversion to float.
  4. 21 % std.int(5.2), # explicit conversion to int.
  5. false and not true, # will short circuit.
  6. 1 == 1.0, # false, int and float are always distinct.
  7. "abc" > "def",
  8. "hello" ++ " world",
  9. ]
  10. std.assert(array[0] == 3)
  11. # this would cause a panic:
  12. # let x = array[20]
  13. let dictionary = @[
  14. age: 20,
  15. height: 150,
  16. greet: function()
  17. std.print("hello!")
  18. end,
  19. ]
  20. std.assert(dictionary["age"] == 20)
  21. std.assert(dictionary.height == 150)
  22. dictionary.greet() # prints "hello!"

As you may be wondering, the standard library, which can be used through the std global variable, is nothing but a dictionary full of useful functions.