Added (info) built-in
This commit is contained in:
@@ -124,38 +124,38 @@ typedef struct {
|
||||
|
||||
typedef enum {
|
||||
Built_In_Addition,
|
||||
Built_In_Subtraction,
|
||||
Built_In_Multiplication,
|
||||
Built_In_Division,
|
||||
Built_In_Greater,
|
||||
Built_In_Less,
|
||||
Built_In_Equal,
|
||||
Built_In_Greater_Equal,
|
||||
Built_In_Less_Equal,
|
||||
Built_In_If,
|
||||
Built_In_And,
|
||||
Built_In_Or,
|
||||
Built_In_Not,
|
||||
Built_In_Pair,
|
||||
Built_In_First,
|
||||
Built_In_Rest,
|
||||
Built_In_Load,
|
||||
Built_In_Define,
|
||||
Built_In_Let,
|
||||
Built_In_Macro_Define,
|
||||
Built_In_Breakpoint,
|
||||
Built_In_Mutate,
|
||||
Built_In_Lambda,
|
||||
Built_In_Macro,
|
||||
Built_In_Define,
|
||||
Built_In_Division,
|
||||
Built_In_Equal,
|
||||
Built_In_Eval,
|
||||
Built_In_Quote,
|
||||
Built_In_Prog,
|
||||
Built_In_List,
|
||||
Built_In_Print,
|
||||
Built_In_Read,
|
||||
Built_In_Info,
|
||||
Built_In_Type,
|
||||
Built_In_Exit,
|
||||
Built_In_First,
|
||||
Built_In_Greater,
|
||||
Built_In_Greater_Equal,
|
||||
Built_In_If,
|
||||
Built_In_Info,
|
||||
Built_In_Lambda,
|
||||
Built_In_Less,
|
||||
Built_In_Less_Equal,
|
||||
Built_In_Let,
|
||||
Built_In_List,
|
||||
Built_In_Load,
|
||||
Built_In_Macro,
|
||||
Built_In_Macro_Define,
|
||||
Built_In_Multiplication,
|
||||
Built_In_Mutate,
|
||||
Built_In_Not,
|
||||
Built_In_Or,
|
||||
Built_In_Pair,
|
||||
Built_In_Print,
|
||||
Built_In_Prog,
|
||||
Built_In_Quote,
|
||||
Built_In_Read,
|
||||
Built_In_Rest,
|
||||
Built_In_Subtraction,
|
||||
Built_In_Type,
|
||||
} Built_In_Name;
|
||||
|
||||
/**
|
||||
@@ -165,9 +165,8 @@ char* Built_In_Name_to_string(Built_In_Name name) {
|
||||
switch (name) {
|
||||
case Built_In_Addition: return "+";
|
||||
case Built_In_And: return "and";
|
||||
case Built_In_Define: return "define";
|
||||
case Built_In_Macro_Define: return "macro-define";
|
||||
case Built_In_Breakpoint: return "breakpoint";
|
||||
case Built_In_Define: return "define";
|
||||
case Built_In_Division: return "/";
|
||||
case Built_In_Equal: return "=";
|
||||
case Built_In_Eval: return "eval";
|
||||
@@ -178,12 +177,13 @@ char* Built_In_Name_to_string(Built_In_Name name) {
|
||||
case Built_In_If: return "if";
|
||||
case Built_In_Info: return "info";
|
||||
case Built_In_Lambda: return "lambda";
|
||||
case Built_In_Let: return "let";
|
||||
case Built_In_Less: return "<";
|
||||
case Built_In_Less_Equal: return "<=";
|
||||
case Built_In_Let: return "let";
|
||||
case Built_In_List: return "list";
|
||||
case Built_In_Load: return "load";
|
||||
case Built_In_Macro: return "macro";
|
||||
case Built_In_Macro_Define: return "macro-define";
|
||||
case Built_In_Multiplication: return "*";
|
||||
case Built_In_Mutate: return "mutate";
|
||||
case Built_In_Not: return "not";
|
||||
@@ -278,38 +278,39 @@ Ast_Node* create_ast_node_keyword(char* keyword) {
|
||||
Ast_Node* create_ast_node_built_in_function(char* name) {
|
||||
Built_In_Name type;
|
||||
if (string_equal(name, "+")) type = Built_In_Addition;
|
||||
else if (string_equal(name, "-")) type = Built_In_Subtraction;
|
||||
else if (string_equal(name, "*")) type = Built_In_Multiplication;
|
||||
else if (string_equal(name, "-")) type = Built_In_Subtraction;
|
||||
else if (string_equal(name, "/")) type = Built_In_Division;
|
||||
else if (string_equal(name, "<")) type = Built_In_Less;
|
||||
else if (string_equal(name, "<=")) type = Built_In_Less_Equal;
|
||||
else if (string_equal(name, "=")) type = Built_In_Equal;
|
||||
else if (string_equal(name, ">")) type = Built_In_Greater;
|
||||
else if (string_equal(name, ">=")) type = Built_In_Greater_Equal;
|
||||
else if (string_equal(name, "<")) type = Built_In_Less;
|
||||
else if (string_equal(name, "<=")) type = Built_In_Less_Equal;
|
||||
else if (string_equal(name, "if")) type = Built_In_If;
|
||||
else if (string_equal(name, "and")) type = Built_In_And;
|
||||
else if (string_equal(name, "or")) type = Built_In_Or;
|
||||
else if (string_equal(name, "not")) type = Built_In_Not;
|
||||
else if (string_equal(name, "pair")) type = Built_In_Pair;
|
||||
else if (string_equal(name, "first")) type = Built_In_First;
|
||||
else if (string_equal(name, "rest")) type = Built_In_Rest;
|
||||
else if (string_equal(name, "load")) type = Built_In_Load;
|
||||
else if (string_equal(name, "let")) type = Built_In_Let;
|
||||
else if (string_equal(name, "define")) type = Built_In_Define;
|
||||
else if (string_equal(name, "macro-define")) type = Built_In_Macro_Define;
|
||||
else if (string_equal(name, "breakpoint")) type = Built_In_Breakpoint;
|
||||
else if (string_equal(name, "mutate")) type = Built_In_Mutate;
|
||||
else if (string_equal(name, "lambda")) type = Built_In_Lambda;
|
||||
else if (string_equal(name, "macro")) type = Built_In_Macro;
|
||||
else if (string_equal(name, "define")) type = Built_In_Define;
|
||||
else if (string_equal(name, "eval")) type = Built_In_Eval;
|
||||
else if (string_equal(name, "quote")) type = Built_In_Quote;
|
||||
else if (string_equal(name, "prog")) type = Built_In_Prog;
|
||||
else if (string_equal(name, "list")) type = Built_In_List;
|
||||
else if (string_equal(name, "print")) type = Built_In_Print;
|
||||
else if (string_equal(name, "read")) type = Built_In_Read;
|
||||
else if (string_equal(name, "info")) type = Built_In_Info;
|
||||
else if (string_equal(name, "type")) type = Built_In_Type;
|
||||
else if (string_equal(name, "exit")) type = Built_In_Exit;
|
||||
else if (string_equal(name, "first")) type = Built_In_First;
|
||||
else if (string_equal(name, "if")) type = Built_In_If;
|
||||
else if (string_equal(name, "info")) type = Built_In_Info;
|
||||
else if (string_equal(name, "info")) type = Built_In_Info;
|
||||
else if (string_equal(name, "lambda")) type = Built_In_Lambda;
|
||||
else if (string_equal(name, "let")) type = Built_In_Let;
|
||||
else if (string_equal(name, "list")) type = Built_In_List;
|
||||
else if (string_equal(name, "load")) type = Built_In_Load;
|
||||
else if (string_equal(name, "macro")) type = Built_In_Macro;
|
||||
else if (string_equal(name, "macro-define")) type = Built_In_Macro_Define;
|
||||
else if (string_equal(name, "mutate")) type = Built_In_Mutate;
|
||||
else if (string_equal(name, "not")) type = Built_In_Not;
|
||||
else if (string_equal(name, "or")) type = Built_In_Or;
|
||||
else if (string_equal(name, "pair")) type = Built_In_Pair;
|
||||
else if (string_equal(name, "print")) type = Built_In_Print;
|
||||
else if (string_equal(name, "prog")) type = Built_In_Prog;
|
||||
else if (string_equal(name, "quote")) type = Built_In_Quote;
|
||||
else if (string_equal(name, "read")) type = Built_In_Read;
|
||||
else if (string_equal(name, "rest")) type = Built_In_Rest;
|
||||
else if (string_equal(name, "type")) type = Built_In_Type;
|
||||
else return nullptr;
|
||||
|
||||
Ast_Node* node = new(Ast_Node);
|
||||
|
||||
@@ -148,7 +148,7 @@ Ast_Node* lookup_symbol(Symbol* sym, Environment* env) {
|
||||
return result;
|
||||
|
||||
create_error(Error_Type_Symbol_Not_Defined, create_ast_node_nil());
|
||||
printf("%s\n", sym->identifier);
|
||||
/* printf("%s\n", sym->identifier); */
|
||||
return nullptr;
|
||||
}
|
||||
void print_indent(int indent) {
|
||||
|
||||
+65
@@ -410,6 +410,69 @@ Ast_Node* eval_expr(Ast_Node* node, Environment* env) {
|
||||
#endif
|
||||
return create_ast_node_nil();
|
||||
}
|
||||
case Built_In_Info: {
|
||||
try {
|
||||
arguments_length = list_length(arguments);
|
||||
}
|
||||
if (arguments_length != 1) {
|
||||
report_error(Error_Type_Wrong_Number_Of_Arguments);
|
||||
}
|
||||
|
||||
print(arguments->value.pair->first);
|
||||
|
||||
Ast_Node* type = eval_expr(
|
||||
create_ast_node_pair(
|
||||
create_ast_node_symbol("type"),
|
||||
create_ast_node_pair(arguments->value.pair->first, create_ast_node_nil())),
|
||||
env);
|
||||
|
||||
if (type) {
|
||||
printf(" is of type ");
|
||||
print(type);
|
||||
printf("\n");
|
||||
// just make sure type was not redefined and
|
||||
// returns something that is not a keyword
|
||||
if (type->type == Ast_Node_Type_Keyword &&
|
||||
string_equal(type->value.keyword->identifier, "dynamic-function") ||
|
||||
string_equal(type->value.keyword->identifier, "dynamic-macro"))
|
||||
{
|
||||
Ast_Node* fun = eval_expr(arguments->value.pair->first, env);
|
||||
printf("\nMacro? %s\n", (fun->value.function->is_macro) ? "yes" : "no");
|
||||
if (fun->value.function->docstring)
|
||||
printf("Docstring:\n==========\n%s\n\n", fun->value.function->docstring);
|
||||
else
|
||||
printf("No docstring avaliable\n");
|
||||
|
||||
printf("Arguments:\n==========\n");
|
||||
printf("Postitional: {");
|
||||
if (fun->value.function->positional_arguments->next_index != 0) {
|
||||
printf("%s", fun->value.function->positional_arguments->identifiers[0]);
|
||||
for (int i = 1; i < fun->value.function->positional_arguments->next_index; ++i) {
|
||||
printf(", %s", fun->value.function->positional_arguments->identifiers[i]);
|
||||
}
|
||||
}
|
||||
printf("}\n");
|
||||
printf("Keyword: {");
|
||||
if (fun->value.function->keyword_arguments->next_index != 0) {
|
||||
printf("%s", fun->value.function->keyword_arguments->identifiers[0]);
|
||||
for (int i = 1; i < fun->value.function->keyword_arguments->next_index; ++i) {
|
||||
printf(", %s", fun->value.function->keyword_arguments->identifiers[i]);
|
||||
}
|
||||
}
|
||||
printf("}\n");
|
||||
printf("Rest: {");
|
||||
if (fun->value.function->rest_argument)
|
||||
printf("%s", fun->value.function->rest_argument);
|
||||
printf("}\n");
|
||||
|
||||
}
|
||||
} else {
|
||||
printf(" is not defined\n");
|
||||
delete_error();
|
||||
}
|
||||
|
||||
return create_ast_node_nil();
|
||||
}
|
||||
case Built_In_Macro:
|
||||
case Built_In_Lambda: {
|
||||
/*
|
||||
@@ -449,6 +512,8 @@ Ast_Node* eval_expr(Ast_Node* node, Environment* env) {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user