From 4bf64f65611d6012f649e8cf5e1a329095611668 Mon Sep 17 00:00:00 2001 From: Mattia Giambirtone Date: Mon, 1 Aug 2022 11:03:49 +0200 Subject: [PATCH] Updated the manual and changed the syntax for foreach loops --- docs/manual.md | 15 ++++++++------- src/frontend/parser.nim | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/manual.md b/docs/manual.md index 07317b7..6bfc616 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -57,7 +57,7 @@ __Note__: Peon supports [name stropping](https://en.wikipedia.org/wiki/Stropping ``` fn fib(n: int): int { - if (n < 3) { + if n < 3 { return n; } return fib(n - 1) + fib(n - 2); @@ -109,7 +109,7 @@ __Note__: Code the likes of `a.b()` is desugared to `b(a)` if there exists a fun ### Generic declarations ``` -fn genericSum[T](a, b: T): T { # Note: "a, b: T" means that both a and b are of type T +fn genericSum[T: Number](a, b: T): T { # Note: "a, b: T" means that both a and b are of type T return a + b; } @@ -123,7 +123,7 @@ genericSum(1'u8, 250'u8); #### Multiple generics ``` -fn genericSth[T, K](a: T, b: K) { # Note: no return type == void function! +fn genericSth[T: someInterface, K: someInterface2](a: T, b: K) { # Note: no return type == void function! # code... } @@ -149,7 +149,7 @@ fn someF: int { ``` generator count(n: int): int { - while (n > 0) { + while n > 0 { yield n; n -= 1; } @@ -175,12 +175,13 @@ coroutine req(url: string): string { coroutine main(urls: list[string]) { pool = concur.pool(); # Creates a task pool: like a nursery in njsmith's article - for (var i = 0; i < urls.len(); i += 1) { - pool.spawn(req, urls[i]); + foreach url in urls { + pool.spawn(req, urls); } # The pool has internal machinery that makes the parent # task wait until all child exit! When this function - # returns, ALL child tasks will have exited somehow + # returns, ALL child tasks will have exited somehow. + # Exceptions and return values propagate neatly, too. } diff --git a/src/frontend/parser.nim b/src/frontend/parser.nim index dbe8f10..8f0391b 100644 --- a/src/frontend/parser.nim +++ b/src/frontend/parser.nim @@ -655,7 +655,7 @@ proc forEachStmt(self: Parser): Statement = self.currentLoop = Loop self.expect(Identifier) let identifier = newIdentExpr(self.peek(-1), self.scopeDepth) - self.expect(":") + self.expect("in") let expression = self.expression() self.expect(LeftBrace) result = newForEachStmt(identifier, expression, self.blockStmt(), tok)