Skip to content

Latest commit

 

History

History
2440 lines (1794 loc) · 36.8 KB

SUPPORTED_FORMS.md

File metadata and controls

2440 lines (1794 loc) · 36.8 KB

Supported S-Expression Forms

This documentation is auto-generated from the test suite. Do not edit manually.

control-flow Forms

(try (setf a 1) ((as Exception e) (setf a 2)))

Tests as-form emission in except clauses.

Lisp S-Expression:

(try (setf a 1) ((as Exception e) (setf a 2)))

Generated Python (after formatting):

try:
    a = 1
except Exception as e:
    a = 2

`(if (== a b)

     (return 1)
     (return 2))`

Tests if/else emission.

Lisp S-Expression:

(if (== a b)
    (return 1)
    (return 2))

Generated Python (after formatting):

if a == b:
    return 1
else:
    return 2

(when (> a b) (return a))

Tests when emission.

Lisp S-Expression:

(when (> a b) (return a))

Generated Python (after formatting):

if a > b:
    return a

(unless (> a b) (return b))

Tests unless emission.

Lisp S-Expression:

(unless (> a b) (return b))

Generated Python (after formatting):

if not a > b:
    return b

(while (< a b) (setf a (+ a 1)))

Tests while loop emission.

Lisp S-Expression:

(while (< a b) (setf a (+ a 1)))

Generated Python (after formatting):

while a < b:
    a = a + 1

(for (i (range 3)) (print i))

Tests for loop emission.

Lisp S-Expression:

(for (i (range 3)) (print i))

Generated Python (after formatting):

for i in range(3):
    print(i)

(for-generator (i (range 3)) (* i 2))

Tests for-generator emission.

Lisp S-Expression:

(for-generator (i (range 3)) (* i 2))

Generated Python (after formatting):

i*2 for i in range(3)

(with (open (string "f.txt")) (setf data (dot f read)))

Tests with-statement emission.

Lisp S-Expression:

(with (open (string "f.txt")) (setf data (dot f read)))

Generated Python (after formatting):

with open("f.txt"):
    data = f.read

(with (as (open (string "f.txt")) f) (setf data (dot f read)))

Tests with-statement emission using 'as'.

Lisp S-Expression:

(with (as (open (string "f.txt")) f) (setf data (dot f read)))

Generated Python (after formatting):

with open("f.txt") as f:
    data = f.read

`(try (setf a 1) (Exception (setf a 2)) (else (setf a 3))

  (finally (setf a 4)))`

Tests try/except/else/finally emission.

Lisp S-Expression:

(try (setf a 1) (Exception (setf a 2)) (else (setf a 3)) (finally (setf a 4)))

Generated Python (after formatting):

try:
    a = 1
except Exception:
    a = 2
else:
    a = 3
finally:
    a = 4

(cond ((> a b) (return a)) ((< a b) (return b)) (t (return 0)))

Tests cond emission.

Lisp S-Expression:

(cond ((> a b) (return a)) ((< a b) (return b)) (t (return 0)))

Generated Python (after formatting):

if a > b:
    return a
elif a < b:
    return b
else:
    return 0

(? (> a b) a b)

Tests ternary emission.

Lisp S-Expression:

(? (> a b) a b)

Generated Python (after formatting):

a if a > b else b

(def foo nil (return_ (x)))

Tests return_ emission inside a function.

Lisp S-Expression:

(def foo nil (return_ (x)))

Generated Python (after formatting):

def foo():
    return x

(try (setf a 1) ("Exception as e" (print e)))

Tests try/except block using string directly for except clause.

Lisp S-Expression:

(try (setf a 1) ("Exception as e" (print e)))

Generated Python (after formatting):

try:
    a = 1
except Exception as e:
    print(e)

`(do0

  (for (i (range 5))
   (if (== i 2)
       break
       continue)))`

Tests loop control statements break and continue.

Lisp S-Expression:

(do0
 (for (i (range 5))
  (if (== i 2)
      break
      continue)))

Generated Python (after formatting):

for i in range(5):
    if i == 2:
        break
    else:
        continue

(do0 yield (yield x))

Tests yield statement and yield function call variants.

Lisp S-Expression:

(do0 yield (yield x))

Generated Python (after formatting):

yield
yield (x)

`(with

  (ntuple (as (open (string "a.txt")) f) (as (open (string "b.txt")) g))
  (setf a 1))`

Tests with-statement with multiple context managers using ntuple.

Lisp S-Expression:

(with (ntuple (as (open (string "a.txt")) f) (as (open (string "b.txt")) g))
 (setf a 1))

Generated Python (after formatting):

with open("a.txt") as f, open("b.txt") as g:
    a = 1

(with conn (setf a 1))

Tests with-statement with a simple variable context manager.

Lisp S-Expression:

(with conn (setf a 1))

Generated Python (after formatting):

with conn:
    a = 1

(for (x items) (print x))

Tests looping over a sequence variable (without explicit range/function call).

Lisp S-Expression:

(for (x items) (print x))

Generated Python (after formatting):

for x in items:
    print(x)

(setf x (? c1 a (? c2 b d)))

Tests nested ternary operations with precedence-aware parenthesis formatting.

Lisp S-Expression:

(setf x (? c1 a (? c2 b d)))

Generated Python (after formatting):

x = a if c1 else b if c2 else d

(for ((ntuple root folders files) (os.walk (string "."))) (print root))

Tests unpacking multiple loop variables directly inside a for loop.

Lisp S-Expression:

(for ((ntuple root folders files) (os.walk (string "."))) (print root))

Generated Python (after formatting):

for root, folders, files in os.walk("."):
    print(root)

(try (f) ((tuple KeyError ValueError) (print (string "error"))))

Tests try-except clause with a tuple of exceptions.

Lisp S-Expression:

(try (f) ((tuple KeyError ValueError) (print (string "error"))))

Generated Python (after formatting):

try:
    f()
except (
    KeyError,
    ValueError,
):
    print("error")

core Forms

(def foo (x) (return x))

Tests a simple function definition with one argument and a return statement.

Lisp S-Expression:

(def foo (x) (return x))

Generated Python (after formatting):

def foo(x):
    return x

`(setf a 1

       b 2)`

Tests 'setf' for assigning multiple variables in sequence.

Lisp S-Expression:

(setf a 1
      b 2)

Generated Python (after formatting):

a = 1
b = 2

(= a 1)

Tests direct assignment emission.

Lisp S-Expression:

(= a 1)

Generated Python (after formatting):

a = 1

(list 1 2)

Tests list literal emission.

Lisp S-Expression:

(list 1 2)

Generated Python (after formatting):

[1, 2]

(tuple 1 2 3)

Tests tuple literal emission.

Lisp S-Expression:

(tuple 1 2 3)

Generated Python (after formatting):

(
    1,
    2,
    3,
)

(paren a b)

Tests paren emission for comma-separated values.

Lisp S-Expression:

(paren a b)

Generated Python (after formatting):

(a, b)

(ntuple a b c)

Tests ntuple emission without surrounding parentheses.

Lisp S-Expression:

(ntuple a b c)

Generated Python (after formatting):

a, b, c

(curly 1 2 3)

Tests curly emission for set literals.

Lisp S-Expression:

(curly 1 2 3)

Generated Python (after formatting):

{1, 2, 3}

(dict ((string "a") 1) ((string "b") 2))

Tests dict literal emission with explicit key/value pairs.

Lisp S-Expression:

(dict ((string "a") 1) ((string "b") 2))

Generated Python (after formatting):

{("a"): (1), ("b"): (2)}

(dictionary :a 1 :b 2)

Tests keyword-based dictionary constructor emission.

Lisp S-Expression:

(dictionary :a 1 :b 2)

Generated Python (after formatting):

dict(a=1, b=2)

(aref arr 1)

Tests array reference emission.

Lisp S-Expression:

(aref arr 1)

Generated Python (after formatting):

arr[1]

(aref arr (slice 1 2))

Tests slice emission inside indexing.

Lisp S-Expression:

(aref arr (slice 1 2))

Generated Python (after formatting):

arr[1:2]

(aref arr (slice 1 5 2))

Tests slice emission with a step.

Lisp S-Expression:

(aref arr (slice 1 5 2))

Generated Python (after formatting):

arr[1:5:2]

(aref arr (slice nil 3))

Tests slice emission with an open start.

Lisp S-Expression:

(aref arr (slice nil 3))

Generated Python (after formatting):

arr[:3]

(aref arr (slice 1 nil))

Tests slice emission with an open end.

Lisp S-Expression:

(aref arr (slice 1 nil))

Generated Python (after formatting):

arr[1:]

(aref arr i j)

Tests multi-index emission.

Lisp S-Expression:

(aref arr i j)

Generated Python (after formatting):

arr[i, j]

(dot obj attr)

Tests dot form emission.

Lisp S-Expression:

(dot obj attr)

Generated Python (after formatting):

obj.attr

(foo 1 :bar 2 :baz 3)

Tests keyword argument emission in function calls.

Lisp S-Expression:

(foo 1 :bar 2 :baz 3)

Generated Python (after formatting):

foo(1, bar=2, baz=3)

(foo :bar 2)

Tests keyword-only call emission.

Lisp S-Expression:

(foo :bar 2)

Generated Python (after formatting):

foo(bar=2)

(string "hi")

Tests string literal emission.

Lisp S-Expression:

(string "hi")

Generated Python (after formatting):

"hi"

(string-b "data")

Tests byte string literal emission.

Lisp S-Expression:

(string-b "data")

Generated Python (after formatting):

b"data"

(string3 "block")

Tests triple-quoted string literal emission.

Lisp S-Expression:

(string3 "block")

Generated Python (after formatting):

"""block"""

(rstring3 "raw")

Tests raw triple-quoted string literal emission.

Lisp S-Expression:

(rstring3 "raw")

Generated Python (after formatting):

r"""raw"""

(fstring "{x}")

Tests f-string emission.

Lisp S-Expression:

(fstring "{x}")

Generated Python (after formatting):

f"{x}"

(fstring3 "{x}")

Tests triple-quoted f-string emission.

Lisp S-Expression:

(fstring3 "{x}")

Generated Python (after formatting):

f"""{x}"""

(comment "note")

Tests single line comment emission.

Lisp S-Expression:

(comment "note")

Generated Python (after formatting):

# note

(comments "line1" "line2")

Tests multi-line comment emission.

Lisp S-Expression:

(comments "line1" "line2")

Generated Python (after formatting):

# line1
# line2

(symbol foo-bar)

Tests symbol emission with hyphen to colon conversion.

Lisp S-Expression:

(symbol foo-bar)

Generated Python (after formatting):

foo: bar

(lambda (x) (+ x 1))

Tests lambda emission with a single expression body.

Lisp S-Expression:

(lambda (x) (+ x 1))

Generated Python (after formatting):

lambda x: x + 1

(class Foo nil (def __init__ (self x) (setf (dot self x) x)))

Tests class emission with a simple initializer.

Lisp S-Expression:

(class Foo nil (def __init__ (self x) (setf (dot self x) x)))

Generated Python (after formatting):

class Foo:
    def __init__(self, x):
        self.x = x

(class Child (Base) (def __init__ (self) (return 1)))

Tests class emission with a parent class.

Lisp S-Expression:

(class Child (Base) (def __init__ (self) (return 1)))

Generated Python (after formatting):

class Child(Base):
    def __init__(self):
        return 1

(do0 (setf a 1) (setf b 2))

Tests do0 emission without extra indentation.

Lisp S-Expression:

(do0 (setf a 1) (setf b 2))

Generated Python (after formatting):

a = 1
b = 2

(cell (setf a 1))

Tests cell emission with export comment.

Lisp S-Expression:

(cell (setf a 1))

Generated Python (after formatting):

# export
a = 1

(space alpha beta)

Tests space emission.

Lisp S-Expression:

(space alpha beta)

Generated Python (after formatting):

alpha beta

(setf (aref u2 0 0) v)

Tests assigning to a specific index via aref inside setf.

Lisp S-Expression:

(setf (aref u2 0 0) v)

Generated Python (after formatting):

u2[0, 0] = v

`(def simulate (e y &key (t_max 1.0))

  (declare (type float e)
           (type list y)
           (type float t_max)
           (values list))
  (return y))`

Tests function definitions with parameter and return type declarations.

Lisp S-Expression:

(def simulate (e y &key (t_max 1.0))
 (declare (type float e)
          (type list y)
          (type float t_max)
          (values list))
 (return y))

Generated Python (after formatting):

def simulate(e: float, y: list, t_max: float = 1.0) -> list:
    return y

(space async (def time_generator nil (return 1)))

Tests async function definitions using space construct.

Lisp S-Expression:

(space async (def time_generator nil (return 1)))

Generated Python (after formatting):

async def time_generator():
    return 1

(do0 (@rt (string "/")) (def get (request) (return 1)))

Tests function decorators using the symbol fallback starting with @.

Lisp S-Expression:

(do0 (@rt (string "/")) (def get (request) (return 1)))

Generated Python (after formatting):

@rt("/")
def get(request):
    return 1

(curly (for-generator ((ntuple i s) (enumerate chars)) (slice s (+ i 1))))

Tests dictionary comprehension using curly, for-generator and slice.

Lisp S-Expression:

(curly (for-generator ((ntuple i s) (enumerate chars)) (slice s (+ i 1))))

Generated Python (after formatting):

{s: i + 1 for i, s in enumerate(chars)}

(list (for-generator (r responses) r))

Tests list comprehension using list and for-generator.

Lisp S-Expression:

(list (for-generator (r responses) r))

Generated Python (after formatting):

[r for r in responses]

(func **tub.input)

Tests keyword argument unpacking in function calls.

Lisp S-Expression:

(func **tub.input)

Generated Python (after formatting):

func(**tub.input)

(def foo (self *args **kwargs) (return 1))

Tests function definitions with *args and **kwargs unpacking parameters.

Lisp S-Expression:

(def foo (self *args **kwargs) (return 1))

Generated Python (after formatting):

def foo(self, *args, **kwargs):
    return 1

(foo)

Tests function/constructor call with no arguments.

Lisp S-Expression:

(foo)

Generated Python (after formatting):

foo()

(tuple 2.2 2.2d0)

Tests single-float and double-float number representations.

Lisp S-Expression:

(tuple 2.2 2.2d0)

Generated Python (after formatting):

(
    2.2,
    2.2,
)

#C(1.0 2.0)

Tests representation of complex numbers.

Lisp S-Expression:

#C(1.0 2.0)

Generated Python (after formatting):

1.0 + 1j * 2.0

("list" generator)

Tests raw string in function call position.

Lisp S-Expression:

("list" generator)

Generated Python (after formatting):

list(generator)

(tuple (aref xf (slice)) (aref xf (slice "" max_len)))

Tests empty slice and slice with raw strings.

Lisp S-Expression:

(tuple (aref xf (slice)) (aref xf (slice "" max_len)))

Generated Python (after formatting):

(
    xf[:],
    xf[:max_len],
)

(tuple (paren* * (+ a b)) (paren* + (+ a b)))

Tests paren* construct for precedence-aware parentheses.

Lisp S-Expression:

(tuple (paren* * (+ a b)) (paren* + (+ a b)))

Generated Python (after formatting):

(
    (a + b),
    a + b,
)

(do0 "df = pd.read_csv('data.csv')" "@threaded" (def func nil (return 1)))

Tests raw code insertion via bare strings at block level.

Lisp S-Expression:

(do0 "df = pd.read_csv('data.csv')" "@threaded" (def func nil (return 1)))

Generated Python (after formatting):

df = pd.read_csv("data.csv")


@threaded
def func():
    return 1

(tuple (lambda () 42) (lambda (x y) (+ x y)))

Tests lambda functions with zero or multiple arguments.

Lisp S-Expression:

(tuple (lambda () 42) (lambda (x y) (+ x y)))

Generated Python (after formatting):

(
    lambda: 42,
    lambda x, y: x + y,
)

(tuple (super) (super ImageModel self))

Tests calling superclass methods in Python.

Lisp S-Expression:

(tuple (super) (super ImageModel self))

Generated Python (after formatting):

(
    super(),
    super(ImageModel, self),
)

(dot model (aref weights i j) (item))

Tests chained dot access including functions and array references.

Lisp S-Expression:

(dot model (aref weights i j) (item))

Generated Python (after formatting):

model.weights[i, j].item()

(setf (ntuple a b) (tuple 1 2))

Tests assignment to unpacked values on the left hand side.

Lisp S-Expression:

(setf (ntuple a b) (tuple 1 2))

Generated Python (after formatting):

a, b = (
    1,
    2,
)

(export (setf a 1))

Tests 'export' comment cell construct.

Lisp S-Expression:

(export (setf a 1))

Generated Python (after formatting):

# |export
a = 1

(indent a)

Tests standalone 'indent' formatting construct.

Lisp S-Expression:

(indent a)

Generated Python (after formatting):

a

(lambda (x &key (y 2)) (+ x y))

Tests lambda expression with keyword arguments.

Lisp S-Expression:

(lambda (x &key (y 2)) (+ x y))

Generated Python (after formatting):

lambda x, y=2: x + y

(aref arr ":" 0)

Tests raw string index insertion inside aref.

Lisp S-Expression:

(aref arr ":" 0)

Generated Python (after formatting):

arr[:, 0]

(tuple (dict) (dictionary))

Tests empty dict literal and empty dictionary constructor.

Lisp S-Expression:

(tuple (dict) (dictionary))

Generated Python (after formatting):

(
    {},
    dict(),
)

`(do0 (def f1 nil (return)) (def f2 nil (return x))

  (def f3 nil (return (ntuple x y))))`

Tests return statement variants including empty return, single value, and multi-value returns.

Lisp S-Expression:

(do0 (def f1 nil (return)) (def f2 nil (return x))
 (def f3 nil (return (ntuple x y))))

Generated Python (after formatting):

def f1():
    return


def f2():
    return x


def f3():
    return x, y

(def f nil pass)

Tests standalone 'pass' statement representation.

Lisp S-Expression:

(def f nil pass)

Generated Python (after formatting):

def f():
    pass

(curly (for-generator (x (range 5)) x))

Tests set comprehension using curly braces and for-generator.

Lisp S-Expression:

(curly (for-generator (x (range 5)) x))

Generated Python (after formatting):

{x for x in range(5)}

(def outer (x) (def inner (y) (return (+ x y))) (return inner))

Tests nested function definition emission.

Lisp S-Expression:

(def outer (x) (def inner (y) (return (+ x y))) (return inner))

Generated Python (after formatting):

def outer(x):
    def inner(y):
        return x + y

    return inner

(class Outer nil (class Inner nil (def f (self) pass)))

Tests nested class definition emission.

Lisp S-Expression:

(class Outer nil (class Inner nil (def f (self) pass)))

Generated Python (after formatting):

class Outer:
    class Inner:
        def f(self):
            pass

(def foo (x &key (y (+ 1 2))) (return (* x y)))

Tests default parameters in function definitions with expression values.

Lisp S-Expression:

(def foo (x &key (y (+ 1 2))) (return (* x y)))

Generated Python (after formatting):

def foo(x, y=1 + 2):
    return x * y

(aref arr (slice nil nil -1))

Tests slice with open bounds and a negative step value.

Lisp S-Expression:

(aref arr (slice nil nil -1))

Generated Python (after formatting):

arr[::-1]

(def foo (&key (x 1) (y 2)) (return (+ x y)))

Tests function definition with only keyword arguments.

Lisp S-Expression:

(def foo (&key (x 1) (y 2)) (return (+ x y)))

Generated Python (after formatting):

def foo(x=1, y=2):
    return x + y

(setf image.flags.writeable False)

Tests symbol emission containing dots as member access.

Lisp S-Expression:

(setf image.flags.writeable False)

Generated Python (after formatting):

image.flags.writeable = False

(tuple |0J| |1J|)

Tests symbol fallback for complex number literals like 1j or 0j.

Lisp S-Expression:

(tuple |0J| |1J|)

Generated Python (after formatting):

(
    0j,
    1j,
)

(- "1/8" x)

Tests raw code insertion inside an expression using a bare string.

Lisp S-Expression:

(- "1/8" x)

Generated Python (after formatting):

1 / 8 - x

`(setf x 0.3333333333333333d0

       y 123456.78901234567d0
       z 1.0d-20
       w 1.0d30)`

Tests that float literals are emitted with enough digits to read back as the very same number.

Lisp S-Expression:

(setf x 0.3333333333333333d0
      y 123456.78901234567d0
      z 1.0d-20
      w 1.0d30)

Generated Python (after formatting):

x = 0.3333333333333333
y = 123456.78901234567
z = 1.0e-20
w = 1.0e30

(print (== 0.3333333333333333d0 (/ 1 3)))

Verifies that an emitted double float literal is bit-identical to the value Python computes.

Lisp S-Expression:

(print (== 0.3333333333333333d0 (/ 1 3)))

Generated Python (after formatting):

print(0.3333333333333333 == (1 / 3))

(list (? (> x 0) (for-generator (x xs) x)))

Tests the two-argument ternary as the filter of a comprehension (the else branch is omitted).

Lisp S-Expression:

(list (? (> x 0) (for-generator (x xs) x)))

Generated Python (after formatting):

[x for x in xs if x > 0]

import Forms

(import sys)

Tests single import emission.

Lisp S-Expression:

(import sys)

Generated Python (after formatting):

import sys

(import (np numpy))

Tests import emission with alias.

Lisp S-Expression:

(import (np numpy))

Generated Python (after formatting):

import numpy as np

(imports (sys (np numpy) (plt matplotlib.pyplot)))

Tests multiple import emissions with aliases.

Lisp S-Expression:

(imports (sys (np numpy) (plt matplotlib.pyplot)))

Generated Python (after formatting):

import sys
import numpy as np
import matplotlib.pyplot as plt

(import-from math sin cos)

Tests from-import emission.

Lisp S-Expression:

(import-from math sin cos)

Generated Python (after formatting):

from math import sin, cos

(imports-from (math sin cos) (pathlib Path))

Tests multiple from-import emissions.

Lisp S-Expression:

(imports-from (math sin cos) (pathlib Path))

Generated Python (after formatting):

from math import sin, cos
from pathlib import Path

operator Forms

(+ 1 2)

Tests the '+' operator with two integer arguments.

Lisp S-Expression:

(+ 1 2)

Generated Python (after formatting):

1 + 2

(incf a 2)

Tests incf emission with explicit increment.

Lisp S-Expression:

(incf a 2)

Generated Python (after formatting):

a += 2

(decf a 3)

Tests decf emission with explicit decrement.

Lisp S-Expression:

(decf a 3)

Generated Python (after formatting):

a -= 3

(and a b)

Tests logical and emission.

Lisp S-Expression:

(and a b)

Generated Python (after formatting):

a and b

(or a b)

Tests logical or emission.

Lisp S-Expression:

(or a b)

Generated Python (after formatting):

a or b

(== a b)

Tests equality comparison emission.

Lisp S-Expression:

(== a b)

Generated Python (after formatting):

a == b

(!= a b)

Tests inequality comparison emission.

Lisp S-Expression:

(!= a b)

Generated Python (after formatting):

a != b

(< a b)

Tests less-than comparison emission.

Lisp S-Expression:

(< a b)

Generated Python (after formatting):

a < b

(<= a b)

Tests less-than-or-equal comparison emission.

Lisp S-Expression:

(<= a b)

Generated Python (after formatting):

a <= b

(> a b)

Tests greater-than comparison emission.

Lisp S-Expression:

(> a b)

Generated Python (after formatting):

a > b

(>= a b)

Tests greater-than-or-equal comparison emission.

Lisp S-Expression:

(>= a b)

Generated Python (after formatting):

a >= b

(in a b)

Tests membership comparison emission.

Lisp S-Expression:

(in a b)

Generated Python (after formatting):

(a in b)

(not-in a b)

Tests negative membership comparison emission.

Lisp S-Expression:

(not-in a b)

Generated Python (after formatting):

(a not in b)

(is a b)

Tests identity comparison emission.

Lisp S-Expression:

(is a b)

Generated Python (after formatting):

(a is b)

(is-not a b)

Tests negative identity comparison emission.

Lisp S-Expression:

(is-not a b)

Generated Python (after formatting):

(a is not b)

(% a b)

Tests modulo operator emission.

Lisp S-Expression:

(% a b)

Generated Python (after formatting):

a % b

(- a b)

Tests subtraction operator emission.

Lisp S-Expression:

(- a b)

Generated Python (after formatting):

a - b

(* a b c)

Tests multiplication operator emission.

Lisp S-Expression:

(* a b c)

Generated Python (after formatting):

a * b * c

(/ a b)

Tests division operator emission.

Lisp S-Expression:

(/ a b)

Generated Python (after formatting):

a / b

(@ a b)

Tests matrix-multiplication operator emission.

Lisp S-Expression:

(@ a b)

Generated Python (after formatting):

a @ b

(// a b)

Tests floor division operator emission.

Lisp S-Expression:

(// a b)

Generated Python (after formatting):

a // b

(** a b)

Tests exponentiation operator emission.

Lisp S-Expression:

(** a b)

Generated Python (after formatting):

a**b

(<< a b)

Tests left shift operator emission.

Lisp S-Expression:

(<< a b)

Generated Python (after formatting):

a << b

(>> a b)

Tests right shift operator emission.

Lisp S-Expression:

(>> a b)

Generated Python (after formatting):

a >> b

(& a b)

Tests bitwise and operator emission.

Lisp S-Expression:

(& a b)

Generated Python (after formatting):

a & b

(logand a b c)

Tests logand emission.

Lisp S-Expression:

(logand a b c)

Generated Python (after formatting):

a & b & c

(^ a b)

Tests bitwise xor operator emission.

Lisp S-Expression:

(^ a b)

Generated Python (after formatting):

a ^ b

(logxor a b)

Tests logxor emission.

Lisp S-Expression:

(logxor a b)

Generated Python (after formatting):

a ^ b

(|\|| a b)

Tests bitwise or operator emission.

Lisp S-Expression:

(|\|| a b)

Generated Python (after formatting):

a | b

(logior a b c)

Tests bitwise or operator emission.

Lisp S-Expression:

(logior a b c)

Generated Python (after formatting):

a | b | c

(print (+ 5 8))

Verifies that the generated code for '+' executes correctly.

Lisp S-Expression:

(print (+ 5 8))

Generated Python (after formatting):

print(5 + 8)

(tuple (- x) (/ x))

Tests unary minus and unary division operators.

Lisp S-Expression:

(tuple (- x) (/ x))

Generated Python (after formatting):

(
    -x,
    1.0 / x,
)

(~ x)

Tests unary bitwise negation operator.

Lisp S-Expression:

(~ x)

Generated Python (after formatting):

~x

(not a)

Tests unary logical negation operator.

Lisp S-Expression:

(not a)

Generated Python (after formatting):

not a

(tuple (< a b c) (<= x y z) (== d e f))

Tests chained comparison operators with multiple arguments.

Lisp S-Expression:

(tuple (< a b c) (<= x y z) (== d e f))

Generated Python (after formatting):

(
    a < b < c,
    x <= y <= z,
    d == e == f,
)

(tuple (and a b c) (or d e f))

Tests logical and/or operators with more than two arguments.

Lisp S-Expression:

(tuple (and a b c) (or d e f))

Generated Python (after formatting):

(
    a and b and c,
    d or e or f,
)

(tuple (logand a b c) (logxor x y z) (logior p q r))

Tests bitwise and/xor/or operators with more than two arguments.

Lisp S-Expression:

(tuple (logand a b c) (logxor x y z) (logior p q r))

Generated Python (after formatting):

(
    a & b & c,
    x ^ y ^ z,
    p | q | r,
)

(tuple (not (== a b)) (not (and a b)))

Tests logical not precedence parenting.

Lisp S-Expression:

(tuple (not (== a b)) (not (and a b)))

Generated Python (after formatting):

(
    not a == b,
    not (a and b),
)

(@ a b c)

Tests matrix multiplication chaining with multiple operands.

Lisp S-Expression:

(@ a b c)

Generated Python (after formatting):

a @ b @ c

(/ a b c)

Tests division with more than two arguments (left associative, like Common Lisp and Python).

Lisp S-Expression:

(/ a b c)

Generated Python (after formatting):

a / b / c

(// a b c)

Tests floor division with more than two arguments.

Lisp S-Expression:

(// a b c)

Generated Python (after formatting):

a // b // c

(** (- a) 2)

Tests that a unary minus below ** keeps its parentheses: python reads -a2 as -(a2).

Lisp S-Expression:

(** (- a) 2)

Generated Python (after formatting):

(-a) ** 2

(print (** -2 2))

Tests that a negative literal below ** keeps its parentheses.

Lisp S-Expression:

(print (** -2 2))

Generated Python (after formatting):

print((-2) ** 2)

(** (/ a) 2)

Tests that the one-argument division below ** keeps its parentheses.

Lisp S-Expression:

(** (/ a) 2)

Generated Python (after formatting):

(1.0 / a) ** 2

(== a (== b c))

Tests that a comparison inside a comparison keeps its parentheses: python chains a==b==c.

Lisp S-Expression:

(== a (== b c))

Generated Python (after formatting):

a == (b == c)

(print (== (< 1 2) (< 3 4)))

Tests that a comparison keeps its parentheses on either side of a comparison.

Lisp S-Expression:

(print (== (< 1 2) (< 3 4)))

Generated Python (after formatting):

print((1 < 2) == (3 < 4))

(print (< 1 2 3))

Tests that an n-ary comparison is emitted as a python comparison chain, like in Common Lisp.

Lisp S-Expression:

(print (< 1 2 3))

Generated Python (after formatting):

print(1 < 2 < 3)

(print (<< 1 (>> 8 1)))

Tests that a shift as the right operand of a shift keeps its parentheses (<< is left associative).

Lisp S-Expression:

(print (<< 1 (>> 8 1)))

Generated Python (after formatting):

print(1 << (8 >> 1))

(tuple (@ a (* b c)) (* a (@ b c)))

Tests that @ and * keep their parentheses inside each other although they share a precedence level.

Lisp S-Expression:

(tuple (@ a (* b c)) (* a (@ b c)))

Generated Python (after formatting):

(
    a @ (b * c),
    a * (b @ c),
)

(tuple (+ a (+ b c)) (* a (* b c)) (and a (and b c)) (logand a (& b c)))

Tests that operators which are associative with each other stay flat.

Lisp S-Expression:

(tuple (+ a (+ b c)) (* a (* b c)) (and a (and b c)) (logand a (& b c)))

Generated Python (after formatting):

(
    a + b + c,
    a * b * c,
    a and b and c,
    a & b & c,
)

(tuple (and a (or b c)) (or a (and b c)))

Tests that 'or' below 'and' is parenthesized while 'and' below 'or' is not.

Lisp S-Expression:

(tuple (and a (or b c)) (or a (and b c)))

Generated Python (after formatting):

(
    a and (b or c),
    a or b and c,
)

(tuple (? c (? d e f) g) (? c a (? d e f)))

Tests the conditional expression: only the else branch may contain another one without parentheses.

Lisp S-Expression:

(tuple (? c (? d e f) g) (? c a (? d e f)))

Generated Python (after formatting):

(
    (e if d else f) if c else g,
    a if c else e if d else f,
)

(dot (- a b) c)

Tests that the object of a dot form is parenthesized when it is an operator expression.

Lisp S-Expression:

(dot (- a b) c)

Generated Python (after formatting):

(a - b).c

(print (aref (+ (list 1 2) (list 3)) 2))

Tests that the sequence of an aref form is parenthesized when it is an operator expression.

Lisp S-Expression:

(print (aref (+ (list 1 2) (list 3)) 2))

Generated Python (after formatting):

print(([1, 2] + [3])[2])

(tuple (aref (dot a b) i) (dot (aref a i) b) (aref (aref a i) j))

Tests that indexing and attribute access are not parenthesized inside each other.

Lisp S-Expression:

(tuple (aref (dot a b) i) (dot (aref a i) b) (aref (aref a i) j))

Generated Python (after formatting):

(
    a.b[i],
    a[i].b,
    a[i][j],
)

(in (== a b) c)

Tests that a comparison inside 'in' keeps its parentheses, otherwise python would chain them.

Lisp S-Expression:

(in (== a b) c)

Generated Python (after formatting):

((a == b) in c)

(dot (lambda (x) x) __name__)

Tests that a lambda used inside an expression is parenthesized, otherwise its body would swallow the rest.

Lisp S-Expression:

(dot (lambda (x) x) __name__)

Generated Python (after formatting):

(lambda x: x).__name__

(? c (ntuple a b) (ntuple d e))

Tests that a bare comma list used inside an expression is parenthesized.

Lisp S-Expression:

(? c (ntuple a b) (ntuple d e))

Generated Python (after formatting):

(a, b) if c else (d, e)

(tuple (- a -1) (/ a -1) (* a -1))

Tests a negative literal as the right operand of the non-associative operators.

Lisp S-Expression:

(tuple (- a -1) (/ a -1) (* a -1))

Generated Python (after formatting):

(
    a - (-1),
    a / (-1),
    a * -1,
)

(setf z (+ #C(1.0d0 2.0d0) 1))

Tests that a complex literal, which is emitted as a sum, is parenthesized when used as an operand.

Lisp S-Expression:

(setf z (+ #C(1.0d0 2.0d0) 1))

Generated Python (after formatting):

z = (1.0 + 1j * 2.0) + 1

(tuple (- (- a)) (~ (~ a)) (not (not a)))

Tests that nested unary operators need no parentheses.

Lisp S-Expression:

(tuple (- (- a)) (~ (~ a)) (not (not a)))

Generated Python (after formatting):

(
    --a,
    ~~a,
    not not a,
)