💾 Archived View for vierkantor.com › xukut › manual › swail › function.gmi captured on 2024-07-08 at 23:27:01. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-05-10)

-=-=-=-=-=-=-

XukutOS manual → Swail → Functions

Functions are the smallest useful unit of code in Swail, and are the canonical example of callable objects. Generally you'll encounter functions of the type `swail:fn', which are also called `fn_sexpr' in assembly code. Apart from that there are some low-level types of functions like `fn_asmcc' and `fn_oarcc' that are used to implement Swail. We'll focus on `swail:fn' in this manual chapter.

To define a function, you need its parameters and a bunch of code. The special form `(swail:fn ,params . ,code)' constructs a function out of these parts.

Calling a function `fn' with arguments `args' is done with the anonymous form: `(,fn . ,args)', or using the function `swail:call'. When a function is called with a list of arguments, Swail creates a new lexical environment extending the environment where the function was created. The parameters are bound to the arguments, and then the function's code is executed. The last result(s) of evaluation is returned from the function; this is the result of the call.

function (swail:call fn args)

Call the object `fn' on the list of arguments `args'. Returns the result of this call.

This is useful if the list of arguments has a dynamically determined size.

Examples:

(swail:call (swail:fn (a b c) (swail:list a b c)) (swail:cons 1 (swail:list 2 3))) ; evaluates to '(1 2 3)
(swail:call swail:list args) ; evaluates to `args', for all lists `args'
(swail:call fn (swail:list . args)) ; evaluates like `(fn args)', for all `fn' and `args'

Argument parsing

The process of determining the value of each parameter during execution of a function is called argument parsing. If a `swail:argument-error' is raised during argument parsing, the default restart is to retry argument parsing with a newly supplied list of arguments. Let `args' be a list of arguments and `params' the form specifying the parameters to the function (note that `params' doesn't have to be a list).

If `args' is not a list, then an `argument-error' is raised.

More information on errors and restarts.

If `params' is a list, then `args' should be a list of exactly the same length, otherwise an `argument-error' is raised. The `n'th entry of `params' is bound to the `n'th entry of `args'.

More information about variable binding.

If `params' is a dotted list (including the case that `params' is a single symbol), then `args' should be a list of at least the length of `params'. The `n'th entry of `params' is bound to the `n'th entry of `args', and the final tail of `params' is bound to the list of remaining `args' (so it will be `nil' if the length of `args' and `params' are the same).

If you need more complicated ways of parsing arguments, you can use methods instead.

See also

How to interface with functions from assembly code

Any questions? Contact me:

By email at vierkantor@vierkantor.com

Through mastodon @vierkantor@mastodon.vierkantor.com

XukutOS user's manual: Swail

XukutOS user's manual

XukutOS main page