Posted to tcl by gps at Mon Apr 06 10:44:54 GMT 2009view raw

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. enum {
  6. FORTCL_CELL_TYPE_INT = 1,
  7. FORTCL_CELL_TYPE_LONG,
  8. FORTCL_CELL_TYPE_STRING,
  9. FORTCL_CELL_TYPE_BIGNUM
  10. };
  11.  
  12. struct fortcl_cell {
  13. union {
  14. int i;
  15. long l;
  16. size_t string_offset;
  17. size_t bignum_offset;
  18. } types;
  19.  
  20. char type;
  21. };
  22.  
  23. struct fortcl_interp {
  24. char **strings;
  25. size_t strings_used;
  26. };
  27.  
  28. struct fortcl_interp *
  29. fortcl_interp_create(void) {
  30. struct fortcl_interp *interp;
  31.  
  32. interp = malloc(sizeof *interp);
  33.  
  34. if(NULL == interp) {
  35. perror("malloc");
  36. return NULL;
  37. }
  38.  
  39. return interp;
  40. }
  41.  
  42. struct fortcl_cell
  43. fortcl_string_create(struct fortcl_interp *interp, const char *cp, size_t length) {
  44. struct fortcl_cell result;
  45.  
  46. /*
  47. * This is just an example. The real version would expand the strings array
  48. * if needed, and try to enlarge it by N, where N > 1 to avoid reallocs.
  49. */
  50.  
  51. interp->strings = malloc(sizeof(*(interp->strings)));
  52. /*error checking not added*/
  53.  
  54. result.types.string_offset = interp->strings_used;
  55. result.type = FORTCL_CELL_TYPE_STRING;
  56.  
  57. interp->strings[interp->strings_used] = strdup(cp);
  58. /*error checking for strdup not added.*/
  59.  
  60. interp->strings_used += 1;
  61.  
  62. return result;
  63. }
  64.  
  65. char *
  66. fortcl_string_get(struct fortcl_interp *interp, struct fortcl_cell str) {
  67.  
  68. if(FORTCL_CELL_TYPE_STRING == str.type) {
  69. return interp->strings[str.types.string_offset];
  70. } else {
  71. /* perhaps fortcl_string_create... and return the pointer. */
  72. return NULL;
  73. }
  74. }
  75.  
  76.  
  77. void
  78. fortcl_example_puts(struct fortcl_interp *interp, struct fortcl_cell str) {
  79. puts(fortcl_string_get(interp, str));
  80. }
  81.  
  82. struct fortcl_cell
  83. fortcl_integer_create(int i) {
  84. struct fortcl_cell cell;
  85.  
  86. cell.types.i = i;
  87. cell.type = FORTCL_CELL_TYPE_INT;
  88.  
  89. return cell;
  90. }
  91.  
  92. struct fortcl_cell
  93. fortcl_example_multiply(struct fortcl_interp *interp, size_t objc, struct fortcl_cell *argv) {
  94. struct fortcl_cell result;
  95.  
  96. result.types.i = argv[0].types.i * argv[1].types.i;
  97. result.type = FORTCL_CELL_TYPE_INT;
  98.  
  99. return result;
  100. }
  101.  
  102. int
  103. main(int argc, char *argv[]) {
  104. struct fortcl_interp *interp;
  105. struct fortcl_cell num[2];
  106. struct fortcl_cell str;
  107. struct fortcl_cell magic;
  108.  
  109. interp = fortcl_interp_create();
  110.  
  111. if(NULL == interp) {
  112. return EXIT_FAILURE;
  113. }
  114.  
  115. str = fortcl_string_create(interp, "Hello", 5);
  116.  
  117. fortcl_example_puts(interp, str);
  118.  
  119. num[0] = fortcl_integer_create(6);
  120. num[1] = fortcl_integer_create(7);
  121.  
  122. magic = fortcl_example_multiply(interp, 2, num);
  123.  
  124. printf("The magic number is: %d\n", magic.types.i);
  125.  
  126. return EXIT_SUCCESS;
  127. }
  128.