Files
wren/example/yaml_demo.wren
T
retoor 79ff93c9a2 feat: add pexpect and yaml modules with jinja from-import support
Add new pexpect module for process spawning and interaction, including Spawn class with expect, sendline, and timeout handling. Introduce yaml module for YAML parsing with support for nested structures, lists, and data types. Extend jinja template engine with brace tokenization and FromImportNode for macro imports across templates. Register all new foreign functions in modules.c and include generated demo scripts.
2026-01-25 21:09:29 +00:00

106 lines
2.6 KiB
JavaScript
Vendored

// retoor <retoor@molodetz.nl>
import "yaml" for Yaml
System.print("=== YAML Module Demo ===\n")
System.print("--- Basic Parsing ---")
var simple = Yaml.parse("name: Wren-CLI\nversion: 0.4.0")
System.print("Name: %(simple["name"])")
System.print("Version: %(simple["version"])")
System.print("\n--- Nested Structures ---")
var nested = "
database:
host: localhost
port: 5432
credentials:
user: admin
password: secret
"
var config = Yaml.parse(nested)
System.print("Host: %(config["database"]["host"])")
System.print("Port: %(config["database"]["port"])")
System.print("User: %(config["database"]["credentials"]["user"])")
System.print("\n--- Lists ---")
var listYaml = "
languages:
- Wren
- C
- Python
"
var langs = Yaml.parse(listYaml)
System.print("Languages:")
for (lang in langs["languages"]) {
System.print(" - %(lang)")
}
System.print("\n--- Complex Structure (Navigation) ---")
var navYaml = "
sections:
- title: Getting Started
pages:
- file: index
title: Overview
- file: installation
title: Installation
- title: API Reference
pages:
- file: yaml
title: yaml
"
var nav = Yaml.parse(navYaml)
for (section in nav["sections"]) {
System.print("Section: %(section["title"])")
for (page in section["pages"]) {
System.print(" - %(page["title"]) (%(page["file"]))")
}
}
System.print("\n--- Data Types ---")
var types = "
string: hello world
number: 42
float: 3.14159
bool_true: true
bool_false: false
null_value: null
quoted: \"with spaces\"
"
var data = Yaml.parse(types)
System.print("String: %(data["string"]) (%(data["string"].type))")
System.print("Number: %(data["number"]) (%(data["number"].type))")
System.print("Float: %(data["float"]) (%(data["float"].type))")
System.print("Bool true: %(data["bool_true"]) (%(data["bool_true"].type))")
System.print("Bool false: %(data["bool_false"]) (%(data["bool_false"].type))")
System.print("Null: %(data["null_value"])")
System.print("Quoted: %(data["quoted"])")
System.print("\n--- Stringify ---")
var obj = {
"name": "Example",
"count": 42,
"active": true,
"items": ["one", "two", "three"],
"nested": {
"key": "value"
}
}
System.print("Serialized:")
System.print(Yaml.stringify(obj))
System.print("\n--- Round-trip ---")
var original = "
title: Test
items:
- first
- second
"
var parsed = Yaml.parse(original)
var reserialized = Yaml.stringify(parsed)
var reparsed = Yaml.parse(reserialized)
System.print("Original title: %(parsed["title"])")
System.print("Reparsed title: %(reparsed["title"])")
System.print("Items match: %(parsed["items"][0] == reparsed["items"][0])")