Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STR is the inverse of READ-STRING #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions bin/lumen.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,19 @@ escape = function (s) {
}
return __s1 + "\"";
};
var simple_id63 = function (x) {
var ____id = (function () {
try {
return [true, read_string(x)];
}
catch (__e21) {
return [false, __e21];
}
})();
var __ok = ____id[0];
var __v9 = ____id[1];
return __ok && __v9 === x;
};
str = function (x, stack) {
if (nil63(x)) {
return "nil";
Expand All @@ -604,7 +617,11 @@ str = function (x, stack) {
}
} else {
if (string63(x)) {
return escape(x);
if (simple_id63(x)) {
return x;
} else {
return escape(x);
}
} else {
if (atom63(x)) {
return tostring(x);
Expand All @@ -627,7 +644,7 @@ str = function (x, stack) {
var ____o10 = x;
var __k16 = undefined;
for (__k16 in ____o10) {
var __v9 = ____o10[__k16];
var __v10 = ____o10[__k16];
var __e17;
if (numeric63(__k16)) {
__e17 = parseInt(__k16);
Expand All @@ -636,25 +653,25 @@ str = function (x, stack) {
}
var __k17 = __e17;
if (number63(__k17)) {
__xs11[__k17] = str(__v9, __l4);
__xs11[__k17] = str(__v10, __l4);
} else {
add(__ks, __k17 + ":");
add(__ks, str(__v9, __l4));
add(__ks, str(__v10, __l4));
}
}
drop(__l4);
var ____o11 = join(__xs11, __ks);
var ____i22 = undefined;
for (____i22 in ____o11) {
var __v10 = ____o11[____i22];
var __v11 = ____o11[____i22];
var __e18;
if (numeric63(____i22)) {
__e18 = parseInt(____i22);
} else {
__e18 = ____i22;
}
var ____i221 = __e18;
__s = __s + __sp + __v10;
__s = __s + __sp + __v11;
__sp = " ";
}
return __s + ")";
Expand All @@ -674,17 +691,17 @@ apply = function (f, args) {
return f.apply(f, __args);
};
call = function (f) {
var ____r75 = unstash(Array.prototype.slice.call(arguments, 1));
var __f = destash33(f, ____r75);
var ____id = ____r75;
var __args11 = cut(____id, 0);
var ____r77 = unstash(Array.prototype.slice.call(arguments, 1));
var __f = destash33(f, ____r77);
var ____id1 = ____r77;
var __args11 = cut(____id1, 0);
return apply(__f, __args11);
};
setenv = function (k) {
var ____r76 = unstash(Array.prototype.slice.call(arguments, 1));
var __k18 = destash33(k, ____r76);
var ____id1 = ____r76;
var __keys = cut(____id1, 0);
var ____r78 = unstash(Array.prototype.slice.call(arguments, 1));
var __k18 = destash33(k, ____r78);
var ____id2 = ____r78;
var __keys = cut(____id2, 0);
if (string63(__k18)) {
var __e19;
if (__keys.toplevel) {
Expand All @@ -697,15 +714,15 @@ setenv = function (k) {
var ____o12 = __keys;
var __k19 = undefined;
for (__k19 in ____o12) {
var __v11 = ____o12[__k19];
var __v12 = ____o12[__k19];
var __e20;
if (numeric63(__k19)) {
__e20 = parseInt(__k19);
} else {
__e20 = __k19;
}
var __k20 = __e20;
__entry[__k20] = __v11;
__entry[__k20] = __v12;
}
__frame[__k18] = __entry;
return __frame[__k18];
Expand Down
70 changes: 50 additions & 20 deletions bin/lumen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,32 @@ function escape(s)
end
return __s1 .. "\""
end
local function simple_id63(x)
local ____id = {xpcall(function ()
return read_string(x)
end, function (m)
if obj63(m) then
return m
else
local __e8
if string63(m) then
__e8 = clip(m, (search(m, ": ") or -2) + 2)
else
local __e9
if nil63(m) then
__e9 = ""
else
__e9 = str(m)
end
__e8 = __e9
end
return {stack = debug.traceback(), message = __e8}
end
end)}
local __ok = ____id[1]
local __v9 = ____id[2]
return __ok and __v9 == x
end
function str(x, stack)
if nil63(x) then
return "nil"
Expand All @@ -522,7 +548,11 @@ function str(x, stack)
end
else
if string63(x) then
return escape(x)
if simple_id63(x) then
return x
else
return escape(x)
end
else
if atom63(x) then
return tostring(x)
Expand All @@ -545,23 +575,23 @@ function str(x, stack)
local ____o10 = x
local __k8 = nil
for __k8 in next, ____o10 do
local __v9 = ____o10[__k8]
local __v10 = ____o10[__k8]
if number63(__k8) then
__xs11[__k8] = str(__v9, __l4)
__xs11[__k8] = str(__v10, __l4)
else
if not string63(__k8) then
__k8 = str(__k8, __l4)
end
add(__ks, __k8 .. ":")
add(__ks, str(__v9, __l4))
add(__ks, str(__v10, __l4))
end
end
drop(__l4)
local ____o11 = join(__xs11, __ks)
local ____i22 = nil
for ____i22 in next, ____o11 do
local __v10 = ____o11[____i22]
__s = __s .. __sp .. __v10
local __v11 = ____o11[____i22]
__s = __s .. __sp .. __v11
__sp = " "
end
return __s .. ")"
Expand All @@ -582,31 +612,31 @@ function apply(f, args)
return f(values(__args))
end
function call(f, ...)
local ____r72 = unstash({...})
local __f = destash33(f, ____r72)
local ____id = ____r72
local __args11 = cut(____id, 0)
local ____r75 = unstash({...})
local __f = destash33(f, ____r75)
local ____id1 = ____r75
local __args11 = cut(____id1, 0)
return apply(__f, __args11)
end
function setenv(k, ...)
local ____r73 = unstash({...})
local __k9 = destash33(k, ____r73)
local ____id1 = ____r73
local __keys = cut(____id1, 0)
local ____r76 = unstash({...})
local __k9 = destash33(k, ____r76)
local ____id2 = ____r76
local __keys = cut(____id2, 0)
if string63(__k9) then
local __e8
local __e10
if __keys.toplevel then
__e8 = hd(environment)
__e10 = hd(environment)
else
__e8 = last(environment)
__e10 = last(environment)
end
local __frame = __e8
local __frame = __e10
local __entry = __frame[__k9] or {}
local ____o12 = __keys
local __k10 = nil
for __k10 in next, ____o12 do
local __v11 = ____o12[__k10]
__entry[__k10] = __v11
local __v12 = ____o12[__k10]
__entry[__k10] = __v12
end
__frame[__k9] = __entry
return __frame[__k9]
Expand Down
6 changes: 5 additions & 1 deletion runtime.l
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,17 @@
(cat! s1 c1)))
(cat s1 "\"")))

(define simple-id? (x)
(let ((ok v) (guard (read-string x)))
(and ok (= v x))))

(define-global str (x stack)
(if (nil? x) "nil"
(nan? x) "nan"
(= x inf) "inf"
(= x -inf) "-inf"
(boolean? x) (if x "true" "false")
(string? x) (escape x)
(string? x) (if (simple-id? x) x (escape x))
(atom? x) (tostring x)
(function? x) "function"
(and stack (in? x stack)) "circular"
Expand Down