Skip to content

Commit

Permalink
using new_parser form pure Lua, improve error output, less dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
lalawue committed Apr 30, 2022
1 parent 01852f5 commit 488efa4
Show file tree
Hide file tree
Showing 60 changed files with 6,101 additions and 3,144 deletions.
37 changes: 20 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

NAME=moocscript
SUFX=mooc
OLIB=export LUA_PATH="./out/?.lua;"
ODIR=out/$(NAME)
CS=./bin/$(NAME) -s

Expand All @@ -22,22 +21,26 @@ INSTALL_BIN_PATH=/usr/local/bin
INSTALL_LUA_EXEC=/usr/local/bin/lua
# for store $(NAME) core *.lua
INSTALL_LUA_PATH=/usr/local/opt/$(NAME)
# lpeg.so and lfs.so location
INSTALL_LUA_CPATH=/usr/local/lib/lua/5.1

all:
@echo "Usage:"
@echo "\t $ make test \t# $(NAME) busted"
@echo "\t $ make out \t# self hosted $(NAME) busted"
@echo "\t $ make out \t# make gen then ./out/$(NAME) busted"
@echo "\t $ make gen \t# generate .lua from .mooc into ./out"
@echo "\t $ make install \t# please edit Makefile first"
@echo "\t $ make uninstall"
@echo "\t $ make uninstall \t# please edit Makefile first"

test:
rm -f *.out
rm -rf out/
echo 'package.path="./?.lua" -- auto generated by Makefile' > spec/aaa_spec.lua
busted

out:
out: gen
echo 'package.path="./out/?.lua" -- auto generated by Makefile' > spec/aaa_spec.lua
busted

gen:
rm -f *.out
rm -rf out/
mkdir -p $(ODIR)
Expand All @@ -46,24 +49,24 @@ out:
$(CS) $(NAME)/parser.$(SUFX) > $(ODIR)/parser.lua
$(CS) $(NAME)/utils.$(SUFX) > $(ODIR)/utils.lua
$(CS) $(NAME)/class.$(SUFX) > $(ODIR)/class.lua
$(OLIB) && busted

MN_DIR=$(INSTALL_LUA_PATH)/$(NAME)/
MN_BIN=$(INSTALL_BIN_PATH)/$(NAME)

install:
rm -rf $(MN_DIR)
mkdir -p $(MN_DIR)
echo "#!$(INSTALL_LUA_EXEC)\n" > $(MN_BIN)
echo "package.cpath = package.cpath .. \";$(INSTALL_LUA_CPATH)/?.so;\"" >> $(MN_BIN)
echo "package.path = package.path .. \";$(INSTALL_LUA_PATH)/?.lua;\"" >> $(MN_BIN)
cat bin/$(NAME) | grep -v '#!' >> $(MN_BIN)
cp -a $(NAME)/*.lua $(MN_DIR)
chmod +x $(MN_BIN)
@echo 'Please edit Makefile first !!!'
# rm -rf $(MN_DIR)
# mkdir -p $(MN_DIR)
# echo "#!$(INSTALL_LUA_EXEC)\n" > $(MN_BIN)
# echo "package.path = package.path .. \";$(INSTALL_LUA_PATH)/?.lua;\"" >> $(MN_BIN)
# cat bin/$(NAME) | grep -v '#!' >> $(MN_BIN)
# cp -a $(NAME)/*.lua $(MN_DIR)
# chmod +x $(MN_BIN)

uninstall:
rm -f $(MN_BIN)
rm -rf $(MN_DIR)
@echo 'Please edit Makefile first !!!'
# rm -f $(MN_BIN)
# rm -rf $(MN_DIR)

clean:
rm -f *.out
Expand Down
147 changes: 95 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,47 @@
[1]: https://img.shields.io/badge/license-MIT-blue.svg
[2]: LICENSE

- [MoonCake](#mooncake)
- [Features](#features)
- [Examples](#examples)
- [class / struct / extension](#class--struct--extension)
- [guard / continue / switch / defer](#guard--continue--switch--defer)
- [import / closure / string expression](#import--closure--string-expression)
- [More Depth](#more-depth)
- [Install](#install)
- [Running](#running)
- [Test](#test)
- [Editor with LSP Support](#editor-with-lsp-support)
- [Projects using MoonCake](#projects-using-mooncake)

## MoonCake

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

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

- [The Language](docs/language.md)
- [CommandLine Usage](docs/cmdline.md)
- [Library Interface](docs/library.md)

## Features

with differences from Lua

- variable default local scope
- support guard keyword
- support switch keyword
- support continue keyword
- support defer keyword in function scope
- support create class / struct
- support extension class / struct
- support import keyword
- support anonymous function form '{ in }' likes in Swift
- 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)")`

## Examples

### class/extension
### class / struct / extension

```lua
class Animal {

foot = 4
foot = 2

wing = 0

Expand All @@ -57,30 +65,41 @@ class Bird : Animal {
}
}

extension Bird {
struct Songster {

tune = 'do'

fn canSing() {
return true
}
}

extension Bird: Songster {

feather = true

fn canRun() {
return false
return self.foot >= 4
}
}

b = Bird()
print(b:canFly()) -- true
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 / defer

```lua

import CJson from "cjson" -- import CJson
import sort, concat from table {} -- import table.sort, table.concat

tbl = { "A", "B", "C" }

-- guard, continue, switch
do {
for i, v in ipairs(tbl) {

guard i > 1 else {
continue
}
Expand All @@ -96,15 +115,6 @@ do {
-- print 'case B'
-- print 'default C'

-- anonymous function
do {
sort(tbl, { a, b in
return a > b
})
print(concat(tbl))
}
-- print 'CBA'

-- defer keyword
do {
fn aboutDeferKeyword() {
Expand All @@ -121,6 +131,36 @@ do {
-- print 'return value'
```

### import / closure / string expression

```lua
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 !
```

## More Depth

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

- [The Language](docs/language.md)
- [CommandLine Usage](docs/cmdline.md)
- [Library Interface](docs/library.md)

## Install

recommend install from [LuaRocks](https://luarocks.org/)
Expand All @@ -136,25 +176,26 @@ $ vi Makefile
$ make install
```

or just run as playground in project root dir, but need [LPeg](http://www.inf.puc-rio.br/~roberto/lpeg/) installed, and in Lua's package.cpath
or just run as playground in project root dir

```sh
$ export LUA_PATH=./?.lua
$ ./bin/moocscript
```

with requirement

- [Lua](https://www.lua.org/) >= 5.1 **OR** [LuaJIT](https://luajit.org/) >= 2.0
- [LPeg](http://www.inf.puc-rio.br/~roberto/lpeg/) >= 1.0.2
- [LuaFileSystem](http://keplerproject.github.io/luafilesystem/) >= 1.5 ( only if you need project compile )
- [LuaFileSystem](http://keplerproject.github.io/luafilesystem/) >= 1.5 (only if you need project building, bundling)


## Running

check install first

```sh
$ moocscript -v
moocscript v0.3.20210612, Lua 5.3, LPeg 1.0.2
moocscript v0.7.20220501, Lua 5.4
```

you can run .lua or .mooc source directly, support options below
Expand All @@ -170,32 +211,34 @@ Usage: [OPTIONS] SOURCE.[lua|mooc]
-v version
```

project config example is examples/proj/proj_config.mooc
project config example is examples/proj/proj_config.mooc, you can see how to config it through [CommandLine Usage](docs/cmdline.md).

## Test

using [busted](https://olivinelabs.com/busted/), running from project dir
using [busted](https://olivinelabs.com/busted/), running from project dir, first `make test` before `busted`,
for generating package.path including current `moocscript/` dir.

```sh
$ luarocks install busted
$ busted
$ make test
●●●●●●●●●●●●●●●●●●...
209 successes / 0 failures / 0 errors / 0 pending : 0.16972 seconds
243 successes / 0 failures / 0 errors / 0 pending : 0.21381 seconds
```

you can install [LuaCov](https://keplerproject.github.io/luacov/) to get code coverage report

```sh
$ luarocks install luacov
$ make test
$ busted -c
$ luacov
$ cat luacov.report.out | grep 'moocscript/'
$ cat luacov.report.out | grep '^moocscript/'
...
moocscript/class.lua 30 2 93.75%
moocscript/compile.lua 1109 14 98.75%
moocscript/core.lua 86 2 97.73%
moocscript/parser.lua 241 3 98.77%
moocscript/utils.lua 110 4 96.49%
moocscript/class.lua 56 15 84.51%
moocscript/compile.lua 871 28 96.81%
moocscript/core.lua 68 11 86.08%
moocscript/parser.lua 1131 22 98.02%
moocscript/utils.lua 129 17 87.94%
...
```

Expand All @@ -211,5 +254,5 @@ you can create your own .vsix package through `vsce package`, or you can downloa

## Projects using MoonCake

- [rpc_framework](https://github.com/lalawue/rpc_framework)
- [cincau](https://github.com/lalawue/cincau)
- [rpc_framework](https://github.com/lalawue/rpc_framework)
1 change: 1 addition & 0 deletions bin/moocscript
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function Cmd.option(...)
config.option = "ast"
config.opcount = 2
config.fname = b
config.shebang = true
config.comment = true
elseif a == "-s" then
-- source config
Expand Down
Loading

0 comments on commit 488efa4

Please sign in to comment.