we have macros. OOOOOOFFF

This commit is contained in:
FelixBrendel
2018-12-28 18:48:07 +01:00
parent 9b5692da9d
commit 93e180a8f1
12 changed files with 803 additions and 641 deletions
+435 -317
View File
@@ -1,358 +1,476 @@
char* parser_file;
int parser_line;
int parser_col;
// forward decls -- start
void load_built_ins_into_environment(Environment*);
int list_length(Ast_Node*);
void parse_argument_list(Ast_Node*, Function*);
Ast_Node* eval_expr(Ast_Node*, Environment*);
// forward decls -- end
#define inject_scl(_ret) \
ret->sourceCodeLocation = new(Source_Code_Location); \
ret->sourceCodeLocation->file = parser_file; \
ret->sourceCodeLocation->line = parser_line; \
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
char* parser_file;
int parser_line;
int parser_col;
// TODO(Felix): use the array list macro here?
Ast_Node_Array_List* create_Ast_Node_Array_List(int initial_length) {
Ast_Node_Array_List* ret = new (Ast_Node_Array_List);
// NOTE(Felix): In this environment, the build in vunctions will
// be loaded, and the macros will be stroed in form of
// special-lambdas, that get executed in this environment at
// read-time
Environment* environment_for_macros;
// create one with 16 entries first
ret->length = initial_length;
ret->data = (struct Ast_Node**)malloc(initial_length * sizeof(struct Ast_Node));
ret->next_index = 0;
void init(Environment* env) {
// if we already initialized it, then skip
if (environment_for_macros)
return;
return ret;
}
void append_to_Ast_Node_Array_List(Ast_Node_Array_List* list, struct Ast_Node* node) {
if (list->next_index == list->length) {
list->length *= 2;
list->data = (struct Ast_Node**)realloc(list->data, list->length * sizeof(struct Ast_Node));
// NOTE(Felix): For now we just allow executing built-ins at
// read-time (while creating macros). If later we want to
// change that, we have to define some funcions in this
// environment.
environment_for_macros = env;
}
list->data[list->next_index++] = node;
}
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] != ';')
return;
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] != ';')
return;
// eat the comment line
do {
++(*index_in_text);
++parser_col;
} while (text[(*index_in_text)] != '\n' &&
text[(*index_in_text)] != '\r' &&
text[(*index_in_text)] != '\0');
}
// eat the comment line
do {
++(*index_in_text);
++parser_col;
} while (text[(*index_in_text)] != '\n' &&
text[(*index_in_text)] != '\r' &&
text[(*index_in_text)] != '\0');
}
void eat_whitespace(char* text, int* index_in_text) {
// skip whitespaces
while (text[(*index_in_text)] == ' ' ||
text[(*index_in_text)] == '\t' ||
text[(*index_in_text)] == '\n' ||
text[(*index_in_text)] == '\r')
{
if (text[(*index_in_text)] == '\n') {
++parser_line;
parser_col = 0;
void eat_whitespace(char* text, int* index_in_text) {
// skip whitespaces
while (text[(*index_in_text)] == ' ' ||
text[(*index_in_text)] == '\t' ||
text[(*index_in_text)] == '\n' ||
text[(*index_in_text)] == '\r')
{
if (text[(*index_in_text)] == '\n') {
++parser_line;
parser_col = 0;
}
++parser_col;
++(*index_in_text);
}
++parser_col;
++(*index_in_text);
}
}
void eat_until_code(char* text, int* index_in_text) {
int position_before;
do {
position_before = *index_in_text;
eat_comment_line(text, index_in_text);
eat_whitespace(text, index_in_text);
} while (position_before != *index_in_text);
}
char* 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] != ')' &&
text[*index_in_text+atom_length] != '(' &&
text[*index_in_text+atom_length] != '\0' &&
text[*index_in_text+atom_length] != '\n' &&
text[*index_in_text+atom_length] != '\r' &&
text[*index_in_text+atom_length] != '\t')
{
++atom_length;
void eat_until_code(char* text, int* index_in_text) {
int position_before;
do {
position_before = *index_in_text;
eat_comment_line(text, index_in_text);
eat_whitespace(text, index_in_text);
} while (position_before != *index_in_text);
}
// let's mark the end of the atom there quickly, so the string can
// be copied from there easily and then put the char that was
// before there back
char before = text[*index_in_text+atom_length];
text[*index_in_text+atom_length] = '\0';
char* 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] != ')' &&
text[*index_in_text+atom_length] != '(' &&
text[*index_in_text+atom_length] != '\0' &&
text[*index_in_text+atom_length] != '\n' &&
text[*index_in_text+atom_length] != '\r' &&
text[*index_in_text+atom_length] != '\t')
{
++atom_length;
}
// get the atom
char* atom = (char*)malloc(atom_length*sizeof(char)+1); // plus null char
strcpy(atom, text+(*index_in_text));
// let's mark the end of the atom there quickly, so the string can
// be copied from there easily and then put the char that was
// before there back
char before = text[*index_in_text+atom_length];
text[*index_in_text+atom_length] = '\0';
// restore the original string
text[*index_in_text+atom_length] = before;
// get the atom
char* atom = (char*)malloc(atom_length*sizeof(char)+1); // plus null char
strcpy(atom, text+(*index_in_text));
// update the index to point to the character after the atom
// ended
*index_in_text += atom_length;
// restore the original string
text[*index_in_text+atom_length] = before;
return atom;
}
// update the index to point to the character after the atom
// ended
*index_in_text += atom_length;
Ast_Node* parse_number(char* text, int* index_in_text) {
double number;
char* str_number = read_atom(text, index_in_text);
sscanf(str_number, "%lf", &number);
Ast_Node* ret = create_ast_node_number(number);
inject_scl(ret);
return ret;
}
return atom;
}
Ast_Node* parse_keyword(char* text, int* index_in_text) {
// we are now on the colon
++(*index_in_text);
++parser_col;
char* str_keyword = read_atom(text, index_in_text);
Ast_Node* ret = create_ast_node_keyword(str_keyword);
inject_scl(ret);
return ret;
}
Ast_Node* 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);
Ast_Node* ret = create_ast_node_symbol(str_symbol);
inject_scl(ret);
return ret;
}
Ast_Node* parse_string(char* text, int* index_in_text) {
// the first character is the '"'
++(*index_in_text);
++parser_col;
// 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';
Ast_Node* ret = create_ast_node_string(str, 0);
Ast_Node* parse_number(char* text, int* index_in_text) {
double number;
char* str_number = read_atom(text, index_in_text);
sscanf(str_number, "%lf", &number);
Ast_Node* ret = create_ast_node_number(number);
inject_scl(ret);
return ret;
}
// okay so the first letter was not actually closing the string...
int string_length = 0;
while (text[*index_in_text+string_length] != '"' ||
text[*index_in_text+string_length] == '\\')
{
++string_length;
Ast_Node* parse_keyword(char* text, int* index_in_text) {
// we are now on the colon
++(*index_in_text);
++parser_col;
char* str_keyword = read_atom(text, index_in_text);
Ast_Node* ret = create_ast_node_keyword(str_keyword);
inject_scl(ret);
return ret;
}
// we found the end of the string
text[*index_in_text+string_length] = '\0';
Ast_Node* 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);
Ast_Node* ret = create_ast_node_symbol(str_symbol);
inject_scl(ret);
return ret;
}
char* string = (char*)malloc(string_length*sizeof(char)+1); // plus null char
Ast_Node* parse_string(char* text, int* index_in_text) {
// the first character is the '"'
++(*index_in_text);
++parser_col;
if (!unescape_string(text+(*index_in_text))) {
create_error(
Error_Type::Unknown_Error,
create_source_code_location(parser_file, parser_line, parser_col));
// 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';
Ast_Node* ret = create_ast_node_string(str, 0);
inject_scl(ret);
return ret;
}
// okay so the first letter was not actually closing the string...
int string_length = 0;
while (text[*index_in_text+string_length] != '"' ||
text[*index_in_text+string_length] == '\\')
{
++string_length;
}
// 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
if (!unescape_string(text+(*index_in_text))) {
create_error(
Error_Type::Unknown_Error,
create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
strcpy(string, 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
Ast_Node* ret = create_ast_node_string(string, string_length);
inject_scl(ret);
return ret;
}
Ast_Node* parse_atom(char* text, int* index_in_text) {
// numbers
if ((text[*index_in_text] <= 57 && // if number
text[*index_in_text] >= 48)
||
((text[*index_in_text] == '+' || // or if sign and then number
text[*index_in_text] == '-')
&&
(text[*index_in_text +1] <= 57 &&
text[*index_in_text +1] >= 48))
||
((text[*index_in_text] == '.') // or if . and then number
&&
(text[*index_in_text +1] <= 57 &&
text[*index_in_text +1] >= 48)))
return parse_number(text, index_in_text);
// keywords
if (text[*index_in_text] == ':')
return parse_keyword(text, index_in_text);
// strings
if (text[*index_in_text] == '"')
return parse_string(text, index_in_text);
return parse_symbol(text, index_in_text);
}
Ast_Node* parse_expression(char* text, int* index_in_text) {
// if it is quoted
if (text[*index_in_text] == '\'') {
++(*index_in_text);
++parser_col;
Ast_Node* result;
if (text[*index_in_text] == '(' || text[*index_in_text] == '\'' ) {
try {
result = parse_expression(text, index_in_text);
}
} else {
try {
result = parse_atom(text, index_in_text);
}
}
return create_ast_node_pair(
create_ast_node_symbol("quote"),
create_ast_node_pair(result, create_ast_node_nil()));
}
// if it is not quoted
++(*index_in_text);
++parser_col;
eat_whitespace(text, index_in_text);
// if there was actually nothing in the list, we define here,
// that that means nil
if (text[(*index_in_text)] == ')') {
++(*index_in_text);
++parser_col;
return create_ast_node_nil();
}
// okay there is something
Ast_Node* head = new(Ast_Node);
head->type = Ast_Node_Type::Pair;
head->value.pair = new(Pair);
Ast_Node* expression = head;
while (true) {
if (text[(*index_in_text)] == '(' || text[(*index_in_text)] == '\'' ) {
try {
head->value.pair->first = parse_expression(text, index_in_text);
}
} else {
try {
head->value.pair->first = parse_atom(text, index_in_text);
}
}
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] == '\0') {
create_error(Error_Type::Unexpected_Eof, create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
if (text[(*index_in_text)] == ')') {
head->value.pair->rest = create_ast_node_nil();
++parser_col;
++(*index_in_text);
break;
} else if (text[(*index_in_text)] == '.') {
++parser_col;
++(*index_in_text);
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] == '(')
head->value.pair->rest = parse_expression(text, index_in_text);
else
head->value.pair->rest = parse_atom(text, index_in_text);
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] != ')')
create_error(Error_Type::Syntax_Error, create_source_code_location(parser_file, parser_line, parser_col));
++parser_col;
++(*index_in_text);
break;
} else {
head->value.pair->rest = create_ast_node_pair(nullptr, nullptr);
head = head->value.pair->rest;
}
}
// check if we have to create or delete or run macros
if (expression->value.pair->first->type == Ast_Node_Type::Symbol) {
if (string_equal("define-syntax", expression->value.pair->first->value.symbol->identifier)) {
// create a new macro
Ast_Node* arguments = expression->value.pair->rest;
int arguments_length;
// HACK(Felix): almost code duplicate from
// `built_ins.cpp`: special-lambda
try {
arguments_length = list_length(arguments);
}
// (define-syntax defun (name args :rest body) (...))
if (arguments_length < 2) {
create_error(Error_Type::Wrong_Number_Of_Arguments, expression->sourceCodeLocation);
return nullptr;
}
if (arguments->value.pair->first->type != Ast_Node_Type::Symbol) {
create_error(Error_Type::Type_Missmatch, expression->sourceCodeLocation);
return nullptr;
}
// extract the name
Ast_Node* symbol_for_macro = arguments->value.pair->first;
arguments = arguments->value.pair->rest;
Function* function = new(Function);
function->parent_environment = environment_for_macros;
function->is_special_form = true;
// if parameters were specified
if (arguments->value.pair->first->type != Ast_Node_Type::Nil) {
try {
assert_type(arguments->value.pair->first, Ast_Node_Type::Pair);
}
try {
parse_argument_list(arguments->value.pair->first, function);
}
} else {
function->positional_arguments = create_positional_argument_list(1);
function->keyword_arguments = create_keyword_argument_list(1);
function->rest_argument = nullptr;
}
arguments = arguments->value.pair->rest;
// if there is a docstring, use it
if (arguments->value.pair->first->type == Ast_Node_Type::String) {
function->docstring = arguments->value.pair->first->value.string->value;
arguments = arguments->value.pair->rest;
} else {
function->docstring = nullptr;
}
// we are now in the function body, just wrap it in an
// implicit prog
function->body = create_ast_node_pair(
create_ast_node_symbol("prog"),
arguments);
Ast_Node* macro = new(Ast_Node);
macro->type = Ast_Node_Type::Function;
macro->value.function = function;
define_symbol(symbol_for_macro, macro, environment_for_macros);
// print_environment(environment_for_macros);
return create_ast_node_nil();
} else if (string_equal("delete-syntax", expression->value.pair->first->value.symbol->identifier)) {
/* --- deleting an existing macro --- */
// TODO(Felix): this is a hard one because when
// environments will be made from hashmaps, how can we
// delete stuff from hashmaps? If we do probing on
// collision and then delte the first colliding entry,
// how can we find the second one? How many probes do
// we have to do to know for sure that an elemenet is
// not in the hashmap? It would be much easier if we
// never deleted any elements from the hashmap, so
// that, when an entry is not found immidiately, we
// know for sure that it does not exist in the table.
create_error(Error_Type::Not_Yet_Implemented, expression->sourceCodeLocation);
return nullptr;
} else {
// if threre is a macro named like this, then macroexpand
// if not it is regular code, dont touch.
for (int i = 0; i < environment_for_macros->next_index; ++i) {
if (string_equal(expression->value.pair->first->value.symbol->identifier, environment_for_macros->keys[i]) &&
environment_for_macros->values[i]->type == Ast_Node_Type::Function)
{
// every `Function` that is defined in this
// environment _has_ to be a macro because
// only built_ins are in there otherwise,
// which are `CFunction`s.
try {
expression = eval_expr(expression, environment_for_macros);
}
}
}
}
}
return expression;
}
Ast_Node* parse_single_expression(char* text) {
parser_file = "stdin";
parser_line = 1;
parser_col = 1;
int index_in_text = 0;
Ast_Node* result;
eat_until_code(text, &index_in_text);
if (text[(index_in_text)] == '\0')
return create_ast_node_nil();
if (text[(index_in_text)] == '(' || text[(index_in_text)] == '\'' )
try {
result = parse_expression(text, &index_in_text);
}
else
try {
result = parse_atom(text, &index_in_text);
}
eat_until_code(text, &index_in_text);
if (text[(index_in_text)] == '\0')
return result;
create_error(Error_Type::Trailing_Garbage, create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
strcpy(string, 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] = '"';
Ast_Node_Array_List* parse_program(char* file_name, char* text) {
parser_file = (char*)malloc(strlen(file_name) * sizeof(char) + 1);
strcpy(parser_file, file_name);
parser_line = 1;
parser_col = 0;
*index_in_text += string_length +1; // plus one because we want to
// go after the quotes
Ast_Node_Array_List* program = create_Ast_Node_array_list(16);
Ast_Node* ret = create_ast_node_string(string, string_length);
inject_scl(ret);
return ret;
}
int index_in_text = 0;
Ast_Node* parse_atom(char* text, int* index_in_text) {
// numbers
if ((text[*index_in_text] <= 57 && // if number
text[*index_in_text] >= 48)
||
((text[*index_in_text] == '+' || // or if sign and then number
text[*index_in_text] == '-')
&&
(text[*index_in_text +1] <= 57 &&
text[*index_in_text +1] >= 48))
||
((text[*index_in_text] == '.') // or if . and then number
&&
(text[*index_in_text +1] <= 57 &&
text[*index_in_text +1] >= 48)))
return parse_number(text, index_in_text);
// keywords
if (text[*index_in_text] == ':')
return parse_keyword(text, index_in_text);
// strings
if (text[*index_in_text] == '"')
return parse_string(text, index_in_text);
return parse_symbol(text, index_in_text);
}
Ast_Node* parse_expression(char* text, int* index_in_text) {
if (text[*index_in_text] == '\'') {
++(*index_in_text);
++parser_col;
Ast_Node* result;
if (text[*index_in_text] == '(' || text[*index_in_text] == '\'' ) {
try {
result = parse_expression(text, index_in_text);
}
} else {
try {
result = parse_atom(text, index_in_text);
}
}
return create_ast_node_pair(
create_ast_node_symbol("quote"),
create_ast_node_pair(result, create_ast_node_nil()));
}
++(*index_in_text);
++parser_col;
eat_whitespace(text, index_in_text);
// if there was actually nothing in the list, return nil
if (text[(*index_in_text)] == ')') {
++(*index_in_text);
++parser_col;
return create_ast_node_nil();
}
// okay there is something
Ast_Node* head = new(Ast_Node);
head->type = Ast_Node_Type::Pair;
head->value.pair = new(Pair);
Ast_Node* expression = head;
while (true) {
if (text[(*index_in_text)] == '(' || text[(*index_in_text)] == '\'' ) {
try {
head->value.pair->first = parse_expression(text, index_in_text);
}
} else {
try {
head->value.pair->first = parse_atom(text, index_in_text);
}
}
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] == '\0') {
create_error(Error_Type::Unexpected_Eof, create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
if (text[(*index_in_text)] == ')') {
head->value.pair->rest = create_ast_node_nil();
++parser_col;
++(*index_in_text);
break;
} else if (text[(*index_in_text)] == '.') {
++parser_col;
++(*index_in_text);
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] == '(')
head->value.pair->rest = parse_expression(text, index_in_text);
else
head->value.pair->rest = parse_atom(text, index_in_text);
eat_until_code(text, index_in_text);
if (text[(*index_in_text)] != ')')
while (text[index_in_text] != '\0') {
switch (text[index_in_text]) {
case '(': {
Ast_Node* parsed;
try {
parsed = parse_expression(text, &index_in_text);
}
append_to_Ast_Node_array_list(program, parsed);
} break;
case ';':
case ' ':
case '\t':
case '\n':
case '\r': {
eat_until_code(text, &index_in_text);
} break;
default:
/* syntax error */
create_error(Error_Type::Syntax_Error, create_source_code_location(parser_file, parser_line, parser_col));
++parser_col;
++(*index_in_text);
break;
} else {
head->value.pair->rest = create_ast_node_pair(nullptr, nullptr);
head = head->value.pair->rest;
}
}
return expression;
}
Ast_Node* parse_single_expression(char* text) {
parser_file = "stdin";
parser_line = 1;
parser_col = 1;
int index_in_text = 0;
Ast_Node* result;
eat_until_code(text, &index_in_text);
if (text[(index_in_text)] == '\0')
return create_ast_node_nil();
if (text[(index_in_text)] == '(' || text[(index_in_text)] == '\'' )
try {
result = parse_expression(text, &index_in_text);
}
else
try {
result = parse_atom(text, &index_in_text);
}
eat_until_code(text, &index_in_text);
if (text[(index_in_text)] == '\0')
return result;
create_error(Error_Type::Trailing_Garbage, create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
Ast_Node_Array_List* parse_program(char* file_name, char* text) {
parser_file = (char*)malloc(strlen(file_name) * sizeof(char) + 1);
strcpy(parser_file, file_name);
parser_line = 1;
parser_col = 0;
Ast_Node_Array_List* program = create_Ast_Node_Array_List(16);
int index_in_text = 0;
while (text[index_in_text] != '\0') {
switch (text[index_in_text]) {
case '(': {
Ast_Node* parsed;
try {
parsed = parse_expression(text, &index_in_text);
return nullptr;
}
append_to_Ast_Node_Array_List(program, parsed);
} break;
case ';':
case ' ':
case '\t':
case '\n':
case '\r': {
eat_until_code(text, &index_in_text);
} break;
default:
/* syntax error */
create_error(Error_Type::Syntax_Error, create_source_code_location(parser_file, parser_line, parser_col));
return nullptr;
}
return program;
}
return program;
}
#undef inject_scl
}