The @encode directive returns a const char * which is a coded type descriptor of the various elements of the datatype that was passed in. Example follows:
struct
@Lothars answer might be "cynical", but it's pretty close to the mark, unfortunately. In order to implement something like @encode()
, you need a full blown parser in order to extract the the type information. Well, at least for anything other than "trivial" @encode()
statements (i.e., @encode(char *)
). Modern compilers generally have either two or three main components:
The front end must parse all the source code and basically converts the source code text in to an internal, "machine useable" form.
The back end translates the internal, "machine useable" form in to executable code.
Compilers that have an "intermediate end" typically do so because of some need: they support multiple "front ends", possibly made up of completely different languages. Another reason is to simplify optimization: all the optimization passes work on the same intermediate representation. The gcc
compiler suite is an example of a "three stage" compiler. llvm
could be considered an "intermediate and back end" stage compiler: The "low level virtual machine" is the intermediate representation, and all the optimization takes place in this form. llvm
also able to keep it in this intermediate representation right up until the last second- this allows for "link time optimization". The clang
compiler is really a "front end" that (effectively) outputs llvm
intermediate representation.
So, if you want to add @encode()
functionality to an 'existing' compiler, you'd probably have to do it as a "source to source" 'compiler / preprocessor'. This was how the original Objective-C and C++ compilers were written- they parsed the input source text and converted it to "plain C" which was then fed in to the standard C compiler. There's a few ways to do this:
yacc
and lex
to put together a ANSI-C parser. You'll need a grammar- ANSI C grammar (Yacc) is a good start. Actually, to be clear, when I say yacc
, I really mean bison and flex
. And also, loosely, the other various yacc
and lex
like C-based tools: lemon, dparser, etc...perl
with Yapp or EYapp, which are pseudo-yacc
clones in perl
. Probably better for quickly prototyping an idea compared to C-based yacc
and lex
- it's perl
after all: Regular expressions, associative arrays, no memory management, etc.Note: I have no personal experience using any of these tools to do anything like adding @encode()
, but I suspect they would be a big help.
@Lothar makes a good point in his comment. I had actually intended to include lcc
, but it looks like it got lost along the way.
lcc
C compiler. This is a C compiler that is particularly small, at least in terms of source code size. It also has a book, which I highly recommend.tcc
C compiler. Not quite as pedagogical as lcc
, but definitely still worth looking at.poc
Objective-C compiler. This is a "source to source" Objective-C compiler. It parses the Objective-C source code and emits C source code, which it then passes to gcc
(well, usually gcc
). Has a number of Objective-C extensions / features that aren't available in gcc
. Definitely worth looking at.