1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-06-08 21:29:30 +00:00

Allowed subscripted array element in function X return value

This commit is contained in:
Curtis F Kaylor 2018-07-21 22:23:10 -04:00
parent eb90c48f0d
commit 7b15ce426f
6 changed files with 40 additions and 21 deletions

View File

@ -475,7 +475,7 @@ When assigning to an array element, the index may be a literal, constant,
or simple variable.
When using an array element in an expression or pop statement, the index
may be any expression.
may be any expression.
Examples:
z = r[i]; //Store the value from element i of array r into variable z
@ -483,10 +483,17 @@ Examples:
z = d[15-i]; //Store the value element 15-i of array d into variable z
c = t[getc()]; //Get a character, translate using array t and store in c
Note: After a subscripted array reference, the 6502 X register will contain
the value of the index (unless the register Y was used as the index, in
which X register is not changed). When evaluating an expression used as an
index, the Accumulator is tempororaly pushed onto the 6502 stack.
Note: Register references may be used as array indexes within expressions,
but the contents of each registers may change with each term evaluation.
Using a constant, literal, or the X or Y registers as an array index will
generates the same amount of code as a simple variable reference and leave
both the X and Y registers unchanged. Using the A register as an index will
generate one extra byte of code, while using a simple variable as index
will generate 1 to 2 extra bytes of code. In either case, the index value
will be left in the X register. When an expression is used as an index,
one extra byte of stack space is used, and an additional three bytes of
code is generated. The X register will contain the result of the expression
and the Y register will be left in an unknown state.
STRUCTS
@ -690,19 +697,19 @@ PLURAL ASSIGNMENTS
C02 allows a function to return up to three values by specifying multiple
variables, separated by commas, to the left of the assignment operator (=).
The first and second variables to be assigned may be either simple variables
or subscripted array elements. The third variable, if used, may only be a
simple variable. Registers are not allowed in plural assignments.
All three variables to be assigned may be either simple variables or
subscripted array elements. Registers are not allowed in plural assignments.
Examples:
row, col = scnpos(); //Get current screen position
cr, mn, mx = cpmnmx(a, b); //Compare two values, return min and max
x[i], y[i] = rotate(x[i],y[i],d); //Rotate x[i] and y[i] by d degrees
x[i], y[i], z[i] = get3d(i); //Generate 3d coordinate for index i
Note: When compiled, a plural assignment generates an STX for the third
assignment (if specified), an STY for the second assignment and an STA for
the first assignment.
the first assignment. Using a subscripted array element for the third
assignment generates an overhead of three bytes of machine code.
GOTO STATEMENT

View File

@ -27,8 +27,6 @@ void addprm(char* prmtr) {
void addfnc(void) {
if (infunc) ERROR("Nested Function Definitions Not Allowed\n", 0, EXIT_FAILURE)
expect('(');
infunc = TRUE; //Set Inside Function Definition Flag
DEBUG("Set infunc to %d\n", infunc)
strcpy(fncnam, word); //Save Function Name
prmcnt = 0; //Initialze Number of Parameters
skpspc(); //Skip Spaces
@ -43,6 +41,8 @@ void addfnc(void) {
}
expect(')');
if (look(';')) return; //Forward Definition
infunc = TRUE; //Set Inside Function Definition Flag
DEBUG("Set infunc to %d\n", infunc)
setlbl(fncnam); //Set Function Entry Point
if (prmcnt > 0) asmlin("STA", prmtra); //Store First Parameter
if (prmcnt > 1) asmlin("STY", prmtry); //Store Second Parameter

View File

@ -77,13 +77,20 @@ void prcasn(char trmntr) {
else prsxpr(trmntr); //Parse Expression
DEBUG("Processing X assignment variable '%s'\n", xsnvar)
if (xsnvar[0]) {
asmlin("STX", xsnvar);
if (strlen(xsnidx)) { //Process X variable Index
asmlin("PHA", ""); //Save Accumulator
asmlin("TXA", ""); //Transfer Return Value to Accumulator
prcidx(xsnivt, xsnvar, xsnidx); //Process Index
asmlin("STA", xsnvar); //Store Return Value
asmlin("PLA", ""); //Restore Accumulator
}
else asmlin("STX", xsnvar); //Store Return Value
xsnvar[0] = 0;
}
DEBUG("Processing Y assignment variable '%s'\n", ysnvar)
if (ysnvar[0]) {
if (strlen(ysnidx)) prcidx(ysnivt, ysnvar, ysnidx);
asmlin("STY", ysnvar);
if (strlen(ysnidx)) prcidx(ysnivt, ysnvar, ysnidx); //Process Index
asmlin("STY", ysnvar); //Store Return Value
ysnvar[0] = 0;
}
DEBUG("Checking if '%s' is a register\n", asnvar)
@ -91,8 +98,8 @@ void prcasn(char trmntr) {
else if (strcmp(asnvar, "Y")==0) asmlin("TAY", "");
else if (strcmp(asnvar, "A")==0) return;
DEBUG("Processing assignment variable '%s'\n", asnvar)
if (strlen(asnidx)) prcidx(asnivt, asnvar, asnidx);
asmlin("STA", asnvar);
if (strlen(asnidx)) prcidx(asnivt, asnvar, asnidx); //Process Index
asmlin("STA", asnvar); //Store Return Value
}
/* Parse and Return Array Index and Type */
@ -134,7 +141,9 @@ void prcvar(char trmntr) {
prsvar(FALSE); //get variable name
strcpy(xsnvar, word);
DEBUG("Set STX variable to %s\n", xsnvar)
if (valtyp == ARRAY) ERROR("Array element not allowed in third assignment\n", 0, EXIT_FAILURE)
//if (valtyp == ARRAY) ERROR("Array element not allowed in third assignment\n", 0, EXIT_FAILURE)
if (valtyp == ARRAY) xsnivt = getidx(xsnidx); //Get Array Index and Type
else xsnidx[0] = 0;
}
}
prcasn(trmntr);

View File

@ -7,9 +7,11 @@ int asntyp; //Assigned Variable Type
char asnidx[VARLEN+1] ; //Assigned Variable Index
int asnivt; //Assigned Index Variable Type
char ysnvar[VARLEN+1]; //Assigned Y Variable Name
char ysnidx[VARLEN+1] ; //Assigned Variable Index
int ysnivt; //Assigned Index Variable Type
char ysnidx[VARLEN+1] ; //Assigned Y Variable Index
int ysnivt; //Assigned Y Index Variable Type
char xsnvar[VARLEN+1]; //Assigned X Variable Name
char xsnidx[VARLEN+1] ; //Assigned X Variable Index
int xsnivt; //Assigned X Index Variable Type
char xstmnt[LINELEN]; //Expected Statement

View File

@ -18,5 +18,6 @@ b = s[n[i]];
b = s[c + n[i-1]];
b = s[func()];
b = s[0] & d[c-1] | n[i+2];
b = s[A] + b[X] + c[Y];
pop b, d[i], n[func()], s[c-n[i+3]];

View File

@ -69,8 +69,8 @@ r[i], d, f = testfn(); //Return 3 Values with Array
c, z[i], f = testfn(); //Return 3 Values with Array
r[i], z[j] = testfn(); //Return 2 Values with Arrays
r[i], z[j], f = testfn(); //Return 2 Values with Arrays
r[i], z[j], f[b] = testfn(); //Error - Array Element not Allowed with STX assignment
//void fnoutr() {char fninnr() {} }
//A, Y = testfn(); //Error - Registers not Allowed when returning multiple values
//r[i], z[j], f[b] = testfn(); //Error - Array Element not Allowed with STX assignment