diff options
| author | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2026-10-31 13:46:11 +0100 |
|---|---|---|
| committer | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2026-10-31 21:13:19 +0100 |
| commit | 4aa2b717f9e050783b567540d9bb0193d692e3e9 (patch) | |
| tree | 39b3734c464655e6d1c21390945209a66f952654 /src/qreg-commands.c | |
| parent | e0bf13622b0922252439e3fa8b43df9877678d75 (diff) | |
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`.
Diffstat (limited to 'src/qreg-commands.c')
| -rw-r--r-- | src/qreg-commands.c | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/src/qreg-commands.c b/src/qreg-commands.c index 0c4c8d4..14cc95f 100644 --- a/src/qreg-commands.c +++ b/src/qreg-commands.c @@ -294,7 +294,7 @@ teco_state_queryqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg, if (teco_expressions_args() > 0) { /* Query character from Q-Register string */ - teco_int_t pos = teco_expressions_pop_num(0); + teco_int_t pos = teco_expressions_pop_int(0); if (pos < 0) { teco_error_range_set(error, "Q"); return NULL; @@ -305,12 +305,13 @@ teco_state_queryqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg, return NULL; teco_expressions_push(c); } else { - /* Query integer */ - teco_int_t value; + /* Query number */ + teco_number_t value; + gboolean is_float; - if (!qreg->vtable->get_integer(qreg, &value, error)) + if (!qreg->vtable->get_number(qreg, &value, &is_float, error)) return NULL; - teco_expressions_push(value); + teco_expressions_push_num(value, is_float); } return &teco_state_start; @@ -407,7 +408,7 @@ teco_state_setqregstring_nobuilding_done(teco_machine_main_t *ctx, start = buffer = g_malloc(args*6); 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, "^U"); return NULL; @@ -416,12 +417,12 @@ teco_state_setqregstring_nobuilding_done(teco_machine_main_t *ctx, } /* 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); } else { buffer = g_malloc(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, "^U"); return NULL; @@ -607,9 +608,12 @@ teco_state_setqreginteger_got_register(teco_machine_main_t *ctx, teco_qreg_t *qr if (!teco_expressions_eval(FALSE, error)) return NULL; if (teco_expressions_args() || teco_num_sign < 0) { - teco_int_t v; - if (!teco_expressions_pop_num_calc(&v, teco_num_sign, error) || - !qreg->vtable->set_integer(qreg, v, error)) + teco_number_t v; + gboolean is_float; + + if (!teco_expressions_pop_num_calc(&v, &is_float, teco_num_sign, error)) + return NULL; + if (!qreg->vtable->set_number(qreg, v, is_float, error)) return NULL; if (teco_machine_main_eval_colon(ctx) > 0) @@ -654,13 +658,19 @@ teco_state_increaseqreg_got_register(teco_machine_main_t *ctx, teco_qreg_t *qreg if (ctx->flags.mode > TECO_MODE_NORMAL) return &teco_state_start; - teco_int_t value, add; + teco_number_t value, add; + gboolean value_is_float, add_is_float; - if (!qreg->vtable->get_integer(qreg, &value, error) || - !teco_expressions_pop_num_calc(&add, teco_num_sign, error) || - !qreg->vtable->set_integer(qreg, value += add, error)) + if (!qreg->vtable->get_number(qreg, &value, &value_is_float, error) || + !teco_expressions_pop_num_calc(&add, &add_is_float, teco_num_sign, error)) + return NULL; + if (value_is_float) + value.f += add_is_float ? add.f : add.i; + else + value.i += add_is_float ? (teco_int_t)add.f : add.i; + if (!qreg->vtable->set_number(qreg, value, value_is_float, error)) return NULL; - teco_expressions_push(value); + teco_expressions_push_num(value, value_is_float); return &teco_state_start; } |
