~technomancy/fennel-lang.org

3b5fa2d272dcc0611522bfe362e8b62d229ad0f8 — Phil Hagelberg 9 days ago 7a35468
Add error handling to See Fennel.
4 files changed, 52 insertions(+), 24 deletions(-)

M antifennel
M fennel.css
M see.html
M see.lua
M antifennel => antifennel +20 -18
@@ 1918,7 1918,7 @@ package.preload["lang.lexer"] = package.preload["lang.lexer"] or function(...)
  local ASCII_a, ASCII_f, ASCII_z = 97, 102, 122
  local ASCII_A, ASCII_Z = 65, 90
  local END_OF_STREAM = ( - 1)
  local Reserved_keyword = {["and"] = 1, ["break"] = 2, ["do"] = 3, ["else"] = 4, ["elseif"] = 5, ["end"] = 6, ["false"] = 7, ["for"] = 8, ["function"] = 9, ["if"] = 11, ["in"] = 12, ["local"] = 13, ["nil"] = 14, ["not"] = 15, ["or"] = 16, ["repeat"] = 17, ["return"] = 18, ["then"] = 19, ["true"] = 20, ["until"] = 21, ["while"] = 22, ["goto"] = 10}
  local Reserved_keyword = {["and"] = 1, ["break"] = 2, ["do"] = 3, ["else"] = 4, ["elseif"] = 5, ["end"] = 6, ["false"] = 7, ["for"] = 8, ["function"] = 9, ["goto"] = 10, ["if"] = 11, ["in"] = 12, ["local"] = 13, ["nil"] = 14, ["not"] = 15, ["or"] = 16, ["repeat"] = 17, ["return"] = 18, ["then"] = 19, ["true"] = 20, ["until"] = 21, ["while"] = 22}
  local uint64, int64 = ffi.typeof("uint64_t"), ffi.typeof("int64_t")
  local complex = ffi.typeof("complex")
  local Token_symbol = {TK_concat = "..", TK_eof = "<eof>", TK_eq = "==", TK_ge = ">=", TK_le = "<=", TK_ne = "~="}


@@ 5129,7 5129,7 @@ package.preload["fennel"] = package.preload["fennel"] or function(...)
      walk((custom_iterator or pairs), nil, nil, root)
      return root
    end
    local lua_keywords = {"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while"}
    local lua_keywords = {"and", "break", "do", "else", "elseif", "end", "false", "for", "function", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "goto"}
    for i, v in ipairs(lua_keywords) do
      lua_keywords[v] = i
    end


@@ 5556,25 5556,27 @@ local function compile(rdr, filename)
  local ast_tree = parse(ast_builder, ls)
  return letter(compiler(nil, ast_tree))
end
local filename = arg[1]
local f = (filename and io.open(filename))
if f then
  f:close()
  for _, code in ipairs(compile(reader.file(filename), filename)) do
    print(fnlfmt.fnlfmt(code))
  end
  return nil
elseif (arg[1] == false) then
  local function _1_(str)
if ((debug and debug.getinfo) and (debug.getinfo(3) == nil)) then
  local filename = arg[1]
  local f = (filename and io.open(filename))
  if f then
    f:close()
    for _, code in ipairs(compile(reader.file(filename), filename)) do
      print(fnlfmt.fnlfmt(code))
    end
    return nil
  else
    print(("Usage: %s LUA_FILENAME"):format(arg[0]))
    print("Compiles LUA_FILENAME to Fennel and prints output.")
    return os.exit(1)
  end
else
  local function _1_(str, source)
    local out = {}
    for _, code in ipairs(compile(reader.string(str), "*source")) do
    for _, code in ipairs(compile(reader.string(str), (source or "*source"))) do
      table.insert(out, fnlfmt.fnlfmt(code))
    end
    return out
    return table.concat(out, "\n")
  end
  return _1_
else
  print(("Usage: %s LUA_FILENAME"):format(arg[0]))
  print("Compiles LUA_FILENAME to Fennel and prints output.")
  return os.exit(1)
end

M fennel.css => fennel.css +10 -0
@@ 119,3 119,13 @@ tt, pre, code, kbd { font-family: "Fira Mono", Inconsolata, monospace; }
}

hr { margin-top: 2em; clear: both; }

#see-fennel #out {
    color: #dd1111;
    clear: both;
    font-size: 80%;
    padding: 1em;
    background-color: #f8f8f8;
    border-radius: 3px;
    border: 1px solid #ddd;
}

M see.html => see.html +5 -1
@@ 16,12 16,16 @@
      <button id="compile-fennel">→</button>
      <button id="compile-lua">←</button>

      <pre id="out"></pre>

      <hr />

      <p>Fennel is copyright © 2016-2020 Calvin Rose and
        contributors and is released under the MIT/X11 license.
        <a href="https://git.sr.ht/~technomancy/fennel-lang.org">This website</a>
        is under the same terms.</p>
        is under the same terms.
        <a href="https://git.sr.ht/~technomancy/antifennel">Antifennel</a>
        is also under the same terms.</p>
    </div>
  </body>
</html>

M see.lua => see.lua +17 -5
@@ 1,11 1,12 @@
package.path = "./?.lua"
local js = require("js")

package.loaded.ffi = {typeof=function() end}os = {getenv=function() end}
-- minimal shims just to allow the compilers to load in Fengari
package.loaded.ffi = {typeof=function() end}
os = {getenv=function() end}
io = {open=function() end}
bit = {band = function() end, rshift=function() end}
unpack = table.unpack
arg={false}

function print(...) js.global.console.log(...) end



@@ 16,18 17,29 @@ local fennel = require("fennel")
local document = js.global.document
local compile_fennel = document:getElementById("compile-fennel")
local compile_lua = document:getElementById("compile-lua")
local out = document:getElementById("out")

local fennel_source = document:getElementById("fennel-source")
local lua_source = document:getElementById("lua-source")

compile_fennel.onclick = function()
   lua_source.value = fennel.compileString(fennel_source.value)
   local ok, code = pcall(fennel.compileString, fennel_source.value)
   if ok then lua_source.value = code
   else out.innerHTML = "Fennel: " .. code end
end

compile_lua.onclick = function()
   fennel_source.value = table.concat(antifennel(lua_source.value), "\n")
   -- for Lua that doesn't parse, antifennel gives crummy error messages
   local ok, msg = load(lua_source.value)
   if not ok then out.innerHTML = "Lua: " .. msg return end

   local ok, code = pcall(antifennel, lua_source.value)
   if ok then fennel_source.value = code
   else out.innerHTML = "Lua: " .. code end
end

out.innerHTML = "Loaded Fennel " .. fennel.version .. " in " .. _VERSION

-- TODO: keyboard shortcuts
-- TODO: error handling
-- TODO: multiple Fennel versions?
-- TODO: samples