mirror of
https://github.com/CPunch/Cosmo.git
synced 2025-11-17 04:30:05 +00:00
Added table support to the parser
a new instruction, OP_NEWARRAY has been added. this instruction will create a table out of the uint16_t values on the stack, with incremental keys [0-(u16-1)]
This commit is contained in:
19
src/cvm.c
19
src/cvm.c
@@ -619,6 +619,25 @@ int cosmoV_execute(CState *state) {
|
||||
cosmoV_makeTable(state, pairs);
|
||||
break;
|
||||
}
|
||||
case OP_NEWARRAY: {
|
||||
uint16_t pairs = READUINT();
|
||||
StkPtr val;
|
||||
CObjTable *newObj = cosmoO_newTable(state);
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj)); // so our GC doesn't free our new table
|
||||
|
||||
for (int i = 0; i < pairs; i++) {
|
||||
val = cosmoV_getTop(state, i + 1);
|
||||
|
||||
// set key/value pair
|
||||
CValue *newVal = cosmoT_insert(state, &newObj->tbl, cosmoV_newNumber(i));
|
||||
*newVal = *val;
|
||||
}
|
||||
|
||||
// once done, pop everything off the stack + push new table
|
||||
cosmoV_setTop(state, pairs + 1); // + 1 for our table
|
||||
cosmoV_pushValue(state, cosmoV_newObj(newObj));
|
||||
break;
|
||||
}
|
||||
case OP_INDEX: {
|
||||
StkPtr key = cosmoV_getTop(state, 0); // key should be the top of the stack
|
||||
StkPtr temp = cosmoV_getTop(state, 1); // after that should be the table
|
||||
|
||||
Reference in New Issue
Block a user