Permalink
Browse files

added help builtin

  • Loading branch information...
1 parent 0885443 commit 4132fbb7aa44bc647387e9f10b36b744e0412c4e @aimxhaisse committed Jul 16, 2011
Showing with 36 additions and 6 deletions.
  1. +32 −5 builtins.c
  2. +3 −1 examples.trol
  3. +1 −0 lisp.h
View
@@ -8,13 +8,15 @@
static node *sym_builtin_add(node_list *params);
static node *sym_builtin_print(node_list *params);
static node *sym_builtin_list(node_list *params);
+static node *sym_builtin_help(node_list *params);
sym_builtins builtins[] = {
- { "+", &sym_builtin_add },
- { "add", &sym_builtin_add },
- { "puts", &sym_builtin_print },
- { "list", &sym_builtin_list },
- { NULL, NULL}
+ { "+", &sym_builtin_add, "performs an addition with all parameters" },
+ { "add", &sym_builtin_add, "performs an addition with all parameters" },
+ { "puts", &sym_builtin_print, "prints each parameter on a new line" },
+ { "list", &sym_builtin_list, "returns a list containing all parameters"},
+ { "help", &sym_builtin_help, "displays help about a builtin"},
+ { NULL, NULL, NULL}
};
static void
@@ -100,6 +102,31 @@ sym_builtin_list(node_list *params)
return result;
}
+static node
+*sym_builtin_help(node_list *params)
+{
+ node_sym *sym;
+ int i;
+ int found;
+
+ if (NULL == params->elem || params->elem->type != SYM)
+ builtin_error("bad parameter for builtin help");
+ sym = params->elem->elem;
+ for (i = 0, found = 0; NULL != builtins[i].name; ++i)
+ {
+ if (0 == strcmp(builtins[i].name, sym->name))
+ {
+ printf("%s\n", builtins[i].help);
+ found++;
+ break;
+ }
+ }
+ if (!found)
+ builtin_error("no help found for this builtin");
+
+ return node_new_nil();
+}
+
node *
sym_builtin_exec(node_sym *sym, node_list *params)
{
View
@@ -1,3 +1,5 @@
(puts (+ 3 4 45 (+ 3 4 5 6)))
(puts (puts nil))
-(puts (list 3 4 (list) () nil))
+(puts (list 3 4 (list) () nil))
+(puts (help puts))
+(help +)
View
1 lisp.h
@@ -69,6 +69,7 @@ typedef struct sym_builtins_s
{
char *name;
node *(*builtin)(node_list *params);
+ char *help;
} sym_builtins;
#endif /* LISP_H */

0 comments on commit 4132fbb

Please sign in to comment.