From 5899e2263814d846b7b0e9b08d745a1ef1ea0f04 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Mon, 11 Apr 2016 14:21:02 +1200 Subject: [PATCH] Doing my head in! --- src/forth.jl | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) 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 -- 2.20.1