打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
compiler construction

I am looking for a Lua front-end compiler that is type-checked at compile time, but outputs standard Lua 5.1 byte-code (that has only run-time types). What I want is a decent amount of static, compile-time syntactic analysis and optional typing, to detect trivial errors sooner than run-time. The resulting byte-code would have to play nicely with existing Lua byte-code that was compiled with the standard LoadString().

To be clear -- any difference would only occur at byte-compilation time. At runtime, the byte code would have no idea that anything different/unusual happened to it during the compile phase.

What I have in mind sounds a lot like ActionScript; I wouldn't even mind an ActionScript compiler that outputs Lua byte code!

Has anyone heard of such an effort? I've seen some references to using MetaLua to do this, but honestly I am not bright enough to make heads of tails of their documentation

asked May 2 '09 at 1:30
Armentage
2,25132228

6  
By the way, you say strong typing, but I think you mean static typing. There is a difference. For example, Python is strongly typed, but dynamically typed. C is weakly typed, but statically typed. – Zifre May 2 '09 at 13:54
up vote 13 down vote accepted

In the summer of 2005 or thereabouts, I worked with an incredibly smart undergraduate student on the problem of doing some compile-time type inference for Lua, possibly assisted by annotations. This problem turns out to be incredibly hard! (My student wrote a short technical note, but it's not really intended for general circulation.)

If I wanted to solve the problem you have posed, with the twin constraints that it allow significant static type checking and that it interoperate with standard bytecode-compiled Lua code, I would design a new language from scratch to satisfy these two constraints. It would be a substantial amount of work but significantly easier than trying to retrofit a type system to Lua.

answered May 2 '09 at 2:37
Norman Ramsey
139k41284461

    
I hear what you're saying; I've been putting a lot of thought into this, and while it seems very simply to cover the most basic cases (i.e. catching local x:int = "hello" as an error) things get very difficult when you start worrying about tables or returning typed tuples from functions. But those guys at Adobe figured it out with Javascript! – Armentage May 2 '09 at 3:03
    
Table and the infinite number of ways they are used is definitely the problem. If you're willing to have lots of annotations things probably get simpler. – Norman Ramsey May 2 '09 at 3:27
2  
Norman, knowledge should be shared. I for one would be very interested in learning what were the conclusions of your study! – alphazero May 5 '09 at 12:27
    
@Armentage: Note that another difficulty is that you would need to expose this type system to things like userdata, allowing the creation of new types that can be checked. Remember: the primary purpose of Lua is embedding, so if you have type safety, it needs to combine well with user extensions of the language. – Nicol Bolas Apr 21 '12 at 17:13

Please see this Metalua blog post.

-{ extension "types" }function sum (x :: list(number)) :: number  local acc :: number = 0  for i=1, #x do acc=acc+x[i] end  return accend

This is looks like a run-time solution though.

Anyway, feel free to ask your question in Metalua mailing list. If you want to extend Lua syntax, Metalua is the first tool to look at.

P.S. Please never write Lua as all-caps!

answered May 2 '09 at 6:18
Alexander Gladysh
13.3k1970131

There is no such thing. It may be possible to extend MetaLua to do this but nobody has done it, and AFAIK, there are no plans to do so. Lua is meant to be a dynamic language, if you want a statically typed language, use one.

What you are essentially looking for is something like Java or C#. In that case, you could use a project like Lua.NET to integrate existing Lua code with C#. There is also Kahlua for Java.

answered May 2 '09 at 1:36
Zifre
16.5k56996

    
I am not looking for LUA.NET. LUA.NET implies some sort of .NET runtime. The .NET runtime is nothing like the LUA ByteCode VM. What I am looking for would only apply strong-typing at byte-compile time, and then run the resulting code blissfully unaware that anything unusually happened to it at compile time. – Armentage May 2 '09 at 1:37
    
Unfortunately, I am already working with a large (and growing) LUA code base. Due to LUA's nature, changing any existing library code is pretty much impossible. An optional strong-typing front end would solve this problem, without necessarily changing the basic feel of the language. – Armentage May 2 '09 at 2:34
5  
@Armentage, Lua is not an acronym, it is a proper noun. It is written "Lua", not "LUA". See lua.org/about.html#name for the official story. – RBerteig May 2 '09 at 21:22
    
@Armentage: If you want to pursue this in the fall I might be able to find a student who would be interested. – Norman Ramsey May 2 '09 at 21:54
    
We had an intern at my firm build something like this over the summer. Results were interesting... his mentor actually had him build his type-safe lua pre-processor in Haskell... – Armentage Nov 5 '09 at 4:55

There is a new paper "Typed Lua: An Optional Type System for Lua" from PUC-Rio just published in Dyla'14. http://dl.acm.org/citation.cfm?id=2617553

It is about "initial design of Typed Lua, an optionally-typed extension of the Lua scripting language". It's still in progress, and the type system is still relatively simple. No type inference/type checking tool provided.

Regarding the metalua based typing system, Tidal Lock: optional static type checking and inference for Lua from Fabien. http://lua-users.org/lists/lua-l/2013-02/msg00403.html.

answered Jul 3 '14 at 23:57
hwang
334

    
The slides for the Dyla14 paper are available: lua.org/wshop14/Murbach.pdf – Charles Stewart Sep 7 '15 at 7:31

This question is six years old... but here's a new answer: http://terralang.org/

Like C, Terra is a simple, statically-typed, compiled language with manual memory management. But unlike C, it is designed from the beginning to interoperate with Lua. Terra functions are first-class Lua values created using the terra keyword. When needed they are JIT-compiled to machine code.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
How the Java Virtual Machine (JVM) Works
ISO 639
CINT for Compilers 5.16.19 Freeware Download
Lua 5.1 参考手册
动态语言
Code Conventions for the Java Programming Language
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服