Google

星期一, 八月 25, 2008

孟岩《来自巴西的Lua语言新星让我脸红》及网友评论

一种自由软件的脚本语言 Lua ,由于在游戏界日益成为主流脚本语言(得到著名的卢卡斯艺术公司两款游戏采用,并在GameDev.net的调查中高居脚本语言榜首),最近引起了业界的广泛关注。
Lua (葡萄牙语,义为月亮)是一种轻量级的强大的扩展语言,用纯ANSI C编写,最初版本只有6000行代码,却实现了垃圾收集、反射、面向对象等机制。Lua 是由巴西里约热内卢天主教大学计算机系的Roberto Ierusalimschy 等人于1993年开发的。
最近,Roberto Ierusalimschy 撰写的《Programming in Lua》一书也已经出版
---tangl_99

Python是荷兰人写的,Ruby是日本人写的,Lua是巴西人写的,我这个中国人只能在这里脸红。
---孟岩

不必人人制轮胎,难道你不想躺在轮胎做的吊床上休息?
---牡蛎

只选对的,不选贵的。
---路人甲

转载自tangl_99的blog孟岩的blog

Python是荷兰人写的,Ruby是日本人写的,Lua是巴西人写的,我这个中国人只能在这里脸红。
Lua是所有动态语言中间平均效率最高的一个。它最初是以Library的形式存在,纯粹只是用作C/C++的一个扩展。现在不仅发展出了解释器,还发展 出了编译器。游戏开发里Lua是使用最广泛的脚本语言。当世界上第一本Lua图书上市销售的时候,一位读者评价道:"Lua is a real gem."
(牡蛎到没有听过Lua、Python或者Ruby的编译器——.NET的冒牌货除外)

Lua很棒,Lua是巴西人发明的,这些都令我不爽,但是还不至于脸红,最多眼红。
让我脸红的是Lua的源代码,百分之一百的ANSI C,一点都不掺杂。在任何支持ANSI C编译器的平台上都可以轻松编译通过。我试过,真是一点废话都没有。
我1996年就学会了C,1997年就跑去研究Win32 API,后来是C++,STL,Java... 直到2002年看到C Interfaces and Implementations,才知道仅仅用ANSI C就可以实现一个强大的优美的library,直到2004年看到Lua的源代码才知道仅仅用ANSI C就可以实现一个非常快的虚拟机、非常棒的解释器/编译器和非常漂亮的语言实现。
这8年我都干什么去了?
---孟岩

http://www.chinaitpower.com/2005September/2005-09-13/204728.html

标签: , , , , , , ,

星期四, 八月 21, 2008

Complete syntax of Lua in extended BNF

Complete syntax of Lua in extended BNF. (It does not describe operator precedences.)
 chunk ::= {stat [`;´]} [laststat [`;´]]

block ::= chunk

stat ::= varlist1 `=´ explist1 |
functioncall |
do block end |
while exp do block end |
repeat block until exp |
if exp then block {elseif exp then block} [else block] end |
for Name `=´ exp `,´ exp [`,´ exp] do block end |
for namelist in explist1 do block end |
function funcname funcbody |
local function Name funcbody |
local namelist [`=´ explist1]

laststat ::= return [explist1] | break

funcname ::= Name {`.´ Name} [`:´ Name]

varlist1 ::= var {`,´ var}

var ::= Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name

namelist ::= Name {`,´ Name}

explist1 ::= {exp `,´} exp

exp ::= nil | false | true | Number | String | `...´ | function |
prefixexp | tableconstructor | exp binop exp | unop exp

prefixexp ::= var | functioncall | `(´ exp `)´

functioncall ::= prefixexp args | prefixexp `:´ Name args

args ::= `(´ [explist1] `)´ | tableconstructor | String

function ::= function funcbody

funcbody ::= `(´ [parlist1] `)´ block end

parlist1 ::= namelist [`,´ `...´] | `...´

tableconstructor ::= `{´ [fieldlist] `}´

fieldlist ::= field {fieldsep field} [fieldsep]

field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp

fieldsep ::= `,´ | `;´

binop ::= `+´ | `-´ | `*´ | `/´ | `^´ | `%´ | `..´ |
`<´ | `<=´ | `>´ | `>=´ | `==´ | `~=´ |
and | or

unop ::= `-´ | not | `#´


标签: , ,

星期日, 五月 04, 2008

Lua入门

Lua是一个被设计成用于扩展C/C++编程的轻量级脚本语言。不幸的是,网上很少有关于这个语言的在线教程。我主要通过阅读其源代码来了解它是怎样工作并且通过一些发布的程序学习它。希望,通过下面的教程,会让学习Lua更加简单。

本教程涵盖了Lua5.1。在Lua的每一个版本中都有一些非常不同之处。下面的示例代码将不能在老版本的Lua下运行。如果你仍然在使用老版本而且不愿意升级,不用担心,我已经在文章底部提供了4.0和5.0教程的源代码下载连接。好了,让我们开始吧!

首先,你需要下载Lua。你需要从Lua下载页面去下载源代码。如果你需要编译好了的二进制库,你能在LuaBinaries 中找到你想要的库(lib or dll)。

现在,我们需要安装Lua。在Linux下,你应该先解压文件,然后以root 用户在命令行键入"make linux"和"make linux install"。如果你需要帮助,请参考源代码文件夹中的INSTALL文件。现在,我下载了windows平台下的二进制库包并把它们解压到"C:\ Program Files\lua5.1"。

在Linux下不需要我们做任何设置,但是在windows平台下我们必须配置Visual C++,以便让编译器和连接器找到Lua文件。

  1. 打开Visual C++,选择Tools菜单中的选项菜单。
  2. 展开"项目",并选择"VC++ 目录"。
  3. 选择"包含文件",添加一个新路径"C:\Program Files\lua5.1\include"。
  4. 在选择"库文家",添加路径"C:\Program Files\lua5.1\lib\dll"(这里假设你下载的库为dll,你也可以下载静态链接库)。
  5. 确定。

现在你可以开始编译你的第一个Lua应用了。

使用Lua开始你的第一个程序

这个程序简短且直接,下面做一点说明:

  1. lua_open()返回一个指向Lua解释器的一个指针。
  2. luaL_openlibs()用于装载Lua库,它提供了一些简单的函数,如:print。
  3. 通过调用luaL_dofile()执行脚本。它的作用是读取并解释脚本。
  4. 最后,通过lua_close()函数关闭Lua。

保存文件为luatest.cpp。如果你直接使用C而不是C++,将文件名改为luatest.c,然后将extern "C"删除。

#include 
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/* Lua解释器指针 */
lua_State* L;
int main ( int argc, char *argv[] )
{
/* 初始化Lua */
L = lua_open();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 运行脚本 */
luaL_dofile(L, "test.lua");
/* 清除Lua */
lua_close(L);
/* 暂停 */
printf( "Press enter to exit…" );
getchar();
return 0;
}

下面是test.lua的内容。

-- simple test
print "Hello, World!"

编译

在Linux下,在命令行键入:

g++ luatest.cpp -llua -ldl -o luatest

然后,键入下列命令运行:

./luatest

如果没有问题,程序将在终端输出Hello, World!

在Visual C++你将需要进行下列步骤:

  1. 创建一个新的空Win32控制台应用工程。
  2. 将"luatest.cpp"加入你的工程。
  3. 选择项目菜单中的属性菜单。
  4. 在"连接器"的"输入"栏目的"附加依赖项"中输入"lua5.1.lib"。
  5. 确定。

此时,按F7构建程序。

如果你采用的是dll库,请确保将其放在应用程序的目录中或者windows系统能够找到它的地方。如果你采用的是静态连接库,则不需要。 (Groov0V翻译,转载自CSDN)

谢谢这么简洁清晰的表述
g++ luatest.cpp `pkg-config lua5.1 --libs --cflags` -o luates

http://www.wujianrong.com/archives/2007/10/lua.html

标签: ,

脚本语言:LUA简介


Lua 是一个扩展式程序设计语言,它被设计成支持通用的过程式编程,并有相关数据描述的设施。 Lua 也能对面向对象编程,函数式编程,数据驱动式编程提供很好的支持。 它可以作为一个强大、轻量的脚本语言,供任何需要的程序使用。 Lua 以一个用 clean C 写成的库形式提供。(所谓 Clean C ,指的 ANSI C 和 C++ 中共通的一个子集)

作为一个扩展式语言,Lua 没有 "main" 程序的概念:它只能 嵌入 一个宿主程序中工作,这个宿主程序被称作 embedding program 或简称为 host 。 宿主程序可以通过调用函数执行一小段 Lua 代码,可以读写 Lua 变量,可以注入 C 函数让 Lua 代码调用。 这些扩展的 C 函数,可以大大的扩展了 Lua 可以处理事务的领域,这样就可以订制出各种语言, 而它们共享一个统一的句法格式的框架。 Lua 的官方发布版就包含了一个叫做 lua 的简单的宿主程序,它用 Lua 库提供了一个保证独立的 Lua 解释器。

Lua 是一个自由软件,它的使用许可决定了对它的使用过程一般没有任何保证。 这份手册中描述的东西的实现,可以在 Lua 的官方网站 www.lua.org 找到,

跟其它的许多参考手册一样,这份文档有些地方比较枯燥。 关于 Lua 的设计想法的探讨,可以看看 Lua 网站上提供的技术论文。 有关用 Lua 编程的细节介绍,可以读一下 Roberto 的书,Programming in Lua (Second Edition)

http://www.codingnow.com/2000/download/lua_manual.html

标签: , ,

赖勇浩:动态大比拼Python、Lua和Ruby

So which of the three languages is the best to use on your project? That depends a great deal on what you want to accomplish. To wrap up the book, I've outlined some of the pros and cons of each language in this section.
这三种语言中的哪一种更合适你的项目?很大程度取决于你想达到什么样的目标。作为一书的结尾,在这一节里,我会描述一下它们各自的优点和不足。
Python Pros and Cons
Python的优点和不足
The pros of Python are as follows:
Python的优点如下:
· Python has more extension modules than the other languages.
· Python比其它语言有更多扩展模块。
· Many online Python tutorials exist. There are also plenty of English books and reference materials, many sample scripts exist online, and there is a wealth of introductory material. The Python.org Website is a good place to start looking for these because it has sections for beginners, tutorials, guides organized by topic, and lists of links and references.
· 在网上可以找到很多Python教程。不仅如此,还有大量的英文书籍和资料。Python.org有很多为初学者准备的依主题组织的资料、教程和编程指南,此外还有很多链接和参考。
· Most folks really enjoy the syntax of the Python language because it appears clean and is easy to read.
· 很多人都喜欢Python语言的语法,因为它清晰易读。
· Python has an edge where libraries are concerned. There are many libraries, and, for the most part, they are well documented.
· Python在支持库上大占优势,因为它有很多库,而且大部分都有完善的文档支持。
· Lots of tools that tie into Python are available, and they are often easier to find than the tools for Lua and Ruby.
· 很多工具可以用于Python,但难以找到适用于Lua和Ruby的。
The cons of Python are as follows:
Python的不足:
· Existing Python debuggers are considered quirky and slow. Debugging support on Macintosh and consoles is even weaker.
· 现有的Python调试器诡异而且效率低下。在Macintosh(苹果计算机)和控制台下进行调试是一个梦魇。
· It can be difficult to bundle Python with other languages. There are lots of binary DLLs, and Python has (compared to the other languages) a large standard distribution.
· Python难以与其它语言集成,它有太多的二进制DLL,而且Python带了巨大的标准发布包。
· Lots of folks really dislike the white space sensitivity of Python syntax.
· 同样的,也有很多人不喜欢Python对空白符敏感的语法。
· Python can be quite slow at times, as everything is an object on the heap.
· 因为栈上的任何东西都是对象,所以Python有时候会慢得难以忍受。
Lua Pros and Cons
Lua的优点和不足
The pros of Lua are as follows:
Lua的优点:
· Lua is probably the fastest of the three languages and usually uses the least amount of runtime memory.
· Lua在这三种语言中是运行时速度最快而且占内在最少的。
· Lau has the smallest memory footprint for bundling.
· 集成Lua只会增加极少的内存占用率。
· The Lau C API is very well documented and has good examples for integrating with C.
· Lua的C API有着完备着文档和很好的例子,可以容易地通过C集成在软件里。
The cons of Lua are as follows:
Lua的不足:
· The documentation has improved but is still a bit sketchy overall. Of the three languages, Lua it is probably the least documented (the API being the exception), with the least amount of code comments. This makes for the largest ramp-up time to learn, and there isn't much in the way of introductory Lua material.
· 尽管Lua的文档已经增进不少,但仍然有所不足。三种语言中,Lua可能是文档最少的(API方面是个例外),因为代码中极少注释,这也增加了学习的难度。另外,对Lua的本质也没有什么介绍。
· There isn't a lot of built-in functionality for Lua. There is little support if you need to create a large, complex application.
· Lua内建的功能很少,并没有对创建大型的复杂应用程序提供足够支持。
· Lua could use a better garbage collectorthe current development is moving towards that now. Right now, Lua GC uses a very simple and traditional simple mark and sweep.
· Lua应该使用更好的垃圾收集机制,Lua现在的GC仍然使用非常简单而且传统的“标记和清除”方式。
Pros and Cons of Ruby
Ruby的优点和不足
The pros of Ruby are as follows:
Ruby的优点:
· Ruby possesses fairly good advanced debuggers.
· Ruby有极好的高级调试器。
· Ruby is object oriented from the ground up, and programmers who are OOP enthusiasts or who are used to the OOP paradigm will find the language extremely comfortable.
· Ruby是最彻底的面向对象语言,对于OOP狂热者和惯于使用OOP范式的程序员,他们将会发现Ruby是最适合他们的语言。
· Ruby has arguably the simplest syntax, with no real rules exceptions. Especially true for OOP enthusiasts.
· Ruby的语法极为简单,没有让人出乎意料规则,特别适合OOP行家的习惯。
The cons of Ruby are as follows:
Ruby的不足:
· Lack of English documentation.
· 英文文档极度缺乏。
· Fewer existing works and samples for games than with the other languages.
· 与其它语言相比,在游戏方面所作的工作和实例都少得多。

http://soft.zdnet.com.cn/software_zone/2007/0702/415264.shtml

标签: , , ,

辽ICP备05003652号
流风洄雪听天籁,轻云蔽日看落花

Powered by Blogger