1 Star 2 Fork 0

lalawue/mooncake

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

MIT licensed

MoonCake

MoonCake was a Swift like programming language that compiles into Lua, runs on Lua 5.1 and above, including LuaJIT.

Features

with differences from Lua

  • always declare variable as local, unless using export keyword
  • using { and } instead of keyword do, then, end to seperate code block
  • support guard keyword, which must transfer control at scope end
  • support switch keyword, you can case a lot of conditions at a time
  • support continue keyword, implemented by goto, available in Lua 5.2 and LuaJIT
  • support defer keyword in function scope, including anonymous function
  • support class and struct for simpler Object Oriented programming
  • support extension keyword for extend class/struct
  • support import keyword for simpler require a lot of sub modules
  • support convenient anonymous function form { in } likes in Swift
  • support expression in string like print("5 + 3 = \(5 + 3)")

Playground

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.

Highlight

class / struct / extension

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 / defer

-- 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 / closure / string expression

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 !

Documentation

recommand install and running first, or get more straight expressions from examples/ dir, before dig into detials about the language.

Install

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

Running

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.

Test

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%
...

Editor with LSP Support

MoocHelper is a High-performance MoonCake/Lua plugin, Language Server Protocol for MoonCake/Lua, modified from LuaHelper.

App Market Installation

Local Installation

  • download a pre-build one in releases, or in gitee mirror.
  • or create your own .vsix package with npm install -g vsce, then vsce package under luahelper-vscode dir

vscode_extension

vscode_install_vsix

Projects using MoonCake

The MIT License (MIT) Copyright (c) 2021 lalawue Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A Swift like program language that compiles into Lua expand collapse
Lua and 3 more languages
MIT
Cancel

Releases (11)

All

Contributors

All

Activities

Load More
can not load any more
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/lalawue/mooncake.git
git@gitee.com:lalawue/mooncake.git
lalawue
mooncake
mooncake
master

Search

344bd9b3 5694891 D2dac590 5694891