Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require("computer")
- local term = require("term")
- local text = require("text")
- local interpret
- -- Current Word List and additional information
- local WORDS = {WORDS = {isLua = true}, ["."] = {isLua = true},
- ["+"] = {isLua = true}}
- local STACK = {n = 0} -- Stack, n is number of items in the stack
- local VER = "0a" -- Version
- local HOE = true -- H.O.E. Halt on error
- WORDS.WORDS[1] = function()
- for i, word in pairs(WORDS) do
- io.write(i.." ")
- end
- io.write("\n")
- return 0
- end
- WORDS["."][1] = function()
- if not pcall(function()
- io.write(STACK[STACK.n].."\n")
- STACK.n = STACK.n - 1
- return 0 -- No error
- end) then
- io.write("ERROR: Stack Empty\n")
- return 1 -- An error happened
- end
- end
- WORDS["+"][1] = function()
- if not pcall(function()
- STACK[STACK.n-1] = STACK[STACK.n] + STACK[STACK.n-1]
- STACK.n = STACK.n-1
- end) then
- io.write("ERROR: Not enough items in stack\n")
- return 1
- end
- end
- -- Actual Running
- term.clear()
- io.write("Zef's FORTH "..VER.."\n")
- io.write(computer.freeMemory().." bytes free\n\n")
- io.write("> ")
- local input = io.read()
- local function interpret(input)
- local splitInput = text.tokenize(input)
- for wordIndex, wordText in pairs(splitInput) do
- if tonumber(wordText) then
- -- Current "word" is just a number
- STACK[STACK.n+1] = tonumber(wordText)
- STACK.n = STACK.n + 1
- else
- if WORDS[wordText] then
- -- The Word Exists
- if WORDS[wordText].isLua then
- -- Function is implemented in lua
- if WORDS[wordText][1]() == 1 and HOE then
- io.write("\nExecuting Stopped, View Error Above\n")
- break
- end
- else
- -- Function is implemented in Forth
- interpret(WORDS[wordText][1])
- end
- else
- -- The Word Does Not Exist
- end
- end
- end
- end
- interpret(input)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement