return -length(primitives)
end
-callPrim(addr::Int64) = primitives[-addr]()
+function callPrim(addr::Int64)
+ if addr >=0 || -addr>length(primitives)
+ error("Attempted to execute non-existent primitive at address $addr.")
+ else
+ primitives[-addr]()
+ end
+end
getPrimName(addr::Int64) = primNames[-addr]
# Word creation functions
# Outer interpreter
TRACE = defPrimWord("TRACE", () -> begin
- println("Val: $(popPS())")
- print("RS: "); printRS()
+ println("reg.W: $(reg.W) reg.IP: $(reg.IP)")
print("PS: "); printPS()
+ print("RS: "); printRS()
print("[paused]")
readline()
return NEXT
end, flags=F_IMMED)
-TICK = defWord("'",
- [LIT, 32, WORD, FIND, TOCFA, EXIT])
-
#### VM loop ####
initFileName = Pkg.dir("forth/src/lib.4th")
end
-function run(;initialize=false)
+function run(;initialize=true)
# Begin with STDIN as source
push!(sources, STDIN)
close(pop!(sources))
end
+ # Want backtrace in here eventually
+ println("reg.W: $(reg.W) reg.IP: $(reg.IP)")
+ print("PS: "); printPS()
+ print("RS: "); printRS()
+
mem[STATE] = 0
mem[NUMTIB] = 0
reg.PSP = mem[PSP0]
: LITERAL IMMEDIATE ['] LIT , , ;
-: CHAR BL WORD 1+ @ ;
+: ' BL WORD FIND >CFA ;
+: CHAR BL WORD 1+ @ ;
: [CHAR] IMMEDIATE
CHAR
['] LIT , ,
;
: +LOOP IMMEDIATE
+
+ trace
+
['] DUP , \ Store copy of increment
['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
+ trace
+
\ Condition differently depending on sign of increment
['] SWAP , ['] 0>= , [COMPILE] IF
['] 0<= ,
['] 0> ,
[COMPILE] THEN
+ trace
+
\ Branch back to begining of loop kernel
['] 0BRANCH , HERE @ - ,
\ Clean up
['] RDROP , ['] RDROP , ['] RDROP ,
+ trace
+
\ Record address of loop end for any LEAVEs to use
HERE @ SWAP !