Tables without commas

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Tables without commas

Pedro Miller Rabinovitch-3
Hello all,

just felt like sharing something with the group. For some reason, I wanted to specify tables like this:

	myThing = {
		name = "Thing"
		use = "none"
		owner = idPersona
	}

No commas, just like {X,HT}ML attribute lists or anything like that.

I quickly patched lparser.c (see below), and it seems to be working pretty nicely, at least for the cases I tried (I'm not in possession of any unit tests over here). The only thing people should look out for is stuff like:

	a = { strlen "aaa" }

...which will match the expression and set a[1] to be 4, and *not* to function `strlen'. The next does, though:

	a = { (strlen) "aaa" }

Anyway. It's a toy for whoever wants it. It just ain't Lua no more. ;)

Please do warn me if you see any blatant problems with this that I did not realize. Thanks.

	Pedro.

511,512c511,513
<   while (ls->t.token == ',') {
<     next(ls);
---
   for (;;) {
     if (ls->t.token == ',')
       next(ls);
530,531c531,533
<   while (ls->t.token == ',') {
<     next(ls);
---
   for (;;) {
     if (ls->t.token == ',')
       next(ls);
--
Pedro Miller Rabinovitch
Gerente Geral de Tecnologia
Cipher Technology
www.cipher.com.br

Reply | Threaded
Open this post in threaded view
|

Re: Tables without commas

Edgar Toernig
> For some reason, I wanted to specify tables like this:
> 
>         myThing = {
>                 name = "Thing"
>                 use = "none"
>                 owner = idPersona
>         }
> 
> No commas, just like {X,HT}ML attribute lists or anything like that.

This is how it is in Sol (a derivation of Lua), the ',' in the record
field constructor is optional.  The only place where it _is_ required
is before a [key]=val field ({ a=b , [foo]=2 }).  Without the ','
you'd get a=b[foo]=2 and that will raise a syntax error.
 
> The only thing people should look out for is stuff like:
> 
>         a = { strlen "aaa" }
> 
> ...which will match the expression and set a[1] to be 4, and *not* to
> function `strlen'. The next does, though:
> 
>         a = { (strlen) "aaa" }

You get much more problems.  Sol requires the ',' in list field parts.
I.e. what's the result of { 1 -2 } or { a (b+c)*d }?  (Btw, in Sol your
two examples above would create exactly the same code - it calls strlen
for "aaa" and would give a[1]=3 (not 4!) ;-)  IMO the ',' is necessary
in the list fields.

Ciao, ET.



Reply | Threaded
Open this post in threaded view
|

Re: Tables without commas

Pedro Miller Rabinovitch-3
This is how it is in Sol (a derivation of Lua), the ',' in the record
field constructor is optional.  The only place where it _is_ required
is before a [key]=val field ({ a=b , [foo]=2 }).  Without the ','
you'd get a=b[foo]=2 and that will raise a syntax error.

Sure thing.

 > The only thing people should look out for is stuff like:
 >         a = { strlen "aaa" }
You get much more problems.  Sol requires the ',' in list field parts.

Correct. It's a nice approach, I guess -- originally I really only intended the optional comma behaviour for recfields.

I.e. what's the result of { 1 -2 } or { a (b+c)*d }?  (Btw, in Sol your
two examples above would create exactly the same code - it calls strlen
for "aaa" and would give a[1]=3 (not 4!) ;-)  IMO the ',' is necessary
in the list fields.

strlen"aaa" == 4 ?? Doh. My old example had 4 'a's. Please ignore. :-)

I suppose the recfield part is pretty useful. I'll probably keep the patch around for my not-Lua language interpreter.

Thanks for the tips -- that's exactly the kind of help/critique I was expecting when I posted.

	Pedro.
--
Pedro Miller Rabinovitch
Gerente Geral de Tecnologia
Cipher Technology
www.cipher.com.br