little cleanup
This commit is contained in:
+49
-4
@@ -446,6 +446,51 @@ proc load_built_ins_into_environment() -> void {
|
||||
*target = *source;
|
||||
return target;
|
||||
});
|
||||
defun("vector-length", "TODO", __LINE__, cLambda {
|
||||
try evaluated_arguments = eval_arguments(arguments, &arguments_length);
|
||||
try assert_arguments_length(1, arguments_length);
|
||||
Lisp_Object* vec = evaluated_arguments->value.pair.first;
|
||||
assert_type(vec, Lisp_Object_Type::Vector);
|
||||
|
||||
return Memory::create_lisp_object_number((double)vec->value.vector.length);
|
||||
});
|
||||
defun("vector-ref", "TODO", __LINE__, cLambda {
|
||||
try evaluated_arguments = eval_arguments(arguments, &arguments_length);
|
||||
try assert_arguments_length(2, arguments_length);
|
||||
|
||||
Lisp_Object* vec = evaluated_arguments->value.pair.first;
|
||||
Lisp_Object* idx = evaluated_arguments->value.pair.rest->value.pair.first;
|
||||
|
||||
try assert_type(vec, Lisp_Object_Type::Vector);
|
||||
try assert_type(idx, Lisp_Object_Type::Number);
|
||||
|
||||
int int_idx = ((size_t)idx->value.number);
|
||||
|
||||
try assert(int_idx >= 0);
|
||||
try assert(int_idx < vec->value.vector.length);
|
||||
|
||||
return vec->value.vector.data+int_idx;
|
||||
});
|
||||
defun("vector-set!", "TODO", __LINE__, cLambda {
|
||||
try evaluated_arguments = eval_arguments(arguments, &arguments_length);
|
||||
try assert_arguments_length(3, arguments_length);
|
||||
|
||||
Lisp_Object* vec = evaluated_arguments->value.pair.first;
|
||||
Lisp_Object* idx = evaluated_arguments->value.pair.rest->value.pair.first;
|
||||
Lisp_Object* val = evaluated_arguments->value.pair.rest->value.pair.rest->value.pair.first;
|
||||
|
||||
try assert_type(vec, Lisp_Object_Type::Vector);
|
||||
try assert_type(idx, Lisp_Object_Type::Number);
|
||||
|
||||
int int_idx = ((size_t)idx->value.number);
|
||||
|
||||
try assert(int_idx >= 0);
|
||||
try assert(int_idx < vec->value.vector.length);
|
||||
|
||||
vec->value.vector.data[int_idx] = *val;
|
||||
|
||||
return val;
|
||||
});
|
||||
defun("set!", "TODO", __LINE__, cLambda {
|
||||
try assert_arguments_length(2, list_length(arguments));
|
||||
Lisp_Object* target_symbol = arguments->value.pair.first;
|
||||
@@ -912,17 +957,17 @@ proc load_built_ins_into_environment() -> void {
|
||||
}
|
||||
printf("}\n");
|
||||
printf("Keyword: {");
|
||||
if (fun->value.function.keyword_arguments->next_index != 0) {
|
||||
if (fun->value.function.keyword_arguments->values->next_index != 0) {
|
||||
printf("%s",
|
||||
Memory::get_c_str(fun->value.function.keyword_arguments->keywords[0]->value.symbol.identifier));
|
||||
Memory::get_c_str(fun->value.function.keyword_arguments->keywords->data[0]->value.symbol.identifier));
|
||||
if (fun->value.function.keyword_arguments->values->data[0]) {
|
||||
printf(" (");
|
||||
print(fun->value.function.keyword_arguments->values->data[0]);
|
||||
printf(")");
|
||||
}
|
||||
for (int i = 1; i < fun->value.function.keyword_arguments->next_index; ++i) {
|
||||
for (int i = 1; i < fun->value.function.keyword_arguments->values->next_index; ++i) {
|
||||
printf(", %s",
|
||||
Memory::get_c_str(fun->value.function.keyword_arguments->keywords[i]->value.symbol.identifier));
|
||||
Memory::get_c_str(fun->value.function.keyword_arguments->keywords->data[i]->value.symbol.identifier));
|
||||
if (fun->value.function.keyword_arguments->values->data[i]) {
|
||||
printf(" (");
|
||||
print(fun->value.function.keyword_arguments->values->data[i]);
|
||||
|
||||
@@ -98,18 +98,18 @@ proc generate_docs(String* path) -> void {
|
||||
fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.positional_arguments->symbols[i]->value.symbol.identifier));
|
||||
}
|
||||
}
|
||||
if (fun->value.function.keyword_arguments->next_index != 0) {
|
||||
if (fun->value.function.keyword_arguments->values->next_index != 0) {
|
||||
if (!printed_at_least_some_args)
|
||||
fprintf(f, ":");
|
||||
fprintf(f, "\n - keyword :: ");
|
||||
fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.keyword_arguments->keywords[0]->value.symbol.identifier));
|
||||
fprintf(f, "=%s=", Memory::get_c_str(fun->value.function.keyword_arguments->keywords->data[0]->value.symbol.identifier));
|
||||
if (fun->value.function.keyword_arguments->values->data[0]) {
|
||||
fprintf(f, " =(");
|
||||
print(fun->value.function.keyword_arguments->values->data[0], true, f);
|
||||
fprintf(f, ")=");
|
||||
}
|
||||
for (int i = 1; i < fun->value.function.keyword_arguments->next_index; ++i) {
|
||||
fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.keyword_arguments->keywords[i]->value.symbol.identifier));
|
||||
for (int i = 1; i < fun->value.function.keyword_arguments->values->next_index; ++i) {
|
||||
fprintf(f, ", =%s=", Memory::get_c_str(fun->value.function.keyword_arguments->keywords->data[i]->value.symbol.identifier));
|
||||
if (fun->value.function.keyword_arguments->values->data[i]) {
|
||||
fprintf(f, " =(");
|
||||
print(fun->value.function.keyword_arguments->values->data[i], true, f);
|
||||
@@ -124,7 +124,7 @@ proc generate_docs(String* path) -> void {
|
||||
}
|
||||
// if no args at all
|
||||
if (fun->value.function.positional_arguments->next_index == 0 &&
|
||||
fun->value.function.keyword_arguments->next_index == 0 &&
|
||||
fun->value.function.keyword_arguments->values->next_index == 0 &&
|
||||
!fun->value.function.rest_argument)
|
||||
{
|
||||
fprintf(f, "none.");
|
||||
|
||||
+6
-5
@@ -43,7 +43,7 @@ proc apply_arguments_to_function(Lisp_Object* arguments, Function* function) ->
|
||||
return;
|
||||
|
||||
// find out how many keyword args we /have/ to read
|
||||
for (int i = 0; i < function->keyword_arguments->next_index; ++i) {
|
||||
for (int i = 0; i < function->keyword_arguments->values->next_index; ++i) {
|
||||
if (function->keyword_arguments->values->data[i] == nullptr)
|
||||
++obligatory_keywords_count;
|
||||
else
|
||||
@@ -54,10 +54,10 @@ proc apply_arguments_to_function(Lisp_Object* arguments, Function* function) ->
|
||||
while (Memory::get_type(arguments->value.pair.first) == Lisp_Object_Type::Keyword) {
|
||||
// check if this one is even an accepted keyword
|
||||
bool accepted = false;
|
||||
for (int i = 0; i < function->keyword_arguments->next_index; ++i) {
|
||||
for (int i = 0; i < function->keyword_arguments->values->next_index; ++i) {
|
||||
if (string_equal(
|
||||
arguments->value.pair.first->value.symbol.identifier,
|
||||
function->keyword_arguments->keywords[i]->value.symbol.identifier))
|
||||
function->keyword_arguments->keywords->data[i]->value.symbol.identifier))
|
||||
{
|
||||
accepted = true;
|
||||
break;
|
||||
@@ -128,10 +128,11 @@ proc apply_arguments_to_function(Lisp_Object* arguments, Function* function) ->
|
||||
|
||||
proc check_keyword_args = [&]() -> void {
|
||||
// check if all necessary keywords have been read in
|
||||
for (int i = 0; i < function->keyword_arguments->next_index; ++i) {
|
||||
String* defined_keyword = function->keyword_arguments->keywords[i]->value.symbol.identifier;
|
||||
for (int i = 0; i < function->keyword_arguments->values->next_index; ++i) {
|
||||
String* defined_keyword = function->keyword_arguments->keywords->data[i]->value.symbol.identifier;
|
||||
bool was_set = false;
|
||||
for (int j = 0; j < read_in_keywords->next_index; ++j) {
|
||||
// TODO(Felix): Later compare the keywords, not their strings!!
|
||||
if (string_equal(
|
||||
read_in_keywords->data[j],
|
||||
defined_keyword))
|
||||
|
||||
+3
-10
@@ -44,10 +44,8 @@ proc append_to_positional_argument_list(Positional_Arguments* args, Lisp_Object*
|
||||
|
||||
proc create_keyword_argument_list(int initial_capacity) -> Keyword_Arguments* {
|
||||
Keyword_Arguments* ret = new(Keyword_Arguments);
|
||||
ret->keywords = (Lisp_Object**)malloc(initial_capacity * sizeof(Lisp_Object*));
|
||||
ret->values = create_Lisp_Object_array_list(initial_capacity);
|
||||
ret->next_index = 0;
|
||||
ret->length = initial_capacity;
|
||||
ret->keywords = create_Lisp_Object_array_list(initial_capacity);
|
||||
ret->values = create_Lisp_Object_array_list(initial_capacity);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -55,11 +53,6 @@ proc append_to_keyword_argument_list(Keyword_Arguments* args,
|
||||
Lisp_Object* keyword,
|
||||
Lisp_Object* default_value) -> void
|
||||
{
|
||||
if (args->next_index == args->length) {
|
||||
args->length *= 2;
|
||||
args->keywords = (Lisp_Object**)realloc(args->keywords, args->length * sizeof(Lisp_Object*));
|
||||
}
|
||||
|
||||
args->keywords[args->next_index++] = keyword;
|
||||
append_to_array_list(args->keywords, keyword);
|
||||
append_to_array_list(args->values, default_value);
|
||||
}
|
||||
|
||||
+3
-7
@@ -227,13 +227,8 @@ namespace Memory {
|
||||
}
|
||||
|
||||
proc allocate_vector(int size) -> Lisp_Object* {
|
||||
/*
|
||||
int object_memory_size;
|
||||
Int_Array_List* free_spots_in_object_memory;
|
||||
Lisp_Object* object_memory;
|
||||
int next_index_in_object_memory = 0;
|
||||
|
||||
*/
|
||||
// NOTE(Felix): Vectors are now only allocated at the back of
|
||||
// the memory, we don't check the free list at all right now
|
||||
|
||||
if (object_memory_size - next_index_in_object_memory < size) {
|
||||
create_out_of_memory_error(
|
||||
@@ -243,6 +238,7 @@ namespace Memory {
|
||||
"calling Memory::init()");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int start = next_index_in_object_memory;
|
||||
next_index_in_object_memory += size;
|
||||
return object_memory+start;
|
||||
|
||||
+7
-16
@@ -268,27 +268,18 @@ namespace Parser {
|
||||
}
|
||||
|
||||
Lisp_Object* ret = nullptr;
|
||||
// TODO(Felix): use Memory::create_list() here
|
||||
if (quoteType == '\'')
|
||||
try ret = Memory::create_lisp_object_pair(
|
||||
Memory::get_or_create_lisp_object_symbol("quote"),
|
||||
Memory::create_lisp_object_pair(result, Memory::nil));
|
||||
try ret = Memory::create_list(Memory::get_or_create_lisp_object_symbol("quote"), result);
|
||||
else if (quoteType == '`')
|
||||
try ret = Memory::create_lisp_object_pair(
|
||||
Memory::get_or_create_lisp_object_symbol("quasiquote"),
|
||||
Memory::create_lisp_object_pair(result, Memory::nil));
|
||||
try ret = Memory::create_list(Memory::get_or_create_lisp_object_symbol("quasiquote"), result);
|
||||
else if (quoteType == ',')
|
||||
try ret = Memory::create_lisp_object_pair(
|
||||
Memory::get_or_create_lisp_object_symbol("unquote"),
|
||||
Memory::create_lisp_object_pair(result, Memory::nil));
|
||||
try ret = Memory::create_list(Memory::get_or_create_lisp_object_symbol("unquote"), result);
|
||||
else if (quoteType == '@') {
|
||||
try ret = Memory::create_lisp_object_pair(
|
||||
Memory::get_or_create_lisp_object_symbol("unquote-splicing"),
|
||||
Memory::create_lisp_object_pair(result, Memory::nil));
|
||||
}
|
||||
if (ret) {
|
||||
inject_scl(ret);
|
||||
try ret = Memory::create_list(Memory::get_or_create_lisp_object_symbol("unquote-splicing"), result);
|
||||
}
|
||||
|
||||
if (ret) inject_scl(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
+2
-4
@@ -89,13 +89,11 @@ struct Positional_Arguments {
|
||||
};
|
||||
|
||||
struct Keyword_Arguments {
|
||||
Lisp_Object** keywords; // Array of Pointers to Lisp_Object<Keyword>
|
||||
// Array of Pointers to Lisp_Object<Keyword>
|
||||
Lisp_Object_Array_List* keywords;
|
||||
// NOTE(Felix): values[i] will be nullptr if no defalut value was
|
||||
// declared for key identifiers[i]
|
||||
Lisp_Object_Array_List* values;
|
||||
// TODO(Felix): Why do we use a Array list here??
|
||||
int next_index;
|
||||
int length;
|
||||
};
|
||||
|
||||
struct Function {
|
||||
|
||||
Reference in New Issue
Block a user