saintech / nanocc

a Nano C Compiller & VM, Go lang port of "c4 - C in four functions", wich can compile its C version

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nanocc

Build Status Coverage Status Go Report Card Lines of Go Code

A Nano C Compiller & VM

nanocc is port of very primitive "C to custom bytecode" compiller and runner - rswier/c4. This port is mostly a line-by-line translation from c to go, and is in parity with commit 3f098f3.

Motivation

This project was created to examine the work of the most popular of the simplest C compilers - c4, "C in four functions". The goal is to be as close to the original and as easy as possible to understand.

Features

The compiler does not support the entire C syntax, only a small part of it. As it is written in the original:

// char, int, and pointer types
// if, while, return, and expression statements
// just enough features to allow self-compilation and a bit more

So, the compiler is able to compile its C version and then itself.

Differences from c4

  • The most important difference - nanocc uses one big byte array as VM memory instead of shared with unsafe pointers in c4.
  • Many comments have been added (many of which are translated from comzyh/c4).
  • Tests. Many C tests sources are taken from EarlGray/c4 (under GPL-2.0).

Installing / Getting started

Via Go

# Download/Update/Instal
$ go get -u github.com/saintech/nanocc
$ cd $(go env GOPATH)/src/github.com/saintech/nanocc/testdata

# Usage
$ nanocc
usage: nanocc [-s] [-d] file ...

# Compile and run hello.c
$ nanocc hello.c
hello, world
exit(0) cycle = 9

# Compile and run its C version which compile and run hello.c
$ nanocc c4.c hello.c
hello, world
exit(0) cycle = 9
exit(0) cycle = 25983

# Compile and run its C version which compile and run self
# which compile and run hello.c
$ nanocc c4.c c4.c hello.c
hello, world
exit(0) cycle = 9
exit(0) cycle = 25983
exit(0) cycle = 10154153

# We need to go deeper...

We need to go deeper

Contribution

The door is always open 😊

Thanks

License

It is in the public domain under the WTFPL license.

About

a Nano C Compiller & VM, Go lang port of "c4 - C in four functions", wich can compile its C version

License:Do What The F*ck You Want To Public License


Languages

Language:Go 61.0%Language:C 39.0%