简体   繁体   English

静态类型 Lua

[英]Statically typed Lua

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).我正在寻找在编译时进行类型检查的 Lua 前端编译器,但输出标准的 Lua 5.1 字节码(只有运行时类型)。 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().生成的字节码必须与用标准 LoadString() 编译的现有 Lua 字节码很好地配合。

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;我的想法听起来很像 ActionScript; I wouldn't even mind an ActionScript compiler that outputs Lua byte code!我什至不介意输出 Lua 字节码的 ActionScript 编译器!

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我已经看到一些使用 MetaLua 来做到这一点的参考资料,但老实说,我不够聪明,无法对他们的文档进行反驳

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.在 2005 年左右的夏天,我和一个非常聪明的本科生一起研究了为 Lua 做一些编译时类型推断的问题,可能是在注释的帮助下。 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.如果我想解决您提出的问题,通过允许重要静态类型检查以及与标准字节码编译的 Lua 代码互操作的双重约束,我会从头开始设计一种新语言来满足这两个约束。 It would be a substantial amount of work but significantly easier than trying to retrofit a type system to Lua.这将是大量的工作,但比尝试将类型系统改造为 Lua 要容易得多。

This question is six years old... but here's a new answer: http://terralang.org/这个问题已有六年历史了……但这里有一个新答案: http : //terralang.org/

Like C, Terra is a simple, statically-typed, compiled language with manual memory management.与 C 一样,Terra 是一种简单的、静态类型的、具有手动内存管理功能的编译语言。 But unlike C, it is designed from the beginning to interoperate with Lua.但与 C 不同的是,它从一开始就被设计为与 Lua 互操作。 Terra functions are first-class Lua values created using the terra keyword. Terra 函数是使用 terra 关键字创建的一流 Lua 值。 When needed they are JIT-compiled to machine code.需要时,它们会被 JIT 编译为机器代码。

Please seethis Metalua blog post .请参阅此 Metalua 博客文章

-{ extension "types" }

function sum (x :: list(number)) :: number
  local acc :: number = 0
  for i=1, #x do acc=acc+x[i] end
  return acc
end

This is looks like a run-time solution though.不过,这看起来像是一个运行时解决方案。

Anyway, feel free to ask your question in Metalua mailing list .无论如何,请随时在Metalua 邮件列表中提出您的问题。 If you want to extend Lua syntax, Metalua is the first tool to look at.如果你想扩展 Lua 语法,Metalua 是第一个查看的工具。

PS Please never write Lua as all-caps ! PS请不要把Lua写成全大写

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.可能可以扩展 MetaLua 来做到这一点,但没有人这样做过,而且 AFAIK,也没有这样做的计划。 Lua is meant to be a dynamic language, if you want a statically typed language, use one. Lua 旨在成为一种动态语言,如果您想要一种静态类型语言,请使用一种。

What you are essentially looking for is something like Java or C#.您本质上要寻找的是 Java 或 C# 之类的东西。 In that case, you could use a project like Lua.NET to integrate existing Lua code with C#.在这种情况下,您可以使用Lua.NET 之类的项目将现有的 Lua 代码与 C# 集成。 There is also Kahlua for Java.还有用于 Java 的Kahlua

There is a new paper "Typed Lua: An Optional Type System for Lua" from PUC-Rio just published in Dyla'14. PUC-Rio 刚刚在 Dyla'14 上发表了一篇新论文“Typed Lua: An Optional Type System for Lua”。 http://dl.acm.org/citation.cfm?id=2617553 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".它是关于“Typed Lua 的初始设计,Lua 脚本语言的可选类型扩展”。 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.关于基于 metalua 的类型系统,Tidal Lock:来自 Fabien 的 Lua 的可选静态类型检查和推理。 http://lua-users.org/lists/lua-l/2013-02/msg00403.html . http://lua-users.org/lists/lua-l/2013-02/msg00403.html

There is also Ravi https://github.com/dibyendumajumdar/ravi还有拉维https://github.com/dibyendumajumdar/ravi

Ravi Programming Language is a derivative of Lua 5.3 with limited optional static typing and LLVM and libgccjit based JIT compilers Ravi 编程语言是 Lua 5.3 的衍生物,具有有限的可选静态类型和基于 LLVM 和 libgccjit 的 JIT 编译器

I really enjoy programing in Terra (see above)我真的很喜欢在 Terra 中编程(见上文)

I recommend EmmyLua .我推荐EmmyLua

This is an Intellij/VSCode plugin that supports typing documentation.这是一个支持打字文档的 Intellij/VSCode 插件。 I found the overall documenting approaching method to be very friendly.我发现整体记录方法非常友好。 Also thanks to its IDE support, EmmyLua also support hinting.同样由于其 IDE 支持,EmmyLua 还支持提示。

Here is a little snippet for EmmyLua doc:这是 EmmyLua 文档的一个小片段:

--- @alias recipe_prototype any
--- @alias recipe_name string
--- @alias ingredient_name string

--- @class Coordinate
--- @field x number
--- @field y number

--- @class Entity
--- @field entity_number number unique identifier of entity
--- @field name string entity name
--- @field position Coordinate
--- @field direction any defines.direction.east/south/west/north

--- @class BlueprintSection
--- @field entities Entity[]
--- @field inlets number[] index of inlets in entities list
--- @field outlets number[] index of outlets in entities list

--- @type BlueprintSection
BlueprintSection = {}

--- @return BlueprintSection
function BlueprintSection.new()
    --- ...
end

--- @param other BlueprintSection
--- @param xoff number optional, x-offset of the other section, default to width of self
--- @param yoff number optional, y-offset of the other section, default to 0
--- @return BlueprintSection new self
function BlueprintSection:concat(other, xoff, yoff)
   -- ...
end

For more doc reference, check https://emmylua.github.io有关更多文档参考,请查看https://emmylua.github.io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM