Depython
Introduction
Python is an interpreted language, but not a toy interpreter like CFWAE. We can’t possibly translate python into working CFWAE++, could we?
Python’s Abstract Syntax Tree
import ast code = """ def plusC(a, b): return a + b plusC(2, 3) """ tree = ast.parse(code) ast.dump(tree)
Evaluating this program yields
"Module(body=[FunctionDef(name='plusC', args=arguments(args=[Name(id='a', ctx=Param()), Name(id='b', ctx=Param())], vararg=None, kwarg=None, defaults=[]), body=[Return(value=BinOp(left=Name(id='a', ctx=Load()), op=Add(), right=Name(id='b', ctx=Load())))], decorator_list=[]), Expr(value=Call(func=Name(id='plusC', ctx=Load()), args=[Num(n=2), Num(n=3)], keywords=[], starargs=None, kwargs=None))])"
Let’s clean up the above, leaving out some of the details:
[FunctionDef(name='plusC', args=arguments(args=[Name(id='a'), Name(id='b')]), body=[Return(value=BinOp(left=Name(id='a'), op=Add(), right=Name(id='b')))]), Expr(value=Call(func=Name(id='plusC'), args=[Num(n=2), Num(n=3)]))]
Do now: Convert the above expression into a CFWAE++ ExprC.