From 5039981dc904e6382cbdcbca6b974338acb63309 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Sat, 9 Apr 2016 22:21:12 +1200 Subject: [PATCH] Taking a more traditional approach now. --- src/forth.jl | 89 ++++------------------------------------------------ 1 file changed, 6 insertions(+), 83 deletions(-) diff --git a/src/forth.jl b/src/forth.jl index 7f5756e..ca57e87 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -1,93 +1,16 @@ module forth -instream = STDIN +RS = Array{Int64, 1}() +DS = Array{Int64, 1}() -currentLine = "" -currentPos = 0 +primitives = Array{Expr,1}() -function readPattern(pattern::Regex) - if currentPos<1 || currentPos>length(currentLine) - if eof(instream) - return "" - else - global currentLine = readline(instream) - global currentPos = 1 - end - end - m = match(pattern, currentLine[currentPos:length(currentLine)]) - if m != nothing - global currentPos += length(m.match) - return m.match - else - return "" - end +type Definition + name::AbstractString + data::Array{Int64,1} end -readSpaces() = readPattern(r"^([ \t]*)") -readWord() = readPattern(r"^([^\s]+)") -readNewline() = readPattern(r"^(\n)") -readRestOfLine() = readPattern(r"^([^\n]*)") - -word = "" -function getWordOrNewline() - global word = readWord() - if word == "" - global word = readNewline() - end -end - -modes = Dict{AbstractString,Function}() -mode = "" - -dict = Dict{AbstractString, Function}() -dict["%J"] = () -> begin - rol = readRestOfLine() - println("Evaluating '$rol'") - eval(parse(rol)) -end - -function interpretPrimitive() - if haskey(dict, word) - dict[word]() - return true - else - return false - end -end -interpretNonPrimitive() = false -interpretNumber() = false - -modes["interpret"] = () -> begin - getWordOrNewline() - - if ! (interpretPrimitive() || - interpretNonPrimitive() || - interpretNumber()) - println("Error: unknown word '$word'.") - end -end - -function repl() - - global mode = "interpret" - idx = 1 - while mode != "stop" - modes[mode]() - end -end - -# Bootstrapping interpreter - -firstProg = """%J dict["\\n"] = () -> nothing -%J dict["\\n"] = () -> nothing -%J dict[""] = () -> global mode = "stop" -%J global DS = [] -%J global RS = [] -""" - -instream = IOBuffer(firstProg) -repl() end -- 2.20.1