v0.58: Allow constant exprs for array dimensions

This commit is contained in:
Bobbi Webber-Manners 2018-05-03 01:12:21 -04:00 committed by GitHub
parent 5a4fa67d1b
commit ed032c11ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 58 additions and 20 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
eightball

Binary file not shown.

View File

@ -155,6 +155,7 @@ void linksubs(void);
char compile = 0; /* 0 means interpret, 1 means compile */
char compilingsub = 0; /* 1 when compiling subroutine, 0 otherwise */
char onlyconstants = 0; /* 0 is normal, 1 means only allow const exprs */
char readbuf[255]; /* Buffer for reading from file */
char lnbuf[255]; /* Input text line buffer */
char *txtPtr; /* Pointer to next character to read in lnbuf */
@ -323,7 +324,8 @@ const char unaryops[] = "-+!~*^";
#define ERR_TYPE 120 /* Type error */
#define ERR_DIVZERO 121 /* Divide by zero */
#define ERR_VALUE 122 /* Bad value */
#define ERR_LINK 123 /* Linkage error */
#define ERR_CONST 123 /* Const value reqd */
#define ERR_LINK 124 /* Linkage error */
/*
* Error reporting
@ -401,6 +403,9 @@ void error(unsigned char errcode)
case ERR_VALUE:
print("bad value");
break;
case ERR_CONST:
print("not const");
break;
case ERR_LINK:
print("link");
break;
@ -967,17 +972,23 @@ unsigned char subscript(int *idx)
}
/*
* Parse array dimension. Must be a literal value.
* Parse array dimension. Must be a constant expression.
*/
unsigned char dimension(int *idx)
{
unsigned char oldcompile = compile;
if (expect('[')) {
return 1;
}
if (parseint(idx)) {
error(ERR_NUM);
onlyconstants = 1; /* Only allow constant terms */
compile = 0; /* Actually evaluate */
if (eval(0, idx)) {
onlyconstants = 0;
return 1;
}
onlyconstants = 0;
compile = oldcompile;
if (expect(']')) {
return 1;
}
@ -987,6 +998,7 @@ unsigned char dimension(int *idx)
/*
* Handles a predicate
* Returns 0 on success, 1 on error
* If the global variable onlyconstants is set then only allow constant predicates.
*/
unsigned char P()
{
@ -1008,6 +1020,11 @@ unsigned char P()
if ((*txtPtr == '&') || (isalphach(*txtPtr))) {
if (onlyconstants) {
error(ERR_CONST);
return 1;
}
addressmode = 0;
/*
@ -1049,6 +1066,11 @@ unsigned char P()
} else if (*txtPtr == '(') {
if (onlyconstants) {
error(ERR_CONST);
return 1;
}
/* No taking address of functions thank you! */
if (addressmode) {
error(ERR_VAR);
@ -2610,7 +2632,6 @@ unsigned char assignorcreate(unsigned char mode)
switch (mode) {
case WORD_MODE:
case BYTE_MODE:
/* Array dimensions must be numeric literal */
if (dimension(&i) == 1) {
return RET_ERROR;
}

Binary file not shown.

View File

@ -37,7 +37,7 @@
/* */
/**************************************************************************/
#define VERSIONSTR "0.57"
#define VERSIONSTR "0.58"
void print(char *str);

Binary file not shown.

View File

@ -274,6 +274,22 @@ void execute_instruction()
#endif
switch (memory[pc]) {
/*
* Miscellaneous
*/
case VM_END: /* Terminate execution */
#ifdef __GNUC__
exit(0);
#else
/* Spin forever */
for (delay = 0; delay < 25000; ++delay);
exit(0);
#endif
break;
/*
* Load Immediate
*/
case VM_LDIMM: /* Pushes the following 16 bit word to the evaluation stack */
++evalptr;
CHECKOVERFLOW();
@ -484,21 +500,12 @@ void execute_instruction()
#endif
break;
/*
* Miscellaneous
*/
case VM_ATOR: /* Convert absolute address in X to relative address */
XREG = (XREG - fp - 1) & 0xffff;
break;
case VM_RTOA: /* Convert relative address in X to absolute address */
XREG = (XREG + fp + 1) & 0xffff;
break;
case VM_END: /* Terminate execution */
#ifdef __GNUC__
exit(0);
#else
/* Spin forever */
for (delay = 0; delay < 25000; ++delay);
exit(0);
#endif
break;
/*
* Integer math
*/

View File

@ -70,7 +70,7 @@
enum bytecode {
/**** Miscellaneous ********************************************************/
VM_END, /* Terminate execution */
/**** Load and Store *******************************************************/
/**** Load Immediate *******************************************************/
VM_LDIMM, /* Pushes the following 16 bit word to the evaluation stack */
/* Absolute addressing: */
VM_LDAWORD,/* Replaces X with 16 bit value pointed to by X. */
@ -96,6 +96,7 @@ enum bytecode {
VM_PSHBYTE,/* Push 8 bit value in X onto call stack. Drop X. */
VM_SPTOFP, /* Copy stack pointer to frame pointer. (Enter function scope) */
VM_FPTOSP, /* Copy frame pointer to stack pointer. (Release local vars) */
VM_ATOR, /* Convert absolute address in X to relative address */
VM_RTOA, /* Convert relative address in X to absolute address */
/**** Integer math *********************************************************/
VM_INC, /* X = X+1. */
@ -165,6 +166,7 @@ char *bytecodenames[] = {
"PSHB",
"SPFP",
"FPSP",
"ATOR",
"RTOA",
"INC",
"DEC",

BIN
test.d64

Binary file not shown.

BIN
test.dsk

Binary file not shown.

View File

@ -11,6 +11,7 @@ pr.msg "Word vars:"; pr.nl
word w1=10
word w2=100
word w3=50
w1=10
status=(w1==10)&&(w2==100)&&(w3==50)
call expect(status)
@ -59,7 +60,7 @@ call expect((wpre==0)&&(warr[0]==12)&&(warr[1]==123)&&(warr[2]==12)&&(warr[9]==1
'------------------
pr.msg "Byte arrays:"; pr.nl
byte bpre=0
byte barr[10]=12
byte barr[2*5]=12
byte bpost=0
call expect((&barr[2]-&barr[1])==1)
@ -185,6 +186,9 @@ call expect(barr[7]==$34)
call c1()
call expect(iw==555)
call noret()
call expect(iw==9876)
pr.msg " Recursive:"; pr.nl
call recurse1(5, &iw)
call expect(iw==120)
@ -355,6 +359,10 @@ sub c3()
return 0
endsub
sub noret()
iw = 9876
endsub
sub sqr(word x)
return x*x
endsub