diff --git a/.gitignore b/.gitignore index 1cd9d6e..ec4f21c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /vs/*/* -/build *.ilk *.aux *.fls @@ -7,6 +6,7 @@ *.toc *.fdb_latexmk *.pdb +*.obj *.exe *.user *.psess diff --git a/bin/a.slime b/bin/a.slime deleted file mode 100644 index 80a75f4..0000000 --- a/bin/a.slime +++ /dev/null @@ -1 +0,0 @@ -(define-syntax (e x) `(+ 1 ,x)) diff --git a/bin/b.slime b/bin/b.slime deleted file mode 100644 index 93db67e..0000000 --- a/bin/b.slime +++ /dev/null @@ -1,6 +0,0 @@ -(import "a.slime") - -(define (f) (e 2)) -(show f) -(f) -(show f) diff --git a/bin/c.slime b/bin/c.slime deleted file mode 100644 index 737273e..0000000 --- a/bin/c.slime +++ /dev/null @@ -1,3 +0,0 @@ -(import "b.slime") - -(printf (e 3)) diff --git a/build.bat b/build.bat index 6432008..9af70c7 100644 --- a/build.bat +++ b/build.bat @@ -3,27 +3,23 @@ pushd %~dp0 set exeName=slime.exe -set binDir=bin -mkdir build 2>nul -pushd build +pushd bin taskkill /F /IM %exeName% > NUL 2> NUL echo ---------- Compiling ---------- call timecmd cl ../src/main.cpp /std:c++latest /Fe%exeName% /W3 /Zi /nologo /EHsc /link /NODEFAULTLIB:libucrt libucrtd.lib +popd if %errorlevel% == 0 ( echo. echo Done echo. - pushd ..\bin - call timecmd ..\build\slime.exe --run-tests - popd + call timecmd bin\slime.exe --run-tests ) else ( echo. echo Fuckin' ell ) popd -popd diff --git a/build/libucrtd.lib b/build/libucrtd.lib deleted file mode 100644 index 4607828..0000000 Binary files a/build/libucrtd.lib and /dev/null differ diff --git a/build/slime.exe.dbg b/build/slime.exe.dbg deleted file mode 100644 index 16fa920..0000000 Binary files a/build/slime.exe.dbg and /dev/null differ diff --git a/src/built_ins.cpp b/src/built_ins.cpp index 81746f1..a20c110 100644 --- a/src/built_ins.cpp +++ b/src/built_ins.cpp @@ -41,14 +41,14 @@ proc built_in_load(String* file_name, Environment* env) -> Lisp_Object* { // save the current working directory // get the direction of the exe - char* exe_path = exe_dir(); + char* exe_path = get_exe_dir(); defer { free(exe_path); }; fullpath[0] = '\0'; sprintf(fullpath, "%s%s", exe_path, Memory::get_c_str(file_name)); - // printf("Fullpath: %s\n", fullpath); + printf("Fullpath: %s\n", fullpath); file_content = read_entire_file(fullpath); if (!file_content) { diff --git a/src/defines.cpp b/src/defines.cpp index d4c1478..b7ca3d0 100644 --- a/src/defines.cpp +++ b/src/defines.cpp @@ -14,7 +14,6 @@ constexpr bool is_debug_build = false; # define if_linux if constexpr (0) # define debug_break() if_debug __debugbreak() #else -# include # define debug_break() if_debug raise(SIGTRAP) # define if_windows if (0) # define if_linux if (1) diff --git a/src/docgeneration.cpp b/src/docgeneration.cpp index 28921d8..7d76a7b 100644 --- a/src/docgeneration.cpp +++ b/src/docgeneration.cpp @@ -2,7 +2,7 @@ proc generate_docs(Environment* env, String* path) -> void { // save the current working directory char* cwd = get_cwd(); // get the direction of the exe - char* exe_path = exe_dir(); + char* exe_path = get_exe_dir(); change_cwd(exe_path); defer { diff --git a/src/forward_decls.cpp b/src/forward_decls.cpp index b8069d2..8862ebb 100644 --- a/src/forward_decls.cpp +++ b/src/forward_decls.cpp @@ -12,7 +12,7 @@ proc load_built_ins_into_environment(Environment*) -> void; proc parse_argument_list(Lisp_Object*, Function*) -> void; proc print_environment(Environment*) -> void; -proc exe_dir() -> char*; +// proc get_exe_dir() -> char*; proc Lisp_Object_Type_to_string(Lisp_Object_Type type) -> const char*; diff --git a/src/io.cpp b/src/io.cpp index 1175195..4e70940 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -387,104 +387,3 @@ proc log_error() -> void { puts(console_normal); } -proc exe_dir() -> char* { - if_linux { - size_t size = 512, i, n; - char *path, *temp; - - while (1) { - size_t used; - - path = (char*)malloc(size); - if (!path) { - errno = ENOMEM; - return NULL; - } - - used = readlink("/proc/self/exe", path, size); - - if (used == -1) { - const int saved_errno = errno; - free(path); - errno = saved_errno; - return NULL; - } else - if (used < 1) { - free(path); - errno = EIO; - return NULL; - } - - if ((size_t)used >= size) { - free(path); - size = (size | 2047) + 2049; - continue; - } - - size = (size_t)used; - break; - } - - /* Find final slash. */ - n = 0; - for (i = 0; i < size; i++) - if (path[i] == '/') - n = i; - - /* Optimize allocated size, - ensuring there is room for - a final slash and a - string-terminating '\0', */ - temp = path; - path = (char*)realloc(temp, n + 2); - if (!path) { - free(temp); - errno = ENOMEM; - return NULL; - } - - /* and properly trim and terminate the path string. */ - path[n+0] = '/'; - path[n+1] = '\0'; - - return path; - } - - if_windows { - DWORD last_error; - DWORD result; - DWORD path_size = 1024; - char* path = (char*)malloc(1024); - - while (true) { - memset(path, 0, path_size); - result = GetModuleFileName(0, path, path_size - 1); - last_error = GetLastError(); - - if (0 == result) { - free(path); - path = 0; - break; - } - else if (result == path_size - 1) { - free(path); - /* May need to also check for ERROR_SUCCESS here if XP/2K */ - if (ERROR_INSUFFICIENT_BUFFER != last_error) { - path = 0; - break; - } - path_size = path_size * 2; - path = (char*)malloc(path_size); - } - else - break; - } - - if (!path) { - fprintf(stderr, "Failure: %d\n", last_error); - return ""; - } - else - return path; - } -} diff --git a/src/memory.cpp b/src/memory.cpp index e486cba..75c05d0 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -363,7 +363,7 @@ namespace Memory { }; // get the direction of the exe - char* exe_path = exe_dir(); + char* exe_path = get_exe_dir(); change_cwd(exe_path); free(exe_path); diff --git a/src/platform.cpp b/src/platform.cpp index 71339c6..1b01039 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -2,22 +2,134 @@ inline proc get_cwd() -> char* { const int buf_size = 2048; char* res = (char*)malloc(buf_size * sizeof(char)); - if_linux { - getcwd(res, buf_size); - } - if_windows { - _getcwd(res, buf_size); - } +#ifdef _MSC_VER + _getcwd(res, buf_size); +#else + getcwd(res, buf_size); +#endif return res; } inline proc change_cwd(char* dir) -> void { - if_linux { - chdir(dir); +#ifdef _MSC_VER + _chdir(dir); +#else + chdir(dir); +#endif +} + +proc get_exe_dir() -> char* { +#ifdef _MSC_VER + DWORD last_error; + DWORD result; + DWORD path_size = 1024; + char* path = (char*)malloc(1024); + + while (true) { + memset(path, 0, path_size); + result = GetModuleFileName(0, path, path_size - 1); + last_error = GetLastError(); + + if (0 == result) { + free(path); + path = 0; + break; + } + else if (result == path_size - 1) { + free(path); + /* May need to also check for ERROR_SUCCESS here if XP/2K */ + if (ERROR_INSUFFICIENT_BUFFER != last_error) { + path = 0; + break; + } + path_size = path_size * 2; + path = (char*)malloc(path_size); + } + else + break; } - if_windows { - _chdir(dir); + if (!path) { + fprintf(stderr, "Failure: %d\n", last_error); + return ""; } + else { + // remove the exe name, so we are only left with the path + + int index_in_path = -1; + int last_backslash = -1; + + char c; + while ((c = path[++index_in_path]) != '\0') { + if (c == '\\') + last_backslash = index_in_path; + } + + // we are assuming there are some backslashes + path[last_backslash+1] = '\0'; + + return path; + } +#else + ssize_t size = 512, i, n; + char *path, *temp; + + while (1) { + size_t used; + + path = (char*)malloc(size); + if (!path) { + errno = ENOMEM; + return NULL; + } + + used = readlink("/proc/self/exe", path, size); + + if (used == -1) { + const int saved_errno = errno; + free(path); + errno = saved_errno; + return NULL; + } else + if (used < 1) { + free(path); + errno = EIO; + return NULL; + } + + if ((size_t)used >= size) { + free(path); + size = (size | 2047) + 2049; + continue; + } + + size = (size_t)used; + break; + } + + /* Find final slash. */ + n = 0; + for (i = 0; i < size; i++) + if (path[i] == '/') + n = i; + + /* Optimize allocated size, + ensuring there is room for + a final slash and a + string-terminating '\0', */ + temp = path; + path = (char*)realloc(temp, n + 2); + if (!path) { + free(temp); + errno = ENOMEM; + return NULL; + } + + /* and properly trim and terminate the path string. */ + path[n+0] = '/'; + path[n+1] = '\0'; + + return path; +#endif } diff --git a/src/slime.h b/src/slime.h index a51ab23..63a470e 100644 --- a/src/slime.h +++ b/src/slime.h @@ -2,15 +2,8 @@ #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE -#include -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) -# include -# include -size_t readlink(char*, char*, size_t); -#else -# include -#endif +#include #include #include #include @@ -19,14 +12,23 @@ size_t readlink(char*, char*, size_t); #include #include +#ifdef _MSC_VER +# include +# include + +#else +# include +# include +#endif + #undef _CRT_SECURE_NO_DEPRECATE #undef _CRT_SECURE_NO_WARNINGS namespace Slime { # include "./defines.cpp" +# include "./platform.cpp" # include "./structs.cpp" # include "./forward_decls.cpp" -# include "./platform.cpp" # include "./memory.cpp" # include "./lisp_object.cpp" # include "./error.cpp" diff --git a/src/testing.cpp b/src/testing.cpp index a143ef9..ecf12a4 100644 --- a/src/testing.cpp +++ b/src/testing.cpp @@ -593,7 +593,7 @@ proc run_all_tests() -> bool { Memory::init(4096 * 2000, 1024 * 32, 4096 * 16 * 10); // get the direction of the exe - char* exe_path = exe_dir(); + char* exe_path = get_exe_dir(); // switch to the exe directory for loading pre.slime change_cwd(exe_path); diff --git a/src/visualization.cpp b/src/visualization.cpp index 1147ae2..e1a036a 100644 --- a/src/visualization.cpp +++ b/src/visualization.cpp @@ -4,15 +4,15 @@ proc visualize_lisp_machine() -> void { char* cwd = get_cwd(); // get the direction of the exe - char* exe_path = exe_dir(); + char* exe_path = get_exe_dir(); // switch to the exe directory for loading pre.slime change_cwd(exe_path); - free(exe_path); defer { // switch back to the users directory change_cwd(cwd); free(cwd); + free(exe_path); }; struct Drawn_Area {