Object operator specials
The operator specials are the forms the compiler emits as a single object
operation rather than a call. Most map to an OBJECT_* opcode; the collection
builders at the end of the page emit the BUILD_* family instead.
Constant folding
Reducible (COUNT) operators apply left-to-right constant folding: consecutive
leading arguments that compile to a single CONST are folded together using
the corresponding Python operator before any code is emitted. Folding stops at
the first non-constant argument, which preserves dunder dispatch.
(+ 1 2 a) ; -> CONST 3, VAR a, OBJECT_OPERAND (ADD) 2
(+ 1 2) ; -> CONST 3
(+ a 1 2) ; -> VAR a, CONST 1, CONST 2, OBJECT_OPERAND (ADD) 3 (no fold)
**, instance?, and subclass? never fold.
Comparison operators
All comparison specials require at least two arguments and emit a COUNT opcode with the number of operands. Chained comparisons follow Python semantics: each adjacent pair must satisfy the relation.
Surface |
Alias |
Opcode |
Chained semantics |
Example |
|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>>> (< 0 1 2 3)
True
>>> (<= 1 2 2 4)
True
>>> (== 5 5 7 5)
False
Also: operator functions ==, !=, <, <=, >, >=, equal?, not-equal?, lt?, le?, gt?, ge?.
Membership and identity
These take exactly two operands and also emit OBJECT_COMPARE. Each has a
?-suffixed alias.
Form |
Alias |
Does |
Example |
|---|---|---|---|
|
|
Membership through |
|
|
|
Negated membership |
|
|
|
Identity |
|
|
|
Negated identity |
|
Also: operator functions in, not-in, in?, not-in?, is?, is-not?.
Type predicates
(instance? OBJ TYPE ...) checks whether OBJ is an instance of any of
the types, and (subclass? CLS BASE ...) checks the subclass relation
against any of the bases. An operand may be a tuple of types, opened one
level; a tuple inside a tuple raises TypeError. The runtime instance?
and subclass? take the same operands.
>>> (instance? 42 int)
True
>>> (subclass? list object)
True
Also: functions instance?, subclass?.
Arithmetic and bitwise operators
Each emits OBJECT_OPERAND with the matching selector. The reducible forms
take any number of operands and fold left to right. A form with more than
255 operands, or a chained comparison that wide, compiles as a call of the
operator’s runtime binding, which folds the same way.
Form |
Does |
Example |
|---|---|---|
|
Addition; one operand is unary plus |
|
|
Subtraction; one operand is negation |
|
|
Multiplication |
|
|
True division |
|
|
Floor division |
|
|
Remainder |
|
|
Exponentiation, with an optional modulus |
|
|
Matrix multiplication through |
|
|
Bitwise and |
|
|
Bitwise or |
|
|
Bitwise exclusive or |
|
|
Bitwise inversion, one operand |
|
|
Left shift |
|
|
Right shift |
|
|
Quotient and remainder as a tuple |
|
** takes a third operand as the modulus:
>>> (** 2 10 7)
2
Also: operator functions +, -, *, /, //, **, @, &, |, ^, <<, >>.
Unary and inspection operators
Form |
Opcode |
Does |
Example |
|---|---|---|---|
|
|
Logical negation of truthiness |
|
|
|
The operand’s truth value as a bool |
|
|
|
Identity against |
|
|
|
The |
|
|
|
Length through |
|
|
|
The operand’s type |
|
|
|
The attribute name list |
|
|
The instance |
|
|
|
|
An iterator over the operand |
|
|
|
The next value from an iterator, with an optional default |
|
nil? is a special, unlike None?, which is a
macro.
Also: functions nil?, repr, len, iter, next, dir, vars; type type.
String conversion and formatting
Form |
Opcode |
Does |
Example |
|---|---|---|---|
|
|
Variadic |
|
|
|
Python |
|
The second argument to format is a bare format spec – the text that would
follow the colon in a Python format string, not a {...} template:
>>> (format 42 "x")
"2a"
>>> (format 42)
"42"
See compile/#str and
compile/format.
Collection builders
These emit the BUILD_* opcodes rather than an OBJECT_* one. A literal whose
elements are all constants reduces to a single instruction.
Form |
Does |
Example |
|---|---|---|
|
Build a list; what |
|
|
Build a tuple; what |
|
|
Append to a list binding or list expression |
|
>>> var lst #[1 2]
>>> (#list-append lst 3)
>>> lst
#[1 2 3]
Also: functions #list, #tuple, #list-append.
In-place update
Form |
Does |
|---|---|
Update a variable in place; the special under |
|
Update a subscript in place |
|
Update an attribute in place |