Skip to content

Commit

Permalink
Added new Map type that can use most types as a key
Browse files Browse the repository at this point in the history
It is implemented as a sparse hashmap and will probably
replace the old Tree type in the future
  • Loading branch information
Melchizedek6809 committed Apr 25, 2024
1 parent 83c8eee commit efbd151
Show file tree
Hide file tree
Showing 20 changed files with 12,711 additions and 12,151 deletions.
2 changes: 1 addition & 1 deletion benchmark/recfib/fib.nuj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
((= 1 n) 1)
(#t (+ (fib (- n 1)) (fib (- n 2))))))

(pfmtln "fib(40) is: {}\nGC Runs: {}" (fib 40) (garbage-collection-runs))
(pfmtln "fib(30) is: {}\nGC Runs: {}" (fib 30) (garbage-collection-runs))
4 changes: 2 additions & 2 deletions benchmark/recfib/fib.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
((= 1 n) 1)
(#t (+ (fib (- n 1)) (fib (- n 2))))))

(display "fib(40) = ")
(display (fib 40))
(display "fib(30) = ")
(display (fib 30))
(display "\n")
14 changes: 7 additions & 7 deletions bin/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#endif

/* Add environment key/value pair to tree T */
static lTree *addVar(const char *e, lTree *t){
static void addVar(const char *e, lMap *t){
int endOfKey, endOfString;
for(endOfKey=0;e[endOfKey] != '=';endOfKey++){}
for(endOfString=endOfKey+1;e[endOfString];endOfString++){}
lSymbol *sym = lSymSL(e,endOfKey);
lVal v = lValString(&e[endOfKey+1]);
return lTreeInsert(t, sym, v);
lMapSet(t, lValKeywordS(sym), v);
}

#if (defined(__MSYS__)) || (defined(__MINGW32__)) || (defined(_WIN32))
Expand All @@ -26,23 +26,23 @@ void lRedefineEnvironment(lClosure *c){
lTree *t = NULL;
LPCH env = GetEnvironmentStrings();
while(*env){
t = addVar(env,t);
addVar(env,t);
while(*env++){}
}
lDefineClosureSym(c,lSymS("System/Environment"), lValTree(t));
lDefineClosureSym(c,lSymS("System/Environment"), lValMap(t));
}

#else
extern char **environ;
/* Add Environment args to `environment/variables` */
void lRedefineEnvironment(lClosure *c){
lTree *t = NULL;
lMap *t = lMapAllocRaw();
#ifdef __wasi__
t = addVar("PATH=",t); // Necessary so that tests don't fail
#endif
for(int i=0;environ[i];i++){
t = addVar(environ[i],t);
addVar(environ[i], t);
}
lDefineClosureSym(c,lSymS("System/Environment"), lValTree(t));
lDefineClosureSym(c,lSymS("System/Environment"), lValMap(t));
}
#endif
24,309 changes: 12,247 additions & 12,062 deletions bootstrap/image.c

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/core-operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ static i64 lValToId(lVal v){
case ltBuffer: return v.vBuffer - lBufferList;
case ltArray: return v.vArray - lArrayList;
case ltTree: return v.vTree - lTreeRootList;
case ltMap: return v.vMap - lMapList;
case ltBytecodeArr: return v.vBytecodeArr - lBytecodeArrayList;
case ltKeyword:
case ltSymbol: return v.vSymbol - lSymbolList;
Expand Down Expand Up @@ -204,10 +205,15 @@ void lInit(){
lOperationsCore();
lOperationsArray();
lOperationsTree();
lOperationsMap();
lOperationsBytecode();
lOperationsString();
}

static lVal lnfSymbolTable(){
return lValMap(lSymbolTable);
}

void lOperationsCore(){
lAddNativeFuncV("quote", "(v)", "Return v as is without evaluating", lnfQuote, NFUNC_PURE);
lAddNativeFuncV("read", "(str)", "Read and Parses STR as an S-Expression", lnfRead, NFUNC_PURE);
Expand All @@ -231,9 +237,11 @@ void lOperationsCore(){

lAddNativeFuncR("array/new", "args", "Create a new array from ...ARGS", lnfArrNew, 0);
lAddNativeFuncR("tree/new", "plist", "Return a new tree", lnfTreeNew, 0);
lAddNativeFuncR("map/new", "plist", "Return a new map", lnfMapNew, 0);

lAddNativeFuncV("image/serialize", "(val)", "Serializes val into a binary representation that can be stored", lnfSerialize, 0);
lAddNativeFuncV("image/deserialize", "(buf)", "Deserializes buf into a value", lnfDeserialize, 0);
lAddNativeFunc("symbol-table", "()", "Returns the global symbol table", lnfSymbolTable, 0);

lAddNativeMethodV(&lClassList[ltNil], lSymLTString, "(self)", lnfNilToString, NFUNC_PURE);
lAddNativeMethodV(&lClassList[ltInt], lSymLTString, "(self)", lnfIntToString, NFUNC_PURE);
Expand Down
23 changes: 23 additions & 0 deletions lib/garbage-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ T##MarkMap[ci] = 1
static void lValGCMark (lVal v);
static void lBufferGCMark (const lBuffer *v);
static void lBufferViewGCMark (const lBufferView *v);
static void lMapGCMark (const lMap *v);
static void lTreeGCMark (const lTree *v);
static void lTreeRootGCMark (const lTreeRoot *v);
static void lClosureGCMark (const lClosure *c);
Expand Down Expand Up @@ -92,6 +93,15 @@ static void lTreeFree(lTree *t){
lTreeMarkMap[t - lTreeList] = 2;
}

static void lMapFree(lMap *t){
free(t->entries);
t->entries = NULL;
t->nextFree = lMapFFree;
lMapFFree = t;
lMapActive--;
lMapMarkMap[t - lMapList] = 2;
}

static void lTreeRootFree(lTreeRoot *t){
t->nextFree = lTreeRootFFree;
lTreeRootFFree = t;
Expand Down Expand Up @@ -165,6 +175,9 @@ static void lValGCMark(lVal v){
case ltSymbol:
lSymbolGCMark(v.vSymbol);
break;
case ltMap:
lMapGCMark(v.vMap);
break;
case ltTree:
lTreeRootGCMark(v.vTree);
break;
Expand All @@ -183,6 +196,15 @@ static void lValGCMark(lVal v){
}
}

static void lMapGCMark(const lMap *v){
markerPrefix(lMap);
for(int i=0;i<v->size;i++){
if(v->entries[i].key.type == ltNil){continue;}
lValGCMark(v->entries[i].key);
lValGCMark(v->entries[i].val);
}
}

static void lTreeGCMark(const lTree *v){
markerPrefix(lTree);

Expand Down Expand Up @@ -233,6 +255,7 @@ static void lRootsMark(){
for(uint i=0;i<lNFuncMax;i++){
lNFuncGCMark(&lNFuncList[i]);
}
lMapGCMark(lSymbolTable);
}

lSymbol *lRootsSymbolPush(lSymbol *v){
Expand Down
4 changes: 4 additions & 0 deletions lib/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ static lVal lBufferViewRef(lVal car, size_t i){

lVal lGenericRef(lVal col, lVal key){
switch(col.type){
case ltMap:
return lMapRef(col.vMap, key);
case ltPair:
reqNaturalInt(key);
for(int i=0;i<key.vInt;i++){
Expand Down Expand Up @@ -160,6 +162,8 @@ static lVal lBufferViewSet(lVal car, size_t i, lVal v){

lVal lGenericSet(lVal col, lVal key, lVal v){
switch(col.type){
case ltMap:
return lMapSet(col.vMap, key, v);
case ltBytecodeArr: {
const lBytecodeArray *arr = col.vBytecodeArr;
reqNaturalInt(key);
Expand Down
Loading

0 comments on commit efbd151

Please sign in to comment.