From: Tim Vaughan Date: Mon, 4 Apr 2016 01:16:40 +0000 (+1200) Subject: Bootstrapping outer interpreter. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=e7ffe0f277ef1d2b9b9005e01e2149b4d34190f7;p=forth.jl.git Bootstrapping outer interpreter. --- 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