Add load(), load_library(), lisp std
diff --git a/src/lisp/lib/std.c b/src/lisp/lib/std.c
index 028dd3d..c7efca0 100644
--- a/src/lisp/lib/std.c
+++ b/src/lisp/lib/std.c
@@ -1,5 +1,7 @@
#include "std.h"
+#include "../plat/plat.h"
#include <stdlib.h>
+#include <string.h>
value_t l_plus(value_t a, value_t b)
{
@@ -78,4 +80,37 @@
add_function(env, "print", l_printval, 1, NS_FUNCTION);
add_function(env, "apply", l_apply, 2, NS_FUNCTION);
+
+ if (!load_library(env, "std"))
+ {
+ err("Could not load library `std`, make sure your $LISP_LIBRARY_PATH is correct.");
+ }
+}
+
+bool load_library(struct environment *env, char *name)
+{
+ char *lib_paths = getenv("LISP_LIBRARY_PATH");
+
+ if (!lib_paths)
+ lib_paths = "/lib/lisp";
+
+ for (char *p = strtok(lib_paths, ":"); p; p = strtok(NULL, ":"))
+ {
+ static char path[512];
+ snprintf(path, 512, "%s/%s.lisp", p, name);
+
+ if (file_exists(path))
+ {
+ return load(env, path);
+ }
+
+ snprintf(path, 512, "%s/%s/%s.lisp", p, name, name);
+
+ if (file_exists(path))
+ {
+ return load(env, path);
+ }
+ }
+
+ return false;
}
diff --git a/src/lisp/lib/std.h b/src/lisp/lib/std.h
index 9070de8..a117b5d 100644
--- a/src/lisp/lib/std.h
+++ b/src/lisp/lib/std.h
@@ -8,3 +8,4 @@
void add_function(struct environment *env, char *name, void *func, int nargs, enum namespace ns);
void load_std(struct environment *env);
+bool load_library(struct environment *env, char *name);