feat: add await syntax for direct async function calls and new generator/phonebook apps
Add compiler support for calling async functions directly with `await fn(args)` syntax, automatically translating to `await fn.call(args)`. Introduce `create_merge` Makefile target for merging Wren source files. Include new `apps/generator.wren` source code generator using OpenRouter API, `apps/phonebook.wren` CLI phonebook with SQLite backend, and `apps/minitut.md` Wren tutorial reference. Update `example/await_demo.wren` with parameterized async functions, nested awaits, and expression usage. Fix JSON parsing in `example/openrouter_demo.wren` to strip markdown code fences. Document new await syntax in `manual/api/scheduler.html`.
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "scheduler" for Scheduler, Future
|
||||
|
||||
class Counter {
|
||||
construct new(start) {
|
||||
_value = start
|
||||
}
|
||||
|
||||
value { _value }
|
||||
|
||||
add(n) {
|
||||
var op = async { |x|
|
||||
_value = _value + x
|
||||
return _value
|
||||
}
|
||||
return await op(n)
|
||||
}
|
||||
|
||||
subtract(n) {
|
||||
var op = async { |x|
|
||||
_value = _value - x
|
||||
return _value
|
||||
}
|
||||
return await op(n)
|
||||
}
|
||||
|
||||
reset() {
|
||||
var op = async {
|
||||
_value = 0
|
||||
return _value
|
||||
}
|
||||
return await op()
|
||||
}
|
||||
}
|
||||
|
||||
var counter = Counter.new(10)
|
||||
System.print(counter.value) // expect: 10
|
||||
|
||||
System.print(counter.add(5)) // expect: 15
|
||||
System.print(counter.value) // expect: 15
|
||||
|
||||
System.print(counter.subtract(3)) // expect: 12
|
||||
System.print(counter.value) // expect: 12
|
||||
|
||||
System.print(counter.reset()) // expect: 0
|
||||
System.print(counter.value) // expect: 0
|
||||
|
||||
System.print(counter.add(10)) // expect: 10
|
||||
System.print(counter.subtract(3)) // expect: 7
|
||||
Reference in New Issue
Block a user