summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/lowlevel/lowlevel-grouping.tex
blob: 12d12f7f4d4c0e5bcb49c58d8b196445ea51ef9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
% language=us runpath=texruns:manuals/lowlevel

\environment lowlevel-style

\startdocument
  [title=grouping,
   color=middlecyan]

\startsectionlevel[title=Introduction]

This is a rather short explanation. I decided to write it after presenting the
other topics at the 2019 \CONTEXT\ meeting where there was a question about
grouping.

\stopsectionlevel

\startsectionlevel[title=\PASCAL]

In a language like \PASCAL, the language that \TEX\ has been written in, or
\MODULA, its successor, there is no concept of grouping like in \TEX. But we can
find keywords that suggests this:

\starttyping
for i := 1 to 10 do begin ... end
\stoptyping

This language probably inspired some of the syntax of \TEX\ and \METAPOST. For
instance an assignment in \METAPOST\ uses \type {:=} too. However, the \type
{begin} and \type {end} don't really group but define a block of statements. You
can have local variables in a procedure or function but the block is just a way
to pack a sequence of statements.

\stopsectionlevel

\startsectionlevel[title=\TEX]

In \TEX\ macros (or source code) the following can occur:

\starttyping
\begingroup
    ...
\endgroup
\stoptyping

as well as:

\starttyping
\bgroup
    ...
\egroup
\stoptyping

Here we really group in the sense that assignments to variables inside a group
are forgotten afterwards. All assignments are local to the group unless they are
explicitly done global:

\starttyping
\scratchcounter=1
\def\foo{foo}
\begingroup
    \scratchcounter=2
    \global\globalscratchcounter=2
    \gdef\foo{FOO}
\endgroup
\stoptyping

Here \type {\scratchcounter} is still one after the group is left but its global
counterpart is now two. The \type {\foo} macro is also changed globally.

Although you can use both sets of commands to group, you cannot mix them, so this
will trigger an error:

\starttyping
\bgroup
\endgroup
\stoptyping

The bottomline is: if you want a value to persist after the group, you need to
explicitly change its value globally. This makes a lot of sense in the perspective
of \TEX.

\stopsectionlevel

\startsectionlevel[title=\METAPOST]

The \METAPOST\ language also has a concept of grouping but in this case it's more like a
programming language.

\starttyping
begingroup ;
    n := 123 ;
engroup ;
\stoptyping

In this case the value of \type {n} is 123 after the group is left, unless you do
this (for numerics there is actually no need to declare them):

\starttyping
begingroup ;
    save n ; numeric n ; n := 123 ;
engroup ;
\stoptyping

Given the use of \METAPOST\ (read: \METAFONT) this makes a lot of sense: often
you use macros to simplify code and you do want variables to change. Grouping in
this language serves other purposes, like hiding what is between these commands
and let the last expression become the result. In a \type {vardef} grouping is
implicit.

So, in \METAPOST\ all assignments are global, unless a variable is explicitly
saved inside a group.

\stopsectionlevel

\startsectionlevel[title=\LUA]

In \LUA\ all assignments are global unless a variable is defines local:

\starttyping
local x = 1
local y = 1
for i = 1, 10 do
    local x = 2
    y = 2
end
\stoptyping

Here the value of \type {x} after the loop is still one but \type {y} is now two.
As in \LUATEX\ we mix \TEX, \METAPOST\ and \LUA\ you can mix up these concepts.
Another mixup is using \type {:=}, \type {endfor}, \type {fi} in \LUA\ after done
some \METAPOST\ coding or using \type {end} instead of \type {endfor} in
\METAPOST\ which can make the library wait for more without triggering an error.
Proper syntax highlighting in an editor clearly helps.

\stopsectionlevel

\startsectionlevel[title=\CCODE]

The \LUA\ language is a mix between \PASCAL\ (which is one reason why I like it)
and \CCODE.

\starttyping
int x = 1 ;
int y = 1 ;
for (i=1; i<=10;i++) {
    int x = 2 ;
    y = 2 ;
}
\stoptyping

The semicolon is also used in \PASCAL\ but there it is a separator and not a
statement end, while in \METAPOST\ it does end a statement (expression).

\stopsectionlevel

\stopdocument