这是我让 chatgpt4 根据这个 idea 写的代码:
```
import ast
import json
data = json.load(xxx) # some sample data source
class TransformVarToDict(ast.NodeTransformer):
def visit_Name(self, node):
# Replace variable reference with dictionary access
if isinstance(node.ctx, (ast.Load, ast.Store)):
return ast.Subscript(
value=
ast.Name(id='data', ctx=ast.Load()),
slice=ast.Index(value=ast.Str(s=
node.id)),
ctx=node.ctx
)
return node
def process_formula(formula_str):
# Parse the formula to an AST
parsed = ast.parse(formula_str)
# Transform the AST
transformed = TransformVarToDict().visit(parsed)
ast.fix_missing_locations(transformed) # Fix line numbers
# Compile and execute the modified AST
code = compile(transformed, '<string>', 'exec')
exec(code, globals())
# Sample formulas
formulas = [
"A = B + C",
"D = E - F",
"G = H * I",
"J = K / L"
]
for formula in formulas:
process_formula(formula)
print(data)
```