Skip to content

Commit 57d7dcb

Browse files
authored
Version 2.7 - 20th May 2019
1 parent a4a146a commit 57d7dcb

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

ulisp-esp.ino

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* uLisp ESP Version 2.6a - www.ulisp.com
2-
David Johnson-Davies - www.technoblogy.com - 19th April 2019
1+
/* uLisp ESP Version 2.7 - www.ulisp.com
2+
David Johnson-Davies - www.technoblogy.com - 20th May 2019
33
44
Licensed under the MIT license: https://opensource.org/licenses/MIT
55
*/
@@ -518,20 +518,18 @@ void autorunimage () {
518518
File file = SD.open("/ULISP.IMG");
519519
if (!file) error(PSTR("Problem autorunning from SD card"));
520520
object *autorun = (object *)SDReadInt(file);
521-
object *nullenv = NULL;
522521
file.close();
523522
if (autorun != NULL) {
524523
loadimage(NULL);
525-
apply(autorun, NULL, &nullenv);
524+
apply(autorun, NULL, NULL);
526525
}
527526
#else
528-
object *nullenv = NULL;
529527
EEPROM.begin(EEPROMSIZE);
530528
int addr = 0;
531529
object *autorun = (object *)EpromReadInt(&addr);
532530
if (autorun != NULL && (unsigned int)autorun != 0xFFFF) {
533531
loadimage(NULL);
534-
apply(autorun, NULL, &nullenv);
532+
apply(autorun, NULL, NULL);
535533
}
536534
#endif
537535
}
@@ -865,15 +863,6 @@ object *findvalue (object *var, object *env) {
865863
return pair;
866864
}
867865

868-
object *findtwin (object *var, object *env) {
869-
while (env != NULL) {
870-
object *pair = car(env);
871-
if (pair != NULL && car(pair) == var) return pair;
872-
env = cdr(env);
873-
}
874-
return NULL;
875-
}
876-
877866
// Handling closures
878867

879868
object *closure (int tc, object *fname, object *state, object *function, object *args, object **env) {
@@ -886,10 +875,14 @@ object *closure (int tc, object *fname, object *state, object *function, object
886875
}
887876
object *params = first(function);
888877
function = cdr(function);
889-
// Push state if not already in env
878+
// Dropframe
879+
if (tc) {
880+
while (*env != NULL && car(*env) != NULL) pop(*env);
881+
} else push(nil, *env);
882+
// Push state
890883
while (state != NULL) {
891884
object *pair = first(state);
892-
if (findtwin(car(pair), *env) == NULL) push(pair, *env);
885+
push(pair, *env);
893886
state = cdr(state);
894887
}
895888
// Add arguments to environment
@@ -918,9 +911,7 @@ object *closure (int tc, object *fname, object *state, object *function, object
918911
else error2(fname, PSTR("has too few arguments"));
919912
} else { value = first(args); args = cdr(args); }
920913
}
921-
object *pair = findtwin(var, *env);
922-
if (tc && (pair != NULL)) cdr(pair) = value;
923-
else push(cons(var,value), *env);
914+
push(cons(var,value), *env);
924915
if (trace) { pserial(' '); printobject(value, pserial); }
925916
}
926917
params = cdr(params);
@@ -931,24 +922,24 @@ object *closure (int tc, object *fname, object *state, object *function, object
931922
return tf_progn(function, *env);
932923
}
933924

934-
object *apply (object *function, object *args, object **env) {
925+
object *apply (object *function, object *args, object *env) {
935926
if (symbolp(function)) {
936927
symbol_t name = function->name;
937928
int nargs = listlength(args);
938929
if (name >= ENDFUNCTIONS) error2(function, PSTR("is not valid here"));
939930
if (nargs<lookupmin(name)) error2(function, PSTR("has too few arguments"));
940931
if (nargs>lookupmax(name)) error2(function, PSTR("has too many arguments"));
941-
return ((fn_ptr_type)lookupfn(name))(args, *env);
932+
return ((fn_ptr_type)lookupfn(name))(args, env);
942933
}
943934
if (listp(function) && issymbol(car(function), LAMBDA)) {
944935
function = cdr(function);
945-
object *result = closure(0, NULL, NULL, function, args, env);
946-
return eval(result, *env);
936+
object *result = closure(0, NULL, NULL, function, args, &env);
937+
return eval(result, env);
947938
}
948939
if (listp(function) && issymbol(car(function), CLOSURE)) {
949940
function = cdr(function);
950-
object *result = closure(0, NULL, car(function), cdr(function), args, env);
951-
return eval(result, *env);
941+
object *result = closure(0, NULL, car(function), cdr(function), args, &env);
942+
return eval(result, env);
952943
}
953944
error2(function, PSTR("is an illegal function"));
954945
return NULL;
@@ -1812,11 +1803,11 @@ object *fn_apply (object *args, object *env) {
18121803
}
18131804
if (!listp(car(last))) error3(APPLY, PSTR("last argument is not a list"));
18141805
cdr(previous) = car(last);
1815-
return apply(first(args), cdr(args), &env);
1806+
return apply(first(args), cdr(args), env);
18161807
}
18171808

18181809
object *fn_funcall (object *args, object *env) {
1819-
return apply(first(args), cdr(args), &env);
1810+
return apply(first(args), cdr(args), env);
18201811
}
18211812

18221813
object *fn_append (object *args, object *env) {
@@ -1849,13 +1840,13 @@ object *fn_mapc (object *args, object *env) {
18491840
while (list1 != NULL && list2 != NULL) {
18501841
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
18511842
if (improperp(list2)) error3(name, PSTR("third argument is not a proper list"));
1852-
apply(function, cons(car(list1),cons(car(list2),NULL)), &env);
1843+
apply(function, cons(car(list1),cons(car(list2),NULL)), env);
18531844
list1 = cdr(list1); list2 = cdr(list2);
18541845
}
18551846
} else {
18561847
while (list1 != NULL) {
18571848
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
1858-
apply(function, cons(car(list1),NULL), &env);
1849+
apply(function, cons(car(list1),NULL), env);
18591850
list1 = cdr(list1);
18601851
}
18611852
}
@@ -1875,7 +1866,7 @@ object *fn_mapcar (object *args, object *env) {
18751866
while (list1 != NULL && list2 != NULL) {
18761867
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
18771868
if (improperp(list2)) error3(name, PSTR("third argument is not a proper list"));
1878-
object *result = apply(function, cons(car(list1), cons(car(list2),NULL)), &env);
1869+
object *result = apply(function, cons(car(list1), cons(car(list2),NULL)), env);
18791870
object *obj = cons(result,NULL);
18801871
cdr(tail) = obj;
18811872
tail = obj;
@@ -1884,7 +1875,7 @@ object *fn_mapcar (object *args, object *env) {
18841875
} else if (list1 != NULL) {
18851876
while (list1 != NULL) {
18861877
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
1887-
object *result = apply(function, cons(car(list1),NULL), &env);
1878+
object *result = apply(function, cons(car(list1),NULL), env);
18881879
object *obj = cons(result,NULL);
18891880
cdr(tail) = obj;
18901881
tail = obj;
@@ -1908,7 +1899,7 @@ object *fn_mapcan (object *args, object *env) {
19081899
while (list1 != NULL && list2 != NULL) {
19091900
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
19101901
if (improperp(list2)) error3(name, PSTR("third argument is not a proper list"));
1911-
object *result = apply(function, cons(car(list1), cons(car(list2),NULL)), &env);
1902+
object *result = apply(function, cons(car(list1), cons(car(list2),NULL)), env);
19121903
while (result != NULL && (unsigned int)result >= PAIR) {
19131904
cdr(tail) = result;
19141905
tail = result;
@@ -1920,7 +1911,7 @@ object *fn_mapcan (object *args, object *env) {
19201911
} else if (list1 != NULL) {
19211912
while (list1 != NULL) {
19221913
if (improperp(list1)) error3(name, PSTR("second argument is not a proper list"));
1923-
object *result = apply(function, cons(car(list1),NULL), &env);
1914+
object *result = apply(function, cons(car(list1),NULL), env);
19241915
while (result != NULL && (unsigned int)result >= PAIR) {
19251916
cdr(tail) = result;
19261917
tail = result;
@@ -2523,7 +2514,7 @@ object *fn_sort (object *args, object *env) {
25232514
while (go != ptr) {
25242515
car(compare) = car(cdr(ptr));
25252516
car(cdr(compare)) = car(cdr(go));
2526-
if (apply(predicate, compare, &env)) break;
2517+
if (apply(predicate, compare, env)) break;
25272518
go = cdr(go);
25282519
}
25292520
if (go != ptr) {
@@ -3710,11 +3701,7 @@ object *eval (object *form, object *env) {
37103701
object *envcopy = NULL;
37113702
while (env != NULL) {
37123703
object *pair = first(env);
3713-
if (pair != NULL) {
3714-
object *val = cdr(pair);
3715-
if (integerp(val)) val = number(val->integer);
3716-
push(cons(car(pair), val), envcopy);
3717-
}
3704+
if (pair != NULL) push(pair, envcopy);
37183705
env = cdr(env);
37193706
}
37203707
return cons(symbol(CLOSURE), cons(envcopy,args));
@@ -4145,7 +4132,7 @@ void setup () {
41454132
initworkspace();
41464133
initenv();
41474134
initsleep();
4148-
pfstring(PSTR("uLisp 2.6 "), pserial); pln(pserial);
4135+
pfstring(PSTR("uLisp 2.7 "), pserial); pln(pserial);
41494136
}
41504137

41514138
// Read/Evaluate/Print loop

0 commit comments

Comments
 (0)