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 dcf3428
Show file tree
Hide file tree
Showing 24 changed files with 12,561 additions and 11,942 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
64 changes: 31 additions & 33 deletions bin/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,50 +110,48 @@ static lVal lnfFileStat(lVal aPath){
hFind = FindFirstFile(lBufferData(aPath.vString), &ffd);


lVal ret = lValTree(NULL);
lTreeRoot *t = ret.vTree;
t->root = lTreeInsert(t->root, lsError, lValBool(INVALID_HANDLE_VALUE == hFind));
lMap *map = lMapAllocRaw();
lMapSet(map, lValKeywordS(lsError, lValBool(INVALID_HANDLE_VALUE == hFind)));
if (likely(INVALID_HANDLE_VALUE != hFind)) {
LARGE_INTEGER filesize;
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
t->root = lTreeInsert(t->root, lsSize, lValInt(filesize.QuadPart));
t->root = lTreeInsert(t->root, lsAccessTime, lValInt(FileTime_to_POSIX(ffd.ftLastAccessTime)));
t->root = lTreeInsert(t->root, lsCreationTime, lValInt(FileTime_to_POSIX(ffd.ftCreationTime)));
t->root = lTreeInsert(t->root, lsModificationTime, lValInt(FileTime_to_POSIX(ffd.ftLastWriteTime)));
t->root = lTreeInsert(t->root, lsUserID, lValInt(1000));
t->root = lTreeInsert(t->root, lsGroupID, lValInt(1000));

t->root = lTreeInsert(t->root, lsRegularFile, lValBool(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)));
t->root = lTreeInsert(t->root, lsDirectory, lValBool(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
t->root = lTreeInsert(t->root, lsCharacterDevice, lValBool(false));
t->root = lTreeInsert(t->root, lsBlockDevice, lValBool(false));
t->root = lTreeInsert(t->root, lsNamedPipe, lValBool(false));
lMapSet(map, lValKeywordS(lsSize), lValInt(filesize.QuadPart));
lMapSet(map, lValKeywordS(lsAccessTime), lValInt(FileTime_to_POSIX(ffd.ftLastAccessTime)));
lMapSet(map, lValKeywordS(lsCreationTime), lValInt(FileTime_to_POSIX(ffd.ftCreationTime)));
lMapSet(map, lValKeywordS(lsModificationTime), lValInt(FileTime_to_POSIX(ffd.ftLastWriteTime)));
lMapSet(map, lValKeywordS(lsUserID), lValInt(1000));
lMapSet(map, lValKeywordS(lsGroupID), lValInt(1000));

lMapSet(map, lValKeywordS(lsRegularFile), lValBool(!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)));
lMapSet(map, lValKeywordS(lsDirectory), lValBool(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
lMapSet(map, lValKeywordS(lsCharacterDevice), lValBool(false));
lMapSet(map, lValKeywordS(lsBlockDevice), lValBool(false));
lMapSet(map, lValKeywordS(lsNamedPipe), lValBool(false));
}
return ret;
return lValMap(map);
#else
struct stat statbuf;
int err = stat(lBufferData(aPath.vString), &statbuf);
lVal ret = lValTree(NULL);
lTreeRoot *t = ret.vTree;
t->root = lTreeInsert(t->root, lsError, lValBool(err));
lMap *map = lMapAllocRaw();
lMapSet(map, lValKeywordS(lsError), lValBool(err));
if(err){
t->root = lTreeInsert(t->root, lsErrorNumber, lValInt(errno));
t->root = lTreeInsert(t->root, lsErrorText, lValString(strerror(errno)));
lMapSet(map, lValKeywordS(lsErrorNumber), lValInt(errno));
lMapSet(map, lValKeywordS(lsErrorText), lValString(strerror(errno)));
}else{
t->root = lTreeInsert(t->root, lsUserID, lValInt(statbuf.st_uid));
t->root = lTreeInsert(t->root, lsGroupID, lValInt(statbuf.st_gid));
t->root = lTreeInsert(t->root, lsSize, lValInt(statbuf.st_size));
t->root = lTreeInsert(t->root, lsAccessTime, lValInt(statbuf.st_atime));
t->root = lTreeInsert(t->root, lsModificationTime, lValInt(statbuf.st_mtime));

t->root = lTreeInsert(t->root, lsRegularFile, lValBool(S_ISREG(statbuf.st_mode)));
t->root = lTreeInsert(t->root, lsDirectory, lValBool(S_ISDIR(statbuf.st_mode)));
t->root = lTreeInsert(t->root, lsCharacterDevice, lValBool(S_ISCHR(statbuf.st_mode)));
t->root = lTreeInsert(t->root, lsBlockDevice, lValBool(S_ISBLK(statbuf.st_mode)));
t->root = lTreeInsert(t->root, lsNamedPipe, lValBool(S_ISFIFO(statbuf.st_mode)));
lMapSet(map, lValKeywordS(lsUserID), lValInt(statbuf.st_uid));
lMapSet(map, lValKeywordS(lsGroupID), lValInt(statbuf.st_gid));
lMapSet(map, lValKeywordS(lsSize), lValInt(statbuf.st_size));
lMapSet(map, lValKeywordS(lsAccessTime), lValInt(statbuf.st_atime));
lMapSet(map, lValKeywordS(lsModificationTime), lValInt(statbuf.st_mtime));

lMapSet(map, lValKeywordS(lsRegularFile), lValBool(S_ISREG(statbuf.st_mode)));
lMapSet(map, lValKeywordS(lsDirectory), lValBool(S_ISDIR(statbuf.st_mode)));
lMapSet(map, lValKeywordS(lsCharacterDevice), lValBool(S_ISCHR(statbuf.st_mode)));
lMapSet(map, lValKeywordS(lsBlockDevice), lValBool(S_ISBLK(statbuf.st_mode)));
lMapSet(map, lValKeywordS(lsNamedPipe), lValBool(S_ISFIFO(statbuf.st_mode)));
}
return ret;
return lValMap(map);
#endif
}

Expand Down
Loading

0 comments on commit dcf3428

Please sign in to comment.