1. [ and ] (Array Brackets)
Array construction operators that work together to create literal arrays in PostScript.
1.1. Overview
The bracket operators [ and ] provide the most convenient syntax for constructing arrays with known contents:
-
[marks the beginning of array construction by pushing a mark on the operand stack -
]ends array construction by collecting all objects above the mark into a new array
These operators are typically used together as a pair to define literal arrays in PostScript code.
1.3. Stack Effects
1.4. Description
1.4.1. How Array Construction Works
The [ operator pushes a special mark object on the operand stack to indicate the beginning of an array being constructed. It is functionally equivalent to the mark operator, but its use with ] gives it special significance for array construction.
The ] operator completes array construction by:
-
Counting all objects on the operand stack down to the topmost mark
-
Removing those objects from the stack
-
Creating a new array containing those objects in order
-
Removing the mark
-
Pushing the new array on the stack
The resulting array is created with the literal attribute and allocated in local or global VM according to the current VM allocation mode.
1.6. Examples
[ 1 2 3 ] % Creates array [1 2 3]
[ 42 (hello) /name true ] % Array with different types
[ 1 [ 2 3 ] 4 ] % Array containing another array
% Result: [1 [2 3] 4]
[ ] % Creates empty array []
[ 1 2 add 3 4 mul ] % Expressions evaluated before array creation
% Result: [3 12]
[ 10 20 add 30 ] % Creates [30 30]
1 2 3 % Stack: 1 2 3
mark exch exch exch % Stack: mark 1 2 3
] % Stack: [1 2 3]
1.7. Common Use Cases
1.8. Common Pitfalls
Unmatched Brackets - Every [ must have a corresponding ]. Unmatched brackets cause a [syntaxerror] or [unmatchedmark] error.
|
[ 1 2 3 % Missing ] causes syntaxerror
1 2 3 ] % Missing [ causes unmatchedmark error
Execution vs. Literal - Objects between [ and ] are evaluated before being placed in the array.
|
[ 1 2 add ] % Results in [3], not [1 2 add]
[ /x 5 def ] % Executes def, creates [5]
% NOT [/x 5 def]
Literal vs. Executable Arrays - Arrays created by [ … ] have the literal attribute by default. To create an array without evaluating its contents, use the procedure syntax { … }. Use cvx to make a literal array executable if needed.
|
/proc { 1 2 add } def % Procedure (unevaluated)
[ 1 2 add ] % Array [3] (evaluated)
1.9. Error Conditions
| Error | Condition |
|---|---|
[ |
Operand stack overflow when pushing mark, or resulting array would overflow stack |
[ |
No matching |
[ |
No mark on stack when |
[ |
Insufficient VM to allocate array |
1.10. Implementation Notes
-
The
[operator is functionally equivalent tomark -
The
]operator is syntactic sugar recognized during parsing -
Marks are used for error recovery and can be detected with
counttomark -
When scanned,
[ … ]creates a literal array object -
The objects are placed in the array in the order they appeared between brackets
-
Index 0 contains the first object after
[ -
The interpreter treats
[and]specially during scanning for array literal syntax
1.11. Relationship to Other Operators
1.11.1. Equivalence with mark
The [ operator is functionally equivalent to mark:
mark 1 2 3 ] % Same as [ 1 2 3 ]
[ 1 2 3 % Must be closed with ]
However, [ and ] provide clearer syntax for array construction.
1.11.2. Comparison with astore
The ] operator is similar to astore but more convenient:
% Using [ ]
[ 1 2 3 ]
% Equivalent using astore
1 2 3 3 array astore
The bracket syntax is clearer and doesn’t require knowing the count in advance.
1.12. See Also
-
mark- Push mark object -
cleartomark- Clear to mark -
counttomark- Count objects to mark -
array- Create uninitialized array -
astore- Store stack into array -
packedarray- Create read-only array