From: Tim Vaughan Date: Sun, 17 Apr 2016 12:48:42 +0000 (+1200) Subject: Added arithmetic operations X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=4fc8dd4ebaa234f2a3e5798508c2d8a3987b421a;p=forth.jl.git Added arithmetic operations --- diff --git a/src/forth.jl b/src/forth.jl index edca589..e8478a4 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -155,6 +155,18 @@ function defConst(name::AbstractString, val::Int64; flags::Int64=0) return val end +function defWord(name::AbstractString, wordAddrs::Array{Int64,1}; flags::Int64=0) + createHeader(name, flags) + + mem[mem[HERE]] = DOCOL + mem[HERE] += 1 + + for wordAddr in wordAddrs + mem[mem[HERE]] = wordAddr + mem[HERE] += 1 + end +end + # Threading Primitives NEXT = defPrim("NEXT", () -> begin @@ -258,6 +270,48 @@ QDUP = defPrim("?DUP", () -> begin return NEXT end) +INCR = defPrim("1+", () -> begin + ensurePSDepth(1) + mem[reg.PSP] += 1 + return NEXT +end) + +DECR = defPrim("1-", () -> begin + ensurePSDepth(1) + mem[reg.PSP] -= 1 + return NEXT +end) + +ADD = defPrim("+", () -> begin + a = popPS() + b = popPS() + pushPS(a+b) + return NEXT +end) + +SUB = defPrim("-", () -> begin + a = popPS() + b = popPS() + pushPS(b-a) + return NEXT +end) + +MUL = defPrim("*", () -> begin + a = popPS() + b = popPS() + pushPS(a*b) + return NEXT +end) + +DIVMOD = defPrim("/MOD", () -> begin + a = popPS() + b = popPS() + q,r = divrem(b,a) + pushPS(r) + pushPS(q) + return NEXT +end) + LIT = defPrim("LIT", () -> begin pushPS(mem[reg.IP]) reg.IP += 1 @@ -484,11 +538,13 @@ TOCFA = defPrim(">CFA", () -> begin lenAndFlags = mem[addr+1] len = lenAndFlags & F_LENMASK - pushPS(addr + 1 + len) + pushPS(addr + 2 + len) return NEXT end) +TODFA = defWord(">DFA", [TOCFA, INCR1, EXIT]) + #### VM loop #### #function runVM(reg::Reg) # jmp = NEXT