forked from nim-works/nimskull
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOC] Control flow with procedure calls
- Loading branch information
1 parent
4206347
commit 805d122
Showing
16 changed files
with
350 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
167 changes: 162 additions & 5 deletions
167
tests/lang/s01_basics/s08_control_flow/t06_procedure_calls.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,162 @@ | ||
## This specifices how control flow is transferred in the procedure body. It | ||
## also shows how exceptions, `defer` and `return` affect the control flow. | ||
## Note - even though exceptions are used in this section. it is **not** a specification | ||
## on how exceptions in general are to be used - (used-defined exceptions, coercion to a | ||
## parent exception types in `as` blocks, exception message, stack trace and so on). | ||
## This specifices how control flow is transferred in the procedure body. | ||
## It also shows how exceptions, `defer` and `return` affect the control | ||
## flow. Note - even though exceptions are used in this section. it is | ||
## **not** a specification on how exceptions in general are to be used - | ||
## (used-defined exceptions, coercion to a parent exception types in `as` | ||
## blocks, exception message, stack trace and so on). | ||
|
||
block simple_procedure: | ||
var values: seq[string] | ||
|
||
proc impl() = | ||
values.add "in proc" | ||
|
||
values.add "before proc" | ||
impl() | ||
values.add "after proc" | ||
|
||
doAssert values == @[ | ||
"before proc", | ||
"in proc", | ||
"after proc" | ||
] | ||
|
||
block call_other: | ||
var values: seq[string] | ||
|
||
proc impl2() = | ||
values.add "in-in proc" | ||
|
||
proc impl1() = | ||
values.add "in-before proc" | ||
impl2() | ||
values.add "in-after proc" | ||
|
||
impl1() | ||
|
||
doAssert values == @[ | ||
"in-before proc", | ||
"in-in proc", | ||
"in-after proc", | ||
] | ||
|
||
block self_recursive: | ||
var values: seq[string] | ||
var counter = 0 | ||
|
||
proc recurse() = | ||
let depth = $counter | ||
values.add "pre recurse " & depth | ||
|
||
inc counter | ||
if counter < 3: | ||
recurse() | ||
|
||
values.add "post recurse " & depth | ||
|
||
recurse() | ||
|
||
doAssert values == @[ | ||
"pre recurse 0", | ||
"pre recurse 1", | ||
"pre recurse 2", | ||
"post recurse 2", | ||
"post recurse 1", | ||
"post recurse 0", | ||
] | ||
|
||
block single_defer: | ||
var values: seq[string] | ||
|
||
proc impl() = | ||
defer: | ||
values.add "defer" | ||
|
||
values.add "body" | ||
|
||
impl() | ||
doAssert values == @["body", "defer"] | ||
|
||
block multiple_defers: | ||
## `defer` statements are executed in reverse order of their encontering | ||
## in procedure body. | ||
var values: seq[string] | ||
proc impl() = | ||
defer: values.add "defer 1" | ||
defer: values.add "defer 2" | ||
defer: | ||
## Nesting does not affect defer - code will be executed in regular | ||
## (sequential) manner. | ||
values.add "defer 3" | ||
defer: | ||
values.add "defer 4" | ||
|
||
values.add "body" | ||
|
||
impl() | ||
|
||
doAssert values == @[ | ||
"body", | ||
"defer 3", | ||
"defer 4", | ||
"defer 2", | ||
"defer 1", | ||
] | ||
|
||
block exception: | ||
var values: seq[string] | ||
|
||
proc raised() = | ||
values.add "before" | ||
raise (ref CatchableError)() | ||
values.add "after" | ||
|
||
try: raised() except: discard | ||
|
||
doAssert values == @["before"] | ||
|
||
|
||
|
||
block call_other_exceptino: | ||
var values: seq[string] | ||
|
||
proc impl2() = | ||
values.add "in-in proc" | ||
raise (ref CatchableError)() | ||
|
||
proc impl1() = | ||
values.add "in-before proc" | ||
impl2() | ||
values.add "in-after proc" | ||
|
||
try: impl1() except: discard | ||
|
||
doAssert values == @[ | ||
"in-before proc", | ||
"in-in proc", | ||
] | ||
|
||
block self_recursive_raise: | ||
var values: seq[string] | ||
var counter = 0 | ||
|
||
proc recurse() = | ||
let depth = $counter | ||
values.add "pre recurse " & depth | ||
|
||
inc counter | ||
if counter < 3: | ||
recurse() | ||
|
||
else: | ||
raise (ref CatchableError)() | ||
|
||
values.add "post recurse " & depth | ||
|
||
try: recurse() except: discard | ||
|
||
doAssert values == @[ | ||
"pre recurse 0", | ||
"pre recurse 1", | ||
"pre recurse 2" | ||
] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
tests/lang/s02_core/s01_user_defined_data_types/t07_typeclass.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
discard """ | ||
description: ''' | ||
PLACEHOLDER | ||
''' | ||
""" | ||
|
||
block builtin_typeclasses: | ||
block object_class: | ||
type Obj = object | ||
|
||
doAssert Obj() is object | ||
doAssert Obj is object | ||
doAssert Obj is typedesc | ||
doAssert Obj() isnot typedesc | ||
|
||
block enum_class: | ||
type Enum = enum a | ||
|
||
doAssert Enum is enum | ||
doAssert a is enum | ||
|
||
block distinct_class: | ||
type Dist = distinct int | ||
|
||
doAssert Dist is distinct | ||
doAssert Dist(12) is distinct | ||
|
||
block proc_class: | ||
proc test(): int = discard | ||
|
||
doAssert test is proc | ||
|
||
block tuple_Class: | ||
doAssert (1, 2) is tuple | ||
doAssert (1, 2) is (int, int) |
13 changes: 13 additions & 0 deletions
13
tests/lang/s02_core/s02_procedures/t01_regular_procs_dot_operators.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
discard """ | ||
description: ''' | ||
Specification of the experimental dot operators feature | ||
''' | ||
cmd: "nim $target -r -d:nimPreviewDotLikeOps $options $file" | ||
""" | ||
|
||
{.experimental: "dotOperators".} | ||
|
||
block int_dot_operator: | ||
template `.`(lhs: int, field: untyped): int = 1 | ||
|
||
doAssert 1.test == 1 |
Oops, something went wrong.