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

Allow & operator to work with Labels

This commit is contained in:
Curtis F Kaylor 2019-05-18 22:24:30 -04:00
parent 2f297edbcd
commit d08e23d8d0
9 changed files with 69 additions and 15 deletions

View File

@ -193,6 +193,7 @@ int main(int argc, char *argv[]) {
logstc();
logcon();
loglab();
clssrc(); //Close Source File
clsout(); //Close Output File

View File

@ -16,7 +16,8 @@
#define DATASPC 4096 //Space to Allocate for Variable Data
#define SUBMAX 4 //Maximum Number of Sub Directories
#define LABLEN 6 //Maximum Program Label Length
#define MAXLAB 255 //Maximum Number of Program Labels
#define LBLLEN 6 //Maximum Label Length
#define LBLFMT "L_%04d" //Label Format

View File

@ -147,7 +147,11 @@ void prcadr(int adract, char* symbol) {
void prsadr(int adract) {
DEBUG("Parsing address\n", 0)
if (isnpre()) prsnum(0xFFFF);
else prsvar(FALSE, TRUE);
else {
getwrd();
if (fndlab(word)) strcpy(value, word);
else prsvrw(FALSE, TRUE);
}
if (adract) prcadr(adract, value); //Compile Address Reference
else strcpy(word, value); //Save for Calling Routine
}

View File

@ -176,7 +176,8 @@ void pincdr(void) {
/* Parse Header Word */
void phdwrd(void) {
getwrd();
if (!ptype(MTNONE))
if (match(':')) prslab();
else if (!ptype(MTNONE))
ERROR("Unexpected word '%s' in header\n", word, EXIT_FAILURE)
}

View File

@ -9,9 +9,36 @@
#include <errno.h>
#include "common.h"
#include "files.h"
#include "asm.h"
#include "parse.h"
#include "label.h"
#include "vars.h"
/* Add New Program Label */
void addlab(char *name) {
if (fndvar(name)) ERROR("Label %s conflicts with variable with same name", name, EXIT_FAILURE)
if (fndlab(name)) ERROR("Duplicate program label %s\n", name, EXIT_FAILURE)
DEBUG("Adding Program Label %s ", name) DEBUG("at index %d\n", labcnt)
strcpy(labnam[labcnt++], name);
}
int fndlab(char *name) {
DEBUG("Looking for Program Label %s\n", name)
for (labidx=0; labidx<labcnt; labidx++)
if (strcmp(labnam[labidx], name) == 0) return TRUE;
DEBUG("Label %s Not Found\n", name)
return FALSE;
}
/* Print Program Label Table to Log File */
void loglab(void) {
int i;
fprintf(logfil, "\n%-10s\n", "Label");
for (i=0; i<labcnt; i++) {
fprintf(logfil, "%-10s\n", labnam[i]);
}
}
const char lblflg[] = {LFNONE, LFNONE, LFNONE, LFBGN, LFEND, LFBGN, LFEND, LFEND, LFNONE, LFNONE}; //Label Type Flags
// enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types
@ -48,12 +75,12 @@ void setlbl(char *lblset) {
strcpy(lblasm, lblset);
}
/* parse label in code */
void prslbl(void) {
/* Parse Program Label */
void prslab(void) {
DEBUG("Parsing Label '%s''\n", word)
addlab(word);
CCMNT(nxtchr);
skpchr(); //skip ':'
setlbl(word);
}
/* generate new label */

View File

@ -2,6 +2,10 @@
* C02 Label Parsing, Generation, and Lookup Routines *
******************************************************/
char labnam[MAXLAB+1][LABLEN+1]; //Program Label Names
int labcnt; //Number of Program Labels
int labidx; //Index into labnam[]
char curlbl[LBLLEN+1]; //Most recently generated label
char cmplbl[LBLLEN+1]; //Label for Comparison
char cndlbl[LBLLEN+1]; //Label for Conditional Code
@ -20,11 +24,15 @@ char lbltmp[LBLLEN+1]; //Label Temporary Storage
enum ltypes {LTNONE, LTIF, LTELSE, LTLOOP, LTEND, LTDO, LTDWHL, LTSLCT, LTCASE, LTFUNC}; //Label Types
enum lflags {LFNONE, LFBGN, LFEND}; //Label Flag Types
void addlab(char *name); //Add Program Label
int fndlab(char *name); //Find Program Label
void prslab(); //Parse Program Label
void loglab(void); //Print Program Label Table
void chklbl(char* lbname); //Check Label Contents
int lstlbl(int lbflag); //Find Last Label of Specified Types *
void newlbl(char* lbname); //Generate New Block Label
int poplbl(); //Pop Last Label and Emit on Next Line
void prslbl(); //Parse Label From Code
void pshlbl(int lbtype, char* lbname); //Push Label onto Stack
void reqlbl(char* lbname); //Require Label
void setblk(int blkflg); //Set Block Flag for Last Label

View File

@ -507,7 +507,8 @@ void pstmnt(void) {
return;
}
if(match(':')) {
prslbl(); //Parse Label
prslab(); //Parse Program Label
setlbl(word); //Set Label to Emit
return;
}
if (wordis("ASM")) pasm();

View File

@ -109,17 +109,25 @@ void prsmbr(char* name) {
strcat(name, word); //Add Offset to Struct
}
/* Parse word as variable or function name *
* Args: alwreg - Allow Register Names *
* Sets: value - Identifier Name *
* valtyp - Identifier Type */
void prsvrw(int alwreg, int alwcon) {
valtyp = gettyp(); //Determine Variable Type
if (valtyp != FUNCTION) chksym(alwreg, alwcon, word);
strcpy(value, word);
DEBUG("Parsed variable '%s'\n", value)
if (valtyp == STRUCTURE) prsmbr(value);
}
/* Parse next word as variable or function name *
* Args: alwreg - Allow Register Names *
* Sets: value - Identifier Name *
* valtyp - Identifier Type */
void prsvar(int alwreg, int alwcon) {
getwrd(); //Get Variable Name
valtyp = gettyp(); //Determine Variable Type
if (valtyp != FUNCTION) chksym(alwreg, alwcon, word);
strcpy(value, word);
DEBUG("Parsed variable '%s'\n", value)
if (valtyp == STRUCTURE) prsmbr(value);
prsvrw(alwreg, alwcon); //Parse Variable name in word
}
/* Require and Parse Variable Name *
@ -258,6 +266,7 @@ void setvar(int m, int t) {
* Uses: word - variable name */
void addvar(int m, int t) {
strcpy(vrname, word); //Save Variable Name
if (fndlab(vrname)) ERROR("Variable %s conflicts with label of same name\n", vrname, EXIT_FAILURE)
if (fndvar(vrname)) ERROR("Duplicate declaration of variable '%s\n", vrname, EXIT_FAILURE)
if (t == VTVOID) ERROR("Illegal Variable Type\n", 0, EXIT_FAILURE)
if (m & MTZP) {

View File

@ -80,12 +80,14 @@ char wrtofs[6]; //Write Address Offset
void addvar(int m, int t); //Parse and Compile Variable Declaration
void addstc(); //Parse and Compile Structure Declaration
void defstc(); //Parse Structure Definition
void chksym(int alwreg, int alwcon, char *name); //Error if Variable not defined
int fndvar(char *name); //Find Variable in Variable Table
void chksym(int alwreg, int alwcon, char *name); //Check for Variable
void prsdts(); //Parse Data String
void setdat(); //Set Variable Data
void setvar(int m, int t); //Set Variable Name and Size
void prsdts(); //Parse Data String
void prsvar(int alwreg, int alwcon); //Parse Variable
void prsvar(int alwreg, int alwcon); //Parse Next Word as Variable
void prsvrw(int alwreg, int alwcon); //Parse word as Variable
void prsmbr(char* name); //Parse Struct Member
int psizof(void); //Parse SizeOf Operator
int pidxof(void); //Parse IndexOf Operator