SquareScript Standard Macros - radishengine/drowsy GitHub Wiki
Macros are steps that can be automatically expanded to other steps.
['<^_^>', 'a', 'b', 'string^System.String', '^System.IO.Pipe', '^^i']
to
['< >', 'a', 'b', 'string', 'System.IO.Pipe', 'i'],
['<^>', 'string', 'System.String'],
['<^>', 'System.IO.Pipe'],
['<^>', 'i', ['i']],
['<:>.[]', 'u8', ['target'], 'byteField']
to
['<:>', 'u8', ['.[]', ['target'], 'byteField']]
Macro Name: 'function'
Evaluates To: the callable function object
Parameters:
-
(optional) scoped name:
- if an existing scoped value name, the function also assigned to this
- if a string literal, a new scope is created here that extends to the end of the current block, containing a new named value for this function object
- block or step: the function body definition
If the scoped name parameter is not given, this is effectively identical to a 'c():'
step.
Macro Step Name: '<pop>'
Evaluates to: null
Parameters:
- The names of the popped values in the new scope.
Similar to '< >'
except the values are pulled from the stack, with the rightmost value taken from the top.
For example, this:
[
['<pop>', 'a','b','c'],
]
...gets expanded to:
[
['< >', 'a','b','c'],
['set', 'c', ['pop']],
['set', 'b', ['pop']],
['set', 'a', ['pop']],
]
Scoped stack operations are useful if you want to do pretty much anything more complex than popping the top value.
For example, to duplicate the top value:
['<pop>','top'],
['push', ['top'], ['top']],
['</>'],
To re-insert the top value five places down:
['<pop>','-5','-4','-3','-2','-1'],
['push',['-1'],['-5'],['-4'],['-3'],['-2']],
['</>'],
Or simply to get the order of operands right in a non-commutative operation:
['<pop>', 'left', 'right'],
['push', ['/', ['left'], ['right']]],
['</>'],
As step parameters are always evaluated left-to-right, if the stack-pop is the right operand with the left operand below, ['/', ['pop'], ['pop']]
would end up as (right / left)
.
In fact, this is such a useful use-case, there is a second macro just for this.
['...LR', ['/', ['left'], ['right']]]
to:
['...!', ['<pop>','left','right'], ['/', ['left'], ['right']]]
to:
['...!',
['< >', 'left', 'right'],
['set', ['right'], ['pop']],
['set', ['left'], ['pop']],
['/', ['left'], ['right']]
]
Parameter:
- step to run in context where
['left']
and['right']
are defined
This macro is a specialization of the Scoped Pop Stack that pops the rightmost two values from the stack, calls them "left" and "right", and executes the given operation in this context.
['binop', '<SYMBOL>']
to
['push', ['...!', ['<pop>','L','R'], ['<SYMBOL>','L','R']]]
['unop', '<SYMBOL>']
to
['push', ['<SYMBOL>', ['pop']]]
['c(<pop>)', ['func'], 3]
to
['...!',
['<pop>', 'a1','a2','a3'],
['c()', ['func'], ['a1'],['a2'],['a3']]
]