diff options
Diffstat (limited to 'src/stdio-commands.c')
| -rw-r--r-- | src/stdio-commands.c | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/stdio-commands.c b/src/stdio-commands.c index 34179c3..4bed7fd 100644 --- a/src/stdio-commands.c +++ b/src/stdio-commands.c @@ -18,6 +18,8 @@ #include "config.h" #endif +#include <string.h> + #include <glib.h> #include "sciteco.h" @@ -59,17 +61,31 @@ teco_print(teco_machine_main_t *ctx, guint radix, GError **error) return FALSE; } - /* - * teco_expressions_format() cannot easily be used - * to format __unsigned__ integers. - */ - const gchar *fmt = "%" TECO_INT_MODIFIER "d"; - switch (radix) { - case 8: fmt = "%" TECO_INT_MODIFIER "o"; break; - case 16: fmt = "%" TECO_INT_MODIFIER "X"; break; + teco_number_t number = teco_expressions_peek_num(0); + gboolean is_float = teco_expressions_peek_op(0) == TECO_OP_FLOAT; + + gchar buf[TECO_EXPRESSIONS_FORMAT_LEN]; + gint len; + + if (is_float) { + /* + * FIXME: Perhaps we should support a custom formatter + * for octal and hexadecimals? + */ + teco_expressions_format_float(buf, number.f, 6); + len = strlen(buf); + } else { + /* + * teco_expressions_format_int() cannot easily be used + * to format __unsigned__ integers. + */ + const gchar *fmt = "%" TECO_INT_MODIFIER "d"; + switch (radix) { + case 8: fmt = "%" TECO_INT_MODIFIER "o"; break; + case 16: fmt = "%" TECO_INT_MODIFIER "x"; break; + } + len = g_snprintf(buf, sizeof(buf), fmt, number.i); } - gchar buf[32]; - gint len = g_snprintf(buf, sizeof(buf), fmt, teco_expressions_peek_num(0)); g_assert(len > 0); if (!teco_machine_main_eval_colon(ctx)) buf[len++] = '\n'; @@ -79,18 +95,20 @@ teco_print(teco_machine_main_t *ctx, guint radix, GError **error) } /*$ "=" "==" "===" ":=" ":==" ":===" "print number" - * <n>= -- Print integer as message + * <n>= -- Print number as message * <n>== * <n>=== * <n>:= * <n>:== * <n>:=== * - * Shows integer <n> as a message in the message line and/or + * Shows integer or float <n> as a message in the message line and/or * on the console. - * One \(lq=\(rq formats the integer as a signed decimal number, - * \(lq==\(rq formats as an unsigned octal number and + * One \(lq=\(rq formats the number as a signed decimal, + * \(lq==\(rq formats as an unsigned octal and * \(lq===\(rq as an unsigned hexadecimal number. + * Floating point numbers will currently always be formatted in + * decimal with a precision of 6 and without an exponent. * It is logged with the user-message severity. * The command fails if <n> is not given. * @@ -144,7 +162,7 @@ teco_state_print_decimal_input(teco_machine_main_t *ctx, gunichar chr, GError ** if (ctx->flags.mode == TECO_MODE_NORMAL) { if (G_LIKELY(!is_executing) && !teco_print(ctx, 10, error)) return NULL; - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); } return teco_state_start_input(ctx, chr, error); } @@ -162,7 +180,7 @@ teco_state_print_decimal_end_of_macro(teco_machine_main_t *ctx, GError **error) return TRUE; if (!teco_print(ctx, 10, error)) return FALSE; - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); return TRUE; } @@ -195,7 +213,7 @@ teco_state_print_octal_input(teco_machine_main_t *ctx, gunichar chr, GError **er if (ctx->flags.mode == TECO_MODE_NORMAL) { if (!teco_print(ctx, 16, error)) return NULL; - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); } return &teco_state_start; } @@ -203,7 +221,7 @@ teco_state_print_octal_input(teco_machine_main_t *ctx, gunichar chr, GError **er if (ctx->flags.mode == TECO_MODE_NORMAL) { if (G_LIKELY(!is_executing) && !teco_print(ctx, 8, error)) return NULL; - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); } return teco_state_start_input(ctx, chr, error); } @@ -221,7 +239,7 @@ teco_state_print_octal_end_of_macro(teco_machine_main_t *ctx, GError **error) return TRUE; if (!teco_print(ctx, 8, error)) return FALSE; - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); return TRUE; } @@ -260,7 +278,7 @@ teco_state_print_string_done(teco_machine_main_t *ctx, teco_string_t str, GError if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; - if (!teco_expressions_pop_num_calc(&type, TECO_MSG_USER, error)) + if (!teco_expressions_pop_int_calc(&type, TECO_MSG_USER, error)) return NULL; if (type < TECO_MSG_USER || type > TECO_MSG_MAX) { g_set_error_literal(error, TECO_ERROR, TECO_ERROR_FAILED, @@ -398,7 +416,7 @@ teco_state_control_typeout(teco_machine_main_t *ctx, GError **error) gchar *p = buf+args; for (gint i = 0; i < args; i++) { - teco_int_t chr = teco_expressions_pop_num(0); + teco_int_t chr = teco_expressions_pop_int(0); if (chr < 0 || chr > 0xFF) { teco_error_codepoint_set(error, "^T"); return; @@ -415,7 +433,7 @@ teco_state_control_typeout(teco_machine_main_t *ctx, GError **error) gchar *p = buf; for (gint i = args; i > 0; i--) { - teco_int_t chr = teco_expressions_peek_num(i-1); + teco_int_t chr = teco_expressions_peek_int(i-1); if (chr < 0 || !g_unichar_validate(chr)) { teco_error_codepoint_set(error, "^T"); return; @@ -424,7 +442,7 @@ teco_state_control_typeout(teco_machine_main_t *ctx, GError **error) } /* we pop only now since we had to peek in reverse order */ for (gint i = 0; i < args; i++) - teco_expressions_pop_num(0); + teco_expressions_pop_num(0, NULL); teco_interface_msg_literal(TECO_MSG_USER, buf, p-buf); } |
