fixed import bug

This commit is contained in:
fumfar hiwi
2019-12-04 14:51:06 +01:00
parent 667989e373
commit efbdfeed2e
19 changed files with 3698 additions and 3680 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
(define a 10) (define a 1111)
(define (get-a-1) a) (define (get-a-1) a)
+8 -15
View File
@@ -1,27 +1,20 @@
(import "tests/import1.slime") (import "tests/import1.slime")
(print) (assert (= a 1111))
(print ">" a) (assert (= (get-a-1) 1111))
(assert (= a 10))
(print ">" (get-a-1))
(assert (= (get-a-1) 10))
(import "tests/import2.slime") (import "tests/import2.slime")
(print ">" a)
(assert (= a 10)) (assert (= a 1111))
(print ">" (get-a-1)) (assert (= (get-a-1) 1111))
(assert (= (get-a-1) 10)) (assert (= (get-a-2) 1111))
(print ">" (get-a-2))
(assert (= (get-a-2) 10))
(set-a-2 11) (set-a-2 11)
(print "> should be 11 from now on")
(print ">" a)
(assert (= a 11)) (assert (= a 11))
(print ">" (get-a-1))
(assert (= (get-a-1) 11)) (assert (= (get-a-1) 11))
(print ">" (get-a-2))
(assert (= (get-a-2) 11)) (assert (= (get-a-2) 11))
+3 -1
View File
@@ -1,4 +1,5 @@
proc generate_docs(String* path) -> void { namespace Slime {
proc generate_docs(String* path) -> void {
FILE *f = fopen(Memory::get_c_str(path), "w"); FILE *f = fopen(Memory::get_c_str(path), "w");
if (!f) { if (!f) {
create_generic_error("The file for writing the documentation (%s) " create_generic_error("The file for writing the documentation (%s) "
@@ -141,4 +142,5 @@ proc generate_docs(String* path) -> void {
}; };
print_this_env(print_this_env, get_current_environment(), (char*)""); print_this_env(print_this_env, get_current_environment(), (char*)"");
}
} }
+5 -5
View File
@@ -97,11 +97,11 @@ namespace Slime {
} }
}; };
// if(env == get_root_environment()) { if(env == get_root_environment()) {
// print_indent(indent); print_indent(indent);
// printf("[built-ins]-Environment (%lld)\n", (long long)env); printf("[built-ins]-Environment (%lld)\n", (long long)env);
// return; return;
// } }
for_hash_map (env->hm) { for_hash_map (env->hm) {
print_indent(indent); print_indent(indent);
+8 -5
View File
@@ -1,11 +1,13 @@
proc delete_error() -> void { namespace Slime {
proc delete_error() -> void {
using Globals::error; using Globals::error;
free(error); free(error);
error = nullptr; error = nullptr;
} }
proc create_error(const char* c_func_name,const char* c_file_name, int c_file_line, Lisp_Object* type, String* message) -> void { proc create_error(const char* c_func_name,const char* c_file_name, int c_file_line, Lisp_Object* type, String* message) -> void {
delete_error(); delete_error();
if (Globals::breaking_on_errors) { if (Globals::breaking_on_errors) {
debug_break(); debug_break();
@@ -29,9 +31,9 @@ proc create_error(const char* c_func_name,const char* c_file_name, int c_file_li
} }
// visualize_lisp_machine(); // visualize_lisp_machine();
} }
proc create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, const char* format, ...) -> void { proc create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, const char* format, ...) -> void {
using Globals::error; using Globals::error;
int length = 200; int length = 200;
@@ -51,4 +53,5 @@ proc create_error(const char* c_func_name, const char* c_file_name, int c_file_l
strcpy(&formatted_string->data, out_msg); strcpy(&formatted_string->data, out_msg);
free(out_msg); free(out_msg);
create_error(c_func_name, c_file_name, c_file_line, type, formatted_string); create_error(c_func_name, c_file_name, c_file_line, type, formatted_string);
}
} }
+22 -20
View File
@@ -1,8 +1,9 @@
proc create_extended_environment_for_function_application( namespace Slime {
proc create_extended_environment_for_function_application(
Lisp_Object* unevaluated_arguments, Lisp_Object* unevaluated_arguments,
Lisp_Object* function, Lisp_Object* function,
bool should_evaluate) -> Environment* bool should_evaluate) -> Environment*
{ {
profile_this(); profile_this();
bool is_c_function = Memory::get_type(function) == Lisp_Object_Type::CFunction; bool is_c_function = Memory::get_type(function) == Lisp_Object_Type::CFunction;
Environment* new_env; Environment* new_env;
@@ -221,9 +222,9 @@ proc create_extended_environment_for_function_application(
try read_rest_arg(); try read_rest_arg();
return new_env; return new_env;
} }
proc apply_arguments_to_function(Lisp_Object* arguments, Lisp_Object* function, bool should_evaluate_args) -> Lisp_Object* { proc apply_arguments_to_function(Lisp_Object* arguments, Lisp_Object* function, bool should_evaluate_args) -> Lisp_Object* {
profile_this(); profile_this();
Environment* new_env; Environment* new_env;
Lisp_Object* result; Lisp_Object* result;
@@ -243,7 +244,7 @@ proc apply_arguments_to_function(Lisp_Object* arguments, Lisp_Object* function,
try result = eval_expr(function->value.function->body); try result = eval_expr(function->value.function->body);
return result; return result;
} }
/** /**
This parses the argument specification of funcitons into their This parses the argument specification of funcitons into their
@@ -251,7 +252,7 @@ proc apply_arguments_to_function(Lisp_Object* arguments, Lisp_Object* function,
positional_arguments, keyword_arguments and rest_argument and positional_arguments, keyword_arguments and rest_argument and
filling it in filling it in
*/ */
proc create_arguments_from_lambda_list_and_inject(Lisp_Object* arguments, Lisp_Object* function) -> void { proc create_arguments_from_lambda_list_and_inject(Lisp_Object* arguments, Lisp_Object* function) -> void {
Arguments* result; Arguments* result;
if (Memory::get_type(function) == Lisp_Object_Type::CFunction) { if (Memory::get_type(function) == Lisp_Object_Type::CFunction) {
result = &function->value.cFunction->args; result = &function->value.cFunction->args;
@@ -329,10 +330,10 @@ proc create_arguments_from_lambda_list_and_inject(Lisp_Object* arguments, Lisp_O
else else
create_parsing_error("The rest argument must be a symbol."); create_parsing_error("The rest argument must be a symbol.");
} }
} }
proc list_length(Lisp_Object* node) -> int { proc list_length(Lisp_Object* node) -> int {
if (node == Memory::nil) if (node == Memory::nil)
return 0; return 0;
@@ -349,14 +350,14 @@ proc list_length(Lisp_Object* node) -> int {
create_parsing_error("Can't calculate length of ill formed list."); create_parsing_error("Can't calculate length of ill formed list.");
return 0; return 0;
} }
proc copy_scl(Source_Code_Location*) -> Source_Code_Location* { proc copy_scl(Source_Code_Location*) -> Source_Code_Location* {
// TODO(Felix): // TODO(Felix):
return nullptr; return nullptr;
} }
proc eval_arguments(Lisp_Object* arguments) -> Lisp_Object* { proc eval_arguments(Lisp_Object* arguments) -> Lisp_Object* {
profile_this(); profile_this();
// int my_out_arguments_length = 0; // int my_out_arguments_length = 0;
if (arguments == Memory::nil) { if (arguments == Memory::nil) {
@@ -390,9 +391,9 @@ proc eval_arguments(Lisp_Object* arguments) -> Lisp_Object* {
} }
// *(out_arguments_length) = my_out_arguments_length; // *(out_arguments_length) = my_out_arguments_length;
return evaluated_arguments; return evaluated_arguments;
} }
proc eval_expr(Lisp_Object* node) -> Lisp_Object* { proc eval_expr(Lisp_Object* node) -> Lisp_Object* {
profile_this(); profile_this();
using namespace Globals::Current_Execution; using namespace Globals::Current_Execution;
@@ -476,16 +477,16 @@ proc eval_expr(Lisp_Object* node) -> Lisp_Object* {
} }
} }
} }
proc is_truthy(Lisp_Object* expression) -> bool { proc is_truthy(Lisp_Object* expression) -> bool {
Lisp_Object* result; Lisp_Object* result;
try result = eval_expr(expression); try result = eval_expr(expression);
return result != Memory::nil; return result != Memory::nil;
} }
proc interprete_file (char* file_name) -> Lisp_Object* { proc interprete_file (char* file_name) -> Lisp_Object* {
try Memory::init(4096 * 256); try Memory::init(4096 * 256);
Lisp_Object* result; Lisp_Object* result;
@@ -493,9 +494,9 @@ proc interprete_file (char* file_name) -> Lisp_Object* {
try result = built_in_load(Memory::create_string(file_name)); try result = built_in_load(Memory::create_string(file_name));
return result; return result;
} }
proc interprete_stdin() -> void { proc interprete_stdin() -> void {
try_void Memory::init(4096 * 256* 100); try_void Memory::init(4096 * 256* 100);
printf("Welcome to the lispy interpreter.\n"); printf("Welcome to the lispy interpreter.\n");
@@ -519,4 +520,5 @@ proc interprete_stdin() -> void {
} }
}(); }();
} }
}
} }
+34 -32
View File
@@ -1,37 +1,38 @@
void add_to_load_path(const char*); namespace Slime {
bool lisp_object_equal(Lisp_Object*,Lisp_Object*); void add_to_load_path(const char*);
Lisp_Object* built_in_load(String*); bool lisp_object_equal(Lisp_Object*,Lisp_Object*);
Lisp_Object* built_in_import(String*); Lisp_Object* built_in_load(String*);
void delete_error(); Lisp_Object* built_in_import(String*);
void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, const char* format, ...); void delete_error();
void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, String* message); void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, const char* format, ...);
void create_error(Lisp_Object* type, const char* message, const char* c_file_name, int c_file_line); void create_error(const char* c_func_name, const char* c_file_name, int c_file_line, Lisp_Object* type, String* message);
Lisp_Object* eval_arguments(Lisp_Object*); void create_error(Lisp_Object* type, const char* message, const char* c_file_name, int c_file_line);
Lisp_Object* eval_expr(Lisp_Object*); Lisp_Object* eval_arguments(Lisp_Object*);
bool is_truthy (Lisp_Object*); Lisp_Object* eval_expr(Lisp_Object*);
int list_length(Lisp_Object*); bool is_truthy (Lisp_Object*);
void* load_built_ins_into_environment(); int list_length(Lisp_Object*);
void create_arguments_from_lambda_list_and_inject(Lisp_Object* formal_arguments, Lisp_Object* function); void* load_built_ins_into_environment();
void create_arguments_from_lambda_list_and_inject(Lisp_Object* formal_arguments, Lisp_Object* function);
Lisp_Object* lookup_symbol(Lisp_Object* symbol, Environment*); Lisp_Object* lookup_symbol(Lisp_Object* symbol, Environment*);
void define_symbol(Lisp_Object* symbol, Lisp_Object* value); void define_symbol(Lisp_Object* symbol, Lisp_Object* value);
void print(Lisp_Object* node, bool print_repr = false, FILE* file = stdout); void print(Lisp_Object* node, bool print_repr = false, FILE* file = stdout);
void print_environment(Environment*); void print_environment(Environment*);
bool run_all_tests(); bool run_all_tests();
inline Environment* get_root_environment(); inline Environment* get_root_environment();
inline Environment* get_current_environment(); inline Environment* get_current_environment();
inline void push_environment(Environment*); inline void push_environment(Environment*);
inline void pop_environment(); inline void pop_environment();
const char* Lisp_Object_Type_to_string(Lisp_Object_Type type); const char* Lisp_Object_Type_to_string(Lisp_Object_Type type);
void visualize_lisp_machine(); void visualize_lisp_machine();
void generate_docs(String* path); void generate_docs(String* path);
void log_error(); void log_error();
namespace Memory { namespace Memory {
Environment* create_built_ins_environment(); Environment* create_built_ins_environment();
Lisp_Object* create_lisp_object_cfunction(bool is_special); Lisp_Object* create_lisp_object_cfunction(bool is_special);
inline Lisp_Object_Type get_type(Lisp_Object* node); inline Lisp_Object_Type get_type(Lisp_Object* node);
@@ -55,9 +56,9 @@ namespace Memory {
inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*); inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*); inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*); inline Lisp_Object* create_list(Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*,Lisp_Object*);
} }
namespace Parser { namespace Parser {
// extern Environment* environment_for_macros; // extern Environment* environment_for_macros;
extern String* standard_in; extern String* standard_in;
@@ -68,9 +69,9 @@ namespace Parser {
Lisp_Object* parse_expression(char* text, int* index_in_text); Lisp_Object* parse_expression(char* text, int* index_in_text);
Lisp_Object* parse_single_expression(char* text); Lisp_Object* parse_single_expression(char* text);
Lisp_Object* parse_single_expression(wchar_t* text); Lisp_Object* parse_single_expression(wchar_t* text);
} }
namespace Globals { namespace Globals {
extern char* bin_path; extern char* bin_path;
extern Log_Level log_level; extern Log_Level log_level;
extern Array_List<void*> load_path; extern Array_List<void*> load_path;
@@ -80,4 +81,5 @@ namespace Globals {
} }
extern Error* error; extern Error* error;
extern bool breaking_on_errors; extern bool breaking_on_errors;
}
} }
+1 -1
View File
@@ -1,4 +1,4 @@
namespace GC { namespace Slime::GC {
proc maybe_mark(Environment* env) -> void; proc maybe_mark(Environment* env) -> void;
int current_mark; int current_mark;
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Globals { namespace Slime::Globals {
char* bin_path = nullptr; char* bin_path = nullptr;
Log_Level log_level = Log_Level::Debug; Log_Level log_level = Log_Level::Debug;
+35 -33
View File
@@ -1,4 +1,5 @@
proc string_equal(const char input[], const char check[]) -> bool { namespace Slime {
proc string_equal(const char input[], const char check[]) -> bool {
if (input == check) return true; if (input == check) return true;
for(int i = 0; input[i] == check[i]; i++) { for(int i = 0; input[i] == check[i]; i++) {
@@ -7,32 +8,32 @@ proc string_equal(const char input[], const char check[]) -> bool {
} }
return false; return false;
} }
proc string_equal(String* str, const char check[]) -> bool { proc string_equal(String* str, const char check[]) -> bool {
return string_equal(Memory::get_c_str(str), check); return string_equal(Memory::get_c_str(str), check);
} }
proc string_equal(const char check[], String* str) -> bool { proc string_equal(const char check[], String* str) -> bool {
return string_equal(Memory::get_c_str(str), check); return string_equal(Memory::get_c_str(str), check);
} }
proc string_equal(String* str1, String* str2) -> bool { proc string_equal(String* str1, String* str2) -> bool {
if (str1 == str2) if (str1 == str2)
return true; return true;
return string_equal(Memory::get_c_str(str1), Memory::get_c_str(str2)); return string_equal(Memory::get_c_str(str1), Memory::get_c_str(str2));
} }
proc get_nibble(char c) -> char { proc get_nibble(char c) -> char {
if (c >= 'A' && c <= 'F') if (c >= 'A' && c <= 'F')
return (c - 'A') + 10; return (c - 'A') + 10;
else if (c >= 'a' && c <= 'f') else if (c >= 'a' && c <= 'f')
return (c - 'a') + 10; return (c - 'a') + 10;
return (c - '0'); return (c - '0');
} }
proc escape_string(char* in) -> char* { proc escape_string(char* in) -> char* {
// TODO(Felix): add more escape sequences // TODO(Felix): add more escape sequences
int i = 0, count = 0; int i = 0, count = 0;
while (in[i] != '\0') { while (in[i] != '\0') {
@@ -62,9 +63,9 @@ proc escape_string(char* in) -> char* {
} }
ret[j++] = '\0'; ret[j++] = '\0';
return ret; return ret;
} }
proc unescape_string(char* in) -> int { proc unescape_string(char* in) -> int {
if (!in) return 0; if (!in) return 0;
char *out = in, *p = in; char *out = in, *p = in;
@@ -117,9 +118,9 @@ proc unescape_string(char* in) -> int {
/* Set the end of string. */ /* Set the end of string. */
*out = '\0'; *out = '\0';
return (int)(out - in); return (int)(out - in);
} }
proc read_entire_file(char* filename) -> char* { proc read_entire_file(char* filename) -> char* {
profile_with_comment(filename); profile_with_comment(filename);
char *fileContent = nullptr; char *fileContent = nullptr;
FILE *fp = fopen(filename, "r"); FILE *fp = fopen(filename, "r");
@@ -156,9 +157,9 @@ proc read_entire_file(char* filename) -> char* {
return fileContent; return fileContent;
/* Don't forget to call free() later! */ /* Don't forget to call free() later! */
} }
proc read_expression() -> char* { proc read_expression() -> char* {
char* line = (char*)malloc(100); char* line = (char*)malloc(100);
if(line == nullptr) if(line == nullptr)
@@ -201,9 +202,9 @@ proc read_expression() -> char* {
*line = '\0'; *line = '\0';
return linep; return linep;
} }
proc read_line() -> char* { proc read_line() -> char* {
char* line = (char*)malloc(100), * linep = line; char* line = (char*)malloc(100), * linep = line;
size_t lenmax = 100, len = lenmax; size_t lenmax = 100, len = lenmax;
int c; int c;
@@ -244,9 +245,9 @@ proc read_line() -> char* {
*line = '\0'; *line = '\0';
return linep; return linep;
} }
proc log_message(Log_Level type, const char* message) -> void { proc log_message(Log_Level type, const char* message) -> void {
if (type > Globals::log_level) if (type > Globals::log_level)
return; return;
@@ -259,9 +260,9 @@ proc log_message(Log_Level type, const char* message) -> void {
default: return; default: return;
} }
printf("%s: %s\n",prefix, message); printf("%s: %s\n",prefix, message);
} }
char* wchar_to_char(const wchar_t* pwchar) { char* wchar_to_char(const wchar_t* pwchar) {
// get the number of characters in the string. // get the number of characters in the string.
int currentCharIndex = 0; int currentCharIndex = 0;
char currentChar = (char)pwchar[currentCharIndex]; char currentChar = (char)pwchar[currentCharIndex];
@@ -292,16 +293,16 @@ char* wchar_to_char(const wchar_t* pwchar) {
filePathC -= (sizeof(char) * charCount); filePathC -= (sizeof(char) * charCount);
return filePathC; return filePathC;
} }
const wchar_t* char_to_wchar(const char* c) { const wchar_t* char_to_wchar(const char* c) {
const size_t cSize = strlen(c)+1; const size_t cSize = strlen(c)+1;
wchar_t* wc = new wchar_t[cSize]; wchar_t* wc = new wchar_t[cSize];
mbstowcs (wc, c, cSize); mbstowcs (wc, c, cSize);
return wc; return wc;
} }
proc print(Lisp_Object* node, bool print_repr, FILE* file) -> void { proc print(Lisp_Object* node, bool print_repr, FILE* file) -> void {
switch (Memory::get_type(node)) { switch (Memory::get_type(node)) {
case (Lisp_Object_Type::Nil): fputs("()", file); break; case (Lisp_Object_Type::Nil): fputs("()", file); break;
case (Lisp_Object_Type::T): fputs("t", file); break; case (Lisp_Object_Type::T): fputs("t", file); break;
@@ -420,9 +421,9 @@ proc print(Lisp_Object* node, bool print_repr, FILE* file) -> void {
putc(')', file); putc(')', file);
} break; } break;
} }
} }
proc print_single_call(Lisp_Object* obj) -> void { proc print_single_call(Lisp_Object* obj) -> void {
printf(console_cyan); printf(console_cyan);
print(obj, true); print(obj, true);
printf(console_normal); printf(console_normal);
@@ -436,9 +437,9 @@ proc print_single_call(Lisp_Object* obj) -> void {
} else { } else {
fputs("no source code location avaliable", stdout); fputs("no source code location avaliable", stdout);
} }
} }
proc print_call_stack() -> void { proc print_call_stack() -> void {
using Globals::Current_Execution::call_stack; using Globals::Current_Execution::call_stack;
printf("callstack [%d] (most recent call last):\n", call_stack.next_index); printf("callstack [%d] (most recent call last):\n", call_stack.next_index);
@@ -447,9 +448,9 @@ proc print_call_stack() -> void {
print_single_call(call_stack.data[i]); print_single_call(call_stack.data[i]);
printf("\n"); printf("\n");
} }
} }
proc log_error() -> void { proc log_error() -> void {
fputs(console_red, stdout); fputs(console_red, stdout);
fputs(Memory::get_c_str(Globals::error->message), stdout); fputs(Memory::get_c_str(Globals::error->message), stdout);
puts(console_normal); puts(console_normal);
@@ -457,4 +458,5 @@ proc log_error() -> void {
fputs(" in: ", stdout); fputs(" in: ", stdout);
print_call_stack(); print_call_stack();
puts(console_normal); puts(console_normal);
}
} }
+3 -4
View File
@@ -39,14 +39,14 @@ unsigned int hm_hash(void* ptr);
unsigned int hm_hash(Slime::Lisp_Object* obj); unsigned int hm_hash(Slime::Lisp_Object* obj);
#include "ftb/hashmap.hpp" #include "ftb/hashmap.hpp"
namespace Slime {
# include "defines.cpp" # include "defines.cpp"
# include "assert.hpp" # include "assert.hpp"
# include "define_macros.hpp" # include "define_macros.hpp"
# include "platform.cpp" # include "platform.cpp"
# include "structs.cpp" # include "structs.cpp"
# include "forward_decls.cpp" # include "forward_decls.cpp"
}
bool hm_objects_match(char* a, char* b) { bool hm_objects_match(char* a, char* b) {
return strcmp(a, b) == 0; return strcmp(a, b) == 0;
@@ -107,7 +107,6 @@ unsigned int hm_hash(Slime::Lisp_Object* obj) {
} }
} }
namespace Slime {
# include "globals.cpp" # include "globals.cpp"
# include "memory.cpp" # include "memory.cpp"
# include "gc.cpp" # include "gc.cpp"
@@ -122,4 +121,4 @@ namespace Slime {
# include "built_ins.cpp" # include "built_ins.cpp"
# include "testing.cpp" # include "testing.cpp"
// # include "undefines.cpp" // # include "undefines.cpp"
}
+7 -5
View File
@@ -1,4 +1,5 @@
proc create_source_code_location(String* file, int line, int col) -> Source_Code_Location* { namespace Slime {
proc create_source_code_location(String* file, int line, int col) -> Source_Code_Location* {
if (!file) if (!file)
return nullptr; return nullptr;
@@ -7,9 +8,9 @@ proc create_source_code_location(String* file, int line, int col) -> Source_Code
ret->line = line; ret->line = line;
ret->column = col; ret->column = col;
return ret; return ret;
} }
proc Lisp_Object_Type_to_string(Lisp_Object_Type type) -> const char* { proc Lisp_Object_Type_to_string(Lisp_Object_Type type) -> const char* {
switch (type) { switch (type) {
case(Lisp_Object_Type::Nil): return "nil"; case(Lisp_Object_Type::Nil): return "nil";
case(Lisp_Object_Type::T): return "t"; case(Lisp_Object_Type::T): return "t";
@@ -26,9 +27,9 @@ proc Lisp_Object_Type_to_string(Lisp_Object_Type type) -> const char* {
case(Lisp_Object_Type::HashMap): return "hashmap"; case(Lisp_Object_Type::HashMap): return "hashmap";
} }
return "unknown"; return "unknown";
} }
Lisp_Object::~Lisp_Object() { Lisp_Object::~Lisp_Object() {
free(sourceCodeLocation); free(sourceCodeLocation);
sourceCodeLocation = 0; sourceCodeLocation = 0;
@@ -50,4 +51,5 @@ Lisp_Object::~Lisp_Object() {
} break; } break;
default: break; default: break;
} }
}
} }
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Memory { namespace Slime::Memory {
// ------------------ // ------------------
// global symbol / keyword table // global symbol / keyword table
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Parser { namespace Slime::Parser {
String* standard_in; String* standard_in;
String* parser_file; String* parser_file;
int parser_line; int parser_line;
+12 -9
View File
@@ -1,4 +1,6 @@
inline proc get_cwd() -> char* { namespace Slime {
inline proc get_cwd() -> char* {
const int buf_size = 2048; const int buf_size = 2048;
char* res = (char*)malloc(buf_size * sizeof(char)); char* res = (char*)malloc(buf_size * sizeof(char));
@@ -9,19 +11,19 @@ inline proc get_cwd() -> char* {
#endif #endif
return res; return res;
} }
inline proc change_cwd(char* dir) -> void { inline proc change_cwd(char* dir) -> void {
#ifdef _MSC_VER #ifdef _MSC_VER
_chdir(dir); _chdir(dir);
#else #else
chdir(dir); chdir(dir);
#endif #endif
} }
#ifdef _MSC_VER #ifdef _MSC_VER
int vasprintf(char **strp, const char *fmt, va_list ap) { int vasprintf(char **strp, const char *fmt, va_list ap) {
// _vscprintf tells you how big the buffer needs to be // _vscprintf tells you how big the buffer needs to be
int len = _vscprintf(fmt, ap); int len = _vscprintf(fmt, ap);
if (len == -1) { if (len == -1) {
@@ -40,18 +42,18 @@ int vasprintf(char **strp, const char *fmt, va_list ap) {
} }
*strp = str; *strp = str;
return r; return r;
} }
int asprintf(char **strp, const char *fmt, ...) { int asprintf(char **strp, const char *fmt, ...) {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
int r = vasprintf(strp, fmt, ap); int r = vasprintf(strp, fmt, ap);
va_end(ap); va_end(ap);
return r; return r;
} }
#endif #endif
proc get_exe_dir() -> char* { proc get_exe_dir() -> char* {
#ifdef _MSC_VER #ifdef _MSC_VER
DWORD last_error; DWORD last_error;
DWORD result; DWORD result;
@@ -164,4 +166,5 @@ proc get_exe_dir() -> char* {
return path; return path;
#endif #endif
}
} }
+42 -40
View File
@@ -1,13 +1,14 @@
struct Lisp_Object; namespace Slime {
struct String; struct Lisp_Object;
struct Environment; struct String;
struct Environment;
enum struct Thread_Type { enum struct Thread_Type {
Main, Main,
GarbageCollection GarbageCollection
}; };
enum struct Lisp_Object_Type { enum struct Lisp_Object_Type {
Nil, Nil,
T, T,
Symbol, Symbol,
@@ -22,75 +23,75 @@ enum struct Lisp_Object_Type {
// OwningPointer, // OwningPointer,
Function, Function,
CFunction, CFunction,
}; };
enum class Lisp_Object_Flags enum class Lisp_Object_Flags
{ {
// bits 1 to 5 (including) will be reserved for the type // bits 1 to 5 (including) will be reserved for the type
Already_Garbage_Collected = 1 << 5, Already_Garbage_Collected = 1 << 5,
Under_Construction = 1 << 6, Under_Construction = 1 << 6,
}; };
enum struct Function_Type { enum struct Function_Type {
Lambda, Lambda,
Macro Macro
}; };
enum struct Log_Level { enum struct Log_Level {
None, None,
Critical, Critical,
Warning, Warning,
Info, Info,
Debug, Debug,
}; };
struct Continuation { struct Continuation {
Array_List<Lisp_Object*> call_stack; Array_List<Lisp_Object*> call_stack;
Array_List<Environment*> envi_stack; Array_List<Environment*> envi_stack;
}; };
struct String { struct String {
int length; int length;
char data; char data;
}; };
struct Source_Code_Location { struct Source_Code_Location {
String* file; String* file;
int line; int line;
int column; int column;
}; };
struct Pair { struct Pair {
Lisp_Object* first; Lisp_Object* first;
Lisp_Object* rest; Lisp_Object* rest;
}; };
struct Vector { struct Vector {
int length; int length;
Lisp_Object* data; Lisp_Object* data;
}; };
struct Positional_Arguments { struct Positional_Arguments {
Array_List<Lisp_Object*> symbols; Array_List<Lisp_Object*> symbols;
}; };
struct Keyword_Arguments { struct Keyword_Arguments {
// Array of Pointers to Lisp_Object<Keyword> // Array of Pointers to Lisp_Object<Keyword>
Array_List<Lisp_Object*> keywords; Array_List<Lisp_Object*> keywords;
// NOTE(Felix): values[i] will be nullptr if no defalut value was // NOTE(Felix): values[i] will be nullptr if no defalut value was
// declared for key identifiers[i] // declared for key identifiers[i]
Array_List<Lisp_Object*> values; Array_List<Lisp_Object*> values;
}; };
struct Arguments { struct Arguments {
Positional_Arguments positional; Positional_Arguments positional;
Keyword_Arguments keyword; Keyword_Arguments keyword;
// NOTE(Felix): rest_argument will be nullptr if no rest argument // NOTE(Felix): rest_argument will be nullptr if no rest argument
// is declared otherwise its a symbol // is declared otherwise its a symbol
Lisp_Object* rest; Lisp_Object* rest;
}; };
struct Environment { struct Environment {
Array_List<Environment*> parents; Array_List<Environment*> parents;
Hash_Map<void*, Lisp_Object*> hm; Hash_Map<void*, Lisp_Object*> hm;
@@ -98,22 +99,22 @@ struct Environment {
parents.~Array_List(); parents.~Array_List();
hm.~Hash_Map(); hm.~Hash_Map();
} }
}; };
struct Function { struct Function {
Function_Type type; Function_Type type;
Arguments args; Arguments args;
Lisp_Object* body; // maybe implicit begin Lisp_Object* body; // maybe implicit begin
Environment* parent_environment; // we are doing closures now!! Environment* parent_environment; // we are doing closures now!!
}; };
struct cFunction { struct cFunction {
Lisp_Object* (*body)(); Lisp_Object* (*body)();
Arguments args; Arguments args;
bool is_special_form; bool is_special_form;
}; };
struct Lisp_Object { struct Lisp_Object {
Source_Code_Location* sourceCodeLocation; Source_Code_Location* sourceCodeLocation;
u64 flags; u64 flags;
Lisp_Object* userType; // keyword Lisp_Object* userType; // keyword
@@ -132,11 +133,12 @@ struct Lisp_Object {
~value() {} ~value() {}
} value; } value;
~Lisp_Object(); ~Lisp_Object();
}; };
struct Error { struct Error {
Lisp_Object* position; Lisp_Object* position;
// type has to be a keyword // type has to be a keyword
Lisp_Object* type; Lisp_Object* type;
String* message; String* message;
}; };
}
+44 -38
View File
@@ -1,3 +1,5 @@
namespace Slime {
#define epsilon 2.2204460492503131E-16 #define epsilon 2.2204460492503131E-16
#define testresult int #define testresult int
@@ -113,7 +115,7 @@
} \ } \
} }
proc test_array_lists_adding_and_removing() -> testresult { proc test_array_lists_adding_and_removing() -> testresult {
// test adding and removing // test adding and removing
Array_List<int> list; Array_List<int> list;
list.append(1); list.append(1);
@@ -137,9 +139,9 @@ proc test_array_lists_adding_and_removing() -> testresult {
assert_equal_int(list[1], 2); assert_equal_int(list[1], 2);
return pass; return pass;
} }
proc test_array_lists_sorting() -> testresult { proc test_array_lists_sorting() -> testresult {
// test adding and removing // test adding and removing
Array_List<int> list; Array_List<int> list;
list.append(1); list.append(1);
@@ -171,9 +173,9 @@ proc test_array_lists_sorting() -> testresult {
assert_equal_int(list[5], 5); assert_equal_int(list[5], 5);
return pass; return pass;
} }
proc test_array_lists_searching() -> testresult { proc test_array_lists_searching() -> testresult {
Array_List<int> list; Array_List<int> list;
list.append(1); list.append(1);
list.append(2); list.append(2);
@@ -190,9 +192,9 @@ proc test_array_lists_searching() -> testresult {
assert_equal_int(index, -1); assert_equal_int(index, -1);
return pass; return pass;
} }
proc test_eval_operands() -> testresult { proc test_eval_operands() -> testresult {
char operands_string[] = "((eval 1) (+ 1 2) \"okay\" (eval :haha))"; char operands_string[] = "((eval 1) (+ 1 2) \"okay\" (eval :haha))";
Lisp_Object* operands = Parser::parse_single_expression(operands_string); Lisp_Object* operands = Parser::parse_single_expression(operands_string);
try operands = eval_arguments(operands); try operands = eval_arguments(operands);
@@ -222,9 +224,9 @@ proc test_eval_operands() -> testresult {
assert_equal_string(operands->value.pair.first->value.symbol, "haha"); assert_equal_string(operands->value.pair.first->value.symbol, "haha");
return pass; return pass;
} }
proc test_parse_atom() -> testresult { proc test_parse_atom() -> testresult {
int index_in_text = 0; int index_in_text = 0;
char string[] = char string[] =
"123 -1.23e-2 " // numbers "123 -1.23e-2 " // numbers
@@ -278,9 +280,9 @@ proc test_parse_atom() -> testresult {
assert_equal_string(result->value.symbol, "+"); assert_equal_string(result->value.symbol, "+");
return pass; return pass;
} }
proc test_parse_expression() -> testresult { proc test_parse_expression() -> testresult {
int index_in_text = 0; int index_in_text = 0;
char string[] = "(fun + 12)"; char string[] = "(fun + 12)";
@@ -333,9 +335,9 @@ proc test_parse_expression() -> testresult {
result = result->value.pair.rest; result = result->value.pair.rest;
return pass; return pass;
} }
proc test_built_in_add() -> testresult { proc test_built_in_add() -> testresult {
char exp_string[] = "(+ 10 4)"; char exp_string[] = "(+ 10 4)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string); Lisp_Object* expression = Parser::parse_single_expression(exp_string);
Lisp_Object* result; Lisp_Object* result;
@@ -347,9 +349,9 @@ proc test_built_in_add() -> testresult {
assert_equal_double(result->value.number, 14); assert_equal_double(result->value.number, 14);
return pass; return pass;
} }
proc test_built_in_substract() -> testresult { proc test_built_in_substract() -> testresult {
char exp_string[] = "(- 10 4)"; char exp_string[] = "(- 10 4)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string); Lisp_Object* expression = Parser::parse_single_expression(exp_string);
Lisp_Object* result; Lisp_Object* result;
@@ -362,10 +364,10 @@ proc test_built_in_substract() -> testresult {
assert_equal_double(result->value.number, 6); assert_equal_double(result->value.number, 6);
return pass; return pass;
} }
proc test_built_in_multiply() -> testresult { proc test_built_in_multiply() -> testresult {
char exp_string[] = "(* 10 4)"; char exp_string[] = "(* 10 4)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string); Lisp_Object* expression = Parser::parse_single_expression(exp_string);
Lisp_Object* result; Lisp_Object* result;
@@ -377,10 +379,10 @@ proc test_built_in_multiply() -> testresult {
assert_equal_double(result->value.number, 40); assert_equal_double(result->value.number, 40);
return pass; return pass;
} }
proc test_built_in_divide() -> testresult { proc test_built_in_divide() -> testresult {
char exp_string[] = "(/ 20 4)"; char exp_string[] = "(/ 20 4)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string); Lisp_Object* expression = Parser::parse_single_expression(exp_string);
Lisp_Object* result; Lisp_Object* result;
@@ -392,10 +394,10 @@ proc test_built_in_divide() -> testresult {
assert_equal_double(result->value.number, 5); assert_equal_double(result->value.number, 5);
return pass; return pass;
} }
proc test_built_in_if() -> testresult { proc test_built_in_if() -> testresult {
char exp_string1[] = "(if 1 4 5)"; char exp_string1[] = "(if 1 4 5)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string1); Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
Lisp_Object* result; Lisp_Object* result;
@@ -416,9 +418,9 @@ proc test_built_in_if() -> testresult {
assert_equal_double(result->value.number, 5); assert_equal_double(result->value.number, 5);
return pass; return pass;
} }
proc test_built_in_and() -> testresult { proc test_built_in_and() -> testresult {
char exp_string1[] = "(and 1 \"asd\" 4)"; char exp_string1[] = "(and 1 \"asd\" 4)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string1); Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
Lisp_Object* result; Lisp_Object* result;
@@ -438,9 +440,9 @@ proc test_built_in_and() -> testresult {
assert_equal_type(result, Lisp_Object_Type::Nil); assert_equal_type(result, Lisp_Object_Type::Nil);
return pass; return pass;
} }
proc test_built_in_or() -> testresult { proc test_built_in_or() -> testresult {
char exp_string1[] = "(or \"asd\" nil)"; char exp_string1[] = "(or \"asd\" nil)";
Lisp_Object* expression = Parser::parse_single_expression(exp_string1); Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
Lisp_Object* result; Lisp_Object* result;
@@ -460,10 +462,10 @@ proc test_built_in_or() -> testresult {
assert_equal_type(result, Lisp_Object_Type::Nil); assert_equal_type(result, Lisp_Object_Type::Nil);
return pass; return pass;
} }
proc test_built_in_not() -> testresult { proc test_built_in_not() -> testresult {
char exp_string1[] = "(not ())"; char exp_string1[] = "(not ())";
Lisp_Object* expression = Parser::parse_single_expression(exp_string1); Lisp_Object* expression = Parser::parse_single_expression(exp_string1);
Lisp_Object* result; Lisp_Object* result;
@@ -484,9 +486,9 @@ proc test_built_in_not() -> testresult {
assert_equal_type(result, Lisp_Object_Type::Nil); assert_equal_type(result, Lisp_Object_Type::Nil);
return pass; return pass;
} }
proc test_built_in_type() -> testresult { proc test_built_in_type() -> testresult {
// Environment* env; // Environment* env;
// try env = get_root_environment(); // try env = get_root_environment();
@@ -535,9 +537,9 @@ proc test_built_in_type() -> testresult {
assert_equal_string(result->value.symbol, "number"); assert_equal_string(result->value.symbol, "number");
return pass; return pass;
} }
proc test_singular_t_and_nil() -> testresult { proc test_singular_t_and_nil() -> testresult {
// nil testing // nil testing
char exp_string1[] = "()"; char exp_string1[] = "()";
char exp_string2[] = "nil"; char exp_string2[] = "nil";
@@ -567,9 +569,9 @@ proc test_singular_t_and_nil() -> testresult {
assert_not_null(result3); assert_not_null(result3);
return pass; return pass;
} }
proc test_singular_symbols() -> testresult { proc test_singular_symbols() -> testresult {
auto cc_s_aa = Memory::get_symbol("aa"); auto cc_s_aa = Memory::get_symbol("aa");
auto cc_s_aa2 = Memory::get_symbol("aa2"); auto cc_s_aa2 = Memory::get_symbol("aa2");
auto s_s_aa = Memory::get_symbol(Memory::create_string("aa")); auto s_s_aa = Memory::get_symbol(Memory::create_string("aa"));
@@ -580,9 +582,9 @@ proc test_singular_symbols() -> testresult {
assert_not_equal_int(cc_s_aa, cc_s_aa2); assert_not_equal_int(cc_s_aa, cc_s_aa2);
return pass; return pass;
} }
proc test_file(const char* file) -> testresult { proc test_file(const char* file) -> testresult {
profile_with_name(file); profile_with_name(file);
// Memory::reset(); // Memory::reset();
// assert_no_error(); // assert_no_error();
@@ -593,15 +595,17 @@ proc test_file(const char* file) -> testresult {
pop_environment(); pop_environment();
return pass; return pass;
} }
proc run_all_tests() -> bool { proc run_all_tests() -> bool {
profile_this(); profile_this();
bool result = true; bool result = true;
try Memory::init(409600); try Memory::init(409600);
push_environment(Memory::create_child_environment(
get_current_environment()));
printf("-- Util --\n"); printf("-- Util --\n");
invoke_test(test_array_lists_adding_and_removing); invoke_test(test_array_lists_adding_and_removing);
invoke_test(test_array_lists_sorting); invoke_test(test_array_lists_sorting);
@@ -629,6 +633,7 @@ proc run_all_tests() -> bool {
invoke_test(test_singular_t_and_nil); invoke_test(test_singular_t_and_nil);
invoke_test(test_singular_symbols); invoke_test(test_singular_symbols);
pop_environment();
printf("\n-- Test Files --\n"); printf("\n-- Test Files --\n");
invoke_test_script("evaluation_of_default_args"); invoke_test_script("evaluation_of_default_args");
@@ -644,7 +649,7 @@ proc run_all_tests() -> bool {
invoke_test_script("singular_imports"); invoke_test_script("singular_imports");
return result; return result;
} }
#undef epsilon #undef epsilon
#undef testresult #undef testresult
@@ -664,3 +669,4 @@ proc run_all_tests() -> bool {
#undef assert_not_null #undef assert_not_null
#undef invoke_test #undef invoke_test
#undef invoke_test_script #undef invoke_test_script
}
+3 -1
View File
@@ -1,4 +1,5 @@
proc visualize_lisp_machine() -> void { namespace Slime {
proc visualize_lisp_machine() -> void {
// // save the current working directory // // save the current working directory
// char* cwd = get_cwd(); // char* cwd = get_cwd();
@@ -551,4 +552,5 @@ proc visualize_lisp_machine() -> void {
// " viewBox='%012d %012d %012d %012d'" // " viewBox='%012d %012d %012d %012d'"
// ">", -padding, -padding, max_x + 2*padding, max_y + 2*padding); // ">", -padding, -padding, max_x + 2*padding, max_y + 2*padding);
}
} }