| Cobra | Inline Programs | variables | ||
|---|---|---|---|---|
NAMEvariables and variable assignmentsDESCRIPTIONVariable names can be introduced at any place in an inline program, and they do not need to be declared. The type of a variable (which can be one of three different types: a string, an integer value, or a token reference) is deduced from assignments to that variable, and can therefore change from one assignment to the next.Variable ScopeThere are only two levels of scope in inline programs: global and function local, depending on the first time that the variable is encountered.
AssignmentsAssignment statements are written with a single equals sign:lhs = expr;The left-hand side (lhs) can be a reference to a token (e.g., .) or a token field (e.g., .mark), a variable, or an element of an associative array. Some examples of each use are: .mark = 5; # mark is the only integer token field that can be assigned to .mark--; # post decrement and increment are defined .mark++; # as you suspected, this is a comment .txt = "Foo"; # .txt is one of two text fields that can be assigned to .typ = q.txt + .typ; # .typ is the other; + is used here for string catenation . = .nxt; # at the end of the complete token sequence, this will be a no-op # to check, one can do: if (. == .nxt), or better: if (. == End) . = .prv; # at start of the complete token sequencem this is also a no-op . = .jmp; # results in a reference to a dummy null-token if .jmp isnt set # this can be recognized by checking the .seq field # which will then be 0 q = .; q = .nxt; . = q; . = q.jmp; A[.txt] = .len; # associative array assignment, to correlate a string with a value val = .lnr; The + operator for StringsOn the right-hand side of an assignment, strings can be catenated with the plus operator (+).This is also optional for string arguments in print statements. Therefore
%{
name = .fnm + " " + .typ;
print name "\n";
Stop;
%}
has the same effect as:
%{
print .fnm " " .typ "\n";
Stop;
%}
and as
typing:
%{
print .fnm + " " + .typ + "\n";
Stop;
%}
But the shorthand, without the plus, doesn't work for assignments, so typing
%{
name = .fnm " " .typ; # syntax error
%}
will give a syntax error;
NOTESThe global keyword requires Cobra Version 3.2. |
||||
|
Inline Programs Manual Tutorial |
(Last Updated: 13 November 2020) | |||