From: Tim Vaughan Date: Mon, 11 Apr 2016 02:21:02 +0000 (+1200) Subject: Doing my head in! X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=forth.jl.git;a=commitdiff_plain;h=5899e2263814d846b7b0e9b08d745a1ef1ea0f04 Doing my head in! --- diff --git a/src/forth.jl b/src/forth.jl index c6b8cba..aac4c2a 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -25,7 +25,7 @@ STATE = 0 BASE = 10 -# Stack manipulation macros +# Stack manipulation functions function pushRS(val::Int64) global RSP @@ -51,7 +51,7 @@ function popPS() return val end -# Primitive creation functions +# Primitive creation and calling functions function defPrim(name::AbstractString, expr::Expr) global HERE, LATEST @@ -80,6 +80,8 @@ defConst(name::AbstractString, val::Int64) = defPrim(name, quote jmp = Next end) +callPrim(addr::Int64) = eval(primitives[-addr]) + # Threading Primitives NEXT = defPrim("NEXT", quote @@ -207,14 +209,30 @@ end) # I/O KEY = defPrim("KEY", quote + + jmp = NEXT +end) + +EMIT = defPrim("EMIT", quote + + jmp = NEXT +end) + +WORD = defPrim("WORD", quote + + jmp = NEXT +end) + +NUMBER = defPrim("NUMBER", quote + jmp = NEXT end) -# VM loop +#### VM loop #### jmp = NEXT function runVM() while true - eval(primitives[-jmp]) + callPrim(jmp) end end