Add "values"

This oneliner is way too important to be left out.
This commit is contained in:
Přemysl Eric Janouch 2017-05-26 01:36:43 +02:00
parent 735dfd026a
commit ec7a0dc95f
Signed by: p
GPG Key ID: B715679E3A361BE6
2 changed files with 13 additions and 4 deletions

View File

@ -32,17 +32,17 @@ The parser, however, does a bunch of transformations:
As an example, consider the following snippet: As an example, consider the following snippet:
print (if { eq? @var foo } { print (if { eq? @var foo } {
quote 'Hello world\n' values 'Hello world\n'
} else { } else {
quote 'Error\n' values 'Error\n'
}) })
which gets expanded to the following: which gets expanded to the following:
((print (if (quote ((eq? (set var) foo))) ((print (if (quote ((eq? (set var) foo)))
(quote ((quote 'Hello world\n'))) (quote ((values 'Hello world\n')))
else else
(quote ((quote 'Error\n')))))) (quote ((values 'Error\n'))))))
Observe that the whole program is enclosed in an implicit pair of `{}` and that Observe that the whole program is enclosed in an implicit pair of `{}` and that
`quote` is a very powerful special form which can replace many others if needed. `quote` is a very powerful special form which can replace many others if needed.
@ -85,6 +85,10 @@ Retrieve or set a named variable. The syntax sugar for retrieval is `@`.
Return a list made of given arguments. The syntax sugar for lists is `[]`. Return a list made of given arguments. The syntax sugar for lists is `[]`.
`values [<item>]...`
Return an arbitrary number of values.
`if <cond> <body> [elif <cond> <body>]... [else <body>]` `if <cond> <body> [elif <cond> <body>]... [else <body>]`
Conditional evaluation, strings evaluate to themselves. Conditional evaluation, strings evaluate to themselves.

5
ell.c
View File

@ -977,6 +977,10 @@ defn (fn_list) {
return check (ctx, (*result = new_list (values))); return check (ctx, (*result = new_list (values)));
} }
defn (fn_values) {
return !args || check (ctx, (*result = new_clone_list (args)));
}
defn (fn_if) { defn (fn_if) {
struct item *cond, *body, *keyword; struct item *cond, *body, *keyword;
for (cond = args; ; cond = keyword->next) { for (cond = args; ; cond = keyword->next) {
@ -1280,6 +1284,7 @@ static bool
init_runtime_library (struct context *ctx) { init_runtime_library (struct context *ctx) {
if (!native_register (ctx, "set", fn_set) if (!native_register (ctx, "set", fn_set)
|| !native_register (ctx, "list", fn_list) || !native_register (ctx, "list", fn_list)
|| !native_register (ctx, "values", fn_values)
|| !native_register (ctx, "if", fn_if) || !native_register (ctx, "if", fn_if)
|| !native_register (ctx, "map", fn_map) || !native_register (ctx, "map", fn_map)
|| !native_register (ctx, "print", fn_print) || !native_register (ctx, "print", fn_print)