bitwise-cli is a command-line interpreter for arithmetic and bitwise expressions. Written in Zig and blazing fast.
Note
This program now runs in non-canonical mode, allowing features such as input manipulation and input history! Like in
the shell, use the UP and DOWN arrow keys to cycle through input history, and LEFT and RIGHT arrow keys to seek
through the current buffer string.
-
Supports:
- Variable assignment (
foo = 20) - Certain builtin functions
sqrt(x)sin(x)cos(x)pow(x, y)exit()orexit(x)
- Parenthesis
- Arithmetic operators (*, /, +, -)
>and<expressions- Bitwise operators (&, |, <<, >>, ~, ^)
All numbers are read as floats (f64), and coerced to integers (i64) for operations that require it (mainly bit operations)
- Variable assignment (
Variable names are now fully supported. Use any alphabetical character or string as a variable name, as long as it's not reserved for a function name
Important
Variable names are NO LONGER initialized to 0 at runtime. Instead, the program will print an error if the user does not initialize the variable with a value.
Note
to exit the program, either use <CTRL-D> for EOF or use the builtin exit function, exit(return_value)
$> ./bitwise
>>> x = 6
6
>>> y = 10
10
>>> foo = sqrt(x + y)
4
>>> bar = pow(foo, 2)
16
>>> exit()Note
Zig version 0.15.0 or higher is required
git clone https://github.com/jpwol/bitwise-cli.git
cd bitwise-cli
zig build
cd bin
./bitwiseOptionally, specify one of Debug, ReleaseSafe, ReleaseFast, ReleaseSmall for -Doptimize:
zig build -Doptimize=ReleaseFastThe previous version of this program, which can be found under the legacy branch, made use of the shunting yard algorithm, and a stack-based approach to parsing and evaluating.
The weakness of this stack-based approach is the difficulty in parsing variables, as well as assigning the correct values to them based on expressions.
This new version uses an Abstract Syntax Tree and recursive descent parsing to easily and quickly parse the tokens of any given expression and make sure all variable and function names are evaluated correctly.