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

Update ADD/DROP to optionally take an index #209

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
54 changes: 36 additions & 18 deletions bin/lumen.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,30 @@ string_literal63 = function (x) {
id_literal63 = function (x) {
return string63(x) && char(x, 0) === "|";
};
add = function (l, x) {
l.push(x);
add = function (l, x, i) {
if (nil63(i)) {
l.push(x);
} else {
if (number63(i)) {
l.splice(i, 0, x);
} else {
l[i] = x;
}
}
return undefined;
};
drop = function (l) {
return l.pop();
drop = function (l, i) {
if (nil63(i)) {
return l.pop();
} else {
if (number63(i)) {
return l.splice(i, 1)[0];
} else {
var __x1 = l[i];
delete l[i];
return __x1;
}
}
};
last = function (l) {
return l[edge(l)];
Expand Down Expand Up @@ -192,10 +210,10 @@ reduce = function (f, x) {
join = function () {
var __ls = unstash(Array.prototype.slice.call(arguments, 0));
var __r38 = [];
var ____x1 = __ls;
var ____x2 = __ls;
var ____i4 = 0;
while (____i4 < _35(____x1)) {
var __l11 = ____x1[____i4];
while (____i4 < _35(____x2)) {
var __l11 = ____x2[____i4];
if (__l11) {
var __n3 = _35(__r38);
var ____o2 = __l11;
Expand Down Expand Up @@ -223,26 +241,26 @@ find = function (f, t) {
var ____o3 = t;
var ____i6 = undefined;
for (____i6 in ____o3) {
var __x2 = ____o3[____i6];
var __x3 = ____o3[____i6];
var __e5 = undefined;
if (numeric63(____i6)) {
__e5 = parseInt(____i6);
} else {
__e5 = ____i6;
}
var ____i61 = __e5;
var __y = f(__x2);
var __y = f(__x3);
if (__y) {
return __y;
}
}
};
first = function (f, l) {
var ____x3 = l;
var ____x4 = l;
var ____i7 = 0;
while (____i7 < _35(____x3)) {
var __x4 = ____x3[____i7];
var __y1 = f(__x4);
while (____i7 < _35(____x4)) {
var __x5 = ____x4[____i7];
var __y1 = f(__x5);
if (__y1) {
return __y1;
}
Expand Down Expand Up @@ -279,10 +297,10 @@ sort = function (l, f) {
};
map = function (f, x) {
var __t1 = [];
var ____x6 = x;
var ____x7 = x;
var ____i9 = 0;
while (____i9 < _35(____x6)) {
var __v3 = ____x6[____i9];
while (____i9 < _35(____x7)) {
var __v3 = ____x7[____i9];
var __y2 = f(__v3);
if (is63(__y2)) {
add(__t1, __y2);
Expand Down Expand Up @@ -338,7 +356,7 @@ empty63 = function (t) {
var ____o6 = t;
var ____i12 = undefined;
for (____i12 in ____o6) {
var __x7 = ____o6[____i12];
var __x8 = ____o6[____i12];
var __e9 = undefined;
if (numeric63(____i12)) {
__e9 = parseInt(____i12);
Expand Down Expand Up @@ -687,7 +705,7 @@ setenv = function (k) {
var __keys = cut(____id1, 0);
if (string63(__k18)) {
var __e19 = undefined;
if (__keys.toplevel) {
if (drop(__keys, "toplevel")) {
__e19 = hd(environment);
} else {
__e19 = last(environment);
Expand Down
55 changes: 37 additions & 18 deletions bin/lumen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,30 @@ end
function id_literal63(x)
return string63(x) and char(x, 0) == "|"
end
function add(l, x)
return table.insert(l, x)
function add(l, x, i)
if nil63(i) then
table.insert(l, x)
else
if number63(i) then
table.insert(l, i + 1, x)
else
l[i] = x
end
end
return nil
end
function drop(l)
return table.remove(l)
function drop(l, i)
if nil63(i) then
return table.remove(l)
else
if number63(i) then
return table.remove(l, i)
else
local __x1 = l[i]
l[i] = nil
return __x1
end
end
end
function last(l)
return l[edge(l) + 1]
Expand Down Expand Up @@ -178,10 +197,10 @@ end
function join(...)
local __ls = unstash({...})
local __r37 = {}
local ____x2 = __ls
local ____x3 = __ls
local ____i4 = 0
while ____i4 < _35(____x2) do
local __l11 = ____x2[____i4 + 1]
while ____i4 < _35(____x3) do
local __l11 = ____x3[____i4 + 1]
if __l11 then
local __n3 = _35(__r37)
local ____o2 = __l11
Expand All @@ -202,19 +221,19 @@ function find(f, t)
local ____o3 = t
local ____i6 = nil
for ____i6 in next, ____o3 do
local __x3 = ____o3[____i6]
local __y = f(__x3)
local __x4 = ____o3[____i6]
local __y = f(__x4)
if __y then
return __y
end
end
end
function first(f, l)
local ____x4 = l
local ____x5 = l
local ____i7 = 0
while ____i7 < _35(____x4) do
local __x5 = ____x4[____i7 + 1]
local __y1 = f(__x5)
while ____i7 < _35(____x5) do
local __x6 = ____x5[____i7 + 1]
local __y1 = f(__x6)
if __y1 then
return __y1
end
Expand Down Expand Up @@ -242,10 +261,10 @@ function sort(l, f)
end
function map(f, x)
local __t1 = {}
local ____x7 = x
local ____x8 = x
local ____i9 = 0
while ____i9 < _35(____x7) do
local __v3 = ____x7[____i9 + 1]
while ____i9 < _35(____x8) do
local __v3 = ____x8[____i9 + 1]
local __y2 = f(__v3)
if is63(__y2) then
add(__t1, __y2)
Expand Down Expand Up @@ -287,7 +306,7 @@ function empty63(t)
local ____o6 = t
local ____i12 = nil
for ____i12 in next, ____o6 do
local __x8 = ____o6[____i12]
local __x9 = ____o6[____i12]
return false
end
return true
Expand Down Expand Up @@ -595,7 +614,7 @@ function setenv(k, ...)
local __keys = cut(____id1, 0)
if string63(__k9) then
local __e8 = nil
if __keys.toplevel then
if drop(__keys, "toplevel") then
__e8 = hd(environment)
else
__e8 = last(environment)
Expand Down
28 changes: 20 additions & 8 deletions runtime.l
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,25 @@
(define-global id-literal? (x)
(and (string? x) (= (char x 0) "|")))

(define-global add (l x)
(target js: (do ((get l 'push) x) nil)
lua: ((get table 'insert) l x)))

(define-global drop (l)
(target js: ((get l 'pop))
lua: ((get table 'remove) l)))
(define-global add (l x i)
(if (nil? i)
(target js: ((get l 'push) x)
lua: ((get table 'insert) l x))
(number? i)
(target js: ((get l 'splice) i 0 x)
lua: ((get table 'insert) l (+ i 1) x))
(set (get l i) x))
nil)

(define-global drop (l i)
(if (nil? i)
(target js: ((get l 'pop))
lua: ((get table 'remove) l))
(number? i)
(target js: (at ((get l 'splice) i 1) 0)
lua: ((get table 'remove) l i))
(with x (get l i)
(wipe (get l i)))))

(define-global last (l)
(at l (edge l)))
Expand Down Expand Up @@ -335,7 +347,7 @@

(define-global setenv (k rest: keys)
(when (string? k)
(let (frame (if (get keys 'toplevel)
(let (frame (if (drop keys 'toplevel)
(hd environment)
(last environment))
entry (or (get frame k) (obj)))
Expand Down