builtins.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* Declarations for builtin functions.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_BUILTINS__
  14. #define __KP_BUILTINS__ 1
  15. #include "interp.hpp"
  16. #include "initop.hpp"
  17. KP_DECLS_BEGIN
  18. // Basic routine to copy any object.
  19. KP_EXPORT result<object> copy (interpreter *interp,
  20. object obj, bool deep = false);
  21. // Hash a generic object.
  22. KP_EXPORT result<uint32_t> xhash (interpreter *interp, object obj);
  23. // Hash an object as an address.
  24. KP_EXPORT uint32_t hash_addr (object obj);
  25. // Return the length of a generic object.
  26. KP_EXPORT result<object> length (interpreter *interp, object obj);
  27. // Add objects X and Y.
  28. KP_EXPORT result<object> add (interpreter *interp, object x, object y);
  29. // Subtract object Y from X.
  30. KP_EXPORT result<object> sub (interpreter *interp, object x, object y);
  31. // Multiply objects X and Y.
  32. KP_EXPORT result<object> mul (interpreter *interp, object x, object y);
  33. // Divide object X by Y.
  34. KP_EXPORT result<object> div (interpreter *interp, object x, object y);
  35. // Return the modulo of X by Y.
  36. KP_EXPORT result<object> modulo (interpreter *interp, object x, object y);
  37. // Test for object equality.
  38. KP_EXPORT result<bool> equal (interpreter *interp, object x, object y);
  39. // Compare 2 objects.
  40. KP_EXPORT result<int> xcmp (interpreter *interp, object x, object y);
  41. #define DEFBUILTIN(name) \
  42. KP_EXPORT result<object> name (interpreter *interp, object *argv, int argc)
  43. // Make a generic exception for bootstrapping.
  44. DEFBUILTIN (p_mkexc);
  45. // Make a list out of the arguments in the stack.
  46. DEFBUILTIN (list_fct);
  47. /* Make a list out of the arguments in the stack, chaining it
  48. * to the last one, which must be a list as well. */
  49. DEFBUILTIN (list_star);
  50. // Make a tuple out of the arguments in the stack.
  51. DEFBUILTIN (tuple_fct);
  52. // Make a table out of the arguments in the stack.
  53. DEFBUILTIN (table_fct);
  54. // Perform addition for every object.
  55. DEFBUILTIN (add_fct);
  56. // Perform subtraction for every object.
  57. DEFBUILTIN (sub_fct);
  58. // Perform multiplication for every object.
  59. DEFBUILTIN (mul_fct);
  60. // Perform division for every object.
  61. DEFBUILTIN (div_fct);
  62. // Call function with list of arguments.
  63. DEFBUILTIN (apply_fct);
  64. // Create an array out of the arguments on the stack.
  65. DEFBUILTIN (array_fct);
  66. // Create a table out of the arguments on the stack.
  67. DEFBUILTIN (table_fct);
  68. // Create a tree out of the arguments on the stack.
  69. DEFBUILTIN (tree_fct);
  70. // Add a global definition for a symbol.
  71. DEFBUILTIN (p_putd);
  72. // Reverse an object.
  73. DEFBUILTIN (reverse_fct);
  74. // Reverse an object inplace, destructively.
  75. DEFBUILTIN (nreverse_fct);
  76. // Modify an object at an index, inplace.
  77. DEFBUILTIN (nput_fct);
  78. // Construct a subsequence.
  79. DEFBUILTIN (subseq_fct);
  80. // Mutate a collection value for a key.
  81. DEFBUILTIN (nzap_fct);
  82. // Exit khipu.
  83. DEFBUILTIN (exit_fct);
  84. #undef DEFBUILTIN
  85. typedef result<object> (*indexer_t) (interpreter *, object, object, object);
  86. KP_EXPORT indexer_t index_seq (object seq);
  87. struct builtin_iter
  88. {
  89. const char *curp;
  90. builtin_iter ();
  91. const char *name () const
  92. {
  93. return (this->curp);
  94. }
  95. void adv ();
  96. bool valid () const;
  97. };
  98. // Utility for the sorting functions.
  99. struct comparator
  100. {
  101. interpreter *interp;
  102. comparator (interpreter *ip) : interp (ip)
  103. {
  104. }
  105. virtual result<bool> operator() (object x, object y)
  106. {
  107. int c = KP_TRY (xcmp (this->interp, x, y));
  108. return (c < 0);
  109. }
  110. };
  111. // Init OP for builtins.
  112. KP_EXPORT init_op init_builtins;
  113. KP_DECLS_END
  114. #endif