Skip to content

Commit 21c6fc3

Browse files
authored
Version 3.6b - 20th June 2021
Fixes technoblogy#51, fixes technoblogy#52
1 parent 8e1bdf8 commit 21c6fc3

File tree

1 file changed

+29
-39
lines changed

1 file changed

+29
-39
lines changed

ulisp-esp.ino

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* uLisp ESP Version 3.6 - www.ulisp.com
2-
David Johnson-Davies - www.technoblogy.com - 4th April 2021
1+
/* uLisp ESP Version 3.6b - www.ulisp.com
2+
David Johnson-Davies - www.technoblogy.com - 20th June 2021
33
44
Licensed under the MIT license: https://opensource.org/licenses/MIT
55
*/
@@ -226,6 +226,7 @@ object *apply (symbol_t name, object *function, object *args, object *env);
226226
char *lookupsymbol (symbol_t name);
227227
char *cstring (object *form, char *buffer, int buflen);
228228
object *edit (object *fun);
229+
void pfstring (PGM_P s, pfun_t pfun);
229230

230231
// Error handling
231232

@@ -353,6 +354,13 @@ object *stream (uint8_t streamtype, uint8_t address) {
353354
return ptr;
354355
}
355356

357+
object *newstring () {
358+
object *ptr = myalloc();
359+
ptr->type = STRING;
360+
ptr->chars = 0;
361+
return ptr;
362+
}
363+
356364
// Garbage collection
357365

358366
void markobject (object *obj) {
@@ -993,21 +1001,20 @@ void indent (uint8_t spaces, char ch, pfun_t pfun) {
9931001
}
9941002

9951003
object *startstring (symbol_t name) {
996-
object *string = myalloc();
997-
string->type = STRING;
998-
GlobalString = NULL;
1004+
object *string = newstring();
1005+
GlobalString = string;
9991006
GlobalStringIndex = 0;
10001007
return string;
10011008
}
10021009

1003-
void buildstring (uint8_t ch, int *chars, object **head) {
1010+
void buildstring (uint8_t ch, object *string, int *chars) {
10041011
static object* tail;
10051012
static uint8_t shift;
10061013
if (*chars == 0) {
10071014
shift = (sizeof(int)-1)*8;
10081015
*chars = ch<<shift;
10091016
object *cell = myalloc();
1010-
if (*head == NULL) *head = cell; else tail->car = cell;
1017+
if (cdr(string) == NULL) cdr(string) = cell; else tail->car = cell;
10111018
cell->car = NULL;
10121019
cell->chars = *chars;
10131020
tail = cell;
@@ -1020,18 +1027,15 @@ void buildstring (uint8_t ch, int *chars, object **head) {
10201027
}
10211028

10221029
object *readstring (uint8_t delim, gfun_t gfun) {
1023-
object *obj = myalloc();
1024-
obj->type = STRING;
1030+
object *obj = newstring();
10251031
int ch = gfun();
10261032
if (ch == -1) return nil;
1027-
object *head = NULL;
10281033
int chars = 0;
10291034
while ((ch != delim) && (ch != -1)) {
10301035
if (ch == '\\') ch = gfun();
1031-
buildstring(ch, &chars, &head);
1036+
buildstring(ch, obj, &chars);
10321037
ch = gfun();
10331038
}
1034-
obj->cdr = head;
10351039
return obj;
10361040
}
10371041

@@ -1073,7 +1077,7 @@ int gstr () {
10731077
}
10741078

10751079
void pstr (char c) {
1076-
buildstring(c, &GlobalStringIndex, &GlobalString);
1080+
buildstring(c, GlobalString, &GlobalStringIndex);
10771081
}
10781082

10791083
char *cstringbuf (object *arg) {
@@ -1100,17 +1104,14 @@ char *cstring (object *form, char *buffer, int buflen) {
11001104
}
11011105

11021106
object *lispstring (char *s) {
1103-
object *obj = myalloc();
1104-
obj->type = STRING;
1107+
object *obj = newstring();
11051108
char ch = *s++;
1106-
object *head = NULL;
11071109
int chars = 0;
11081110
while (ch) {
11091111
if (ch == '\\') ch = *s++;
1110-
buildstring(ch, &chars, &head);
1112+
buildstring(ch, obj, &chars);
11111113
ch = *s++;
11121114
}
1113-
obj->cdr = head;
11141115
return obj;
11151116
}
11161117

@@ -1857,10 +1858,10 @@ object *sp_withoutputtostring (object *args, object *env) {
18571858
object *pair = cons(var, stream(STRINGSTREAM, 0));
18581859
push(pair,env);
18591860
object *string = startstring(WITHOUTPUTTOSTRING);
1861+
push(string, GCStack);
18601862
object *forms = cdr(args);
18611863
eval(tf_progn(forms,env), env);
1862-
string->cdr = GlobalString;
1863-
GlobalString = NULL;
1864+
pop(GCStack);
18641865
return string;
18651866
}
18661867

@@ -1887,7 +1888,7 @@ object *sp_withi2c (object *args, object *env) {
18871888
object *var = first(params);
18881889
int address = checkinteger(WITHI2C, eval(second(params), env));
18891890
params = cddr(params);
1890-
if (address == 0) params = cdr(params); // Ignore port
1891+
if (address == 0 && params != NULL) params = cdr(params); // Ignore port
18911892
int read = 0; // Write
18921893
I2CCount = 0;
18931894
if (params != NULL) {
@@ -3066,8 +3067,7 @@ object *fn_stringfn (object *args, object *env) {
30663067
object *arg = first(args);
30673068
int type = arg->type;
30683069
if (type == STRING) return arg;
3069-
object *obj = myalloc();
3070-
obj->type = STRING;
3070+
object *obj = newstring();
30713071
if (type == CHARACTER) {
30723072
object *cell = myalloc();
30733073
cell->car = NULL;
@@ -3077,14 +3077,12 @@ object *fn_stringfn (object *args, object *env) {
30773077
} else if (type == SYMBOL) {
30783078
char *s = symbolname(arg->name);
30793079
char ch = *s++;
3080-
object *head = NULL;
30813080
int chars = 0;
30823081
while (ch) {
30833082
if (ch == '\\') ch = *s++;
3084-
buildstring(ch, &chars, &head);
3083+
buildstring(ch, arg, &chars);
30853084
ch = *s++;
30863085
}
3087-
obj->cdr = head;
30883086
} else error(STRINGFN, PSTR("can't convert to string"), arg);
30893087
return obj;
30903088
}
@@ -3094,9 +3092,7 @@ object *fn_concatenate (object *args, object *env) {
30943092
object *arg = first(args);
30953093
if (arg->name != STRINGFN) error2(CONCATENATE, PSTR("only supports strings"));
30963094
args = cdr(args);
3097-
object *result = myalloc();
3098-
result->type = STRING;
3099-
object *head = NULL;
3095+
object *result = newstring();
31003096
int chars = 0;
31013097
while (args != NULL) {
31023098
object *obj = first(args);
@@ -3106,14 +3102,13 @@ object *fn_concatenate (object *args, object *env) {
31063102
int quad = obj->chars;
31073103
while (quad != 0) {
31083104
char ch = quad>>((sizeof(int)-1)*8) & 0xFF;
3109-
buildstring(ch, &chars, &head);
3105+
buildstring(ch, result, &chars);
31103106
quad = quad<<8;
31113107
}
31123108
obj = car(obj);
31133109
}
31143110
args = cdr(args);
31153111
}
3116-
result->cdr = head;
31173112
return result;
31183113
}
31193114

@@ -3126,16 +3121,13 @@ object *fn_subseq (object *args, object *env) {
31263121
int end;
31273122
args = cddr(args);
31283123
if (args != NULL) end = checkinteger(SUBSEQ, car(args)); else end = stringlength(arg);
3129-
object *result = myalloc();
3130-
result->type = STRING;
3131-
object *head = NULL;
3124+
object *result = newstring();
31323125
int chars = 0;
31333126
for (int i=start; i<end; i++) {
31343127
char ch = nthchar(arg, i);
31353128
if (ch == 0) error2(SUBSEQ, PSTR("index out of range"));
3136-
buildstring(ch, &chars, &head);
3129+
buildstring(ch, result, &chars);
31373130
}
3138-
result->cdr = head;
31393131
return result;
31403132
}
31413133

@@ -3153,7 +3145,6 @@ object *fn_princtostring (object *args, object *env) {
31533145
object *arg = first(args);
31543146
object *obj = startstring(PRINCTOSTRING);
31553147
prin1object(arg, pstr);
3156-
obj->cdr = GlobalString;
31573148
return obj;
31583149
}
31593150

@@ -3162,7 +3153,6 @@ object *fn_prin1tostring (object *args, object *env) {
31623153
object *arg = first(args);
31633154
object *obj = startstring(PRIN1TOSTRING);
31643155
printobject(arg, pstr);
3165-
obj->cdr = GlobalString;
31663156
return obj;
31673157
}
31683158

@@ -3641,7 +3631,7 @@ object *fn_format (object *args, object *env) {
36413631
}
36423632
n++;
36433633
}
3644-
if (output == nil) { obj->cdr = GlobalString; return obj; }
3634+
if (output == nil) return obj;
36453635
else return nil;
36463636
}
36473637

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy