Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
python source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
Codon 0.19.2
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
def eval_expr(expr): """Evaluate an expression and return the result.""" match expr: case BinaryOp('+', left, right): return eval_expr(left) + eval_expr(right) case BinaryOp('-', left, right): return eval_expr(left) - eval_expr(right) case BinaryOp('*', left, right): return eval_expr(left) * eval_expr(right) case BinaryOp('/', left, right): return eval_expr(left) / eval_expr(right) case UnaryOp('+', arg): return eval_expr(arg) case UnaryOp('-', arg): return -eval_expr(arg) case VarExpr(name): raise ValueError(f"Unknown value of: {name}") case float() | int(): return expr case _: raise ValueError(f"Invalid expression value: {repr(expr)}")
python source #2
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
Codon 0.19.2
PyPy 3.10
PyPy 3.11
PyPy 3.9
Python 3.10
Python 3.11
Python 3.12
Python 3.13
Python 3.5
Python 3.6
Python 3.7
Python 3.8
Python 3.9
Pythran 0.15
Options
Source code
def eval_expr(expr): """Evaluate an expression and return the result.""" if (isinstance(expr, BinaryOp) and expr.op == "+"): return eval_expr(expr.left) + eval_expr(expr.right) elif (isinstance(expr, BinaryOp) and expr.op == "-"): return eval_expr(expr.left) - eval_expr(expr.right) elif (isinstance(expr, BinaryOp) and expr.op == "*"): return eval_expr(expr.left) * eval_expr(expr.right) elif (isinstance(expr, BinaryOp) and expr.op == "/"): return eval_expr(expr.left) / eval_expr(expr.right) elif (isinstance(expr, UnaryOp) and expr.op == "+"): return eval_expr(expr.arg) elif (isinstance(expr, UnaryOp) and expr.op == "-"): return -eval_expr(expr.arg) elif isinstance(expr, VarExpr): raise ValueError(f"Unknown value of: {expr.name}") elif isinstance(expr, float) or isinstance(expr, int): return expr else: raise ValueError(f"Invalid expression value: {repr(expr)}")
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree