MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / eval

Function eval

18-with-match/lispy/original/lis.py:110–132  ·  view source on GitHub ↗

Evaluate an expression in an environment.

(x, env=global_env)

Source from the content-addressed store, hash-verified

108################ eval
109
110def eval(x, env=global_env):
111 "Evaluate an expression in an environment."
112 if isinstance(x, Symbol): # variable reference
113 return env[x]
114 elif not isinstance(x, List): # constant literal
115 return x
116 elif x[0] == 'quote': # (quote exp)
117 (_, exp) = x
118 return exp
119 elif x[0] == 'if': # (if test conseq alt)
120 (_, test, conseq, alt) = x
121 exp = (conseq if eval(test, env) else alt)
122 return eval(exp, env)
123 elif x[0] == 'define': # (define var exp)
124 (_, var, exp) = x
125 env[var] = eval(exp, env)
126 elif x[0] == 'lambda': # (lambda (var...) body)
127 (_, parms, body) = x
128 return Procedure(parms, body, env)
129 else: # (proc arg...)
130 proc = eval(x[0], env)
131 args = [eval(exp, env) for exp in x[1:]]
132 return proc(*args)

Callers 7

testFunction · 0.70
__call__Method · 0.70
replFunction · 0.70
bottle.pyFile · 0.50
loadFunction · 0.50
executeMethod · 0.50

Calls 1

ProcedureClass · 0.70

Tested by 1

testFunction · 0.56