From 6da1530d0fb73a2922430ab1b70d22063f685e8b Mon Sep 17 00:00:00 2001 From: FelixBrendel Date: Thu, 27 Jun 2019 23:59:27 +0200 Subject: [PATCH] windows works again and also alists working now --- bin/alist.slime | 2 +- bin/cxr.slime | 4 +- bin/tests/alists.slime | 1 - src/built_ins.cpp | 2 +- src/docgeneration.cpp | 10 ++-- src/io.cpp | 130 +++++++++++++++++++++++++++-------------- src/memory.cpp | 10 ++-- src/platform.cpp | 23 ++++++++ src/slime.h | 10 +++- src/testing.cpp | 2 +- src/visualization.cpp | 15 ++--- 11 files changed, 141 insertions(+), 68 deletions(-) create mode 100644 src/platform.cpp diff --git a/bin/alist.slime b/bin/alist.slime index e9898f2..b95cfb9 100644 --- a/bin/alist.slime +++ b/bin/alist.slime @@ -40,7 +40,7 @@ (error "key was not found in alist")) ((= (caar associations) key) (cdar associations)) - (else (alist-get-intern (rest alist) key)))) + (else (alist-get-intern (rest associations) key)))) (alist-get-intern associations key))) diff --git a/bin/cxr.slime b/bin/cxr.slime index 7dafcc9..630ebfa 100644 --- a/bin/cxr.slime +++ b/bin/cxr.slime @@ -18,7 +18,7 @@ (car (caar seq))) (define (caadr seq) - (caar (cdr seq))) + (car (cadr seq))) (define (cadar seq) (car (cdr (car seq)))) @@ -33,7 +33,7 @@ (cdr (car (cdr seq)))) (define (cddar seq) - (cddr (car seq))) + (cdr (cdar seq))) (define (cdddr seq) (cdr (cddr seq))) diff --git a/bin/tests/alists.slime b/bin/tests/alists.slime index 3a473c9..dc0121e 100644 --- a/bin/tests/alists.slime +++ b/bin/tests/alists.slime @@ -37,7 +37,6 @@ ;; key1: value1) (assert (= (length (first a)) 3)) -(pprint-alist a) (assert (= (alist-get a 'key1) 'value4)) (alist-remove! a 'key1) diff --git a/src/built_ins.cpp b/src/built_ins.cpp index 9538d92..81746f1 100644 --- a/src/built_ins.cpp +++ b/src/built_ins.cpp @@ -923,7 +923,7 @@ proc load_built_ins_into_environment(Environment* env) -> void { try evaluated_arguments = eval_arguments(arguments, env, &arguments_length); try assert_arguments_length(1, arguments_length); - return Memory::create_lisp_object_number((u64)&(evaluated_arguments->value.pair.first->value)); + return Memory::create_lisp_object_number((float)((u64)&(evaluated_arguments->value.pair.first->value))); }); defun("generate-docs", "TODO", __LINE__, cLambda { try arguments_length = list_length(arguments); diff --git a/src/docgeneration.cpp b/src/docgeneration.cpp index 389bb68..28921d8 100644 --- a/src/docgeneration.cpp +++ b/src/docgeneration.cpp @@ -1,15 +1,15 @@ proc generate_docs(Environment* env, String* path) -> void { // save the current working directory - char cwd[1024]; - getcwd(cwd, 1024); + char* cwd = get_cwd(); // get the direction of the exe char* exe_path = exe_dir(); - chdir(exe_path); - free(exe_path); + change_cwd(exe_path); defer { // switch back to the users directory - chdir(cwd); + change_cwd(cwd); + free(exe_path); + free(cwd); }; FILE *f = fopen(Memory::get_c_str(path), "w"); diff --git a/src/io.cpp b/src/io.cpp index 633b707..1175195 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -388,63 +388,103 @@ proc log_error() -> void { } proc exe_dir() -> char* { - size_t size = 512, i, n; - char *path, *temp; + if_linux { + size_t size = 512, i, n; + char *path, *temp; - while (1) { - ssize_t used; + while (1) { + size_t used; - path = (char*)malloc(size); - if (!path) { - errno = ENOMEM; - return NULL; - } + path = (char*)malloc(size); + if (!path) { + errno = ENOMEM; + return NULL; + } - used = readlink("/proc/self/exe", path, size); + 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) { + if (used == -1) { + const int saved_errno = errno; free(path); - errno = EIO; + 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; } - if ((size_t)used >= size) { - free(path); - size = (size | 2047) + 2049; - continue; + size = (size_t)used; + break; } - 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; + } - /* 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; } - /* and properly trim and terminate the path string. */ - path[n+0] = '/'; - path[n+1] = '\0'; + 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(); - return path; + 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 48df357..e486cba 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -357,17 +357,19 @@ namespace Memory { Parser::environment_for_macros = ret; // save the current working directory - char cwd[1024]; - getcwd(cwd, 1024); + char* cwd = get_cwd(); + defer { + free(cwd); + }; // get the direction of the exe char* exe_path = exe_dir(); - chdir(exe_path); + change_cwd(exe_path); free(exe_path); built_in_load(Memory::create_string("pre.slime"), ret); - chdir(cwd); + change_cwd(cwd); return ret; } diff --git a/src/platform.cpp b/src/platform.cpp new file mode 100644 index 0000000..71339c6 --- /dev/null +++ b/src/platform.cpp @@ -0,0 +1,23 @@ +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); + } + + return res; +} + +inline proc change_cwd(char* dir) -> void { + if_linux { + chdir(dir); + } + + if_windows { + _chdir(dir); + } +} diff --git a/src/slime.h b/src/slime.h index 2651988..a51ab23 100644 --- a/src/slime.h +++ b/src/slime.h @@ -3,7 +3,14 @@ #define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_DEPRECATE #include -#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 @@ -19,6 +26,7 @@ namespace Slime { # include "./defines.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 89a34bf..a143ef9 100644 --- a/src/testing.cpp +++ b/src/testing.cpp @@ -596,7 +596,7 @@ proc run_all_tests() -> bool { char* exe_path = exe_dir(); // switch to the exe directory for loading pre.slime - chdir(exe_path); + change_cwd(exe_path); free(exe_path); bool result = true; diff --git a/src/visualization.cpp b/src/visualization.cpp index 51bfb6e..1147ae2 100644 --- a/src/visualization.cpp +++ b/src/visualization.cpp @@ -1,17 +1,18 @@ proc visualize_lisp_machine() -> void { // save the current working directory - char cwd[1024]; - getcwd(cwd, 1024); + char* cwd = get_cwd(); + // get the direction of the exe char* exe_path = exe_dir(); // switch to the exe directory for loading pre.slime - chdir(exe_path); + change_cwd(exe_path); free(exe_path); defer { // switch back to the users directory - chdir(cwd); + change_cwd(cwd); + free(cwd); }; struct Drawn_Area { @@ -180,7 +181,7 @@ proc visualize_lisp_machine() -> void { case Lisp_Object_Type::T: return draw_text("t"); case Lisp_Object_Type::Nil: return draw_text("()"); case Lisp_Object_Type::Pair: return draw_pair(obj); - case Lisp_Object_Type::Number: return draw_float(obj->value.number); + case Lisp_Object_Type::Number: return draw_float((float)obj->value.number); case Lisp_Object_Type::Symbol: return draw_text(&obj->value.string->data); case Lisp_Object_Type::Keyword: { Drawn_Area colon = draw_text(":", "#c61b6e"); @@ -303,7 +304,7 @@ proc visualize_lisp_machine() -> void { // ------------------- draw_new_line(); - int free_string_memory = Memory::next_free_spot_in_string_memory - Memory::string_memory; + int free_string_memory = (int)(Memory::next_free_spot_in_string_memory - Memory::string_memory); for (int i = 0; i < Memory::free_spots_in_string_memory->next_index; ++i) { free_string_memory += ((String*)(Memory::free_spots_in_string_memory->data[i]))->length; } @@ -491,7 +492,7 @@ proc visualize_lisp_machine() -> void { draw_new_line(); write_x = start_x + 600; - draw_float(numbers->data[i]->value.number); + draw_float((float)(numbers->data[i]->value.number)); } write_x = start_x + 900;