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

@ -146,4 +146,4 @@ void cosmoB_loadDebug(CState *state) {
// set debug proto to the debug object // set debug proto to the debug object
state->protoObj = cosmoV_readObject(*cosmoV_pop(state)); state->protoObj = cosmoV_readObject(*cosmoV_pop(state));
} }

View File

@ -8,4 +8,4 @@ COSMO_API void cosmoB_loadDebug(CState *state);
COSMO_API int cosmoB_print(CState *state, int nargs, CValue *args); COSMO_API int cosmoB_print(CState *state, int nargs, CValue *args);
COSMO_API int cosmoB_assert(CState *state, int nargs, CValue *args); COSMO_API int cosmoB_assert(CState *state, int nargs, CValue *args);
#endif #endif

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

@ -36,4 +36,4 @@ static inline uint16_t readu16Chunk(CChunk *chunk, int offset) {
return *((uint16_t*)(&chunk->buf[offset])); return *((uint16_t*)(&chunk->buf[offset]));
} }
#endif #endif

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:
@ -189,4 +189,4 @@ int disasmInstr(CChunk *chunk, int offset, int indent) {
return 1; return 1;
} }

View File

@ -6,4 +6,4 @@
COSMO_API void disasmChunk(CChunk *chunk, const char *name, int indent); COSMO_API void disasmChunk(CChunk *chunk, const char *name, int indent);
COSMO_API int disasmInstr(CChunk *chunk, int offset, int indent); COSMO_API int disasmInstr(CChunk *chunk, int offset, int indent);
#endif #endif

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;
@ -336,4 +336,4 @@ CToken cosmoL_scanToken(CLexState *state) {
} }
return makeError(state, "Unknown symbol!"); return makeError(state, "Unknown symbol!");
} }

View File

@ -100,4 +100,4 @@ void cosmoL_freeLexState(CState *state, CLexState *lstate);
CToken cosmoL_scanToken(CLexState *state); CToken cosmoL_scanToken(CLexState *state);
#endif #endif

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;
} }
} }
@ -300,4 +302,4 @@ COSMO_API void cosmoM_removeRoot(CState *state, CObj *oldRoot) {
prev = root; prev = root;
root = root->nextRoot; root = root->nextRoot;
} }
} }

View File

@ -67,4 +67,4 @@ static inline void *cosmoM_xmalloc(CState *state, size_t sz) {
return cosmoM_reallocate(state, NULL, 0, sz); return cosmoM_reallocate(state, NULL, 0, sz);
} }
#endif #endif

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);
} }
} }
@ -472,4 +472,4 @@ const char *cosmoO_typeStr(CObj* obj) {
default: default:
return "<unkn obj>"; // TODO: maybe panic? could be a malformed object :eyes: return "<unkn obj>"; // TODO: maybe panic? could be a malformed object :eyes:
} }
} }

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))
@ -169,4 +169,4 @@ const char *cosmoO_typeStr(CObj* obj);
#define cosmoO_readCString(x) ((CObjString*)x)->str #define cosmoO_readCString(x) ((CObjString*)x)->str
#endif #endif

View File

@ -1 +1 @@
#include "coperators.h" #include "coperators.h"

View File

@ -63,4 +63,4 @@ typedef enum {
OP_RETURN OP_RETURN
} COPCODE; // there can be a max of 256 instructions } COPCODE; // there can be a max of 256 instructions
#endif #endif

View File

@ -44,4 +44,4 @@ typedef uint8_t INSTRUCTION;
#define CERROR(err) \ #define CERROR(err) \
printf("%s : %s\n", "[ERROR]", err) printf("%s : %s\n", "[ERROR]", err)
#endif #endif

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;
@ -1494,4 +1496,4 @@ CObjFunction* cosmoP_compileString(CState *state, const char *source, const char
freeParseState(&parser); freeParseState(&parser);
cosmoM_unfreezeGC(state); cosmoM_unfreezeGC(state);
return resFunc; return resFunc;
} }

View File

@ -7,4 +7,4 @@
// compiles source into CChunk, if NULL is returned, a syntaxical error has occurred and pushed onto the stack // compiles source into CChunk, if NULL is returned, a syntaxical error has occurred and pushed onto the stack
CObjFunction* cosmoP_compileString(CState *state, const char *source, const char *module); CObjFunction* cosmoP_compileString(CState *state, const char *source, const char *module);
#endif #endif

View File

@ -108,4 +108,4 @@ void cosmoV_printStack(CState *state) {
printValue(*top); printValue(*top);
printf("\n"); printf("\n");
} }
} }

View File

@ -58,4 +58,4 @@ COSMO_API void cosmoV_register(CState *state, int pairs);
COSMO_API void cosmoV_freeState(CState *state); COSMO_API void cosmoV_freeState(CState *state);
COSMO_API void cosmoV_printStack(CState *state); COSMO_API void cosmoV_printStack(CState *state);
#endif #endif

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
@ -248,4 +248,4 @@ void cosmoT_printTable(CTable *tbl, const char *name) {
printf("\n"); printf("\n");
} }
} }
} }

View File

@ -22,11 +22,11 @@ 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);
void cosmoT_printTable(CTable *tbl, const char *name); void cosmoT_printTable(CTable *tbl, const char *name);
#endif #endif

View File

@ -85,4 +85,4 @@ void printValue(CValue val) {
default: default:
printf("<unkn val>"); printf("<unkn val>");
} }
} }

View File

@ -116,4 +116,4 @@ COSMO_API bool cosmoV_equal(CValue valA, CValue valB);
COSMO_API CObjString *cosmoV_toString(CState *state, CValue val); COSMO_API CObjString *cosmoV_toString(CState *state, CValue val);
COSMO_API const char *cosmoV_typeStr(CValue val); // return constant char array for corresponding type COSMO_API const char *cosmoV_typeStr(CValue val); // return constant char array for corresponding type
#endif #endif

View File

@ -1059,4 +1059,4 @@ int cosmoV_execute(CState *state) {
return -1; return -1;
} }
#undef NUMBEROP #undef NUMBEROP

View File

@ -92,4 +92,4 @@ static inline void cosmoV_pushString(CState *state, const char *str) {
cosmoV_pushLString(state, str, strlen(str)); cosmoV_pushLString(state, str, strlen(str));
} }
#endif #endif

View File

@ -134,4 +134,4 @@ int main(int argc, const char *argv[]) {
} }
return 0; return 0;
} }