Is is possible to nest a counter within an ASCIIdoc macro? - macros

I use ASCIIdoc macros, and I would like to embedded counter in a substitution macro, but it doesn't work as expected.
source document:
:macro-with-counter: foo-{counter:id}
* {macro-with-counter}: bar-one
* {macro-with-counter}: bar-two
* {macro-with-counter}: bar-three
expected rendering with an incrementing counter:
foo-1: bar-one
foo-2: bar-two
foo-3: bar-three
actual rendering:
foo-1: bar-one
foo-1: bar-two
foo-1: bar-three
Is it possible to nest a counter in a substitution macro in ASCIIdoc?

Try this code:
foo-id: 0
* foo-{counter:foo-id}: bar-one
* foo-{counter:foo-id}: bar-two
* foo-{counter:foo-id}: bar-three

Simple answer: it is not possible to nest macros in ASCIIdoc. This implies a recursion capability that isn't there.

Related

Why are macros based on abstract syntax trees better than macros based on string preprocessing?

I am beginning my journey of learning Rust. I came across this line in Rust by Example:
However, unlike macros in C and other languages, Rust macros are expanded into abstract syntax trees, rather than string preprocessing, so you don't get unexpected precedence bugs.
Why is an abstract syntax tree better than string preprocessing?
If you have this in C:
#define X(A,B) A+B
int r = X(1,2) * 3;
The value of r will be 7, because the preprocessor expands it to 1+2 * 3, which is 1+(2*3).
In Rust, you would have:
macro_rules! X { ($a:expr,$b:expr) => { $a+$b } }
let r = X!(1,2) * 3;
This will evaluate to 9, because the compiler will interpret the expansion as (1+2)*3. This is because the compiler knows that the result of the macro is supposed to be a complete, self-contained expression.
That said, the C macro could also be defined like so:
#define X(A,B) ((A)+(B))
This would avoid any non-obvious evaluation problems, including the arguments themselves being reinterpreted due to context. However, when you're using a macro, you can never be sure whether or not the macro has correctly accounted for every possible way it could be used, so it's hard to tell what any given macro expansion will do.
By using AST nodes instead of text, Rust ensures this ambiguity can't happen.
A classic example using the C preprocessor is
#define MUL(a, b) a * b
// ...
int res = MUL(x + y, 5);
The use of the macro will expand to
int res = x + y * 5;
which is very far from the expected
int res = (x + y) * 5;
This happens because the C preprocessor really just does simple text-based substitutions, it's not really an integral part of the language itself. Preprocessing and parsing are two separate steps.
If the preprocessor instead parsed the macro like the rest of the compiler, which happens for languages where macros are part of the actual language syntax, this is no longer a problem as things like precedence (as mentioned) and associativity are taken into account.

How to test, if an element is in a list?

Is there a built-in function to test, if a given element is in a list in Rexx?
I could not find one. The alternative would be to loop over the list and check each element manually.
No (unless things have changed); just loop through the list.
An alternative is instead / as well have a lookup variable
i.e.
lookup. = 0 /* not all versions of Rexx support
default initialisation like this */
....
addToList:
parse arg item
numberInList = numberInList + 1
list.numberInList = item
lookup.item = 1
return
You can then check if item is in the list by
if lookup.item = 1 then do
......
It depends what you mean by a list.
At work, I use classic REXX. I frequently store lists of words in a single variable, space delimited. So WORDPOS() is the built-in function I use.
If you are using a List class in ooREXX. then why not use the hasItem method from the Collection class.

How to define convenient unit of measurement syntax in CoffeeScript?

In this example of using Groovy, the author describes how one can use Groovy tricks to define a syntax for units of measurements, such that you can write, e.g.,
3.cm + 12.m * 3 - 1.km
and have it work as expected. Is there any way to define a similarly clever syntax for associating units of measurement with numbers in CoffeeScript? (I'm very new to CoffeeScript; sorry if this is something that has already been solved or has an obvious answer.)
I think BasicWolf's answer is the most idiomatic, as you could have those functions in their own module and only import them only when you want to use them without having to pollute the global namespace or the JS builtin objects.
In Groovy, you can use a Category to avoid polluting the builtin classes with extra methods.
But, if you don't care about adding things to the builtin objects, you can go a step further and use Object.defineProperties to make the syntax of this exactly like Groovy's example :)
Object.defineProperties Number.prototype,
km: {get: -> # * 1000}
m: {get: -> #}
cm: {get: -> # * 0.01}
console.log 3.cm + 12.m * 3 - 1.km # -> -963.97
I wouldn't recommend it, but this works:
Number::cm = ->
this / 100
Number::m = ->
this
Number::km = ->
this * 1000
3.cm() + 12.m() * 3 - 1.km() # evaluates to -963.97
You can't get rid of the parentheses because 3.cm references the function cm instead of invoking it.
Unfortunately it is not possible to do it that way with CoffeeScript. What you can do is something like (in both CoffeeScript and JavaScript):
cm(3) + m(12) * 3 - km(1)
Here cm(), m(), km() are functions which convert the values to e.g. meters. In terms of CoffeeScript the following expression
(cm 3) + 3 * (m 12) - (km 1)
is also valid.

Objective c : Using #define constant values in math expressions

I'm new to iPhone development and I'm just trying out some simple drawing routines and I'm having trouble using defined values in simple math.
I have a line like this:
int offset = (((myValue - min_value) * 6) - middle);
and this works fine - but I don't like using the hard coded 6 in there (because I'll use it lots of places.
So I thought I'd define a constant using #define:
#define WIDTH_OFFSET 6;
then I could use:
int offset = (((myValue - min_value) * WIDTH_OFFSET) - middle);
however - this gets a compiler error : "Expected Expression."
I can get round this by breaking up the calculation onto several lines:
int offset = myValue - min_value;
offset = offset * WIDTH_OFFSET;
offset = offset - middle;
The compiler thinks this is fine.
I'm guessing there's some implicit cast or some other language feature at work here - can anyone explain to me what is happening?
Remove the semicolon ; after #define:
#define WIDTH_OFFSET 6
#define substitutes its arguments literally, so your expression after preprocessing becomes
(((myValue - min_value) * 6;) - middle);
As you can see, there is a semicolon in the middle of the expression, which is a syntax error.
On the other hand, your other expression
int offset = myValue - min_value;
offset = offset * WIDTH_OFFSET;
does not exhibit such problem, because having two semicolons in a row as in
offset = offset * 6;;
is syntactically valid.
When you #define something where you use it is exactly the same as if you typed it in yourself there. So where you are using WIDTH_OFFSET you are getting 6; in its place - which of course is not your intention. So just remove the semicolon.
As dasblinkenlight said, remove the semi colon. The explanation for this is that #defines are a literal substitution into your code. Thus with the semi colon your broken code read:
int offset = (((myValue - min_value) * 6;) - middle);
The working code read:
offset = offset * 6;;
Which is syntactically fine as the ;; is effectively a blank 'line' of code.
Basically, macros are convenient functions that are placed inline by the preprocessor. So you can think that they are doing copy/paste for matching entries, in your case it will substitute any occurency of WIDTH_OFFSET with 6; so, just like others said, remover the semicolon ; and you are all set.
Also, when defining macros for simple math functions, remeber to put them in brackets ( and ) otherwise, you could end up with some math operation order bugs( like unintended part multiplication before addition)

C language preprocessor behavior

There are different kind of macros in the C language, nested macro is one of them.
Considering a program with the following macro
#define HYPE(x,y) (SQUR(x)+SQUR(y))
#define SQUR(x) (x*x)
Using this we can successfully compile to get the result.
As we all know the C preprocessor replaces all the occurrence of the identifiers with the replacement-string. Considering the above example I would like to know how many times the C preprocessor traverses the program to replace the macro with the replacement values. I assume it cannot be done in one go.
the replacement takes place, when "HYPE" is actually used. it is not expanded when the #define statement occurs.
eg:
1 #define FOO 1
2
3 void foo() {
4 printf("%d\n", FOO);
5 }
so the replacement takes place in line 5, and not in line 1. hence the answer to your question is: once.
A #define'd macro invocation is expanded until there are no more terms to expand, except it doesn't recurse. For example:
#define TIMES *
#define factorial(n) ((n) == 0 ? 1 : (n) TIMES factorial((n)-1))
// Doesn't actually work, don't use.
Suppose you say factorial(2). It will expand to ((2) == 0 ? 1 : (2) * factorial((2)-1)). Note that factorial is expanded, then TIMES is also expanded, but factorial isn't expanded again afterwards, as that would be recursion.
However, note that nesting (arguably a different type of "recursion") is in fact expanded multiple times in the same expression:
#define ADD(a,b) ((a)+(b))
....
ADD(ADD(1,2),ADD(3,4)) // expands to ((((1)+(2)))+(((3)+(4))))