|
| 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 | +```yaml |
| 13 | +!YS-v0: |
| 14 | + |
| 15 | +# Get data from external sources: |
| 16 | +names-url =: |
| 17 | + "https://raw.githubusercontent.com/dominictarr/\ |
| 18 | + random-name/master/first-names.json" |
| 19 | + |
| 20 | +name-list =: &first-names json/load(curl(names-url)) |
| 21 | + |
| 22 | +# Data object with literal keys and generated values: |
| 23 | +name:: rand-nth(*first-names) |
| 24 | +aka:: name-list.rand-nth() |
| 25 | +age:: &num 2 * 3 * 7 |
| 26 | +color:: &hue qw(red green blue yellow) |
| 27 | + .shuffle() |
| 28 | + .first() |
| 29 | +title:: "$(*num) shades of $(*hue)." |
| 30 | +``` |
| 31 | +
|
| 32 | +and get: |
| 33 | +```json |
| 34 | +{ |
| 35 | + "name": "Dolores", |
| 36 | + "aka": "Anita", |
| 37 | + "age": 42, |
| 38 | + "color": "green", |
| 39 | + "title": "42 shades of green." |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +## Description |
| 45 | + |
| 46 | +[YS](https://yamlscript.org) is a functional programming language with a clean |
| 47 | +YAML syntax. |
| 48 | + |
| 49 | +YS can be used for enhancing ordinary [YAML](https://yaml.org) files with |
| 50 | +functional operations, such as: |
| 51 | + |
| 52 | +* Import (parts of) other YAML files to any node |
| 53 | +* String interpolation including function calls |
| 54 | +* Data transforms including ones defined by you |
| 55 | + |
| 56 | +This YS library should be a drop-in replacement for your current YAML loader! |
| 57 | + |
| 58 | +Most existing YAML files are already valid YS files. |
| 59 | +This means that YS works as a normal YAML loader, but can also evaluate |
| 60 | +functional expressions if asked to. |
| 61 | + |
| 62 | +Under the hood, YS code compiles to the Clojure programming language. |
| 63 | +This makes YS a complete functional programming language right out of the box. |
| 64 | + |
| 65 | +Even though YS compiles to Clojure, and Clojure compiles to Java, there is no |
| 66 | +dependency on Java or the JVM. |
| 67 | +YS is compiled to a native shared library (`libyamlscript.so`) that can be used |
| 68 | +by any programming language that can load shared libraries. |
| 69 | + |
| 70 | +To see the Clojure code that YS compiles to, you can use the YS |
| 71 | +CLI binary `ys` to run: |
| 72 | + |
| 73 | +```text |
| 74 | +$ ys --compile file.ys |
| 75 | +(let |
| 76 | + [names-url "https://raw.githubusercontent.com/dominictarr/random-name/master/first-names.json" |
| 77 | + name-list (_& 'first-names (json/load (curl names-url)))] |
| 78 | + (% |
| 79 | + "name" (rand-nth (_** 'first-names)) |
| 80 | + "aka" (rand-nth name-list) |
| 81 | + "age" (_& 'num (mul+ 2 3 7)) |
| 82 | + "color" (_& 'hue (first (shuffle (qw red green blue yellow)))) |
| 83 | + "title" (str (_** 'num) " shades of " (_** 'hue) "."))) |
| 84 | +``` |
| 85 | + |
| 86 | +## Crystal Usage |
| 87 | + |
| 88 | +Here's a simple example of using the YAMLScript Crystal binding: |
| 89 | + |
| 90 | +```crystal |
| 91 | +require "yamlscript" |
| 92 | +
|
| 93 | +# YAMLScript code |
| 94 | +ys_code = <<-YS |
| 95 | +!YS-v0 |
| 96 | +inc: 41 |
| 97 | +YS |
| 98 | +
|
| 99 | +# Load and evaluate the YAMLScript |
| 100 | +result = YAMLScript.load(ys_code) |
| 101 | +puts "Result: #{result}" # Output: Result: 42 |
| 102 | +
|
| 103 | +# Regular YAML also works |
| 104 | +yaml_code = "foo: bar" |
| 105 | +result = YAMLScript.load(yaml_code) |
| 106 | +puts "YAML result: #{result["foo"]}" # Output: YAML result: bar |
| 107 | +``` |
| 108 | + |
| 109 | +## Development |
| 110 | + |
| 111 | +To build and run tests: |
| 112 | + |
| 113 | +```bash |
| 114 | +# Clone the repository |
| 115 | +git clone https://github.com/yaml/yamlscript.git |
| 116 | +cd yamlscript/crystal |
| 117 | + |
| 118 | +# Build the binding and copy required libraries |
| 119 | +make build |
| 120 | + |
| 121 | +# Run the tests |
| 122 | +make test |
| 123 | + |
| 124 | +# Run the example |
| 125 | +make run-example |
| 126 | +``` |
| 127 | + |
| 128 | +## See Also |
| 129 | + |
| 130 | +* [YS Web Site](https://yamlscript.org) |
| 131 | +* [YS Blog](https://yamlscript.org/blog) |
| 132 | +* [YS Source Code](https://github.com/yaml/yamlscript) |
| 133 | +* [YS Samples](https://github.com/yaml/yamlscript/tree/main/sample) |
| 134 | +* [YS Programs](https://rosettacode.org/wiki/Category:YAMLScript) |
| 135 | +* [YAML](https://yaml.org) |
| 136 | +* [Clojure](https://clojure.org) |
| 137 | + |
| 138 | + |
| 139 | +## Authors |
| 140 | + |
| 141 | +* [Josephine Pfeiffer](https://github.com/pfeifferj) |
| 142 | +* [Ingy döt Net](https://github.com/ingydotnet) |
| 143 | + |
| 144 | +## License & Copyright |
| 145 | + |
| 146 | +Copyright 2022-2025 Ingy döt Net <[email protected]> |
| 147 | + |
| 148 | +This project is licensed under the terms of the `MIT` license. |
| 149 | +See [LICENSE](https://github.com/yaml/yamlscript/blob/main/License) for |
| 150 | +more details. |
0 commit comments