Skip to content

Conversation

@manthanabc
Copy link
Contributor

@manthanabc manthanabc commented Mar 23, 2025

Adding a simple calculator

  • Introduces a basic calculator page for ilia
  • Added tests for parser
  • Implemented a basic parser, tokenizer

Preview

screenshot
screenshot

Limitations

Currently it does not follow PEDMAS rule and strictly solves from left to write. Kept it this way to avoid complexity. could use something like libmatheval-dev in future to improve functionality.
Parenthesis do not work yet, expect for function calls like sin(exp), cos(exp)

Bindings

Since C was already taken by Commands page, and K was taken by Keybindings page, I decided to go with o as a shortcut key for calculator although i am not too happy with this and could just remove it altogether.

return x < 0 ? -x : x;
}

public void test_parse () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

result_frame.add(result_box);

// Button grid
var grid = new Gtk.Grid();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request: Can the calculator buttons fill to consume available space in the parent container?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lia
updated preview

}

public char get_keybinding() {
return 'o';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: why not use a?

Calculator
 ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this seems much cleaner than using o

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A is used by the application page (default one), maybe i could use e for expression? or l ?

Calculator
  ^

or we could just move to a keychaining based approach where shortcuts can be two letter long with some threshold value between two consecutive keypresses to distiguish between like C and Ca to remove ambiguity

namespace Ilia {
public class ExpressionParser : Object {

public static List<string> tokenize(string expr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concern: this looks like a good start of a general purpose math evaluator but perhaps there is something available that has some maturity.. Perhaps it's possible to call into GCalc.Parser as a library? https://source.puri.sm/sebastian.krzyszkowiak/gnome-calculator/-/blob/pureos/byzantium/gcalc/gcalc-parser.vala#L59

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to find a library but they all seem too much for what i wanted to add, but thinking in retrospect we can always make use of more general calculator.
We should be able to use the libgcalc directly, i'll try to get that working

@manthanabc
Copy link
Contributor Author

Moving forwrads i was thinking of

  1. Statically linking glibcalc, copying over the galc folder to src/calculator
  2. Merging gcalc related tests in gnome-calculator to src/test folder
  3. Remove the parser file, and making calculator.vala make use of gcalc

Outcomes

Being able to evaluate all types of expresssions without any restrictions
sin(80)^2 + cos(80)^2
Result (1)

Being able to evavluate linear equations of form
8x + 4 = 12
Result (x=1)

Evaluating simple conversions of form
1 week in days
Result (7 days)

1 GB in MB
Result (1024 MB)

1 meter in mm
Result ( 1000 )
and more..

In future we should also be able to include currency providers to do
conversions like
1EUR in INR
Result (85 INR)

full list of features that should work https://github.com/GNOME/gnome-calculator/blob/main/tests/test-equation.vala
Ig we could also detect if input is some type of expression and show result without having to go
to the calculator page?

would like to confirm before making this changes.

This can also be easily archived by directly calling gnome-calculator -s {expression} and taking the
result from stdout, but i think integrating gcalc would be less hacky.

@kgilmer
Copy link
Member

kgilmer commented Apr 7, 2025

Moving forwrads i was thinking of

1. Statically linking glibcalc, copying over the galc folder to src/calculator

is it just this: https://github.com/GNOME/gnome-calculator/tree/main/gcalc? Anything else required (transitive dependencies, etc.) for it to work?

2. Merging gcalc related tests in gnome-calculator to src/test folder

Tests are good, but if we leave the upstream source unchanged we would not expect to introduce a regression ourselves I'm thinking. Do you expect source changes would be needed in gcalc?

This can also be easily archived by directly calling gnome-calculator -s {expression} and taking the result from stdout, but i think integrating gcalc would be less hacky.

A direct integration is cleaner, agreed. Are there other advantages to this approach? Maybe we don't need to bundle upstream source but still get the features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants