Skip to content

Commit

Permalink
coliru run
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Dec 21, 2024
1 parent 0c974c7 commit c226541
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 85 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ such as Visual Studio and Visual Studio Code, providing a
seamless integration.

It can also function as a preprocessor, converting C23 code to C89.
This allows developers to use modern features while targeting
This allows developers to use modern or experimental features while targeting
compilers that do not yet support the latest language standards.

The objective of the C89 generation is to produce low-level C code,
simplifying the process of writing C backends **dedicated to code generation**.

Previous versions of Cake included a conversion mode to
translate code while preserving the preprocessor parts.
Although useful, this process could not guarantee 100% conversion,
Expand All @@ -64,7 +67,7 @@ http://thradams.com/cake3/playground.html.
* Sarif output
* Backend generating C89 compatible code
* AST
* So far, 34 preprocessor diagnostics, 236 compiler diagnostics
* More than 260 diagnostics


# Build
Expand Down
23 changes: 4 additions & 19 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
#pragma safety enable;

struct X
{
char * _Owner _Opt p;
char * p2;
};

char* _Owner _Opt strdup(const char *s);
void free(void* _Owner _Opt p);

int main() {
const char* _Owner _Opt p0 = strdup("a");
struct X x = {
.p = p0
};
free(x.p);
}
#pragma cake diagnostic check "-Wanalyzer-null-dereference"
int main()
{
defer {return 1;}
}
2 changes: 1 addition & 1 deletion src/web/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function OnSwap()
outputEditor.setValue(temp);
}

function OnCompileOuput()
function Run()
{
document.getElementById("output").value = "";
var http = new XMLHttpRequest();
Expand Down
15 changes: 8 additions & 7 deletions src/web/playground.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,21 @@
<select id="outtype" style="margin-left:auto;margin-right: 10px; ">
<option value="-2">C89</option>
<!--
<option value="-1">C89 (ANSI C)</option>
<option value="1">C99</option>
<option value="2">C11</option>
<option value="3">C23</option>
<option value="4">C2Y</option>
<option value="100">C2Y++</option>
-->
<option value="-1">C89 (ANSI C)</option>
<option value="1">C99</option>
<option value="2">C11</option>
<option value="3">C23</option>
<option value="4">C2Y</option>
<option value="100">C2Y++</option>
-->
<option value="0">Preprocess only</option>
<!--<option value="-2">C89 -direct-compilation</option>-->
</select>

<label>Options</label><input type="text" id="options" />

<button onclick="Share()">Share</button>
<button onclick="Run()">Run</button>
<input type="text" id="sharelink" style="width:100%;display:none" />
</div>
<div id="containerOne" class="containerOne">
Expand Down
93 changes: 37 additions & 56 deletions src/web/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
sample["C99"] = [];
sample["C99"]["_Bool"] =
`
/*
_Bool type was introduced in C99 as built-in type used
to represent boolean values and the header <stdbool.h>
with macros bool true and false.
C23 introduced keywords (see C23 bool sample).
*/

int main(void)
{
_Bool b = 1;

_Bool b2 = 123; //TODO

return 0;
}
`;
Expand Down Expand Up @@ -97,7 +93,7 @@ sample["C99"]["__func__"] =
`;


sample["C99"]["for block scope"] =
sample["C99"]["init-clause of the for loop"] =
`
int main()
{
Expand All @@ -109,9 +105,16 @@ int main()
}
`;

sample["C99"]["Mixed declarations"] =
`
//not converted yet
sample["C99"]["inline"] =
`
int main()
{
const int max = 10;
for (int n = max - 1; n >= 0; n--)
{
// body of loop
}
}
`;

sample["C99"]["restrict pointers"] =
Expand Down Expand Up @@ -209,9 +212,6 @@ sample["C11"]["_Generic"] =
default: cbrtl \\
)(X)

#pragma expand cbrt


int main(void)
{
cbrt(1.0);
Expand Down Expand Up @@ -260,10 +260,7 @@ sample["C11"]["u8 literals"] =
/*
* cake input source code encode is always utf8
* cake output source code is also utf8
*
* This web output also works with utf8. So everything just works
* even without u8 prefix. (press compile output)
*
* u8 prefix may be useful in case you have a compiler where
* the input or output is not uft8.
*/
Expand Down Expand Up @@ -321,20 +318,32 @@ sample["C23"]["Binary Literal"] =
`
int main()
{
int a = X;
int b = 0B10;
}
`;


sample["C23"]["static_assert"] =
`
int main()
{
static_assert(1 == 1, "error");

/*message is optional in C23*/
static_assert(1 == 1);
//https://en.cppreference.com/w/c/language/_Static_assert
#include <assert.h> // no longer needed since C23

int main(void)
{
// Test if math works, C23:
static_assert((2 + 2) % 3 == 1, "Whoa dude, you knew!");
// Pre-C23 alternative:
_Static_assert(2 + 2 * 2 == 6, "Lucky guess!?");

// This will produce an error at compile time.
// static_assert(sizeof(int) < sizeof(char), "Unmet condition!");

constexpr int _42 = 2 * 3 * 2 * 3 + 2 * 3;
static_assert(_42 == 42); // the message string can be omitted.

// const int _13 = 13;
// Compile time error - not an integer constant expression:
// static_assert(_13 == 13);
}
`;

Expand Down Expand Up @@ -480,9 +489,6 @@ sample["C23"]["#warning"] =

int main()
{
/*
We just comment this line when target is < c23
*/
#warning TODO ..missing code
}
`;
Expand Down Expand Up @@ -674,18 +680,10 @@ int main()


sample["C23"]["bool true false"] =
`
/*
C23 introduced keyword bool as alternative to _Bool and
true and false as constants.
Cake translate bool to _Bool when compiling to C99/C11
and to unsigned char when compiling to C89.
*/
`

#include <stdio.h>


int main()
{
bool b = true;
Expand All @@ -704,7 +702,7 @@ int main()
`;

sample["C23"]["nullptr"] =
`
`

#include <stdlib.h>
#include <stdio.h>
Expand All @@ -716,24 +714,8 @@ int main()

auto a = nullptr;


printf("%s", _Generic(nullptr, typeof(nullptr) : "nullptr_t"));
}

/*
in case you want to add a compatibility header with
nullptr defined as macro, then nullptr macro is preserved
like this. The same for other features like static_assert.
*/

#define nullptr ((void*)0)

int F()
{
void * p = nullptr;
void * p2 = NULL;
}

`;


Expand Down Expand Up @@ -895,8 +877,7 @@ enum X : short {
int main() {
enum X x = A;
}


//TODO _Generic on enum X
`;

sample["C2Y"] = [];
Expand Down

0 comments on commit c226541

Please sign in to comment.