From e7ffe0f277ef1d2b9b9005e01e2149b4d34190f7 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Mon, 4 Apr 2016 13:16:40 +1200 Subject: [PATCH] Bootstrapping outer interpreter. --- src/forth.jl | 53 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/forth.jl b/src/forth.jl index e720c7f..46c3c9c 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -1,23 +1,52 @@ -module FORTH +module forth -function input(prompt::AbstractString="") - print(prompt) - strip(readline) +currentLine = "" +currentPos = 0 + +function nextLine() + if eof(STDIN) + return false + else + currentLine = readLine() + currentPos = 1 + return true + end +end + +function readPattern(pattern::Regex) + m = match(pattern, currentLine) + pos += length(m.match) + return m.match end -dictCore = Set{AbstractString}([ - "bye" -]) +modes = Dict{AbstractString,Function}() +mode = "" -function coreEval(word::AbstractString) - if word == "bye" +function interpretPrimitive() + if haskey(dict, word) + dict[word]() + return true + else + return false end +interpretNonPrimitive() = false +interpretNumber() = false -function repl() +modes["interpret"] = () -> begin + getWord() + + if ! (interpretPrimitive() || + interpretNonPrimitive() || + interpretNumber()) + println("Error: unknown word '$word'.") + end +end - while true - line = input() +function repl() + mode = "interpret" + while mode != "stop" + modes[mode]() end end -- 2.20.1