erplb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // builtins table
  2. static builtin_entry builtin_commands[] = {
  3. { {3,'d','u','p'}, &_dup },
  4. { {4,'d','r','o','p'}, &_drop },
  5. { {5,'c','l','e','a','r'}, &_clear },
  6. { {4,'s','w','a','p'}, &_swap },
  7. { {4,'o','v','e','r'}, &_over },
  8. { {5,'d','e','p','t','h'}, &_depth },
  9. { {6,'l','a','m','b','d','a'}, &_lambda },
  10. { {5,'t','a','b','l','e'}, &_table },
  11. { {5,'s','t','a','c','k'}, &_stack },
  12. { {5,'p','r','i','n','t'}, &_print },
  13. { {4,'q','u','i','t'}, &_quit },
  14. { {0}, NULL }
  15. };
  16. // builtin functions:
  17. BUILTIN(_dup) {
  18. if (!stack)
  19. return _warning("DUP" INSUFFICIENT_ARGS_STK);
  20. Atom *a = duplicate_atom(stack->head);
  21. return a;
  22. }
  23. BUILTIN(_drop) {
  24. if (!stack)
  25. return _warning("DROP" INSUFFICIENT_ARGS_STK);
  26. stack_pop(1, &stack);
  27. return NULL;
  28. }
  29. BUILTIN(_clear) {
  30. if (!stack)
  31. return _warning("CLEAR" EMPTY_STACK);
  32. stack_pop(0, &stack);
  33. return NULL;
  34. }
  35. BUILTIN(_swap) {
  36. if (!stack || !stack->tail)
  37. return _warning("SWAP" INSUFFICIENT_ARGS_STK);
  38. Atom *a = stack->head;
  39. stack->head = stack->tail->head;
  40. stack->tail->head = a;
  41. return NULL;
  42. }
  43. BUILTIN(_over) {
  44. if (!stack || !stack->tail)
  45. return _warning("OVER" INSUFFICIENT_ARGS_STK);
  46. return duplicate_atom(stack->tail->head);
  47. }
  48. BUILTIN(_depth) {
  49. Atom *a = create_int(0);
  50. a->num = list_size(stack);
  51. return a;
  52. }
  53. BUILTIN(_print) {
  54. if (!li) {
  55. if (!stack)
  56. return _warning("PRINT" INSUFFICIENT_ARGS_STK);
  57. print_expression(stack->head);
  58. putchar(10);
  59. return NULL;
  60. }
  61. while (li) {
  62. print_expression(li->head);
  63. if (li->tail)
  64. putchar(' ');
  65. li = li->tail;
  66. }
  67. putchar(10);
  68. return NULL;
  69. }
  70. BUILTIN(_stack) {
  71. if (!stack) return _warning("STACK: <Empty>");
  72. printf("STACK: ");
  73. _print(stack, ctx);
  74. return NULL;
  75. }
  76. BUILTIN(_lambda) {
  77. if (li) {
  78. if (!valid_lambda(li))
  79. return _warning("LAMBDA" INVALID_ARGS);
  80. return create_lambda(duplicate_list(li));
  81. }
  82. if (!stack || !stack->head) return _warning("LAMBDA" INSUFFICIENT_ARGS_STK);
  83. if (!is_list(stack->head)) return _warning("LAMBDA" INVALID_ARGS_STK);
  84. if (!valid_lambda(stack->head->list)) return _warning("LAMBDA" INVALID_ARGS_STK);
  85. return create_lambda(duplicate_list(stack->head->list));
  86. }
  87. BUILTIN(_table) {
  88. #warning TODO
  89. return NULL;
  90. }
  91. BUILTIN(_quit) {
  92. #warning TODO
  93. puts("Quitting...");
  94. interrupted = 1;
  95. return NULL;
  96. }