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