My primary programming language, j, was recently open-sourced. In order to improve it, I'm studying the source, which is written in C.
But it's been a long (!) time since I've read or written C, and I wasn't even good at it then. And the way this particular codebase is written is ... idiosyncratic (many APL interpreters, J among them, have their source written in high-level "APL style", even when written in a low-level language; very terse, redundancy eschewed, heavy macro use, etc.)
At the moment, I'm trying to understand the fundamental data structures it employs. The most fundamental one is the typedef A ("A" is for "array"):
typedef struct {I k,flag,m,t,c,n,r,s[1];}* A;
which I understand fine. But I'm struggling to wrap my head around what AF is, two lines later:
typedef A (*AF)();
What does this syntax mean? In particular, what does it mean when things are later declared as "type AF"? Is an AF simply a pointer to an A?
My immediate goal is to interpret memory dumps which include things of type V (for "verb"), whose first two members are AFs:
typedef struct {AF f1,f2;A f,g,h;I flag,mr,lr,rr,fdep;C id;} V;
but my overall goal is larger than that, so please elaborate on the syntax employed in the definition of AF.
typedef is a reserved keyword in the programming languages C and C++. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
typedef struct { int scruples; int drams; int grains; } WEIGHT; The structure WEIGHT can then be used in the following declarations: WEIGHT chicken, cow, horse, whale; In the following example, the type of yds is "pointer to function with no parameter specified, returning int ".
A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared.
The typedef in C/C++ is a keyword used to assign alternative names to the existing datatypes. It is mostly used with user-defined datatypes when the naming of the predefined datatypes becomes slightly complicated to use in programs.
As already answered, an AF (Array Function) is a pointer to a function that returns an A (Array object pointer).
In the definition of V (Verb, ie. function object), there are two AFs. v1 is a pointer to the monadic function implementation, and v2 is the pointer to the dyadic function. If the V represents an operator (adverb), then v1 and v2 are still the monadic and dyadic implementations respectively, but also f g and h may be used to hold (curried) left and/or right arguments. mr lr and rr are monadic-rank, left-rank, and right-rank respectively. And id holds an opcode so a printable representation can still be recovered from the structure.
If any of the operands in f g or h are themselves verbs, their V struct will be at f->k bytes past *f, respectively for g and h, like all "payload data".
A very useful link I found for understanding the basic types in the J implementation is Roger Hui BAA talk notes (2.69M scanned pdf). And the full write-up is at Implementation of J (html).
You might also find my primitive clone instructive. See also my questions here and here.
AF is a typedef for a function pointer. Specifically, AF refers to a pointer to a function that takes an unspecified number of parameters and return a result of type A.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With