1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 21:57:38 +03:00

Change \177 to \u{029e}, correctly define keyword, change loadstring to load

This commit is contained in:
Gabriel M 2020-05-15 23:09:09 -03:00
parent a62a56f535
commit e2b93732eb
4 changed files with 16 additions and 10 deletions

View File

@ -233,7 +233,7 @@ local function lua_to_mal(a)
end end
local function lua_eval(str) local function lua_eval(str)
local f, err = loadstring("return "..str) local f, err = load("return "..str)
if err then if err then
types.throw("lua-eval: can't load code: "..err) types.throw("lua-eval: can't load code: "..err)
end end
@ -250,8 +250,14 @@ M.ns = {
['number?'] = function(a) return types._number_Q(a) end, ['number?'] = function(a) return types._number_Q(a) end,
symbol = function(a) return types.Symbol:new(a) end, symbol = function(a) return types.Symbol:new(a) end,
['symbol?'] = function(a) return types._symbol_Q(a) end, ['symbol?'] = function(a) return types._symbol_Q(a) end,
['string?'] = function(a) return types._string_Q(a) and "\177" ~= string.sub(a,1,1) end, ['string?'] = function(a) return types._string_Q(a) and "\u{029e}" ~= string.sub(a,1,2) end,
keyword = function(a) return "\177"..a end, keyword = function(a)
if types._keyword_Q(a) then
return a
else
return "\u{029e}"..a
end
end,
['keyword?'] = function(a) return types._keyword_Q(a) end, ['keyword?'] = function(a) return types._keyword_Q(a) end,
['fn?'] = function(a) return types._fn_Q(a) end, ['fn?'] = function(a) return types._fn_Q(a) end,
['macro?'] = function(a) return types._macro_Q(a) end, ['macro?'] = function(a) return types._macro_Q(a) end,
@ -272,7 +278,7 @@ M.ns = {
['-'] = function(a,b) return a-b end, ['-'] = function(a,b) return a-b end,
['*'] = function(a,b) return a*b end, ['*'] = function(a,b) return a*b end,
['/'] = function(a,b) return math.floor(a/b) end, ['/'] = function(a,b) return math.floor(a/b) end,
-- ['time-ms'] = function() return math.floor(socket.gettime() * 1000) end, ['time-ms'] = function() return math.floor(os.clock()*1000) end,
list = function(...) return List:new(table.pack(...)) end, list = function(...) return List:new(table.pack(...)) end,
['list?'] = function(a) return types._list_Q(a) end, ['list?'] = function(a) return types._list_Q(a) end,

View File

@ -23,8 +23,8 @@ function M._pr_str(obj, print_readably)
end end
return "{".. table.concat(res, " ").."}" return "{".. table.concat(res, " ").."}"
elseif type(obj) == 'string' then elseif type(obj) == 'string' then
if string.sub(obj,1,1) == "\177" then if string.sub(obj,1,2) == "\u{029e}" then
return ':' .. string.sub(obj,2) return ':' .. string.sub(obj,3)
else else
if _r then if _r then
local sval = obj:gsub('\\', '\\\\') local sval = obj:gsub('\\', '\\\\')

View File

@ -46,15 +46,15 @@ function M.read_atom(rdr)
elseif float_re:exec(token) then return tonumber(token) elseif float_re:exec(token) then return tonumber(token)
elseif string_re:exec(token) then elseif string_re:exec(token) then
local sval = string.sub(token,2,string.len(token)-1) local sval = string.sub(token,2,string.len(token)-1)
sval = string.gsub(sval, '\\\\', '\177') sval = string.gsub(sval, '\\\\', '\u{029e}')
sval = string.gsub(sval, '\\"', '"') sval = string.gsub(sval, '\\"', '"')
sval = string.gsub(sval, '\\n', '\n') sval = string.gsub(sval, '\\n', '\n')
sval = string.gsub(sval, '\177', '\\') sval = string.gsub(sval, '\u{029e}', '\\')
return sval return sval
elseif string.sub(token,1,1) == '"' then elseif string.sub(token,1,1) == '"' then
throw("expected '\"', got EOF") throw("expected '\"', got EOF")
elseif string.sub(token,1,1) == ':' then elseif string.sub(token,1,1) == ':' then
return "\177" .. string.sub(token,2) return "\u{029e}" .. string.sub(token,2)
elseif token == "nil" then return Nil elseif token == "nil" then return Nil
elseif token == "true" then return true elseif token == "true" then return true
elseif token == "false" then return false elseif token == "false" then return false

View File

@ -118,7 +118,7 @@ end
-- Keywords -- Keywords
function M._keyword_Q(obj) function M._keyword_Q(obj)
return M._string_Q(obj) and "\177" == string.sub(obj,1,1) return M._string_Q(obj) and "\u{029e}" == string.sub(obj,1,2)
end end