mirror of
https://github.com/RevCurtisP/C02.git
synced 2024-06-25 14:29:27 +00:00
Allow functions in any term, fix bug in string escape parsing
This commit is contained in:
parent
ff70ba739a
commit
74e43fe108
34
src/expr.c
34
src/expr.c
|
@ -17,14 +17,16 @@
|
|||
/* 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);
|
||||
oprstk[trmidx] = oper; //Save Operator
|
||||
typstk[trmidx] = valtyp; //Save Value Type
|
||||
strcpy(trmstk[trmidx++], term); //Save Term
|
||||
}
|
||||
|
||||
/* Pop Term and Operator off Stack */
|
||||
void poptrm(void) {
|
||||
strcpy(term, trmstk[--trmidx]);
|
||||
oper = oprstk[trmidx];
|
||||
strcpy(term, trmstk[--trmidx]); //Restore Term
|
||||
valtyp = typstk[trmidx]; //Restore Value Type
|
||||
oper = oprstk[trmidx]; //Restore Operator
|
||||
}
|
||||
|
||||
/* Parse value (literal or identifier) *
|
||||
|
@ -100,15 +102,34 @@ void chkidx(void) {
|
|||
}
|
||||
}
|
||||
|
||||
/* Process Function in Term */
|
||||
void prcfnc(void) {
|
||||
asmlin("PHA", ""); //Save Accumulator
|
||||
prsfnc(0); //Parse Function Call
|
||||
asmlin("PHA", ""); //Push Result of Function Call onto Stack
|
||||
asmlin("TSX", ""); //X will be one byte before Result
|
||||
asmlin("LDA", "$102,X"); //Get Original Accumulator Value from Stack
|
||||
strcpy(term, "$101,X"); //Set Term to Return Value on Stack
|
||||
}
|
||||
|
||||
/* Post Function Call Processing */
|
||||
void pstfnc(void) {
|
||||
asmlin("INX", ""); //Increment Past Return Value
|
||||
asmlin("INX", ""); //Increment Past Saved Accumutator Value
|
||||
asmlin("TXS", ""); //Return Stack Pointer to Original State
|
||||
}
|
||||
|
||||
/* Parse Term in Expression *
|
||||
* Sets: term - the term (as a string) */
|
||||
void prstrm(void) {
|
||||
DEBUG("Parsing term\n", 0)
|
||||
prsval(FALSE);
|
||||
if (valtyp == FUNCTION) ERROR("Function call only allowed in first term\n", 0, EXIT_FAILURE)
|
||||
strcpy(term, value);
|
||||
DEBUG("Parsed term %s\n", term)
|
||||
chkidx(); //Check for Array Index
|
||||
strcpy(term, value);
|
||||
DEBUG("Parsed term %s\n", term)
|
||||
if (valtyp == FUNCTION) prcfnc(); //Process Function Call
|
||||
else chkidx(); //Check for Array Index
|
||||
skpspc();
|
||||
}
|
||||
|
||||
|
@ -220,6 +241,7 @@ void prsrxp(char trmntr) {
|
|||
prsopr(); //Parse Operator
|
||||
prstrm(); //Parse Term
|
||||
prcopr(); //Process Operator
|
||||
if (valtyp == FUNCTION) pstfnc(); //Do Post Function Processing
|
||||
trmcnt--;
|
||||
}
|
||||
expect(trmntr);
|
||||
|
|
|
@ -6,6 +6,7 @@ char term[255]; //Term parsed from equation
|
|||
|
||||
char oprstk[MAXTRM]; //Operator Stack
|
||||
char trmstk[MAXTRM][VARLEN+1]; //Function/Index Terms Stack
|
||||
int typstk[MAXTRM]; //Value Type Stack
|
||||
int trmidx; //Next Index in Stack
|
||||
|
||||
int trmcnt; //Number of total terms in current expression
|
||||
|
|
|
@ -139,6 +139,7 @@ void getwrd(void) {
|
|||
char escape(char c) {
|
||||
DEBUG("Escaping character '%c'\n", c)
|
||||
switch (c) {
|
||||
case 'n': return 0x0a;
|
||||
case 'r': return 0x0d;
|
||||
default: return c;
|
||||
}
|
||||
|
@ -151,7 +152,7 @@ void getstr(void) {
|
|||
DEBUG("Parsing string\n", 0)
|
||||
strdel = getnxt(); //Get String Delimiter
|
||||
CCMNT(strdel);
|
||||
while(match(strdel) == escnxt) {
|
||||
while(!match(strdel) || escnxt) {
|
||||
CCMNT(nxtchr);
|
||||
if (escnxt) {
|
||||
word[wrdlen++] = escape(getnxt());
|
||||
|
|
Loading…
Reference in New Issue
Block a user