summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-06-07 18:35:32 -0400
committerJune McEnroe <june@causal.agency>2019-06-07 18:35:32 -0400
commita49de3f93e6ceb01d0324dfe3e6faed03db37a38 (patch)
treee82a8c2a7a2c5fdcaa9ea39e55672b0b7de064a3 /bin
parentAdd A Closed and Common Orbit (diff)
downloadsrc-a49de3f93e6ceb01d0324dfe3e6faed03db37a38.tar.gz
src-a49de3f93e6ceb01d0324dfe3e6faed03db37a38.zip
Add variables to bit
Diffstat (limited to '')
-rw-r--r--bin/bit.y32
-rw-r--r--bin/man1/bit.112
2 files changed, 28 insertions, 16 deletions
diff --git a/bin/bit.y b/bin/bit.y
index 637fe91c..2d97766e 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