Live data from Hacker News

Cdecl – Turns English phrases into C declarations

cdecl.org

31–40 of 56 posts

Re: Cdecl – Turns English phrases into C declarations

#31
post #28

Hey, this is my site, first published 2009! This is the venerable cdecl enhanced with blocks support. It used to be a shared host with a PHP script shelling out to the cdecl executable, written in K&R C. Now it's that same executable running on AWS Lambda. Yes Lambda really will run arbitrary ELF binaries.

Didnt know what blocks are , it seems it's an apple extension.

Yup, they’re an extension to C/Objective-C/C++ implemented in Clang: https://en.m.wikipedia.org/wiki/Blocks_(C_language_extension...

Re: Cdecl – Turns English phrases into C declarations

#32
post #3

Here's an easy way to understand how these things work: in C, the type of a pointer/function/array mess is declared by how it's used. For a declaration like "int ( * ( * foo)(void))[3]", you can read it as "for a variable foo, after computing the expression ( * ( * foo)(void))[3], the result is an int." So one way to read C "gibberish" is to ignore the type at the beginning and parse the rest as an expression like a…

The other important part is to remember that precedence follows the same precedence as ordinary expressions, i.e. array subscripting and function call have higher precedence than pointer dereference.

It is notable that the chapter in K&R which discusses declarations also presents a partial version of the cdecl program and one of the exercises is for the reader to complete it --- really helping to dispel the notion that compilers are not mysterious magic. In my experience, it's rare for an introductory book on a programming language to also contain such "hints" on how it could be implemented.

Re: Cdecl – Turns English phrases into C declarations

#34

Earlier quoted context omitted.

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; ) Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null; C: int ((foo)(const void *))[3] Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

In the interest of furthering annoying language smuggery, the rough Rust equivalent: foo: fn() -> Box Alternately, if the pointer is into static memory and not something allocated on the heap: foo: fn() -> &'static ; That's pretty nice to look at and not too hard to read. In my opinion, for commonly used syntax (like fn decls), some well-chosen punctuation marks (', ->, :, in this case) are often boon to readability…

> Alternately, if the pointer is into static memory and not something allocated on the heap

There's really no need to box a 12-bytes array in the first place.

Re: Cdecl – Turns English phrases into C declarations

#35

Earlier quoted context omitted.

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; ) Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null; C: int ((foo)(const void *))[3] Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

In the interest of furthering annoying language smuggery, the rough Rust equivalent: foo: fn() -> Box Alternately, if the pointer is into static memory and not something allocated on the heap: foo: fn() -> &'static ; That's pretty nice to look at and not too hard to read. In my opinion, for commonly used syntax (like fn decls), some well-chosen punctuation marks (', ->, :, in this case) are often boon to readability…

> Separately, though, what do you mean by your "gimmick" comment?

Just meaning the website CDecl - its a neat tool to make C readable in English, but Ada is a real language used to make planes fly etc. Many people have contempt for it though which is why I made the joke xD

Interesting that you can note which type of memory an anonymous type comes from in Rust. I suppose its for optimization purposes? Doesn't seem that helpful from a pure typing perspective.

As an aside I doubt a layman would be able to understand that notation in Rust, whereas my girlfriend might be able to grasp or read Ada code or the output of CDecl.

Re: Cdecl – Turns English phrases into C declarations

#36
post #3

Here's an easy way to understand how these things work: in C, the type of a pointer/function/array mess is declared by how it's used. For a declaration like "int ( * ( * foo)(void))[3]", you can read it as "for a variable foo, after computing the expression ( * ( * foo)(void))[3], the result is an int." So one way to read C "gibberish" is to ignore the type at the beginning and parse the rest as an expression like a…

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; ) Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null; C: int ((foo)(const void *))[3] Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

I'm not defending C's syntax as sane here, because it's not. It boils down to have two problems:

1. The syntax isn't "type id, id, id;", it's "type expr, expr, expr;" The trend for C-style languages have been to move to the former type syntax, so C/C++ is the anomaly here.

2. Pointer declarators show up to the left of the name while function and array declarators show up to the right of the name. This means you can't figure out the type by scanning in one direction. Contrast this with LLVM, where function arguments and pointer types both go to the right of the leaf type (while arrays are infix), or Rust, where they both live on the left of the leaf type.

Re: Cdecl – Turns English phrases into C declarations

#37

Earlier quoted context omitted.

How about just using Ada? It has the added bonus of not being a gimmick (depending on who you ask I suppose ; ) Ada: type Ret_Typ is array (1..3) of Integer; Foo : access function return not null access Ret_Typ := null; C: int ((foo)(const void *))[3] Cdecl: declare foo as pointer to function (pointer to const void) returning pointer to array 3 of int

I'm not defending C's syntax as sane here, because it's not. It boils down to have two problems: 1. The syntax isn't "type id, id, id;", it's "type expr, expr, expr;" The trend for C-style languages have been to move to the former type syntax, so C/C++ is the anomaly here. 2. Pointer declarators show up to the left of the name while function and array declarators show up to the right of the name. This means you can't…

Reading for both ends is maddening for seasoned devs, but in a general sense most languages are symbol salads these days for arbitrary, subjective reasons. The decisions made during the C development to squeeze the juice out of 60 character wide terminals haunt us to this day... like case sensitivity

Re: Cdecl – Turns English phrases into C declarations

#38

tried: declare xxx as integer pointer to array of string equal to "mumu" and "kaka" got: bad character '"'...apostrophe instead of double quote has the same result...well, I guess I expected too much

You are mixing type declarations and values. Foo equals bar is not a constraint that can be specified via the type system (generally speaking).

Re: Cdecl – Turns English phrases into C declarations

#40

Earlier quoted context omitted.

I'm not defending C's syntax as sane here, because it's not. It boils down to have two problems: 1. The syntax isn't "type id, id, id;", it's "type expr, expr, expr;" The trend for C-style languages have been to move to the former type syntax, so C/C++ is the anomaly here. 2. Pointer declarators show up to the left of the name while function and array declarators show up to the right of the name. This means you can't…

Reading for both ends is maddening for seasoned devs, but in a general sense most languages are symbol salads these days for arbitrary, subjective reasons. The decisions made during the C development to squeeze the juice out of 60 character wide terminals haunt us to this day... like case sensitivity

> like case sensitivity

What's wrong with case sensitivity?

Post reply on HN