Add built-in functions to exit the program and format floats as strings.

This commit is contained in:
Jesse D. McDonald 2012-07-06 10:51:27 -05:00
parent dcacdecfff
commit 8ba3fa0860
2 changed files with 27 additions and 0 deletions

View File

@ -2,6 +2,7 @@
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "gc.h"
@ -23,6 +24,8 @@ static void bi_display(interp_state_t *state);
static void bi_register_finalizer(interp_state_t *state);
static void bi_current_context(interp_state_t *state);
static void bi_call_with_context(interp_state_t *state);
static void bi_exit(interp_state_t *state);
static void bi_float_to_string(interp_state_t *state);
void builtin_init(void)
{
@ -58,6 +61,10 @@ void builtin_init(void)
register_builtin(BI_CURRENT_CONTEXT, make_builtin_fn(bi_current_context));
register_builtin(BI_CALL_WITH_CONTEXT, make_builtin_fn(bi_call_with_context));
register_builtin(BI_EXIT, make_builtin_fn(bi_exit));
register_builtin(BI_FLOAT_TO_STRING, make_builtin_fn(bi_float_to_string));
}
void register_builtin(const char *name, value_t value)
@ -227,4 +234,22 @@ static void bi_call_with_context(interp_state_t *state)
state->kw_vals.value = NIL;
}
static void bi_exit(interp_state_t *state)
{
state->ctx.value = NIL;
state->lambda.value = END_PROGRAM;
state->kw_args.value = NIL;
state->kw_vals.value = NIL;
}
static void bi_float_to_string(interp_state_t *state)
{
char buffer[32];
native_float_t flt = get_float(CAR(state->argv.value));
snprintf(buffer, sizeof buffer, "%.18g", (double)flt);
interp_return_values(state, cons(string_to_value(buffer), NIL));
}
/* vim:set sw=2 expandtab: */

View File

@ -28,6 +28,8 @@
#define BI_DISPLAY "display"
#define BI_CURRENT_CONTEXT "current-context"
#define BI_CALL_WITH_CONTEXT "call-with-context"
#define BI_EXIT "exit"
#define BI_FLOAT_TO_STRING "float->string"
/* Lambda: Instances of this structure are fundamental callable objects. */
#define LAMBDA_SLOT_GLOBAL_VARS 0