interp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* tripl/arch/interp/interp.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl 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. * tripl 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 tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <dlfcn.h>
  24. #include <errno.h>
  25. #include <klist.h>
  26. #include <tripl/tripl.h>
  27. #include <tripl/limits.h>
  28. #include <tripl/arch.h>
  29. int interp_init(void);
  30. __arch int interp_new_var(char *name, long value, int attr);
  31. __arch int interp_stack_dump(void);
  32. __arch int interp_set_var(char *name, long value, int attr);
  33. extern char *psave;
  34. struct klist_node stack;
  35. struct arch interp_arch_fn = {
  36. .arch_init = interp_init,
  37. .arch_new_var = interp_new_var,
  38. .arch_stack_dump = interp_stack_dump,
  39. .arch_import_shared = NULL,
  40. .arch_funcall = NULL,
  41. .arch_set_var = interp_set_var
  42. };
  43. __arch int interp_init(void) {
  44. klist_init(&stack);
  45. return 0;
  46. }
  47. static struct klist_node *find_name(char *name) {
  48. struct klist_node *n;
  49. for (n = stack.next; n != &stack; n = n->next)
  50. if (!strcmp(n->payload, name))
  51. return n;
  52. return NULL;
  53. }
  54. __arch int interp_new_var(char *name, long value, int attr) {
  55. push_nr(value, name, attr);
  56. return 0;
  57. }
  58. __arch int interp_set_var(char *name, long value, int attr) {
  59. struct klist_node *n;
  60. /* find the name */
  61. if (!(n = find_name(name)))
  62. exit(generic_error("name not found", psave, lno));
  63. if (n->xmask & 4)
  64. exit(generic_error("variable sets to be read-only", psave, lno));
  65. n->value = value;
  66. return 0;
  67. }
  68. __arch int interp_stack_dump(void) {
  69. struct klist_node *n;
  70. int i;
  71. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  72. printf("stack dump: \n");
  73. ansi_csi_ed(stdout);
  74. for (i = 0, n = stack.next; n != &stack; n = n->next, i++)
  75. printf("\t%3d: %s \t%ld @ %08x\n", i, (char *) n->payload, n->value, n->xmask);
  76. return 0;
  77. }