-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCommands.c
More file actions
8575 lines (8068 loc) · 243 KB
/
Commands.c
File metadata and controls
8575 lines (8068 loc) · 243 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************************************
PicoMite MMBasic
* @file commands.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
/**
* @file Commands.c
* @author Geoff Graham, Peter Mather
* @brief Source for standard MMBasic commands
*/
/**
* @cond
* The following section will be excluded from the documentation.
*/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#include "hardware/flash.h"
#include "hardware/dma.h"
#include "hardware/structs/watchdog.h"
#include "re.h"
#ifndef USBKEYBOARD
#include "class/cdc/cdc_device.h"
#endif
#ifdef PICOMITE
#include "pico/multicore.h"
#endif
#ifndef PICOMITEWEB
#include "Turtle.h"
#endif
#define overlap (VRes % (FontTable[gui_font >> 4][1] * (gui_font & 0b1111)) ? 0 : 1)
/* Stride-aware array access macros for struct member arrays */
#define STRIDE_FLOAT(ptr, idx, stride) (*(MMFLOAT *)((char *)(ptr) + (idx) * (stride)))
#define STRIDE_INT(ptr, idx, stride) (*(long long int *)((char *)(ptr) + (idx) * (stride)))
#include <math.h>
void flist(int, int, int);
// void clearprog(void);
char *KeyInterrupt = NULL;
unsigned char *SaveNextDataLine = NULL;
void execute_one_command(unsigned char *p);
void ListNewLine(int *ListCnt, int all);
int printWrappedText(const char *text, int screenWidth, int listcnt, int all);
char MMErrMsg[MAXERRMSG]; // the error message
volatile bool Keycomplete = false;
int keyselect = 0;
extern volatile unsigned int ScrewUpTimer;
int SaveNextData = 0;
struct sa_data datastore[MAXRESTORE];
int restorepointer = 0;
uint64_t g_flag = 0;
const uint8_t pinlist[] = { // this is a Basic program to print out the status of all the pins
1, 132, 128, 95, 113, 37, 0,
1, 153, 128, 95, 113, 37, 144, 48, 32, 204, 32, 241, 109, 97, 120, 32, 103, 112, 41, 0,
1, 168, 128, 34, 71, 80, 34, 130, 186, 95, 113, 37, 41, 44, 32, 241, 112, 105, 110, 110, 111, 32, 34, 71, 80, 34, 130, 186,
95, 113, 37, 41, 41, 44, 241, 112, 105, 110, 32, 241, 112, 105, 110, 110, 111, 32, 34, 71, 80, 34, 130, 186, 95, 113, 37, 41, 41, 41, 0,
1, 166, 128, 0,
1, 147, 128, 95, 113, 37, 0, 0};
const uint8_t i2clist[] = { // this is a Basic program to print out the I2C devices connected to the SYSTEM I2C pins
1, 132, 128, 105, 110, 116, 101, 103, 101, 114, 32, 95, 97, 100, 0,
1, 132, 128, 105, 110, 116, 101, 103, 101, 114, 32, 120, 95, 44, 121, 95, 0,
1, 168, 128, 34, 32, 72, 69, 88, 32, 32, 48, 32, 32, 49, 32, 32, 50, 32, 32, 51, 32, 32, 52, 32, 32, 53, 32, 32, 54, 32, 32, 55, 32, 32,
56, 32, 32, 57, 32, 32, 65, 32, 32, 66, 32, 32, 67, 32, 32, 68, 32, 32, 69, 32, 32, 70, 34, 0,
1, 153, 128, 121, 95, 32, 144, 32, 48, 32, 204, 32, 55, 0,
1, 168, 128, 34, 32, 34, 59, 32, 164, 121, 95, 44, 32, 49, 41, 59, 32, 34, 48, 58, 32, 34, 59, 0,
1, 153, 128, 120, 95, 32, 144, 32, 48, 32, 204, 32, 49, 53, 0,
1, 161, 128, 95, 97, 100, 32, 144, 32, 121, 95, 32, 133, 32, 49, 54, 32, 130, 32, 120, 95, 0,
1, 158, 128, 241, 83, 89, 83, 84, 69, 77, 32, 73, 50, 67, 41, 144, 34, 73, 50, 67, 34, 32, 203, 32, 228, 128, 99, 104,
101, 99, 107, 32, 95, 97, 100, 32, 199, 32, 229, 128, 32, 99, 104, 101, 99, 107, 32, 95, 97, 100, 0,
1, 158, 128, 243, 68, 41, 32, 144, 32, 48, 32, 203, 0,
1, 158, 128, 95, 97, 100, 32, 144, 32, 48, 32, 203, 32, 168, 128, 34, 45, 45, 32, 34, 59, 0,
1, 158, 128, 95, 97, 100, 32, 143, 32, 48, 32, 203, 32, 168, 128, 164, 95, 97, 100, 44, 32, 50, 41, 59, 34, 32, 34, 59, 0,
1, 139, 128, 0, 1, 168, 128, 34, 45, 45, 32, 34, 59, 0, 1, 143, 128, 0, 1, 166, 128, 120, 95, 0,
1, 168, 128, 0, 1, 166, 128, 121, 95, 0, 1, 147, 128, 120, 95, 44, 121, 95, 0,
1, 147, 128, 95, 97, 100, 0, 0};
// stack to keep track of nested FOR/NEXT loops
struct s_forstack g_forstack[MAXFORLOOPS + 1];
int g_forindex;
// stack to keep track of nested DO/LOOP loops
struct s_dostack g_dostack[MAXDOLOOPS];
int g_doindex; // counts the number of nested DO/LOOP loops
// stack to keep track of GOSUBs, SUBs and FUNCTIONs
unsigned char *gosubstack[MAXGOSUB];
unsigned char *errorstack[MAXGOSUB];
int gosubindex;
unsigned char g_DimUsed = false; // used to catch OPTION BASE after DIM has been used
#ifdef STRUCTENABLED
// Structure type definition table
struct s_structdef *g_structtbl[MAX_STRUCT_TYPES]; // Array of pointers, allocated per-type
int g_structcnt = 0; // Number of defined structure types
int g_StructArg = -1; // Struct index for pending DIM AS structtype (-1 if none)
int g_StructMemberType = 0; // Type of struct member being accessed (0 if not a member access)
int g_StructMemberOffset = 0; // Offset of member within struct (for EXTRACT/INSERT/SORT)
int g_StructMemberSize = 0; // Size of the member (for EXTRACT/INSERT/SORT)
int g_ExprStructType = -1; // Struct type index from expression evaluation (-1 if not a struct)
#endif
int TraceOn; // used to track the state of TRON/TROFF
unsigned char *TraceBuff[TRACE_BUFF_SIZE];
int TraceBuffIndex; // used for listing the contents of the trace buffer
int OptionErrorSkip; // how to handle an error
int MMerrno; // the error number
unsigned char cmdlinebuff[STRINGSIZE];
const unsigned int CaseOption = 0xffffffff; // used to store the case of the listed output
void __not_in_flash_func(cmd_null)(void)
{
// do nothing (this is just a placeholder for commands that have no action)
}
/** @endcond */
/**
* This command increments an integer or a float or concatenates two strings
* @param a the integer, float or string to be changed
* @param b OPTIONAL for integers and floats - defaults to 1. Otherwise the amount to increment the number or the string to concatenate
*/
#if LOWRAM
void MIPS16 cmd_inc(void)
{
#else
void MIPS16 __not_in_flash_func(cmd_inc)(void)
{
#endif
unsigned char *p, *q;
int vtype;
getcsargs(&cmdline, 3);
if (argc == 1)
{
p = findvar(argv[0], V_FIND);
if (g_vartbl[g_VarIndex].type & T_CONST)
StandardError(22);
vtype = TypeMask(g_vartbl[g_VarIndex].type);
#ifdef STRUCTENABLED
if (g_StructMemberType != 0)
vtype = TypeMask(g_StructMemberType);
#endif
if (vtype & T_STR)
StandardError(6); // sanity check
if (vtype & T_NBR)
(*(MMFLOAT *)p) = (*(MMFLOAT *)p) + 1.0;
else if (vtype & T_INT)
*(long long int *)p = *(long long int *)p + 1;
else
SyntaxError();
;
}
else
{
p = findvar(argv[0], V_FIND);
if (g_vartbl[g_VarIndex].type & T_CONST)
StandardError(22);
vtype = TypeMask(g_vartbl[g_VarIndex].type);
#ifdef STRUCTENABLED
if (g_StructMemberType != 0)
vtype = TypeMask(g_StructMemberType);
#endif
if (vtype & T_STR)
{
int size = g_vartbl[g_VarIndex].size;
#ifdef STRUCTENABLED
if (g_StructMemberType & T_STR)
size = g_StructMemberSize;
#endif
q = getstring(argv[2]);
if (*p + *q > size)
error("String too long");
Mstrcat(p, q);
}
else if (vtype & T_NBR)
{
(*(MMFLOAT *)p) = (*(MMFLOAT *)p) + getnumber(argv[2]);
}
else if (vtype & T_INT)
{
*(long long int *)p = *(long long int *)p + getinteger(argv[2]);
}
else
SyntaxError();
}
}
// the PRINT command
void cmd_print(void)
{
unsigned char *s, *p;
unsigned char *ss;
MMFLOAT f;
long long int i64;
int i, t, fnbr;
int docrlf; // this is used to suppress the cr/lf if needed
#define ADV_CHARPOS(ch) \
do \
{ \
if ((ch) == '\r' || (ch) == '\n') \
{ \
charpos = 1; \
} \
else if ((ch) == '\t') \
{ \
int nexttab = (((charpos - 1) / 14) + 1) * 14 + 1; \
charpos = nexttab; \
} \
else \
{ \
charpos++; \
} \
} while (0)
#define ADV_CHUNK(ptr, len) \
do \
{ \
for (int adv_i = 0; adv_i < (len); adv_i++) \
ADV_CHARPOS((ptr)[adv_i]); \
} while (0)
getargs(&cmdline, (MAX_ARG_COUNT * 2) - 1, (unsigned char *)";,"); // this is a macro and must be the first executable stmt
// s = 0; *s = 56; // for testing the exception handler
docrlf = true;
int charpos = MMCharPos; // Track current column for TAB() function
if (argc > 0 && *argv[0] == '#')
{ // check if the first arg is a file number
argv[0]++;
if ((*argv[0] == 'G') || (*argv[0] == 'g'))
{
argv[0]++;
if (!((*argv[0] == 'P') || (*argv[0] == 'p')))
SyntaxError();
;
argv[0]++;
if (!((*argv[0] == 'S') || (*argv[0] == 's')))
SyntaxError();
;
if (!GPSchannel)
error("GPS not activated");
if (argc != 3)
error("Only a single string parameter allowed");
p = argv[2];
t = T_NOTYPE;
p = evaluate(p, &f, &i64, &s, &t, true); // get the value and type of the argument
ss = (unsigned char *)s;
if (!(t & T_STR))
error("Only a single string parameter allowed");
int i, xsum = 0;
if (ss[1] != '$' || ss[ss[0]] != '*')
error("GPS command must start with dollar and end with star");
for (i = 1; i <= ss[0]; i++)
{
SerialPutchar(GPSchannel, s[i]);
if (s[i] == '$')
xsum = 0;
if (s[i] != '*')
xsum ^= s[i];
}
i = xsum / 16;
i = i + '0';
if (i > '9')
i = i - '0' + 'A';
SerialPutchar(GPSchannel, i);
i = xsum % 16;
i = i + '0';
if (i > '9')
i = i - '0' + 'A';
SerialPutchar(GPSchannel, i);
SerialPutchar(GPSchannel, 13);
SerialPutchar(GPSchannel, 10);
return;
}
else
{
fnbr = getinteger(argv[0]); // get the number
i = 1;
if (argc >= 2 && *argv[1] == ',')
i = 2; // and set the next argument to be looked at
}
}
else
{
fnbr = 0; // no file number so default to the standard output
i = 0;
}
// Get initial buffer for accumulating output
unsigned char *outbuf = GetTempMemory(STRINGSIZE);
unsigned char *bufptr = outbuf;
int bufsize = STRINGSIZE;
int used = 0;
for (; i < argc; i++)
{ // step through the arguments
if (*argv[i] == ',')
{
// Check if we need more space for a tab
if (used + 1 >= bufsize)
{
// Need to expand buffer
unsigned char *newbuf = GetTempMemory(bufsize + STRINGSIZE);
memcpy(newbuf, outbuf, used);
ClearSpecificTempMemory(outbuf);
outbuf = newbuf;
bufptr = outbuf + used;
bufsize += STRINGSIZE;
}
*bufptr++ = '\t';
used++;
ADV_CHARPOS('\t');
docrlf = false; // a trailing comma should suppress CR/LF
}
else if (*argv[i] == ';')
{
docrlf = false; // other than suppress cr/lf do nothing for a semicolon
}
else
{ // we have a normal expression
p = argv[i];
while (*p)
{
t = T_NOTYPE;
MMCharPos = charpos; // Ensure TAB() sees the current column before evaluation
p = evaluate(p, &f, &i64, &s, &t, true); // get the value and type of the argument
if (t & T_NBR)
{
*inpbuf = ' '; // preload a space
FloatToStr((char *)inpbuf + ((f >= 0) ? 1 : 0), f, 0, STR_AUTO_PRECISION, (unsigned char)' '); // if positive output a space instead of the sign
int len = strlen((char *)inpbuf);
// Check if we need more space
while (used + len >= bufsize)
{
unsigned char *newbuf = GetTempMemory(bufsize + STRINGSIZE);
memcpy(newbuf, outbuf, used);
ClearSpecificTempMemory(outbuf);
outbuf = newbuf;
bufptr = outbuf + used;
bufsize += STRINGSIZE;
}
strcpy((char *)bufptr, (char *)inpbuf);
bufptr += len;
used += len;
ADV_CHUNK(inpbuf, len);
}
else if (t & T_INT)
{
*inpbuf = ' '; // preload a space
IntToStr((char *)inpbuf + ((i64 >= 0) ? 1 : 0), i64, 10); // if positive output a space instead of the sign
int len = strlen((char *)inpbuf);
// Check if we need more space
while (used + len >= bufsize)
{
unsigned char *newbuf = GetTempMemory(bufsize + STRINGSIZE);
memcpy(newbuf, outbuf, used);
ClearSpecificTempMemory(outbuf);
outbuf = newbuf;
bufptr = outbuf + used;
bufsize += STRINGSIZE;
}
strcpy((char *)bufptr, (char *)inpbuf);
bufptr += len;
used += len;
ADV_CHUNK(inpbuf, len);
}
else if (t & T_STR)
{
// s is already a MMBasic string, need to extract the C string part
unsigned char *cstr = s + 1; // Skip length byte
int len = *s; // Get length from MMBasic string
// Check if we need more space
while (used + len >= bufsize)
{
unsigned char *newbuf = GetTempMemory(bufsize + STRINGSIZE);
memcpy(newbuf, outbuf, used);
ClearSpecificTempMemory(outbuf);
outbuf = newbuf;
bufptr = outbuf + used;
bufsize += STRINGSIZE;
}
memcpy(bufptr, cstr, len);
bufptr += len;
used += len;
ADV_CHUNK(cstr, len);
}
else
error("Attempt to print reserved word");
}
docrlf = true;
}
}
if (docrlf)
{
// Check if we need more space for cr/lf
if (used + 2 >= bufsize)
{
unsigned char *newbuf = GetTempMemory(bufsize + STRINGSIZE);
memcpy(newbuf, outbuf, used);
ClearSpecificTempMemory(outbuf);
outbuf = newbuf;
bufptr = outbuf + used;
bufsize += STRINGSIZE;
}
*bufptr++ = '\r';
*bufptr++ = '\n';
used += 2;
ADV_CHARPOS('\r');
ADV_CHARPOS('\n');
}
// Null terminate the C string
*bufptr = '\0';
// Output the buffer - don't null terminate, use explicit lengths
if (used <= 255)
{
// Simple case: fits in a single MMBasic string
unsigned char *mmstr = GetTempMemory(256);
mmstr[0] = used;
memcpy(mmstr + 1, outbuf, used);
MMfputs(mmstr, fnbr);
}
else
{
// Need to output in chunks of maximum 255 bytes
int offset = 0;
unsigned char *mmstr = GetTempMemory(256);
while (offset < used)
{
int chunklen = (used - offset > 255) ? 255 : (used - offset);
mmstr[0] = chunklen;
memcpy(mmstr + 1, outbuf + offset, chunklen);
MMfputs(mmstr, fnbr);
offset += chunklen;
}
}
if (PrintPixelMode != 0)
SSPrintString("\033[m");
PrintPixelMode = 0;
MMCharPos = charpos; // Final column state after buffered output
#undef ADV_CHUNK
#undef ADV_CHARPOS
}
void cmd_arrayset(void)
{
array_set(cmdline);
}
void array_set(unsigned char *tp)
{
MMFLOAT f;
long long int i64;
unsigned char *s;
#ifdef rp2350
int dims[MAXDIM] = {0};
#else
short dims[MAXDIM] = {0};
#endif
int i, t, copy, card1 = 1;
unsigned char size = 0;
MMFLOAT *a1float = NULL;
int64_t *a1int = NULL;
unsigned char *a1str = NULL;
int s1; // stride for numeric arrays
getcsargs(&tp, 3);
if (!(argc == 3))
StandardError(2);
findvar(argv[2], V_FIND | V_EMPTY_OK | V_NOFIND_ERR);
t = g_vartbl[g_VarIndex].type;
evaluate(argv[0], &f, &i64, &s, &t, false);
if (t & T_STR)
{
card1 = parsestringarray(argv[2], &a1str, 2, 0, dims, true, &size);
copy = (int)size + 1;
memset(a1str, 0, copy * card1);
if (*s)
{
for (i = 0; i < card1; i++)
{
Mstrcpy(&a1str[i * copy], s);
}
}
}
else
{
card1 = parsenumberarray(argv[2], &a1float, &a1int, 2, 0, dims, true, &s1);
if (t & T_STR)
SyntaxError();
;
if (a1float != NULL)
{
for (i = 0; i < card1; i++)
STRIDE_FLOAT(a1float, i, s1) = ((t & T_INT) ? (MMFLOAT)i64 : f);
}
else
{
for (i = 0; i < card1; i++)
STRIDE_INT(a1int, i, s1) = ((t & T_INT) ? i64 : FloatToInt64(f));
}
}
}
void cmd_add(void)
{
array_add(cmdline);
}
void array_add(unsigned char *tp)
{
MMFLOAT f;
long long int i64;
unsigned char *s;
#ifdef rp2350
int dims[MAXDIM] = {0};
#else
short dims[MAXDIM] = {0};
#endif
int i, t, card1 = 1, card2 = 1;
MMFLOAT *a1float = NULL, *a2float = NULL, scale;
int64_t *a1int = NULL, *a2int = NULL;
unsigned char *a1str = NULL, *a2str = NULL;
int s1, s2;
getcsargs(&tp, 5);
if (!(argc == 5))
StandardError(2);
findvar(argv[0], V_FIND | V_EMPTY_OK | V_NOFIND_ERR);
t = g_vartbl[g_VarIndex].type;
if (t & T_STR)
{
unsigned char size = 0, size2 = 0;
unsigned char *toadd;
card1 = parsestringarray(argv[0], &a1str, 1, 0, dims, false, &size);
evaluate(argv[2], &f, &i64, &s, &t, false);
if (!(t & T_STR))
SyntaxError();
;
toadd = getstring(argv[2]);
card2 = parsestringarray(argv[4], &a2str, 3, 0, dims, true, &size2);
if (card1 != card2)
StandardError(16);
unsigned char *buff = GetTempStrMemory(); // this will last for the life of the command
int copy = size + 1;
int copy2 = size2 + 1;
for (i = 0; i < card1; i++)
{
unsigned char *sarg1 = a1str + i * copy;
unsigned char *sarg2 = a2str + i * copy2;
if (*sarg1 + *toadd > size2)
error("String too long");
Mstrcpy(buff, sarg1);
Mstrcat(buff, toadd);
Mstrcpy(sarg2, buff);
}
}
else
{
card1 = parsenumberarray(argv[0], &a1float, &a1int, 1, 0, dims, false, &s1);
evaluate(argv[2], &f, &i64, &s, &t, false);
if (t & T_STR)
SyntaxError();
;
scale = getnumber(argv[2]);
card2 = parsenumberarray(argv[4], &a2float, &a2int, 3, 0, dims, true, &s2);
if (card1 != card2)
StandardError(16);
if (scale != 0.0)
{
if (a2float != NULL && a1float != NULL)
{
for (i = 0; i < card1; i++)
STRIDE_FLOAT(a2float, i, s2) = ((t & T_INT) ? (MMFLOAT)i64 : f) + STRIDE_FLOAT(a1float, i, s1);
}
else if (a2float != NULL && a1float == NULL)
{
for (i = 0; i < card1; i++)
STRIDE_FLOAT(a2float, i, s2) = ((t & T_INT) ? (MMFLOAT)i64 : f) + ((MMFLOAT)STRIDE_INT(a1int, i, s1));
}
else if (a2float == NULL && a1float != NULL)
{
for (i = 0; i < card1; i++)
STRIDE_INT(a2int, i, s2) = FloatToInt64(((t & T_INT) ? i64 : FloatToInt64(f)) + STRIDE_FLOAT(a1float, i, s1));
}
else
{
for (i = 0; i < card1; i++)
STRIDE_INT(a2int, i, s2) = ((t & T_INT) ? i64 : FloatToInt64(f)) + STRIDE_INT(a1int, i, s1);
}
}
else
{
if (a2float != NULL && a1float != NULL)
{
for (i = 0; i < card1; i++)
STRIDE_FLOAT(a2float, i, s2) = STRIDE_FLOAT(a1float, i, s1);
}
else if (a2float != NULL && a1float == NULL)
{
for (i = 0; i < card1; i++)
STRIDE_FLOAT(a2float, i, s2) = ((MMFLOAT)STRIDE_INT(a1int, i, s1));
}
else if (a2float == NULL && a1float != NULL)
{
for (i = 0; i < card1; i++)
STRIDE_INT(a2int, i, s2) = FloatToInt64(STRIDE_FLOAT(a1float, i, s1));
}
else
{
for (i = 0; i < card1; i++)
STRIDE_INT(a2int, i, s2) = STRIDE_INT(a1int, i, s1);
}
}
}
}
void cmd_insert(void)
{
array_insert(cmdline);
}
void array_insert(unsigned char *tp)
{
int i, j, t, start, increment, dim[MAXDIM], pos[MAXDIM], off[MAXDIM], dimcount = 0, target = -1;
int64_t *a1int = NULL, *a2int = NULL;
MMFLOAT *afloat = NULL;
unsigned char *a1str = NULL, *a2str = NULL;
unsigned char size = 0, size2 = 0;
#ifdef rp2350
int dims[MAXDIM] = {0};
#else
short dims[MAXDIM] = {0};
#endif
getcsargs(&tp, 15);
if (argc < 7)
StandardError(2);
findvar(argv[0], V_FIND | V_EMPTY_OK | V_NOFIND_ERR);
t = g_vartbl[g_VarIndex].type;
if (t & T_STR)
{
parsestringarray(argv[0], &a1str, 1, 0, dims, false, &size);
}
else
{
parsenumberarray(argv[0], &afloat, &a1int, 1, 0, dims, false, NULL);
if (!a1int)
a1int = (int64_t *)afloat;
}
if (dims[1] <= 0)
error("Argument 1 must be a 2D or more array");
for (i = 0; i < MAXDIM; i++)
{
if (dims[i] - g_OptionBase > 0)
{
dimcount++;
dim[i] = dims[i] - g_OptionBase;
}
else
dim[i] = 0;
}
if (((argc - 1) / 2 - 1) != dimcount)
StandardError(2);
for (i = 0; i < dimcount; i++)
{
if (*argv[i * 2 + 2])
pos[i] = getint(argv[i * 2 + 2], g_OptionBase, dim[i] + g_OptionBase) - g_OptionBase;
else
{
if (target != -1)
error("Only one index can be omitted");
target = i;
pos[i] = 1;
}
}
if (t & T_STR)
{
parsestringarray(argv[i * 2 + 2], &a2str, i + 1, 1, dims, true, &size2);
}
else
{
parsenumberarray(argv[i * 2 + 2], &afloat, &a2int, i + 1, 1, dims, true, NULL);
if (!a2int)
a2int = (int64_t *)afloat;
}
if (target == -1)
return;
if (dim[target] + g_OptionBase != dims[0])
error("Size mismatch between insert and target array");
if (size != size2)
error("String arrays differ in string length");
i = dimcount - 1;
while (i >= 0)
{
off[i] = 1;
for (j = 0; j < i; j++)
off[i] *= (dim[j] + 1);
i--;
}
start = 1;
for (i = 0; i < dimcount; i++)
{
start += (pos[i] * off[i]);
}
start--;
increment = off[target];
start -= increment;
if (t & T_STR)
{
int copy = (int)size + 1;
for (i = 0; i <= dim[target]; i++)
{
unsigned char *p = a2str + i * copy;
unsigned char *q = &a1str[(start + i * increment) * copy];
memcpy(q, p, copy);
}
}
else
{
for (i = 0; i <= dim[target]; i++)
a1int[start + i * increment] = *a2int++;
}
return;
}
void cmd_slice(void)
{
array_slice(cmdline);
}
void array_slice(unsigned char *tp)
{
int i, j, t, start, increment, dim[MAXDIM], pos[MAXDIM], off[MAXDIM], dimcount = 0, target = -1, toarray = 0;
int64_t *a1int = NULL, *a2int = NULL;
MMFLOAT *afloat = NULL;
unsigned char *a1str = NULL, *a2str = NULL;
unsigned char size = 0, size2 = 0;
#ifdef rp2350
int dims[MAXDIM] = {0};
#else
short dims[MAXDIM] = {0};
#endif
getcsargs(&tp, 15);
if (argc < 7)
StandardError(2);
findvar(argv[0], V_FIND | V_EMPTY_OK | V_NOFIND_ERR);
t = g_vartbl[g_VarIndex].type;
if (t & T_STR)
{
parsestringarray(argv[0], &a1str, 1, 0, dims, false, &size);
}
else
{
parsenumberarray(argv[0], &afloat, &a1int, 1, 0, dims, false, NULL);
if (!a1int)
a1int = (int64_t *)afloat;
}
if (dims[1] <= 0)
error("Argument 1 must be a 2D or more array");
for (i = 0; i < MAXDIM; i++)
{
if (dims[i] - g_OptionBase > 0)
{
dimcount++;
dim[i] = dims[i] - g_OptionBase;
}
else
dim[i] = 0;
}
if (((argc - 1) / 2 - 1) != dimcount)
StandardError(2);
for (i = 0; i < dimcount; i++)
{
if (*argv[i * 2 + 2])
pos[i] = getint(argv[i * 2 + 2], g_OptionBase, dim[i] + g_OptionBase) - g_OptionBase;
else
{
if (target != -1)
error("Only one index can be omitted");
target = i;
pos[i] = 1;
}
}
if (t & T_STR)
{
toarray = parsestringarray(argv[i * 2 + 2], &a2str, i + 1, 1, dims, true, &size2) - 1;
}
else
{
toarray = parsenumberarray(argv[i * 2 + 2], &afloat, &a2int, i + 1, 1, dims, true, NULL) - 1;
if (!a2int)
a2int = (int64_t *)afloat;
}
if (dim[target] != toarray)
error("Size mismatch between slice and target array");
if (size != size2)
error("String arrays differ in string length");
i = dimcount - 1;
while (i >= 0)
{
off[i] = 1;
for (j = 0; j < i; j++)
off[i] *= (dim[j] + 1);
i--;
}
start = 1;
for (i = 0; i < dimcount; i++)
{
start += (pos[i] * off[i]);
}
start--;
increment = off[target];
start -= increment;
if (t & T_STR)
{
int copy = (int)size + 1; // allow for the length character of the string
for (i = 0; i <= dim[target]; i++)
{
unsigned char *p = a2str + i * copy;
unsigned char *q = &a1str[(start + i * increment) * copy];
memcpy(p, q, copy);
}
}
else
{
for (i = 0; i <= dim[target]; i++)
*a2int++ = a1int[start + i * increment];
}
return;
}
// the LET command
// because the LET is implied (ie, line does not have a recognisable command)
// it ends up as the place where mistyped commands are discovered. This is why
// the error message is "Unknown command"
#ifdef rp2350
void __not_in_flash_func(cmd_let)(void)
#else
void MIPS16 __not_in_flash_func(cmd_let)(void)
#endif
{
int t, size;
MMFLOAT f;
long long int i64;
unsigned char *s;
unsigned char *p1, *p2;
int vartype; // effective type (may differ for struct members)
p1 = cmdline;
// search through the line looking for the equals sign
while (*p1 && tokenfunction(*p1) != op_equal)
p1++;
if (!*p1)
StandardError(36);
// check that we have a straight forward variable
p2 = skipvar(cmdline, false);
skipspace(p2);
if (p1 != p2)
SyntaxError();
;
// create the variable and get the length if it is a string
p2 = findvar(cmdline, V_FIND);
size = g_vartbl[g_VarIndex].size;
if (g_vartbl[g_VarIndex].type & T_CONST)
StandardError(22);
#ifdef STRUCTENABLED
// For struct member access, use the member type instead of the base variable type
if (g_StructMemberType != 0)
{
vartype = g_StructMemberType;
// For string members, use the size set by ResolveStructMember
if (vartype & T_STR)
{
size = g_StructMemberSize;
}
}
else
{
vartype = g_vartbl[g_VarIndex].type;
}
#else
vartype = g_vartbl[g_VarIndex].type;
#endif
// step over the equals sign, evaluate the rest of the command and save in the variable
p1++;
if (vartype & T_STR)
{
t = T_STR;
p1 = evaluate(p1, &f, &i64, &s, &t, false);
if (*s > size)
error("String too long");
Mstrcpy(p2, s);
}
else if (vartype & T_NBR)
{
t = T_NBR;
p1 = evaluate(p1, &f, &i64, &s, &t, false);
if (t & T_NBR)
(*(MMFLOAT *)p2) = f;
else
(*(MMFLOAT *)p2) = (MMFLOAT)i64;
}
#ifdef STRUCTENABLED
else if (vartype & T_STRUCT)
{
// Struct assignment - evaluate the right side and copy struct data
// Save destination info BEFORE evaluate changes g_VarIndex
int dest_struct_idx = (int)g_vartbl[g_VarIndex].size;
int dest_struct_size = g_structtbl[dest_struct_idx]->total_size;
t = T_NOTYPE; // Let evaluate determine the actual type
p1 = evaluate(p1, &f, &i64, &s, &t, false);
// Check that RHS is a struct
if (!(t & T_STRUCT))
{
error("Expected a structure value");
}
// Validate struct types match (g_ExprStructType set by getvalue or function return)
if (g_ExprStructType >= 0 && g_ExprStructType != dest_struct_idx)
{
error("Structure types must match");
}
if (s != NULL)
{
if (dest_struct_idx >= 0 && dest_struct_idx < g_structcnt)
{
memcpy(p2, s, dest_struct_size);
}
else
{
error("Invalid struct type");
}
}
else
{
error("No struct value");
}
}
#endif
else
{
t = T_INT;
p1 = evaluate(p1, &f, &i64, &s, &t, false);
if (t & T_INT)
(*(long long int *)p2) = i64;
else
(*(long long int *)p2) = FloatToInt64(f);
}
checkend(p1);
}
/**
* @cond
* The following section will be excluded from the documentation.
*/
int MIPS16 as_strcmpi(const char *s1, const char *s2)
{
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
unsigned char c1, c2;
if (p1 == p2)
return 0;
do
{
c1 = tolower(*p1++);
c2 = tolower(*p2++);
if (c1 == '\0')
break;
} while (c1 == c2);
return c1 - c2;
}