apfl/src/globals.c

50 lines
1.3 KiB
C
Raw Normal View History

#include <stdio.h>
#include "apfl.h"
#include "context.h"
#include "globals.h"
#include "scope.h"
#define TRY_FORMAT(ctx, x) \
do { \
if (!(x)) { \
apfl_raise_const_error((ctx), APFL_RESULT_ERR, apfl_messages.io_error); \
} \
} while (0)
static void
print(apfl_ctx ctx)
{
struct apfl_format_writer w = apfl_format_file_writer(stdout);
size_t len = apfl_len(ctx, 0);
for (size_t i = 0; i < len; i++) {
if (i > 0) {
TRY_FORMAT(ctx, apfl_format_put_string(w, " "));
}
apfl_get_list_member_by_index(ctx, 0, i);
struct apfl_string_view sv = apfl_get_string(ctx, -1);
TRY_FORMAT(ctx, apfl_format_put_string(w, sv));
apfl_drop(ctx, -1);
}
if (len > 0) {
TRY_FORMAT(ctx, apfl_format_put_string(w, "\n"));
}
apfl_push_nil(ctx);
}
static const struct global_def globals[] = {
{"print", print},
{NULL, NULL},
};
const struct global_def *
apfl_globals(void)
{
return globals;
}