Skip to content

Commit a46baae

Browse files
authored
Version 4.3a - 25th September 2022
Fixes technoblogy#57
1 parent 2ec6cdd commit a46baae

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

ulisp-esp.ino

+36-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* uLisp ESP Version 4.3 - www.ulisp.com
2-
David Johnson-Davies - www.technoblogy.com - 15th September 2022
1+
/* uLisp ESP Version 4.3a - www.ulisp.com
2+
David Johnson-Davies - www.technoblogy.com - 25th September 2022
33
44
Licensed under the MIT license: https://opensource.org/licenses/MIT
55
*/
@@ -173,6 +173,7 @@ Adafruit_SSD1306 tft(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
173173
#define tstflag(x) (Flags & 1<<(x))
174174

175175
#define issp(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
176+
#define isbr(x) (x == ')' || x == '(' || x == '"' || x == '#')
176177
#define longsymbolp(x) (((x)->name & 0x03) == 0)
177178
#define twist(x) ((uint32_t)((x)<<2) | (((x) & 0xC0000000)>>30))
178179
#define untwist(x) (((x)>>2 & 0x3FFFFFFF) | ((x) & 0x03)<<30)
@@ -634,11 +635,14 @@ unsigned int saveimage (object *arg) {
634635
SD.begin(SDCARD_SS_PIN);
635636
File file;
636637
if (stringp(arg)) {
637-
file = SD.open(MakeFilename(arg), FILE_WRITE);
638+
char buffer[BUFFERSIZE];
639+
file = SD.open(MakeFilename(arg, buffer), FILE_WRITE);
640+
if (!file) error2(SAVEIMAGE, PSTR("problem saving to SD card or invalid filename"));
638641
arg = NULL;
639-
} else if (arg == NULL || listp(arg)) file = SD.open("/ULISP.IMG", FILE_WRITE);
640-
else error(SAVEIMAGE, PSTR("illegal argument"), arg);
641-
if (!file) error2(SAVEIMAGE, PSTR("problem saving to SD card"));
642+
} else if (arg == NULL || listp(arg)) {
643+
file = SD.open("/ULISP.IMG", FILE_WRITE);
644+
if (!file) error2(SAVEIMAGE, PSTR("problem saving to SD card"));
645+
} else error(SAVEIMAGE, invalidarg, arg);
642646
SDWriteInt(file, (uintptr_t)arg);
643647
SDWriteInt(file, imagesize);
644648
SDWriteInt(file, (uintptr_t)GlobalEnv);
@@ -703,10 +707,14 @@ unsigned int loadimage (object *arg) {
703707
#if defined(sdcardsupport)
704708
SD.begin(SDCARD_SS_PIN);
705709
File file;
706-
if (stringp(arg)) file = SD.open(MakeFilename(arg));
707-
else if (arg == NULL) file = SD.open("/ULISP.IMG");
708-
else error(LOADIMAGE, PSTR("illegal argument"), arg);
709-
if (!file) error2(LOADIMAGE, PSTR("problem loading from SD card"));
710+
if (stringp(arg)) {
711+
char buffer[BUFFERSIZE];
712+
file = SD.open(MakeFilename(arg, buffer));
713+
if (!file) error2(LOADIMAGE, PSTR("problem loading from SD card or invalid filename"));
714+
} else if (arg == NULL) {
715+
file = SD.open("/ULISP.IMG");
716+
if (!file) error2(LOADIMAGE, PSTR("problem loading from SD card"));
717+
} else error(LOADIMAGE, invalidarg, arg);
710718
SDReadInt(file);
711719
unsigned int imagesize = SDReadInt(file);
712720
GlobalEnv = (object *)SDReadInt(file);
@@ -1200,7 +1208,7 @@ object *readbitarray (gfun_t gfun) {
12001208
char ch = gfun();
12011209
object *head = NULL;
12021210
object *tail = NULL;
1203-
while (!issp(ch) && ch != ')' && ch != '(') {
1211+
while (!issp(ch) && !isbr(ch)) {
12041212
if (ch != '0' && ch != '1') error2(NIL, PSTR("illegal character in bit array"));
12051213
object *cell = cons(number(ch - '0'), NULL);
12061214
if (head == NULL) head = cell;
@@ -1210,7 +1218,7 @@ object *readbitarray (gfun_t gfun) {
12101218
}
12111219
LastChar = ch;
12121220
int size = listlength(NIL, head);
1213-
object *array = makearray(NIL, cons(number(size), NULL), 0, true);
1221+
object *array = makearray(MAKEARRAY, cons(number(size), NULL), number(0), true);
12141222
size = (size + sizeof(int)*8 - 1)/(sizeof(int)*8);
12151223
int index = 0;
12161224
while (head != NULL) {
@@ -1419,7 +1427,7 @@ char *cstring (builtin_t name, object *form, char *buffer, int buflen) {
14191427
return buffer;
14201428
}
14211429

1422-
IPAddress ipstring (builtin_t name, object *form) {
1430+
uint32_t ipstring (builtin_t name, object *form) {
14231431
form = cdr(checkstring(name, form));
14241432
int p = 0;
14251433
union { uint32_t ipaddress; uint8_t ipbytes[4]; } ;
@@ -2379,9 +2387,12 @@ object *sp_withspi (object *args, object *env) {
23792387
object *sp_withsdcard (object *args, object *env) {
23802388
#if defined(sdcardsupport)
23812389
object *params = first(args);
2390+
if (params == NULL) error2(WITHSDCARD, nostream);
23822391
object *var = first(params);
2383-
object *filename = eval(second(params), env);
2384-
params = cddr(params);
2392+
params = cdr(params);
2393+
if (params == NULL) error2(WITHSDCARD, PSTR("no filename specified"));
2394+
object *filename = eval(first(params), env);
2395+
params = cdr(params);
23852396
SD.begin();
23862397
int mode = 0;
23872398
if (params != NULL && first(params) != NULL) mode = checkinteger(WITHSDCARD, first(params));
@@ -3395,7 +3406,9 @@ object *fn_readfromstring (object *args, object *env) {
33953406
object *arg = checkstring(READFROMSTRING, first(args));
33963407
GlobalString = arg;
33973408
GlobalStringIndex = 0;
3398-
return read(gstr);
3409+
object *val = read(gstr);
3410+
LastChar = 0;
3411+
return val;
33993412
}
34003413

34013414
object *fn_princtostring (object *args, object *env) {
@@ -3822,7 +3835,7 @@ object *fn_format (object *args, object *env) {
38223835
int len = stringlength(formatstr);
38233836
uint8_t n = 0, width = 0, w, bra = 0;
38243837
char pad = ' ';
3825-
bool tilde = false, mute = false, comma, quote;
3838+
bool tilde = false, mute = false, comma = false, quote = false;
38263839
while (n < len) {
38273840
char ch = nthchar(formatstr, n);
38283841
char ch2 = ch & ~0x20; // force to upper case
@@ -4904,7 +4917,7 @@ const char doc191[] PROGMEM = "(pprint item [str])\n"
49044917
"If str is specified it prints to the specified stream. It returns no value.";
49054918
const char doc192[] PROGMEM = "(pprintall [str])\n"
49064919
"Pretty-prints the definition of every function and variable defined in the uLisp workspace.\n"
4907-
"Is str is specified it prints to the specified stream. It returns no value.";
4920+
"If str is specified it prints to the specified stream. It returns no value.";
49084921
const char doc193[] PROGMEM = "(format output controlstring arguments*)\n"
49094922
"Outputs its arguments formatted according to the format directives in controlstring.";
49104923
const char doc194[] PROGMEM = "(require 'symbol)\n"
@@ -5802,7 +5815,7 @@ object *nextitem (gfun_t gfun) {
58025815
char ch2 = ch & ~0x20; // force to upper case
58035816
if (ch == '\\') { // Character
58045817
base = 0; ch = gfun();
5805-
if (issp(ch) || ch == ')' || ch == '(') return character(ch);
5818+
if (issp(ch) || isbr(ch)) return character(ch);
58065819
else LastChar = ch;
58075820
} else if (ch == '|') {
58085821
do { while (gfun() != '|'); }
@@ -5831,7 +5844,7 @@ object *nextitem (gfun_t gfun) {
58315844
buffer[2] = '\0'; buffer[3] = '\0'; buffer[4] = '\0'; buffer[5] = '\0'; // In case symbol is < 5 letters
58325845
float divisor = 10.0;
58335846

5834-
while(!issp(ch) && ch != ')' && ch != '(' && index < bufmax) {
5847+
while(!issp(ch) && !isbr(ch) && index < bufmax) {
58355848
buffer[index++] = ch;
58365849
if (base == 10 && ch == '.' && !isexponent) {
58375850
isfloat = true;
@@ -5859,7 +5872,7 @@ object *nextitem (gfun_t gfun) {
58595872
}
58605873

58615874
buffer[index] = '\0';
5862-
if (ch == ')' || ch == '(') LastChar = ch;
5875+
if (isbr(ch)) LastChar = ch;
58635876
if (isfloat && valid == 1) return makefloat(fresult * sign * pow(10, exponent * esign));
58645877
else if (valid == 1) {
58655878
if (base == 10 && result > ((unsigned int)INT_MAX+(1-sign)/2))
@@ -5942,7 +5955,7 @@ void setup () {
59425955
initenv();
59435956
initsleep();
59445957
initgfx();
5945-
pfstring(PSTR("uLisp 4.3 "), pserial); pln(pserial);
5958+
pfstring(PSTR("uLisp 4.3a "), pserial); pln(pserial);
59465959
}
59475960

59485961
// Read/Evaluate/Print loop
@@ -5994,4 +6007,4 @@ void loop () {
59946007
#endif
59956008
client.stop();
59966009
repl(NULL);
5997-
}
6010+
}

0 commit comments

Comments
 (0)