[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: ANN: Serpent: Lua serializer and pretty printer
- From: Paul K <paulclinger@...>
- Date: Wed, 13 Jun 2012 11:12:11 -0700
Hi David,
>>> - Dumping may be used as a poor man's test of structural equality:
>>> serialize(a) == serialize(b). This can be useful in test suites.
I made several changes to get closer to serialize(a) == serialize(b),
but there is still one issue remains. I modified the sorting such that
true (boolean) and 'true' (string) are always sorted in the same order
and removed references to addresses in serialized variables.
Unfortunately, when tables are used as keys, I can only sort them by
their stringified values, and these are simply addresses, so the order
is arbitrary. This only makes difference for the serialized fragment
that includes shared/self-references, so if you don't care about that,
your serialize(a) == serialize(b) test should work (I included it in
the tests).
The new version (https://github.com/pkulchenko/serpent) also includes
Ico's patch for maxlevel.
Paul.
On Tue, Jun 12, 2012 at 11:23 PM, Paul K <paulclinger@yahoo.com> wrote:
> Hi All,
>
> I released a new Serpent version that incorporates the options we've
> been discussing earlier. It's mostly as described in my previous
> email, but I also added nohuge option (for those who are on a platform
> that generates proper strings in those cases, but want a bit faster
> implementation) and changed "long" to "block" method. As the interface
> has changed, I also updated the tests, the documentation, and the
> benchmark.
>
>>>>> (2) A "--[[table: 0x9aa7988]]" style comment is appended after each
>>>>> (3) The keys are not currently being sorted.
>>>>> (10) Dumps of bytecode are probably not useful for pretty printing
>>>>> (12) In "nil values are included when expected ({1, nil, 3} instead of
>
> All these items should be addressed: block(a, {comment=false,
> sortkeys=true, nocode=true, sparse=true}), should produce diffable
> output.
>
> Custom formatters also allow making tweaks to the output, for example,
> printing of ASTs in Metalua format. You can use the following code:
>
> print((require "serpent").block(ast, {comment = false, custom =
> function(tag,head,body,tail)
> local out = head..body..tail
> if tag:find('^lineinfo') then
> out = out:gsub("\n%s+", "") -- collapse lineinfo to one line
> elseif tag == '' then
> body = body:gsub('%s*lineinfo = [^\n]+', '')
> local _,_,atag = body:find('tag = "(%w+)"%s*$')
> if atag then
> out = "`"..atag..head.. body:gsub('%s*tag = "%w+"%s*$', '')..tail
> out = out:gsub("\n%s+", ""):gsub(",}","}")
> else out = head..body..tail end
> end
> return tag..out
> end}))
>
> to generate this output:
>
> {
> `Call{`Id{"require"},`String{"foobar"}},
> `Local{{`Id{"foo"}},{`Id{"foo"}}},
> `Local{{`Id{"y"}},{`Number{5}}},
> `Do{`Local{{`Id{"y"}},{`Number{6}}},`Local{{`Id{"y"}},{`Number{7}}}},
> `Localrec{{`Id{"test"}},{`Function{{`Id{"x"}},{`Call{`Id{"print"},`String{"123"},`Id{"x"},`Id{"y"},`Id{"z"}}}}}},
> `Call{`Id{"bar"},`Number{123}},
> `Set{{`Id{"bar"}},{`Function{{},{}}}},
> `Local{{`Id{"g"}},{`Function{{`Id{"w"}},{`Return{`Op{"mul",`Id{"w"},`Number{2}}}}}}},
> `Set{{`Id{"g"}},{`Number{1}}},
> `Call{`Id{"g"},`Number{1}},
> }
>
> The updated version is on github: https://github.com/pkulchenko/serpent.
>
> Paul.