Bootstrapping outer interpreter.
[forth.jl.git] / src / forth.jl
1 module forth
2
3 currentLine = ""
4 currentPos = 0
5
6 function nextLine()
7     if eof(STDIN)
8         return false
9     else
10         currentLine = readLine()
11         currentPos = 1
12         return true
13     end
14 end
15
16 function readPattern(pattern::Regex)
17     m = match(pattern, currentLine)
18     pos += length(m.match)
19     return m.match
20 end
21
22 modes = Dict{AbstractString,Function}()
23 mode = ""
24
25 function interpretPrimitive()
26     if haskey(dict, word)
27         dict[word]()
28         return true
29     else
30         return false
31 end
32 interpretNonPrimitive() = false
33 interpretNumber() = false
34
35 modes["interpret"] = () -> begin
36     getWord()
37     
38     if ! (interpretPrimitive() ||
39         interpretNonPrimitive() ||
40         interpretNumber())
41         println("Error: unknown word '$word'.")
42     end
43 end
44
45 function repl()
46
47     mode = "interpret"
48     while mode != "stop"
49         modes[mode]()
50     end
51 end
52
53 end