POV-Ray : Newsgroups : povray.general : Serializing a dictionary Server Time
12 Jul 2025 19:19:43 EDT (-0400)
  Serializing a dictionary (Message 1 to 10 of 17)  
Goto Latest 10 Messages Next 7 Messages >>>
From: Josh English
Subject: Serializing a dictionary
Date: 5 Jul 2025 21:42:26
Message: <6869d482$1@news.povray.org>
I have a dictionary in 3.8 and one of the elements is an array.
I'm trying to write the definitions out to a text file:

#macro WriteTileSet(fname)
   #fopen TILE fname write
   #for(I, 0, MaxEntropy-1, 1)
     #write (TILE, "#declare TileSet[", S(I), "] = dictionary {\n")
     #write (TILE, "  .name = ", TileSet[I].name, ";\n")
     #write (TILE, "  .edges = array[", S(TileEdges),"] {"
     #for(E, 0, TileEdges-1, 1)
       #write (TILE, TileSet[I].edges[E], ", ")
     #end
     #write (TILE, "};\n")
     #write (TILE, "  .pidx = ", S(TileSet[I].pidx),";\n")
     #write (TILE, "  .rot = ", S(TileSet[I].rot), ";\n")
     #write (TILE, "}\n"
   #end // for I

   #fclose TILE
#end

And I'm getting an error:

Parse Error: Can't nest directives accessing the same file.

Is there a way around this? I don't want to have to manually write each 
element out.

I think it requires another macro to convert the .edges element to a
string that I can then slap into the #write directive. Is this the only 
way or am I missing something?

Uncle Josh

Josh


Post a reply to this message

From: jr
Subject: Re: Serializing a dictionary
Date: 6 Jul 2025 00:10:00
Message: <web.6869f5bde086e5d13ecaab5e6cde94f1@news.povray.org>
hi,

Josh English <Jos### [at] joshuarenglishcom> wrote:
> I have a dictionary in 3.8 and one of the elements is an array.
> ...
> And I'm getting an error:
> Parse Error: Can't nest directives accessing the same file.
> Is there a way around this? I don't want to have to manually write each
> element out.
> I think it requires another macro to convert the .edges element to a
> string that I can then slap into the #write directive. Is this the only
> way or am I missing something?

I've been looking at (un)serialising dictionaries using my 'Filed()' macro[*],
and have not yet found a "good way" for dealing with arrays, especially
multi-dimensional.  I think converting the array to a string, and storing that,
will work, eg "3.1415:1.23:0.1" for an array of three floats; have a look at my
'strSplit()' macro[**] for the unserialising.

[*] <wiki.povray.org/content/User:Jr>
[**] <news.povray.org/web.64b08576d08f3556b49d80446cde94f1%40news.povray.org>


regards, jr.


Post a reply to this message

From: William F Pokorny
Subject: Re: Serializing a dictionary
Date: 6 Jul 2025 01:47:39
Message: <686a0dfb$1@news.povray.org>
On 7/5/25 21:42, Josh English wrote:
> And I'm getting an error:
> 
> Parse Error: Can't nest directives accessing the same file.

You are missing a couple of closing parens.

#macro WriteTileSet(fname)
   #fopen TILE fname write
   #for(I, 0, MaxEntropy-1, 1)
     #write (TILE, "#declare TileSet[", S(I), "] = dictionary {\n")
     #write (TILE, "  .name = ", TileSet[I].name, ";\n")
     #write (TILE, "  .edges = array[", S(TileEdges),"] {"    // <--
     #for(E, 0, TileEdges-1, 1)
       #write (TILE, TileSet[I].edges[E], ", ")
     #end
     #write (TILE, "};\n")
     #write (TILE, "  .pidx = ", S(TileSet[I].pidx),";\n")
     #write (TILE, "  .rot = ", S(TileSet[I].rot), ";\n")
     #write (TILE, "}\n"                                      // <---
   #end // for I

   #fclose TILE
#end

The parser is seeing another #write directive start, while being in the 
middle of parsing a prior #write directive. As long as the .edges 
element is a string, you should be on your way after adding the closing 
parens.

Bill P.


Post a reply to this message

From: Josh English
Subject: Re: Serializing a dictionary
Date: 6 Jul 2025 15:48:47
Message: <686ad31f$1@news.povray.org>
On 7/5/2025 10:47 PM, William F Pokorny wrote:
> On 7/5/25 21:42, Josh English wrote:
>> And I'm getting an error:
>>
>> Parse Error: Can't nest directives accessing the same file.
> 
> You are missing a couple of closing parens.
>
> 
> Bill P.

AAARGH! Thank you. Yeah, that was the problem (and the missing quotation 
marks in the resulting text file).

I wish the Windows UI had a better way to tell me where the errors 
actually are. I make mistakes like this and it fails in some other part 
of the code.

I want to build a linter for POV-Ray some day, maybe that will help.

Thank you again.

Josh


Post a reply to this message

From: Josh English
Subject: Re: Serializing a dictionary
Date: 6 Jul 2025 15:51:00
Message: <686ad3a4$1@news.povray.org>
On 7/5/2025 9:05 PM, jr wrote:
> hi,
> 
> Josh English <Jos### [at] joshuarenglishcom> wrote:
>> I have a dictionary in 3.8 and one of the elements is an array.
>> ...
>> And I'm getting an error:
>> Parse Error: Can't nest directives accessing the same file.
>> Is there a way around this? I don't want to have to manually write each
>> element out.
>> I think it requires another macro to convert the .edges element to a
>> string that I can then slap into the #write directive. Is this the only
>> way or am I missing something?
> 
> I've been looking at (un)serialising dictionaries using my 'Filed()' macro[*],
> and have not yet found a "good way" for dealing with arrays, especially
> multi-dimensional.  I think converting the array to a string, and storing that,
> will work, eg "3.1415:1.23:0.1" for an array of three floats; have a look at my
> 'strSplit()' macro[**] for the unserialising.
> 
> [*] <wiki.povray.org/content/User:Jr>
> [**] <news.povray.org/web.64b08576d08f3556b49d80446cde94f1%40news.povray.org>
> 
> 
> regards, jr.
> 

I'll look into it. I've found it's generally easier just to build an 
include file that can be imported later than trying to manage 
customizing serialization.

Now if POV-Ray 4.0 could read and write JSON or XML, I'd be in heaven, 
but I don't think that's in the plans.

Uncle Josh


Post a reply to this message

From: Bald Eagle
Subject: Re: Serializing a dictionary
Date: 7 Jul 2025 10:30:00
Message: <web.686bd980e086e5d1f625f77c25979125@news.povray.org>
Josh English <Jos### [at] joshuarenglishcom> wrote:

> AAARGH! Thank you. Yeah, that was the problem (and the missing quotation
> marks in the resulting text file).
>
> I wish the Windows UI had a better way to tell me where the errors
> actually are. I make mistakes like this and it fails in some other part
> of the code.
>
> I want to build a linter for POV-Ray some day, maybe that will help.

So, this is a common problem, with start and end bracketing "directives" or
whatever we call them

#if #end
#while #end
texture {     }
function {     }
etc

I use large (5-space) TABS to maximize the visual placing of the start and end
of my code blocks.

I have also (mostly) trained myself to type the opening AND closing parts first,
and then fill in the space between.

On top of that, if I'm not being lazy, I specifically comment my closing
directive to match the opening one.  I will number or name nested code blocks.
"Outer", "Middle", "Inner"
"#if 1", "#if 2", "#if 3"

I wrote a bunch of macros to help keep track of where in the hierarchy I am when
my code craps out.

Maybe someone can suggest a command-line script to grep opening and closing
parentheses and give a count.

Might be able to paste it into a spreadsheet and get a count that way.

- BE


Post a reply to this message

From: jr
Subject: Re: Serializing a dictionary
Date: 7 Jul 2025 11:45:00
Message: <web.686bea5ce086e5d13ecaab5e6cde94f1@news.povray.org>
hi,

"Bald Eagle" <cre### [at] netscapenet> wrote:
> ...
> I have also (mostly) trained myself to type the opening AND closing parts first,
> and then fill in the space between.

very good practice/advice.  fwiw, a suitable editor helps, for instance using
'vim' makes jumping between opening and closing brace/parenthesis/bracket a
single (shifted) key press.


> ...
> Maybe someone can suggest a command-line script to grep opening and closing
> parentheses and give a count.

not totally what you're after, you only get the line count of occurrence, but,
in practice, the opening/closing counts should be the same.  (no time to write a
script, sorry :-))

  $ grep -ce '[{]' myscene.pov
  $ grep -ce '[}]' myscene.pov


> Might be able to paste it into a spreadsheet and get a count that way.

do you use 'sc(1)' ?


regards, jr.


Post a reply to this message

From: jr
Subject: Re: Serializing a dictionary
Date: 7 Jul 2025 12:45:00
Message: <web.686bf940e086e5d13ecaab5e6cde94f1@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:
> "Bald Eagle" <cre### [at] netscapenet> wrote:
> > ...
> > Maybe someone can suggest a command-line script to grep opening and closing
> > parentheses and give a count.
>
> not totally what you're after, ...(no time to write a script, sorry :-))

no script, but a (simple) re-purposed program (named "brace parenthesis bracket
counts"), attached.  compile with:

  $ gcc -std=c99 -O2 -o bpbcounts bpbcounts.c

(sorry, no spreadsheet importer :-))


enjoy, jr.


Post a reply to this message


Attachments:
Download 'bpbcounts.c.txt' (2 KB)

From: kurtz le pirate
Subject: Re: Serializing a dictionary
Date: 8 Jul 2025 04:08:56
Message: <686cd218$1@news.povray.org>
On 07/07/2025 18:43, jr wrote:
> "jr" <cre### [at] gmailcom> wrote:
>> "Bald Eagle" <cre### [at] netscapenet> wrote:
>>> ...
>>> Maybe someone can suggest a command-line script to grep opening and closing
>>> parentheses and give a count.
>>
>> not totally what you're after, ...(no time to write a script, sorry :-))
> 
> no script, but a (simple) re-purposed program (named "brace parenthesis bracket
> counts"), attached.  compile with:
> 
>    $ gcc -std=c99 -O2 -o bpbcounts bpbcounts.c
> 
> (sorry, no spreadsheet importer :-))
> 
> 
> enjoy, jr.


A long time ago, I started doing the same thing (but in Perl). I stopped 
because I got stuck on a problem: even if the number of pairs is 
correct, the syntax isn't necessarily right. You could be missing a 
brace in one place and have one too many in another. All in all, the 
count is good, but the syntax is not.


But indeed, this kind of script already gives good indications.



-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message


Attachments:
Download 'analyser.pl.zip' (3 KB)

From: Josh English
Subject: Re: Serializing a dictionary
Date: 9 Jul 2025 00:41:01
Message: <686df2dd$1@news.povray.org>
On 7/7/2025 7:28 AM, Bald Eagle wrote:
> Josh English <Jos### [at] joshuarenglishcom> wrote:
> 
> 
> So, this is a common problem, with start and end bracketing "directives" or
> whatever we call them
> 
> #if #end
> #while #end
> texture {     }
> function {     }
> etc
> 
> I use large (5-space) TABS to maximize the visual placing of the start and end
> of my code blocks.

Yeah, I should get into that practice. I don't do that in any other 
language, though, so I suspect an uphill battle.
> 

> 
> Maybe someone can suggest a command-line script to grep opening and closing
> parentheses and give a count.

I might be able to scribble out some Python to do that, come to think of it.


Uncle Josh


Post a reply to this message

Goto Latest 10 Messages Next 7 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.