Cosmo/src/cvm.h
CPunch 181ef8a18c Added dictionaries {}
Objects are now separate from {} dictionaries

the . operator now only indexes fields on objects, the [] operator can only be used on objects if the __index or __newindex functions are defined

Additionally 4 new instructions have been added to the VM: OP_NEWDICT, OP_INDEX, OP_INCINDEX, and OP_NEWINDEX.

The syntax to create a dictionary is as follows { <key> : <value>, <otherkey> : <othervalue> } eg. { "hello" : "world", "foo" : 1337 }

The Lexer & Parser was extended to add the TOKEN_COLON ':' token.
2020-12-09 20:32:42 -06:00

48 lines
1.5 KiB
C

#ifndef COSMOVM_H
#define COSMOVM_H
#include <string.h>
#include "cosmo.h"
#include "cstate.h"
typedef enum {
COSMOVM_OK,
COSMOVM_RUNTIME_ERR,
COSMOVM_BUILDTIME_ERR
} COSMOVMRESULT;
// args = # of pass parameters, nresults = # of expected results
COSMO_API COSMOVMRESULT cosmoV_call(CState *state, int args);
COSMO_API void cosmoV_makeObject(CState *state, int pairs);
COSMO_API void cosmoV_makeDictionary(CState *state, int pairs);
COSMO_API bool cosmoV_getObject(CState *state, CObjObject *object, CValue key, CValue *val);
COSMO_API void cosmoV_error(CState *state, const char *format, ...);
// nice to have wrappers
// pushes a cosmo_Number to the stack
static inline void cosmoV_pushNumber(CState *state, cosmo_Number num) {
cosmoV_pushValue(state, cosmoV_newNumber(num));
}
// pushes a boolean to the stack
static inline void cosmoV_pushBoolean(CState *state, bool b) {
cosmoV_pushValue(state, cosmoV_newBoolean(b));
}
// pushes a C Function to the stack
static inline void cosmoV_pushCFunction(CState *state, CosmoCFunction func) {
cosmoV_pushValue(state, cosmoV_newObj(cosmoO_newCFunction(state, func)));
}
static inline void cosmoV_pushLString(CState *state, const char *str, size_t len) {
cosmoV_pushValue(state, cosmoV_newObj(cosmoO_copyString(state, str, len)));
}
// accepts a null terminated string and copys the buffer to the VM heap
static inline void cosmoV_pushString(CState *state, const char *str) {
cosmoV_pushLString(state, str, strlen(str));
}
#endif