Blaze calculator
I wanted to build a Spotlight alternative for Linux and decided to start
implementing the calculator.
I skipped all the GUI, as I wanted to get a working prototype as soon as
possible.
This turned out very useful even without the rest of Spotlight's features, so
here it is.
Absolutely experimental, use at your own risk.
Build / Install
To get started quickly, run the following command to install blaze
in your
$GOPATH/bin
:
go install github.com/francescobbo/blaze@latest
If you want to build from source:
git clone https://github.com/francescobbo/blaze.git
cd blaze
go generate ./...
go build
You can also run the tests:
go test ./...
Built with AI
This project started off with a heavy reliance on AI for code generation. In
just a few hours of work there was a decent working prototype with a
"handcrafted" tokenizer, parser and evaluator.
Since I wanted to add more complexity to the project, I decided to rewrite the
tokenizer and parser with ANTLR4. LLMs still helped
crafting the grammar, but failed miserably when it came to visiting the parse
tree.
Example
How to use
- Clone the repository
- Compile the code:
go build
- Run the code:
./blaze
- Type in your math expression, the program will print the result as you type.
Features
- Real-time evaluation of mathematical expressions
- Basic arithmetic operations:
+
, -
, *
, /
, ^
(or **
)
- Nested expressions with parentheses
- Percentages (e.g.,
10% * 200
, 10% of 200
)
- Modulo operation (e.g.,
10 % 3
, 10 mod 3
)
- Constants:
pi
, e
, phi
- Trigonometric functions:
sin
, cos
, tan
, asin
, acos
, atan
- Logarithmic functions:
log
, ln
- Square root:
sqrt
- Copy the result to clipboard by pressing
Enter
- Unit conversions:
- weight (
10 kg to oz
)
- length (
10 m to yd
)
- area (
10 sqm to sqft
)
- volume (
10 L to cup
)
- time (
1d to s
)
- temperature (
0c to f
)
- Currency conversion:
10 usd to eur
, 10 eur to usd
, 1 btc to usd
- All of the above can be combined in a single expression:
10 usd to eur * 5 + 10%
sqrt(10 kg in g)
cos(2pi) * 2 btc to usd
Note: currency rates are obtained from
ExchangeRate-API and
Coinbase API.
Currency rates are cached for 1 hour (1 minute for cryptocurrencies) to avoid
hitting the API too frequently. The cache is stored in an appropriate location
based on the OS (see appDataPath
)