From 902940401d1c1e657cdaf3e7bae8cbb89d14ffa9 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 15 Nov 2026 08:57:03 +0100 Subject: added left shift (^<) and right shift (^>) operators Previously you'd have to write `a * 2^*b` for left shift, which is inefficient and cumbersome. --- TODO | 3 --- doc/sciteco.7.template | 15 ++++++++++++--- src/core-commands.c | 2 ++ src/expressions.c | 10 ++++++++++ src/expressions.h | 16 +++++++++------- src/lexer.c | 2 +- src/number-commands.c | 12 ++++++++++++ src/number-commands.h | 2 ++ tests/testsuite.at | 5 +++++ 9 files changed, 53 insertions(+), 14 deletions(-) diff --git a/TODO b/TODO index 9318dc6..42c01a3 100644 --- a/TODO +++ b/TODO @@ -570,9 +570,6 @@ Features: https://github.com/fpjohnston/TECO-64/blob/master/doc/oper.md However, instead of introducing a separate parser state, better use operators like ~=, ~< etc. - * ^& could be a NAND operator. - * ^% as a synonym to ^/ - * ^< left shift, ^> right shift * Perhaps all operations on integers should be checked for overflows using the appropriate GCC intrinsics. There is probably little benefit in having unchecked operations. diff --git a/doc/sciteco.7.template b/doc/sciteco.7.template index b6b0795..7a10f09 100644 --- a/doc/sciteco.7.template +++ b/doc/sciteco.7.template @@ -1129,12 +1129,21 @@ T};\^ Add \fIb\fP to \fIa\fP. T};\^ = -\fIa\fB & \fIb\fP;T{ -.SCITECO_TOPIC & and ampersand -Binary AND. +\fIa\fB ^< \fIb\fB;T{ +.SCITECO_TOPIC ^< "left shift" +Left shift \fIa\fP by \fIb\fP bits. T};T{ integers T} +\fIa\fB ^> \fIb\fB;T{ +.SCITECO_TOPIC ^> "right shift" +Right shift \fIa\fP by \fIb\fP bits. +T};\^ += +\fIa\fB & \fIb\fP;T{ +.SCITECO_TOPIC & and ampersand +Binary AND. +T};\^ = \fIa\fB ^# \fIb\fP;T{ .SCITECO_TOPIC ^# xor diff --git a/src/core-commands.c b/src/core-commands.c index b7e7a60..f7e6f01 100644 --- a/src/core-commands.c +++ b/src/core-commands.c @@ -1453,6 +1453,8 @@ teco_state_control_input(teco_machine_main_t *ctx, gunichar chr, GError **error) ['*'] = {&teco_state_start, teco_state_control_pow}, ['/'] = {&teco_state_start, teco_state_control_mod}, ['#'] = {&teco_state_start, teco_state_control_xor}, + ['<'] = {&teco_state_start, teco_state_control_left_shift}, + ['>'] = {&teco_state_start, teco_state_control_right_shift}, /* * Commands diff --git a/src/expressions.c b/src/expressions.c index e4dc5ad..84d76cb 100644 --- a/src/expressions.c +++ b/src/expressions.c @@ -317,6 +317,16 @@ teco_expressions_calc(GError **error) teco_expressions_push(vleft.i - vright.i); break; + case TECO_OP_SHL: + teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) << + (vright_is_float ? (teco_int_t)vright.f : vright.i)); + break; + + case TECO_OP_SHR: + teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) >> + (vright_is_float ? (teco_int_t)vright.f : vright.i)); + break; + case TECO_OP_AND: teco_expressions_push((vleft_is_float ? (teco_int_t)vleft.f : vleft.i) & (vright_is_float ? (teco_int_t)vright.f : vright.i)); diff --git a/src/expressions.h b/src/expressions.h index 1c1b4e9..5d71cd6 100644 --- a/src/expressions.h +++ b/src/expressions.h @@ -84,15 +84,17 @@ typedef enum { /* * Real operators */ - TECO_OP_POW = 0x60, // ^* - TECO_OP_MOD = 0x50, // ^/ + TECO_OP_OR = 0x10, // # + TECO_OP_XOR = 0x20, // ^# + TECO_OP_AND = 0x30, // & + TECO_OP_SHL = 0x40, // ^< + TECO_OP_SHR, // ^> + TECO_OP_SUB = 0x50, // - + TECO_OP_ADD, // + + TECO_OP_MOD = 0x60, // ^/ TECO_OP_DIV, // / TECO_OP_MUL, // * - TECO_OP_SUB = 0x40, // - - TECO_OP_ADD, // + - TECO_OP_AND = 0x30, // & - TECO_OP_XOR = 0x20, // ^# - TECO_OP_OR = 0x10 // # + TECO_OP_POW = 0x70 // ^* } teco_operator_t; extern gint teco_num_sign; diff --git a/src/lexer.c b/src/lexer.c index 2eae8c4..1eaf6ff 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -46,7 +46,7 @@ teco_lexer_getstyle(teco_view_t *view, teco_machine_main_t *machine, strchr(",+-*/#&()", chr)) old_style = SCE_SCITECO_OPERATOR; else if (machine->parent.current == &teco_state_control && - strchr("*/#", chr)) + strchr("<>*/#", chr)) /* * Two-character operators must always begin with caret * They get a separate style, so we can extend it back to diff --git a/src/number-commands.c b/src/number-commands.c index 6ba2874..0656cf0 100644 --- a/src/number-commands.c +++ b/src/number-commands.c @@ -361,6 +361,18 @@ teco_state_start_comma(teco_machine_main_t *ctx, GError **error) teco_expressions_push_op(TECO_OP_NEW); } +void +teco_state_control_left_shift(teco_machine_main_t *ctx, GError **error) +{ + teco_expressions_push_calc(TECO_OP_SHL, error); +} + +void +teco_state_control_right_shift(teco_machine_main_t *ctx, GError **error) +{ + teco_expressions_push_calc(TECO_OP_SHR, error); +} + /*$ ^_ negate * n^_ -> ~n -- Binary negation * diff --git a/src/number-commands.h b/src/number-commands.h index 79fa77b..98818f7 100644 --- a/src/number-commands.h +++ b/src/number-commands.h @@ -35,6 +35,8 @@ void teco_state_start_brace_open(teco_machine_main_t *ctx, GError **error); void teco_state_start_brace_close(teco_machine_main_t *ctx, GError **error); void teco_state_start_comma(teco_machine_main_t *ctx, GError **error); +void teco_state_control_left_shift(teco_machine_main_t *ctx, GError **error); +void teco_state_control_right_shift(teco_machine_main_t *ctx, GError **error); void teco_state_control_negate(teco_machine_main_t *ctx, GError **error); void teco_state_control_pow(teco_machine_main_t *ctx, GError **error); void teco_state_control_mod(teco_machine_main_t *ctx, GError **error); diff --git a/tests/testsuite.at b/tests/testsuite.at index e86088e..8a81bab 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -105,6 +105,11 @@ TE_CHECK([[2^*-5 - 0"N(0/0)']], 0, ignore, ignore) TE_CHECK([[2.^*-5:=]], 0, [0.03125], ignore) AT_CLEANUP +AT_SETUP([Bit shifting]) +TE_CHECK([[(@0ff^<8) - @0ff00"N(0/0)]], 0, ignore, ignore) +TE_CHECK([[(@0ffaa^>8) - @0ff"N(0/0)]], 0, ignore, ignore) +AT_CLEANUP + AT_SETUP([Missing left operand]) TE_CHECK([[+23=]], 1, ignore, ignore) AT_CLEANUP -- cgit v1.2.3