3

I am trying to learn a bit about Vala and wanted to create a Calculator to test how Gtk worked. The problem is that I coded everything around the supposition that there would be a way to parse a string that contained the required operations. Something like this:

string operation = "5+2/3*4"

I have done this with Python and it is as simple as using the compilers parser. I understand Python is math oriented, but I thought that perhaps there would be Vala library waiting for me as an answer... I haven't found it if it does exist, but as I was looking at the string documentation, I noticed this part:

/* Strings prefixed with '@' are string templates. They can evaluate
* embedded variables and expressions prefixed with '$'.
* Since Vala 0.7.8.
*/
string name = "Dave";
println (@"Good morning, $name!");
println (@"4 + 3 = $(4 + 3)");

So... I thought that maybe there was a way to make it work that way, maybe something like this:

stdout.printf(@"$(operation)")

I understand that this is not an accurate supposition as it will just substitute the variable and require a further step to actually evaluate it.

Right now the two main doubts I am having are: a) Is there a library function capable of doing this? and b) Is it possible to work out a solution using string templates?

  • The string templates approach will not do any good here. You need a full expression parser which Vala does not easily provide. – Jens Mühlenhoff Mar 23 '16 at 12:36
  • [GNOME Calculator](https://git.gnome.org/browse/gnome-calculator/tree/src) is written with Vala, if that is a useful reference. – AlThomas Mar 23 '16 at 18:23

2 Answers2

2

Here's something I found that would do the work. I used the C++ libmatheval library, for this I first required a vapi file to bind it to Vala. Which I found here. There are a lot of available vapi files under the project named vala-extra-apis, and they are recognized in GNOME's Vala List of Bindings although they are not included at install.

0

You could parse the expression using libvala (which is part of the compiler).

The compiler creates a CodeContext and runs the Vala parser over a (or several) .vala file(s).

You could then create your own CodeVisitor decendant class that visits the necessary nodes of the parse tree and evaluates expressions.

As far as I can see there is no expression evaluator that does this, yet. That is because normally vala code is translated to C code and the C compiler then does compile time expression evaluation or the finished executable does the run time evaluation.

Python is different, because it is primarily a scripting language and has evaluation build directly into the runtime / interpreter.

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
  • I found a way by binding the C++ matheval library via a vapi file, but could you explain a bit further on the CodeContext and the CodeVisitor class? – Alvaro Bravo Mar 23 '16 at 18:10
  • What do you want to know? Note that stackoverflow is not a discussion forum. If you have a further specific question, you should just go ahead and ask a new question. – Jens Mühlenhoff Mar 23 '16 at 23:34
  • If you are interested in how the compiler works, I would suggest you to study the vala source code: https://git.gnome.org/browse/vala/tree/ There is also some info at the Hacking Vala page: https://wiki.gnome.org/Projects/Vala/Hacking – Jens Mühlenhoff Mar 23 '16 at 23:35