Geoff the Medio wrote: ↑Sun Nov 01, 2020 2:01 pm
o01eg wrote: ↑Sun Nov 01, 2020 1:18 pm...walk Python AST to convert it into FOCS value refs tree...
Once one has a data structure in memory, there's no addition "parsing" required to generate FreeOrion internal ValueRefs, Effects, Conditions, Building or Tech definitions, etc. But doing this parsing and data structure conversion should not involve executing Python scripts.
What I would like to have is some kind of python preprocessing/metaprogramming available. Like generating the species colony buildings. But the of course one could preprocess outside - run a generator script which generates content files (like it is done currently for buildings) and checkin the generated content in git/bundle it with the plugin.
If the content is specified in (simplified) python syntax, one can simply import it in a python script (and use the structures or AST to generate the simplified python content).
Such a preprocessing step could also be the way to implement focs macro expansion, although i would rather like to have that as part of simplified python (assignment to constants).
Vezzra wrote: ↑Sun Nov 01, 2020 2:10 pm
Geoff the Medio wrote: ↑Sun Nov 01, 2020 2:01 pmo01eg appears to be implementing how I understood the FOCS -> Python plan to work, which is to use the Python code parser to generate a Python data structure and convert that to FreeOrion content data
I see... I never thought of such an approach. As I said in my post above to o01eg, I always thought the approach would be to expose all the necessary functions to create the condition, effects, valueref etc. objects to Python and create them by executing Python scripts.
Well that is the conversion part.
As thinksome said, real sandboxing of complete python will be easier in lua (or e.g. when using jython). But we do not need that.
YAML is only the import format. And breaking out of a python parser is probably not much easier than breaking out of a yaml parser. Also I do think yaml is not a great match for valuerefs etc.
Doing content definition in simplified python I think using AST we could whitelist a very few operations and keep it secure. But depending on how we want valuerefs and conditions to look, executing the definition might not make any sense.
While simple data structures like the gamerules are probably a good point to start dabbling with AST and conversion, it completely sidesteps the difficult parts.
So how would the conditions/valuerefs look like? Like python functions? Like python operations?
Code: Select all
# straightforward focs conditions; emulating value refs via property access in python, this is not really working python. source would be undefined, execution timing is unclear:
scope = And( Ship(), OwnedBy(source.empire) )
# scope function returns predicates:
def scope():
return And( Ship(), OwnedBy(Source().EmpireID()) )
# scope function evaluates parameters, using python operators, lots of mentioning rootCandidate which is usually implicit:
def scope(source: Planet, rootCandidate):
return rootCandidate isinstance Ship && OwnedBy(rootCandidate, source.empireID)
# builder pattern for predicate, call daisy-chaining
def scope(source: Planet, rootCandidate) {
return rootCandidate.isShip().ownedBy(source.empireId)
#
something pythonic using member functions and properties for effects, using type hinting for IDE support:
Code: Select all
def effects(source: Planet, target: Ship) {
if source.defense > target.structure:
target.destroy()
else:
target.structure -= source.defense
}
ok, thats probably already too long.. ill stop here
Any code or patches in anything posted here is released under the CC and GPL licences in use for the FO project.
Look, ma... four combat bouts!