bitwise-cli is a command-line interpreter for arithmetic and bitwise expressions.
-
Written in Zig (blazing fast)
-
Supports:
- Variable assignment
- Certain builtin functions
- sqrt()
- sin()
- cos()
- exit()
- 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 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
All variables are initialized to 0 at runtime.
Note
to exit the program, either use <CTRL-D>
for EOF or use the builtin exit function, exit(return_value)
$> ./bitwise
>>> x
0
>>> x = 5
5
>>> y = 10
10
>>> foo = x + y
15
>>> foo
15
>>> exit(0)
Note
Zig version 0.14.0 or higher is required
git clone https://github.com/jpwol/bitwise-cli.git
cd bitwise-cli
zig build
cd bin
./bitwise
Optionally, specify one of Debug, ReleaseSafe, ReleaseFast, ReleaseSmall
for -Doptimize
:
zig build -Doptimize=ReleaseFast
The 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.