From 9ff796f2feff7d5c7e714ce610bc0e5c43f9b71e Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Tue, 26 Apr 2016 21:20:21 +1200 Subject: [PATCH] Fixed broken DO LOOP --- src/forth.jl | 22 +++++++++++++++++++++- src/lib.4th | 11 ++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/forth.jl b/src/forth.jl index e2292e1..99d9451 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -952,11 +952,31 @@ CHAR = defPrimWord("CHAR", () -> begin return NEXT end) +initialized = false +initFileName = nothing +if isfile("lib.4th") + initFileName = "lib.4th" +elseif isfile(Pkg.dir("forth/src/lib.4th")) + initFileName = Pkg.dir("forth/src/lib.4th") +end + + #### VM loop #### -function run() +function run(;initialize=true) # Begin with STDIN as source push!(sources, STDIN) + global initialized, initFileName + if !initialized && initialize + if initFileName != nothing + print("Including definitions from $initFileName.") + push!(sources, open(initFileName, "r")) + initialized = true + else + println("No library file found. Only primitive words available.") + end + end + # Start with IP pointing to first instruction of outer interpreter reg.IP = QUIT + 1 diff --git a/src/lib.4th b/src/lib.4th index b44fb65..c9e07bf 100644 --- a/src/lib.4th +++ b/src/lib.4th @@ -106,25 +106,18 @@ ; : DO IMMEDIATE - ' LIT , - HERE @ - 0 , - ' >R , ' >R , ' >R , + ' >R , ' >R , HERE @ ; : I RSP@ 2- @ ; -: LEAVE RDROP RDROP RDROP EXIT ; - : LOOP IMMEDIATE ' R> , ' R> , ' 1+ , ' 2DUP , ' - , ' SWAP , ' >R , ' SWAP , ' >R , ' 0<= , ' 0BRANCH , HERE @ - , - ' RDROP , ' RDROP , ' RDROP , - DUP HERE @ SWAP - - SWAP ! + ' RDROP , ' RDROP , ; -- 2.20.1