From: Tim Vaughan Date: Mon, 18 Apr 2016 11:19:15 +0000 (+1200) Subject: Added compilation words. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=b6b53f943f1a2ceb89b03c2a2930c66d4972df07;p=forth.jl.git Added compilation words. --- diff --git a/src/forth.jl b/src/forth.jl index 01618f4..cdfe88d 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -659,6 +659,61 @@ end) TODFA = defWord(">DFA", [TOCFA, INCR, EXIT]) +# Compilation + +CREATE = defPrim("CREATE", () -> begin + + wordLen = popPS() + wordAddr = popPS() + word = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]]) + + mem[mem[HERE]] = mem[LATEST]; mem[HERE] += 1 + mem[LATEST] = mem[HERE] + mem[mem[HERE]] = wordLen; mem[HERE] += 1 + + mem[mem[HERE]:(mem[HERE]+wordLen-1)] = collect(Int64, word) + mem[HERE] += wordLen + + return mem[NEXT] +end) + +COMMA = defPrim(",", () -> begin + mem[mem[HERE]] = popPS() + mem[HERE] += 1 + + return mem[NEXT] +end) + +LBRAC = defPrim("[", () -> begin + mem[STATE] = 0 + return mem[NEXT] +end, flags=F_IMMEDIATE) + +RBRAC = defPrim("]", () -> begin + mem[STATE] = 1 + return mem[NEXT] +end, flags=F_IMMEDIATE) + +HIDDEN = defPrim("HIDDEN", () -> begin + addr = popPS() + 1 + mem[addr] = mem[addr] $ F_HIDDEN + reurn mem[NEXT] +end) + +COLON = defWord(":", + [WORD, + CREATE, + LIT, DOCOL, COMMA, + LATEST, FETCH, HIDDEN, + RBRAC, + EXIT]) + +SEMICOLON = defWord(";", + [LIT, EXIT, COMMA, + LATEST, FETCH, HIDDEN, + LBRAC, + EXIT], flags=F_IMMEDIATE) + #### VM loop #### function runVM() jmp = NEXT