The Lambda Lab
/
projects
/
forth.jl.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
4aaffe5
)
Debugging interpreter.
author
Tim Vaughan
<tgvaughan@gmail.com>
Sat, 7 May 2016 05:18:52 +0000
(17:18 +1200)
committer
Tim Vaughan
<tgvaughan@gmail.com>
Sat, 7 May 2016 05:18:52 +0000
(17:18 +1200)
src/forth.jl
patch
|
blob
|
history
src/lib.4th
patch
|
blob
|
history
diff --git
a/src/forth.jl
b/src/forth.jl
index
1679115
..
90cf1cf
100644
(file)
--- a/
src/forth.jl
+++ b/
src/forth.jl
@@
-679,7
+679,7
@@
TOCFA = defPrimWord(">CFA", () -> begin
return NEXT
end)
return NEXT
end)
-TO
DFA = defWord(">D
FA", [TOCFA, INCR, EXIT])
+TO
PFA = defWord(">P
FA", [TOCFA, INCR, EXIT])
# Branching
# Branching
@@
-767,11
+767,16
@@
WORD = defPrimWord("WORD", () -> begin
# Start reading in word
count = 0
# 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[addr] = mem[TIB+mem[TOIN]]
+ mem[TOIN] += 1
+
+ if (mem[addr] == delim)
+ break
+ end
+
count += 1
addr += 1
count += 1
addr += 1
- mem[TOIN] += 1
end
# Record count
end
# Record count
@@
-781,6
+786,32
@@
WORD = defPrimWord("WORD", () -> begin
return NEXT
end)
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",
STATE, STATE_CFA = defNewVar("STATE", 0)
INTERPRET = defWord("INTERPRET",
@@
-789,7
+820,7
@@
INTERPRET = defWord("INTERPRET",
DUP, FETCH, ZE, ZBRANCH, 3,
DROP, EXIT, # Exit if TIB is exhausted
DUP, FETCH, ZE, ZBRANCH, 3,
DROP, EXIT, # Exit if TIB is exhausted
- STATE_CFA, FETCH, ZBRANCH, 2
7
,
+ STATE_CFA, FETCH, ZBRANCH, 2
8
,
# Compiling
DUP, FIND, ZBRANCH, 17,
# Compiling
DUP, FIND, ZBRANCH, 17,
@@
-802,16
+833,16
@@
INTERPRET = defWord("INTERPRET",
COMMA, BRANCH, -33,
# No word found, parse number
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!
# Interpreting
DUP, FIND, QDUP, ZBRANCH, 7,
# Found word. Execute!
- SWAP, DROP, TOCFA, EXECUTE, BRANCH, -5
0
,
+ SWAP, DROP, TOCFA, EXECUTE, BRANCH, -5
1
,
# No word found, parse number and leave on stack
# No word found, parse number and leave on stack
- NUMBER, BRANCH, -5
3
,
+ NUMBER, BRANCH, -5
4
,
EXIT]
)
EXIT]
)
@@
-835,9
+866,10
@@
BYE = defPrimWord("BYE", () -> begin
end)
INCLUDE = defPrimWord("INCLUDE", () -> begin
end)
INCLUDE = defPrimWord("INCLUDE", () -> begin
+ pushPS(32)
callPrim(mem[WORD])
callPrim(mem[WORD])
- word
Len = popPS()
- word
Addr = popPS()
+ word
Addr = popPS()+1
+ word
Len = mem[wordAddr-1]
word = getString(wordAddr, wordLen)
push!(sources, open(word, "r"))
word = getString(wordAddr, wordLen)
push!(sources, open(word, "r"))
@@
-851,7
+883,6
@@
end)
# Compilation
HEADER = defPrimWord("HEADER", () -> begin
# Compilation
HEADER = defPrimWord("HEADER", () -> begin
-
wordAddr = popPS()+1
wordLen = mem[wordAddr-1]
word = getString(wordAddr, wordLen)
wordAddr = popPS()+1
wordLen = mem[wordAddr-1]
word = getString(wordAddr, wordLen)
@@
-878,13
+909,13
@@
HIDDEN = defPrimWord("HIDDEN", () -> begin
end)
HIDE = defWord("HIDE",
end)
HIDE = defWord("HIDE",
- [WORD,
+ [
LIT, 32,
WORD,
FIND,
HIDDEN,
EXIT])
COLON = defWord(":",
FIND,
HIDDEN,
EXIT])
COLON = defWord(":",
- [WORD,
+ [
LIT, 32,
WORD,
HEADER,
LIT, DOCOL, COMMA,
LATEST_CFA, FETCH, HIDDEN,
HEADER,
LIT, DOCOL, COMMA,
LATEST_CFA, FETCH, HIDDEN,
@@
-904,7
+935,7
@@
IMMEDIATE = defPrimWord("IMMEDIATE", () -> begin
end, flags=F_IMMED)
TICK = defWord("'",
end, flags=F_IMMED)
TICK = defWord("'",
- [WORD, FIND, TOCFA, EXIT])
+ [
LIT, 32,
WORD, FIND, TOCFA, EXIT])
#### VM loop ####
#### VM loop ####
diff --git
a/src/lib.4th
b/src/lib.4th
index
c38faa1
..
c9df3d9
100644
(file)
--- a/
src/lib.4th
+++ b/
src/lib.4th
@@
-34,7
+34,7
@@
: LITERAL IMMEDIATE ['] LIT , , ;
: CHAR
: LITERAL IMMEDIATE ['] LIT , , ;
: CHAR
- WORD
+
BL
WORD
DROP @
;
DROP @
;
@@
-47,7
+47,7
@@
: SPACE BL emit ;
: [COMPILE] IMMEDIATE
: 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
FIND \ find it in the dictionary
>CFA \ get its codeword
, \ and compile that
@@
-220,7
+220,11
@@
: ( IMMEDIATE
1 \ allowed nested parens by keeping track of depth
BEGIN
: ( 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
DUP [CHAR] ( = IF \ open paren?
DROP \ drop the open paren
1+ \ depth increases
@@
-483,7
+487,7
@@
( CONSTANTS AND VARIABLES ------------------------------------------------------ )
: CONSTANT
( 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 )
DOCOL , ( append DOCOL (the codeword field of this word) )
['] LIT , ( append the codeword LIT )
, ( append the value on the top of the stack )
@@
-501,7
+505,7
@@
: VALUE ( n -- )
: 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 )
DOCOL , ( append DOCOL )
['] LIT , ( append the codeword LIT )
, ( append the initial value )
@@
-509,9
+513,9
@@
;
: TO IMMEDIATE ( n -- )
;
: 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 )
FIND ( look it up in the dictionary )
- >
D
FA ( get a pointer to the first data field (the 'LIT') )
+ >
P
FA ( get a pointer to the first data field (the 'LIT') )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
@@
-524,9
+528,9
@@
( x +TO VAL adds x to VAL )
: +TO IMMEDIATE
( 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 )
FIND ( look it up in the dictionary )
- >
D
FA ( get a pointer to the first data field (the 'LIT') )
+ >
P
FA ( get a pointer to the first data field (the 'LIT') )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
1+ ( increment to point at the value )
STATE @ IF ( compiling? )
['] LIT , ( compile LIT )
@@
-602,7
+606,7
@@
( FORGET ---------------------------------------------------------------------- )
: FORGET
( 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 )
;
DUP @ LATEST ! ( set LATEST to point to the previous word )
HERE ! ( and store HERE with the dictionary address )
;
@@
-631,7
+635,7
@@
;
: SEE
;
: 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."
?DUP 0= IF
." Word '" TYPE ." ' not found in dictionary."
@@
-684,7
+688,7
@@
4 SPACES
4 SPACES
- >
D
FA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
+ >
P
FA ( 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 )
( now we start decompiling until we hit the end of the word )
BEGIN ( end start )