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

Generate error when using integer as array

This commit is contained in:
Curtis F Kaylor 2019-12-21 12:00:43 -05:00
parent 8203daed7d
commit 66bf02a227

View File

@ -19,15 +19,15 @@
/* Begin Program Block */ /* Begin Program Block */
void bgnblk(char blkchr) { void bgnblk(char blkchr) {
DEBUG("Beginning program block\n", 0) DEBUG("stmnt.bgnblk: Beginning program block\n", 0)
if (blkchr) { if (blkchr) {
DEBUG("Expecting Block Delimiter '%c'\n", blkchr); DEBUG("stmnt.bgnblk: Expecting Block Delimiter '%c'\n", blkchr);
expect(blkchr); expect(blkchr);
inblck = TRUE; inblck = TRUE;
} }
else else
inblck = look('{'); inblck = look('{');
DEBUG("Set INBLCK to %d\n", inblck) DEBUG("stmnt.bgnblk: Set INBLCK to %d\n", inblck)
setblk(inblck); setblk(inblck);
} }
@ -35,9 +35,9 @@ void bgnblk(char blkchr) {
* Args: blkflg: End of Multiline Block */ * Args: blkflg: End of Multiline Block */
void endblk(int blkflg) { void endblk(int blkflg) {
int lbtype; int lbtype;
DEBUG("Ending program block with flag %d\n", blkflg) DEBUG("stmnt.endblk: Ending program block with flag %d\n", blkflg)
expect('}'); //Block End Character expect('}'); //Block End Character
DEBUG("Found inblck set to %d\n", inblck) DEBUG("stmnt.endblk: Found inblck set to %d\n", inblck)
if (inblck != blkflg) ERROR("Encountered '}' without matching '{'\n", 0, EXIT_FAILURE) if (inblck != blkflg) ERROR("Encountered '}' without matching '{'\n", 0, EXIT_FAILURE)
lbtype = poplbl(); lbtype = poplbl();
if (lbtype == LTDO) pdowhl(); //Parse While at End of Do Loop if (lbtype == LTDO) pdowhl(); //Parse While at End of Do Loop
@ -82,7 +82,7 @@ void setasn(char *name, char ispntr) {
} }
void prcixy(char rgstr, char* idx, int ivt) { void prcixy(char rgstr, char* idx, int ivt) {
DEBUG("Processing %c register indexed assignment\n", rgstr) DEBUG("stmnt.prcixy: Processing %c register indexed assignment\n", rgstr)
if (strlen(idx)) { //Process X variable Index if (strlen(idx)) { //Process X variable Index
if (ivt != LITERAL) { if (ivt != LITERAL) {
asmlin("PHA", ""); //Save Accumulator asmlin("PHA", ""); //Save Accumulator
@ -101,14 +101,14 @@ void prcixy(char rgstr, char* idx, int ivt) {
/* Process Assignment of X and Y variables */ /* Process Assignment of X and Y variables */
void prcaxy(void) { void prcaxy(void) {
DEBUG("Processing X assignment variable '%s'\n", xsnvar) DEBUG("stmnt.prcaxy: Processing X assignment variable '%s'\n", xsnvar)
if (xsnvar[0]) { if (xsnvar[0]) {
setasn(xsnvar, FALSE); setasn(xsnvar, FALSE);
if (strlen(xsnidx)) prcixy('X', xsnidx, xsnivt); //Process Index if (strlen(xsnidx)) prcixy('X', xsnidx, xsnivt); //Process Index
else asmlin("STX", word); //Store Return Value else asmlin("STX", word); //Store Return Value
xsnvar[0] = 0; xsnvar[0] = 0;
} }
DEBUG("Processing Y assignment variable '%s'\n", ysnvar) DEBUG("stmnt.axy: Processing Y assignment variable '%s'\n", ysnvar)
if (ysnvar[0]) { if (ysnvar[0]) {
setasn(ysnvar, FALSE); setasn(ysnvar, FALSE);
if (strlen(ysnidx)) prcixy('Y', ysnidx, ysnivt); //Process Index if (strlen(ysnidx)) prcixy('Y', ysnidx, ysnivt); //Process Index
@ -123,11 +123,11 @@ void prcasn(char trmntr, char ispntr) {
if (look('(')) prssif(trmntr); //Parse Shortcut If if (look('(')) prssif(trmntr); //Parse Shortcut If
else prsxpr(trmntr); //Parse Expression else prsxpr(trmntr); //Parse Expression
prcaxy(); //Process X and Y assignments prcaxy(); //Process X and Y assignments
DEBUG("Checking if '%s' is a register\n", asnvar) DEBUG("stmnt.prcasn: Checking if '%s' is a register\n", asnvar)
if (strcmp(asnvar, "X")==0) asmlin("TAX", ""); if (strcmp(asnvar, "X")==0) asmlin("TAX", "");
else if (strcmp(asnvar, "Y")==0) asmlin("TAY", ""); else if (strcmp(asnvar, "Y")==0) asmlin("TAY", "");
else if (strcmp(asnvar, "A")==0) return; else if (strcmp(asnvar, "A")==0) return;
DEBUG("Processing assignment variable '%s'\n", asnvar) DEBUG("stmnt.prcasn: Processing assignment variable '%s'\n", asnvar)
setasn(asnvar, ispntr); setasn(asnvar, ispntr);
if (asnidx[0]) { if (asnidx[0]) {
if (ispntr) prcptx(asnidx); //Process Pointer Index if (ispntr) prcptx(asnidx); //Process Pointer Index
@ -142,7 +142,8 @@ void prcasn(char trmntr, char ispntr) {
/* Process Integer Assignment */ /* Process Integer Assignment */
void prcasi(char trmntr) { void prcasi(char trmntr) {
DEBUG("Processing Integer Assignment\n", 0); DEBUG("stmnt.prcasi: Processing Integer Assignment\n", 0);
if (look('[')) ERROR("Illegal use of Integer Variable %s as Array\n", vrname, EXIT_FAILURE)
expect('='); expect('=');
strcpy(xsnvar, vrname); //Set Assignment LSB strcpy(xsnvar, vrname); //Set Assignment LSB
strcpy(ysnvar, vrname); strcat(ysnvar, "+1"); //Set Assignment MSB strcpy(ysnvar, vrname); strcat(ysnvar, "+1"); //Set Assignment MSB
@ -183,7 +184,7 @@ int prcava(char *name, char trmntr, char ispntr) {
/* Process Assignment Variable(s) */ /* Process Assignment Variable(s) */
void prcavr(char trmntr) { void prcavr(char trmntr) {
chksym(TRUE, FALSE, word); chksym(TRUE, FALSE, word);
DEBUG("Processing assignment of variable %s\n", word); DEBUG("stmnt.prcavr: Processing assignment of variable %s\n", word);
strcpy(vrname, word); //save variable to assign to strcpy(vrname, word); //save variable to assign to
if (valtyp == STRUCTURE) prsmbr(vrname); //Updates word and vartyp if (valtyp == STRUCTURE) prsmbr(vrname); //Updates word and vartyp
if (vartyp == VTINT) { if (vartyp == VTINT) {
@ -192,18 +193,18 @@ void prcavr(char trmntr) {
return; return;
} }
if (prcava(vrname, trmntr, FALSE)) return; if (prcava(vrname, trmntr, FALSE)) return;
if (look(',')) { if (look(',')) {
if (asntyp == REGISTER) ERROR("Register %s not allowed in plural assignment\n", asnvar, EXIT_FAILURE) if (asntyp == REGISTER) ERROR("Register %s not allowed in plural assignment\n", asnvar, EXIT_FAILURE)
prsvar(FALSE, FALSE); //get variable name prsvar(FALSE, FALSE); //get variable name
strcpy(ysnvar, word); strcpy(ysnvar, word);
DEBUG("Set STY variable to %s\n", ysnvar) DEBUG("stmnt.prcvar: Set STY variable to %s\n", ysnvar)
if (valtyp == ARRAY) ysnivt = getidx(ysnidx); //Get Array Index and Type if (valtyp == ARRAY) ysnivt = getidx(ysnidx); //Get Array Index and Type
else ysnidx[0] = 0; else ysnidx[0] = 0;
DEBUG("Set STY index to '%s'", ysnidx) DETAIL(" and type to %d\n", ysnivt) DEBUG("stmnt.prcvar: Set STY index to '%s'", ysnidx) DETAIL(" and type to %d\n", ysnivt)
if (look(',')) { if (look(',')) {
prsvar(FALSE, FALSE); //get variable name prsvar(FALSE, FALSE); //get variable name
strcpy(xsnvar, word); strcpy(xsnvar, word);
DEBUG("Set STX variable to %s\n", xsnvar) DEBUG("stmnt.prcvar: 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 if (valtyp == ARRAY) xsnivt = getidx(xsnidx); //Get Array Index and Type
else xsnidx[0] = 0; else xsnidx[0] = 0;
@ -247,7 +248,7 @@ void prsasn(char trmntr) {
if (match('*')) prcasp(trmntr); if (match('*')) prcasp(trmntr);
else { else {
getwrd(); //Get Variable to be Assigned getwrd(); //Get Variable to be Assigned
DEBUG("Parsing assignment of word %s\n", word) DEBUG("stmnt.prsasn: Parsing assignment of word %s\n", word)
prcavr(trmntr); prcavr(trmntr);
} }
} }
@ -255,16 +256,16 @@ void prsasn(char trmntr) {
/* parse and compile 'break'/'continue' statement */ /* parse and compile 'break'/'continue' statement */
void pbrcnt(int lbflag) void pbrcnt(int lbflag)
{ {
DEBUG("Parsing BREAK/CONTINUE statement\n", 0) DEBUG("stmnt.pbrcnt: Parsing BREAK/CONTINUE statement\n", 0)
if (lstlbl(lbflag) < 0) ERROR("Break/continue statement outside of loop\n", 0, EXIT_FAILURE) if (lstlbl(lbflag) < 0) ERROR("Break/continue statement outside of loop\n", 0, EXIT_FAILURE)
DEBUG("Found Label '%s'\n", tmplbl) DEBUG("stmnt.pbrcnt: Found Label '%s'\n", tmplbl)
asmlin("JMP", tmplbl); asmlin("JMP", tmplbl);
expect(';'); expect(';');
} }
/* parse and compile 'do' statement */ /* parse and compile 'do' statement */
void pdo(void) { void pdo(void) {
DEBUG("Parsing DO statement '%c'\n", nxtchr) DEBUG("stmnt.pdo: Parsing DO statement '%c'\n", nxtchr)
newlbl(endlbl); //Create End Label newlbl(endlbl); //Create End Label
pshlbl(LTDWHL, endlbl); //and Push onto Stack pshlbl(LTDWHL, endlbl); //and Push onto Stack
reqlbl(loplbl); //Get or Create/Set Loop Label reqlbl(loplbl); //Get or Create/Set Loop Label
@ -274,7 +275,7 @@ void pdo(void) {
/* parse and compile 'while' after 'do' statement */ /* parse and compile 'while' after 'do' statement */
void pdowhl(void) { void pdowhl(void) {
DEBUG("Parsing WHILE after DO '%c'\n", nxtchr) DEBUG("stmnt.dowhl: Parsing WHILE after DO '%c'\n", nxtchr)
getwrd(); //Check for While getwrd(); //Check for While
if (!wordis("WHILE")) expctd("while statement"); if (!wordis("WHILE")) expctd("while statement");
expect('('); expect('(');
@ -288,7 +289,7 @@ void pdowhl(void) {
/* parse and compile for statement */ /* parse and compile for statement */
void pfor(void) { void pfor(void) {
DEBUG("Parsing FOR statement '%c'\n", nxtchr); DEBUG("stmnt.pfor: Parsing FOR statement '%c'\n", nxtchr);
expect('('); expect('(');
prsasn(';'); //Process Initial Assignment prsasn(';'); //Process Initial Assignment
newlbl(forlbl); //Create For Loop Conditional Label newlbl(forlbl); //Create For Loop Conditional Label
@ -309,7 +310,7 @@ void pfor(void) {
/* parse and compile if statement */ /* parse and compile if statement */
void pif(void) { void pif(void) {
DEBUG("Parsing IF statement\n", 0) DEBUG("stmnt.pif: Parsing IF statement\n", 0)
expect('('); expect('(');
newlbl(cndlbl); //Create New Label newlbl(cndlbl); //Create New Label
pshlbl(LTIF,cndlbl); //Push Onto Stack pshlbl(LTIF,cndlbl); //Push Onto Stack
@ -319,7 +320,7 @@ void pif(void) {
/* parse and compile else statement */ /* parse and compile else statement */
void pelse(void) { void pelse(void) {
DEBUG("Parsing ELSE statement\n", 0) DEBUG("stmnt.pelse: Parsing ELSE statement\n", 0)
strcpy(lbltmp, lblasm); //Save Line Label strcpy(lbltmp, lblasm); //Save Line Label
lblasm[0] = 0; //and Clear It lblasm[0] = 0; //and Clear It
if (toplbl(skplbl) != LTELSE) { //Get Chained ELSE Label or if (toplbl(skplbl) != LTELSE) { //Get Chained ELSE Label or
@ -333,9 +334,9 @@ void pelse(void) {
/* parse and compile if statement */ /* parse and compile if statement */
void pgoto(void) { void pgoto(void) {
DEBUG("Parsing GOTO statement\n", 0) DEBUG("stmnt.pgoto: Parsing GOTO statement\n", 0)
if (look('(')) { if (look('(')) {
DEBUG("Processing Indirect GOTO\n", 0); DEBUG("stmnt.pgoto: Processing Indirect GOTO\n", 0);
prsxpi(')', FALSE); //Parse Integer Expression w/o Assembly prsxpi(')', FALSE); //Parse Integer Expression w/o Assembly
sprintf(word, "(%s)", value); //Set Indirect Argument sprintf(word, "(%s)", value); //Set Indirect Argument
} else { } else {
@ -347,9 +348,9 @@ void pgoto(void) {
/* parse and compile inline statement */ /* parse and compile inline statement */
void pinlne(void) { void pinlne(void) {
DEBUG("Parsing INLINE statement\n", 0) DEBUG("stmnt.pinlne: Parsing INLINE statement\n", 0)
do { do {
DEBUG("Parsing inline parameter\n", 0) DEBUG("stmnt.pinlne: Parsing inline parameter\n", 0)
if (look('&')) { if (look('&')) {
reqvar(FALSE); //Get Variable Name reqvar(FALSE); //Get Variable Name
strcpy(word, "<"); strcpy(word, "<");
@ -379,9 +380,15 @@ void pinlne(void) {
expect(';'); expect(';');
} }
/* parse and compile label */
void plabel(void) {
prslab(); //Parse Program Label
setlbl(word); //Set Label to Emit
}
/* parse and compile pop statement */ /* parse and compile pop statement */
void ppop(void) { void ppop(void) {
DEBUG("Parsing POP statement\n", 0) DEBUG("stmnt.ppop: Parsing POP statement\n", 0)
do { do {
if (look('.')) term[0]=0; if (look('.')) term[0]=0;
else { else {
@ -397,7 +404,7 @@ void ppop(void) {
/* parse and compile push statement */ /* parse and compile push statement */
void ppush(void) { void ppush(void) {
DEBUG("Parsing PUSH statement\n", 0) DEBUG("stmnt.ppush: Parsing PUSH statement\n", 0)
do { do {
if (!chkadr(ADPUSH, TRUE)) { if (!chkadr(ADPUSH, TRUE)) {
prsxpr(0); //Parse Expression prsxpr(0); //Parse Expression
@ -409,7 +416,7 @@ void ppush(void) {
/* parse and compile return statement */ /* parse and compile return statement */
void pretrn(int INTRPT) { void pretrn(int INTRPT) {
DEBUG("Parsing RETURN statement\n", 0) DEBUG("stmnt.pretrn: Parsing RETURN statement\n", 0)
skpspc(); skpspc();
if (INTRPT) { if (INTRPT) {
expect(';'); expect(';');
@ -423,7 +430,7 @@ void pretrn(int INTRPT) {
/* parse and compile select statement */ /* parse and compile select statement */
void pslct(void) { void pslct(void) {
DEBUG("Parsing SELECT statement\n", 0) DEBUG("stmnt.pslct: Parsing SELECT statement\n", 0)
expect('('); expect('(');
prsxpr(')'); //Parse Expression prsxpr(')'); //Parse Expression
newlbl(endlbl); //Create New Label newlbl(endlbl); //Create New Label
@ -435,7 +442,7 @@ void pslct(void) {
/* process end of case block */ /* process end of case block */
void ecase(void) { void ecase(void) {
DEBUG("Processing end of CASE block\n", 0) DEBUG("stmnt.ecase: Processing end of CASE block\n", 0)
if (poplbl(cndlbl) != LTCASE) ERROR("%s not at end of CASE block\n", word, EXIT_FAILURE) if (poplbl(cndlbl) != LTCASE) ERROR("%s not at end of CASE block\n", word, EXIT_FAILURE)
if (toplbl(endlbl) != LTSLCT) ERROR("Illegal nesting in SELECT statement\n", 0, EXIT_FAILURE) if (toplbl(endlbl) != LTSLCT) ERROR("Illegal nesting in SELECT statement\n", 0, EXIT_FAILURE)
asmlin("JMP", endlbl); //Emit jump over default case asmlin("JMP", endlbl); //Emit jump over default case
@ -474,14 +481,17 @@ void pdflt(void) {
/* parse and compile while statement */ /* parse and compile while statement */
void pwhile(void) { void pwhile(void) {
DEBUG("Parsing WHILE statement '%c'\n", nxtchr) DEBUG("stmnt.pwhile: Parsing WHILE statement '%c'\n", nxtchr)
expect('('); expect('(');
newlbl(endlbl); //Create End Label newlbl(endlbl); //Create End Label
DEBUG("stmnt.pwhile: Created End Label %s, ", endlbl);
pshlbl(LTEND, endlbl); //and Push onto Stack pshlbl(LTEND, endlbl); //and Push onto Stack
reqlbl(loplbl); //Get or Create/Set Loop Label reqlbl(loplbl); //Get or Create/Set Loop Label
DETAIL("Loop Label %s, ", loplbl);
pshlbl(LTLOOP, loplbl); //Push onto Stack pshlbl(LTLOOP, loplbl); //Push onto Stack
if (!look(')')) { if (!look(')')) {
newlbl(cndlbl); //Create Conditional Skip Label newlbl(cndlbl); //Create Conditional Skip Label
DETAIL("Conditional Label %s, ", cndlbl);
prscnd(')', TRUE); //Parse Conditional Expession prscnd(')', TRUE); //Parse Conditional Expession
asmlin("JMP", endlbl); //Emit Jump to End of Loop asmlin("JMP", endlbl); //Emit Jump to End of Loop
setlbl(cndlbl); //and Set Label to Emit on Next Line setlbl(cndlbl); //and Set Label to Emit on Next Line
@ -503,7 +513,7 @@ void prsfns(void) {
/* parse and compile identifier (variable or function call) */ /* parse and compile identifier (variable or function call) */
void prssym(void) { void prssym(void) {
DEBUG("Parsing Identifier %s\n", word) DEBUG("stmnt.prssym: Parsing Identifier %s\n", word)
valtyp = gettyp(); valtyp = gettyp();
if (valtyp == FUNCTION) prsfns(); //Parse Statement Function Call if (valtyp == FUNCTION) prsfns(); //Parse Statement Function Call
else prcavr(';'); //Parse Assignment else prcavr(';'); //Parse Assignment
@ -511,69 +521,33 @@ void prssym(void) {
/* parse and compile program statement */ /* parse and compile program statement */
void pstmnt(void) { void pstmnt(void) {
DEBUG("Parsing statement '%s'\n", word) DEBUG("stmnt.pstmnt: Parsing statement '%s'\n", word)
if (wordis("DO")) { //Keywords that start a new block
pdo(); if (wordis("DO")) { pdo(); return; }
return; if (wordis("ELSE")) { pelse(); return; }
} if (wordis("FOR")) { pfor(); return; }
if (wordis("ELSE")) { if (wordis("IF")) { pif(); return; }
pelse(); if (wordis("SWITCH")) ERROR("SWITCH not implemented. Use SELECT.\n", word, EXIT_FAILURE)
return; if (wordis("SELECT")) { pslct(); return; }
} if (wordis("CASE")) { pcase(); return; }
if (wordis("FOR")) { if (wordis("DEFAULT")) { pdflt(); return; }
pfor(); if (wordis("WHILE")) { pwhile(); return; }
return; if (match(':')) { plabel(); return; }
}
if (wordis("IF")) { //Keywords that don't start a block
pif(); if (wordis("ASM")) pasm();
return; else if (wordis("BREAK")) pbrcnt(LFEND);
} else if (wordis("CONTINUE")) pbrcnt(LFBGN);
if (wordis("SWITCH")) { else if (wordis("GOTO")) pgoto();
ERROR("SWITCH not implemented. Use SELECT.\n", word, EXIT_FAILURE) else if (wordis("INLINE")) pinlne();
} else if (wordis("POP")) ppop();
if (wordis("SELECT")) { else if (wordis("PUSH")) ppush();
pslct(); else if (wordis("RESUME")) pretrn(TRUE);
return; else if (wordis("RETURN")) pretrn(FALSE);
} else prssym();
if (wordis("CASE")) {
pcase();
return;
}
if (wordis("DEFAULT")) {
pdflt();
return;
}
if (wordis("WHILE")) {
pwhile();
return;
}
if(match(':')) {
prslab(); //Parse Program Label
setlbl(word); //Set Label to Emit
return;
}
if (wordis("ASM")) pasm();
else if (wordis("BREAK"))
pbrcnt(LFEND);
else if (wordis("CONTINUE"))
pbrcnt(LFBGN);
else if (wordis("GOTO"))
pgoto();
else if (wordis("INLINE"))
pinlne();
else if (wordis("POP"))
ppop();
else if (wordis("PUSH"))
ppush();
else if (wordis("RESUME"))
pretrn(TRUE);
else if (wordis("RETURN"))
pretrn(FALSE);
else
prssym();
if (lblcnt && !inblck) { if (lblcnt && !inblck) {
DEBUG("Ending implied block\n", 0) DEBUG("stmnt.pstmnt: Ending implied block\n", 0)
if (poplbl() == LTDO) if (poplbl() == LTDO) pdowhl(); //Parse While at End of Do Loop
pdowhl(); //Parse While at End of Do Loop
} }
} }