Minimal programming language prototype, created with goal to have very small compiler, so that anyone can write his own compiler for it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.9KB

  1. #include <xolatile/xtandard.c>
  2. enum {
  3. coin_none,
  4. coin_type, coin_loop, coin_if, coin_else, coin_case, coin_return, coin_import, coin_system,
  5. coin_function, coin_variable, coin_constant, coin_data_type, coin_string, coin_number, coin_marker, coin_label,
  6. coin_add, coin_subtract, coin_multiply, coin_divide, coin_modulus, coin_equal, coin_above, coin_below,
  7. coin_open, coin_close, coin_next, coin_stop, coin_branch, coin_and, coin_or, coin_not,
  8. coin_range, coin_enrange, coin_derange, coin_machine
  9. };
  10. static int coin_code = 0;/*
  11. static int type_code = 0;
  12. static int function_code = 0;
  13. static int variable_code = 0;
  14. static int constant_code = 0;*/
  15. static int coin_enum [240] = { 0 };
  16. static int coin_data [240] = { 0 };
  17. static char coin_text [240] [40] = { "" };
  18. /*
  19. static char type_name [27] [40] = { "" };
  20. static int type_size [27] = { 0 };
  21. static char function_name [9] [40] = { "" };
  22. static int function_type [9] = { 0 };
  23. static char function_argument_name [9] [6] [40] = { { "" } };
  24. static int function_argument_type [9] [6] = { { 0 } };
  25. static char variable_name [27] [40] = { "" };
  26. static int variable_type [27] = { 0 };
  27. static int variable_data [27] = { 0 };
  28. static char constant_name [30] [40] = { "" };
  29. static int constant_type [30] = { 0 };
  30. static int constant_data [30] = { 0 };
  31. */
  32. #include <stdio.h>
  33. #define STRING_LIMIT (40)
  34. static char * word_list [] = {
  35. "type", "loop", "if", "else", "case", "return", "import", "system"
  36. };
  37. static void add_coin (int type, int data, char * text) {
  38. coin_enum [coin_code] = type;
  39. coin_data [coin_code] = data;
  40. string_copy (coin_text [coin_code], text);
  41. ++coin_code;
  42. }
  43. /*
  44. static void add_type (char * name, int size) {
  45. string_copy (type_name [type_code], name);
  46. type_size [type_code] = size;
  47. ++type_code;
  48. }
  49. static void add_function (char * name, int type, char * * agrument_name, int * argument_type) {
  50. string_copy (type_name [type_code], name);
  51. type_size [type_code] = size;
  52. ++type_code;
  53. }
  54. */
  55. int main (void) {
  56. char * buffer = null;
  57. int offset = 0;
  58. int length = 0;
  59. buffer = file_import ("./test_x.x");
  60. for (offset = 0; buffer [offset] != '\0'; ++offset) {
  61. int size = 0;
  62. char data [STRING_LIMIT] = "";
  63. if (buffer [offset] == '"') {
  64. for (++offset; (buffer [offset] != '"') && (buffer [offset] != '\0'); ++offset) {
  65. data [size++] = buffer [offset];
  66. }
  67. data [size] = '\0';
  68. add_coin (coin_string, 0, data);
  69. } else if (character_is_digit (buffer [offset]) == true) {
  70. data [0] = buffer [offset];
  71. for (++offset; (character_is_digit (buffer [offset]) == true) && (buffer [offset] != '\0'); ++offset) {
  72. data [size++] = buffer [offset];
  73. }
  74. add_coin (coin_number, 0, data);
  75. data [size] = '\0';
  76. --offset;
  77. } else if (character_is_alpha (buffer [offset]) == true) {
  78. for (; ((character_is_alpha (buffer [offset]) == true) ||
  79. (character_is_digit (buffer [offset]) == true) ||
  80. (character_is_underscore (buffer [offset]) == true)) && (buffer [offset] != '\0'); ++offset) {
  81. data [size++] = buffer [offset];
  82. }
  83. data [size] = '\0';
  84. for (length = 0; length < (int) (sizeof (word_list) / sizeof (word_list [0])); ++length) {
  85. if (string_compare_limit (data, word_list [length], string_length (word_list [length]) + 1) == true) {
  86. add_coin (length, 0, data);
  87. goto here;
  88. }
  89. }
  90. add_coin (coin_marker, 0, data);
  91. /*if ((coin_enum [coin_count - 2] == coin_word) && (coin_data [coin_count - 2] == word_type)) {
  92. coin_enum [coin_count - 1] = coin_type;
  93. }
  94. for (length = 0; length < type_code; ++length) {
  95. if (string_compare_limit (data, type_name [length], string_length (type_name [length]) + 1) == true) {
  96. coin_enum [coin_count - 1] = coin_type;
  97. }
  98. }*/
  99. here:
  100. --offset;
  101. } else if (character_compare_array (buffer [offset], ",.;:=<>#&|!+*-/%()[]") == true) {
  102. char s [2] = "";
  103. s [0] = buffer [offset];
  104. add_coin (coin_none, 0, s);
  105. } else {
  106. if (character_is_blank (buffer [offset]) == false) {
  107. echo ("\033[1;31mCharacter set exception point: Segmentation\033[0m\n");
  108. printf ("%c -- %i\n", buffer [offset], (int) buffer [offset]);
  109. exit (log_failure);
  110. }
  111. }
  112. }
  113. for (length = 0; length < coin_code; ++length) {
  114. switch (coin_enum [length]) {
  115. case coin_string: printf ("\033[1;36m%s\033[0m ", coin_text [length]); break;
  116. case coin_number: printf ("\033[1;34m%s\033[0m ", coin_text [length]); break;
  117. /*case coin_type: printf ("\033[1;36m%s\033[0m ", coin_text [length]); break;*/
  118. case coin_marker: printf ("\033[1;37m%s\033[0m ", coin_text [length]); break;
  119. case coin_type:
  120. case coin_if:
  121. case coin_else:
  122. case coin_case:
  123. case coin_loop:
  124. case coin_import:
  125. case coin_system:
  126. case coin_return:
  127. printf ("\033[1;33m%s\033[0m ", coin_text [length]); break;
  128. default: printf ("\033[1;31m%s\033[0m ", coin_text [length]); break;
  129. }
  130. }
  131. printf ("\n");
  132. /*
  133. for (length = 0; length < coin_count; ++length) {
  134. if ((coin_enum [length] == coin_word) && (coin_data [length] == word_return)) {
  135. ++length;
  136. if ((coin_enum [length] == coin_symbol) && (symbol_data [coin_data [length]] == '(')) {
  137. echo ("return (\n");
  138. ++length;
  139. } else if ((coin_enum [length] == coin_symbol) && (symbol_data [coin_data [length]] == ';')) {
  140. echo ("; return;\nxor rax, rax\nret\n");
  141. ++length;
  142. } else if (coin_enum [length] == coin_number && (coin_enum [length + 1] == coin_symbol) && (symbol_data [coin_data [length + 1]] == ';')) {
  143. printf ("; return {number};\nmov rax, %i\nret\n", number_data [coin_data [length]]);
  144. ++length;
  145. } else {
  146. kill ("return ?\n");
  147. }
  148. } else if ((coin_enum [length] == coin_word) && (coin_data [length] == word_if)) {
  149. ++length;
  150. if ((coin_enum [length] == coin_symbol) && (symbol_data [coin_data [length]] == '(')) {
  151. echo ("if (\n");
  152. ++length;
  153. } else {
  154. kill ("if ?\n");
  155. }
  156. } else if ((coin_enum [length] == coin_word) && (coin_data [length] == word_else)) {
  157. ++length;
  158. if ((coin_enum [length] == coin_symbol) && (symbol_data [coin_data [length]] == ':')) {
  159. echo ("else :\n");
  160. ++length;
  161. } else if ((coin_enum [length] == coin_word) && (coin_data [length] == word_if)) {
  162. echo ("else if\n");
  163. ++length;
  164. } else {
  165. echo ("else expression\n");
  166. }
  167. } else if ((coin_enum [length] == coin_word) && (coin_data [length] == word_type)) {
  168. ++length;
  169. if (coin_enum [length] == coin_type) {
  170. echo ("type <name> -- ");
  171. echo (marker_name [coin_data [length]]);
  172. echo ("\n");
  173. ++length;
  174. } else {
  175. kill ("type ?\n");
  176. }
  177. }
  178. }
  179. */
  180. buffer = deallocate (buffer);
  181. return (log_success);
  182. }