feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules

Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
This commit is contained in:
2026-01-24 22:40:22 +00:00
parent 0a65272f80
commit 5a4054ec66
105 changed files with 23275 additions and 94 deletions
+332
View File
@@ -0,0 +1,332 @@
<!DOCTYPE html>
<!-- retoor <retoor@molodetz.nl> -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Control Flow - Wren-CLI Manual</title>
<link rel="stylesheet" href="../css/style.css">
</head>
<body>
<button class="mobile-menu-toggle">Menu</button>
<div class="container">
<aside class="sidebar">
<div class="sidebar-header">
<h1><a href="../index.html">Wren-CLI</a></h1>
<div class="version">v0.4.0</div>
</div>
<nav class="sidebar-nav">
<div class="section">
<span class="section-title">Getting Started</span>
<ul>
<li><a href="../getting-started/index.html">Overview</a></li>
<li><a href="../getting-started/installation.html">Installation</a></li>
<li><a href="../getting-started/first-script.html">First Script</a></li>
<li><a href="../getting-started/repl.html">Using the REPL</a></li>
</ul>
</div>
<div class="section">
<span class="section-title">Language</span>
<ul>
<li><a href="index.html">Syntax Overview</a></li>
<li><a href="classes.html">Classes</a></li>
<li><a href="methods.html">Methods</a></li>
<li><a href="control-flow.html" class="active">Control Flow</a></li>
<li><a href="fibers.html">Fibers</a></li>
<li><a href="modules.html">Modules</a></li>
</ul>
</div>
<div class="section">
<span class="section-title">API Reference</span>
<ul>
<li><a href="../api/index.html">Overview</a></li>
<li><a href="../api/http.html">http</a></li>
<li><a href="../api/websocket.html">websocket</a></li>
<li><a href="../api/tls.html">tls</a></li>
<li><a href="../api/net.html">net</a></li>
<li><a href="../api/dns.html">dns</a></li>
<li><a href="../api/json.html">json</a></li>
<li><a href="../api/base64.html">base64</a></li>
<li><a href="../api/regex.html">regex</a></li>
<li><a href="../api/jinja.html">jinja</a></li>
<li><a href="../api/crypto.html">crypto</a></li>
<li><a href="../api/os.html">os</a></li>
<li><a href="../api/env.html">env</a></li>
<li><a href="../api/signal.html">signal</a></li>
<li><a href="../api/subprocess.html">subprocess</a></li>
<li><a href="../api/sqlite.html">sqlite</a></li>
<li><a href="../api/datetime.html">datetime</a></li>
<li><a href="../api/timer.html">timer</a></li>
<li><a href="../api/io.html">io</a></li>
<li><a href="../api/scheduler.html">scheduler</a></li>
<li><a href="../api/math.html">math</a></li>
</ul>
</div>
<div class="section">
<span class="section-title">Tutorials</span>
<ul>
<li><a href="../tutorials/index.html">Tutorial List</a></li>
<li><a href="../tutorials/http-client.html">HTTP Client</a></li>
<li><a href="../tutorials/websocket-chat.html">WebSocket Chat</a></li>
<li><a href="../tutorials/database-app.html">Database App</a></li>
<li><a href="../tutorials/template-rendering.html">Templates</a></li>
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
</ul>
</div>
<div class="section">
<span class="section-title">How-To Guides</span>
<ul>
<li><a href="../howto/index.html">How-To List</a></li>
<li><a href="../howto/http-requests.html">HTTP Requests</a></li>
<li><a href="../howto/json-parsing.html">JSON Parsing</a></li>
<li><a href="../howto/regex-patterns.html">Regex Patterns</a></li>
<li><a href="../howto/file-operations.html">File Operations</a></li>
<li><a href="../howto/async-operations.html">Async Operations</a></li>
<li><a href="../howto/error-handling.html">Error Handling</a></li>
</ul>
</div>
</nav>
</aside>
<main class="content">
<nav class="breadcrumb">
<a href="../index.html">Home</a>
<span class="separator">/</span>
<a href="index.html">Language</a>
<span class="separator">/</span>
<span>Control Flow</span>
</nav>
<article>
<h1>Control Flow</h1>
<p>Wren provides standard control flow constructs for conditionals, loops, and early exit.</p>
<h2>Truthiness</h2>
<p>Before covering control flow, understand how Wren evaluates truthiness:</p>
<ul>
<li><code>false</code> is falsy</li>
<li><code>null</code> is falsy</li>
<li>Everything else is truthy (including <code>0</code>, <code>""</code>, <code>[]</code>)</li>
</ul>
<pre><code>if (0) System.print("0 is truthy")
if ("") System.print("empty string is truthy")
if ([]) System.print("empty list is truthy")
if (false) System.print("false is falsy") // Not printed
if (null) System.print("null is falsy") // Not printed</code></pre>
<h2>If Statements</h2>
<p>Basic conditional execution:</p>
<pre><code>if (condition) {
System.print("condition is true")
}</code></pre>
<h3>If-Else</h3>
<pre><code>if (score >= 90) {
System.print("A")
} else {
System.print("Not A")
}</code></pre>
<h3>If-Else If-Else</h3>
<pre><code>if (score >= 90) {
System.print("A")
} else if (score >= 80) {
System.print("B")
} else if (score >= 70) {
System.print("C")
} else {
System.print("F")
}</code></pre>
<h3>Single Expression</h3>
<p>For single expressions, braces are optional:</p>
<pre><code>if (x > 0) System.print("positive")</code></pre>
<h2>Ternary Operator</h2>
<p>For inline conditionals:</p>
<pre><code>var status = age >= 18 ? "adult" : "minor"
var max = a > b ? a : b</code></pre>
<h2>Logical Operators</h2>
<h3>And (&&)</h3>
<p>Returns the first falsy value or the last value:</p>
<pre><code>System.print(true && false) // false
System.print(true && true) // true
System.print(1 && 2) // 2
System.print(null && 1) // null</code></pre>
<h3>Or (||)</h3>
<p>Returns the first truthy value or the last value:</p>
<pre><code>System.print(false || true) // true
System.print(false || false) // false
System.print(null || "default") // default
System.print(1 || 2) // 1</code></pre>
<p>Use <code>||</code> for default values:</p>
<pre><code>var name = providedName || "Anonymous"</code></pre>
<h2>While Loops</h2>
<p>Repeat while a condition is true:</p>
<pre><code>var i = 0
while (i &lt; 5) {
System.print(i)
i = i + 1
}</code></pre>
<div class="example-output">0
1
2
3
4</div>
<h2>For Loops</h2>
<p>Iterate over any sequence:</p>
<pre><code>for (item in [1, 2, 3]) {
System.print(item)
}</code></pre>
<h3>Range Iteration</h3>
<pre><code>for (i in 1..5) {
System.print(i)
}
// Prints: 1 2 3 4 5
for (i in 1...5) {
System.print(i)
}
// Prints: 1 2 3 4 (exclusive)</code></pre>
<h3>String Iteration</h3>
<pre><code>for (char in "hello") {
System.print(char)
}
// Prints each character</code></pre>
<h3>Map Iteration</h3>
<pre><code>var person = {"name": "Alice", "age": 30}
for (key in person.keys) {
System.print("%(key): %(person[key])")
}</code></pre>
<h2>Break</h2>
<p>Exit a loop early:</p>
<pre><code>for (i in 1..100) {
if (i > 5) break
System.print(i)
}
// Prints: 1 2 3 4 5</code></pre>
<h2>Continue</h2>
<p>Skip to the next iteration:</p>
<pre><code>for (i in 1..10) {
if (i % 2 == 0) continue
System.print(i)
}
// Prints: 1 3 5 7 9 (odd numbers only)</code></pre>
<h2>Block Scoping</h2>
<p>Blocks create new scopes:</p>
<pre><code>var x = "outer"
{
var x = "inner"
System.print(x) // inner
}
System.print(x) // outer</code></pre>
<p>Variables declared in a block are not visible outside:</p>
<pre><code>if (true) {
var temp = "temporary"
}
// temp is not accessible here</code></pre>
<h2>Iterating with Index</h2>
<p>Use range to get indices:</p>
<pre><code>var list = ["a", "b", "c"]
for (i in 0...list.count) {
System.print("%(i): %(list[i])")
}</code></pre>
<div class="example-output">0: a
1: b
2: c</div>
<h2>Infinite Loops</h2>
<p>Create with <code>while (true)</code>:</p>
<pre><code>var count = 0
while (true) {
count = count + 1
if (count >= 5) break
System.print(count)
}</code></pre>
<h2>Nested Loops</h2>
<pre><code>for (i in 1..3) {
for (j in 1..3) {
System.print("%(i), %(j)")
}
}</code></pre>
<p>Break only exits the innermost loop:</p>
<pre><code>for (i in 1..3) {
for (j in 1..10) {
if (j > 2) break // Only breaks inner loop
System.print("%(i), %(j)")
}
}</code></pre>
<h2>Iteration Patterns</h2>
<h3>Filtering</h3>
<pre><code>var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
var evens = []
for (n in numbers) {
if (n % 2 == 0) evens.add(n)
}
System.print(evens) // [2, 4, 6, 8, 10]</code></pre>
<h3>Mapping</h3>
<pre><code>var numbers = [1, 2, 3, 4, 5]
var squared = []
for (n in numbers) {
squared.add(n * n)
}
System.print(squared) // [1, 4, 9, 16, 25]</code></pre>
<h3>Finding</h3>
<pre><code>var numbers = [1, 3, 5, 8, 9, 11]
var firstEven = null
for (n in numbers) {
if (n % 2 == 0) {
firstEven = n
break
}
}
System.print(firstEven) // 8</code></pre>
<h3>Reducing</h3>
<pre><code>var numbers = [1, 2, 3, 4, 5]
var sum = 0
for (n in numbers) {
sum = sum + n
}
System.print(sum) // 15</code></pre>
<h2>Functional Alternatives</h2>
<p>Lists provide functional methods that are often cleaner:</p>
<pre><code>var numbers = [1, 2, 3, 4, 5]
var evens = numbers.where { |n| n % 2 == 0 }.toList
var squared = numbers.map { |n| n * n }.toList
var sum = numbers.reduce(0) { |acc, n| acc + n }</code></pre>
</article>
<footer class="page-footer">
<a href="methods.html" class="prev">Methods</a>
<a href="fibers.html" class="next">Fibers</a>
</footer>
</main>
</div>
<script src="../js/main.js"></script>
</body>
</html>