[hackers] [sbase] bc: Implement the . operand || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Tue, 25 Nov 2025 11:26:22 +0100 (CET)

commit c759756d2e47cf2737949d0007937e467a4825eb
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
AuthorDate: Tue Nov 25 11:21:47 2025 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.net>
CommitDate: Tue Nov 25 11:21:47 2025 +0100

    bc: Implement the . operand
    
    While it is not specified in POSIX, all the bc implementations since
    70's store the last expression in the dot variable which can be used
    in later experssions.

diff --git a/bc.y b/bc.y
index 5da6792..f268e6c 100644
--- a/bc.y
+++ b/bc.y
_AT_@ -70,6 +70,7 @@ int cflag, dflag, lflag, sflag;
 %token <str> STRING NUMBER
 %token <str> EQOP '+' '-' '*' '/' '%' '^' INCDEC
 %token HOME LOOP
+%token DOT
 %token EQ
 %token LE
 %token GE
_AT_@ -204,6 +205,7 @@ expr : nexpr
 
 nexpr : NUMBER {$$ = code(" %s", $1);}
         | ID {$$ = code("l%s", var($1));}
+ | DOT {$$ = code("l.");}
         | SCALE {$$ = code("K");}
         | IBASE {$$ = code("I");}
         | OBASE {$$ = code("O");}
_AT_@ -567,6 +569,8 @@ follow(int next, int yes, int no)
 static int
 operand(int ch)
 {
+ int peekc;
+
         switch (ch) {
         case '\n':
         case '{':
_AT_@ -578,6 +582,12 @@ operand(int ch)
         case ',':
         case ';':
                 return ch;
+ case '.':
+ peekc = ungetc(getchar(), stdin);
+ if (strchr(DIGITS, peekc))
+ return number(ch);
+ yylval.str = ".";
+ return DOT;
         case '"':
                 return string(ch);
         case '*':
_AT_@ -643,7 +653,7 @@ repeat:
                 yyerror("invalid input character");
         } else if (islower(ch)) {
                 return iden(ch);
- } else if (ch == '.' || strchr(DIGITS, ch)) {
+ } else if (strchr(DIGITS, ch)) {
                 return number(ch);
         } else {
                 if (ch == '/') {
Received on Tue Nov 25 2025 - 11:26:22 CET

This archive was generated by hypermail 2.3.0 : Tue Nov 25 2025 - 11:36:37 CET