C Reference - kevinlawler/kona GitHub Wiki

Typedefs

Type Definition
void * V
long I
double F
char C
char * S
const char * cS
unsigned char UC
unsigned long UI

Macros

Definitions

Macro Definition
Z static
O printf
R return
SW switch
CS(n,x) case n:x;break;
CSR(n,x) case n:x;
CD default
II LONG_MAX
IN LONG_MIN
FI 1/0.0
FN 0/0.0

Utility

Macro Definition Meaning
xt x->t Type of array x
xn x->n Number of elements in array x
P(x,y) {if(x)R(y);} If x is true return y
U(x) P(!(x),0) If x is false return 0
Example usage:
K z=newK(0,3); U(z)
means create new list with 3 elements, but
if could not, return 0
GC goto cleanup Jump to the cleanup section within a function
AE(x) (sizeof(x)/sizeof(x[0])) Number of elements in array x
diff(x,y) (((V*)(x)) - (V*)(y)) Difference between pointers x and y
in(x,y) (diff(x,y) < AE(y) && diff(x,y)>=0) Check if pointer x is inside the range of y

Iteration

Loop from 0 to n-1 executing statement x on each iteration. i is initially 0 and is incremented on each iteration, _i is set to the value of n.

For the macros DO2 and DO3 the variable i is replaced by j and k, _i by _j and _k respectively.

DO(n,x) {I i=0,_i=(n);for(;i<_i;++i){x;}}
DO2(n,x) {I j=0,_j=(n);for(;j<_j;++j){x;}}
DO3(n,x) {I k=0,_k=(n);for(;k<_k;++k){x;}}

Errors

Macro Error
ME Memory(wsfull)
TE Type
VE Valence
PE Parse
IE Int
XE Index
LE Length
RE Rank
NE Nonce
DOE Domain
NYI Not yet implemented

K Object

Definition

typedef struct k0{I c,t,n;struct k0 *k[1];} *K;

Field Meaning
I c Reference Count
I t Type
I n Number of elements
struct k0 *k[1] Pointer to object's data. It is array to allow
"bracket" indexing, like here:

K z=newK(0,3); U(z) kK(z)[0]=Ki(10); kK(z)[1]=Kf(1.1); kK(z)[2]=_n();

An object's type t is an integer in the interval [-4,7]. A negative integer indicates that the object is an array of the corresponding type, the object will have more than one element of the type.

Type Scalar Array
List 0
Integer 1 -1
Float 2 -2
Char 3 -3
Symbol 4 -4
Dictionary 5
Null 6
Function(type 7) 7

Refer to Data and Execution for a more detailed explanation of K objects.

Accessors

Name Definition Result
ke(x) (((K)x)->k) K pointer to object's data
kK(x) ke(x) K pointer to object's data
kI(x) ((I*)ke(x)) I pointer to I data
kF(x) ((F*)ke(x)) F pointer to F data
kC(x) ((C*)ke(x)) C pointer to C data
kS(x) ((S*)ke(x)) S pointer to S data
kV(x) ((V*)ke(x)) Void pointer to object's data
kVC(x) ((K)kV(x)[CODE]) K pointer to CODE void pointer in a type 7 object
kW(x) ((V*)kS(kVC(x))) Void pointer to string in CODE of a type 7 object

Creation

K newK(I t,I n) Creates a K object with type t and number of elements n
Function Result
K Ki(I x) K object of type I with an element x
K Kf(F x) K object of type F with an element x
K Kc(C x) K object of type C with an element x
K Ks(S x) K object of type S with an element x
K Kd() Empty dictionary object
K Kn() Null object
K Kv() Function(type 7) object

Sample Code

Defining a simple function to calculate the average of an integer array.

K average(K x)
{
  I sum=0; K z;
  P(1!=ABS(xt),DOE);        // Check if object is of type integer, return a domain error if it isn't
  DO(xn, sum+=kI(x)[i]);    // Sum the elements of the array
  U(z=Kf(sum/(F)xn));       // Generate a float object with a single element equal to the average
  R z;                      // Return the average
}
⚠️ **GitHub.com Fallback** ⚠️