From 8ba3fa08609697a0ab63ddfc278b1025b3062481 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Fri, 6 Jul 2012 10:51:27 -0500 Subject: [PATCH] Add built-in functions to exit the program and format floats as strings. --- builtin.c | 25 +++++++++++++++++++++++++ builtin.h | 2 ++ 2 files changed, 27 insertions(+) diff --git a/builtin.c b/builtin.c index c9ecca7..0f74138 100644 --- a/builtin.c +++ b/builtin.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #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: */ diff --git a/builtin.h b/builtin.h index a3bc97c..04927a4 100644 --- a/builtin.h +++ b/builtin.h @@ -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