X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fforth.jl;h=766bd4e1cdf2f3ff24685ab186a75188103f8202;hb=4b2f52917ce00e4af92d2a3a86e199d21748da5b;hp=90cf1cfafee4e3e56c9924d38dbb86ca428abf8a;hpb=6162399a858a397add5ed60ba33f31a4d6dba4c2;p=forth.jl.git diff --git a/src/forth.jl b/src/forth.jl index 90cf1cf..766bd4e 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -121,7 +121,13 @@ function defPrim(f::Function; name="nameless") 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 @@ -721,8 +727,9 @@ end) # Outer interpreter TRACE = defPrimWord("TRACE", () -> begin - print("RS: "); printRS() + println("reg.W: $(reg.W) reg.IP: $(reg.IP)") print("PS: "); printPS() + print("RS: "); printRS() print("[paused]") readline() @@ -812,6 +819,11 @@ PARSE = defPrimWord("PARSE", () -> begin return NEXT end) +BYE = defPrimWord("BYE", () -> begin + println("Bye!") + return 0 +end) + STATE, STATE_CFA = defNewVar("STATE", 0) INTERPRET = defWord("INTERPRET", @@ -820,29 +832,30 @@ INTERPRET = defWord("INTERPRET", DUP, FETCH, ZE, ZBRANCH, 3, DROP, EXIT, # Exit if TIB is exhausted - STATE_CFA, FETCH, ZBRANCH, 28, + STATE_CFA, FETCH, ZBRANCH, 31, # Compiling - DUP, FIND, ZBRANCH, 17, + DUP, FIND, QDUP, ZBRANCH, 19, # Found word. + SWAP, DROP, DUP, TOCFA, SWAP, INCR, FETCH, LIT, F_IMMED, AND, ZBRANCH, 4, # Immediate: Execute! - EXECUTE, BRANCH, -30, + EXECUTE, BRANCH, -33, # Not immediate: Compile! - COMMA, BRANCH, -33, + COMMA, BRANCH, -36, # No word found, parse number - BTICK, LIT, COMMA, NUMBER, COMMA, BRANCH, -40, + NUMBER, BTICK, LIT, COMMA, COMMA, BRANCH, -43, # Interpreting DUP, FIND, QDUP, ZBRANCH, 7, # Found word. Execute! - SWAP, DROP, TOCFA, EXECUTE, BRANCH, -51, + SWAP, DROP, TOCFA, EXECUTE, BRANCH, -54, # No word found, parse number and leave on stack - NUMBER, BRANCH, -54, + NUMBER, BRANCH, -57, EXIT] ) @@ -860,11 +873,6 @@ QUIT = defWord("QUIT", INTERPRET, PROMPT, BRANCH,-4]) -BYE = defPrimWord("BYE", () -> begin - println("Bye!") - return 0 -end) - INCLUDE = defPrimWord("INCLUDE", () -> begin pushPS(32) callPrim(mem[WORD]) @@ -934,9 +942,6 @@ IMMEDIATE = defPrimWord("IMMEDIATE", () -> begin return NEXT end, flags=F_IMMED) -TICK = defWord("'", - [LIT, 32, WORD, FIND, TOCFA, EXIT]) - #### VM loop #### @@ -948,7 +953,7 @@ elseif isfile(Pkg.dir("forth/src/lib.4th")) initFileName = Pkg.dir("forth/src/lib.4th") end -function run(;initialize=false) +function run(;initialize=true) # Begin with STDIN as source push!(sources, STDIN) @@ -982,6 +987,11 @@ function run(;initialize=false) 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]