The Kaleidoscope tutorial implemented with C# and LLVMSharp.
My implementation does not follow the official Kaleidoscope tutorial written in C++, other C# implementations can be found at LLVMSharp and in here.
Some useful resources are:
The aim of the project was to try to use LLVMSharp and to have something running wih LLVM. The behavior of the demo is not the same as the official tutorial (which I did not run) nor as the C# examples in LLVMSharp, which I was not able to make it run correctly.
As of today the project should support almost everything up to Chapter 7 of the official Kaleidscope tutorial. Feel free to drop me a line or to open an issue. Be kind it's a work in progress :)
- Functions definitions requires a comma ',' between arguments.
- I defined a
putchard
function which usesConsole.Writeline
. - I used the visitor pattern in the interpreter/emitter
- I defined more AST types
I split the implementation of Chapter 6 and Chapter 7. Chapter 7 introduces mutable variables and remove the need for the phi
nodes in the for
expression so I kept Chapter 6 as a reference. Both chapters can run the mandelbrot example, Chapter 7 has an additional demo
file to run the iterative fibonacci e to compute the sum of the first 10 natural numbers.
If you're playing with LLVM like I did, I found very useful to look at the IR generated by a simple C
program. Use the command clang -S -emit-llvm foo.c
on your test file to view the emitted IR, for example you can see the emitted code for a simple for loop. Another useful trick is to look at your generated IR with the optimization passes and without the optimization passes.
Look for the mandelbrot file included in the sources and from the folder Kaleidoscope.Chapter6 or Kaleidoscope.Chapter7 run dotnet run mandelbrot