Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. local computer = require("computer")
  2. local term = require("term")
  3. local text = require("text")
  4. local interpret
  5.  
  6. -- Current Word List and additional information
  7. local WORDS = {WORDS = {isLua = true}, ["."] = {isLua = true},
  8.                ["+"] = {isLua = true}, [":"] = {isLua = true},
  9.                EXIT = {isLua = true}, IGNORECAPS = {isLua = true},
  10.                HOE = {isLua = true},}
  11.  
  12. local STACK = {n = 0} -- Stack, n is number of items in the stack
  13. local VER = "0a" -- Version
  14. local HOE = true -- H.O.E. Halt on error
  15. local IGNORECAPS = false -- When enabled capitalizes inputs automatically
  16. local IDW = false -- Is defining word, best way I could think of doing this
  17. local CWD = ""    -- Current Word Definition, SHOULD BE BLANK WHEN IDW IS FALSE
  18. local CWN = ""    -- Current Word Name, SHOULD BE BLANK WHEN IDW IS FALSE
  19.  
  20. -- Halt on error
  21. WORDS.HOE[1] = function()
  22.   if STACK.n > 0 then
  23.     if STACK[STACK.n] == 1 then
  24.       HOE = true
  25.     else
  26.       HOE = false
  27.     end
  28.     STACK[STACK.n] = nil
  29.     STACK.n = STACK.n - 1
  30.   else
  31.     io.stderr:write("ERROR: Stack Empty\n")
  32.     return 1 -- An error happened
  33.   end
  34. end
  35.  
  36. -- Ignore Caps
  37. WORDS.IGNORECAPS[1] = function()
  38.   if STACK.n > 0 then
  39.     if STACK[STACK.n] == 1 then
  40.       IGNORECAPS = true
  41.     else
  42.       IGNORECAPS = false
  43.     end
  44.     STACK[STACK.n] = nil
  45.     STACK.n = STACK.n - 1
  46.   else
  47.     io.stderr:write("ERROR: Stack Empty\n")
  48.     return 1 -- An error happened
  49.   end
  50. end
  51.  
  52. -- Display all current words
  53. WORDS.WORDS[1] = function()
  54.   for i, word in pairs(WORDS) do
  55.     io.write(i.." ")
  56.   end
  57.   io.write("\n")
  58.   return 0
  59. end
  60.  
  61. -- Output top item in stack to terminal
  62. WORDS["."][1] = function()
  63.   if not pcall(function()
  64.     io.write(STACK[STACK.n].."\n")
  65.     STACK[STACK.n] = nil
  66.     STACK.n = STACK.n - 1
  67.     return 0 -- No error
  68.   end) then
  69.     io.stderr:write("ERROR: Stack Empty\n")
  70.     return 1 -- An error happened
  71.   end
  72. end
  73.  
  74. -- Addition
  75. WORDS["+"][1] = function()
  76.   if not pcall(function()
  77.     STACK[STACK.n-1] = STACK[STACK.n] + STACK[STACK.n-1]
  78.     STACK[STACK.n] = nil
  79.     STACK.n = STACK.n-1
  80.   end) then
  81.     io.stderr:write("ERROR: Not enough items in stack\n")
  82.     return 1
  83.   end
  84. end
  85.  
  86. -- Begin Word Definition
  87. WORDS[":"][1] = function(NEXTWORD)
  88.   IDW = true
  89.   CWD = ""
  90.   CWN = NEXTWORD
  91.   return 2 -- Skip the next word
  92. end
  93.  
  94. -- Exit interpreter
  95. WORDS.EXIT[1] = function()
  96.   os.exit(0)
  97. end
  98.  
  99. -- Interperate The Input
  100. local function interpret(input)
  101.   local splitInput = text.tokenize(input)
  102.  
  103.   for wordIndex, wordText in pairs(splitInput) do
  104.   if not SKIPNEXT and not IDW then
  105.     if tonumber(wordText) then
  106.       -- Current "word" is just a number
  107.       STACK[STACK.n+1] = tonumber(wordText)
  108.       STACK.n = STACK.n + 1
  109.     else
  110.       if IGNORECAPS then wordText = wordText:upper() end
  111.       if WORDS[wordText] then
  112.         -- The Word Exists
  113.         if WORDS[wordText].isLua then
  114.           -- Function is implemented in lua
  115.           local EXITCODE = WORDS[wordText][1](splitInput[wordIndex+1])
  116.           if EXITCODE == 1 and HOE then
  117.             io.stderr:write("\nExecuting Stopped, View Error Above\n")
  118.             break
  119.           elseif EXITCODE == 2 then
  120.             SKIPNEXT = true
  121.           end
  122.  
  123.         else
  124.           -- Function is implemented in Forth
  125.           interpret(WORDS[wordText][1])
  126.         end
  127.       else
  128.         -- The Word Does Not Exist
  129.         io.stderr:write("ERROR: Word Does Not Exist\n")
  130.       end
  131.  
  132.     end
  133.   elseif SKIPNEXT then; SKIPNEXT = false;
  134.   else
  135.     if wordText == ";" then
  136.       WORDS[CWN] = {}; WORDS[CWN][1] = CWD; CWD = ""; CWN = ""; IDW = false;
  137.     else; CWD = CWD..wordText.." "; end
  138.   end
  139.   end
  140. end
  141.  
  142. -- Actual Running
  143.  
  144. term.clear()
  145. io.write("Zef's Forth "..VER.."\n")
  146. io.write(computer.freeMemory().." bytes free\n\n")
  147. while true do
  148.   if not IDW then; io.write("> "); else; io.write("COMP> "); end
  149.   local input = io.read()
  150.   interpret(input)
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement