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
+422
View File
@@ -0,0 +1,422 @@
<!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>Classes - 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" class="active">Classes</a></li>
<li><a href="methods.html">Methods</a></li>
<li><a href="control-flow.html">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>Classes</span>
</nav>
<article>
<h1>Classes</h1>
<p>Wren is a class-based object-oriented language. Everything in Wren is an object, and every object is an instance of a class.</p>
<h2>Defining Classes</h2>
<p>Define a class with the <code>class</code> keyword:</p>
<pre><code>class Animal {
}</code></pre>
<p>This creates a class named <code>Animal</code> with no methods or fields.</p>
<h2>Constructors</h2>
<p>Constructors create new instances. Define them with <code>construct</code>:</p>
<pre><code>class Person {
construct new(name, age) {
_name = name
_age = age
}
}
var alice = Person.new("Alice", 30)</code></pre>
<p>A class can have multiple named constructors:</p>
<pre><code>class Point {
construct new(x, y) {
_x = x
_y = y
}
construct origin() {
_x = 0
_y = 0
}
construct fromList(list) {
_x = list[0]
_y = list[1]
}
}
var p1 = Point.new(3, 4)
var p2 = Point.origin()
var p3 = Point.fromList([5, 6])</code></pre>
<h2>Fields</h2>
<p>Instance fields are prefixed with <code>_</code>. They are private to the class:</p>
<pre><code>class Counter {
construct new() {
_count = 0
}
increment() {
_count = _count + 1
}
count { _count }
}</code></pre>
<p>Fields are not declared; they are created when first assigned.</p>
<h2>Getters and Setters</h2>
<p>Getters are methods without parentheses:</p>
<pre><code>class Circle {
construct new(radius) {
_radius = radius
}
radius { _radius }
area { 3.14159 * _radius * _radius }
}
var c = Circle.new(5)
System.print(c.radius) // 5
System.print(c.area) // 78.53975</code></pre>
<p>Setters use <code>=</code> suffix:</p>
<pre><code>class Circle {
construct new(radius) {
_radius = radius
}
radius { _radius }
radius=(value) { _radius = value }
}
var c = Circle.new(5)
c.radius = 10
System.print(c.radius) // 10</code></pre>
<h2>Methods</h2>
<p>Methods are defined inside the class body:</p>
<pre><code>class Rectangle {
construct new(width, height) {
_width = width
_height = height
}
area() {
return _width * _height
}
perimeter() {
return 2 * (_width + _height)
}
}
var rect = Rectangle.new(4, 5)
System.print(rect.area()) // 20
System.print(rect.perimeter()) // 18</code></pre>
<h2>Static Members</h2>
<p>Static methods and fields belong to the class, not instances:</p>
<pre><code>class Math {
static pi { 3.14159 }
static square(x) {
return x * x
}
static cube(x) {
return x * x * x
}
}
System.print(Math.pi) // 3.14159
System.print(Math.square(4)) // 16
System.print(Math.cube(3)) // 27</code></pre>
<p>Static fields use double underscore:</p>
<pre><code>class Counter {
static count { __count }
static increment() {
if (__count == null) __count = 0
__count = __count + 1
}
}
Counter.increment()
Counter.increment()
System.print(Counter.count) // 2</code></pre>
<h2>Inheritance</h2>
<p>Classes can inherit from a single superclass using <code>is</code>:</p>
<pre><code>class Animal {
construct new(name) {
_name = name
}
name { _name }
speak() {
System.print("...")
}
}
class Dog is Animal {
construct new(name, breed) {
super(name)
_breed = breed
}
breed { _breed }
speak() {
System.print("Woof!")
}
}
var dog = Dog.new("Rex", "German Shepherd")
System.print(dog.name) // Rex
System.print(dog.breed) // German Shepherd
dog.speak() // Woof!</code></pre>
<h3>Calling Super</h3>
<p>Use <code>super</code> to call the superclass constructor or methods:</p>
<pre><code>class Parent {
construct new() {
_value = 10
}
value { _value }
describe() {
System.print("Parent value: %(_value)")
}
}
class Child is Parent {
construct new() {
super()
_extra = 20
}
describe() {
super.describe()
System.print("Child extra: %(_extra)")
}
}
var child = Child.new()
child.describe()
// Output:
// Parent value: 10
// Child extra: 20</code></pre>
<h2>This</h2>
<p>Use <code>this</code> to refer to the current instance:</p>
<pre><code>class Node {
construct new(value) {
_value = value
_next = null
}
value { _value }
next { _next }
append(value) {
_next = Node.new(value)
return this
}
}
var n = Node.new(1).append(2).append(3)</code></pre>
<h2>Object Class</h2>
<p>All classes implicitly inherit from <code>Object</code>:</p>
<pre><code>class Foo {}
System.print(Foo is Class) // true
System.print(Foo.supertype) // Object</code></pre>
<h2>Type Checking</h2>
<p>Use <code>is</code> to check if an object is an instance of a class:</p>
<pre><code>var dog = Dog.new("Rex", "Shepherd")
System.print(dog is Dog) // true
System.print(dog is Animal) // true
System.print(dog is Object) // true
System.print(dog is String) // false</code></pre>
<p>Get the class of an object with <code>type</code>:</p>
<pre><code>System.print(dog.type) // Dog
System.print(dog.type.name) // Dog
System.print(dog.type.supertype) // Animal</code></pre>
<h2>Foreign Classes</h2>
<p>Foreign classes are implemented in C. They can hold native data:</p>
<pre><code>foreign class Socket {
construct new() {}
foreign connect(host, port)
foreign send(data)
foreign receive()
foreign close()
}</code></pre>
<p>Foreign classes are used by built-in modules to provide native functionality.</p>
<h2>Complete Example</h2>
<pre><code>class Shape {
construct new() {}
area { 0 }
perimeter { 0 }
describe() {
System.print("Area: %(area)")
System.print("Perimeter: %(perimeter)")
}
}
class Rectangle is Shape {
construct new(width, height) {
_width = width
_height = height
}
width { _width }
height { _height }
area { _width * _height }
perimeter { 2 * (_width + _height) }
}
class Square is Rectangle {
construct new(side) {
super(side, side)
}
}
class Circle is Shape {
construct new(radius) {
_radius = radius
}
static pi { 3.14159 }
radius { _radius }
area { Circle.pi * _radius * _radius }
perimeter { 2 * Circle.pi * _radius }
}
var shapes = [
Rectangle.new(4, 5),
Square.new(3),
Circle.new(2)
]
for (shape in shapes) {
System.print("%(shape.type.name):")
shape.describe()
System.print("")
}</code></pre>
</article>
<footer class="page-footer">
<a href="index.html" class="prev">Syntax Overview</a>
<a href="methods.html" class="next">Methods</a>
</footer>
</main>
</div>
<script src="../js/main.js"></script>
</body>
</html>