Subversion Repositories Krakow BASIC

Rev

Rev 7 | Rev 9 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 8
Line 1... Line 1...
1
/* $Id: basic.c 7 2024-09-04 00:18:36Z nishi $ */
1
/* $Id: basic.c 8 2024-09-04 11:49:13Z nishi $ */
2
 
2
 
3
/* Krakow BASIC - Multi-platform simple BASIC */
3
/* Krakow BASIC - Multi-platform simple BASIC */
4
 
4
 
5
#if defined(PLATFORM_SHIROI)
5
#if defined(PLATFORM_SHIROI)
6
 
6
 
Line 293... Line 293...
293
#endif
293
#endif
294
 
294
 
295
unsigned char basicbuffer[BUFFER_SIZE];
295
unsigned char basicbuffer[BUFFER_SIZE];
296
char linebuf[LINE_BUFFER_SIZE];
296
char linebuf[LINE_BUFFER_SIZE];
297
 
297
 
-
 
298
int hexnum(char c) {
-
 
299
	if('0' <= c && c <= '9') {
-
 
300
		return c - '0';
-
 
301
	} else if('A' <= c && c <= 'F') {
-
 
302
		return c - 'A' + 10;
-
 
303
	} else if('a' <= c && c <= 'f') {
-
 
304
		return c - 'a' + 10;
-
 
305
	}
-
 
306
	return 0;
-
 
307
}
-
 
308
 
298
int pexpr(char* expr, char* buffer, int* number) {
309
int pexpr(char* expr, char* buffer, int* number) {
299
	char ownbuf[128];
310
	char ownbuf[128];
300
	int i;
311
	int i;
301
	int start = 0;
312
	int start = 0;
302
	int br = 0;
313
	int br = 0;
303
	int result = 0;
314
	int result = 0;
304
	int stack[32];
315
	int stack[32];
305
	int sp = 0;
316
	int sp = 0;
306
	char put = 0;
317
	char put = 0;
-
 
318
	char hex = 0;
307
	for(i = 0; expr[i] != 0; i++) ownbuf[i] = expr[i];
319
	for(i = 0; expr[i] != 0; i++) ownbuf[i] = expr[i];
308
	ownbuf[i] = 0;
320
	ownbuf[i] = 0;
309
	for(i = 0; i < 32; i++) stack[i] = 0;
321
	for(i = 0; i < 32; i++) stack[i] = 0;
310
	for(i = 0;; i++) {
322
	for(i = 0;; i++) {
311
		if(ownbuf[i] == 0) {
323
		if(ownbuf[i] == 0) {
312
			break;
324
			break;
313
		} else if('0' <= ownbuf[i] && ownbuf[i] <= '9') {
325
		} else if(ownbuf[i] == '&' && put == 0) {
-
 
326
			put = 1;
-
 
327
			hex = 1;
-
 
328
		} else if(('0' <= ownbuf[i] && ownbuf[i] <= '9') || (hex ? ('A' <= ownbuf[i] && ownbuf[i] <= 'F') : 0) || (hex ? ('a' <= ownbuf[i] && ownbuf[i] <= 'f') : 0)) {
314
			stack[sp] *= 10;
329
			stack[sp] *= hex ? 16 : 10;
-
 
330
			if(hex == 1) {
-
 
331
				stack[sp] += hexnum(ownbuf[i]);
-
 
332
			} else {
315
			stack[sp] += ownbuf[i] - '0';
333
				stack[sp] += ownbuf[i] - '0';
-
 
334
			}
316
			put = 1;
335
			put = 1;
-
 
336
		} else if(ownbuf[i] == 'R') {
-
 
337
			put = 0;
-
 
338
			hex = 0;
-
 
339
			if(sp < 1) {
-
 
340
				return -1;
-
 
341
			} else {
-
 
342
				int top = stack[--sp];
-
 
343
				stack[sp++] = (int)*(unsigned char*)top;
-
 
344
			}
-
 
345
			stack[sp] = 0;
317
		} else if(ownbuf[i] == '+' || ownbuf[i] == '-' || ownbuf[i] == '*' || ownbuf[i] == '/') {
346
		} else if(ownbuf[i] == '+' || ownbuf[i] == '-' || ownbuf[i] == '*' || ownbuf[i] == '/') {
318
			put = 0;
347
			put = 0;
-
 
348
			hex = 0;
319
			if(sp < 2) {
349
			if(sp < 2) {
320
				return -1;
350
				return -1;
321
			} else {
351
			} else {
322
				int top = stack[--sp];
352
				int top = stack[--sp];
323
				int bottom = stack[--sp];
353
				int bottom = stack[--sp];
Line 458... Line 488...
458
				putnum(linenum);
488
				putnum(linenum);
459
			}
489
			}
460
			putstr(NEWLINE);
490
			putstr(NEWLINE);
461
			return 1;
491
			return 1;
462
		}
492
		}
-
 
493
	} else if(strcaseequ(rcmd, "POKE")) {
-
 
494
		int argc = 0;
-
 
495
		char* farg = 0;
-
 
496
		char* sarg = 0;
-
 
497
		int addr, data, ret0, ret1;
-
 
498
		if(arg[0] != 0) argc++;
-
 
499
		for(i = 0; arg[i] != 0; i++) {
-
 
500
			if(arg[i] == ',') {
-
 
501
				arg[i] = 0;
-
 
502
				farg = arg;
-
 
503
				sarg = arg + i + 1;
-
 
504
				for(; *sarg != 0 && (*sarg == '\t' || *sarg == ' '); sarg++)
-
 
505
					;
-
 
506
				argc++;
-
 
507
			}
-
 
508
		}
-
 
509
		if(argc != 2) {
-
 
510
			putstr("! Invalid argument");
-
 
511
			if(linenum != -1) {
-
 
512
				putstr(" in line ");
-
 
513
				putnum(linenum);
-
 
514
			}
-
 
515
			putstr(NEWLINE);
-
 
516
			return 1;
-
 
517
		}
-
 
518
		addr = 0;
-
 
519
		data = 0;
-
 
520
		ret0 = pexpr(farg, 0, &addr);
-
 
521
		ret1 = pexpr(sarg, 0, &data);
-
 
522
		if(ret0 == 0) {
-
 
523
			putstr("! Invalid argument");
-
 
524
			if(linenum != -1) {
-
 
525
				putstr(" in line ");
-
 
526
				putnum(linenum);
-
 
527
			}
-
 
528
			putstr(NEWLINE);
-
 
529
			return 1;
-
 
530
		} else if(ret0 == -1) {
-
 
531
			putstr("! Syntax error");
-
 
532
			if(linenum != -1) {
-
 
533
				putstr(" in line ");
-
 
534
				putnum(linenum);
-
 
535
			}
-
 
536
			putstr(NEWLINE);
-
 
537
			return 1;
-
 
538
		}
-
 
539
		if(ret1 == 1) {
-
 
540
			unsigned char* a = (unsigned char*)addr;
-
 
541
			*a = data;
-
 
542
		} else if(ret1 == 0) {
-
 
543
			putstr("! Invalid argument");
-
 
544
			if(linenum != -1) {
-
 
545
				putstr(" in line ");
-
 
546
				putnum(linenum);
-
 
547
			}
-
 
548
			putstr(NEWLINE);
-
 
549
			return 1;
-
 
550
		} else if(ret1 == -1) {
-
 
551
			putstr("! Syntax error");
-
 
552
			if(linenum != -1) {
-
 
553
				putstr(" in line ");
-
 
554
				putnum(linenum);
-
 
555
			}
-
 
556
			putstr(NEWLINE);
-
 
557
			return 1;
-
 
558
		}
463
	} else if(num == 0 && strcaseequ(rcmd, "LIST")) {
559
	} else if(num == 0 && strcaseequ(rcmd, "LIST")) {
464
		int addr = BUFFER_SIZE - 1;
560
		int addr = BUFFER_SIZE - 1;
465
		int saddr = 0;
561
		int saddr = 0;
466
		int lbuf[LINES];
562
		int lbuf[LINES];
467
		int shift[LINES];
563
		int shift[LINES];
Line 711... Line 807...
711
	cursor();
807
	cursor();
712
	linebuf[0] = 0;
808
	linebuf[0] = 0;
713
	lineind = 0;
809
	lineind = 0;
714
	while(1) {
810
	while(1) {
715
		char c;
811
		char c;
716
#if defined(PLATFORM_C64) || defined(PLATFORM_A800XL)
812
#if defined(PLATFORM_C64) || defined(PLATFORM_A800XL) || defined(PLATFORM_APPLE2)
717
		c = oggetch(1);
813
		c = oggetch(1);
718
#else
814
#else
719
		c = agetch();
815
		c = agetch();
720
#endif
816
#endif
-
 
817
		if(c != 0) {
721
		if(c != 0) killcursor();
818
			killcursor();
-
 
819
		}
722
		if(c == 1) {
820
		if(c == 1) {
723
			putstr("Break");
821
			putstr("Break");
724
			putstr(NEWLINE);
822
			putstr(NEWLINE);
725
			lineind = 0;
823
			lineind = 0;
726
		} else if(c == '\n') {
824
		} else if(c == '\n') {