Some basic grammatical collation in Lua programming _lua

Source: Internet
Author: User
Tags lua reserved

Lua is fairly easy to learn, so let's start by creating the first LUA program!
First LUA Program
Interactive mode Programming:

A model named interactive mode is provided in Lua. In this mode, you can type one command after another and get an immediate effect. The shell used by this is lua-i or only LUA commands are invoked. Once in this mode, press Enter and interactive mode will be started, as shown in the following figure.

$ lua-i 
$ lua 5.1.4 Copyright (C) 1994-2008 lua.org, Puc-rio quit
to end; CD, dir and edit also available

You can use the following statement to print what you want

Press ENTER to get the following output.

Copy Code code as follows:
' Test '

Default mode programming:

Invokes the file that the interpreter starts executing with the LUA filename parameter and continues until the script completes. When the script completes, the interpreter is no longer active.

Let's write a simple LUA program. All LUA files will have extensions. Lua. So put the following code in a Test.lua file.

Copy Code code as follows:

Print ("test") [code]

Assuming that the LUA environment is set up correctly, let's run the program using the following command:

[Code]
$ LUA Test.lua


We will get the following output.

Copy Code code as follows:

Test

Let's try another way to execute the LUA program. The following is the modified Test.lua file:

Copy Code code as follows:

#!/usr/local/bin/lua

Print ("Test")


Here, assume that the LUA interpreter is installed in the/usr/local/bin directory. The first line is interpreted by the interpreter if the # number begins to be ignored. Now, try running this program as follows:

Copy Code code as follows:

$ chmod A+rx Test.lua
$./test.lua

will get the following output.

Copy Code code as follows:

Test

Now let's look at the basic architecture in the LUA program, so it's easy to understand the basic building blocks of the LUA programming language.
Lua Tokens

A LUA program consists of a different token, which can be a keyword, identifier, constant, string literal, or symbol. For example, the following LUA statement contains three tokens:

Copy Code code as follows:

Io.write ("Hello World, from", _version, "!\n")

Each token is:

Copy Code code as follows:

Io.write
(
"Hello World, from", _version, "!\n"
)

Comments

Annotations are like help text in LUA programs, which are interpreted as ignoring. They start with-[[Terminate and character-], as follows:

Copy Code code as follows:

--[[my "in Lua--]]

Identifier

A LUA identifier is used to identify variables, functions, or any other user-defined project name. An identifier begins with a letter A through Z or a to Z or underscore _ followed by 0 or more letters, underscores and numbers (0?9).

Punctuation characters in identifiers, such as @,$, and%, are not allowed in Lua. LUA is a case-sensitive programming language. Therefore, Manpower and Manpower are two different identifiers in Lua. Here are some examples of acceptable identifiers:

Copy Code code as follows:

Mohd Zara ABC Move_name a_123
Myname50 _temp J A23b9 RetVal

Keywords

Some of the reserved words in Lua are listed below. These reserved words cannot be used as constants or variables, or any other identifier.
Nil, not, or, repeat, return, then, true, until, and while.

whitespace characters in Lua

Contains only spaces, may have comment lines, is called a blank line, and the LUA explanation completely ignores it.

Whitespace is the term used in Lua to describe spaces, tabs, line breaks, and annotations. A space separates the part of another statement so that the interpretation is determined, where an element in a declaration, such as int, ends and the next element begins. So, in the following statement:

Copy Code code as follows:

Local Age

There must be at least one white space character (usually a space) between the local and the age interpreter so that they can be distinguished. On the other hand, in the following statement

Copy Code code as follows:

Fruit = apples + oranges--get The total fruit

Space characters are necessary, such as: fruit and = between, or = and apples, although it is free, if you want to improve the readability of the purpose, then add some space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.