-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