1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +00:00

Fixed bug with constant parsing and array indexes.

This commit is contained in:
Curtis F Kaylor 2018-01-28 13:27:33 -05:00
parent 883c927301
commit 0e12946dd2
6 changed files with 77 additions and 49 deletions

6
c02.c
View File

@ -36,6 +36,7 @@ void init()
curlin = 0;
inpfil = srcfil;
strcpy(inpnam, srcnam);
strcpy(incdir, "../include/");
alcvar = TRUE;
inblck = FALSE;
xstmnt[0] = 0;
@ -43,12 +44,11 @@ void init()
nxtptr = 0;
vrwrtn = FALSE;
zpaddr = 0;
invasc = FALSE;
}
/* Reads and parses the next Word in Source File */
pword()
void pword()
{
lsrtrn = FALSE; //Clear RETURN flag
getwrd();

5
expr.c
View File

@ -51,7 +51,7 @@ void chkidx()
prsidx();
if (valtyp == CONSTANT) {
strcat(term, "+");
strcat(term, value);
strcat(term, word);
}
else if (strcmp(value, "Y")==0)
strcat(term, ",Y");
@ -120,7 +120,7 @@ void prsstr(adract)
DEBUG("Parsing anonymous string\n", 0);
newlbl(vrname); //Generate Variable Name
value[0] = 0; //Use Variable Size 0
setvar(VTCHAR); //Set Variable Name, Type, and Size
setvar(MTNONE, VTCHAR); //Set Variable Name, Type, and Size
prsdts(); //Parse Data String
setdat(); //Set Variable Data
varcnt++; //Increment Variable Counter
@ -187,7 +187,6 @@ void prsftm()
}
if (wordis("A"))
return;
//if (ispopr()) oper = prspst(0, term); //Check for Post-Operator
if (wordis("X"))
asmlin("TXA", "");
else if (wordis("Y"))

View File

@ -24,7 +24,7 @@ void pincnm()
skpspc();
dlmtr = getnxt();
if (dlmtr == '<') {
strcpy(incnam, "include/");
strcpy(incnam, incdir);
inclen = strlen(incnam);
dlmtr = '>';
}
@ -68,6 +68,20 @@ void pdefin()
DEBUG("Defined as '%s'\n", value);
}
/* Parse ASCII Subdirective */
void pascii()
{
getwrd(); //Get Pragma Subdirective
if (wordis("INVERT")) {
invasc = TRUE;
}
else {
printf("Unrecognized option '%s'\n", word);
exterr(EXIT_FAILURE);
}
}
/* Parse Origin Subdirective */
void porign()
{
@ -97,7 +111,9 @@ void pprgma()
{
getwrd(); //Get Pragma Subdirective
DEBUG("Parsing pragma directive '%s'\n", word);
if (wordis("ORIGIN"))
if (wordis("ASCII"))
pascii(); //Parse Ascii
else if (wordis("ORIGIN"))
porign(); //Parse Origin
else if (wordis("VARTABLE"))
pvrtbl(); //Parse Vartable

38
parse.c
View File

@ -33,6 +33,9 @@ int isoper() {return TF(strchr("+-&|^", nxtchr));}
int ispopr() {return TF(strchr("+-<>", nxtchr));}
int isxpre() {return TF(isvpre() || match('-'));}
/* Conversion Functions */
char invchr(char c) {return isalpha(c)?(islower(c)?toupper(c):tolower(c)):c;}
/* if Word is s then return TRUE else return FALSE*/
int wordis(char *s)
{
@ -165,6 +168,7 @@ void getstr() {
if (match('\\'))
escnxt = TRUE;
else
if (invasc) nxtchr = invchr(nxtchr);
word[wrdlen++] = nxtchr;
skpchr();
}
@ -228,12 +232,12 @@ int prshex()
int wrdlen = 0;
int digit;
int number = 0;
DEBUG("Parsing hexadecimal constant\n", 0);
DEBUG("Parsing hexadecimal constant '", 0);
if (!match('$'))
expctd("hexadecimal number");
word[wrdlen++] = getnxt();
while (ishexd()) {
DEBUG("Found hex digit '%c'\n", nxtchr);
DETAIL("%c", nxtchr);
word[wrdlen++] = nxtchr;
if (isdec())
digit = nxtchr - '0';
@ -242,6 +246,7 @@ int prshex()
number = number * 16 + digit;
skpchr();
}
DETAIL("'\n", 0);
word[wrdlen] = 0;
return (number);
}
@ -261,6 +266,7 @@ int prschr()
word[wrdlen++] = getnxt();
c = getnxt();
DEBUG("Extracted character %c\n", c);
if (invasc) c = invchr(c);
word[wrdlen++] = c;
expect('\'');
word[wrdlen++] = '\'';
@ -290,8 +296,8 @@ int prsnum(int maxval)
default:
number = prsdec();
}
DEBUG("Parsed number '%s'\n", word);
DEBUG("with value '%d'\n", number);
DEBUG("Parsed number '%s' ", word);
DETAIL("with value '%d'\n", number);
if (number > maxval) {
ERROR("Out of bounds constant '%d';\n", number, EXIT_FAILURE);
@ -333,8 +339,12 @@ int prsdef()
/* Parse numeric constant *
* Args: maxval - maximum allowed value *
* Sets: cnstnt - the constant (as an integer) *
* value - the constant (as a string) *
* valtyp - value type (CONSTANT) */
* value - the constant (as asm arg) *
* valtyp - value type (CONSTANT) *
* word - constant (as a string) *
* Note: Value is converted to hexadecimal *
* because DASM uses the format 'c for *
* character arguments instead of 'c' */
void prscon()
{
skpspc();
@ -344,6 +354,7 @@ void prscon()
cnstnt = prsbyt();
valtyp = CONSTANT;
ACMNT(word);
strcpy(word, value); //Patch for DASM
strcpy(value, "#");
strcat(value, word);
DEBUG("Generated constant '%s'\n", value);
@ -382,9 +393,18 @@ void prsopr()
skpspc();
}
void prcpst(char* name)
void prcidx(char *name, char *index)
{
if (strlen(index)) {
asmlin("LDX", index);
strcat(name,",X");
}
}
void prcpst(char* name, char *index)
{
DEBUG("Processing post operation '%c'\n", oper);
prcidx(name, index);
switch(oper)
{
case '+':
@ -434,7 +454,7 @@ void prcpst(char* name)
}
/* Parse Post Operator */
int prspst(char trmntr, char* name) {
int prspst(char trmntr, char* name, char* index) {
oper = getnxt();
CCMNT(oper);
DEBUG("Checking for post operation '%c'\n", oper);
@ -442,7 +462,7 @@ int prspst(char trmntr, char* name) {
skpchr();
CCMNT(oper);
expect(trmntr);
prcpst(name); //Process Post-Op
prcpst(name, index); //Process Post-Op
oper = 0;
}
else {

28
stmnt.c
View File

@ -33,29 +33,6 @@ void prssif(char trmntr) {
void prcasn(char trmntr)
{
expect('=');
/*
if (strlen(asnvar) == 1 && strchr("XY", asnvar[0])) {
DEBUG("Processing assignment of register '%s'\n", asnvar);
prsval(TRUE); //Get value to assign
expect(trmntr);
if (strlen(value) == 1 && strchr("XY", value[0])) {
ERROR("Illegal Reference to Register %s\n", value, EXIT_FAILURE);
}
if (asnvar[0] == 'X') {
if (strcmp(value, "A") == 0)
asmlin("TAX", "");
else
asmlin("LDX", value);
}
else {
if (strcmp(value, "A") == 0)
asmlin("TAY", "");
else
asmlin("LDY", value);
}
return;
}
*/
DEBUG("Processing assignment of variable '%s'\n", asnvar);
if (look('('))
prssif(trmntr); //Parse Shortcut If
@ -99,12 +76,15 @@ void prcvar(char trmntr)
if (valtyp == ARRAY) {
prsidx(); //Parse Array Index
asnivt = valtyp;
if (asnivt == CONSTANT)
strncpy(asnidx, word, VARLEN);
else
strncpy(asnidx, value, VARLEN);
}
else
asnidx[0] = 0;
if (ispopr()) {
if (prspst(trmntr, asnvar)) //Parse Post Operator
if (prspst(trmntr, asnvar, asnidx)) //Parse Post Operator
expctd("post operator");
}
else

23
vars.c
View File

@ -139,6 +139,7 @@ void setdat()
}
datlen[varcnt] = dlen;
dattyp[varcnt] = dtype;
DEBUG("Total data alllocated: %d bytes\n", dsize);
}
/* Parse and store variable data */
@ -164,13 +165,14 @@ void prsdat()
/* Add Variable to Variable table *
* Uses: word - variable name *
* value - variable size */
void setvar(int t)
void setvar(int m, int t)
{
DEBUG("Adding variable '%s'\n", word);
DEBUG("Added variable '%s' ", word);
strncpy(varnam[varcnt], vrname, VARLEN);
varmod[varcnt] = m;
vartyp[varcnt] = t;
strncpy(varsiz[varcnt], value, 3);
DEBUG("Added at index %d\n", varcnt);
DETAIL("at index %d\n", varcnt);
}
/* Parse and Compile Variable Declaration *
@ -190,7 +192,7 @@ void addvar(int m, int t)
}
else
pvarsz(); //Check for Array Declaration and Get Size
setvar(t); //Add to Variable Table
setvar(m, t); //Add to Variable Table
if (m != MTZP)
prsdat(); //Parse Variable Data
varcnt++; //Increment Variable Counter
@ -252,6 +254,8 @@ void pdecl(int m, int t)
break;
}
expect(';');
DEBUG("Variable Declaration Completed\n", 0);
SCMNT(""); //Clear Assembler Comment
}
/* Check for and Parse Type Keyword */
@ -264,6 +268,7 @@ int ptype(int m)
pdecl(m, VTCHAR); //Parse 'char' declaration
else
result = FALSE;
//DEBUG("Returning %d from function ptype\n", result)'
return result;
}
@ -272,7 +277,11 @@ int pmodfr()
{
DEBUG("Parsing modifier '%s'\n", word);
int result = TRUE;
if (wordis("ZEROPAGE")) {
if (wordis("ALIGNED")) {
getwrd();
ptype(MTALGN);
}
else if (wordis("ZEROPAGE")) {
getwrd();
ptype(MTZP);
}
@ -308,6 +317,10 @@ void vartbl()
DEBUG("Set Label to '%s'\n", lblasm);
if (strcmp(varsiz[i], "*") == 0)
continue;
if (varmod[i] == MTALGN) {
DEBUG("Alligning variable '%s'\n", varnam[i]);
asmlin(ALNOP, "256");
}
if (datlen[i])
vardat(i); //Write Variable Data
else if (strlen(varsiz[i]) > 0) {