From 4aa2b717f9e050783b567540d9bb0193d692e3e9 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 31 Oct 2026 13:46:11 +0100 Subject: support floating point numbers * Extends the SciTECO type system. A number on the expression stack or in a Q-register can be either an integer or a float. * Floating point constants begin with a digit (like all other numbers), are followed by a `.` and an optional fractional part. Exponents cannot currently be written. E.g. `1.` == `1.0`. But `.0` would still be `.,0`. (!) To push an integer followed by dot, you must now write e.g. `1,.` or `1 .`. * All of the basic arithmetic operators operate like this: If both operands are integers, the result is also an integer (integer arithmetics). If at least one of the operands is a float, the result is also a float (float arithmetics). * All of the binary operators (AND, OR, XOR) always cast all operands to integer and the result is always an integer as well. * You can "cast" integers to float by `*1.` or `/1.`. You can "cast" floats to integers using `#0`. * Most commands still accept only integers. Floats are implicitly casted to integers. Float-aware commands are: =, \, ^W, Qq, Uq, %q and the conditionals. * `%q` will not change the current type of register q. * Numeric part of "lexer.font" is now a float (in points). * You cannot currently print octal and hexadecimal floats. `==` and `===` are identical to `=` on floats. * `===` is now printing hex integers in lower case. This is because SciTECO's coding style for hex constants is also lower case. * `--with-teco-integer` renamed to `--with-teco-number`. --- src/parser.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 45e1e3c..231ad3e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -792,15 +792,17 @@ teco_state_stringbuilding_ctle_num_input(teco_machine_stringbuilding_t *ctx, gun /* parse-only mode */ return &teco_state_stringbuilding_start; - teco_int_t value; - if (!qreg->vtable->get_integer(qreg, &value, error)) + teco_number_t value; + gboolean is_float; + if (!qreg->vtable->get_number(qreg, &value, &is_float, error)) return NULL; /* * NOTE: Numbers can always be safely formatted as null-terminated strings. */ gchar buffer[TECO_EXPRESSIONS_FORMAT_LEN]; - const gchar *num = teco_expressions_format(buffer, value, ctx->qreg_table_locals->radix); + const gchar *num = is_float ? teco_expressions_format_float(buffer, value.f, 6) + : teco_expressions_format_int(buffer, value.i, ctx->qreg_table_locals->radix); teco_machine_stringbuilding_append(ctx, num, strlen(num)); return &teco_state_stringbuilding_start; @@ -830,7 +832,7 @@ teco_state_stringbuilding_ctle_u_input(teco_machine_stringbuilding_t *ctx, gunic return &teco_state_stringbuilding_start; teco_int_t value; - if (!qreg->vtable->get_integer(qreg, &value, error)) + if (!teco_qreg_get_integer(qreg, &value, error)) return NULL; if (!teco_machine_stringbuilding_append_c(ctx, value)) { -- cgit v1.2.3