added load path

This commit is contained in:
FelixBrendel
2019-10-15 15:34:15 +02:00
parent c80478a2cb
commit af4f8cf630
5 changed files with 30 additions and 49 deletions
-20
View File
@@ -14,26 +14,6 @@
(define (force promise) (promise)) (define (force promise) (promise))
(define (pair-stream oject expression) (pair object (delay expression)))
(define stream-first first)
(define (stream-rest stream) (force (rest stream)))
(define (stream-ref s n) (if (= n 0) (stream-first s) (stream-ref (stream-rest s) (- n 1))))
(define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-first stream)) (pair-stream (stream-first stream) (stream-filter pred (stream-rest stream)))) (else (stream-filter pred (stream-rest stream)))))
(define (stream-map proc s) (if (stream-null? s) the-empty-stream (pair-stream (proc (stream-first s)) (stream-map proc (stream-rest s)))))
(define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-first s)) (stream-for-each proc (stream-rest s)))))
(define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (pair-stream low (stream-enumerate-interval (+ low 1) high))))
(define (prime? x) (define (prime-helper x k) (cond ((= x k) t) ((= (% x k) 0) ()) (else (prime-helper x (+ k 1))))) (cond ((= x 1) ()) ((= x 2) t) (else (prime-helper x 2))))
(define (a) (stream-first (stream-rest (stream-filter prime? (stream-enumerate-interval 10000 1020)))))
(define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil))) (define-syntax (when condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is true.\n\n{{{example_start}}}\n(when (not ())\n (print "Hello ")\n (print "from ")\n (print "when!"))\n\n(when ()\n (print "Goodbye ")\n (print "World!"))\n{{{example_end}}}\n" (if (= (rest body) ()) `(if ,condition (unquote-splicing body) nil) `(if ,condition (begin (unquote-splicing body)) nil)))
(define-syntax (unless condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is false." (if (= (rest body) ()) `(if ,condition nil (unquote-splicing body)) `(if ,condition nil (begin (unquote-splicing body))))) (define-syntax (unless condition . body) :doc "Special form for when multiple actions should be done if a\ncondition is false." (if (= (rest body) ()) `(if ,condition nil (unquote-splicing body)) `(if ,condition nil (begin (unquote-splicing body)))))
+19 -21
View File
@@ -42,6 +42,12 @@ proc lisp_object_equal(Lisp_Object* n1, Lisp_Object* n2) -> bool {
return false; return false;
} }
proc add_to_load_path(const char* path) -> void {
using Globals::load_path;
append_to_array_list(&load_path, (void*)path);
}
proc built_in_load(String* file_name) -> Lisp_Object* { proc built_in_load(String* file_name) -> Lisp_Object* {
// char* full_file_name = find_slime_file(file_name); // char* full_file_name = find_slime_file(file_name);
char* file_content; char* file_content;
@@ -50,33 +56,25 @@ proc built_in_load(String* file_name) -> Lisp_Object* {
file_content = read_entire_file(Memory::get_c_str(file_name)); file_content = read_entire_file(Memory::get_c_str(file_name));
if (!file_content) { if (!file_content) {
// try slime's bin dir for_array_list (Globals::load_path) {
// save the current working directory
// get the direction of the exe
char* exe_path = get_exe_dir();
defer {
free(exe_path);
};
fullpath[0] = '\0'; fullpath[0] = '\0';
sprintf(fullpath, "%s%s", exe_path, Memory::get_c_str(file_name)); sprintf(fullpath, "%s%s", (char*)it, Memory::get_c_str(file_name));
// printf("Fullpath: %s\n", fullpath);
file_content = read_entire_file(fullpath); file_content = read_entire_file(fullpath);
if (file_content)
break;
}
if (!file_content) { if (!file_content) {
char* cwd = get_cwd(); printf("Load path:\n");
defer { for_array_list (Globals::load_path) {
free(cwd); printf(" - %s\n", (char*) it);
};
create_generic_error("The file to load '%s' was not found: "
"neither in the cwd (%s) "
"nor in slime's exe dir (%s)",
Memory::get_c_str(file_name), cwd, fullpath);
return nullptr;
} }
create_generic_error("The file to load '%s' was not found in the load path.",
Memory::get_c_str(file_name));
} }
}
Lisp_Object* result = Memory::nil; Lisp_Object* result = Memory::nil;
Lisp_Object_Array_List program; Lisp_Object_Array_List program;
try program = Parser::parse_program(Memory::create_string(fullpath), file_content); try program = Parser::parse_program(Memory::create_string(fullpath), file_content);
+2
View File
@@ -1,4 +1,5 @@
// proc assert_type(Lisp_Object*, Lisp_Object_Type) -> void; // proc assert_type(Lisp_Object*, Lisp_Object_Type) -> void;
proc add_to_load_path(const char*) -> void;
proc lisp_object_equal(Lisp_Object*,Lisp_Object*) -> bool; proc lisp_object_equal(Lisp_Object*,Lisp_Object*) -> bool;
proc built_in_load(String*) -> Lisp_Object*; proc built_in_load(String*) -> Lisp_Object*;
proc built_in_import(String*) -> Lisp_Object*; proc built_in_import(String*) -> Lisp_Object*;
@@ -45,6 +46,7 @@ namespace Globals {
char* bin_path = nullptr; char* bin_path = nullptr;
Log_Level log_level = Log_Level::Debug; Log_Level log_level = Log_Level::Debug;
Void_Ptr_Array_List load_path = create_Void_Ptr_array_list();
namespace Current_Execution { namespace Current_Execution {
Lisp_Object_Array_List call_stack = create_Lisp_Object_array_list(); Lisp_Object_Array_List call_stack = create_Lisp_Object_array_list();
Environment_Array_List envi_stack = create_Environment_array_list(); Environment_Array_List envi_stack = create_Environment_array_list();
+4
View File
@@ -174,6 +174,10 @@ namespace Memory {
} }
proc init(int oms, int ems, int sms) { proc init(int oms, int ems, int sms) {
char* exe_path = get_exe_dir();
defer {free(exe_path);};
add_to_load_path(exe_path);
global_symbol_table = create_String_hashmap(); global_symbol_table = create_String_hashmap();
global_keyword_table = create_String_hashmap(); global_keyword_table = create_String_hashmap();
+1 -4
View File
@@ -21,9 +21,6 @@
# include <signal.h> # include <signal.h>
#endif #endif
#undef _CRT_SECURE_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#include "./ftb/macros.hpp" #include "./ftb/macros.hpp"
#include "./ftb/profiler.hpp" #include "./ftb/profiler.hpp"
#include "./ftb/hashmap.hpp" #include "./ftb/hashmap.hpp"
@@ -45,5 +42,5 @@ namespace Slime {
# include "./docgeneration.cpp" # include "./docgeneration.cpp"
# include "./built_ins.cpp" # include "./built_ins.cpp"
# include "./testing.cpp" # include "./testing.cpp"
# include "./undefines.cpp" // # include "./undefines.cpp"
} }