Cosmo/src/cbaselib.c

196 lines
5.9 KiB
C
Raw Normal View History

2020-10-28 05:16:30 +00:00
#include "cbaselib.h"
2020-11-12 23:17:41 +00:00
#include "cvm.h"
2020-10-28 05:16:30 +00:00
#include "cvalue.h"
#include "cobj.h"
#include "cmem.h"
2020-10-28 05:16:30 +00:00
int cosmoB_print(CState *state, int nargs, CValue *args) {
2020-10-28 05:16:30 +00:00
for (int i = 0; i < nargs; i++) {
CObjString *str = cosmoV_toString(state, args[i]);
printf("%s", cosmoO_readCString(str));
}
printf("\n");
return 0; // print doesn't return any args
2020-11-13 05:04:09 +00:00
}
int cosmoB_assert(CState *state, int nargs, CValue *args) {
if (nargs != 1) {
cosmoV_error(state, "assert() expected 1 argument, got %d!", nargs);
return 0; // nothing pushed onto the stack to return
}
if (!IS_BOOLEAN(args[0])) {
cosmoV_typeError(state, "assert()", "<boolean>", "%s", cosmoV_typeStr(args[0]));
return 0;
}
if (!cosmoV_readBoolean(args[0])) { // expression passed was false, error!
cosmoV_error(state, "assert() failed!");
} // else do nothing :)
return 0;
}
2021-01-02 20:33:11 +00:00
int cosmoB_type(CState *state, int nargs, CValue *args) {
if (nargs != 1) {
cosmoV_error(state, "type() expected 1 argument, got %d!", nargs);
return 0;
}
// push the type string to the stack
cosmoV_pushString(state, cosmoV_typeStr(args[0]));
return 1; // 1 return value, the type string :D
}
int cosmoB_pcall(CState *state, int nargs, CValue *args) {
if (nargs < 1) {
cosmoV_error(state, "pcall() expected at least 1 argument!");
return 0;
}
// call the passed callable
COSMOVMRESULT res = cosmoV_pcall(state, nargs-1, 1);
// insert false before the result
cosmo_insert(state, 0, cosmoV_newBoolean(res == COSMOVM_OK));
return 2;
}
2020-12-31 08:26:06 +00:00
// ================================================================ [STRING.*] ================================================================
// string.sub
int cosmoB_sSub(CState *state, int nargs, CValue *args) {
if (nargs == 2) {
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1])) {
cosmoV_typeError(state, "string.sub()", "<string>, <number>", "%s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
2020-12-31 08:26:06 +00:00
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
cosmo_Number indx = cosmoV_readNumber(args[1]);
// make sure we stay within memory
if (indx < 0 || indx >= str->length) {
cosmoV_error(state, "string.sub() expected index to be 0-%d, got %d!", str->length, indx);
2020-12-31 08:26:06 +00:00
return 0;
}
cosmoV_pushLString(state, str->str + ((int)indx), str->length - ((int)indx));
} else if (nargs == 3) {
if (!IS_STRING(args[0]) || !IS_NUMBER(args[1]) || !IS_NUMBER(args[2])) {
cosmoV_typeError(state, "string.sub()", "<string>, <number>, <number>", "%s, %s, %s", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]), cosmoV_typeStr(args[2]));
2020-12-31 08:26:06 +00:00
return 0;
}
CObjString *str = cosmoV_readString(args[0]);
cosmo_Number indx = cosmoV_readNumber(args[1]);
cosmo_Number length = cosmoV_readNumber(args[2]);
// make sure we stay within memory
if (indx + length < 0 || indx + length >= str->length || indx < 0 || indx >= str->length) {
cosmoV_error(state, "string.sub() expected subbed string goes out of bounds, max length is %d!", str->length);
2020-12-31 08:26:06 +00:00
return 0;
}
cosmoV_pushLString(state, str->str + ((int)indx), ((int)length));
} else {
cosmoV_error(state, "Expected 2 or 3 arguments, got %d!", nargs);
2020-12-31 08:26:06 +00:00
return 0;
}
return 1;
}
2021-01-02 20:33:11 +00:00
void cosmoB_loadLibrary(CState *state) {
// print
cosmoV_pushString(state, "print");
cosmoV_pushCFunction(state, cosmoB_print);
// assert (for unit testing)
cosmoV_pushString(state, "assert");
cosmoV_pushCFunction(state, cosmoB_assert);
// type
cosmoV_pushString(state, "type");
cosmoV_pushCFunction(state, cosmoB_type);
// pcall
cosmoV_pushString(state, "pcall");
cosmoV_pushCFunction(state, cosmoB_pcall);
2021-01-02 20:33:11 +00:00
// string.
cosmoV_pushString(state, "string");
// sub
cosmoV_pushString(state, "sub");
cosmoV_pushCFunction(state, cosmoB_sSub);
cosmoV_makeTable(state, 1);
2021-01-02 20:33:11 +00:00
// string.
// register these all to the global table
cosmoV_register(state, 5);
// make string object for CObjStrings
// sub
cosmoV_pushString(state, "sub");
cosmoV_pushCFunction(state, cosmoB_sSub);
cosmoV_makeObject(state, 1);
StkPtr obj = cosmoV_pop(state);
state->protoObjects[COBJ_STRING] = cosmoV_readObject(*obj);
2021-01-02 20:33:11 +00:00
}
// ================================================================ [DEBUG] ================================================================
int cosmoB_dsetProto(CState *state, int nargs, CValue *args) {
2020-11-13 05:04:09 +00:00
if (nargs == 2) {
CObj *obj = cosmoV_readObj(args[0]); // object to set proto too
2020-11-15 18:22:11 +00:00
CObjObject *proto = cosmoV_readObject(args[1]);
2020-11-13 05:04:09 +00:00
2020-11-15 18:22:11 +00:00
obj->proto = proto; // boom done
2020-11-13 18:54:06 +00:00
} else {
cosmoV_error(state, "Expected 2 parameters, got %d!", nargs);
}
2020-11-13 05:04:09 +00:00
return 0; // nothing
2020-11-13 05:04:09 +00:00
}
int cosmoB_dgetProto(CState *state, int nargs, CValue *args) {
2020-11-13 18:54:06 +00:00
if (nargs != 1) {
cosmoV_error(state, "Expected 1 argument, got %d!", nargs);
2020-11-13 18:54:06 +00:00
}
cosmoV_pushValue(state, cosmoV_newObj(cosmoV_readObject(args[0])->_obj.proto)); // just return the proto
return 1; // 1 result
2020-11-13 05:04:09 +00:00
}
void cosmoB_loadDebug(CState *state) {
2020-12-06 20:11:33 +00:00
// make __getter object for debug proto
cosmoV_pushString(state, "__getter");
// key & value pair
cosmoV_pushString(state, "__proto"); // key
2020-12-05 23:55:09 +00:00
cosmoV_pushCFunction(state, cosmoB_dgetProto); // value
cosmoV_makeTable(state, 1);
2020-12-06 20:11:33 +00:00
// make __setter object
cosmoV_pushString(state, "__setter");
cosmoV_pushString(state, "__proto");
2020-11-15 18:22:11 +00:00
cosmoV_pushCFunction(state, cosmoB_dsetProto);
2020-11-13 05:04:09 +00:00
cosmoV_makeTable(state, 1);
2020-12-06 20:11:33 +00:00
// we call makeObject leting it know there are 2 sets of key & value pairs on the stack
2020-12-05 23:55:09 +00:00
cosmoV_makeObject(state, 2);
// set debug protos to the debug object
state->protoObjects[COBJ_OBJECT] = cosmoV_readObject(*cosmoV_pop(state));
2021-01-02 05:06:24 +00:00
}