Skip to content

Commit

Permalink
flow algorithm 2.0 (3.0 will be required soon)
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Jun 14, 2024
1 parent 72d7f1a commit 2919a81
Show file tree
Hide file tree
Showing 35 changed files with 2,309 additions and 2,643 deletions.
116 changes: 59 additions & 57 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1351,47 +1351,13 @@ https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3038.htm

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2778.pdf

## C2Y Transformations

## Cake Extensions (Not in C23)

### Extension - try catch throw

```
try-statement:
try secondary-block
try secondary-block catch secondary-block
```

```
jump-statement:
throw;
```

try catch is a external block that we can jump off.

try catch is a **LOCAL jump** this is on purpose not a limitation.


catch block is optional.

```c
try
{
for (int i = 0 ; i < 10; i++) {
for (int j = 0 ; j < 10; j++) {
...
if (error) throw;
...
}
}
}
catch
{
}
```

### Extension - defer

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3199.htm

*defer* will call the defer statement before the block exit at inverse order of declaration.

```
Expand Down Expand Up @@ -1440,8 +1406,7 @@ int main() {

### Extension - if with initializer

No idea why C++ 17 if with initializer was not proposed for C23!
But in cake it is implemented.
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3196.htm

```c
#include <stdio.h>
Expand Down Expand Up @@ -1471,11 +1436,64 @@ int main()
}
```

An extension if + initializer + defer expression was considered but not implemented yet.

C++ proposal
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html

### Extension typename on _Generic

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3260.pdf

```c
int main()
{
const int * const p;
static_assert(_Generic(p, const int *: 1));

/*extension*/
static_assert(_Generic(int, int : 1));
static_assert(_Generic(typeof(p), const int * const: 1));
}
```


## Cake Extensions (Not in C23, C2Y)

### Extension - try catch throw

```
try-statement:
try secondary-block
try secondary-block catch secondary-block
```

```
jump-statement:
throw;
```

try catch is a external block that we can jump off.

try catch is a **LOCAL jump** this is on purpose not a limitation.


catch block is optional.

```c
try
{
for (int i = 0 ; i < 10; i++) {
for (int j = 0 ; j < 10; j++) {
...
if (error) throw;
...
}
}
}
catch
{
}
```

### Extension Literal function - lambdas

Lambdas without capture where implemented using a syntax similar of compound literal for function pointer.
Expand Down Expand Up @@ -1610,23 +1628,7 @@ Arithmetic types, pointer types, and the nullptr_t type are collectively called

See [ownership](ownership.html)

### Extension typename on _Generic

```c
int main()
{
const int * const p;
static_assert(_Generic(p, const int *: 1));

/*extension*/
static_assert(_Generic(int, int : 1));
static_assert(_Generic(typeof(p), const int * const: 1));
}

```

and the lvalue conversion will not happen, allowing
more precise (with qualifiers) type match.

### Extension assert statement

Expand Down
16 changes: 16 additions & 0 deletions src/expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,14 @@ struct expression* owner postfix_expression_tail(struct parser_ctx* ctx, struct
}
else if (ctx->current->type == '++')
{
if (type_is_owner(&p_expression_node->type))
{
compiler_diagnostic_message(C_ERROR_OPERATOR_INCREMENT_CANNOT_BE_USED_IN_OWNER,
ctx,
p_expression_node->first_token,
"operator ++ cannot be used in owner pointers");
}

if (!expression_is_lvalue(p_expression_node))
{
compiler_diagnostic_message(C_ERROR_OPERATOR_NEEDS_LVALUE,
Expand All @@ -2013,6 +2021,14 @@ struct expression* owner postfix_expression_tail(struct parser_ctx* ctx, struct
}
else if (ctx->current->type == '--')
{
if (type_is_owner(&p_expression_node->type))
{
compiler_diagnostic_message(C_ERROR_OPERATOR_DECREMENT_CANNOT_BE_USED_IN_OWNER,
ctx,
p_expression_node->first_token,
"operator -- cannot be used in owner pointers");
}

if (!expression_is_lvalue(p_expression_node))
{
compiler_diagnostic_message(C_ERROR_OPERATOR_NEEDS_LVALUE,
Expand Down
33 changes: 19 additions & 14 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#pragma safety enable

void free(void* _Owner _Opt p);
char* _Owner _Opt strdup(const char* s);
[[noreturn]] void exit( int exit_code );
#define NULL ((void*)0)
void f()
{
char * _Opt _Owner s = strdup("a");

if (s == NULL)
exit(1);

static_state(s, "not-null");
static_debug(s);
free(s);

#pragma safety enable

#include <stdlib.h>
#include <string.h>

struct X {
char *_Owner _Opt name;
};

struct X * _Owner make();


int main() {
struct X * _Owner p = make();

static_debug(p);
}


Loading

0 comments on commit 2919a81

Please sign in to comment.