Varrefs in ranges
jllang opened this issue · comments
According to the Spin online reference (https://spinroot.com/spin/Man/grammar.html), the variable of a range in a for
-loop or a select
statement belongs to a syntactic category known as varref
. varref
on the other hand subsumes variable names as well as references to indices in arrays and fields in records. However, Spin doesn't seem to be able to parse for
-loops or select
statements for which the varref
instance is a reference to an index in an array or a field in a record. The following code demonstrates my point:
int a [1];
typedef foo
{
int bar;
}
foo x;
init
{
for (a[0] : 0 .. 4) /* bad index in for-construct a */
{
printf("%d\n", a[0])
}
for (x.bar : 0 .. 4) /* bad index in for-construct x */
{
printf("%d\n", x.bar)
}
select (a[0] : 0 .. 4) /* bad index in for-construct a */
{
printf("%d\n", a[0])
}
select (x.bar : 0 .. 4) /* bad index in for-construct x */
{
printf("%d\n", x.bar)
}
}
I wrote the syntax errors given by Spin in the comments to the appropriate lines. Notice how the errors for select
mention the for-construct. To see the errors for each individual case, comment out the other for
and select
statements.
Could this be a mistake in the specification? Maybe a for
and select
range over a name
instead of a varref
?
P.S. A few days ago, I managed to cause an infinite loop in Spin by referring to an array as the for
-loop variable. Unfortunately, I didn't save that code so I can't tell how to reproduce this infinite loop.
good error reports on all of these
i'll be working through them and see what I can fix
The grammar definition is incorrect (although it would be nice if the language could be extended to make it right the way it was defined). I've corrected the grammar page, as well as the man pages for 'for' and 'select.' These pages had not been updated in 8 years, so I guess it was time!
Thanks for reporting this. If you can recreate that infinite loop, I'd love to find out what can trigger it.
It seems that there's still
range : varref ':' expr '..' expr
| varref IN varref
By the way, could the expr
instances in the first production be restricted to anyexpr
?
I thought I fixed that -- ok, uploaded the corrected page again.
Now I also see name
s instead of varref
s in the grammar page.
By the way, could the
expr
instances in the first production be restricted toanyexpr
?
good point -- I just checked in the code (spin.y) and it does look like it enforces that restriction, so I've updated the grammar.html page. Of course, there's still some bizarre stuff that one could do in a range expression, but this helps a little.