The Croc Programming Language
Basic operations

Macros

#define croc_isTrue   croc_asBool
 

Functions

word_t croc_foreachBegin (CrocThread *t, uword_t numContainerVals)
 
int croc_foreachNext (CrocThread *t, word_t state, uword_t numIndices)
 
void croc_foreachEnd (CrocThread *t, word_t state)
 
void croc_removeKey (CrocThread *t, word_t obj)
 
word_t croc_pushToString (CrocThread *t, word_t slot)
 
word_t croc_pushToStringRaw (CrocThread *t, word_t slot)
 
int croc_asBool (CrocThread *t, word_t slot)
 
crocint_t croc_asInt (CrocThread *t, word_t slot)
 
crocfloat_t croc_asFloat (CrocThread *t, word_t slot)
 
int croc_in (CrocThread *t, word_t item, word_t container)
 
crocint_t croc_cmp (CrocThread *t, word_t a, word_t b)
 
int croc_equals (CrocThread *t, word_t a, word_t b)
 
int croc_is (CrocThread *t, word_t a, word_t b)
 
word_t croc_idx (CrocThread *t, word_t container)
 
void croc_idxa (CrocThread *t, word_t container)
 
word_t croc_idxi (CrocThread *t, word_t container, crocint_t idx)
 
void croc_idxai (CrocThread *t, word_t container, crocint_t idx)
 
word_t croc_slice (CrocThread *t, word_t container)
 
void croc_slicea (CrocThread *t, word_t container)
 
word_t croc_field (CrocThread *t, word_t container, const char *name)
 
word_t croc_fieldStk (CrocThread *t, word_t container)
 
void croc_fielda (CrocThread *t, word_t container, const char *name)
 
void croc_fieldaStk (CrocThread *t, word_t container)
 
word_t croc_rawField (CrocThread *t, word_t container, const char *name)
 
word_t croc_rawFieldStk (CrocThread *t, word_t container)
 
void croc_rawFielda (CrocThread *t, word_t container, const char *name)
 
void croc_rawFieldaStk (CrocThread *t, word_t container)
 
word_t croc_hfield (CrocThread *t, word_t container, const char *name)
 
word_t croc_hfieldStk (CrocThread *t, word_t container)
 
void croc_hfielda (CrocThread *t, word_t container, const char *name)
 
void croc_hfieldaStk (CrocThread *t, word_t container)
 
word_t croc_pushLen (CrocThread *t, word_t slot)
 
crocint_t croc_len (CrocThread *t, word_t slot)
 
void croc_lena (CrocThread *t, word_t slot)
 
void croc_lenai (CrocThread *t, word_t slot, crocint_t length)
 
word_t croc_cat (CrocThread *t, uword_t num)
 
void croc_cateq (CrocThread *t, word_t dest, uword_t num)
 

Detailed Description

All sorts of simple operations on values of any type.

Macro Definition Documentation

#define croc_isTrue   croc_asBool

An alias for croc_asBool.

Function Documentation

word_t croc_foreachBegin ( CrocThread t,
uword_t  numContainerVals 
)

This, croc_foreachNext, and croc_foreachEnd are used together to perform the equivalent of the Croc 'foreach' loop.

Here's how you use them:

  1. Push the container value(es) that you want to iterate over (the equivalent of the stuff after the semicolon in the Croc 'foreach' loop.
  2. Call croc_foreachBegin, telling it how many values you pushed, and store the value it returns.
  3. In a loop, call croc_foreachNext with the value that croc_foreachBegin returned and the number of indices you want. Use the return value from croc_foreachNext to determine if the loop should continue.
  4. Inside the loop, access the indices as normal stack slots.
  5. When you leave the loop (either by exiting normally or by breaking), call croc_foreachEnd to clean up.

It sounds complex, but it's easier in an example. Let's translate the following loop into API calls.

// Assume a is a global that holds [1, 2, 3]
foreach(i, val; a)
writeln(i, ": ", val)

In the native API:

word_t state;
...
croc_pushGlobal(t, "a"); // push the container
for(state = croc_foreachBegin(t, 1); // 1 since we pushed 1 value; save the state!
croc_foreachNext(t, state, 2); ) // pass the state, and 2 to mean 2 indices
{
// In here, the top two stack slots contain the two indices we asked for (the index and value).
croc_pushGlobal(t, "writeln");
croc_dup(t, -4); // key
croc_pushString(t, ": ");
croc_dup(t, -5); // value
croc_call(t, -5, 0);
// Don't have to clean up the stack here, croc_foreachNext will do it for us
}
// clean up!
croc_foreachEnd(t, state);
// Now the stack is the same as it was before the initial croc_pushGlobal
Parameters
numContainerValsis how many values you pushed for the container. This must be 1, 2, or 3, and there must be that many values on top of the stack.
Returns
a value used to keep track of the state of the foreach loop. There will also be some values on the stack which you shouldn't mess with.
int croc_foreachNext ( CrocThread t,
word_t  state,
uword_t  numIndices 
)

Gets the next values in a foreach loop, as demonstrated in croc_foreachBegin.

Parameters
stateis the value that was returned from croc_foreachBegin.
numIndicesis how many indices you want. In Croc, this would be the number of variables to the left of the semicolon in the 'foreach'. You can indeed pass 1 for this parameter, and it will work the same way as in Croc; the first index returned from the iterator function will be ignored, and only the second index will be on top of the stack.
Returns
nonzero if the loop should continue.
void croc_foreachEnd ( CrocThread t,
word_t  state 
)

Cleans up a foreach loop, as demonstrated in croc_foreachBegin.

You should call this no matter how you exit the loop (unless you return from a native function inside the loop... then you don't have to). This will check that the stack is in the appropriate configuration and will pop the bookkeeping variables that croc_foreachBegin pushed.

Parameters
stateis the value that was returned from croc_foreachBegin.
void croc_removeKey ( CrocThread t,
word_t  obj 
)

Removes the key-value pair from the table or namespace at slot obj whose key is the value on top of the stack, then pops that value.

For tables, you can also do this by index-assigning null into a key, but for namespaces this is the only way to remove a key-value pair.

word_t croc_pushToString ( CrocThread t,
word_t  slot 
)

Pushes a string representation of the value at slot by essentially calling the Croc toString on it.

word_t croc_pushToStringRaw ( CrocThread t,
word_t  slot 
)

Pushes a raw string representation of the value at slot by essentially calling the Croc rawToString on it.

int croc_asBool ( CrocThread t,
word_t  slot 
)

The equivalent of using as bool in Croc.

Returns
nonzero if the value at slot converts to true, and zero otherwise.
crocint_t croc_asInt ( CrocThread t,
word_t  slot 
)

The equivalent of using as int in Croc.

Returns
the bool, int, or float in slot typecast to an integer.
crocfloat_t croc_asFloat ( CrocThread t,
word_t  slot 
)

The equivalent of using as float in Croc.

Returns
the int or float in slot typecast to a float.
int croc_in ( CrocThread t,
word_t  item,
word_t  container 
)

The equivalent of Croc's 'in' operator.

Calls opIn metamethods if necessary.

Returns
nonzero if the value at item is in the value at container.
crocint_t croc_cmp ( CrocThread t,
word_t  a,
word_t  b 
)

The equivalent of Croc's <=> operator.

Calls opCmp metamethods if necessary.

Returns
a comparison value (negative for less, 0 for equal, positive for greater) of the result of comparing the values at a and b.
int croc_equals ( CrocThread t,
word_t  a,
word_t  b 
)

The equivalent of Croc's == operator.

Calls opEquals metamethods if necessary.

Returns
nonzero if the values at a and b are equal.
int croc_is ( CrocThread t,
word_t  a,
word_t  b 
)

The equivalent of Croc's is operator.

Returns
nonzero if the values at a and b are identical.
word_t croc_idx ( CrocThread t,
word_t  container 
)

The equivalent of Croc's a[b] operation.

This expects the index to be on top of the stack, and it is replaced with the result of the indexing operation. Calls opIndex metamethods if necessary.

Parameters
containeris the object to index.
Returns
the stack index of the resulting value.
void croc_idxa ( CrocThread t,
word_t  container 
)

The equivalent of Croc's a[b] = c operation.

This expects the value to be assigned (c) to be on top of the stack, and the index (b) below it. Calls opIndexAssign metamethods if necessary. Pops both the value and the index from the top of the stack.

Parameters
containeris the object to index-assign.
word_t croc_idxi ( CrocThread t,
word_t  container,
crocint_t  idx 
)

A convenience function when you want to index a container using an integer index.

Pushes the result of indexing on top of the stack.

Parameters
containeris the object to index.
idxis the integer index.
Returns
the stack index of the pushed value.
void croc_idxai ( CrocThread t,
word_t  container,
crocint_t  idx 
)

A convenience function when you want to index-assign a container using an integer index.

Expects the value to be on top of the stack, and pops it.

Parameters
containeris the object to index-assign.
idxis the integer index.
word_t croc_slice ( CrocThread t,
word_t  container 
)

The equivalent of Croc's a[b .. c] operation.

Expects the slice indices on top of the stack (the high index on top, and the low index below it). Calls opSlice metamethods if necessary. Pops the slice indices and pushes the result.

Parameters
containeris the object to slice.
Returns
the stack index of the resulting value.
void croc_slicea ( CrocThread t,
word_t  container 
)

The equivalent of Croc's a[b .. c] = d operation.

Expects the value to be assigned into the slice on top, and the slice indices below it (the high index below the value, and the low index below that). Calls opSliceAssign metamethods if necessary. Pops all three values.

Parameters
containeris the object to slice-assign.
word_t croc_field ( CrocThread t,
word_t  container,
const char *  name 
)

The equivalent of Croc's a.name operation, this gets the field named name from the object in container and pushes the result.

Calls any opField metamethods if necessary.

Returns
the stack index of the pushed value.
word_t croc_fieldStk ( CrocThread t,
word_t  container 
)

Same as croc_field, but expects the field name to be on top of the stack, which is replaced with the result of the field access (like how croc_idx works).

Returns
the stack index of the resulting value.
void croc_fielda ( CrocThread t,
word_t  container,
const char *  name 
)

The equivalent of Croc's a.name = b operation, this sets the field named name in container to the value on top of the stack, and then pops that value.

Calls any opFieldAssign metamethods if necessary.

void croc_fieldaStk ( CrocThread t,
word_t  container 
)

Same as croc_fielda, but expects the value on top of the stack and the name of the field to assign to be in the slot below it (like how croc_idxa works).

Pops both the name and value.

word_t croc_rawField ( CrocThread t,
word_t  container,
const char *  name 
)

Same as croc_field, but does not call any opField metamethods.

word_t croc_rawFieldStk ( CrocThread t,
word_t  container 
)

Same as croc_fieldStk, but does not call any opField metamethods.

void croc_rawFielda ( CrocThread t,
word_t  container,
const char *  name 
)

Same as croc_fielda, but does not call any opFieldAssign metamethods.

void croc_rawFieldaStk ( CrocThread t,
word_t  container 
)

Same as croc_fieldaStk, but does not call any opFieldAssign metamethods.

word_t croc_hfield ( CrocThread t,
word_t  container,
const char *  name 
)

Gets the hidden field named name from the class or instance in container and pushes it.

Returns
the stack index of the pushed value.
word_t croc_hfieldStk ( CrocThread t,
word_t  container 
)

Same as croc_hfield, but expects the name on top of the stack and replaces the name with the value of the field (like croc_fieldStk).

Returns
the stack index of the resulting value.
void croc_hfielda ( CrocThread t,
word_t  container,
const char *  name 
)

Assigns the value on top of the stack into hidden field named name into the class or instance in container and pops the value (like croc_fielda).

void croc_hfieldaStk ( CrocThread t,
word_t  container 
)

Same as croc_hfielda but expects the value to be on top of the stack and the name of the hidden field to be below it, and pops both (like croc_fieldaStk).

word_t croc_pushLen ( CrocThread t,
word_t  slot 
)

The equivalent of Croc's # operator, this pushes the length of the object in slot, calling opLength metamethods if necessary.

Returns
the stack index of the pushed length.
crocint_t croc_len ( CrocThread t,
word_t  slot 
)

Similar to croc_pushLen, except it expects the length to be an integer (throwing an error if not), and doesn't push the length onto the stack, instead returning it.

Returns
the length of the object at slot.
void croc_lena ( CrocThread t,
word_t  slot 
)

Sets the length of the object at slot to the value on top of the stack, calling opLengthAssign metamethods if necessary, and pops the length.

void croc_lenai ( CrocThread t,
word_t  slot,
crocint_t  length 
)

Same as croc_lena, but this is a convenience function which sets the length of the object at slot to the length given by length instead of using a value on the stack.

The stack is left unchanged.

word_t croc_cat ( CrocThread t,
uword_t  num 
)

The equivalent of Croc's ~ operator, this concatenates the top num values on the stack, calling opCat and opCat_r metamethods as necessary.

The top num values are popped, and the result of concatenation is pushed in their place.

Returns
the stack index of the resulting value.
void croc_cateq ( CrocThread t,
word_t  dest,
uword_t  num 
)

The equivalent of Croc's ~= operator, this appends the top num values to the object in dest, calling opCatAssign metamethods as necessary.

The top num values are popped.