switched to form char* to Strings mostly

This commit is contained in:
FelixBrendel
2019-01-28 23:03:54 +01:00
parent 77fffc7f5e
commit 0a37967fa6
11 changed files with 234 additions and 145 deletions
+35 -33
View File
@@ -1,15 +1,8 @@
namespace Parser {
#define inject_scl(_ret) \
_ret->sourceCodeLocation = new(Source_Code_Location); \
_ret->sourceCodeLocation->file = parser_file; \
_ret->sourceCodeLocation->line = parser_line; \
_ret->sourceCodeLocation->column = parser_col
const char* parser_file;
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 stroed in form of
// special-lambdas, that get executed in this environment at
@@ -28,6 +21,13 @@ namespace Parser {
environment_for_macros = env;
}
proc inject_scl(Lisp_Object* lo) -> void {
lo->sourceCodeLocation = new(Source_Code_Location);
lo->sourceCodeLocation->file = parser_file;
lo->sourceCodeLocation->line = parser_line;
lo->sourceCodeLocation->column = parser_col;
}
void eat_comment_line(char* text, int* index_in_text) {
// safety check if we are actually starting a comment here
if (text[*index_in_text] != ';')
@@ -68,7 +68,7 @@ namespace Parser {
} while (position_before != *index_in_text);
}
char* read_atom(char* text, int* index_in_text) {
String* read_atom(char* text, int* index_in_text) {
int atom_length = 0;
while (text[*index_in_text+atom_length] != ' ' &&
text[*index_in_text+atom_length] != ')' &&
@@ -88,8 +88,9 @@ namespace Parser {
text[*index_in_text+atom_length] = '\0';
// get the atom
char* atom = (char*)malloc(atom_length*sizeof(char)+1); // plus null char
strcpy(atom, text+(*index_in_text));
String* ret = Memory::create_string("", atom_length);
// char* atom = (char*)malloc(atom_length*sizeof(char)+1); // plus null char
strcpy(&ret->data, text+(*index_in_text));
// restore the original string
text[*index_in_text+atom_length] = before;
@@ -98,14 +99,17 @@ namespace Parser {
// ended
*index_in_text += atom_length;
return atom;
return ret;
}
Lisp_Object* parse_number(char* text, int* index_in_text) {
double number;
char* str_number = read_atom(text, index_in_text);
sscanf(str_number, "%lf", &number);
// TODO(Felix): parse the number direcrly from the string and
// dont create a String first
String* str_number = read_atom(text, index_in_text);
sscanf(Memory::get_c_str(str_number), "%lf", &number);
Lisp_Object* ret = Memory::create_lisp_object_number(number);
inject_scl(ret);
return ret;
}
@@ -114,15 +118,16 @@ namespace Parser {
// we are now on the colon
++(*index_in_text);
++parser_col;
char* str_keyword = read_atom(text, index_in_text);
String* str_keyword = read_atom(text, index_in_text);
Lisp_Object* ret = Memory::create_lisp_object_keyword(str_keyword);
inject_scl(ret);
return ret;
}
Lisp_Object* parse_symbol(char* text, int* index_in_text) {
// we are now at the first char of the symbol
char* str_symbol = read_atom(text, index_in_text);
String* str_symbol = read_atom(text, index_in_text);
Lisp_Object* ret = Memory::create_lisp_object_symbol(str_symbol);
inject_scl(ret);
return ret;
@@ -136,9 +141,8 @@ namespace Parser {
// now we are at the first letter, if this is the closing '"' then
// it's easy
if (text[*index_in_text] == '"') {
char* str = new(char);
*str = '\0';
Lisp_Object* ret = Memory::create_lisp_object_string(str, 0);
Lisp_Object* ret = Memory::create_lisp_object_string(
Memory::create_string("", 0));
inject_scl(ret);
// plus one because we want to go after the quotes
@@ -158,7 +162,7 @@ namespace Parser {
// we found the end of the string
text[*index_in_text+string_length] = '\0';
char* string = (char*)malloc(string_length*sizeof(char)+1); // plus null char
String* string = Memory::create_string("", string_length);
if (!unescape_string(text+(*index_in_text))) {
create_error(
@@ -166,7 +170,7 @@ namespace Parser {
create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
strcpy(string, text+(*index_in_text));
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') { */
@@ -179,7 +183,7 @@ namespace Parser {
*index_in_text += string_length +1; // plus one because we want to
// go after the quotes
Lisp_Object* ret = Memory::create_lisp_object_string(string, string_length);
Lisp_Object* ret = Memory::create_lisp_object_string(string);
inject_scl(ret);
return ret;
}
@@ -371,7 +375,7 @@ namespace Parser {
arguments = arguments->value.pair->rest;
// if there is a docstring, use it
if (arguments->value.pair->first->type == Lisp_Object_Type::String) {
function->docstring = arguments->value.pair->first->value.string->value;
function->docstring = arguments->value.pair->first->value.string;
arguments = arguments->value.pair->rest;
} else {
function->docstring = nullptr;
@@ -437,7 +441,7 @@ namespace Parser {
}
Lisp_Object* parse_single_expression(char* text) {
parser_file = "stdin";
parser_file = Memory::create_string("stdin");
parser_line = 1;
parser_col = 1;
@@ -466,10 +470,10 @@ namespace Parser {
return nullptr;
}
void write_expanded_file(const char* file_name, Lisp_Object_Array_List* program) {
void write_expanded_file(String* file_name, Lisp_Object_Array_List* program) {
const char* ext = ".expanded";
char* newName = (char*)calloc(4 + strlen(file_name), sizeof(char));
strcpy(newName, file_name);
char* newName = (char*)calloc(10 + file_name->length, sizeof(char));
strcpy(newName, Memory::get_c_str(file_name));
strcat(newName, ext);
FILE *f = fopen(newName, "w");
@@ -487,15 +491,15 @@ namespace Parser {
}
fclose(f);
free(newName);
}
Lisp_Object_Array_List* parse_program(const char* file_name, char* text) {
parser_file = (char*)malloc(strlen(file_name) * sizeof(char) + 1);
strcpy((char *)parser_file, file_name);
Lisp_Object_Array_List* parse_program(String* file_name, char* text) {
parser_file = file_name;
parser_line = 1;
parser_col = 0;
Lisp_Object_Array_List* program = create_Lisp_Object_array_list(16);
Lisp_Object_Array_List* program = create_Lisp_Object_array_list();
int index_in_text = 0;
@@ -526,6 +530,4 @@ namespace Parser {
return program;
}
#undef inject_scl
}