Skip to content

Commit de1e87f

Browse files
committed
lua: Add Lua binding
1 parent 9b1cb32 commit de1e87f

18 files changed

Lines changed: 751 additions & 3 deletions

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ BINDINGS := \
88
go \
99
java \
1010
julia \
11+
lua \
1112
nodejs \
1213
perl \
1314
perl-alien \

doc/bindings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Currently there are working libraries for:
4545
* [Go](https://github.com/yaml/yamlscript-go)
4646
* [Java](https://clojars.org/org.yamlscript/yamlscript)
4747
* [Julia](https://juliahub.com/ui/Packages/General/YAMLScript)
48+
* [Lua](https://luarocks.org/modules/ingy/yamlscript)
4849
* [NodeJS](https://www.npmjs.com/package/@yaml/yamlscript)
4950
* [Perl](https://metacpan.org/dist/YAMLScript/view/lib/YAMLScript.pod)
5051
* [PHP](https://packagist.org/packages/yaml/yamlscript)

doc/examples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ This page contains a links to programs written in YS.
4141
* YS Repository Utilities
4242
* [util/release-yamlscript](
4343
https://github.com/yaml/yamlscript/blob/main/util/release-yamlscript)
44-
The utility that orchestrates the release of YS, including 12 binary builds
45-
and `libys.so` bindings for 12 programming languages.
44+
The utility that orchestrates the release of YS; including `ys`, libys.so`
45+
and `libys.so` bindings for 13 programming languages.
4646
* [util/brew-update](
4747
https://github.com/yaml/yamlscript/blob/main/util/brew-update)
4848
The utility that updates the Homebrew formula for YS.

doc/install.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ languages:
117117
* [Go](https://github.com/yaml/yamlscript-go)
118118
* [Java](https://clojars.org/org.yamlscript/yamlscript)
119119
* [Julia](https://juliahub.com/ui/Packages/General/YAMLScript)
120+
* [Lua](https://luarocks.org/modules/ingy/yamlscript)
120121
* [NodeJS](https://www.npmjs.com/package/@yaml/yamlscript)
121122
* [Perl](https://metacpan.org/pod/YAMLScript)
122123
* [PHP](https://packagist.org/packages/yaml/yamlscript)

doc/loaders.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following loader libraries are currently available:
2222
* [Go](https://github.com/yaml/yamlscript-go)
2323
* [Java](https://clojars.org/org.yamlscript/yamlscript)
2424
* [Julia](https://juliahub.com/ui/Packages/General/YAMLScript)
25+
* [Lua](https://luarocks.org/modules/ingy/yamlscript)
2526
* [NodeJS](https://www.npmjs.com/package/@yaml/yamlscript)
2627
* [Perl](https://metacpan.org/dist/YAMLScript/view/lib/YAMLScript.pod)
2728
* [PHP](https://packagist.org/packages/yaml/yamlscript)

lua/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*.rockspec
2+
/*.src.rock
3+
/*.tar.gz
4+
/loaded_data.yaml

lua/Makefile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
include ../common/base.mk
2+
include $(COMMON)/binding.mk
3+
include $(MAKES)/lua.mk
4+
include $(MAKES)/luarocks.mk
5+
include $(MAKES)/python.mk
6+
include $(MAKES)/shell.mk
7+
8+
LUA-RELEASE-REVISION ?= 1
9+
10+
export LUA_PATH := $(ROOT)/lua/lib/?.lua;;
11+
export LUA_CPATH := $(LOCAL-PREFIX)/lib/lua/5.4/?.so;;
12+
13+
LUA-CFFI := $(LOCAL-LIB)/lua/5.4/cffi.so
14+
LUA-CJSON := $(LOCAL-LIB)/lua/5.4/cjson.so
15+
LUA-MESON := $(PYTHON-VENV)/bin/meson
16+
17+
LUA-DEPS := \
18+
$(LUA) \
19+
$(LUAROCKS) \
20+
$(LIBYS-SO-FQNP) \
21+
$(LUA-CFFI) \
22+
$(LUA-CJSON) \
23+
24+
LUA-ROCKSPEC := \
25+
yamlscript-$(YAMLSCRIPT_VERSION)-$(LUA-RELEASE-REVISION).rockspec
26+
27+
LUAROCKS-API-KEY := \
28+
$(shell ys -e '.luarocks.token:say' ~/.yamlscript-secrets.yaml 2>/dev/null)
29+
30+
31+
build:: build-doc
32+
33+
build-doc:: ReadMe.md
34+
35+
test:: test-basic test-ffi
36+
37+
test-basic: $(LUA-DEPS)
38+
lua test/test_basic.lua
39+
40+
lua-test-1 := \
41+
local ys = require("yamlscript"); \
42+
local data = ys.new():load("inc: 41"); \
43+
print(data)
44+
45+
lua-test-2 := \
46+
local ys = require("yamlscript"); \
47+
local data = ys.new():load("!YS-v0\ninc: 41"); \
48+
print(data)
49+
50+
test-ffi: $(LUA-DEPS)
51+
lua -e '$(lua-test-1)'
52+
lua -e '$(lua-test-2)'
53+
54+
release: $(LUA-ROCKSPEC) $(YS)
55+
ifndef YS_RELEASE_VERSION_NEW
56+
@echo "Error: YS_RELEASE_VERSION_NEW not set"
57+
@exit 1
58+
endif
59+
ifndef LUAROCKS-API-KEY
60+
@echo "Error: YAMLScript LuaRocks credentials not configured"
61+
@exit 1
62+
endif
63+
$(ROOT)/util/release-lua
64+
luarocks upload --api-key '$(LUAROCKS-API-KEY)' $(LUA-ROCKSPEC)
65+
66+
clean::
67+
$(RM) yamlscript-*.rock
68+
$(RM) yamlscript-*.rockspec
69+
$(RM) yamlscript-*.tar.gz
70+
71+
$(LUA-ROCKSPEC): yamlscript.rockspec
72+
perl -pe 's/%VERSION%/$(YAMLSCRIPT_VERSION)/g; \
73+
s/%REVISION%/$(LUA-RELEASE-REVISION)/g;' $< > $@
74+
75+
$(LUA-CFFI): $(LUAROCKS) $(LUA-MESON)
76+
luarocks install cffi-lua MESON_DIR=$(PYTHON-VENV)
77+
78+
$(LUA-CJSON): $(LUAROCKS) $(LUA-MESON)
79+
luarocks install lua-cjson MESON_DIR=$(PYTHON-VENV)
80+
81+
$(LUA-MESON): $(PYTHON-VENV)
82+
pip install meson ninja

lua/ReadMe.md

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<!-- DO NOT EDIT — THIS FILE WAS GENERATED -->
2+
3+
YS / YAMLScript
4+
===============
5+
6+
Add Logic to Your YAML Files
7+
8+
9+
## Synopsis
10+
11+
Load `file.yaml` with YS:
12+
13+
```yaml
14+
!YS-v0:
15+
16+
# Get data from external sources:
17+
names-url =:
18+
'github:dominictarr/random-name/first-names.json'
19+
20+
name-list =: names-url:curl:json/load
21+
22+
# Data object with literal keys and generated values:
23+
name:: name-list:shuffle:first
24+
aka:: name-list:rand-nth
25+
age:: &num 2 * 3 * 7
26+
color:: &hue
27+
rand-nth: qw(red green blue yellow)
28+
title:: "$(*num) shades of $(*hue)."
29+
```
30+
31+
and get:
32+
```json
33+
{
34+
"name": "Dolores",
35+
"aka": "Anita",
36+
"age": 42,
37+
"color": "green",
38+
"title": "42 shades of green."
39+
}
40+
```
41+
42+
43+
## Description
44+
45+
[YS](https://yamlscript.org) is a functional programming language with a clean
46+
YAML syntax.
47+
48+
YS can be used for enhancing ordinary [YAML](https://yaml.org) files with
49+
functional operations, such as:
50+
51+
* Import (parts of) other YAML files to any node
52+
* String interpolation including function calls
53+
* Data transforms including ones defined by you
54+
55+
This YS library should be a drop-in replacement for your current YAML loader!
56+
57+
Most existing YAML files are already valid YS files.
58+
This means that YS works as a normal YAML loader, but can also evaluate
59+
functional expressions if asked to.
60+
61+
Under the hood, YS code compiles to the Clojure programming language.
62+
This makes YS a complete functional programming language right out of the box.
63+
64+
Even though YS compiles to Clojure, and Clojure compiles to Java, there is no
65+
dependency on Java or the JVM.
66+
YS is compiled to a native shared library (`libys.so`) that can be used
67+
by any programming language that can load shared libraries.
68+
69+
To see the Clojure code that YS compiles to, you can use the YS
70+
CLI binary `ys` to run:
71+
72+
```text
73+
$ ys --compile file.ys
74+
(let
75+
[names-url "https://raw.githubusercontent.com/dominictarr/random-name/master/first-names.json"
76+
name-list (json/load (curl names-url))]
77+
(%
78+
"name" (first (shuffle name-list))
79+
"aka" (rand-nth name-list)
80+
"age" (_& 'num (mul+ 2 3 7))
81+
"color" (_& 'hue (rand-nth (qw red green blue yellow)))
82+
"title" (str (_** 'num) " shades of " (_** 'hue) ".")))
83+
```
84+
85+
## Lua Usage
86+
87+
File `prog.lua`:
88+
89+
```lua
90+
local yamlscript = require("yamlscript")
91+
local ys = yamlscript.new()
92+
local input = io.open('file.ys'):read('*a')
93+
local data = ys:load(input)
94+
print(data)
95+
```
96+
97+
File `file.ys`:
98+
99+
```yaml
100+
!YS-v0:
101+
102+
name =: "World"
103+
104+
foo: [1, 2, ! inc(41)]
105+
bar:: load("other.yaml")
106+
baz:: "Hello, $name!"
107+
```
108+
109+
File `other.yaml`:
110+
111+
```yaml
112+
oh: Hello
113+
```
114+
115+
Run:
116+
117+
```text
118+
$ lua prog.lua
119+
{foo={1,2,42}, bar={oh="Hello"}, baz="Hello, World!"}
120+
```
121+
122+
123+
## Installation
124+
125+
This Lua binding requires:
126+
127+
1. Standard Lua 5.1+ (not LuaJIT)
128+
2. The `cffi-lua` library for FFI capabilities
129+
3. The `cjson` library for JSON parsing
130+
4. A system install of `libys.so`
131+
132+
To install the dependencies:
133+
134+
```bash
135+
# Install Lua (if not already installed)
136+
sudo apt-get install lua5.4
137+
138+
# Install LuaRocks (if not already installed)
139+
sudo apt-get install luarocks
140+
141+
# Install required Lua libraries
142+
luarocks install cffi-lua
143+
luarocks install lua-cjson
144+
145+
# Install libys shared library
146+
curl https://yamlscript.org/install | bash
147+
```
148+
149+
> Note: The above command will install the latest version of the YAMLScript
150+
command line utility, `ys`, and the shared library, `libys.so`, into
151+
`~/local/bin` and `~/.local/lib` respectively.
152+
153+
To use the binding, add the `lib/` directory to your `LUA_PATH`:
154+
155+
```bash
156+
export LUA_PATH="$(pwd)/lib/?.lua;;"
157+
```
158+
159+
See <https://yamlscript.org/doc/install/> for more info.
160+
161+
## See Also
162+
163+
* [YS Web Site](https://yamlscript.org)
164+
* [YS Blog](https://yamlscript.org/blog)
165+
* [YS Source Code](https://github.com/yaml/yamlscript)
166+
* [YS Samples](https://github.com/yaml/yamlscript/tree/main/sample)
167+
* [YS Programs](https://rosettacode.org/wiki/Category:YAMLScript)
168+
* [YAML](https://yaml.org)
169+
* [Clojure](https://clojure.org)
170+
171+
172+
## Authors
173+
174+
## Authors
175+
176+
* Ingy döt Net <ingy@ingy.net>
177+
178+
## License & Copyright
179+
180+
Copyright 2022-2025 Ingy döt Net <ingy@ingy.net>
181+
182+
This project is licensed under the terms of the `MIT` license.
183+
See [LICENSE](https://github.com/yaml/yamlscript/blob/main/License) for
184+
more details.

lua/doc/authors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Authors
2+
3+
* Ingy döt Net <ingy@ingy.net>

lua/doc/readme.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Lua Usage
2+
3+
File `prog.lua`:
4+
5+
```lua
6+
local yamlscript = require("yamlscript")
7+
local ys = yamlscript.new()
8+
local input = io.open('file.ys'):read('*a')
9+
local data = ys:load(input)
10+
print(data)
11+
```
12+
13+
File `file.ys`:
14+
15+
```yaml
16+
!YS-v0:
17+
18+
name =: "World"
19+
20+
foo: [1, 2, ! inc(41)]
21+
bar:: load("other.yaml")
22+
baz:: "Hello, $name!"
23+
```
24+
25+
File `other.yaml`:
26+
27+
```yaml
28+
oh: Hello
29+
```
30+
31+
Run:
32+
33+
```text
34+
$ lua prog.lua
35+
{foo={1,2,42}, bar={oh="Hello"}, baz="Hello, World!"}
36+
```
37+
38+
39+
## Installation
40+
41+
This Lua binding requires:
42+
43+
1. Standard Lua 5.1+ (not LuaJIT)
44+
2. The `cffi-lua` library for FFI capabilities
45+
3. The `cjson` library for JSON parsing
46+
4. A system install of `libys.so`
47+
48+
To install the dependencies:
49+
50+
```bash
51+
# Install Lua (if not already installed)
52+
sudo apt-get install lua5.4
53+
54+
# Install LuaRocks (if not already installed)
55+
sudo apt-get install luarocks
56+
57+
# Install required Lua libraries
58+
luarocks install cffi-lua
59+
luarocks install lua-cjson
60+
61+
# Install libys shared library
62+
curl https://yamlscript.org/install | bash
63+
```
64+
65+
> Note: The above command will install the latest version of the YAMLScript
66+
command line utility, `ys`, and the shared library, `libys.so`, into
67+
`~/local/bin` and `~/.local/lib` respectively.
68+
69+
To use the binding, add the `lib/` directory to your `LUA_PATH`:
70+
71+
```bash
72+
export LUA_PATH="$(pwd)/lib/?.lua;;"
73+
```
74+
75+
See <https://yamlscript.org/doc/install/> for more info.

0 commit comments

Comments
 (0)