|
| 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. |
0 commit comments