0
\$\begingroup\$

What all languages have a construct similar to python's eval, so you can write code during runtime and run it?

A list would be helpful for certain programming challenges - ones where it is obvious an eval is either necessary or highly efficient.

\$\endgroup\$
2
  • \$\begingroup\$ I'm not sure on the answer format, would a community wiki answer be better? Or one language per answer? \$\endgroup\$ May 19 '20 at 11:47
  • \$\begingroup\$ You can cross C++ off the list! T_T \$\endgroup\$
    – Noodle9
    May 19 '20 at 13:08
6
\$\begingroup\$

A few I recall:

  • Lua: loadstring("2 + 2")
  • *Script (Coffee, Java, Type, Action): eval("2 + 2")
  • Erlang / Elixir: Code.eval_string("a + b", [a: 2, b: 2], file: __ENV__.file, line: __ENV__.line)
  • J: ". '2 + 2' (eval it right away) or 3 : '2 + 2' (define a function; (3 : '2 + 2') 0 runs it)
  • Ruby: eval "2 + 2"
  • Perl: eval "2 + 2"
  • Bash: a="ls | wc -l", eval $a
  • PHP: eval("2 + 2")
  • Lisp, obviously: (setq test'(+ 2 2)) and (eval test)
  • Python: eval('2 + 2')
  • ColdFusion: <cfset x = "int(2+2)"> and <cfset y = Evaluate(x)>
  • FORTH: S" 2 2 + ." eval (note: it depends on the implementation; most use either EVALUATE or eval)
  • VBScript, VBA: Execute('2 + 2') (note: for VBA as far as I recall, it's eval)
  • Smalltalk: Compiler evaluate:'2 + 2'
  • APL: The primitive executes a character vector that contains a valid APL expression: ⍎ '2×3' or ⍎ 'S←A String'. The former will execute the mathematical expression and return 6; the latter will create a variable S in the APL workspace, and assign it the value A String
  • PowerShell: invoke-expression '2 + 2'
  • R: eval(parse(text = "2 + 2"))
  • Io: doString("2 + 2")
  • Burlesque: "2 2.+"pe

Other languages:

  • Java: see ScriptEngine functionality.
  • C, C++: they can execute shellcode
  • D: via mixins, int a = 0; and then mixin("a = 2 + 2;"); (note: the string must be constant and known at the compile time)
\$\endgroup\$
17
  • \$\begingroup\$ APL, using the primitive - if S is a character vector containing a valid APL expression, ⍎S will execute it. \$\endgroup\$ May 19 '20 at 13:29
  • \$\begingroup\$ R: eval(parse(text = "2 + 2")) There is a reason it's not great for golfing \$\endgroup\$ May 19 '20 at 13:46
  • 2
    \$\begingroup\$ @AshleyBaldry well, it's not too bad; that can be golfed to eval(parse(t="2 + 2")) \$\endgroup\$
    – Giuseppe
    May 19 '20 at 14:05
  • \$\begingroup\$ "2 2+".V (regular eval) or "2+2".E (python-eval) could be added for 05AB1E and “2 2+”V or “2+2”V for Jelly (V uses either regular or Python eval automatically). Maybe this would be better suited as a cw answer so everyone could add to it (and you could maybe also add TIOs to verify each eval). But +1 for listing all of these yourself already. :) \$\endgroup\$ May 19 '20 at 14:39
  • 1
    \$\begingroup\$ Bytecode could technically work too. You can dynamically load more bytecode while running. \$\endgroup\$
    – Kroppeb
    May 19 '20 at 14:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.