5

Lua一日游:(1) Mac环境搭建与基本语法

 3 years ago
source link: https://changkun.de/blog/posts/lua-1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Lua一日游:(1) Mac环境搭建与基本语法

Published at:

2014-03-09

  |  

Reading: 486 words ~1min

  |  

PV/UV: 5/5

1、环境搭建

Lua的官方网站为lua.org,你可以下载到整个lua的源码,并且在Mac平台下直接使用make命令进行编译,只需要切换到对应的lua目录使用make命令,之后会提示你需要在什么PLATFORM下进行make,我们输入make macosx即可完成整个编译过程。如图所示。

1.png

编译完成后我们可以在src目录中看到两个新生成的文件(lua和luac),如图所示。

2.png

我们可以使用luac对lua script进行编译,或者使用lua直接执行lua脚本(可以是源码,也可以是编译后的文件)。如图所示。

3.png

在Mac中使用Lua进行开发,有一个比较好用的Eclipse插件,或者是独立版的LDT(Lua Dev Tools),你可以在http://www.eclipse.org/koneki/ldt/中下载,下面是它的使用界面:

4.png

2、基本语法

我们直接使用源码来查看:

print("hello lua")  -- 输出
local num = 10      -- 定义一个局部变量
print(num)          -- 输出

-- 定义方法,无参数
function sayHello()
	print("hello lua")
end
sayHello()  -- 执行方法

-- 定义方法,有参数
function max(a,b)
  if a>b then
    return a
  else
    return b
  end
end
print(max(2,4)) -- 执行方法

-- 循环语句
for var=1, 100 do
	print(var)    -- 打印1到100
end

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK