1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: http://codemirror.net/LICENSE
4 // Author: Aliaksei Chapyzhenka
7 if (typeof exports == "object" && typeof module == "object") // CommonJS
8 mod(require("../../lib/codemirror"));
9 else if (typeof define == "function" && define.amd) // AMD
10 define(["../../lib/codemirror"], mod);
11 else // Plain browser env
13 })(function(CodeMirror) {
16 function toWordList(words) {
18 words.split(' ').forEach(function(e){
24 var coreWordList = toWordList(
27 0= = 0< < > U< MIN MAX\
28 2DROP 2DUP 2OVER 2SWAP ?DUP DEPTH DROP DUP OVER ROT SWAP\
32 FM/MOD SM/REM UM/MOD */ */MOD / /MOD MOD\
33 HERE , @ ! CELL+ CELLS C, C@ C! CHARS 2@ 2!\
34 ALIGN ALIGNED +! ALLOT\
36 FIND EXECUTE IMMEDIATE COUNT LITERAL STATE\
40 <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL\
42 . CR EMIT SPACE SPACES TYPE U. .R U.R\
52 SAVE-INPUT RESTORE-INPUT\
55 D- D+ D0< D0= D2* D2/ D< D= DMAX DMIN D>S DABS\
56 M+ M*/ D. D.R 2ROT DU<\
60 GET-CURRENT SET-CURRENT FORTH-WORDLIST GET-ORDER SET-ORDER\
61 PREVIOUS SEARCH-WORDLIST WORDLIST FIND ALSO ONLY FORTH DEFINITIONS ORDER\
62 -TRAILING /STRING SEARCH COMPARE CMOVE CMOVE> BLANK SLITERAL');
64 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');
66 CodeMirror.defineMode('forth', function() {
67 function searchWordList (wordList, word) {
69 for (i = wordList.length - 1; i >= 0; i--) {
70 if (wordList[i].name === word.toUpperCase()) {
77 startState: function() {
81 coreWordList: coreWordList,
82 immediateWordList: immediateWordList,
86 token: function (stream, stt) {
88 if (stream.eatSpace()) {
91 if (stt.state === '') { // interpretation
92 if (stream.match(/^(\]|:NONAME)(\s|$)/i)) {
93 stt.state = ' compilation';
94 return 'builtin compilation';
96 mat = stream.match(/^(\:)\s+(\S+)(\s|$)+/);
98 stt.wordList.push({name: mat[2].toUpperCase()});
99 stt.state = ' compilation';
100 return 'def' + stt.state;
102 mat = stream.match(/^(VARIABLE|2VARIABLE|CONSTANT|2CONSTANT|CREATE|POSTPONE|VALUE|WORD)\s+(\S+)(\s|$)+/i);
104 stt.wordList.push({name: mat[2].toUpperCase()});
105 return 'def' + stt.state;
107 mat = stream.match(/^(\'|\[\'\])\s+(\S+)(\s|$)+/);
109 return 'builtin' + stt.state;
111 } else { // compilation
113 if (stream.match(/^(\;|\[)(\s)/)) {
116 return 'builtin compilation';
118 if (stream.match(/^(\;|\[)($)/)) {
120 return 'builtin compilation';
122 if (stream.match(/^(POSTPONE)\s+\S+(\s|$)+/)) {
128 mat = stream.match(/^(\S+)(\s+|$)/);
130 if (searchWordList(stt.wordList, mat[1]) !== undefined) {
131 return 'variable' + stt.state;
135 if (mat[1] === '\\') {
137 return 'comment' + stt.state;
141 if (searchWordList(stt.coreWordList, mat[1]) !== undefined) {
142 return 'builtin' + stt.state;
144 if (searchWordList(stt.immediateWordList, mat[1]) !== undefined) {
145 return 'keyword' + stt.state;
148 if (mat[1] === '(') {
149 stream.eatWhile(function (s) { return s !== ')'; });
151 return 'comment' + stt.state;
155 if (mat[1] === '.(') {
156 stream.eatWhile(function (s) { return s !== ')'; });
158 return 'string' + stt.state;
160 if (mat[1] === 'S"' || mat[1] === '."' || mat[1] === 'C"') {
161 stream.eatWhile(function (s) { return s !== '"'; });
163 return 'string' + stt.state;
167 if (mat[1] - 0xfffffffff) {
168 return 'number' + stt.state;
170 // if (mat[1].match(/^[-+]?[0-9]+\.[0-9]*/)) {
171 // return 'number' + stt.state;
174 return 'atom' + stt.state;
179 CodeMirror.defineMIME("text/x-forth", "forth");