builtin.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* spearl/builtin.c
  2. *
  3. * This file is part of spearl.
  4. *
  5. * Spearl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Spearl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with spearl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <spearl/spearl.h>
  19. #include <spearl/error.h>
  20. #include <spearl/sys.h>
  21. static struct spearl_list *do_builtin_macro(struct spearl_mcall *mcall) {
  22. (void) mcall;
  23. printf("hello, world");
  24. return root_object.list;
  25. }
  26. int spl_builtin_init(void) {
  27. spearl_define_builtin("macro", do_builtin_macro);
  28. return 0;
  29. }
  30. int expand_builtin_macro(struct spearl_macro *macro, struct spearl_mcall *mcall,
  31. struct spl_klist_node *node) {
  32. struct spearl_list *list;
  33. if (!(list = macro->builtin.do_builtin(mcall)))
  34. return -1;
  35. spl_klist_add(node->next, &container_of(list, struct spearl_object, list)->node);
  36. return 0;
  37. }
  38. struct spearl_macro __visible *spearl_define_builtin(char *name,
  39. spearl_builtin_fn_t fn) {
  40. struct spearl_macro *macro;
  41. macro = spl_macro_alloc();
  42. macro->type = BUILTIN;
  43. macro->name = name;
  44. macro->builtin.name = name;
  45. macro->builtin.do_builtin = fn;
  46. spl_klist_init(&macro->node);
  47. spl_klist_add(&spl_macros, &macro->node);
  48. return macro;
  49. }