Skip to content
Snippets Groups Projects

Data structures builtins

Merged Ivan Kondov requested to merge data-structures-builtins into master
14 files
+ 1131
58
Compare changes
  • Side-by-side
  • Inline
Files
14
+ 32
0
"""functions related to variable substitutions in expressions"""
from copy import deepcopy
from textx import get_children_of_type, get_children
from textx import get_metamodel
from textx import textx_isinstance
def subst(caller, func, params):
"""
substitute dummy identifiers (bound variables) with their parameters
caller: the object of which the new returned expression will become a child
func: object of type FunctionDefinition or Lambda that contain expr
params: an iterable with the parameters in the order of substitution
"""
expr = deepcopy(func.expr)
new_objs = get_children(lambda x: True, expr)
for new_obj in new_objs:
if new_obj is not expr:
new_obj.parent = new_obj.parent.last_copy
expr.parent = caller
meta = get_metamodel(caller)
vrefs = get_children_of_type('VarReference', expr)
for vref in filter(lambda x: textx_isinstance(x.ref, meta['Dummy']), vrefs):
if hasattr(vref.ref, 'name'):
for par, arg in zip(params, func.args):
if vref.ref.name == arg.name:
if textx_isinstance(par, meta['VarReference']):
vref.ref = par.ref
else:
vref.ref = par
break
return expr
Loading