From 71c8857f7ae214fb44e06cea925d1ec98aeaf2ff Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 7 Jun 2019 18:35:32 -0400 Subject: Add variables to bit --- bin/bit.y | 32 +++++++++++++++++++++----------- bin/man1/bit.1 | 12 +++++++----- 2 files changed, 28 insertions(+), 16 deletions(-) (limited to 'bin') diff --git a/bin/bit.y b/bin/bit.y index b9e39c32..99199a3d 100644 --- a/bin/bit.y +++ b/bin/bit.y @@ -27,18 +27,16 @@ #define MASK(b) ((1ULL << (b)) - 1) -#define YYSTYPE uint64_t - -static void yyerror(const char *str) { - warnx("%s", str); -} - +static void yyerror(const char *str); static int yylex(void); -static uint64_t result; +#define YYSTYPE uint64_t + +static uint64_t vars[128]; %} +%right '=' %left '|' %left '^' %left '&' @@ -48,17 +46,17 @@ static uint64_t result; %right '~' %left 'K' 'M' 'G' 'T' -%token Int +%token Int Var %% -start: - expr { result = $1; } +stmt: + expr { vars['_'] = $1; } ; expr: Int - | '_' { $$ = result; } + | Var { $$ = vars[$1]; } | '(' expr ')' { $$ = $2; } | expr 'K' { $$ = $1 << 10; } | expr 'M' { $$ = $1 << 20; } @@ -77,10 +75,15 @@ expr: | expr '&' expr { $$ = $1 & $3; } | expr '^' expr { $$ = $1 ^ $3; } | expr '|' expr { $$ = $1 | $3; } + | Var '=' expr { $$ = vars[$1] = $3; } ; %% +static void yyerror(const char *str) { + warnx("%s", str); +} + #define T(a, b) ((int)(a) << 8 | (int)(b)) static const char *input; @@ -130,6 +133,11 @@ static int yylex(void) { } else if (isdigit(input[0])) { return lexInt(10); } + + if (input[0] == '_' || islower(input[0])) { + yylval = *input++; + return Var; + } switch (T(input[0], input[1])) { case T('<', '<'): input += 2; return Shl; @@ -166,6 +174,8 @@ int main(void) { int error = yyparse(); if (error) continue; + uint64_t result = vars['_']; + int bits = result > UINT32_MAX ? 64 : result > UINT16_MAX ? 32 : result > UINT8_MAX ? 16 diff --git a/bin/man1/bit.1 b/bin/man1/bit.1 index ddb26535..b61bc704 100644 --- a/bin/man1/bit.1 +++ b/bin/man1/bit.1 @@ -1,4 +1,4 @@ -.Dd May 12, 2019 +.Dd June 7, 2019 .Dt BIT 1 .Os . @@ -12,8 +12,8 @@ .Sh DESCRIPTION .Nm is an integer calculator. -Its syntax resembles that of C, -with the following additions: +Its syntax resembles that of C expressions, +with the following changes: . .Bl -bullet .It @@ -34,9 +34,11 @@ The postfix operators .Sy T are used as constant multipliers. .It -The symbol +Single-letter (lower case) variables +can be assigned. +The variable .Sy _ -is used to refer to the previous result. +stores the previous result. .El . .Sh SEE ALSO -- cgit 1.4.1 3e8293063c830&follow=1'>Use mandoc -T html for about-filterJune McEnroe 2020-01-05Add The Book of FloraJune McEnroe 2020-01-04Replace gr alias with git resetJune McEnroe 2020-01-03Remove shotty -c flag from upJune McEnroe 2020-01-03Add Darling Don't CryJune McEnroe 2020-01-01Update license header templates for the new yearJune McEnroe 2019-12-26Add \S to sort inside bracesJune McEnroe 2019-12-23Reformat music.txtJune McEnroe 2019-12-23Rename music.md to music.txtJune McEnroe 2019-12-23Add DO YOU DOUBT ME TRAITORJune McEnroe 2019-12-22Add license header to cgit CSSJune McEnroe 2019-12-22Add The Book of EttaJune McEnroe 2019-12-21Revert "Add first working version of imbox"June McEnroe 2019-12-21Add first working version of imboxJune McEnroe 2019-12-20Respect mailmap in gl pretty formatJune McEnroe 2019-12-20Set LANG in cgit filtersJune McEnroe 2019-12-20Source .editrc before applying -v or -eJune McEnroe 2019-12-20Disable signing commitsJune McEnroe 2019-12-19Ignore about-filterJune McEnroe 2019-12-19Fix matching make tags with no sourcesJune McEnroe 2019-12-19Avoid matching := assignments as tagsJune McEnroe 2019-12-18Hide line numbers when rendering mdocJune McEnroe 2019-12-18Customize cgit CSSJune McEnroe 2019-12-18Use :target rather than :focus pseudo-classJune McEnroe 2019-12-18Copy cgit auxiliary binaries properlyJune McEnroe 2019-12-18Add git.causal.agency cgit configJune McEnroe 2019-12-18Bail from hi if input is binaryJune McEnroe 2019-12-16Post "cgit setup"June McEnroe -0500'>2021-02-07Use scrot for up -s if no screencaptureJune McEnroe Still missing putting the URL in an X selection. 2021-02-07Enable mouse acceleration in XJune McEnroe 2021-02-07Set colours for Xt and cwmJune McEnroe And increase XTerm internalBorder. 2021-02-07Set urgency on bell in xtermJune McEnroe 2021-02-07Add bindings for brightness controlJune McEnroe Weirdly the Fn key doesn't change how the F row registers... I wonder if I can do something about that. 2021-02-07Set X key repeat rateJune McEnroe 2021-02-07Bump font size to 12June McEnroe 11 is what I use on macOS, but I feel like my eyes are working harder here. 2021-02-07Fully configure and rebind cwmJune McEnroe This is sort of a mix of trying to emulate macOS somewhat for my muscle memory and just rebinding some of the cwm defaults to use 4- rather than M-. 2021-02-07Add BintiJune McEnroe 2021-02-07Finish configuring xtermJune McEnroe 2021-02-06Enable tapping, reverse scroll, set scaling in wsconsctlJune McEnroe 2021-02-06Set root window to black on purple snowJune McEnroe 2021-02-06Add xmodmap configurationJune McEnroe 2021-02-06Add initial OpenBSD X configurationJune McEnroe cwm still needs a lot more rebinding, and I need to actually look at its other options. xterm definitely still needs some configuration, but I at least managed to get it to use a decent looking font. Very happy that OpenBSD includes Luxi Mono, which is what my usual font, Go Mono, is based on anyway. Still missing is xmodmap and such. 2021-02-06Add xterm output to schemeJune McEnroe