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

Modified parsing to allow expressions in array indexes

This commit is contained in:
Curtis F Kaylor 2018-07-19 13:42:56 -04:00
parent 24a427583d
commit ca97762a9c
5 changed files with 101 additions and 36 deletions

View File

@ -12,7 +12,7 @@
#define MAXSTM 255 //Maximum Number of Stucture Members
#define VARLEN 6 //Maximum Variable Name Length
#define MAXVAR 255 //Maximum Number of Variables
#define MAXFNS 16 //Maximum Functions in Stack
#define MAXTRM 16 //Maximum Terms in Stack
#define DATASPC 2048 //Space to Allocate for Variable Data
#define LABLEN 6 //Maximum Label Length
#define LABFMT "L_%04d" //Label Format

View File

@ -14,6 +14,19 @@
#include "label.h"
#include "expr.h"
/* Push Term and Operator onto Stack */
void pshtrm(void) {
if (trmidx >= MAXTRM) ERROR("Maximum Function Call/Array Index Depth Exceeded", 0, EXIT_FAILURE)
oprstk[trmidx] = oper;
strcpy(trmstk[trmidx++], term);
}
/* Pop Term and Operator off Stack */
void poptrm(void) {
strcpy(term, trmstk[--trmidx]);
oper = oprstk[trmidx];
}
/* Parse value (literal or identifier) *
* Sets: value - the value (as a string) *
* valtyp - value type */
@ -27,21 +40,24 @@ void prsval(int alwreg) {
skpspc();
}
/* Process Unary Minus */
void prcmns(void) {
DEBUG("Processing unary minus", 0)
asmlin("LDA", "#$00"); //Handle Unary Minus
}
/* Parse array index *
* Sets: value - array index or *
* "" if no index defined */
void prsidx(void) {
void prsidx(int clbrkt) {
expect('[');
prsval(TRUE);
DEBUG("Parsed array index '%s'\n", value)
expect(']');
if (clbrkt) expect(']');
}
/* Check for, Parse, and Process Index */
void chkidx(void) {
//DEBUG("Checking for Array Index with valtyp=%d\n", valtyp)
if (valtyp == ARRAY) {
prsidx();
/* Process Simple Array Index */
void prcsix(void) {
if (valtyp == LITERAL) {
strcat(term, "+");
strcat(term, word);
@ -53,6 +69,34 @@ void chkidx(void) {
else if (strcmp(value, "X")!=0) asmlin("LDX", value);
strcat(term, ",X");
}
}
/* Process Expression Array Index */
void prcxix(void) {
pshtrm(); //Push Array Variable onto Term Stack
if (trmcnt) asmlin("PHA", ""); //Save Accumulator if not first term
prcftm(); //Process First Term of Expression
prsrxp(']'); //Parse Rest of Expression
asmlin("TAX", ""); //Transfer Result of Expression to Index Register
if (trmcnt) asmlin("PLA", ""); //Restore Accumator if not first term
poptrm(); //Pop Array Variable off Term Stack
strcat(term, ",X");
}
/* Check for, Parse, and Process Index */
void chkidx(void) {
//DEBUG("Checking for Array Index with valtyp=%d\n", valtyp)
if (valtyp == ARRAY) {
if (look('-')) {
prcmns();
prcxix();
}
else {
prsidx(FALSE);
if (valtyp > REGISTER) prcxix();
else if (look(']')) prcsix();
else prcxix();
}
}
}
@ -117,8 +161,7 @@ int chkadr(int adract) {
/* Parse function call */
void prsfnc(char trmntr) {
DEBUG("Processing Function Call '%s'\n", term)
if (fnscnt >= MAXFNS) ERROR("Maximum Function Call Depth Exceeded", 0, EXIT_FAILURE)
strcpy(fnstck[fnscnt++], term);
pshtrm(); //Push Function Name onto Term Stack
skpchr(); //skip open paren
CCMNT('(');
if (!chkadr(0) && isxpre() || match('*')) {
@ -130,14 +173,13 @@ void prsfnc(char trmntr) {
}
expect(')');
expect(trmntr);
asmlin("JSR", fnstck[--fnscnt]);
poptrm(); //Pop Function Name off Term Stack
asmlin("JSR", term);
skpspc();
}
/* Parse first term of expession *
* First term can include function calls */
void prsftm(void) {
prsval(TRUE);
/* Process first term of expression */
void prcftm(void) {
DEBUG("Processing first term '%s'\n", value)
strcpy(term, value);
if (valtyp == FUNCTION) prsfnc(0); //Parse Expression Function
@ -147,6 +189,13 @@ void prsftm(void) {
else { chkidx(); asmlin("LDA", term); }
}
/* Parse first term of expession *
* First term can include function calls */
void prsftm(void) {
prsval(TRUE);
prcftm();
}
/* Process Arithmetic or Bitwise Operator *
* and the term that follows it */
void prcopr(void) {
@ -163,20 +212,25 @@ void prcopr(void) {
oper = 0;
}
/* Parse Remainder of Expression */
void prsrxp(char trmntr) {
skpspc();
while (isoper()) {
trmcnt++;
prsopr(); //Parse Operator
prstrm(); //Parse Term
prcopr(); //Process Operator
trmcnt--;
}
expect(trmntr);
}
/* Parse and compile expression */
void prsxpr(char trmntr) {
DEBUG("Parsing expression\n", 0)
skpspc();
if (match('-')) {
DEBUG("Processing unary minus", 0)
asmlin("LDA", "#$00"); //Handle Unary Minus
}
trmcnt = 0;
if (match('-')) prcmns();
else prsftm(); //Parse First Term
skpspc();
while (isoper()) {
prsopr(); //Parse Operator
prstrm(); //Parse Term
prcopr(); //Process Operator
}
expect(trmntr);
prsrxp(trmntr);
}

View File

@ -4,15 +4,20 @@
char term[255]; //Term parsed from equation
char fnstck[MAXFNS][VARLEN+1]; //Function Call Stack
int fnscnt; //Number of Functions in Stack
char oprstk[MAXTRM]; //Operator Stack
char trmstk[MAXTRM][VARLEN+1]; //Function/Index Terms Stack
int trmidx; //Next Index in Stack
int trmcnt; //Number of total terms in current expression
int chkadr(int adract); //Check for and Process Address or String
void chkidx(); //Check for, Parse, and Process Index
void prcftm(); //Process First Term
void prsadr(int adract); //Parse and Compile Address of Operator
void prsfnc(char trmntr); //Parse function call
void prsidx(); //Parse Array Index
void prstrm(); //Parse Term in Expression
void prsrxp(char trmntr); //Parse Rest of Expression
void prsxpr(char trmntr); //Parse Expression

View File

@ -97,7 +97,7 @@ void prcasn(char trmntr) {
/* Parse and Return Array Index and Type */
int getidx(char* idx) {
prsidx(); //Parse Array Index
prsidx(TRUE); //Parse Array Index
if (valtyp == LITERAL) strncpy(idx, word, VARLEN);
else strncpy(idx, value, VARLEN);
DEBUG("Parsed index %s\n", idx)
@ -293,13 +293,14 @@ void pinlne(void) {
void ppop(void) {
DEBUG("Parsing POP statement\n", 0)
do {
asmlin("PLA", ""); //Pop Value off Stack
if (!look('*')) {
if (look('*')) term[0]=0;
else {
reqvar(TRUE);
strcpy(term, value);
chkidx();
asmlin("STA", term);
}
asmlin("PLA", ""); //Pop Value off Stack
if (term[0]) asmlin("STA", term);
} while (look(','));
expect(';');
}

View File

@ -110,11 +110,14 @@ if (i=0)
i++;
else
i--;
here: if (getkey() & $7F == $1B) goto exit;
there: goto flwctl;
if (#TRUE) putln("TRUE");
else if (#FALSE) putln("FALSE");
i = 0;
while (i < 10) {
i++;
@ -123,8 +126,6 @@ while (i:+) {
i--;
}
do {
c = rdkey();
} while (c=0);
@ -138,7 +139,11 @@ do {
for (i=1; i<9; i++)
prchr(i);
if (@z == @r)
for (i=0; i<=@z; i++)
z[i] = r[i];
if (d<f) i=b; else i=c; //Regular IF
i = (d<f) ? b : c; //Shortcut IF