better errors and new define syntax

This commit is contained in:
FelixBrendel
2019-03-13 16:01:40 +01:00
parent 181ace1eed
commit edef0b017b
12 changed files with 276 additions and 426 deletions
+65 -10
View File
@@ -1,8 +1,9 @@
namespace Parser {
String* standard_in;
String* parser_file;
int parser_line;
int parser_col;
// NOTE(Felix): In this environment, the build in functions will
// be loaded, and the macros will be stored in form of
// special-lambdas, that get executed in this environment at
@@ -19,6 +20,7 @@ namespace Parser {
// change that, we have to define some funcions in this
// environment.
environment_for_macros = env;
standard_in = Memory::create_string("stdin");
}
proc inject_scl(Lisp_Object* lo) -> void {
@@ -98,6 +100,7 @@ namespace Parser {
// update the index to point to the character after the atom
// ended
*index_in_text += atom_length;
parser_col += atom_length;
return ret;
}
@@ -147,6 +150,7 @@ namespace Parser {
// plus one because we want to go after the quotes
*index_in_text += 1;
++parser_col;
return ret;
}
@@ -170,18 +174,31 @@ namespace Parser {
create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
// TODO(Felix): manually copy to parse control sequences
// correctly without the need to unescape the string, also
// better for keeping track of the encountered new lines and
// characters since last new line so we can update the parser
// location more easily
strcpy(&string->data, text+(*index_in_text));
/* manually copy to parse control sequences correctly */
/* int temp_index = 0; */
/* while (text+(temp_index+(*index_in_text)) != '\0') { */
/* string[temp_index++] = text[temp_index+(*index_in_text)]; */
/* } */
/* string[temp_index++] = '\0'; */
text[*index_in_text+string_length] = '"';
*index_in_text += string_length +1; // plus one because we want to
// go after the quotes
// plus one because we want to go after the quotes
*index_in_text += string_length +1;
// NOTE(Felix): this only has to be done until we manually
// copy the string and we can do some bookeeping:
/* recalculate the parser cursors position: */
/* new col = (count chars since last \n) + 1 */
for (int i = 0; i < string->length; ++i) {
if (*((&string->data)+i) == '\n') {
++parser_line;
parser_col = 0;
} else {
++parser_col;
}
}
Lisp_Object* ret = Memory::create_lisp_object_string(string);
inject_scl(ret);
@@ -445,7 +462,7 @@ namespace Parser {
}
proc parse_single_expression(char* text) -> Lisp_Object* {
parser_file = Memory::create_string("stdin");
parser_file = standard_in;
parser_line = 1;
parser_col = 1;
@@ -474,6 +491,44 @@ namespace Parser {
return nullptr;
}
proc parse_single_expression_or_bare_words(char* text, char* bare) -> Lisp_Object* {
parser_file = standard_in;
parser_line = 1;
parser_col = 1;
int index_in_text = 0;
Lisp_Object* result;
eat_until_code(text, &index_in_text);
if (text[(index_in_text)] == '\0')
return Memory::nil;
if (text[index_in_text] == '(' ||
text[index_in_text] == '\'' ||
text[index_in_text] == '`' ||
text[index_in_text] == ',')
{
try {
result = parse_expression(text, &index_in_text);
}
return result;
}
else {
int pos = index_in_text;
int end_pos = index_in_text;
while (text[end_pos] != '\n')
++end_pos;
text[end_pos] = '\0';
Lisp_Object* str = Memory::create_lisp_object_string(
Memory::create_string(text+index_in_text));
text[end_pos] = '\n';
return Memory::create_list(
Memory::get_or_create_lisp_object_symbol(bare), str);
}
}
proc write_expanded_file(String* file_name, Lisp_Object_Array_List* program) -> void {
const char* ext = ".expanded";
char* newName = (char*)calloc(10 + file_name->length, sizeof(char));