Save/load compiled patterns
The RPL compiler is incremental. To compile a pattern, the compiler needs the AST for the pattern and an environment that maps pattern names to compiled pattern objects.
Without describing here how the pattern object will change when RPL modules are introduced, the following will remain true:
- Most of the slots in the pattern object record can be saved and loaded as JSON
- Two slots (peg and uncap) are Lua userdata holding lpeg patterns and we need a way to save these
- The lpeg patterns used by Rosie should be leveraging only one Lua function:
common.create_match
; therefore, when saving the pattern object's lpeg userdata, we do not need to be able to save any Lua function; when we encountercommon.create_match
, we can save a marker of some kind, and then when reading from disk, we replace the marker with the function
When the module system is finished, it will be easier to test the saving/loading of compiled patterns. But while developing a save/load technique, something like the following can be used to verify that it is working correctly, showing that matches work before and after the save/load:
> e = lapi.new_engine()
> lapi.load_manifest(e, "$sys/MANIFEST")
true table: 0x7fe4c634aa90 /Users/jjennings/Work/Dev/private/jones/rosie-pattern-language/MANIFEST
> for k,v in pairs(e.env["common.number"]) do print(k,v); end
original_ast table: 0x7fe4c63d2330
ast table: 0x7fe4c3d90480
name choice
peg userdata: 0x7fe4c4071e28
uncap userdata: 0x7fe4c4000028
alias false
raw false
>
>
> -- save(e.env["common.number"], "filename")
> -- e.env["common.number"] = nil
> -- load(e.env["common.number"], "filename")
>
> -- test that pattern here, and if working, test patterns that depend on common.number