mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-24 08:01:05 +00:00
minor refactoring + non-zero int fixed
This commit is contained in:
parent
4cd5c89d28
commit
aa975b7330
4
Makefile
4
Makefile
@ -1,8 +1,8 @@
|
||||
# make clean && make && ./bin/cosmo
|
||||
|
||||
CC=clang
|
||||
CFLAGS=-fPIE -g3 #-O3
|
||||
LDFLAGS=-fsanitize=address
|
||||
CFLAGS=-fPIE -O3
|
||||
LDFLAGS=#-fsanitize=address
|
||||
OUT=bin/cosmo
|
||||
|
||||
CHDR=\
|
||||
|
8
newtest.cosmo
Normal file
8
newtest.cosmo
Normal 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'
|
@ -178,7 +178,7 @@ void sweep(CState *state) {
|
||||
prev->next = object;
|
||||
}
|
||||
|
||||
cosmoO_freeObject(state, oldObj);
|
||||
cosmoO_free(state, oldObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
27
src/cobj.c
27
src/cobj.c
@ -16,7 +16,7 @@ uint32_t hashString(const char *str, size_t sz) {
|
||||
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);
|
||||
obj->type = type;
|
||||
obj->isMarked = false;
|
||||
@ -29,7 +29,7 @@ CObj *cosmoO_allocateObject(CState *state, size_t sz, CObjType type) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
void cosmoO_freeObject(CState *state, CObj* obj) {
|
||||
void cosmoO_free(CState *state, CObj* obj) {
|
||||
#ifdef GC_DEBUG
|
||||
printf("freeing %p [", 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)
|
||||
return false;
|
||||
|
||||
@ -89,14 +89,14 @@ bool cosmoO_equalObject(CObj* obj1, CObj* obj2) {
|
||||
}
|
||||
|
||||
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);
|
||||
return tbl;
|
||||
}
|
||||
|
||||
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->upvals = 0;
|
||||
func->name = NULL;
|
||||
@ -106,7 +106,7 @@ CObjFunction *cosmoO_newFunction(CState *state) {
|
||||
}
|
||||
|
||||
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;
|
||||
return cfunc;
|
||||
}
|
||||
@ -119,7 +119,7 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func) {
|
||||
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->upvalues = upvalues;
|
||||
closure->upvalueCount = func->upvals;
|
||||
@ -128,7 +128,7 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func) {
|
||||
}
|
||||
|
||||
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->closed = cosmoV_newNil();
|
||||
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 *strObj = (CObjString*)cosmoO_allocateObject(state, sizeof(CObjString), COBJ_STRING);
|
||||
CObjString *strObj = (CObjString*)cosmoO_allocateBase(state, sizeof(CObjString), COBJ_STRING);
|
||||
strObj->str = (char*)str;
|
||||
strObj->length = sz;
|
||||
strObj->hash = hash;
|
||||
@ -179,6 +179,15 @@ CObjString *cosmoO_allocateString(CState *state, const char *str, size_t sz, uin
|
||||
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) {
|
||||
switch (val->type) {
|
||||
case COBJ_STRING: {
|
||||
|
@ -84,10 +84,10 @@ static inline bool isObjType(CValue val, CObjType type) {
|
||||
return IS_OBJ(val) && cosmoV_readObj(val)->type == type;
|
||||
}
|
||||
|
||||
CObj *cosmoO_allocateObject(CState *state, size_t sz, CObjType type);
|
||||
void cosmoO_freeObject(CState *state, CObj* obj);
|
||||
CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type);
|
||||
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);
|
||||
CObjFunction *cosmoO_newFunction(CState *state);
|
||||
@ -96,6 +96,9 @@ CObjClosure *cosmoO_newClosure(CState *state, CObjFunction *func);
|
||||
CObjString *cosmoO_toString(CState *state, CObj *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
|
||||
CObjString *cosmoO_copyString(CState *state, const char *str, size_t sz);
|
||||
// pass an already allocated str buffer!
|
||||
|
22
src/cparse.c
22
src/cparse.c
@ -30,7 +30,6 @@ typedef enum {
|
||||
PREC_TERM, // + -
|
||||
PREC_FACTOR, // * /
|
||||
PREC_UNARY, // ! -
|
||||
PREC_OBJ, // {}
|
||||
PREC_CALL, // . ()
|
||||
PREC_PRIMARY // everything else
|
||||
} Precedence;
|
||||
@ -515,11 +514,24 @@ static void object(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);
|
||||
writeu8(pstate, OP_LOADCONST);
|
||||
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)) {
|
||||
expression(pstate);
|
||||
@ -534,9 +546,9 @@ static void dot(CParseState *pstate, bool canAssign) {
|
||||
ParseRule ruleTable[] = {
|
||||
[TOKEN_LEFT_PAREN] = {group, call_, PREC_CALL},
|
||||
[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_LEFT_BRACKET] = {NULL, NULL, PREC_NONE},
|
||||
[TOKEN_LEFT_BRACKET] = {NULL, _index, PREC_CALL},
|
||||
[TOKEN_RIGHT_BRACKET] = {NULL, NULL, PREC_NONE},
|
||||
[TOKEN_COMMA] = {NULL, NULL, PREC_NONE},
|
||||
[TOKEN_DOT] = {NULL, dot, PREC_CALL},
|
||||
|
@ -40,7 +40,7 @@ void cosmoV_freeState(CState *state) {
|
||||
CObj *objs = state->objects;
|
||||
while (objs != NULL) {
|
||||
CObj *next = objs->next;
|
||||
cosmoO_freeObject(state, objs);
|
||||
cosmoO_free(state, objs);
|
||||
objs = next;
|
||||
}
|
||||
|
||||
|
@ -92,7 +92,7 @@ static CTableEntry *findEntry(CTableEntry *entries, int mask, CValue key) {
|
||||
|
||||
static void growTbl(CState *state, CTable *tbl, size_t newCapacity) {
|
||||
CTableEntry *entries = cosmoM_xmalloc(state, sizeof(CTableEntry) * newCapacity);
|
||||
int newCount;
|
||||
int newCount = 0;
|
||||
|
||||
// set all nodes as NIL : NIL
|
||||
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
|
||||
COSMO_API CValue* cosmoT_insert(CState *state, CTable *tbl, CValue key) {
|
||||
// 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;
|
||||
growTbl(state, tbl, newCap);
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ bool cosmoV_equal(CValue valA, CValue valB) {
|
||||
switch (valA.type) {
|
||||
case COSMO_TBOOLEAN: return valA.val.b == valB.val.b;
|
||||
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;
|
||||
default:
|
||||
return false;
|
||||
|
@ -350,7 +350,7 @@ int cosmoV_execute(CState *state) {
|
||||
CObjObject *object = (CObjObject*)temp->val.obj;
|
||||
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_pushValue(state, val); // pushes the field result
|
||||
break;
|
||||
@ -367,8 +367,7 @@ int cosmoV_execute(CState *state) {
|
||||
}
|
||||
|
||||
CObjObject *object = (CObjObject*)temp->val.obj;
|
||||
CValue *newVal = cosmoT_insert(state, &object->tbl, *key);
|
||||
*newVal = *value;
|
||||
cosmoO_setObject(state, object, *key, *value);
|
||||
|
||||
// pop everything off the stack
|
||||
cosmoV_setTop(state, 3);
|
||||
|
Loading…
Reference in New Issue
Block a user