minor refactor, fixed many warnings

This commit is contained in:
CPunch 2021-01-01 23:06:24 -06:00
parent bccabdebd7
commit 7e9b61e018
26 changed files with 53 additions and 49 deletions

View File

@ -37,7 +37,7 @@ void freeChunk(CState* state, CChunk *chunk) {
int addConstant(CState* state, CChunk *chunk, CValue value) { int addConstant(CState* state, CChunk *chunk, CValue value) {
// before adding the constant, check if we already have it // before adding the constant, check if we already have it
for (int i = 0; i < chunk->constants.count; i++) { for (size_t i = 0; i < chunk->constants.count; i++) {
if (cosmoV_equal(value, chunk->constants.values[i])) if (cosmoV_equal(value, chunk->constants.values[i]))
return i; // we already have a matching constant! return i; // we already have a matching constant!
} }

View File

@ -32,7 +32,7 @@ int u8u16OperandInstruction(const char *name, CChunk *chunk, int offset) {
return offset + 4; // op + u8 + u16 return offset + 4; // op + u8 + u16
} }
int constInstruction(const char *name, CChunk *chunk, int offset, int indent) { int constInstruction(const char *name, CChunk *chunk, int offset) {
int index = readu16Chunk(chunk, offset + 1); int index = readu16Chunk(chunk, offset + 1);
printf("%-16s [%05d] - ", name, index); printf("%-16s [%05d] - ", name, index);
CValue val = chunk->constants.values[index]; CValue val = chunk->constants.values[index];
@ -48,7 +48,7 @@ void disasmChunk(CChunk *chunk, const char *name, int indent) {
printIndent(indent); printIndent(indent);
printf("===[[ %s ]]===\n", name); printf("===[[ %s ]]===\n", name);
for (int offset = 0; offset < chunk->count;) { for (size_t offset = 0; offset < chunk->count;) {
offset = disasmInstr(chunk, offset, indent); offset = disasmInstr(chunk, offset, indent);
printf("\n"); printf("\n");
} }
@ -69,11 +69,11 @@ int disasmInstr(CChunk *chunk, int offset, int indent) {
switch (i) { switch (i) {
case OP_LOADCONST: case OP_LOADCONST:
return constInstruction("OP_LOADCONST", chunk, offset, indent); return constInstruction("OP_LOADCONST", chunk, offset);
case OP_SETGLOBAL: case OP_SETGLOBAL:
return constInstruction("OP_SETGLOBAL", chunk, offset, indent); return constInstruction("OP_SETGLOBAL", chunk, offset);
case OP_GETGLOBAL: case OP_GETGLOBAL:
return constInstruction("OP_GETGLOBAL", chunk, offset, indent); return constInstruction("OP_GETGLOBAL", chunk, offset);
case OP_SETLOCAL: case OP_SETLOCAL:
return u8OperandInstruction("OP_SETLOCAL", chunk, offset); return u8OperandInstruction("OP_SETLOCAL", chunk, offset);
case OP_GETLOCAL: case OP_GETLOCAL:

View File

@ -155,7 +155,7 @@ CTokenType identifierType(CLexState *state) {
int length = state->currentChar - state->startChar; int length = state->currentChar - state->startChar;
// check against reserved word list // check against reserved word list
for (int i = 0; i < sizeof(reservedWords) / sizeof(CReservedWord); i++) { for (size_t i = 0; i < sizeof(reservedWords) / sizeof(CReservedWord); i++) {
// it matches the reserved word // it matches the reserved word
if (reservedWords[i].len == length && memcmp(state->startChar, reservedWords[i].word, length) == 0) if (reservedWords[i].len == length && memcmp(state->startChar, reservedWords[i].word, length) == 0)
return reservedWords[i].type; return reservedWords[i].type;

View File

@ -78,7 +78,7 @@ void tableRemoveWhite(CState *state, CTable *tbl) {
} }
void markArray(CState *state, CValueArray *array) { void markArray(CState *state, CValueArray *array) {
for (int i = 0; i < array->count; i++) { for (size_t i = 0; i < array->count; i++) {
markValue(state, array->values[i]); markValue(state, array->values[i]);
} }
} }
@ -132,7 +132,9 @@ void blackenObject(CState *state, CObj *obj) {
break; break;
} }
default: default:
printf("Unknown type in blackenObject with %p, type %d\n", obj, obj->type); #ifdef GC_DEBUG
printf("Unknown type in blackenObject with %p, type %d\n", (void*)obj, obj->type);
#endif
break; break;
} }
} }

View File

@ -11,7 +11,7 @@ uint32_t hashString(const char *str, size_t sz) {
uint32_t hash = sz; uint32_t hash = sz;
size_t step = (sz>>5)+1; size_t step = (sz>>5)+1;
for (int i = sz; i >= step; i-=step) for (size_t i = sz; i >= step; i-=step)
hash = ((hash << 5) + (hash>>2)) + str[i-1]; hash = ((hash << 5) + (hash>>2)) + str[i-1];
return hash; return hash;
@ -394,12 +394,12 @@ CObjString *cosmoO_toString(CState *state, CObj *obj) {
} }
case COBJ_OBJECT: { // TODO: maybe not safe?? case COBJ_OBJECT: { // TODO: maybe not safe??
char buf[64]; char buf[64];
int sz = sprintf(buf, "<obj> %p", obj) + 1; // +1 for the null character int sz = sprintf(buf, "<obj> %p", (void*)obj) + 1; // +1 for the null character
return cosmoO_copyString(state, buf, sz); return cosmoO_copyString(state, buf, sz);
} }
case COBJ_DICT: { case COBJ_DICT: {
char buf[64]; char buf[64];
int sz = sprintf(buf, "<dict> %p", obj) + 1; // +1 for the null character int sz = sprintf(buf, "<dict> %p", (void*)obj) + 1; // +1 for the null character
return cosmoO_copyString(state, buf, sz); return cosmoO_copyString(state, buf, sz);
} }
default: default:
@ -415,17 +415,17 @@ void printObject(CObj *o) {
break; break;
} }
case COBJ_OBJECT: { case COBJ_OBJECT: {
printf("<obj> %p", o); printf("<obj> %p", (void*)o);
break; break;
} }
case COBJ_DICT: { case COBJ_DICT: {
CObjDict *dict = (CObjDict*)o; CObjDict *dict = (CObjDict*)o;
printf("<dict> %p", dict); printf("<dict> %p", (void*)dict);
break; break;
} }
case COBJ_UPVALUE: { case COBJ_UPVALUE: {
CObjUpval *upval = (CObjUpval*)o; CObjUpval *upval = (CObjUpval*)o;
printf("<upvalue %p> -> ", upval->val); printf("<upvalue %p> -> ", (void*)upval->val);
printValue(*upval->val); printValue(*upval->val);
break; break;
} }
@ -444,17 +444,17 @@ void printObject(CObj *o) {
} }
case COBJ_CFUNCTION: { case COBJ_CFUNCTION: {
CObjCFunction *objCFunc = (CObjCFunction*)o; CObjCFunction *objCFunc = (CObjCFunction*)o;
printf("<c function> %p", objCFunc->cfunc); printf("<c function> %p", (void*)objCFunc->cfunc);
break; break;
} }
case COBJ_METHOD: { case COBJ_METHOD: {
CObjMethod *method = (CObjMethod*)o; CObjMethod *method = (CObjMethod*)o;
printf("<method> %p : ", method->obj); printf("<method> %p : ", (void*)method->obj);
printValue(method->func); printValue(method->func);
break; break;
} }
default: default:
printf("<unkn obj %p>", o); printf("<unkn obj %p>", (void*)o);
} }
} }

View File

@ -22,7 +22,7 @@ typedef enum {
COBJ_UPVALUE, COBJ_UPVALUE,
} CObjType; } CObjType;
#define CommonHeader CObj _obj; #define CommonHeader CObj _obj
#define readFlag(x, flag) (x & (1u << flag)) #define readFlag(x, flag) (x & (1u << flag))
#define setFlagOn(x, flag) (x |= (1u << flag)) #define setFlagOn(x, flag) (x |= (1u << flag))

View File

@ -287,8 +287,10 @@ static uint16_t identifierConstant(CParseState *pstate, CToken *name) {
} }
static void addLocal(CParseState *pstate, CToken name) { static void addLocal(CParseState *pstate, CToken name) {
if (pstate->compiler->localCount > UINT8_MAX) if (pstate->compiler->localCount > UINT8_MAX) {
return error(pstate, "UInt overflow! Too many locals in scope!"); error(pstate, "UInt overflow! Too many locals in scope!");
return;
}
Local *local = &pstate->compiler->locals[pstate->compiler->localCount++]; Local *local = &pstate->compiler->locals[pstate->compiler->localCount++];
local->name = name; local->name = name;

View File

@ -73,7 +73,7 @@ uint32_t getValueHash(CValue *val) {
return 0; return 0;
memcpy(buf, &num, sizeof(buf)); memcpy(buf, &num, sizeof(buf));
for (int i = 0; i < sizeof(cosmo_Number)/sizeof(uint32_t); i++) buf[0] += buf[i]; for (size_t i = 0; i < sizeof(cosmo_Number)/sizeof(uint32_t); i++) buf[0] += buf[i];
return buf[0]; return buf[0];
} }
// TODO: add support for other types // TODO: add support for other types
@ -216,7 +216,7 @@ COSMO_API int cosmoT_count(CTable *tbl) {
return tbl->count - tbl->tombstones; return tbl->count - tbl->tombstones;
} }
CObjString *cosmoT_lookupString(CTable *tbl, const char *str, size_t length, uint32_t hash) { CObjString *cosmoT_lookupString(CTable *tbl, const char *str, int length, uint32_t hash) {
if (tbl->count == 0) return 0; // sanity check if (tbl->count == 0) return 0; // sanity check
uint32_t indx = hash & (tbl->capacity - 1); // since we know the capacity will *always* be a power of 2, we can use bitwise & to perform a MUCH faster mod operation uint32_t indx = hash & (tbl->capacity - 1); // since we know the capacity will *always* be a power of 2, we can use bitwise & to perform a MUCH faster mod operation

View File

@ -22,7 +22,7 @@ COSMO_API void cosmoT_addTable(CState *state, CTable *from, CTable *to);
COSMO_API CValue *cosmoT_insert(CState *state, CTable *tbl, CValue key); COSMO_API CValue *cosmoT_insert(CState *state, CTable *tbl, CValue key);
COSMO_API int cosmoT_count(CTable *tbl); COSMO_API int cosmoT_count(CTable *tbl);
CObjString *cosmoT_lookupString(CTable *tbl, const char *str, size_t length, uint32_t hash); CObjString *cosmoT_lookupString(CTable *tbl, const char *str, int length, uint32_t hash);
bool cosmoT_checkShrink(CState *state, CTable *tbl); bool cosmoT_checkShrink(CState *state, CTable *tbl);
bool cosmoT_get(CTable *tbl, CValue key, CValue *val); bool cosmoT_get(CTable *tbl, CValue key, CValue *val);
bool cosmoT_remove(CState *state, CTable *tbl, CValue key); bool cosmoT_remove(CState *state, CTable *tbl, CValue key);