Advertisement
super_koder

CC:NumberWorks-OS

Jun 16th, 2024 (edited)
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. Functions = require "library"
  2. Dictionary = {}
  3. Stack = {}
  4.  
  5. function eval(source)
  6.     for _, token in ipairs(source) do
  7.         if type(token) == "table" then
  8.             table.insert(Stack, token)
  9.         elseif tonumber(token) ~= nil then
  10.             table.insert(Stack, tonumber(token))
  11.         elseif token:sub(1, 1) == "#" then
  12.             table.insert(Stack, token:sub(2))
  13.         elseif token:sub(1, 1) == '"' then
  14.             table.insert(Stack, token:sub(2, #token - 1))
  15.         elseif Functions[token] ~= nil then
  16.             Functions[token](Stack)
  17.         elseif Dictionary[token] ~= nil then
  18.             eval(Dictionary[token])
  19.         else
  20.             term.setTextColor(colors.red)
  21.             print("Unknown token: "..token)
  22.             term.setTextColor(colors.white)
  23.  
  24.             return
  25.         end
  26.     end
  27. end
  28.  
  29. -- Reading raw text
  30. local function parse(str)
  31.     local stack = {}
  32.     local current_list = {}
  33.     local buffer = ""
  34.     local inQuotes = false
  35.     local quoteChar = nil
  36.  
  37.     local function append()
  38.         if buffer ~= "" then
  39.             table.insert(current_list, buffer)
  40.             buffer = ""
  41.         end
  42.     end
  43.  
  44.     for c in str:gmatch "." do
  45.         if inQuotes then
  46.             if c == quoteChar then
  47.                 buffer = buffer .. c
  48.                 append()
  49.                 inQuotes = false
  50.                 quoteChar = nil
  51.             else
  52.                 buffer = buffer .. c
  53.             end
  54.         else
  55.             if c == '{' then
  56.                 append()
  57.                 table.insert(stack, current_list)
  58.                 current_list = {}
  59.             elseif c == '}' then
  60.                 append()
  61.                 local last = table.remove(stack)
  62.                 table.insert(last, current_list)
  63.                 current_list = last
  64.             elseif c == '"' or c == "'" then
  65.                 append()
  66.                 buffer = buffer .. c
  67.                 inQuotes = true
  68.                 quoteChar = c
  69.             elseif c:match("[%w%p]") ~= nil then
  70.                 buffer = buffer .. c
  71.             elseif c:match("%s") ~= nil then
  72.                 append()
  73.             end
  74.         end
  75.     end
  76.     append()
  77.  
  78.     -- Handle the case where the input string does not have balanced braces
  79.     while #stack > 0 do
  80.         local last = table.remove(stack)
  81.         table.insert(last, current_list)
  82.         current_list = last
  83.     end
  84.  
  85.     return current_list
  86. end
  87.  
  88. local function readCode(text)
  89.     eval(parse(text))
  90. end
  91.  
  92. -- main, to be changed before ported to ComputerCraft
  93.  
  94. term.setBackgroundColor(colors.blue)
  95. readCode("clear")
  96.  
  97.  
  98. while true do
  99.     write("$ ")
  100.     local text = read()
  101.     if text == "quit" then break end
  102.  
  103.     readCode(text)
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement