mirror of
https://github.com/RevCurtisP/C02.git
synced 2024-11-22 01:31:33 +00:00
Allow expressions in second and third function argument
This commit is contained in:
parent
223f7bbb97
commit
6703c8cb10
767
src/expr.c
767
src/expr.c
@ -1,372 +1,395 @@
|
|||||||
/***********************************
|
/***********************************
|
||||||
* C02 Expression Parsing Routines *
|
* C02 Expression Parsing Routines *
|
||||||
***********************************/
|
***********************************/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "asm.h"
|
#include "asm.h"
|
||||||
#include "parse.h"
|
#include "parse.h"
|
||||||
#include "vars.h"
|
#include "vars.h"
|
||||||
#include "label.h"
|
#include "label.h"
|
||||||
#include "expr.h"
|
#include "expr.h"
|
||||||
|
|
||||||
/* Push Term and Operator onto Stack */
|
/* Push Term and Operator onto Stack */
|
||||||
void pshtrm(void) {
|
void pshtrm(void) {
|
||||||
if (trmidx >= MAXTRM) ERROR("Maximum Function Call/Array Index Depth Exceeded", 0, EXIT_FAILURE)
|
if (trmidx >= MAXTRM) ERROR("Maximum Function Call/Array Index Depth Exceeded", 0, EXIT_FAILURE)
|
||||||
oprstk[trmidx] = oper; //Put Current Operator on Stack
|
oprstk[trmidx] = oper; //Put Current Operator on Stack
|
||||||
strcpy(trmstk[trmidx], term); //Put Current Term on Stack
|
strcpy(trmstk[trmidx], term); //Put Current Term on Stack
|
||||||
trmidx++; //Increment Stack Pointer
|
trmidx++; //Increment Stack Pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pop Term and Operator off Stack */
|
/* Pop Term and Operator off Stack */
|
||||||
void poptrm(void) {
|
void poptrm(void) {
|
||||||
trmidx--; //Decrement Stack Pointer
|
trmidx--; //Decrement Stack Pointer
|
||||||
strcpy(term, trmstk[trmidx]); //Restore Current Term from Stack
|
strcpy(term, trmstk[trmidx]); //Restore Current Term from Stack
|
||||||
oper = oprstk[trmidx]; //Restore Current Operator from Stack
|
oper = oprstk[trmidx]; //Restore Current Operator from Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse value (literal or identifier) *
|
/* Parse value (literal or identifier) *
|
||||||
* Args: alwreg - allow registers *
|
* Args: alwreg - allow registers *
|
||||||
8 alwcon - allow constants *
|
8 alwcon - allow constants *
|
||||||
* Sets: value - the value (as a string) *
|
* Sets: value - the value (as a string) *
|
||||||
* valtyp - value type */
|
* valtyp - value type */
|
||||||
void prsval(int alwreg, int alwcon) {
|
void prsval(int alwreg, int alwcon) {
|
||||||
DEBUG("Parsing value\n", 0)
|
DEBUG("Parsing value\n", 0)
|
||||||
skpspc();
|
skpspc();
|
||||||
if (islpre()) prslit(); //Parse Literal
|
if (islpre()) prslit(); //Parse Literal
|
||||||
else if (isalph()) prsvar(alwreg, alwcon); //Parse Variable
|
else if (isalph()) prsvar(alwreg, alwcon); //Parse Variable
|
||||||
else if (isbtop()) prsbop(); //Parse Byte Operator
|
else if (isbtop()) prsbop(); //Parse Byte Operator
|
||||||
else expctd("literal or variable");
|
else expctd("literal or variable");
|
||||||
DEBUG("Parsed value of type %d\n", valtyp)
|
DEBUG("Parsed value of type %d\n", valtyp)
|
||||||
skpspc();
|
skpspc();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process Unary Minus */
|
/* Process Unary Minus */
|
||||||
void prcmns(void) {
|
void prcmns(void) {
|
||||||
DEBUG("Processing unary minus", 0)
|
DEBUG("Processing unary minus", 0)
|
||||||
asmlin("LDA", "#$00"); //Handle Unary Minus
|
asmlin("LDA", "#$00"); //Handle Unary Minus
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse array index *
|
/* Parse array index *
|
||||||
* Args: clbrkt - require closing bracket *
|
* Args: clbrkt - require closing bracket *
|
||||||
* Sets: value - array index or *
|
* Sets: value - array index or *
|
||||||
* "" if no index defined */
|
* "" if no index defined */
|
||||||
void prsidx(int clbrkt) {
|
void prsidx(int clbrkt) {
|
||||||
expect('[');
|
expect('[');
|
||||||
prsval(TRUE, TRUE); //Parse Value, Allow Registers & Constants
|
prsval(TRUE, TRUE); //Parse Value, Allow Registers & Constants
|
||||||
DEBUG("Parsed array index '%s'\n", value)
|
DEBUG("Parsed array index '%s'\n", value)
|
||||||
if (clbrkt) expect(']');
|
if (clbrkt) expect(']');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process Simple Array Index *
|
/* Process Simple Array Index *
|
||||||
* Uses: term - array variable name *
|
* Uses: term - array variable name *
|
||||||
* valtyp - array index value type *
|
* valtyp - array index value type *
|
||||||
* value - array index as string *
|
* value - array index as string *
|
||||||
* word - array index raw string *
|
* word - array index raw string *
|
||||||
* Sets: term - modified variable name */
|
* Sets: term - modified variable name */
|
||||||
void prcsix(void) {
|
void prcsix(void) {
|
||||||
if (valtyp == LITERAL) {
|
if (valtyp == LITERAL) {
|
||||||
strcat(term, "+");
|
strcat(term, "+");
|
||||||
strcat(term, word);
|
strcat(term, word);
|
||||||
}
|
}
|
||||||
else if (strcmp(value, "Y")==0)
|
else if (strcmp(value, "Y")==0)
|
||||||
strcat(term, ",Y");
|
strcat(term, ",Y");
|
||||||
else {
|
else {
|
||||||
if (strcmp(value, "A")==0) asmlin("TAX", "");
|
if (strcmp(value, "A")==0) asmlin("TAX", "");
|
||||||
else if (strcmp(value, "X")!=0) asmlin("LDX", value);
|
else if (strcmp(value, "X")!=0) asmlin("LDX", value);
|
||||||
strcat(term, ",X");
|
strcat(term, ",X");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process Expression Array Index */
|
/* Process Expression Array Index */
|
||||||
void prcxix(void) {
|
void prcxix(void) {
|
||||||
pshtrm(); //Push Array Variable onto Term Stack
|
pshtrm(); //Push Array Variable onto Term Stack
|
||||||
if (trmcnt) asmlin("PHA", ""); //Save Accumulator if not first term
|
if (trmcnt) asmlin("PHA", ""); //Save Accumulator if not first term
|
||||||
prcftm(FALSE); //Process First Term of Expression
|
prcftm(FALSE); //Process First Term of Expression
|
||||||
prsrxp(']'); //Parse Rest of Expression
|
prsrxp(']'); //Parse Rest of Expression
|
||||||
asmlin("TAX", ""); //Transfer Result of Expression to Index Register
|
asmlin("TAX", ""); //Transfer Result of Expression to Index Register
|
||||||
if (trmcnt) asmlin("PLA", ""); //Restore Accumator if not first term
|
if (trmcnt) asmlin("PLA", ""); //Restore Accumator if not first term
|
||||||
poptrm(); //Pop Array Variable off Term Stack
|
poptrm(); //Pop Array Variable off Term Stack
|
||||||
strcat(term, ",X");
|
strcat(term, ",X");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for, Parse, and Process Index */
|
/* Check for, Parse, and Process Index */
|
||||||
void chkidx(void) {
|
void chkidx(void) {
|
||||||
//DEBUG("Checking for Array Index with valtyp=%d\n", valtyp)
|
//DEBUG("Checking for Array Index with valtyp=%d\n", valtyp)
|
||||||
if (valtyp == ARRAY) {
|
if (valtyp == ARRAY) {
|
||||||
if (look('-')) {
|
if (look('-')) {
|
||||||
prcmns();
|
prcmns();
|
||||||
prcxix();
|
prcxix();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
prsidx(FALSE);
|
prsidx(FALSE);
|
||||||
if (valtyp > REGISTER) prcxix();
|
if (valtyp > REGISTER) prcxix();
|
||||||
else if (look(']')) prcsix();
|
else if (look(']')) prcsix();
|
||||||
else prcxix();
|
else prcxix();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse Term in Expression *
|
/* Parse Term in Expression *
|
||||||
* Sets: term - the term (as a string) */
|
* Sets: term - the term (as a string) */
|
||||||
int prstrm(int alwint) {
|
int prstrm(int alwint) {
|
||||||
DEBUG("Parsing term\n", 0)
|
DEBUG("Parsing term\n", 0)
|
||||||
prsval(FALSE, TRUE); //Parse Value - Disallow Registers, Allow Constants
|
prsval(FALSE, TRUE); //Parse Value - Disallow Registers, Allow Constants
|
||||||
if (valtyp == FUNCTION) ERROR("Function call only allowed in first term\n", 0, EXIT_FAILURE)
|
if (valtyp == FUNCTION) ERROR("Function call only allowed in first term\n", 0, EXIT_FAILURE)
|
||||||
strcpy(term, value);
|
strcpy(term, value);
|
||||||
if (valtyp == VARIABLE && prcvar(alwint)) return TRUE;
|
if (valtyp == VARIABLE && prcivr(alwint)) return TRUE;
|
||||||
DEBUG("Parsed term %s\n", term)
|
DEBUG("Parsed term %s\n", term)
|
||||||
chkidx(); //Check for Array Index
|
chkidx(); //Check for Array Index
|
||||||
skpspc();
|
skpspc();
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Process Address Reference
|
/* Process Address Reference
|
||||||
* Args: adract = Address Action (adacts) *
|
* Args: adract = Address Action (adacts) *
|
||||||
* symbol = Symbol to Process */
|
* symbol = Symbol to Process */
|
||||||
void prcadr(int adract, char* symbol) {
|
void prcadr(int adract, char* symbol) {
|
||||||
DEBUG("Processing address '%s'\n", word)
|
DEBUG("Processing address '%s'\n", word)
|
||||||
strcpy(word,"#>(");
|
strcpy(word,"#>(");
|
||||||
strcat(word,symbol);
|
strcat(word,symbol);
|
||||||
strcat(word,")");
|
strcat(word,")");
|
||||||
if (adract == ADPUSH) { asmlin("LDA", word); asmlin("PHA", ""); }
|
if (adract == ADPUSH) { asmlin("LDA", word); asmlin("PHA", ""); }
|
||||||
else asmlin("LDY", word);
|
else asmlin("LDY", word);
|
||||||
strcpy(word,"#<(");
|
strcpy(word,"#<(");
|
||||||
strcat(word,symbol);
|
strcat(word,symbol);
|
||||||
strcat(word,")");
|
strcat(word,")");
|
||||||
if (adract == ADPUSH) { asmlin("LDA", word); asmlin("PHA", ""); }
|
if (adract == ADPUSH) { asmlin("LDA", word); asmlin("PHA", ""); }
|
||||||
else asmlin("LDX", word);
|
else asmlin("LDX", word);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse and Compile Address of Operator *
|
/* Parse and Compile Address of Operator *
|
||||||
* Args: adract = Address Action */
|
* Args: adract = Address Action */
|
||||||
void prsadr(int adract) {
|
void prsadr(int adract) {
|
||||||
DEBUG("Parsing address\n", 0)
|
DEBUG("Parsing address\n", 0)
|
||||||
if (isnpre()) prsnum(0xFFFF);
|
if (isnpre()) prsnum(0xFFFF);
|
||||||
else {
|
else {
|
||||||
getwrd();
|
getwrd();
|
||||||
if (fndlab(word)) strcpy(value, word);
|
if (fndlab(word)) strcpy(value, word);
|
||||||
else prsvrw(FALSE, TRUE);
|
else prsvrw(FALSE, TRUE);
|
||||||
}
|
}
|
||||||
if (adract) prcadr(adract, value); //Compile Address Reference
|
if (adract) prcadr(adract, value); //Compile Address Reference
|
||||||
else strcpy(word, value); //Save for Calling Routine
|
else strcpy(word, value); //Save for Calling Routine
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse and Create Anonymous String *
|
/* Parse and Create Anonymous String *
|
||||||
* Args: adract = Address Action *
|
* Args: adract = Address Action *
|
||||||
* alwstr = Allow String */
|
* alwstr = Allow String */
|
||||||
void prsstr(int adract, int alwstr) {
|
void prsstr(int adract, int alwstr) {
|
||||||
if (!alwstr) ERROR("Illegal String Reference", 0, EXIT_FAILURE)
|
if (!alwstr) ERROR("Illegal String Reference", 0, EXIT_FAILURE)
|
||||||
DEBUG("Parsing anonymous string\n", 0)
|
DEBUG("Parsing anonymous string\n", 0)
|
||||||
newlbl(vrname); //Generate Variable Name
|
newlbl(vrname); //Generate Variable Name
|
||||||
value[0] = 0; //Use Variable Size 0
|
value[0] = 0; //Use Variable Size 0
|
||||||
setvar(MTNONE, VTCHAR); //Set Variable Name, Type, and Size
|
setvar(MTNONE, VTCHAR); //Set Variable Name, Type, and Size
|
||||||
prsdts(); //Parse Data String
|
prsdts(); //Parse Data String
|
||||||
setdat(); //Set Variable Data
|
setdat(); //Set Variable Data
|
||||||
varcnt++; //Increment Variable Counter
|
varcnt++; //Increment Variable Counter
|
||||||
if (adract) prcadr(adract, vrname); //Compile Address Reference
|
if (adract) prcadr(adract, vrname); //Compile Address Reference
|
||||||
else strcpy(word, vrname); //Save for Calling Routine
|
else strcpy(word, vrname); //Save for Calling Routine
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for and Process Address or String *
|
/* Check for and Process Address or String *
|
||||||
* Args: adract = Address Action *
|
* Args: adract = Address Action *
|
||||||
* alwstr = Allow String */
|
* alwstr = Allow String */
|
||||||
int chkadr(int adract, int alwstr) {
|
int chkadr(int adract, int alwstr) {
|
||||||
DEBUG("Checking for Address or String\n", 0)
|
DEBUG("Checking for Address or String\n", 0)
|
||||||
int result = TRUE;
|
int result = TRUE;
|
||||||
if (look('&')) prsadr(adract);
|
if (look('&')) prsadr(adract);
|
||||||
else if (match('"')) prsstr(adract, alwstr);
|
else if (match('"')) prsstr(adract, alwstr);
|
||||||
else result = FALSE;
|
else result = FALSE;
|
||||||
skpspc();
|
skpspc();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse Byte Operator */
|
/* Parse Byte Operator */
|
||||||
void prsbop(void) {
|
void prsbop(void) {
|
||||||
char byteop = getnxt();
|
char byteop = getnxt();
|
||||||
CCMNT(byteop);
|
CCMNT(byteop);
|
||||||
DEBUG("Parsing byte operator '%c'\n", byteop)
|
DEBUG("Parsing byte operator '%c'\n", byteop)
|
||||||
if (chkadr(FALSE, FALSE)) {
|
if (chkadr(FALSE, FALSE)) {
|
||||||
sprintf(value, "%c(%s)", byteop, word);
|
sprintf(value, "%c(%s)", byteop, word);
|
||||||
valtyp = LITERAL;
|
valtyp = LITERAL;
|
||||||
} else {
|
} else {
|
||||||
reqvar(FALSE);
|
reqvar(FALSE);
|
||||||
if (vartyp != VTINT) ERROR("Integer Value Expected\n", 0, EXIT_FAILURE)
|
if (vartyp != VTINT) ERROR("Integer Value Expected\n", 0, EXIT_FAILURE)
|
||||||
if (byteop == '>') strcat(value, "+1");
|
if (byteop == '>') strcat(value, "+1");
|
||||||
vartyp = VTCHAR;
|
vartyp = VTCHAR;
|
||||||
}
|
}
|
||||||
DEBUG("Set value to \"%s\"\n", value)
|
DEBUG("Set value to \"%s\"\n", value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse Function Argument or Return Values */
|
/* Parse Function Argument or Return Values */
|
||||||
void prsfpr(char trmntr) {
|
void prsfpr(char trmntr) {
|
||||||
if (!chkadr(ADLDYX, TRUE) && isxpre() || match('.')) {
|
int pusha = 0; int pushy = 0; //A and Y Arguments Pushed
|
||||||
if (!look('.')) {if (prsxpf(0)) goto prsfne;}
|
if (!chkadr(ADLDYX, TRUE) && isxpre() || match('.')) {
|
||||||
if (look(',') && !chkadr(ADLDYX, TRUE)) {
|
if (look('.')) pusha = 255;
|
||||||
if (!look('.')) {
|
else {if (prsxpf(0)) goto prsfne;}
|
||||||
if (prstrm(TRUE)) goto prsfne;
|
if (look(',') && !chkadr(ADLDYX, TRUE)) {
|
||||||
asmlin("LDY", term);
|
if (look('.')) {
|
||||||
}
|
pushy = -1;
|
||||||
if (look(',')) {
|
}
|
||||||
prsval(FALSE, TRUE); //Parse Value - Disallow Registers, Allow Constants
|
else {
|
||||||
if (valtyp > VARIABLE) ERROR("Illegal Value Function Argument\n", 0, EXIT_FAILURE);
|
if (look('(')) {
|
||||||
if (valtyp == VARIABLE && vartyp != VTCHAR) ERROR("Illegal Variable Type\n", 0, EXIT_FAILURE);
|
if (pusha==0) {pusha = 1; asmlin("PHA","");} //Save A on Stack
|
||||||
asmlin("LDX", value); }
|
prsxpr(')'); asmlin("TAY", ""); //Evaluate Expression, and Copy to Y
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
prsfne:
|
if (prstrm(TRUE)) goto prsfne;
|
||||||
expect(trmntr);
|
asmlin("LDY", term);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/* Parse function call */
|
if (look(',')) {
|
||||||
void prsfnc(char trmntr) {
|
if (look('(')) {
|
||||||
DEBUG("Processing Function Call '%s'\n", term)
|
if (pusha==0) {pusha = 1; asmlin("PHA","");} //Save A on Stack
|
||||||
//int argexp = FALSE; //Expression(s) in second and third argument
|
if (pushy==0) {pushy = 1; asmlin("PHA",""); asmlin("PHY","");} //Save Y on Stack
|
||||||
pshtrm(); //Push Function Name onto Term Stack
|
prsxpr(')'); asmlin("TAX", ""); //Evaluate Expression, and Copy to X
|
||||||
skpchr(); //skip open paren
|
}
|
||||||
CCMNT('(');
|
else {
|
||||||
prsfpr(')'); //Parse Function Parameters
|
prsval(FALSE, TRUE); //Parse Value - Disallow Registers, Allow Constants
|
||||||
expect(trmntr);
|
if (valtyp > VARIABLE) ERROR("Illegal Value Function Argument\n", 0, EXIT_FAILURE);
|
||||||
poptrm(); //Pop Function Name off Term Stack
|
if (valtyp == VARIABLE && vartyp != VTCHAR) ERROR("Illegal Variable Type\n", 0, EXIT_FAILURE);
|
||||||
asmlin("JSR", term);
|
asmlin("LDX", value);
|
||||||
skpspc();
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/* Process Integer Variable */
|
}
|
||||||
void prcvri(void) {
|
prsfne:
|
||||||
DEBUG("Processing Integer Variable '%s'\n", value)
|
if (pushy==1) {asmlin("PLA",""); asmlin("TAY","");} //Pull Y Off Stack
|
||||||
asmlin("LDX", value);
|
if (pusha==1) asmlin("PLA",""); //Pull A Off Stack
|
||||||
strcat(value, "+1");
|
expect(trmntr);
|
||||||
asmlin("LDY", value);
|
}
|
||||||
}
|
|
||||||
|
/* Parse function call */
|
||||||
/* Process Variable in Term */
|
void prsfnc(char trmntr) {
|
||||||
int prcvar(int alwint) {
|
DEBUG("Processing Function Call '%s'\n", term)
|
||||||
switch (vartyp) {
|
//int argexp = FALSE; //Expression(s) in second and third argument
|
||||||
case VTINT:
|
pshtrm(); //Push Function Name onto Term Stack
|
||||||
if (!alwint) ERROR("Illegal Use of Integer Variable %s\n", word, EXIT_FAILURE)
|
skpchr(); //skip open paren
|
||||||
prcvri();
|
CCMNT('(');
|
||||||
return TRUE;
|
prsfpr(')'); //Parse Function Parameters
|
||||||
case VTARRAY:
|
expect(trmntr);
|
||||||
if (!alwint) ERROR("Illegal Reference to Array %s\n", word, EXIT_FAILURE)
|
poptrm(); //Pop Function Name off Term Stack
|
||||||
prcadr(ADNONE, term);
|
asmlin("JSR", term);
|
||||||
return TRUE;
|
skpspc();
|
||||||
case VTSTRUCT:
|
}
|
||||||
if (!alwint) ERROR("Illegal Reference to Struct %s\n", word, EXIT_FAILURE)
|
|
||||||
prcadr(ADNONE, term);
|
/* Process Integer Variable */
|
||||||
return TRUE;
|
void prcvri(void) {
|
||||||
default:
|
DEBUG("Processing Integer Variable '%s'\n", value)
|
||||||
return FALSE;
|
asmlin("LDX", value);
|
||||||
}
|
strcat(value, "+1");
|
||||||
}
|
asmlin("LDY", value);
|
||||||
|
}
|
||||||
/* Process first term of expression */
|
|
||||||
int prcftm(int alwint) {
|
/* Process Integer Variable in Term *
|
||||||
DEBUG("Processing first term '%s'\n", value)
|
* Args: alwint = Allow Integer-Like Variable *
|
||||||
strcpy(term, value);
|
* Returns: Integer-Like Variable Processed - TRUE/FALSE */
|
||||||
if (valtyp == VARIABLE && prcvar(alwint)) return TRUE;
|
int prcivr(int alwint) {
|
||||||
if (valtyp == FUNCTION) prsfnc(0); //Parse Expression Function
|
switch (vartyp) {
|
||||||
else if (wordis("A")) return FALSE;
|
case VTINT:
|
||||||
else if (wordis("X")) asmlin("TXA", "");
|
if (!alwint) ERROR("Illegal Use of Integer Variable %s\n", word, EXIT_FAILURE)
|
||||||
else if (wordis("Y")) asmlin("TYA", "");
|
prcvri();
|
||||||
else { chkidx(); asmlin("LDA", term); }
|
return TRUE;
|
||||||
return FALSE;
|
case VTARRAY:
|
||||||
}
|
if (!alwint) ERROR("Illegal Reference to Array %s\n", word, EXIT_FAILURE)
|
||||||
|
prcadr(ADNONE, term);
|
||||||
/* Parse first term of expession *
|
return TRUE;
|
||||||
* First term can include function calls */
|
case VTSTRUCT:
|
||||||
int prsftm(int alwint) {
|
if (!alwint) ERROR("Illegal Reference to Struct %s\n", word, EXIT_FAILURE)
|
||||||
prsval(TRUE, TRUE); //Parse Value, Allow Registers & Constants
|
prcadr(ADNONE, term);
|
||||||
return prcftm(alwint);
|
return TRUE;
|
||||||
}
|
default:
|
||||||
|
return FALSE;
|
||||||
/* Process Arithmetic or Bitwise Operator *
|
}
|
||||||
* and the term that follows it */
|
}
|
||||||
void prcopr(void) {
|
|
||||||
DEBUG("Processing operator '%c'\n", oper)
|
/* Process first term of expression */
|
||||||
switch(oper) {
|
int prcftm(int alwint) {
|
||||||
case '+': asmlin("CLC", ""); asmlin("ADC", term); break; //Addition
|
DEBUG("Processing first term '%s'\n", value)
|
||||||
case '-': asmlin("SEC", ""); asmlin("SBC", term); break; //Subtraction
|
strcpy(term, value);
|
||||||
case '&': asmlin("AND", term); break; //Bitwise AND
|
if (valtyp == VARIABLE && prcivr(alwint)) return TRUE;
|
||||||
case '!': //For systems that don't have pipe in character set
|
if (valtyp == FUNCTION) prsfnc(0); //Parse Expression Function
|
||||||
case '|': asmlin("ORA", term); break; //Bitwise OR
|
else if (wordis("A")) return FALSE;
|
||||||
case '^': asmlin("EOR", term); break; //Bitwise XOR
|
else if (wordis("X")) asmlin("TXA", "");
|
||||||
default: ERROR("Unrecognized operator '%c'\n", oper, EXIT_FAILURE)
|
else if (wordis("Y")) asmlin("TYA", "");
|
||||||
}
|
else { chkidx(); asmlin("LDA", term); }
|
||||||
oper = 0;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse Remainder of Expression */
|
/* Parse first term of expession *
|
||||||
void prsrxp(char trmntr) {
|
* First term can include function calls */
|
||||||
skpspc();
|
int prsftm(int alwint) {
|
||||||
while (isoper()) {
|
prsval(TRUE, TRUE); //Parse Value, Allow Registers & Constants
|
||||||
trmcnt++; //Increment Expression Depth
|
return prcftm(alwint);
|
||||||
prsopr(); //Parse Operator
|
}
|
||||||
prstrm(FALSE); //Parse Term
|
|
||||||
prcopr(); //Process Operator
|
/* Process Arithmetic or Bitwise Operator *
|
||||||
trmcnt--; //Decrement Expression Depth
|
* and the term that follows it */
|
||||||
}
|
void prcopr(void) {
|
||||||
expect(trmntr);
|
DEBUG("Processing operator '%c'\n", oper)
|
||||||
}
|
switch(oper) {
|
||||||
|
case '+': asmlin("CLC", ""); asmlin("ADC", term); break; //Addition
|
||||||
int prsxpp(char trmntr, int alwint) {
|
case '-': asmlin("SEC", ""); asmlin("SBC", term); break; //Subtraction
|
||||||
DEBUG("Parsing expression\n", 0)
|
case '&': asmlin("AND", term); break; //Bitwise AND
|
||||||
skpspc();
|
case '!': //For systems that don't have pipe in character set
|
||||||
trmcnt = 0; //Initialize Expression Depth
|
case '|': asmlin("ORA", term); break; //Bitwise OR
|
||||||
if (match('-')) prcmns(); //Process Unary Minus
|
case '^': asmlin("EOR", term); break; //Bitwise XOR
|
||||||
else if (prsftm(alwint)) return TRUE; //Parse First Term
|
default: ERROR("Unrecognized operator '%c'\n", oper, EXIT_FAILURE)
|
||||||
prsrxp(trmntr); //Parse Remainder of Express
|
}
|
||||||
return FALSE;
|
oper = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse and compile expression */
|
/* Parse Remainder of Expression */
|
||||||
void prsxpr(char trmntr) {
|
void prsrxp(char trmntr) {
|
||||||
prsxpp(trmntr, FALSE);
|
skpspc();
|
||||||
}
|
while (isoper()) {
|
||||||
|
trmcnt++; //Increment Expression Depth
|
||||||
/* Parse and compile function parameter expression *
|
prsopr(); //Parse Operator
|
||||||
* Returns: TRUE if Integer Expression */
|
prstrm(FALSE); //Parse Term
|
||||||
int prsxpf(char trmntr) {
|
prcopr(); //Process Operator
|
||||||
return prsxpp(trmntr, TRUE);
|
trmcnt--; //Decrement Expression Depth
|
||||||
}
|
}
|
||||||
|
expect(trmntr);
|
||||||
/* Parse and Compile Integer Expression *
|
}
|
||||||
* (Address, Integer Literal, Variable, *
|
|
||||||
* Struct Member, or Function) *
|
int prsxpp(char trmntr, int alwint) {
|
||||||
* Args: trmntr - expression terminator *
|
DEBUG("Parsing expression\n", 0)
|
||||||
* asmxpr - assemble expression *
|
skpspc();
|
||||||
* Sets: value - Parsed Value or Symbol */
|
trmcnt = 0; //Initialize Expression Depth
|
||||||
void prsxpi(char trmntr, int asmxpr) {
|
if (match('-')) prcmns(); //Process Unary Minus
|
||||||
skpspc();
|
else if (prsftm(alwint)) return TRUE; //Parse First Term
|
||||||
DEBUG("Parsing integer expression\n", 0)
|
prsrxp(trmntr); //Parse Remainder of Express
|
||||||
if (!chkadr(TRUE, FALSE)) {
|
return FALSE;
|
||||||
if (isnpre()) {
|
}
|
||||||
DEBUG("Parsing Integer Literal\n", 0)
|
|
||||||
int number = prsnum(0xFFFF); //Parse Number into value
|
/* Parse and compile expression */
|
||||||
if (asmxpr) {
|
void prsxpr(char trmntr) {
|
||||||
sprintf(value, "#%d", number & 0xFF); asmlin("LDX", value);
|
prsxpp(trmntr, FALSE);
|
||||||
sprintf(value, "#%d", number >> 8); asmlin("LDY", value);
|
}
|
||||||
}
|
|
||||||
} else if (isalph()) {
|
/* Parse and compile function parameter expression *
|
||||||
prsvar(FALSE, TRUE);
|
* Returns: TRUE if Integer Expression */
|
||||||
if (valtyp == FUNCTION) {
|
int prsxpf(char trmntr) {
|
||||||
strcpy(term, value);
|
return prsxpp(trmntr, TRUE);
|
||||||
prsfnc(0); //Parse Expression Function
|
}
|
||||||
} else if (valtyp == STRUCTURE) {
|
|
||||||
prsmbr(value);
|
/* Parse and Compile Integer Expression *
|
||||||
if (vartyp != VTINT) ERROR("Illegal Member %s In Integer Expression", value, EXIT_FAILURE)
|
* (Address, Integer Literal, Variable, *
|
||||||
} else if (valtyp == VARIABLE && vartyp == VTINT) {
|
* Struct Member, or Function) *
|
||||||
if (asmxpr) prcvri(); //Process Integer Variable
|
* Args: trmntr - expression terminator *
|
||||||
} else {
|
* asmxpr - assemble expression *
|
||||||
ERROR("Illegal Variable %s In Integer Expression", value, EXIT_FAILURE)
|
* Sets: value - Parsed Value or Symbol */
|
||||||
}
|
void prsxpi(char trmntr, int asmxpr) {
|
||||||
} else {
|
skpspc();
|
||||||
ERROR("Expected Integer Value or Function\n", 0, EXIT_FAILURE);
|
DEBUG("Parsing integer expression\n", 0)
|
||||||
}
|
if (!chkadr(TRUE, FALSE)) {
|
||||||
}
|
if (isnpre()) {
|
||||||
expect(trmntr);
|
DEBUG("Parsing Integer Literal\n", 0)
|
||||||
}
|
int number = prsnum(0xFFFF); //Parse Number into value
|
||||||
|
if (asmxpr) {
|
||||||
|
sprintf(value, "#%d", number & 0xFF); asmlin("LDX", value);
|
||||||
|
sprintf(value, "#%d", number >> 8); asmlin("LDY", value);
|
||||||
|
}
|
||||||
|
} else if (isalph()) {
|
||||||
|
prsvar(FALSE, TRUE);
|
||||||
|
if (valtyp == FUNCTION) {
|
||||||
|
strcpy(term, value);
|
||||||
|
prsfnc(0); //Parse Expression Function
|
||||||
|
} else if (valtyp == STRUCTURE) {
|
||||||
|
prsmbr(value);
|
||||||
|
if (vartyp != VTINT) ERROR("Illegal Member %s In Integer Expression", value, EXIT_FAILURE)
|
||||||
|
} else if (valtyp == VARIABLE && vartyp == VTINT) {
|
||||||
|
if (asmxpr) prcvri(); //Process Integer Variable
|
||||||
|
} else {
|
||||||
|
ERROR("Illegal Variable %s In Integer Expression", value, EXIT_FAILURE)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ERROR("Expected Integer Value or Function\n", 0, EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect(trmntr);
|
||||||
|
}
|
||||||
|
60
src/expr.h
60
src/expr.h
@ -1,30 +1,30 @@
|
|||||||
/**********************************
|
/**********************************
|
||||||
* C02 Expession Parsing Routines *
|
* C02 Expession Parsing Routines *
|
||||||
**********************************/
|
**********************************/
|
||||||
|
|
||||||
enum adacts {ADNONE, ADLDYX, ADPUSH};
|
enum adacts {ADNONE, ADLDYX, ADPUSH};
|
||||||
|
|
||||||
char term[255]; //Term parsed from equation
|
char term[255]; //Term parsed from equation
|
||||||
|
|
||||||
char oprstk[MAXTRM]; //Operator Stack
|
char oprstk[MAXTRM]; //Operator Stack
|
||||||
char trmstk[MAXTRM][VARLEN+1]; //Function/Index Terms Stack
|
char trmstk[MAXTRM][VARLEN+1]; //Function/Index Terms Stack
|
||||||
int trmidx; //Next Index in Stack
|
int trmidx; //Next Index in Stack
|
||||||
|
|
||||||
int trmcnt; //Number of total terms in current expression
|
int trmcnt; //Number of total terms in current expression
|
||||||
|
|
||||||
int chkadr(int adract, int alwstr); //Check for and Process Address or String
|
int chkadr(int adract, int alwstr); //Check for and Process Address or String
|
||||||
void chkidx(); //Check for, Parse, and Process Index
|
void chkidx(); //Check for, Parse, and Process Index
|
||||||
int prcftm(int alwint); //Process First Term
|
int prcftm(int alwint); //Process First Term
|
||||||
void prcvri(void); //Process Integer Variable
|
void prcvri(void); //Process Integer Variable
|
||||||
int prcvar(int alwint); //Process Variable in Term
|
int prcivr(int alwint); //Process Integer Variable in Term
|
||||||
void prsadr(int adract); //Parse and Compile Address of Operator
|
void prsadr(int adract); //Parse and Compile Address of Operator
|
||||||
void prsbop(void); //Parse Byte Operator
|
void prsbop(void); //Parse Byte Operator
|
||||||
void prsval(int alwreg, int alwcon); //Parse Value
|
void prsval(int alwreg, int alwcon); //Parse Value
|
||||||
void prsfnc(char trmntr); //Parse function call
|
void prsfnc(char trmntr); //Parse function call
|
||||||
void prsfpr(char trmntr); //Parse Function Paraeters or Return
|
void prsfpr(char trmntr); //Parse Function Paraeters or Return
|
||||||
void prsidx(); //Parse Array Index
|
void prsidx(); //Parse Array Index
|
||||||
int prstrm(int alwint); //Parse Term in Expression
|
int prstrm(int alwint); //Parse Term in Expression
|
||||||
void prsrxp(char trmntr); //Parse Rest of Expression
|
void prsrxp(char trmntr); //Parse Rest of Expression
|
||||||
int prsxpf(char trmntr); //Parse Expression in Function Call
|
int prsxpf(char trmntr); //Parse Expression in Function Call
|
||||||
void prsxpr(char trmntr); //Parse Expression
|
void prsxpr(char trmntr); //Parse Expression
|
||||||
void prsxpi(char trmntr, int asmxpr); //Parse Integer Expression
|
void prsxpi(char trmntr, int asmxpr); //Parse Integer Expression
|
||||||
|
Loading…
Reference in New Issue
Block a user