Skip to content
/ cake Public
forked from thradams/cake

Cake a C23 front end and transpiler written in C

License

Notifications You must be signed in to change notification settings

mingodad/cake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🍰 Cake

Cake is a compiler front end written from scratch in C, specifically designed for C23. It allows you to translate newer versions of C, such as C23, to C99. Additionally, Cake provides a platform for experimenting with new features for the C language, including extensions like lambdas and defer.

Web Playground

This is the best way to try.

http://thradams.com/cake/playground.html

Use cases

If you have a project that is distributed with code, you don't need to limit the project development at the lower supported language version. For instance, you can use attributes like nodiscard during the development or defer, both features improving the code security. Then adding a extra step in your build you can distribute a readable C99 source code that compiles everywhere.

Features

  • C23 preprocessor
  • Syntax analysis
  • Semantic analysis
  • C backend
  • Builds AST

Build

GitHub https://github.com/thradams/cake

MSVC build instructions

Open the Developer Command Prompt of visual studio. Go to the src directory and type

cl build.c && build

This will build cake.exe, then run cake on its own source code.

GCC (Linux) build instructions

Got to the src directory and type:

gcc build.c -o build && ./build

To run unit tests windows/linux add -DTEST for instance:

gcc -DTEST build.c -o build && ./build

Emscripten build instructions (web)

Emscripten https://emscripten.org/ is required.

First do the normal build.

The normal build also generates a file lib.c that is the amalgameted version of the "core lib".

Then at ./src dir type:

call emcc -DMOCKFILES "lib.c" -o "Web\cake.js" -s WASM=0 -s EXPORTED_FUNCTIONS="['_CompileText']" -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']"

This will generate the \src\Web\cake.js

Running cake at command line

Make sure cake is on your system path.

Samples

cake source.c

this will output ./out/source.c

See Manual

Road map

  • Complete semantic analysis + static analysis
  • WASM Backend (eliminate emscripten dependency)
  • WASM runtime (cake -run main.c)

References

A copy of each C standard draft in included in docs folder. The parser is based on C23.

A very nice introduction was written by Al Williams

C23 Programming For Everyone

https://hackaday.com/2022/09/13/c23-programming-for-everyone/

Influenced by

  • Typescript
  • Small C compilers

Participating

You can contribute by trying out cake, reporting bugs, and giving feedback.

Have a suggestion for C?

Status?

Even if not necessary for most features, I want Cake to make a full semantic analysis to catch errors and show warnings. At this point the number of errors/warning is small compared wit gcc/clang/msvc.

How cake is tested?

I am using Visual Studio 2022 IDE to write/debug cake source. Cake is parsing itself using the includes of MSVC and it generates the out dir after build.

I use Visual Studio code with WSL for testing and compiling the code for Linux.

Cake source code is not using any extension so the output is the same of input. This compilation is useful for tracking errors together with the unit tests.

Differences from CFront

CFront was the original compiler for C++ which converted C++ to C.

CFront generated code was used only for direct compilation because it had all macros expanded making it useless to reuse the generated code in other platforms.

Cake have two modes. One is for direct compilation (like CFront) and the other preserves macros includes etc.. making it suitable for distribution.

The other difference is that C++ is a second branch of evolution making C++ more compatible with C89 than C99.

The idea of Cake is to keep the main line of evolution of C and be always 100% compatible. Cake ♥ C.

The added extensions aims to keep the spirit of the language and implement proposed features in a way they can be experimented even before standardization.

About

Cake a C23 front end and transpiler written in C

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 99.4%
  • Other 0.6%