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
3
3
4
4
Licensed under the MIT license: https://opensource.org/licenses/MIT
5
5
*/
@@ -518,20 +518,18 @@ void autorunimage () {
518
518
File file = SD.open (" /ULISP.IMG" );
519
519
if (!file) error (PSTR (" Problem autorunning from SD card" ));
520
520
object *autorun = (object *)SDReadInt (file);
521
- object *nullenv = NULL ;
522
521
file.close ();
523
522
if (autorun != NULL ) {
524
523
loadimage (NULL );
525
- apply (autorun, NULL , &nullenv );
524
+ apply (autorun, NULL , NULL );
526
525
}
527
526
#else
528
- object *nullenv = NULL ;
529
527
EEPROM.begin (EEPROMSIZE);
530
528
int addr = 0 ;
531
529
object *autorun = (object *)EpromReadInt (&addr);
532
530
if (autorun != NULL && (unsigned int )autorun != 0xFFFF ) {
533
531
loadimage (NULL );
534
- apply (autorun, NULL , &nullenv );
532
+ apply (autorun, NULL , NULL );
535
533
}
536
534
#endif
537
535
}
@@ -865,15 +863,6 @@ object *findvalue (object *var, object *env) {
865
863
return pair;
866
864
}
867
865
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
-
877
866
// Handling closures
878
867
879
868
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
886
875
}
887
876
object *params = first (function);
888
877
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
890
883
while (state != NULL ) {
891
884
object *pair = first (state);
892
- if ( findtwin ( car (pair), *env) == NULL ) push (pair, *env);
885
+ push (pair, *env);
893
886
state = cdr (state);
894
887
}
895
888
// Add arguments to environment
@@ -918,9 +911,7 @@ object *closure (int tc, object *fname, object *state, object *function, object
918
911
else error2 (fname, PSTR (" has too few arguments" ));
919
912
} else { value = first (args); args = cdr (args); }
920
913
}
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);
924
915
if (trace) { pserial (' ' ); printobject (value, pserial); }
925
916
}
926
917
params = cdr (params);
@@ -931,24 +922,24 @@ object *closure (int tc, object *fname, object *state, object *function, object
931
922
return tf_progn (function, *env);
932
923
}
933
924
934
- object *apply (object *function, object *args, object ** env) {
925
+ object *apply (object *function, object *args, object *env) {
935
926
if (symbolp (function)) {
936
927
symbol_t name = function->name ;
937
928
int nargs = listlength (args);
938
929
if (name >= ENDFUNCTIONS) error2 (function, PSTR (" is not valid here" ));
939
930
if (nargs<lookupmin (name)) error2 (function, PSTR (" has too few arguments" ));
940
931
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);
942
933
}
943
934
if (listp (function) && issymbol (car (function), LAMBDA)) {
944
935
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);
947
938
}
948
939
if (listp (function) && issymbol (car (function), CLOSURE)) {
949
940
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);
952
943
}
953
944
error2 (function, PSTR (" is an illegal function" ));
954
945
return NULL ;
@@ -1812,11 +1803,11 @@ object *fn_apply (object *args, object *env) {
1812
1803
}
1813
1804
if (!listp (car (last))) error3 (APPLY, PSTR (" last argument is not a list" ));
1814
1805
cdr (previous) = car (last);
1815
- return apply (first (args), cdr (args), & env);
1806
+ return apply (first (args), cdr (args), env);
1816
1807
}
1817
1808
1818
1809
object *fn_funcall (object *args, object *env) {
1819
- return apply (first (args), cdr (args), & env);
1810
+ return apply (first (args), cdr (args), env);
1820
1811
}
1821
1812
1822
1813
object *fn_append (object *args, object *env) {
@@ -1849,13 +1840,13 @@ object *fn_mapc (object *args, object *env) {
1849
1840
while (list1 != NULL && list2 != NULL ) {
1850
1841
if (improperp (list1)) error3 (name, PSTR (" second argument is not a proper list" ));
1851
1842
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);
1853
1844
list1 = cdr (list1); list2 = cdr (list2);
1854
1845
}
1855
1846
} else {
1856
1847
while (list1 != NULL ) {
1857
1848
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);
1859
1850
list1 = cdr (list1);
1860
1851
}
1861
1852
}
@@ -1875,7 +1866,7 @@ object *fn_mapcar (object *args, object *env) {
1875
1866
while (list1 != NULL && list2 != NULL ) {
1876
1867
if (improperp (list1)) error3 (name, PSTR (" second argument is not a proper list" ));
1877
1868
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);
1879
1870
object *obj = cons (result,NULL );
1880
1871
cdr (tail) = obj;
1881
1872
tail = obj;
@@ -1884,7 +1875,7 @@ object *fn_mapcar (object *args, object *env) {
1884
1875
} else if (list1 != NULL ) {
1885
1876
while (list1 != NULL ) {
1886
1877
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);
1888
1879
object *obj = cons (result,NULL );
1889
1880
cdr (tail) = obj;
1890
1881
tail = obj;
@@ -1908,7 +1899,7 @@ object *fn_mapcan (object *args, object *env) {
1908
1899
while (list1 != NULL && list2 != NULL ) {
1909
1900
if (improperp (list1)) error3 (name, PSTR (" second argument is not a proper list" ));
1910
1901
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);
1912
1903
while (result != NULL && (unsigned int )result >= PAIR) {
1913
1904
cdr (tail) = result;
1914
1905
tail = result;
@@ -1920,7 +1911,7 @@ object *fn_mapcan (object *args, object *env) {
1920
1911
} else if (list1 != NULL ) {
1921
1912
while (list1 != NULL ) {
1922
1913
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);
1924
1915
while (result != NULL && (unsigned int )result >= PAIR) {
1925
1916
cdr (tail) = result;
1926
1917
tail = result;
@@ -2523,7 +2514,7 @@ object *fn_sort (object *args, object *env) {
2523
2514
while (go != ptr) {
2524
2515
car (compare) = car (cdr (ptr));
2525
2516
car (cdr (compare)) = car (cdr (go));
2526
- if (apply (predicate, compare, & env)) break ;
2517
+ if (apply (predicate, compare, env)) break ;
2527
2518
go = cdr (go);
2528
2519
}
2529
2520
if (go != ptr) {
@@ -3710,11 +3701,7 @@ object *eval (object *form, object *env) {
3710
3701
object *envcopy = NULL ;
3711
3702
while (env != NULL ) {
3712
3703
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);
3718
3705
env = cdr (env);
3719
3706
}
3720
3707
return cons (symbol (CLOSURE), cons (envcopy,args));
@@ -4145,7 +4132,7 @@ void setup () {
4145
4132
initworkspace ();
4146
4133
initenv ();
4147
4134
initsleep ();
4148
- pfstring (PSTR (" uLisp 2.6 " ), pserial); pln (pserial);
4135
+ pfstring (PSTR (" uLisp 2.7 " ), pserial); pln (pserial);
4149
4136
}
4150
4137
4151
4138
// Read/Evaluate/Print loop
0 commit comments