return NEXT
end)
-TODFA = defWord(">DFA", [TOCFA, INCR, EXIT])
+TOPFA = defWord(">PFA", [TOCFA, INCR, EXIT])
# Branching
# Start reading in word
count = 0
- while (mem[TOIN]<mem[NUMTIB] && mem[TIB+mem[TOIN]] != delim)
+ while (mem[TOIN]<mem[NUMTIB])
mem[addr] = mem[TIB+mem[TOIN]]
+ mem[TOIN] += 1
+
+ if (mem[addr] == delim)
+ break
+ end
+
count += 1
addr += 1
- mem[TOIN] += 1
end
# Record count
return NEXT
end)
+PARSE = defPrimWord("PARSE", () -> begin
+ delim = popPS()
+
+ # Chew up initial occurrences of delim
+ addr = mem[HERE]
+
+ # Start reading input stream
+ count = 0
+ while (mem[TOIN]<mem[NUMTIB])
+ mem[addr] = mem[TIB+mem[TOIN]]
+ mem[TOIN] += 1
+
+ if (mem[addr] == delim)
+ break
+ end
+
+ count += 1
+ addr += 1
+ end
+
+ pushPS(addr)
+ pushPS(count)
+
+ return NEXT
+end)
+
STATE, STATE_CFA = defNewVar("STATE", 0)
INTERPRET = defWord("INTERPRET",
DUP, FETCH, ZE, ZBRANCH, 3,
DROP, EXIT, # Exit if TIB is exhausted
- STATE_CFA, FETCH, ZBRANCH, 27,
+ STATE_CFA, FETCH, ZBRANCH, 28,
# Compiling
DUP, FIND, ZBRANCH, 17,
COMMA, BRANCH, -33,
# No word found, parse number
- NUMBER, BTICK, LIT, COMMA, BRANCH, -39,
+ BTICK, LIT, COMMA, NUMBER, COMMA, BRANCH, -40,
# Interpreting
DUP, FIND, QDUP, ZBRANCH, 7,
# Found word. Execute!
- SWAP, DROP, TOCFA, EXECUTE, BRANCH, -50,
+ SWAP, DROP, TOCFA, EXECUTE, BRANCH, -51,
# No word found, parse number and leave on stack
- NUMBER, BRANCH, -53,
+ NUMBER, BRANCH, -54,
EXIT]
)
end)
INCLUDE = defPrimWord("INCLUDE", () -> begin
+ pushPS(32)
callPrim(mem[WORD])
- wordLen = popPS()
- wordAddr = popPS()
+ wordAddr = popPS()+1
+ wordLen = mem[wordAddr-1]
word = getString(wordAddr, wordLen)
push!(sources, open(word, "r"))
# Compilation
HEADER = defPrimWord("HEADER", () -> begin
-
wordAddr = popPS()+1
wordLen = mem[wordAddr-1]
word = getString(wordAddr, wordLen)
end)
HIDE = defWord("HIDE",
- [WORD,
+ [LIT, 32, WORD,
FIND,
HIDDEN,
EXIT])
COLON = defWord(":",
- [WORD,
+ [LIT, 32, WORD,
HEADER,
LIT, DOCOL, COMMA,
LATEST_CFA, FETCH, HIDDEN,
end, flags=F_IMMED)
TICK = defWord("'",
- [WORD, FIND, TOCFA, EXIT])
+ [LIT, 32, WORD, FIND, TOCFA, EXIT])
#### VM loop ####
: LITERAL IMMEDIATE ['] LIT , , ;
: CHAR
- WORD
+ BL WORD
DROP @
;
: SPACE BL emit ;
: [COMPILE] IMMEDIATE
- WORD \ get the next word
+ BL WORD \ get the next word
FIND \ find it in the dictionary
>CFA \ get its codeword
, \ and compile that
: ( IMMEDIATE
1 \ allowed nested parens by keeping track of depth
BEGIN
- KEY \ read next character
+ >IN #TIB >= IF \ End of TIB?
+ QUERY \ Get next line
+ THEN
+
+ >IN @ 1 >IN +!
DUP [CHAR] ( = IF \ open paren?
DROP \ drop the open paren
1+ \ depth increases
( CONSTANTS AND VARIABLES ------------------------------------------------------ )
: CONSTANT
- WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
+ BL WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
DOCOL , ( append DOCOL (the codeword field of this word) )
['] LIT , ( append the codeword LIT )
, ( append the value on the top of the stack )
: VALUE ( n -- )
- WORD HEADER ( make the dictionary entry (the name follows VALUE) )
+ BL WORD HEADER ( make the dictionary entry (the name follows VALUE) )
DOCOL , ( append DOCOL )
['] LIT , ( append the codeword LIT )
, ( append the initial value )
;
: TO IMMEDIATE ( n -- )
- WORD ( get the name of the value )
+ BL WORD ( get the name of the value )
FIND ( look it up in the dictionary )
- >DFA ( get a pointer to the first data field (the 'LIT') )
+ >PFA ( get a pointer to the first data field (the 'LIT') )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
( x +TO VAL adds x to VAL )
: +TO IMMEDIATE
- WORD ( get the name of the value )
+ BL WORD ( get the name of the value )
FIND ( look it up in the dictionary )
- >DFA ( get a pointer to the first data field (the 'LIT') )
+ >PFA ( get a pointer to the first data field (the 'LIT') )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
( FORGET ---------------------------------------------------------------------- )
: FORGET
- WORD FIND ( find the word, gets the dictionary entry address )
+ BL WORD FIND ( find the word, gets the dictionary entry address )
DUP @ LATEST ! ( set LATEST to point to the previous word )
HERE ! ( and store HERE with the dictionary address )
;
;
: SEE
- WORD 2DUP FIND ( find the dictionary entry to decompile )
+ BL WORD 2DUP FIND ( find the dictionary entry to decompile )
?DUP 0= IF
." Word '" TYPE ." ' not found in dictionary."
4 SPACES
- >DFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
+ >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
( now we start decompiling until we hit the end of the word )
BEGIN ( end start )