MoonCake was a Swift like programming language that compiles into Lua, runs on Lua 5.1 and above, including LuaJIT.
with differences from Lua
local
, unless using export
keyword{
and }
instead of keyword do
, then
, end
to seperate code blockguard
keyword, which must transfer control at scope endswitch
keyword, you can case
a lot of conditions at a timecontinue
keyword, implemented by goto
, available in Lua 5.2 and LuaJITdefer
keyword in function scope, including anonymous functionclass
and struct
for simpler Object Oriented programmingextension
keyword for extend class/structimport
keyword for simpler require
a lot of sub modules{ in }
likes in Swiftprint("5 + 3 = \(5 + 3)")
For it's a pure Lua transpiler, so it can directly running in any environment that Lua supports.
Try it out in your browser https://moocscript.fun.
class Animal {
foot = 2
wing = 0
fn init() {
}
fn canFly() {
return self.wing > 0
}
}
class Bird : Animal {
fn init(wing) {
self.foot = 2
self.wing = wing or 2
}
}
struct Songster {
tune = 'do'
fn canSing() {
return true
}
}
extension Bird: Songster {
feather = true
fn canRun() {
return self.foot >= 4
}
}
b = Bird()
print(b.foot) -- 2
print(b.wing) -- 2
print(b.tune) -- do
print(b.feather) -- true
print(b:canRun()) -- false
print(b:canFly()) -- true
print(b:canSing()) -- true
-- guard, continue, switch
do {
for i, v in ipairs({'A', 'B', 'C', 'D', 'E'}) {
guard i > 1 else {
continue
}
switch v {
case 'B', 'D':
print('case \(v)')
case 'C':
break
default:
print('default \(v)')
}
}
}
-- print 'case B'
-- print 'case D'
-- print 'default E'
-- defer keyword
do {
fn aboutDeferKeyword() {
defer {
print("defer block")
}
print("fn block")
return "return value"
}
print(aboutDeferKeyword())
}
-- print 'fn block'
-- print 'defer block'
-- print 'return value'
import Utils from "moocscript.utils" -- import Utils
import sort, concat from table {} -- import table.sort, table.concat
tbl = { "A", "B", "C" }
-- closure, or anonymous function
do {
sort(tbl, { a, b in
return a > b
})
print(concat(tbl))
}
-- print 'CBA'
-- string expression
print("Hello, world \(600 + 60 + 6) !")
-- Hello, world 666 !
recommand install and running first, or get more straight expressions from examples/
dir, before dig into detials about the language.
recommend install from LuaRocks
$ luarocks install mooncake
or edit Makefile for a custom install
$ vi Makefile
$ make install
or just run as playground in project root dir
$ export LUA_PATH=./?.lua
$ ./bin/moocscript
or just make
to see other generation option
$ make
Usage:
make test # busted ./moocscript/?.lua
make out # busted ./out/moocscript/?.lua
make web # busted ./out/web/moocscript-web.lua
make gen # generate ./out/moocscript/?.lua from ./moocscript/?.mooc
make install # please edit Makefile first
make uninstall # please edit Makefile first
with requirement
check install first
$ moocscript -v
moocscript v0.8.20221204, Lua 5.4
then enter REPL without an editor
$ ./bin/moocscript -i
moocscript v0.7.20221006, Lua 5.4
> export * -- default global variable
> class Person {
name = ''
fn init(name) {
self.name = name
}
fn intro() {
return "My name is \(self.name)"
}
}
> peter = Person("Peter")
> print(peter:intro())
My name is Peter
you can run .lua or .mooc source directly, support options below
$ moocscript
Usage: [OPTIONS] SOURCE.[lua|mooc]
'' load SOURCE and run
-h print help
-a print AST
-s print Lua code
-i enter REPL
-p generate Lua code with project config
-v version
project config example is examples/proj/proj_config.mooc
, you can see how to config it through CommandLine Usage.
using busted, running from project dir, first make test
before busted
,
for generating package.path including current moocscript/
dir.
$ luarocks install busted
$ make test
●●●●●●●●●●●●●●●●●●...
311 successes / 0 failures / 0 errors / 0 pending : 0.217144 seconds
you can install LuaCov to get code coverage report
$ luarocks install luacov
$ make test
$ busted -c
$ luacov
$ cat luacov.report.out | grep '^moocscript/'
...
moocscript/class.lua 60 10 85.71%
moocscript/compile.lua 906 39 95.87%
moocscript/core.lua 65 19 77.38%
moocscript/parser.lua 1156 23 98.05%
moocscript/utils.lua 121 14 89.63%
...
MoocHelper is a High-performance MoonCake/Lua plugin, Language Server Protocol for MoonCake/Lua, modified from LuaHelper.
moochelper
in the input boxnpm install -g vsce
, then vsce package
under luahelper-vscode
dir此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。