davidelettieri / Kaleidoscope

a c# version of the kaleidoscope tutorial

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kaleidoscope

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

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

About

a c# version of the kaleidoscope tutorial

License:MIT License


Languages

Language:C# 100.0%