This year I've been learning Vulkan and since I'm a masochist, I'm doing it while building a game engine. About a year ago now, I participated in a jam. My team ended up building a Minecraft clone and placed pretty well. If I participated again this year, I wanted to have a good entry. I knew I would want to do something with Vulkan and I've written about 6 or so parsers in the last year or so. I landed on this calculator inside a compute shader idea. I didn't end up participating in the event this year. Mostly because I wasn't convinced that I could complete this project within the allotted 24 hours.
One of the ways to write a calculator is very similar to how you would write a compiler/interpreter. The process involves scanning, parsing, and evaluation. This is exactly what the compute shader does.
vkcalc can be summarized as
I/O for the compute shader is an SSBO. I shove the program input into this buffer (some expression, i.e., "1 + 1"). Output is written to a separate part of this same buffer at some offset. One of the things that I would like to fix is setting up a second SSBO for output to be written. Memory for tokens produced from the scan phase and ASTNodes from the parsing phase are all stack allocated. (Not great these should be SSBOs as well.)
All of the parsers that I've done have been with a technique called recursive descent. A small challenge with using a shader is that they don't allow recursion. I ended up using a stack structure to parse without recursion. I also used a stack to do a post-order traversal on the result AST from the parse phase. In order to do an evaluation.
There are several things I need to come back and fix, namely:
This was a fun detour from working on my game engine. I was able to reuse a sizable amount of its Vulkan code. vkcalc source code is HERE
Resources: