cljcc

C compiler implemented in Clojure.

C compiler implemented in Clojure. Based on Writing a C Compiler by Nora Sandler

Clojure codebase is compiled to WASM using GraalVM native image.

Post on how I approached implementing the book in Clojure. Writing a C Compiler in Clojure

Post on how to compile the clojure codebase to WASM. Compiling Clojure to Web Assembly

I have only implemented the first 12 chapters of the book. Refer the supported syntax section below.

Github Source: kaepr/cljcc

Compilation Stage:

Supported Syntax

Basic Features

// Types
int, long

// Variables
int x;                  // Local variables
static long count = 0;   // Static variables
extern long count2 = 0;  // Extern variables

// Control Flow
if (x > 0) { ... } else { ... }
for (int i = 0; i < n; i++) { ... }
while (condition) { ... }
do { ... } while (condition);

// Functions
int main(void);
int add(int a, int b) { return a + b; }

// Operators
+, -, *, /, %, =, ==, !=, >, <, >=, <=, &&, ||, !

Example Code

int main(void) {
  return 42;
}
int main(void) {
  static int x = 1;
  if (x == 42)
    return x;
  x = x + 1;
  return main();
}
static int x = 20;

int main(void) {
  int y = 22;
  return x + y;
}