-
Notifications
You must be signed in to change notification settings - Fork 1
/
programflow.tex
172 lines (139 loc) · 5.44 KB
/
programflow.tex
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
\chapter{Program Flow Statements}
%% \madx consists of a core program, and modules for specific tasks such as
%% twiss parameter calculation, matching, thin lens tracking, and so on.
%% The statements listed here are those executed by the program core. They
%% deal with the I/O, element and sequence declaration, sequence
%% manipulation, statement flow control (e.g. IF, WHILE), MACRO
%% declaration, saving sequences onto files in \madx or \madeight format,
%% and so on.
\section{IF...ELSEIF...ELSE}
\label{sec:if}
\madbox{
IF (logical\_expression) \{ statements; \} \\ \\
ELSEIF (logical\_expression) \{ statements; \} \\ \\
ELSE \{ statements; \}
}
where "logical\_expression" is one of
\madxmp{
xxxxxxx\=xxxxxxxxxxxxxxxx\=xxxxx\= \kill
expr1 \>oper expr2 \\
expr11 \>oper1 expr12 \>\&\& \>expr21 oper2 expr22 \\
expr11 \>oper1 expr12 \>|| \>expr21 oper2 expr22
}
and \texttt{oper} is one of
\begin{madlist}
\ttitem{==} equal
\ttitem{<>} not equal
\ttitem{<} less than
\ttitem{>} greater than
\ttitem{<=} less than or equal
\ttitem{>=} greater than or equal
\end{madlist}
The expressions are arithmetic expressions of type real. The statements
in the curly brackets are executed if the logical expression is true.
\texttt{ELSEIF} constructs are only possible (in any number) behind an
\texttt{IF}, or another \texttt{ELSEIF}; the branch is executed if
"logical\_expression" is true, and if none of the preceding \texttt{IF}
or \texttt{ELSEIF} logical conditions was true.
\texttt{ELSE} construct is only possible once behind an \texttt{IF}, or
an \texttt{ELSEIF}; the branch is executed if "logical\_expression" is
true, and if none of the preceding \texttt{IF} or \texttt{ELSEIF}
logical conditions was true.
\textbf{Warning:}\\
Because \texttt{IF ... ELSEIF ... ELSE} constructs are a \madx special
feature and not part of a full language, \madx does not deal gracefully
with other special constructs such as \texttt{MACRO} or \texttt{LINE}
when they are placed inside \texttt{IF ... ELSEIF ... ELSE} statements:
this can lead to silent and/or catastrophic errors and is due to the
fact that \texttt{MACRO} and \texttt{LINE} constructs contain,
either explicitly or implicitly, a closing curly bracket that unbalances
the \texttt{IF ... ELSEIF ... ELSE} statements.
However it is possible to nest \texttt{IF ... ELSEIF ... ELSE}
constructs to at least six levels deep.
\section{WHILE}
\label{sec:while}
\madbox{
WHILE (logical\_condition) \{ statements; \}
}
executes the statements in curly brackets while the logical\_expression
is true.
\textbf{Warning:}\\
Because \texttt{WHILE} constructs are a \madx special
feature and not part of a full language, \madx does not deal gracefully
with other special constructs such as \texttt{MACRO} or \texttt{LINE}
when they are placed inside \texttt{WHILE} statement blocks: this can
lead to silent and/or catastrophic errors and is due to the fact that
\texttt{MACRO} and \texttt{LINE} constructs contain, either explicitly or
implicitly, a closing curly bracket that unbalances the \texttt{WHILE}
statements.
However it is possible to nest \texttt{WHILE} statements to at least six
levels deep.
Example giving the value of the first ten factorials:
\madxmp{
n = 1; m = 1; \\
while (n <= 10) \{ \\
\qquad m = m * n; value, m; \\
\qquad n = n + 1; \\
\};
}
\section{MACRO}
\label{sec:macro}
The MACRO construct allows the execution of a group of statements via a
single command. Optionally the MACRO construct takes arguments.
\madbox{
label: MACRO = \{ statements; \}; \\
\\
label(arg1, \ldots ,argn): MACRO = \{ statements; \};
}
The first form allows the execution of the defined group of statements via a
single command,
\madxmp{EXEC, label;}
that executes the statements defined between curly brackets exactly
once. The \texttt{EXEC} command can then be issued any number of times.
The second form allows to replace strings anywhere inside the statements
in curly brackets by other strings, or integer numbers prior to
execution. This is a powerful construct and should be handled with care.
Simple example:
\madxmp{
simple(xx,yy): MACRO = \{ xx = yy*yy + xx; VALUE, xx;\}; \\
a = 3; b = 5; \\
EXEC, simple(a,b);
}
yields
\madxmp{
a = 28 ;
}
\textbf{Passing arguments}\\
In the following example we use the fact that a "\$" in front of an
argument means that the truncated integer value of this argument is used
for replacement, rather than the argument string itself.
\madxmp{
tricky(xx,yy,zz): MACRO = \{mzz.yy: xx, l = 1.yy, kzz = k.yy;\}; \\
n=0; \\
WHILE \=(n < 3) \{ \\
\>n = n+1; \\
\>EXEC, tricky(quadrupole, \$n, 1); \\
\>EXEC, tricky(sextupole, \$n, 2); \\
\};
}
%% Whereas the actual use of the preceding example is NOT recommended,
%% a real life example, showing the full power (!) of macros is to be
%% found under \href{foot.html}{macro usage} for the usage, and
%% under \href{foot.html#macro}{macro definition} for the
%% definition.
Because \texttt{MACRO} statements are a \madx construct and not part of
a full language, \madx allows only one level of inclusion of another
\texttt{IF ... ELSEIF ... ELSE}, \texttt{WHILE} or \texttt{MACRO}
statements.
Macros cannot be called with number arguments but always with string
arguments. In case numerical values should be passed to a \texttt{MACRO}
in an \texttt{EXEC} statement, one can conveniently use variables names:
\madxmp{
n1=99; n2=219;\\
EXEC, thismacro(\$n1, \$n2);
}
instead of
\madxmp{
EXEC, thismacro(\$99, \$129); ! fails...
}
%% EOF