More to come...
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

236 行
15KB

  1. #include "../chapter/chapter_0.c"
  2. #include "../chapter/chapter_1.c"
  3. #include "../chapter/chapter_2.c"
  4. #include "../chapter/chapter_3.c"
  5. #include "../chapter/chapter_4.c"
  6. static void highlight_common (void) {
  7. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\"@#$` \t\r\n";
  8. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  9. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  10. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|@#$`", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  11. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  12. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  13. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  14. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  15. }
  16. static void highlight_c (void) {
  17. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  18. char * keywords [] = {
  19. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  20. "do", "while", "for", "continue", "switch", "case", "default", "break",
  21. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  22. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  23. };
  24. int word;
  25. syntax_define (FALSE, FALSE, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  26. syntax_define (FALSE, FALSE, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  27. syntax_define (FALSE, FALSE, "#", "\n", '\\', COLOUR_YELLOW, EFFECT_ITALIC);
  28. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  29. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  30. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  31. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  32. }
  33. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  34. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  35. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  36. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  37. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  38. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  39. }
  40. static void highlight_ada (void) {
  41. char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
  42. char * keywords [] = {
  43. "abort", "else", "new", "return", "abs", "elsif", "not", "reverse",
  44. "abstract", "end", "null", "accept", "entry", "select", "access", "of",
  45. "separate", "aliased", "exit", "or", "some", "all", "others", "subtype",
  46. "and", "for", "out", "array", "function", "at", "tagged", "generic",
  47. "package", "task", "begin", "goto", "pragma", "body", "private", "then",
  48. "type", "case", "in", "constant", "until", "is", "raise", "use",
  49. "if", "declare", "range", "delay", "limited", "record", "when", "delta",
  50. "loop", "rem", "while", "digits", "renames", "with", "do", "mod",
  51. "requeue", "xor", "procedure", "protected", "interface", "synchronized", "exception", "overriding",
  52. "terminate"
  53. };
  54. int word;
  55. syntax_define (FALSE, FALSE, "--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  56. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  57. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  58. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  59. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  60. }
  61. syntax_define (TRUE, FALSE, "()", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  62. syntax_define (TRUE, FALSE, ".,:;<=>+-*/&|'", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  63. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  64. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  65. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  66. }
  67. static void highlight_cpp (void) {
  68. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  69. char * keywords [] = {
  70. "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
  71. "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char",
  72. "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval",
  73. "constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
  74. "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
  75. "export", "extern", "false", "float", "for", "friend", "goto", "if",
  76. "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not",
  77. "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
  78. "reflexpr", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof",
  79. "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this",
  80. "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union",
  81. "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor",
  82. "xor_eq", "final", "override", "import", "module", "transaction_safe"
  83. };
  84. int word;
  85. syntax_define (FALSE, FALSE, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  86. syntax_define (FALSE, FALSE, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  87. syntax_define (FALSE, FALSE, "#", "\n", '\\', COLOUR_YELLOW, EFFECT_ITALIC);
  88. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  89. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  90. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  91. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  92. }
  93. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  94. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  95. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  96. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  97. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  98. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  99. }
  100. static void highlight_flat_assembly (void) {
  101. char * separators = ".,+-=:;(){}[]%$<> \t\r\n";
  102. char * instructions [] = {
  103. "mov", "movabs", "movapd", "movaps", "movebe", "movsd", "movsx", "movzx",
  104. "movsxd", "movd", "movq", "movs", "movsb", "movsw", "movsd", "movsq",
  105. "cmovmp", "cmovrcxz", "cmovc", "cmovnc", "cmove", "cmovne", "cmovz", "cmovnz",
  106. "cmovg", "cmovng", "cmovge", "cmovnge", "cmovl", "cmovnl", "cmovle", "cmovnle",
  107. "cmova", "cmovna", "cmovae", "cmovnae", "cmovb", "cmovnb", "cmovbe", "cmovnbe",
  108. "cmovs", "cmovns", "cmovo", "cmovno", "cmovp", "cmovnp", "cmovpo", "cmovpe",
  109. "cmp", "cmps", "cmpsb", "cmpsw", "cmpsd", "cmpsq", "cmpxchg", "lea",
  110. "monitor", "cpuid", "in", "out", "syscall", "sysenter", "sysret", "sysexit",
  111. "swap", "bswap", "pop", "push", "call", "ret", "enter", "leave",
  112. "and", "or", "not", "neg", "sal", "sar", "shl", "shr",
  113. "inc", "dec", "add", "sub", "mul", "div", "imul", "idiv",
  114. "nop", "fnop", "adc", "sbb", "aaa", "aas", "aam", "aad",
  115. "jmp", "jrcxz", "jc", "jnc", "je", "jne", "jz", "jnz",
  116. "jg", "jng", "jge", "jnge", "jl", "jnl", "jle", "jnle",
  117. "ja", "jna", "jae", "jnae", "jb", "jnb", "jbe", "jnbe",
  118. "js", "jns", "jo", "jno", "jp", "jnp", "jpo", "jpe",
  119. "rep", "repe", "repz", "repne", "repnz", "loop", "loope", "loopne"
  120. };
  121. char * registers [] = {
  122. "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  123. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  124. "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  125. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
  126. "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  127. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
  128. "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
  129. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
  130. "ah", "ch", "dh", "bh"
  131. };
  132. char * keywords [] = {
  133. "format", "executable", "readable", "writable", "segment", "sector", "entry", "macro",
  134. "db", "dw", "dd", "dq", "rb", "rw", "rd", "rq"
  135. };
  136. int word;
  137. syntax_define (FALSE, FALSE, ";", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  138. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  139. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  140. for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
  141. syntax_define (FALSE, TRUE, instructions [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  142. }
  143. for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
  144. syntax_define (FALSE, TRUE, registers [word], separators, '\0', COLOUR_CYAN, EFFECT_BOLD);
  145. }
  146. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  147. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_ITALIC);
  148. }
  149. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  150. syntax_define (TRUE, FALSE, ".,+-=:;%$<>", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  151. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  152. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  153. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  154. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  155. }
  156. int main (int argc, char * * argv) {
  157. int offset = 0;
  158. int select = 0;
  159. int length = 0;
  160. char * buffer = NULL;
  161. if (argc < 2) {
  162. print ("Usage: /1.//program_4 [text_file]/-\n");
  163. return (EXIT_FAILURE);
  164. }
  165. select = file_type (argv [1]);
  166. buffer = file_record (argv [1]);
  167. if ((select == FILE_TYPE_C_SOURCE) || (select == FILE_TYPE_C_HEADER)) {
  168. highlight_c ();
  169. } else if ((select == FILE_TYPE_ADA_BODY) || (select == FILE_TYPE_ADA_SPECIFICATION)) {
  170. highlight_ada ();
  171. } else if ((select == FILE_TYPE_CPP_SOURCE) || (select == FILE_TYPE_CPP_HEADER)) {
  172. highlight_cpp ();
  173. } else if (select == FILE_TYPE_FLAT_ASSEMBLY) {
  174. highlight_flat_assembly ();
  175. } else {
  176. highlight_common ();
  177. }
  178. for (offset = 0; buffer [offset] != '\0'; offset += length) {
  179. select = syntax_select (& buffer [offset], & length);
  180. if (select >= syntax_count) {
  181. terminal_colour (COLOUR_WHITE, EFFECT_NORMAL);
  182. } else {
  183. terminal_colour (syntax_colour [select], syntax_effect [select]);
  184. }
  185. out (& buffer [offset], length);
  186. terminal_cancel ();
  187. }
  188. buffer = deallocate (buffer);
  189. return (EXIT_SUCCESS);
  190. }