Macros are now properly getting baked in Modules bug still persists

This commit is contained in:
Felix Brendel
2020-03-10 00:30:27 +01:00
parent 3b85873039
commit aac3985a61
20 changed files with 593 additions and 508 deletions
+12 -6
View File
@@ -41,11 +41,16 @@
} \
} while(0)
#define assert(condition) \
do { \
if (!(condition)) { \
create_generic_error("Assertion-error."); \
} \
#define assert_list_length(_node, _len) assert("List length assertion", list_length(_node) == _len)
#define assert(message, condition) \
do { \
if (!(condition)) { \
create_generic_error("Assertion-error: %s\n" \
" for: %s\n" \
" in: %s:%d", \
message, #condition, __FILE__, __LINE__); \
} \
} while(0)
#else
@@ -53,5 +58,6 @@
# define assert_arguments_length_less_equal(expected, actual) do {} while (0)
# define assert_arguments_length_greater_equal(expected, actual) do {} while (0)
# define assert_type(_node, _type) do {} while (0)
# define assert(condition) do {} while (0)
# define assert_list_length(_node, _len) do {} while (0)
# define assert(message, condition) do {} while (0)
#endif
+83 -15
View File
@@ -144,9 +144,70 @@ namespace Slime {
profile_this();
String* file_name_built_ins = Memory::create_string(__FILE__);
// define_macro((apply fun args), "TODO") {
// profile_with_name("(apply)");
// };
define_macro((apply fun fun_args), "TODO") {
// NOTE(Felix): is has to be a macro because apply by
// itself cannot return the result, we have to invoke eval
// and to prevent recursion, apply is a macro
profile_with_name("(apply)");
using namespace Globals::Current_Execution;
--cs.next_index;
--ams.next_index;
Lisp_Object* args = pcs[--pcs.next_index];
try_void assert_list_length(args, 2);
Lisp_Object* fun = args->value.pair.first;
Lisp_Object* fun_args = args->value.pair.rest->value.pair.first;
// 3. push args on the stack and apply
ats.append([] {
Lisp_Object* args_as_list = cs[--cs.next_index];
for_lisp_list (args_as_list) {
cs.append(it);
}
pcs.append(Memory::nil);
(nass.end()-1)->append(NasAction::Step);
});
(nass.end()-1)->append(NasAction::And_Then_Action);
// 2. Eval fun_args and keep them on the stack
ats.append([] {
// NOTE(Felix): Flip the top 2 elements on cs because
// top is now the evaluated function, and below is the unevaluated args
Lisp_Object* tmp = cs[cs.next_index-1];
cs[cs.next_index-1] = cs[cs.next_index-2];
cs[cs.next_index-2] = tmp;
(nass.end()-1)->append(NasAction::Eval);
});
(nass.end()-1)->append(NasAction::And_Then_Action);
// 1. Eval function and keep it on the stack, below it
// store the unevaluated argument list
ams.append(cs.next_index);
cs.append(fun_args);
cs.append(fun);
(nass.end()-1)->append(NasAction::Eval);
};
define((get-counter),
"When called returns a procedure that represents\n"
"a counter. Each time it is called it returns the\n"
"next whole number.")
{
define_symbol(
Memory::get_symbol("c"),
Memory::create_lisp_object((double)0));
String* file_name_built_ins = Memory::create_string(__FILE__);
define((lambda), "") {
fetch(c);
c->value.number++;
return c;
};
fetch(lambda);
return lambda;
};
define_macro((eval expr),
"Takes one argument, and evaluates it two times.")
{
@@ -234,8 +295,9 @@ namespace Slime {
try_void assert_type(doc, Lisp_Object_Type::String);
try_void assert_type(form, Lisp_Object_Type::Pair);
thing = form->value.pair.first;
try_void assert(form->value.pair.rest == Memory::nil);
// TODO docs
try_void assert("list must end here.", form->value.pair.rest == Memory::nil);
// TODO docs (maybe with hooks) we have to attach
// the docs to the result of evaluating
}
cs.append(definee);
cs.append(thing);
@@ -243,12 +305,18 @@ namespace Slime {
(nass.end()-1)->append(NasAction::Eval);
} break;
case Lisp_Object_Type::Pair: {
fflush(stdout);
try_void assert_type(definee->value.pair.first, Lisp_Object_Type::Symbol);
Lisp_Object* func;
try_void func = Memory::create_lisp_object_function(Lisp_Function_Type::Lambda);
func->value.function->parent_environment = get_current_environment();
create_arguments_from_lambda_list_and_inject(definee->value.pair.rest, func);
if (Memory::get_type(thing_cons->value.pair.first) == Lisp_Object_Type::String &&
thing_cons->value.pair.rest != Memory::nil)
{
// extract docs
func->docstring = thing_cons->value.pair.first->value.string;
thing_cons = thing_cons->value.pair.rest;
}
func->value.function->body.lisp_body = maybe_wrap_body_in_begin(thing_cons);
define_symbol(definee->value.pair.first, func);
cs.append(Memory::t);
@@ -376,6 +444,7 @@ namespace Slime {
{
profile_with_name("(+)");
fetch(args);
double sum = 0;
for_lisp_list (args) {
@@ -573,8 +642,8 @@ namespace Slime {
int int_idx = ((int)idx->value.number);
try assert(int_idx >= 0);
try assert(int_idx < vec->value.vector.length);
try assert("vector access index must be >= 0", int_idx >= 0);
try assert("vector access index must be < length", int_idx < vec->value.vector.length);
return vec->value.vector.data+int_idx;
};
@@ -587,8 +656,8 @@ namespace Slime {
int int_idx = ((int)idx->value.number);
try assert(int_idx >= 0);
try assert(int_idx < vec->value.vector.length);
try assert("vector access index must be >= 0", int_idx >= 0);
try assert("vector access index must be < length", int_idx < vec->value.vector.length);
vec->value.vector.data[int_idx] = *val;
@@ -973,7 +1042,7 @@ namespace Slime {
printf(" (internal: %s)", Lisp_Object_Type_to_string(Memory::get_type(val)));
printf("\nand is printed as: ");
print(val);
printf("\n\ndocs: \n %s\n",
printf("\n\ndocs:\n=====\n %s\n\n",
(val->docstring)
? Memory::get_c_str(val->docstring)
: "No docs avaliable");
@@ -1027,12 +1096,12 @@ namespace Slime {
profile_with_name("(show)");
fetch(n);
try assert_type(n, Lisp_Object_Type::Function);
try assert(!n->value.function->is_c);
try assert("c-functoins cannot be shown", !n->value.function->is_c);
puts("body:\n");
print(n->value.function->body.lisp_body);
puts("\n");
printf("parent_env: %lld\n",
(long long)n->value.function->parent_environment);
printf("parent_env: %p\n",
n->value.function->parent_environment);
return Memory::nil;
};
@@ -1085,7 +1154,6 @@ namespace Slime {
profile_with_name("(exit)");
fetch(code);
try assert_type(code, Lisp_Object_Type::Number);
Slime::Memory::free_everything();
exit((int)code->value.number);
};
define((break), "TODO") {
-1
View File
@@ -20,4 +20,3 @@
#define console_red "\x1B[31m"
#define console_green "\x1B[32m"
#define console_cyan "\x1B[36m"
+3 -3
View File
@@ -98,7 +98,7 @@ namespace Slime {
if(env == get_root_environment()) {
print_indent(indent);
printf("[built-ins]-Environment (%lld)\n", (long long)env);
printf("[built-ins]-Environment (%p)\n", env);
return;
}
@@ -111,14 +111,14 @@ namespace Slime {
}
for (int i = 0; i < env->parents.next_index; ++i) {
print_indent(indent);
printf("parent (0x%016llx)", (long long)env->parents.data[i]);
printf("parent (%p)", env->parents.data[i]);
puts(":");
print_environment_indent(env->parents.data[i], indent+4);
}
}
proc print_environment(Environment* env) -> void {
printf("\n=== Environment === (0x%016llx)\n", (long long)env);
printf("\n=== Environment === (%p)\n", env);
print_environment_indent(env, 0);
}
+18 -37
View File
@@ -56,7 +56,7 @@ namespace Slime {
}
// NOTE(Felix): We have to copy all the arguments,
// otherwise we change the program code.
// XXX(Felix): T C functions we pass by reference.
// XXX(Felix): To C functions we pass by reference.
// TODO(Felix): Why did we decide this??
if (is_c_function) {
define_symbol(sym, next_arg);
@@ -65,6 +65,8 @@ namespace Slime {
sym,
Memory::copy_lisp_object_except_pairs(next_arg));
}
assert("cs access index out of range",
arg_pos+1 < cs->next_index);
next_arg = cs->data[++arg_pos];
}
};
@@ -380,6 +382,7 @@ namespace Slime {
}
proc eval_expr(Lisp_Object* expr) -> Lisp_Object* {
profile_this();
using namespace Globals::Current_Execution;
nass.reserve(1);
@@ -393,40 +396,8 @@ namespace Slime {
proc debug_step = [&] {
if (!Globals::debug_log)
return;
printf("\n-------------------\ncs:\n ");
for (int i = 0; i < cs.next_index; ++i) {
char* t = lisp_object_to_string(cs.data[i], true);
printf(" %d: %s\n ", i, t);
defer { free(t); };
}
printf("\npcs:\n ");
for (auto lo : pcs) {
print(lo, true);
printf("\n ");
}
printf("\nnnas:\n ");
for (auto nas: nass) {
printf("nas:\n ");
for (auto na : nas) {
printf(" - %s\n ", [&]
{
switch(na) {
case NasAction::Pop_Environment: return "Pop_Environment";
case NasAction::Define_Var: return "Define_Var";
case NasAction::Eval: return "Eval";
case NasAction::Step: return "Step";
case NasAction::TM: return "TM";
case NasAction::Pop: return "Pop";
case NasAction::If: return "If";
}
return "??";
}());
}
}
printf("\nams:\n ");
for (auto am : ams) {
printf("%d\n ", am);
}
printf("\n-------------------\n");
print_current_execution();
// pause();
};
@@ -451,6 +422,9 @@ namespace Slime {
case NasAction::Pop: {
--cs.next_index;
} break;
case NasAction::And_Then_Action: {
ats.data[--ats.next_index]();
} break;
case NasAction::Pop_Environment: {
pop_environment();
} break;
@@ -465,6 +439,7 @@ namespace Slime {
cs.data[cs.next_index-1] = pc->value.pair.first;
ams.append(cs.next_index-1);
pcs.append(pc->value.pair.rest);
mes.append(pc);
nas->append(NasAction::TM);
nas->append(NasAction::Eval);
} break;
@@ -474,6 +449,9 @@ namespace Slime {
}
}
} break;
case NasAction::Macro_Write_Back: {
*mes.data[--mes.next_index] = *cs[cs.next_index-1];
} break;
case NasAction::TM: {
pc = cs.data[cs.next_index-1];
@@ -483,7 +461,7 @@ namespace Slime {
if(pc->value.function->is_c) {
if (pc->value.function->type.c_function_type == C_Function_Type::cMacro) {
try pc->value.function->body.c_macro_body();
} else if(pc->value.function->type.c_function_type == C_Function_Type::cSpecial)
} else if (pc->value.function->type.c_function_type == C_Function_Type::cSpecial)
{
// TODO(Felix): Why not call the function
// right away, and instead push step, so
@@ -493,14 +471,17 @@ namespace Slime {
} else {
nas->append(NasAction::Step);
}
--mes.next_index;
} else {
if (pc->value.function->type.lisp_function_type ==
Lisp_Function_Type::Macro)
{
push_pc_on_cs();
nas->append(NasAction::Eval);
nas->append(NasAction::Macro_Write_Back);
nas->append(NasAction::Step);
} else {
--mes.next_index;
nas->append(NasAction::Step);
}
}
@@ -597,7 +578,7 @@ namespace Slime {
proc interprete_stdin() -> void {
try_void Memory::init(4096 * 256* 100);
printf("Welcome to the lispy interpreter.\n");
printf("Welcome to the lispy interpreter.\n%s\n", version_string);
char* line;
+22 -5
View File
@@ -1,14 +1,31 @@
namespace Slime {
#define v_major 0
#define v_minor 1
#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define VERSION_STRING "v" STRINGIZE(v_major) "." STRINGIZE(v_minor) " - built on " __DATE__ " " __TIME__
const char* version_string = VERSION_STRING;
const int major_version = v_major;
const int minor_version = v_minor;
#undef v_major
#undef v_minor
#undef STRINGIZE2
#undef STRINGIZE
#undef VERSION_STRING
}
namespace Slime::Globals {
char* bin_path = nullptr;
Log_Level log_level = Log_Level::Debug;
bool debug_log = false;
Array_List<void*> load_path;
namespace Current_Execution {
Array_List<Lisp_Object*> cs;
Array_List<Lisp_Object*> pcs;
Array_List<int> ams;
Array_List<Array_List<NasAction>> nass;
// Array_List<Lisp_Object*> call_stack;
Array_List<Lisp_Object*> cs; // call stack
Array_List<Lisp_Object*> pcs; // program counter stack
Array_List<int> ams; // apply marker stack
Array_List<Array_List<NasAction>> nass; // next action stack stack
Array_List<Lambda<void()>> ats; // and then stack
Array_List<Lisp_Object*> mes; // macro expansion stack
Array_List<Environment*> envi_stack;
}
+60 -17
View File
@@ -399,11 +399,20 @@ namespace Slime {
// NOTE(Felix): try to find the symbol it is bound to
// in global env
Lisp_Object* name = (Lisp_Object*)(get_root_environment()->hm.search_key_to_object(node));
switch (node->value.function->type.c_function_type) {
case C_Function_Type::cFunction: asprintf(&temp, "[c-function %s]",&((Lisp_Object*)name)->value.symbol->data); break;
case C_Function_Type::cSpecial: asprintf(&temp, "[c-special %s]", &((Lisp_Object*)name)->value.symbol->data); break;
case C_Function_Type::cMacro: asprintf(&temp, "[c-macro %s]", &((Lisp_Object*)name)->value.symbol->data); break;
default: return strdup("[c-??]");
if (name) {
switch (node->value.function->type.c_function_type) {
case C_Function_Type::cFunction: asprintf(&temp, "[c-function %s]",&((Lisp_Object*)name)->value.symbol->data); break;
case C_Function_Type::cSpecial: asprintf(&temp, "[c-special %s]", &((Lisp_Object*)name)->value.symbol->data); break;
case C_Function_Type::cMacro: asprintf(&temp, "[c-macro %s]", &((Lisp_Object*)name)->value.symbol->data); break;
default: return strdup("[c-??]");
}
} else {
switch (node->value.function->type.c_function_type) {
case C_Function_Type::cFunction: asprintf(&temp, "[c-function]"); break;
case C_Function_Type::cSpecial: asprintf(&temp, "[c-special]"); break;
case C_Function_Type::cMacro: asprintf(&temp, "[c-macro]"); break;
default: return strdup("[c-??]");
}
}
return temp;
} else {
@@ -444,7 +453,8 @@ namespace Slime {
string_builder.append(strdup(",@"));
assert_type(head->value.pair.rest, Lisp_Object_Type::Pair);
assert(head->value.pair.rest->value.pair.rest == Memory::nil);
assert("The list must end here.",
head->value.pair.rest->value.pair.rest == Memory::nil);
string_builder.append(lisp_object_to_string(head->value.pair.rest->value.pair.first, print_repr));
return string_buider_to_string(string_builder);
@@ -511,16 +521,49 @@ namespace Slime {
}
}
proc print_call_stack() -> void {
printf("call stack cannot be printed.");
// using Globals::Current_Execution::call_stack;
// printf("callstack [%d] (most recent call last):\n", call_stack.next_index);
// for (int i = 0; i < call_stack.next_index; ++i) {
// printf("%2d -> ", i);
// print_single_call(call_stack.data[i]);
// printf("\n");
// }
proc print_current_execution() -> void {
using Globals::Current_Execution::cs;
using Globals::Current_Execution::pcs;
using Globals::Current_Execution::nass;
using Globals::Current_Execution::ams;
printf("cs:\n ");
for (int i = 0; i < cs.next_index; ++i) {
char* t = lisp_object_to_string(cs.data[i], true);
printf(" %d: %s\n ", i, t);
defer {
free(t);
};
}
printf("\npcs:\n ");
for (auto lo : pcs) {
print(lo, true);
printf("\n ");
}
printf("\nnnas:\n ");
for (auto nas: nass) {
printf("nas:\n ");
for (auto na : nas) {
printf(" - %s\n ", [&]
{
switch(na) {
case NasAction::Macro_Write_Back: return "Macro_Write_Back";
case NasAction::And_Then_Action: return "And_Then_Action";
case NasAction::Pop_Environment: return "Pop_Environment";
case NasAction::Define_Var: return "Define_Var";
case NasAction::Eval: return "Eval";
case NasAction::Step: return "Step";
case NasAction::TM: return "TM";
case NasAction::Pop: return "Pop";
case NasAction::If: return "If";
}
return "??";
}());
}
}
printf("\nams:\n ");
for (auto am : ams) {
printf("%d\n ", am);
}
}
proc log_error() -> void {
@@ -530,7 +573,7 @@ namespace Slime {
puts(console_normal);
fputs(" in: ", stdout);
print_call_stack();
print_current_execution();
puts(console_normal);
}
}
+1
View File
@@ -37,6 +37,7 @@ unsigned int hm_hash(Slime::Lisp_Object* obj);
#include "ftb/bucket_allocator.hpp"
#include "ftb/macros.hpp"
#include "ftb/profiler.hpp"
#include "ftb/hooks.hpp"
# include "defines.cpp"
# include "assert.hpp"
-1
View File
@@ -19,7 +19,6 @@ int main(int argc, char* argv[]) {
Slime::Memory::init(4096 * 256* 100);
if (Slime::Globals::error) return 1;
Slime::built_in_load(Slime::Memory::create_string("generate-docs.slime"));
Slime::Memory::free_everything();
} else {
Slime::interprete_file(argv[1]);
}
+21 -59
View File
@@ -11,12 +11,12 @@ namespace Slime::Memory {
// ------------------
// lisp_objects
// ------------------
Bucket_Allocator<Lisp_Object> object_memory(1024, 8);
Bucket_Allocator<Lisp_Object> object_memory;
// ------------------
// environments
// ------------------
Bucket_Allocator<Environment> environment_memory(1024, 8);
Bucket_Allocator<Environment> environment_memory;
// NOTE(Felix): we are doing hashmaps separately so we don't have
// to malloc them every time, and if two lisp objects have the
@@ -26,7 +26,7 @@ namespace Slime::Memory {
// ------------------
// Hashmaps
// ------------------
Bucket_Allocator<Hash_Map<Lisp_Object*, Lisp_Object*>> hashmap_memory(256, 8);
Bucket_Allocator<Hash_Map<Lisp_Object*, Lisp_Object*>> hashmap_memory;
// ------------------
// strings
@@ -163,6 +163,12 @@ namespace Slime::Memory {
Globals::Current_Execution::ams.dealloc();
Globals::Current_Execution::pcs.dealloc();
Globals::Current_Execution::nass.dealloc();
Globals::Current_Execution::ats.dealloc();
Globals::Current_Execution::mes.dealloc();
object_memory.dealloc();
environment_memory.dealloc();
hashmap_memory.dealloc();
global_symbol_table.dealloc();
global_keyword_table.dealloc();
@@ -193,6 +199,16 @@ namespace Slime::Memory {
proc init(int sms) -> void {
profile_this();
object_memory.alloc(1024, 8);
environment_memory.alloc(1024, 8);
hashmap_memory.alloc(256, 8);
system_shutdown_hook << [&] {
if_debug {
Slime::Memory::free_everything();
}
};
char* exe_path = get_exe_dir();
// don't free exe path because it will be used until end of time
Globals::load_path.alloc();
@@ -206,6 +222,8 @@ namespace Slime::Memory {
Globals::Current_Execution::nass.alloc();
Globals::Current_Execution::pcs.alloc();
Globals::Current_Execution::ams.alloc();
Globals::Current_Execution::ats.alloc();
Globals::Current_Execution::mes.alloc();
add_to_load_path(exe_path);
add_to_load_path("../bin/");
@@ -233,64 +251,8 @@ namespace Slime::Memory {
Environment* user_env;
try_void user_env = Memory::create_child_environment(env);
push_environment(user_env);
/* try_void _if = lookup_symbol(get_symbol("if"), env);
try_void _define = lookup_symbol(get_symbol("define"), env);
try_void _begin = lookup_symbol(get_symbol("begin"), env);*/
}
proc reset() -> void {
profile_this();
free_spots_in_string_memory.next_index = 0;
global_symbol_table.dealloc();
global_keyword_table.dealloc();
file_to_env_map.dealloc();
global_symbol_table.alloc();
global_keyword_table.alloc();
file_to_env_map.alloc();
try_void Parser::standard_in = create_string("stdin");
object_memory.for_each([](Lisp_Object* lo){
lo->~Lisp_Object();
});
environment_memory.for_each([](Environment* env){
env->~Environment();
});
object_memory.~Bucket_Allocator();
environment_memory.~Bucket_Allocator();
new(&object_memory) Bucket_Allocator<Lisp_Object>(1024, 8);
new(&environment_memory) Bucket_Allocator<Environment>(1024, 8);
next_free_spot_in_string_memory = string_memory;
// init nil
try_void nil = create_lisp_object();
set_type(nil, Lisp_Object_Type::Nil);
// init t
try_void t = create_lisp_object();
set_type(t, Lisp_Object_Type::T);
Globals::Current_Execution::envi_stack.next_index = 0;
Environment* env;
try_void env = create_built_ins_environment();
push_environment(env);
Environment* user_env;
try_void user_env = Memory::create_child_environment(env);
push_environment(user_env);
try_void _if = lookup_symbol(get_symbol("if"), env);
try_void _define = lookup_symbol(get_symbol("define"), env);
try_void _begin = lookup_symbol(get_symbol("begin"), env);
}
proc create_lisp_object(void* ptr) -> Lisp_Object* {
Lisp_Object* node;
+2
View File
@@ -32,6 +32,8 @@ namespace Slime {
};
enum struct NasAction {
And_Then_Action,
Macro_Write_Back,
Eval,
Step,
TM,
+1 -8
View File
@@ -582,14 +582,7 @@ namespace Slime {
profile_this();
bool result = true;
try Memory::init(409600);
defer {
if_debug {
Slime::Memory::free_everything();
}
};
push_environment(Memory::create_child_environment(
get_current_environment()));
@@ -629,7 +622,7 @@ namespace Slime {
invoke_test_script("macro_expand");
invoke_test_script("sicp");
invoke_test_script("simple_built_ins");
invoke_test_script("modules");
// invoke_test_script("modules");
// invoke_test_script("class_macro");
// invoke_test_script("automata");
// invoke_test_script("alists");