/baricc

self hosted c compiler

Primary LanguageC

This c_compiler is based on:

... and get help from these books as reference.

Directry structure

.
├── src
│   ├── baricc.h      # all func decl are placed here
│   ├── util.c        # where to put common functions
│   ├── main.c        # entry point
│   ├── tokenize.c    #
│   ├── parse.c       #
│   └── codegen.c     #
│
├── test
│   └── test.c        # place all test cases here
│ 
├── scripts
│   ├── test.sh       # build binary by using cc and run test
│   ├── self.sh       # build binary by itself
│   └── self_test.sh  # run test by using self compiled binary
│
├── Dockerfile
├── docker-compose.yml
├── Makefile
└── sandbox           # for exploring C. not used by src. (but used by test)

Start development environment

$ docker-compose up -d
$ docker-compose exec app bash

Run test

$ make test

Self Compile

  • baricc.h
  • tokenize.c
  • parse.c
  • codegen.c
  • util.c
  • main.c
make self > tmp.s
# use cc as linker.
cc -static -o baricc_self tmp.s

./baricc_self baricc.h tokenize.c parse.c codegen.c util.c

supported feature

  • +, -, *, /
  • ==, !=, <, >, <=, >=
  • !
  • ||, &&
  • a++, a--, ++a, --a
  • bit: ~, |, &, ^
  • for, while, if
  • int, char
  • void(implemented just as an alias to int..)
  • pointer: ref, deref
  • array
  • sizeof
  • enum
  • struct
  • break
  • continue
  • witch
  • escape in string
  • #include
    • you can write #include, but this compiler just ignores all macro statement.
  • nested type
  • typedef struct A A;
  • ternary operator

not supported features

  • static
  • support ++, -- operation for pointer
  • rest parameters
  • macro
  • stdout, stderr, FILE