• Stars
    star
    921
  • Rank 49,603 (Top 1.0 %)
  • Language
    Clojure
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Clojure implementation on top of C

ClojureC

This is compiler for the Clojure programming language that targets C as a backend. It is based on ClojureScript and was started off ClojureScript's commit 0e0aa7fdd379649bf87f8fff5c6a64e37fe616a4.

Status

This project is no longer actively maintained, though PRs and such are still welcome.

Background, Features, and Limitations

Forked off of a version of ClojureScript circa 2012 (prior to Clojure and ClojureScript being refactored to use the same compiler or front end), ClojureC provides:

  • Generation of C or Objective-C code, enabling faster startup than JVM-based executables

    Objective-C message sending uses the section character (ยง) in place of the at-sign (@), as the latter is already used by Clojure.

  • Ability to statically expose any C function to ClojureC (dynamic loading might be possible but is not currently implemented)

    See clojurec/binder/.

  • An implementation of the Boehm Garbage Collector (GC)

  • Incomplete support for native threads

    Exception handling currently uses two global variables (could be replaced with thread-local vars?).

    Extending protocols might also not be threadsafe.

  • No support for "modern" Clojure language features such as transducers

  • No eval

  • Limited optimization, really only as demanded by the language

    For example, loop/recur is turned into a real loop.

  • Exception support

    Implemented via setjmp/longjmp.

Alternatives to ClojureC

If native threads are not important, but fast startup is, consider using ClojureScript instead of ClojureC, as the startup time for the v8 engine is reportedly much faster than for the JVM.

Future Plans

As there seem to be few known use cases for programs that need to both start up quickly and use native threading, it is perhaps not surprising that there's little apparent interest in making the necessary changes to "productize" ClojureC. Such changes could include:

  • Full support for native threads

  • Supporting the latest version of the Clojure language

  • Supporting a C++ back end

  • Retargeting the GCC compiler back end

  • Implementing a proper optimizer

  • Providing improved debugger support

Other than (perhaps) full native-thread support, each of the above item would represent a substantial investment of resources that are not, at present, available.

Historical Info

Community and Organization

We use a Trello board to keep track of ideas, proposals, TODOs, bugs and who's doing what. If you plan to contribute, please do join the board.

Additional documentation for the project can be found at the wiki.

Preparations

Submodules

ClojureC includes a submodule. To fetch it, do

git submodule init
git submodule update

Dependencies

Before you can run anything make sure you have GLib 2 and the Boehm-Demers-Weiser garbage collector installed. If

pkg-config --cflags glib-2.0
pkg-config --cflags bdw-gc

don't report errors you should be good.

Make sure you're using Leiningen 2 - older versions of Leiningen won't work. Run the testsuite:

lein test

All tests should pass.

Using ClojureC

Note that ClojureC is still in its experimental phase, so please don't expect a polished experience, yet.

From the command line

ClojureC provides a very simple command line compiler interface. Let's say we want to compile samples/echo.cljc:

(ns cljc.user)
(defn -main [& args]
  (apply println args))

If you do the following in the clojurec directory

lein run -c src/cljc/cljc/core.cljc cljc.core run run
lein run -c samples/echo.cljc cljc.user run run
lein run -d cljc.user/-main run
cd run
make -f c/Makefile

you should have a cljc executable in the run directory that acts mostly like echo.

Objective-C bridge

ClojureC features a very rudimentary Objective-C bridge. Here's an example:

(ns cljc.user
  (:require [cljc.objc :as objc]))

(extend-type (ยง NSString)
  ICounted
  (-count [self]
    (ยง self length)))

(defn -main [& args]
  (let [app (ยง (ยง NSApplication) sharedApplication)
        date (ยง (ยง NSDate) :dateWithTimeIntervalSinceNow 3600)
        locale (ยง (ยง NSLocale) currentLocale)
        desc (ยง date :descriptionWithLocale locale)]
    (println "Hello, NSApplication: `" desc "` has count " (count desc))))

If you have that code in /tmp/nsdate.cljc, then this will build and run it:

lein run -c src/cljc/cljc/core.cljc cljc.core run run
lein run -c src/cljc/cljc/objc.cljc cljc.objc run run -m
lein run -c /tmp/nsdate.cljc cljc.user run run -m
lein run -d cljc.user/-main run -m
cd run
make

For a more involved example, see samples/ios.cljc.

iOS sample app

To build the iOS sample app, do

cd samples
./build-ios.sh

The last step will open an XCode project that will build and run the iOS app that build-ios.sh compiled from samples/ios.cljc.

From the REPL

The easiest way to play around with ClojureC interactively is in the namespace clojurec.core-test. For example:

(core-run '(pr (+ 1 2)))
=> [3]

Mobile Platform Notes

Android

See the README.md file in "clojurec/run/android".

More Repositories

1

forthlisp

A Small Lisp in Forth
Forth
150
star
2

metapixel

A Photomosaic Generator
C
111
star
3

mathmap

An image processing programming language and GUI
HTML
73
star
4

michael-alloc

Lock-free allocator as described by Maged M. Michael, 2004
C
41
star
5

linbin

Linear vs binary search benchmark
C
40
star
6

jscreme

A self-hosting Scheme to JavaScript compiler
Scheme
32
star
7

flickr-clojure

Flickr API bindings for Clojure
Clojure
19
star
8

unlambdascheme

A Scheme to Unlambda compiler
Scheme
15
star
9

SimpLang

A very simple programming language for teaching interpreter and compiler building.
C
14
star
10

rwimg

Simple library with simple API to read and write JPEG, PNG and GIF images.
C
8
star
11

clickr

A Flickr client library for Common Lisp
Common Lisp
8
star
12

git-polish-history

A tool for interactively rewriting history to fix the build or test suite failures.
Go
7
star
13

bintrans

A dynamic binary translator - unfinished work
C
6
star
14

clj-simple-dist

Simple Clojure Job Distribution Service
Clojure
5
star
15

lispreader

A small library for reading expressions in Lisp syntax
C
4
star
16

ocamlscheme

A Scheme Interpreter in OCaml
OCaml
4
star
17

chocolate

The Chocolate Problem in Clojure
Clojure
3
star
18

chpp

The Chakotay Preprocessor
C
3
star
19

ImmutableCollections

Free C# immutable collections implementation
C#
3
star
20

PimpMyGlow

An extension of Aerotech's glow club language
Go
3
star
21

iPauker

An iOS companion app for the Pauker flash learning program.
Objective-C
3
star
22

clj-siteswap-sudoku

A siteswap puzzle generator
Clojure
3
star
23

animatedgif

Simple code to generate animated GIFs
C
3
star
24

icfp-2010

ICFP Contest 2010 Code of Team Funktion im Kopf der Mensch
JavaScript
2
star
25

flickrwave

A Flickr Gadget for Google Wave
2
star
26

lisp-utils

A few Common Lisp helpers
Common Lisp
2
star
27

Robots

A simple robot coding/fighting game for MacOS X
Objective-C
2
star
28

icfp-2018

Funktion im Kopf der Mensch 2018
C
2
star
29

simple-sgen-client

A minimal client for SGen
C
2
star
30

siteswap-sudoku

A siteswap sudoku puzzle generator
Prolog
2
star
31

icfp-2011

ICFP Contest 2011 Code of Team Funktion im Kopf der Mensch
Shell
2
star
32

favjumpr

A little Flickr game in the form of a Google Wave Gadget
1
star
33

sparse

A semantic parser of source files
C
1
star
34

undefinder

Finds unused C #defines.
Go
1
star
35

bwproc

A black and white photo processing core in C
C
1
star
36

icfp-2007

ICFP Contest 2007 Code of Team Funktion im Kopf der Mensch
OCaml
1
star
37

MPTaskKit

A small framework for making asynchronous programming easier
Objective-C
1
star
38

locktest

Comparing different lock implementations
C
1
star
39

icfp-2008

ICFP Contest 2008 Code of Team Funktion im Kopf der Mensch
OCaml
1
star
40

goscheme

WatchPeopleCode: A small subset of Scheme in Go
Go
1
star
41

12coins

A solver for the 12 coins problem
C
1
star
42

reloadcache

A simple reloading HTTP cache
Go
1
star
43

CustomReactRenderer

JavaScript
1
star
44

icfp-2002

ICFP Contest 2001 Code of Team Funktion im Kopf der Mensch
OCaml
1
star
45

icfp-2009

ICFP Contest 2009 Code of Team Funktion im Kopf der Mensch
OCaml
1
star
46

bwprocessor

Crude prototype of a black and white photo processing application
C#
1
star
47

icfp-2004

ICFP Contest 2004 Code of Team Funktion im Kopf der Mensch
C
1
star
48

pico8

Just playing around with Pico-8
Lua
1
star
49

quickjs-fetch

TypeScript
1
star
50

icfp-2005

ICFP Contest 2005 Code of Team Funktion im Kopf der Mensch
C
1
star
51

ocamlworkshop

OCaml
1
star
52

dotfiles

Setup for Codespaces
Shell
1
star