Home | About | Forum | Manual | Download | Screenshots

Basic Semantic Rules

High level languages are interpreted by a program called a parser. The parser's first job is to break the source code into fundamental units called tokens or lexemes. There are a few different types of tokens, such as keyword commands, numbers, strings of text, operators and so on. Semantics is just a fancy term for the convention or the writing rules that you must follow so that the parser can tell apart the individual tokens in your code.

Numbers

Numbers appearing in your code must begin with either a digit, a negative sign or a decimal point. Notice that for fractional values, there cannot be any space between the decimal point and the decimal value which is supposed to follow it. It fools the parser and it will assume that you've written two separate numbers.

123
.342
-6.5e+10
23.81

23. 81  -- incorrect

Strings

Strings are sequences of characters or basically, text. Any non-executable text that appears in your code must be "delimited" or enclosed by quotation marks. In Lua, strings begin with either a single (') or a double (") quotation mark and should be closed by another quotation mark of the same type. What if you want your string to contain a quotation mark symbol? In that case, you need to make sure that the embedded quotation mark is preceded by a backslash character (\).

"Hello!"
'Hello!'

"He's stolen the chalice!"
'He\'s stolen...'

'He's stolen...'  -- incorrect
Lua offers a third semantic for strings, called the "long string". These begin with a double left brace ([[) and end with a double right brace (]]). A useful feature of the long string is that it can run across multiple lines. Regular types of strings on the other hand, have to explicitly mark each line break using the backslash lowercase n sequence (\n).

[[ He's stolen
the
chalice ]]

'He\'s stolen\nthe\nchalice'

Identifiers

Identifier is a general term for all the commands, "variables" or other keywords that appear in your code. All identifier names must follow the same set of syntactic rules. They can only begin with an alphabetic character or the underscore symbol (_). Subsequent characters have to be either "alphanumeric" (letters and digits) or underscore. Dashes, spaces or other punctuation symbols cannot appear in identifier names.

a
goto
_var_
my_var2

my var2  -- incorrect
my-var2  -- incorrect
2var     -- incorrect

Comments

Any characters in your code that appear after the double dash sequence (--) all the way until the end of the line are considered a "line comment". The parser ignores these comments entirely. They are simply remarks that the programmer leaves to remind himself or clarify to others the purpose of his code. Unlike the C language, Lua doesn't support block comments. The Lua manual suggests using the long string syntax to represent comments that span across multiple lines.

1 + 2  -- line comment

Next: Expressions