minor refactoring + non-zero int fixed

This commit is contained in:
CPunch 2020-11-05 19:53:55 -06:00
parent 4cd5c89d28
commit aa975b7330
10 changed files with 58 additions and 27 deletions

View File

@ -1,8 +1,8 @@
# make clean && make && ./bin/cosmo # make clean && make && ./bin/cosmo
CC=clang CC=clang
CFLAGS=-fPIE -g3 #-O3 CFLAGS=-fPIE -O3
LDFLAGS=-fsanitize=address LDFLAGS=#-fsanitize=address
OUT=bin/cosmo OUT=bin/cosmo
CHDR=\ CHDR=\

8
newtest.cosmo Normal file
View File

@ -0,0 +1,8 @@
-- makes a hundred thousand entries in the hashtable
var test = {}
for (var i = 0; i < 100000; i=i+1) do
test[i] = "" .. i*i
end
print(test[4]) -- should print '16'

View File

@ -178,7 +178,7 @@ void sweep(CState *state) {
prev->next = object; prev->next = object;
} }
cosmoO_freeObject(state, oldObj); cosmoO_free(state, oldObj);
} }
} }
} }

View File

@ -16,7 +16,7 @@ uint32_t hashString(const char *str, size_t sz) {
return hash; return hash;
} }
CObj *cosmoO_allocateObject(CState *state, size_t sz, CObjType type) { CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type) {
CObj* obj = (CObj*)cosmoM_xmalloc(state, sz); CObj* obj = (CObj*)cosmoM_xmalloc(state, sz);
obj->type = type; obj->type = type;
obj->isMarked = false; obj->isMarked = false;
@ -29,7 +29,7 @@ CObj *cosmoO_allocateObject(CState *state, size_t sz, CObjType type) {
return obj; return obj;
} }
void cosmoO_freeObject(CState *state, CObj* obj) { void cosmoO_free(CState *state, CObj* obj) {
#ifdef GC_DEBUG #ifdef GC_DEBUG
printf("freeing %p [", obj); printf("freeing %p [", obj);
printObject(obj); printObject(obj);
@ -71,7 +71,7 @@ void cosmoO_freeObject(CState *state, CObj* obj) {
} }
} }
bool cosmoO_equalObject(CObj* obj1, CObj* obj2) { bool cosmoO_equal(CObj* obj1, CObj* obj2) {
if (obj1->type != obj2->type) if (obj1->type != obj2->type)
return false; return false;
@ -89,14 +89,14 @@ bool cosmoO_equalObject(CObj* obj1, CObj* obj2) {
} }
CObjObject *cosmoO_newObject(CState *state, int startCap) { CObjObject *cosmoO_newObject(CState *state, int startCap) {
CObjObject *tbl = (CObjObject*)cosmoO_allocateObject(state, sizeof(CObjObject), COBJ_OBJECT); CObjObject *tbl = (CObjObject*)cosmoO_allocateBase(state, sizeof(CObjObject), COBJ_OBJECT);
cosmoT_initTable(state, &tbl->tbl, startCap); cosmoT_initTable(state, &tbl->tbl, startCap);
return tbl; return tbl;
} }
CObjFunction *cosmoO_newFunction(CState *state) { CObjFunction *cosmoO_newFunction(CState *state) {
CObjFunction *func = (CObjFunction*)cosmoO_allocateObject(state, sizeof(CObjFunction), COBJ_FUNCTION); CObjFunction *func = (CObjFunction*)cosmoO_allocateBase(state, sizeof(CObjFunction), COBJ_FUNCTION);
func->args = 0; func->args = 0;
func->upvals = 0; func->upvals = 0;
func->name = NULL; func->name = NULL;
@ -106,7 +106,7 @@ CObjFunction *cosmoO_newFunction(CState *state) {
} }
CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func) { CObjCFunction *cosmoO_newCFunction(CState *state, CosmoCFunction func) {
CObjCFunction *cfunc = (CObjCFunction*)cosmoO_allocateObject(state, sizeof(CObjCFunction), COBJ_CFUNCTION); CObjCFunction *cfunc = (CObjCFunction*)cosmoO_allocateBase(state, sizeof(CObjCFunction), COBJ_CFUNCTION);
cfunc->cfunc = func; cfunc->cfunc = func;
return cfunc; return cfunc;
} }
@ -119,7 +119,7 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func) {
upvalues[i] = NULL; upvalues[i] = NULL;
} }
CObjClosure *closure = (CObjClosure*)cosmoO_allocateObject(state, sizeof(CObjClosure), COBJ_CLOSURE); CObjClosure *closure = (CObjClosure*)cosmoO_allocateBase(state, sizeof(CObjClosure), COBJ_CLOSURE);
closure->function = func; closure->function = func;
closure->upvalues = upvalues; closure->upvalues = upvalues;
closure->upvalueCount = func->upvals; closure->upvalueCount = func->upvals;
@ -128,7 +128,7 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func) {
} }
CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val) { CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val) {
CObjUpval *upval = (CObjUpval*)cosmoO_allocateObject(state, sizeof(CObjUpval), COBJ_UPVALUE); CObjUpval *upval = (CObjUpval*)cosmoO_allocateBase(state, sizeof(CObjUpval), COBJ_UPVALUE);
upval->val = val; upval->val = val;
upval->closed = cosmoV_newNil(); upval->closed = cosmoV_newNil();
upval->next = NULL; upval->next = NULL;
@ -166,7 +166,7 @@ CObjString *cosmoO_takeString(CState *state, char *str, size_t sz) {
} }
CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uint32_t hash) { CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uint32_t hash) {
CObjString *strObj = (CObjString*)cosmoO_allocateObject(state, sizeof(CObjString), COBJ_STRING); CObjString *strObj = (CObjString*)cosmoO_allocateBase(state, sizeof(CObjString), COBJ_STRING);
strObj->str = (char*)str; strObj->str = (char*)str;
strObj->length = sz; strObj->length = sz;
strObj->hash = hash; strObj->hash = hash;
@ -179,6 +179,15 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
return strObj; return strObj;
} }
bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val) {
return cosmoT_get(&object->tbl, key, val);
}
void cosmoO_setObject(CState *state, CObjObject *object, CValue key, CValue val) {
CValue *newVal = cosmoT_insert(state, &object->tbl, key);
*newVal = val;
}
CObjString *cosmoO_toString(CState *state, CObj *val) { CObjString *cosmoO_toString(CState *state, CObj *val) {
switch (val->type) { switch (val->type) {
case COBJ_STRING: { case COBJ_STRING: {

View File

@ -84,10 +84,10 @@ static inline bool isObjType(CValue val, CObjType type) {
return IS_OBJ(val) && cosmoV_readObj(val)->type == type; return IS_OBJ(val) && cosmoV_readObj(val)->type == type;
} }
CObj *cosmoO_allocateObject(CState *state, size_t sz, CObjType type); CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type);
void cosmoO_freeObject(CState *state, CObj* obj); void cosmoO_free(CState *state, CObj* obj);
bool cosmoO_equalObject(CObj* obj1, CObj* obj2); bool cosmoO_equal(CObj* obj1, CObj* obj2);
CObjObject *cosmoO_newObject(CState *state, int startCap); CObjObject *cosmoO_newObject(CState *state, int startCap);
CObjFunction *cosmoO_newFunction(CState *state); CObjFunction *cosmoO_newFunction(CState *state);
@ -96,6 +96,9 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func);
CObjString *cosmoO_toString(CState *state, CObj *val); CObjString *cosmoO_toString(CState *state, CObj *val);
CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val); CObjUpval *cosmoO_newUpvalue(CState *state, CValue *val);
bool cosmoO_getObject(CState *state, CObjObject *object, CValue key, CValue *val);
void cosmoO_setObject(CState *state, CObjObject *object, CValue key, CValue val);
// copies the *str buffer to the heap and returns a CObjString struct which is also on the heap // copies the *str buffer to the heap and returns a CObjString struct which is also on the heap
CObjString *cosmoO_copyString(CState *state, const char *str, size_t sz); CObjString *cosmoO_copyString(CState *state, const char *str, size_t sz);
// pass an already allocated str buffer! // pass an already allocated str buffer!

View File

@ -30,7 +30,6 @@ typedef enum {
PREC_TERM, // + - PREC_TERM, // + -
PREC_FACTOR, // * / PREC_FACTOR, // * /
PREC_UNARY, // ! - PREC_UNARY, // ! -
PREC_OBJ, // {}
PREC_CALL, // . () PREC_CALL, // . ()
PREC_PRIMARY // everything else PREC_PRIMARY // everything else
} Precedence; } Precedence;
@ -515,11 +514,24 @@ static void object(CParseState *pstate, bool canAssign) {
} }
static void dot(CParseState *pstate, bool canAssign) { static void dot(CParseState *pstate, bool canAssign) {
consume(pstate, TOKEN_IDENTIFIER, "Expect property name after '.'."); consume(pstate, TOKEN_IDENTIFIER, "Expected property name after '.'.");
uint8_t name = identifierConstant(pstate, &pstate->previous); uint8_t name = identifierConstant(pstate, &pstate->previous);
writeu8(pstate, OP_LOADCONST); writeu8(pstate, OP_LOADCONST);
writeu16(pstate, name); writeu16(pstate, name);
valuePushed(pstate, 1);
if (canAssign && match(pstate, TOKEN_EQUAL)) {
expression(pstate);
writeu8(pstate, OP_SETOBJECT);
valuePopped(pstate, 2); // pops key, value & object
} else {
writeu8(pstate, OP_GETOBJECT);
// pops key & object but also pushes the field so total popped is 1
}
}
static void _index(CParseState *pstate, bool canAssign) {
expression(pstate);
consume(pstate, TOKEN_RIGHT_BRACKET, "Expected ']' to end index.");
if (canAssign && match(pstate, TOKEN_EQUAL)) { if (canAssign && match(pstate, TOKEN_EQUAL)) {
expression(pstate); expression(pstate);
@ -534,9 +546,9 @@ static void dot(CParseState *pstate, bool canAssign) {
ParseRule ruleTable[] = { ParseRule ruleTable[] = {
[TOKEN_LEFT_PAREN] = {group, call_, PREC_CALL}, [TOKEN_LEFT_PAREN] = {group, call_, PREC_CALL},
[TOKEN_RIGHT_PAREN] = {NULL, NULL, PREC_NONE}, [TOKEN_RIGHT_PAREN] = {NULL, NULL, PREC_NONE},
[TOKEN_LEFT_BRACE] = {object, NULL, PREC_OBJ}, [TOKEN_LEFT_BRACE] = {object, NULL, PREC_NONE},
[TOKEN_RIGHT_BRACE] = {NULL, NULL, PREC_NONE}, [TOKEN_RIGHT_BRACE] = {NULL, NULL, PREC_NONE},
[TOKEN_LEFT_BRACKET] = {NULL, NULL, PREC_NONE}, [TOKEN_LEFT_BRACKET] = {NULL, _index, PREC_CALL},
[TOKEN_RIGHT_BRACKET] = {NULL, NULL, PREC_NONE}, [TOKEN_RIGHT_BRACKET] = {NULL, NULL, PREC_NONE},
[TOKEN_COMMA] = {NULL, NULL, PREC_NONE}, [TOKEN_COMMA] = {NULL, NULL, PREC_NONE},
[TOKEN_DOT] = {NULL, dot, PREC_CALL}, [TOKEN_DOT] = {NULL, dot, PREC_CALL},

View File

@ -40,7 +40,7 @@ void cosmoV_freeState(CState *state) {
CObj *objs = state->objects; CObj *objs = state->objects;
while (objs != NULL) { while (objs != NULL) {
CObj *next = objs->next; CObj *next = objs->next;
cosmoO_freeObject(state, objs); cosmoO_free(state, objs);
objs = next; objs = next;
} }

View File

@ -92,7 +92,7 @@ static CTableEntry *findEntry(CTableEntry *entries, int mask, CValue key) {
static void growTbl(CState *state, CTable *tbl, size_t newCapacity) { static void growTbl(CState *state, CTable *tbl, size_t newCapacity) {
CTableEntry *entries = cosmoM_xmalloc(state, sizeof(CTableEntry) * newCapacity); CTableEntry *entries = cosmoM_xmalloc(state, sizeof(CTableEntry) * newCapacity);
int newCount; int newCount = 0;
// set all nodes as NIL : NIL // set all nodes as NIL : NIL
for (int i = 0; i < newCapacity; i++) { for (int i = 0; i < newCapacity; i++) {
@ -124,7 +124,7 @@ static void growTbl(CState *state, CTable *tbl, size_t newCapacity) {
// returns a pointer to the allocated value // returns a pointer to the allocated value
COSMO_API CValue* cosmoT_insert(CState *state, CTable *tbl, CValue key) { COSMO_API CValue* cosmoT_insert(CState *state, CTable *tbl, CValue key) {
// make sure we have enough space allocated // make sure we have enough space allocated
if (tbl->count + 1 > tbl->capacity * MAX_TABLE_FILL) { if (tbl->count + 1 > (int)(tbl->capacity * MAX_TABLE_FILL)) {
int newCap = tbl->capacity * GROW_FACTOR; int newCap = tbl->capacity * GROW_FACTOR;
growTbl(state, tbl, newCap); growTbl(state, tbl, newCap);
} }

View File

@ -26,7 +26,7 @@ bool cosmoV_equal(CValue valA, CValue valB) {
switch (valA.type) { switch (valA.type) {
case COSMO_TBOOLEAN: return valA.val.b == valB.val.b; case COSMO_TBOOLEAN: return valA.val.b == valB.val.b;
case COSMO_TNUMBER: return valA.val.num == valB.val.num; case COSMO_TNUMBER: return valA.val.num == valB.val.num;
case COSMO_TOBJ: return cosmoO_equalObject(valA.val.obj, valB.val.obj); case COSMO_TOBJ: return cosmoO_equal(valA.val.obj, valB.val.obj);
case COSMO_TNIL: return true; case COSMO_TNIL: return true;
default: default:
return false; return false;

View File

@ -350,7 +350,7 @@ int cosmoV_execute(CState *state) {
CObjObject *object = (CObjObject*)temp->val.obj; CObjObject *object = (CObjObject*)temp->val.obj;
CValue val; // to hold our value CValue val; // to hold our value
cosmoT_get(&object->tbl, *key, &val); cosmoO_getObject(state, object, *key, &val);
cosmoV_setTop(state, 2); // pops the object & the key cosmoV_setTop(state, 2); // pops the object & the key
cosmoV_pushValue(state, val); // pushes the field result cosmoV_pushValue(state, val); // pushes the field result
break; break;
@ -367,8 +367,7 @@ int cosmoV_execute(CState *state) {
} }
CObjObject *object = (CObjObject*)temp->val.obj; CObjObject *object = (CObjObject*)temp->val.obj;
CValue *newVal = cosmoT_insert(state, &object->tbl, *key); cosmoO_setObject(state, object, *key, *value);
*newVal = *value;
// pop everything off the stack // pop everything off the stack
cosmoV_setTop(state, 3); cosmoV_setTop(state, 3);