Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/davidelettieri/kaleidoscope

a c# version of the kaleidoscope tutorial
https://github.com/davidelettieri/kaleidoscope

csharp kaleidoscope llvm

Last synced: about 10 hours ago
JSON representation

a c# version of the kaleidoscope tutorial

Awesome Lists containing this project

README

        

# Kaleidoscope

The Kaleidoscope tutorial implemented with C# and [LLVMSharp](https://github.com/microsoft/LLVMSharp).

My implementation does not follow the official Kaleidoscope tutorial written in C++, other C# implementations can be found at [LLVMSharp](https://github.com/microsoft/LLVMSharp) and in [here](https://ice1000.org/llvm-cs/en/).

Some useful resources are:
* [Stephen Diehl haskell implementation](https://www.stephendiehl.com/llvm/)
* [Crafting interpreters](https://www.craftinginterpreters.com/)

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 :)

## Some differences with the official Kaleidoscope tutorial

* Functions definitions requires a comma ',' between arguments.
* I defined a `putchard` function which uses `Console.Writeline`.
* I used the visitor pattern in the interpreter/emitter
* I defined more AST types

## Chapters

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.

### Notes

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.

## Run the mandelbrot set implementation

Look for the mandelbrot file included in the sources and from the folder Kaleidoscope.Chapter6 or Kaleidoscope.Chapter7 run `dotnet run mandelbrot`