X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fforth.jl;h=766bd4e1cdf2f3ff24685ab186a75188103f8202;hb=4b2f52917ce00e4af92d2a3a86e199d21748da5b;hp=7c0fea8b35b4dd6ef011aa49c16f62f24768df6f;hpb=dc5ec6fb1faceb5aa0e50e485491bfca08a18b1e;p=forth.jl.git diff --git a/src/forth.jl b/src/forth.jl index 7c0fea8..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 @@ -679,7 +685,7 @@ TOCFA = defPrimWord(">CFA", () -> begin return NEXT end) -TODFA = defWord(">DFA", [TOCFA, INCR, EXIT]) +TOPFA = defWord(">PFA", [TOCFA, INCR, EXIT]) # Branching @@ -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() @@ -767,11 +774,16 @@ WORD = defPrimWord("WORD", () -> begin # Start reading in word count = 0 - while (mem[TOIN] begin 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] begin + println("Bye!") + return 0 +end) + STATE, STATE_CFA = defNewVar("STATE", 0) INTERPRET = defWord("INTERPRET", @@ -789,29 +832,30 @@ INTERPRET = defWord("INTERPRET", DUP, FETCH, ZE, ZBRANCH, 3, DROP, EXIT, # Exit if TIB is exhausted - STATE_CFA, FETCH, ZBRANCH, 27, + 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 - NUMBER, BTICK, LIT, COMMA, BRANCH, -39, + NUMBER, BTICK, LIT, COMMA, COMMA, BRANCH, -43, # Interpreting - DUP, FIND, ZBRANCH, 5, + DUP, FIND, QDUP, ZBRANCH, 7, # Found word. Execute! - TOCFA, EXECUTE, BRANCH, -47, + SWAP, DROP, TOCFA, EXECUTE, BRANCH, -54, # No word found, parse number and leave on stack - NUMBER, BRANCH, -50, + NUMBER, BRANCH, -57, EXIT] ) @@ -829,14 +873,11 @@ QUIT = defWord("QUIT", INTERPRET, PROMPT, BRANCH,-4]) -BYE = defPrimWord("BYE", () -> begin - return 0 -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")) @@ -850,7 +891,6 @@ end) # Compilation HEADER = defPrimWord("HEADER", () -> begin - wordAddr = popPS()+1 wordLen = mem[wordAddr-1] word = getString(wordAddr, wordLen) @@ -877,13 +917,13 @@ HIDDEN = defPrimWord("HIDDEN", () -> begin 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, @@ -902,9 +942,6 @@ IMMEDIATE = defPrimWord("IMMEDIATE", () -> begin return NEXT end, flags=F_IMMED) -TICK = defWord("'", - [WORD, FIND, TOCFA, EXIT]) - #### VM loop #### @@ -916,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) @@ -938,25 +975,30 @@ function run(;initialize=false) # Everyting else is simply a consequence of this loop! jmp = NEXT while jmp != 0 -# try - println("Entering prim $(getPrimName(jmp))") + try + #println("Entering prim $(getPrimName(jmp))") jmp = callPrim(jmp) -# catch ex -# showerror(STDOUT, ex) -# println() -# -# while !isempty(sources) && currentSource() != STDIN -# close(pop!(sources)) -# end -# -# mem[STATE] = 0 -# mem[NUMTIB] = 0 -# reg.PSP = mem[PSP0] -# reg.RSP = mem[RSP0] -# reg.IP = QUIT + 1 -# jmp = NEXT -# end + catch ex + showerror(STDOUT, ex) + println() + + while !isempty(sources) && currentSource() != 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] + reg.RSP = mem[RSP0] + reg.IP = QUIT + 1 + jmp = NEXT + end end end