Skip to content

thradams/cake

Repository files navigation

The C Programming language 1978

"C is a general-purpose programming language which features economy of expression, modern control flow and data structures, and a rich set of operators. C is not a "very high level" language, nor a "big" one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages."

"In our experience, C has proven to be a pleasant, expressive, and versatile language for a wide variety of programs. It is easy to learn, and it wears well as one's experience with it grows"

The C Programming language Second Edition 1988

"As we said in the preface to the first edition, C "wears well as one's experience with it grows." With a decade more experience, we still feel that way."

🍰 Cake

Cake is a compiler front end written from scratch in C, designed from the C23 language specification. 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, defer and static object lifetime checks.

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. Cake can also be used as static analyzer, especially the new object lifetime analysis.

Features

  • C23 preprocessor
  • C23 syntax analysis
  • C23 semantic analysis
  • Static object lifetime checks (Extension)
  • Sarif output
  • C backend
  • AST
  • So far, 34 preprocessor diagnostics, 236 compiler diagnostics

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 on Linux build instructions

Got to the src directory and type:

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

Clang on Linux/Windows/MacOS build instructions

Got to the src directory and type:

clang 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 amalgamated 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

  • Remove all warnings of cake source code in safe mode to validate the design and implementation
  • Fixes

References

How did we get here?

A copy of each C standard draft in included in docs folder.

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?

DISCORD SERVER

https://discord.gg/YRekr2N65S

How cake is developed?

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++, designed to convert C++ code into C.

The code generated by CFront was used solely for direct compilation because it had all macros expanded, making the generated output unsuitable for reuse on other platforms.

Cake offers two modes. The first is for direct compilation, similar to CFront. The second mode preserves macros, includes, and other preprocessor directives, making the output suitable for distribution.

Another key difference is that C++ represents a separate evolutionary branch, maintaining greater compatibility with C89 than with C99.

The goal of Cake is to follow the main evolutionary line of C and remain 100% compatible with C.

The added extensions aim to uphold the spirit of the language while allowing developers to experiment with proposed features even before they become standardized.