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.

180 lines
5.1 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. // Author: Aliaksei Chapyzhenka
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. mod(require("../../lib/codemirror"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror"], mod);
  9. else // Plain browser env
  10. mod(CodeMirror);
  11. })(function(CodeMirror) {
  12. "use strict";
  13. function toWordList(words) {
  14. var ret = [];
  15. words.split(' ').forEach(function(e){
  16. ret.push({name: e});
  17. });
  18. return ret;
  19. }
  20. var coreWordList = toWordList(
  21. 'INVERT AND OR XOR\
  22. 2* 2/ LSHIFT RSHIFT\
  23. 0= = 0< < > U< MIN MAX\
  24. 2DROP 2DUP 2OVER 2SWAP ?DUP DEPTH DROP DUP OVER ROT SWAP\
  25. >R R> R@\
  26. + - 1+ 1- ABS NEGATE\
  27. S>D * M* UM*\
  28. FM/MOD SM/REM UM/MOD */ */MOD / /MOD MOD\
  29. HERE , @ ! CELL+ CELLS C, C@ C! CHARS 2@ 2!\
  30. ALIGN ALIGNED +! ALLOT\
  31. CHAR [CHAR] [ ] BL\
  32. FIND EXECUTE IMMEDIATE COUNT LITERAL STATE\
  33. ; DOES> >BODY\
  34. EVALUATE\
  35. SOURCE >IN\
  36. <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL\
  37. FILL MOVE\
  38. . CR EMIT SPACE SPACES TYPE U. .R U.R\
  39. ACCEPT\
  40. TRUE FALSE\
  41. <> U> 0<> 0>\
  42. NIP TUCK ROLL PICK\
  43. 2>R 2R@ 2R>\
  44. WITHIN UNUSED MARKER\
  45. I J\
  46. TO\
  47. COMPILE, [COMPILE]\
  48. SAVE-INPUT RESTORE-INPUT\
  49. PAD ERASE\
  50. 2LITERAL DNEGATE\
  51. D- D+ D0< D0= D2* D2/ D< D= DMAX DMIN D>S DABS\
  52. M+ M*/ D. D.R 2ROT DU<\
  53. CATCH THROW\
  54. FREE RESIZE ALLOCATE\
  55. CS-PICK CS-ROLL\
  56. GET-CURRENT SET-CURRENT FORTH-WORDLIST GET-ORDER SET-ORDER\
  57. PREVIOUS SEARCH-WORDLIST WORDLIST FIND ALSO ONLY FORTH DEFINITIONS ORDER\
  58. -TRAILING /STRING SEARCH COMPARE CMOVE CMOVE> BLANK SLITERAL');
  59. var immediateWordList = toWordList('IF ELSE THEN BEGIN WHILE REPEAT UNTIL RECURSE [IF] [ELSE] [THEN] ?DO DO LOOP +LOOP UNLOOP LEAVE EXIT AGAIN CASE OF ENDOF ENDCASE');
  60. CodeMirror.defineMode('forth', function() {
  61. function searchWordList (wordList, word) {
  62. var i;
  63. for (i = wordList.length - 1; i >= 0; i--) {
  64. if (wordList[i].name === word.toUpperCase()) {
  65. return wordList[i];
  66. }
  67. }
  68. return undefined;
  69. }
  70. return {
  71. startState: function() {
  72. return {
  73. state: '',
  74. base: 10,
  75. coreWordList: coreWordList,
  76. immediateWordList: immediateWordList,
  77. wordList: []
  78. };
  79. },
  80. token: function (stream, stt) {
  81. var mat;
  82. if (stream.eatSpace()) {
  83. return null;
  84. }
  85. if (stt.state === '') { // interpretation
  86. if (stream.match(/^(\]|:NONAME)(\s|$)/i)) {
  87. stt.state = ' compilation';
  88. return 'builtin compilation';
  89. }
  90. mat = stream.match(/^(\:)\s+(\S+)(\s|$)+/);
  91. if (mat) {
  92. stt.wordList.push({name: mat[2].toUpperCase()});
  93. stt.state = ' compilation';
  94. return 'def' + stt.state;
  95. }
  96. mat = stream.match(/^(VARIABLE|2VARIABLE|CONSTANT|2CONSTANT|CREATE|POSTPONE|VALUE|WORD)\s+(\S+)(\s|$)+/i);
  97. if (mat) {
  98. stt.wordList.push({name: mat[2].toUpperCase()});
  99. return 'def' + stt.state;
  100. }
  101. mat = stream.match(/^(\'|\[\'\])\s+(\S+)(\s|$)+/);
  102. if (mat) {
  103. return 'builtin' + stt.state;
  104. }
  105. } else { // compilation
  106. // ; [
  107. if (stream.match(/^(\;|\[)(\s)/)) {
  108. stt.state = '';
  109. stream.backUp(1);
  110. return 'builtin compilation';
  111. }
  112. if (stream.match(/^(\;|\[)($)/)) {
  113. stt.state = '';
  114. return 'builtin compilation';
  115. }
  116. if (stream.match(/^(POSTPONE)\s+\S+(\s|$)+/)) {
  117. return 'builtin';
  118. }
  119. }
  120. // dynamic wordlist
  121. mat = stream.match(/^(\S+)(\s+|$)/);
  122. if (mat) {
  123. if (searchWordList(stt.wordList, mat[1]) !== undefined) {
  124. return 'variable' + stt.state;
  125. }
  126. // comments
  127. if (mat[1] === '\\') {
  128. stream.skipToEnd();
  129. return 'comment' + stt.state;
  130. }
  131. // core words
  132. if (searchWordList(stt.coreWordList, mat[1]) !== undefined) {
  133. return 'builtin' + stt.state;
  134. }
  135. if (searchWordList(stt.immediateWordList, mat[1]) !== undefined) {
  136. return 'keyword' + stt.state;
  137. }
  138. if (mat[1] === '(') {
  139. stream.eatWhile(function (s) { return s !== ')'; });
  140. stream.eat(')');
  141. return 'comment' + stt.state;
  142. }
  143. // // strings
  144. if (mat[1] === '.(') {
  145. stream.eatWhile(function (s) { return s !== ')'; });
  146. stream.eat(')');
  147. return 'string' + stt.state;
  148. }
  149. if (mat[1] === 'S"' || mat[1] === '."' || mat[1] === 'C"') {
  150. stream.eatWhile(function (s) { return s !== '"'; });
  151. stream.eat('"');
  152. return 'string' + stt.state;
  153. }
  154. // numbers
  155. if (mat[1] - 0xfffffffff) {
  156. return 'number' + stt.state;
  157. }
  158. // if (mat[1].match(/^[-+]?[0-9]+\.[0-9]*/)) {
  159. // return 'number' + stt.state;
  160. // }
  161. return 'atom' + stt.state;
  162. }
  163. }
  164. };
  165. });
  166. CodeMirror.defineMIME("text/x-forth", "forth");
  167. });