Skip to content

Commit 6dd95e9

Browse files
authored
Merge pull request #3891 from grandixximo/issue-3880-fix
Clarify o-code numbering and scoping rules in docs
2 parents 69b1f80 + f5829c1 commit 6dd95e9

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

docs/src/gcode/o-code.adoc

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,28 @@ For example 'o100' is easier to see than 'O100' that it is not a '0'.
2424

2525
== Numbering
2626

27-
Numbered o-codes must have a unique number for each subroutine,
27+
There are two categories of o-codes with different scoping rules:
28+
29+
*Subroutine definitions* (`sub`/`endsub`, `call`) are *global*.
30+
Each subroutine must have a unique number or name across the entire program
31+
and all called files.
32+
33+
*Control flow* (`if`/`endif`, `while`/`endwhile`, `do`/`while`,
34+
`repeat`/`endrepeat`, `break`, `continue`) are *local* to the subroutine
35+
or main program where they appear. The interpreter automatically scopes them,
36+
so `o100 if` inside `o<helper>` does not conflict with `o100 if` in the main
37+
program or in any other subroutine. You can safely reuse the same o-numbers
38+
for control flow in different subroutines.
39+
40+
Within a single subroutine body (or the main program), each o-number should
41+
still be used for only one control flow block.
2842

2943
.Numbering Example
3044
[source,{ngc}]
3145
----
3246
(the start of o100)
3347
o100 sub
34-
(notice that the if-endif block uses a different number)
48+
(notice that the if-endif block uses a different number within this sub)
3549
(the start of o110)
3650
o110 if [#2 GT 5]
3751
(some code here)
@@ -42,6 +56,26 @@ o100 sub
4256
o100 endsub
4357
----
4458

59+
.Reusing Control Flow Numbers Across Subroutines (valid)
60+
[source,{ngc}]
61+
----
62+
(o100 if in helper.ngc does not conflict with o100 if in another.ngc)
63+
64+
(file: helper.ngc)
65+
o<helper> sub
66+
o100 if [#1 GT 5]
67+
(do something)
68+
o100 endif
69+
o<helper> endsub
70+
71+
(file: another.ngc)
72+
o<another> sub
73+
o100 if [#1 LT 0]
74+
(do something else)
75+
o100 endif
76+
o<another> endsub
77+
----
78+
4579
[[ocode:comments]]
4680
== Comments(((Comments)))
4781

@@ -50,7 +84,8 @@ change in the future.
5084

5185
The behavior is undefined if:
5286

53-
* The same number is used for more than one block.
87+
* The same number is used for more than one block within the same scope
88+
(see <<Numbering>> for scoping rules).
5489
* Other words are used on a line with an o-word.
5590
* Comments are used on a line with an o-word.
5691

0 commit comments

Comments
 (0)