Syntax ====== Comments -------- Like in Lua, line comments start with :code:`--` and end at the end of the line. .. code-block:: hurdy -- this is a comment Block comments start with :code:`-*` and end with :code:`*-`. .. code-block:: hurdy -* this is a block comment spanning multiple lines *- Block comments cannot (currently) be nested. Comments are ignored during compilation and do not appear in the compiled Lua code. Keywords -------- Hurdy keywords ^^^^^^^^^^^^^^ Hurdy reserves the following keywords .. code-block:: hurdy and as break continue else elseif false for function from global if import in method nil or return repeat true until var while as well as :code:`_hurdytemp` followed by a number (e.g., :code:`_hurdytemp1`, :code:`_hurdytemp568`)—these are used internally by the transpiler. Reserved Hurdy keywords cannot be used a variable names, but can be used as keys to index objects: .. code-block:: hurdy for = 5 -- this is not allowed x.for = 5 -- this is ok Lua keywords ^^^^^^^^^^^^ Lua keywords that are not Hurdy keywords are also reserved and cannot be used as identifiers (including to index objects): .. code-block:: lua do end local not then goto .. code-block:: hurdy local = 5 -- this is not allowed x.local = 5 -- this is also not allowed Identifiers ----------- Identifiers in Hurdy are case-sensitive, start with a letter or underscore, and can contain letters, numbers, or onderscores. Only ASCII letters are allowed in identifiers. Strings ------- Strings follow the same syntax as Lua (for both short and long strings). .. code-block:: hurdy "This is a short string" [[ This is a level 0 long string ]] [===[ This is a level 3 long string ]===] The :code:`\\z` escape sequence is processed during compilation to remove all following whitespace (including newlines), so the resulting code can be run on Lua 5.1 as well: .. code-block:: hurdy "example \z of escape sequence" is equivalent to .. code-block:: hurdy "example of escape sequence" Newlines -------- Statements in Hurdy are sperated using newlines (:code:`\\n`). To allow for freedom of formatting and to split lines that are too long, newlines are ignored after a token that cannot end a statemet. .. code-block:: hurdy print( 1, 2, 3 ) and .. code-block:: hurdy print(1, 2, 3) produce the same result. Operators --------- All of Lua's operators are supported and have the same precedence and associativity rules as in Lua, although in some cases (exponentiation, logical NOT, bitwise XOR, inequality) Hurdy uses a different symbol. Refer to the tables below for the correct symbols. Additionally, Hurdy has extra :ref:`assignment operators `, such as addition assignment. Arithmetic operators ^^^^^^^^^^^^^^^^^^^^ .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description - Notes * - :code:`+` - :code:`+` - Addition - * - :code:`-` - :code:`-` - Subtraction - * - :code:`*` - :code:`*` - Multiplication - * - :code:`/` - :code:`/` - Float division - * - :code:`//` - :code:`//` - Floor division - Requires Lua 5.3+ * - :code:`%` - :code:`%` - Modulo - * - :code:`**` - :code:`^` - Exponentiation - Different than Lua * - :code:`-` - :code:`-` - Unary minus - Logical operators ^^^^^^^^^^^^^^^^^^^^ .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description - Notes * - :code:`and` - :code:`and` - Logical AND - * - :code:`or` - :code:`or` - Logical OR - * - :code:`!` - :code:`not` - Logical NOT - Different than Lua Bitwise operators ^^^^^^^^^^^^^^^^^ Bitwise operators are supported, but the compiled code must be run with Lua 5.3+. .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description - Notes * - :code:`&` - :code:`&` - Bitwise AND - * - :code:`|` - :code:`|` - Bitwise OR - * - :code:`^` - :code:`~` - Bitwise XOR - Different than Lua * - :code:`>>` - :code:`>>` - Bitwise right shift - * - :code:`<<` - :code:`<<` - Bitwise left shift - * - :code:`~` - :code:`~` - Bitwise NOT - Relational operators ^^^^^^^^^^^^^^^^^^^^ .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description - Notes * - :code:`==` - :code:`==` - Equality - * - :code:`!=` - :code:`~=` - Inequality - Different than Lua * - :code:`<` - :code:`<` - Less than - * - :code:`>` - :code:`>` - Greater than - * - :code:`<=` - :code:`<=` - Less or equal - * - :code:`>=` - :code:`>=` - Greater or equal - Additional operators ^^^^^^^^^^^^^^^^^^^^ .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description * - :code:`..` - :code:`..` - Concatenation * - :code:`#` - :code:`#` - Length Assignment operators ^^^^^^^^^^^^^^^^^^^^ .. list-table:: :header-rows: 1 * - Hurdy operator - Lua operator - Description - Notes * - :code:`=` - :code:`=` - Assignment - * - :code:`+=` - - Addition assignment - * - :code:`-=` - - Subtraction assignment - * - :code:`*=` - - Multiplication assignment - * - :code:`/=` - - Float division assignment - * - :code:`//=` - - Floor division assignment - Requires Lua 5.3+ * - :code:`and=` - - Logical AND assignment - * - :code:`or=` - - Logical OR assignment - * - :code:`&=` - - Bitwise AND assignment - Requires Lua 5.3+ * - :code:`|=` - - Bitwise OR assignment - Requires Lua 5.3+ * - :code:`^=` - - Bitwise XOR assignment - Requires Lua 5.3+ * - :code:`>>=` - - Bitwise right shift assignment - Requires Lua 5.3+ * - :code:`<<=` - - Bitwise left shift assignment - Requires Lua 5.3+ The regular assignment operator works just as in Lua, and in particular it can be used with multiple identifiers on the left and/or multiple values on the right: .. code-block:: hurdy x = 3 y = nil, 2 x, y = 3, 2 All the extra assignment operators which are unique to Hurdy associated to binary operators work in the same way, and can only be used with one identifier on the left and one variable on the right. For example for addition assignment .. code-block:: hurdy x += y translates to the Lua code .. code-block:: lua x = x + y .. warning:: Something to keep in mind when using assignment operators is that the left-hand-side is evaluated twice: .. code-block:: hurdy t.x += 2 -- equivalent to t.x = t.x + 2 -- evaluated when adding and when assigning This could lead to unwanted results if :code:`t.x` is obtained through a :code:`__index` metamethod on :code:`t`.