Secure Embed Custom Expression Implementation Strategy

I am designing / prototyping a domain specific language ... in Python, at the moment, at least. The design is simple - but it needs support to indicate an arbitrary function (whose domain is a map from labels to integers - the range is an integer). In many cases, a function will simply select a label in the domain to get the result ... but I want to allow the specification of any function that can be easily (and efficiently) implemented in a general-purpose programming language.

The caveat is that I want the function to be "safe" ... by this I mean:

  • Pure function: deterministic without side effects. (i.e. there is no external state, there is no interaction with files, input-output, devices, etc.).
  • Termination - either successfully, or after certain (small-scale) distributed computing resources have expired.

I am interested in this feature being effectively implemented - I expect definitions to be provided infrequently - and are often evaluated. I would also like for functions to be defined using familiar syntax.

I reviewed support for implementing functions in python ... I know that I could impose restrictions using the eval () function , and I found the AST module - suggesting an approach that includes parsing in AST, then interpreting (or checking, before evaluating), AST tree. I also read about pyparse and changed the implementation to order, an interpreted language.

I cannot help but think that an attempt to block unwanted behavior from eval () is to solve the backward problem (an attempt to block unwanted ex-post functionality), while introducing a custom language involves reinventing the wheel.

Does Python already have a safe, efficient, embedded expression interpreter?

+5
source share

Source: https://habr.com/ru/post/1260007/


All Articles