renaming "ast_node" -> "lisp_object"
This commit is contained in:
+52
-52
@@ -1,8 +1,8 @@
|
||||
// forward decls -- start
|
||||
void load_built_ins_into_environment(Environment*);
|
||||
Ast_Node* eval_expr(Ast_Node*, Environment*);
|
||||
void parse_argument_list(Ast_Node*, Function*);
|
||||
int list_length(Ast_Node*);
|
||||
Lisp_Object* eval_expr(Lisp_Object*, Environment*);
|
||||
void parse_argument_list(Lisp_Object*, Function*);
|
||||
int list_length(Lisp_Object*);
|
||||
// forward decls -- end
|
||||
|
||||
|
||||
@@ -109,34 +109,34 @@ namespace Parser {
|
||||
return atom;
|
||||
}
|
||||
|
||||
Ast_Node* parse_number(char* text, int* index_in_text) {
|
||||
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);
|
||||
Ast_Node* ret = create_ast_node_number(number);
|
||||
Lisp_Object* ret = create_lisp_object_number(number);
|
||||
inject_scl(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ast_Node* parse_keyword(char* text, int* index_in_text) {
|
||||
Lisp_Object* 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);
|
||||
Lisp_Object* ret = create_lisp_object_keyword(str_keyword);
|
||||
inject_scl(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ast_Node* parse_symbol(char* text, int* index_in_text) {
|
||||
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);
|
||||
Ast_Node* ret = create_ast_node_symbol(str_symbol);
|
||||
Lisp_Object* ret = create_lisp_object_symbol(str_symbol);
|
||||
inject_scl(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ast_Node* parse_string(char* text, int* index_in_text) {
|
||||
Lisp_Object* parse_string(char* text, int* index_in_text) {
|
||||
// the first character is the '"'
|
||||
++(*index_in_text);
|
||||
++parser_col;
|
||||
@@ -146,7 +146,7 @@ namespace Parser {
|
||||
if (text[*index_in_text] == '"') {
|
||||
char* str = new(char);
|
||||
*str = '\0';
|
||||
Ast_Node* ret = create_ast_node_string(str, 0);
|
||||
Lisp_Object* ret = create_lisp_object_string(str, 0);
|
||||
inject_scl(ret);
|
||||
|
||||
// plus one because we want to go after the quotes
|
||||
@@ -187,12 +187,12 @@ namespace Parser {
|
||||
*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);
|
||||
Lisp_Object* ret = create_lisp_object_string(string, string_length);
|
||||
inject_scl(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ast_Node* parse_atom(char* text, int* index_in_text) {
|
||||
Lisp_Object* parse_atom(char* text, int* index_in_text) {
|
||||
// numbers
|
||||
if ((text[*index_in_text] <= 57 && // if number
|
||||
text[*index_in_text] >= 48)
|
||||
@@ -220,7 +220,7 @@ namespace Parser {
|
||||
return parse_symbol(text, index_in_text);
|
||||
}
|
||||
|
||||
Ast_Node* parse_expression(char* text, int* index_in_text) {
|
||||
Lisp_Object* parse_expression(char* text, int* index_in_text) {
|
||||
|
||||
// if it is quoted
|
||||
if (text[*index_in_text] == '\'' ||
|
||||
@@ -230,7 +230,7 @@ namespace Parser {
|
||||
char quoteType = text[*index_in_text];
|
||||
++(*index_in_text);
|
||||
++parser_col;
|
||||
Ast_Node* result;
|
||||
Lisp_Object* result;
|
||||
if (text[*index_in_text] == '(' ||
|
||||
text[*index_in_text] == '\'' ||
|
||||
text[*index_in_text] == '`' ||
|
||||
@@ -246,17 +246,17 @@ namespace Parser {
|
||||
}
|
||||
|
||||
if (quoteType == '\'')
|
||||
return create_ast_node_pair(
|
||||
create_ast_node_symbol("quote"),
|
||||
create_ast_node_pair(result, create_ast_node_nil()));
|
||||
return create_lisp_object_pair(
|
||||
create_lisp_object_symbol("quote"),
|
||||
create_lisp_object_pair(result, create_lisp_object_nil()));
|
||||
else if (quoteType == '`')
|
||||
return create_ast_node_pair(
|
||||
create_ast_node_symbol("quasiquote"),
|
||||
create_ast_node_pair(result, create_ast_node_nil()));
|
||||
return create_lisp_object_pair(
|
||||
create_lisp_object_symbol("quasiquote"),
|
||||
create_lisp_object_pair(result, create_lisp_object_nil()));
|
||||
// it has to be an unquote
|
||||
return create_ast_node_pair(
|
||||
create_ast_node_symbol("unquote"),
|
||||
create_ast_node_pair(result, create_ast_node_nil()));
|
||||
return create_lisp_object_pair(
|
||||
create_lisp_object_symbol("unquote"),
|
||||
create_lisp_object_pair(result, create_lisp_object_nil()));
|
||||
}
|
||||
|
||||
|
||||
@@ -271,14 +271,14 @@ namespace Parser {
|
||||
if (text[(*index_in_text)] == ')') {
|
||||
++(*index_in_text);
|
||||
++parser_col;
|
||||
return create_ast_node_nil();
|
||||
return create_lisp_object_nil();
|
||||
}
|
||||
|
||||
// okay there is something
|
||||
Ast_Node* head = new(Ast_Node);
|
||||
head->type = Ast_Node_Type::Pair;
|
||||
Lisp_Object* head = new(Lisp_Object);
|
||||
head->type = Lisp_Object_Type::Pair;
|
||||
head->value.pair = new(Pair);
|
||||
Ast_Node* expression = head;
|
||||
Lisp_Object* expression = head;
|
||||
|
||||
while (true) {
|
||||
if (text[*index_in_text] == '(' ||
|
||||
@@ -303,7 +303,7 @@ namespace Parser {
|
||||
|
||||
|
||||
if (text[(*index_in_text)] == ')') {
|
||||
head->value.pair->rest = create_ast_node_nil();
|
||||
head->value.pair->rest = create_lisp_object_nil();
|
||||
++parser_col;
|
||||
++(*index_in_text);
|
||||
break;
|
||||
@@ -325,16 +325,16 @@ namespace Parser {
|
||||
++(*index_in_text);
|
||||
break;
|
||||
} else {
|
||||
head->value.pair->rest = create_ast_node_pair(nullptr, nullptr);
|
||||
head->value.pair->rest = create_lisp_object_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 (expression->value.pair->first->type == Lisp_Object_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;
|
||||
Lisp_Object* arguments = expression->value.pair->rest;
|
||||
int arguments_length;
|
||||
|
||||
// HACK(Felix): almost code duplicate from
|
||||
@@ -349,13 +349,13 @@ namespace Parser {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (arguments->value.pair->first->type != Ast_Node_Type::Symbol) {
|
||||
if (arguments->value.pair->first->type != Lisp_Object_Type::Symbol) {
|
||||
create_error(Error_Type::Type_Missmatch, expression->sourceCodeLocation);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// extract the name
|
||||
Ast_Node* symbol_for_macro = arguments->value.pair->first;
|
||||
Lisp_Object* symbol_for_macro = arguments->value.pair->first;
|
||||
arguments = arguments->value.pair->rest;
|
||||
|
||||
Function* function = new(Function);
|
||||
@@ -363,9 +363,9 @@ namespace Parser {
|
||||
function->type = Function_Type::Macro;
|
||||
|
||||
// if parameters were specified
|
||||
if (arguments->value.pair->first->type != Ast_Node_Type::Nil) {
|
||||
if (arguments->value.pair->first->type != Lisp_Object_Type::Nil) {
|
||||
try {
|
||||
assert_type(arguments->value.pair->first, Ast_Node_Type::Pair);
|
||||
assert_type(arguments->value.pair->first, Lisp_Object_Type::Pair);
|
||||
}
|
||||
try {
|
||||
parse_argument_list(arguments->value.pair->first, function);
|
||||
@@ -378,7 +378,7 @@ namespace Parser {
|
||||
|
||||
arguments = arguments->value.pair->rest;
|
||||
// if there is a docstring, use it
|
||||
if (arguments->value.pair->first->type == Ast_Node_Type::String) {
|
||||
if (arguments->value.pair->first->type == Lisp_Object_Type::String) {
|
||||
function->docstring = arguments->value.pair->first->value.string->value;
|
||||
arguments = arguments->value.pair->rest;
|
||||
} else {
|
||||
@@ -387,17 +387,17 @@ namespace Parser {
|
||||
|
||||
// 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"),
|
||||
function->body = create_lisp_object_pair(
|
||||
create_lisp_object_symbol("prog"),
|
||||
arguments);
|
||||
|
||||
Ast_Node* macro = new(Ast_Node);
|
||||
macro->type = Ast_Node_Type::Function;
|
||||
Lisp_Object* macro = new(Lisp_Object);
|
||||
macro->type = Lisp_Object_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();
|
||||
return create_lisp_object_nil();
|
||||
|
||||
} else if (string_equal("delete-syntax", expression->value.pair->first->value.symbol->identifier)) {
|
||||
/* --- deleting an existing macro --- */
|
||||
@@ -420,7 +420,7 @@ namespace Parser {
|
||||
|
||||
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 &&
|
||||
environment_for_macros->values[i]->type == Lisp_Object_Type::Function &&
|
||||
environment_for_macros->values[i]->value.function->type == Function_Type::Macro)
|
||||
{
|
||||
try {
|
||||
@@ -444,16 +444,16 @@ namespace Parser {
|
||||
return expression;
|
||||
}
|
||||
|
||||
Ast_Node* parse_single_expression(char* text) {
|
||||
Lisp_Object* parse_single_expression(char* text) {
|
||||
parser_file = "stdin";
|
||||
parser_line = 1;
|
||||
parser_col = 1;
|
||||
|
||||
int index_in_text = 0;
|
||||
Ast_Node* result;
|
||||
Lisp_Object* result;
|
||||
eat_until_code(text, &index_in_text);
|
||||
if (text[(index_in_text)] == '\0')
|
||||
return create_ast_node_nil();
|
||||
return create_lisp_object_nil();
|
||||
if (text[index_in_text] == '(' ||
|
||||
text[index_in_text] == '\'' ||
|
||||
text[index_in_text] == '`' ||
|
||||
@@ -474,7 +474,7 @@ namespace Parser {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void write_expanded_file(char* file_name, Ast_Node_Array_List* program) {
|
||||
void write_expanded_file(char* file_name, Lisp_Object_Array_List* program) {
|
||||
char* ext = ".expanded";
|
||||
char* newName = (char*)calloc(4 + strlen(file_name), sizeof(char));
|
||||
strcpy(newName, file_name);
|
||||
@@ -488,7 +488,7 @@ namespace Parser {
|
||||
|
||||
for (int i = 0; i < program->next_index; ++i) {
|
||||
// a macro will parse as nil for now, so we skip those
|
||||
if (program->data[i]->type == Ast_Node_Type::Nil)
|
||||
if (program->data[i]->type == Lisp_Object_Type::Nil)
|
||||
continue;
|
||||
fprint(f, program->data[i]);
|
||||
fprintf(f, "\n\n");
|
||||
@@ -497,24 +497,24 @@ namespace Parser {
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
Ast_Node_Array_List* parse_program(char* file_name, char* text) {
|
||||
Lisp_Object_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);
|
||||
Lisp_Object_Array_List* program = create_Lisp_Object_array_list(16);
|
||||
|
||||
int index_in_text = 0;
|
||||
|
||||
while (text[index_in_text] != '\0') {
|
||||
switch (text[index_in_text]) {
|
||||
case '(': {
|
||||
Ast_Node* parsed;
|
||||
Lisp_Object* parsed;
|
||||
try {
|
||||
parsed = parse_expression(text, &index_in_text);
|
||||
}
|
||||
append_to_Ast_Node_array_list(program, parsed);
|
||||
append_to_Lisp_Object_array_list(program, parsed);
|
||||
} break;
|
||||
case ';':
|
||||
case ' ':
|
||||
|
||||
Reference in New Issue
Block a user