mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-05 08:10:05 +00:00
added string.sub()
This commit is contained in:
parent
e993cdd9fa
commit
e253129e50
@ -4,18 +4,6 @@
|
||||
#include "cobj.h"
|
||||
#include "cmem.h"
|
||||
|
||||
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);
|
||||
|
||||
cosmoV_register(state, 2);
|
||||
}
|
||||
|
||||
int cosmoB_print(CState *state, int nargs, CValue *args) {
|
||||
for (int i = 0; i < nargs; i++) {
|
||||
CObjString *str = cosmoV_toString(state, args[i]);
|
||||
@ -33,7 +21,7 @@ int cosmoB_assert(CState *state, int nargs, CValue *args) {
|
||||
}
|
||||
|
||||
if (!IS_BOOLEAN(args[0])) {
|
||||
cosmoV_error(state, "assert() expected <boolean>, got %s!", cosmoV_typeStr(args[0]));
|
||||
cosmoV_error(state, "assert() expected (<boolean>), got (%s!)", cosmoV_typeStr(args[0]));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -44,6 +32,51 @@ int cosmoB_assert(CState *state, int nargs, CValue *args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ================================================================ [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_error(state, "string.sub() expected (<string>, <number>), got (%s, %s)!", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]));
|
||||
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);
|
||||
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_error(state, "string.sub() expected (<string>, <number>, <number>), got (%s, %s, %s)!", cosmoV_typeStr(args[0]), cosmoV_typeStr(args[1]), cosmoV_typeStr(args[2]));
|
||||
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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
cosmoV_pushLString(state, str->str + ((int)indx), ((int)length));
|
||||
} else {
|
||||
cosmoV_error(state, "Expected 2 or 3 parameters, got %d!", nargs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cosmoB_dsetProto(CState *state, int nargs, CValue *args) {
|
||||
if (nargs == 2) {
|
||||
CObjObject *obj = cosmoV_readObject(args[0]); // object to set proto too
|
||||
@ -67,6 +100,29 @@ int cosmoB_dgetProto(CState *state, int nargs, CValue *args) {
|
||||
return 1; // 1 result
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// string.
|
||||
cosmoV_pushString(state, "string");
|
||||
|
||||
// sub
|
||||
cosmoV_pushString(state, "sub");
|
||||
cosmoV_pushCFunction(state, cosmoB_sSub);
|
||||
|
||||
cosmoV_makeDictionary(state, 1);
|
||||
// string.
|
||||
|
||||
// register these all to the global table
|
||||
cosmoV_register(state, 3);
|
||||
}
|
||||
|
||||
void cosmoB_loadDebug(CState *state) {
|
||||
// make __getter object for debug proto
|
||||
cosmoV_pushString(state, "__getter");
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
#include "cstate.h"
|
||||
|
||||
|
||||
COSMO_API void cosmoB_loadLibrary(CState *state);
|
||||
COSMO_API void cosmoB_loadDebug(CState *state);
|
||||
COSMO_API int cosmoB_print(CState *state, int nargs, CValue *args);
|
||||
|
@ -371,7 +371,6 @@ static void alignStack(CParseState *pstate, int alignment) {
|
||||
writePop(pstate, pstate->compiler->pushedValues - alignment);
|
||||
} else if (pstate->compiler->pushedValues < alignment) {
|
||||
error(pstate, "Missing expression!");
|
||||
printf("%d < %d\n", pstate->compiler->pushedValues, alignment);
|
||||
}
|
||||
|
||||
pstate->compiler->pushedValues = alignment;
|
||||
|
@ -673,9 +673,9 @@ int cosmoV_execute(CState *state) {
|
||||
|
||||
// call closure/cfunction
|
||||
if (IS_CFUNCTION(val)) {
|
||||
callCFunction(state, cosmoV_readCFunction(val), args, nres, 0);
|
||||
callCFunction(state, cosmoV_readCFunction(val), args, nres, -1);
|
||||
} else if (IS_CLOSURE(val)) {
|
||||
call(state, cosmoV_readClosure(val), args, nres, 0);
|
||||
call(state, cosmoV_readClosure(val), args, nres, -1);
|
||||
} else {
|
||||
cosmoV_error(state, "Cannot call non-function value %s!", cosmoV_typeStr(val));
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user