mirror of https://github.com/RevCurtisP/C02.git
Incomplete implementation of symtbl
This commit is contained in:
parent
b6cec4d75a
commit
6a44c99bdb
12
src/common.h
12
src/common.h
|
@ -4,6 +4,8 @@
|
|||
|
||||
#define FNAMLEN 255 //Maximum File Name Length
|
||||
#define LINELEN 255 //Maximum Input/Output Line Length
|
||||
#define SYMLEN 6 //Maximum Symbol Length
|
||||
#define MAXSYM 255 //Maximum Number of Symbols
|
||||
#define CONLEN 6 //Maximum Constant Name Length
|
||||
#define MAXCON 255 //Maximum Number of Constants
|
||||
#define STCLEN 6 //Maximum Struct Name Length
|
||||
|
@ -16,10 +18,10 @@
|
|||
#define DATASPC 4096 //Space to Allocate for Variable Data
|
||||
#define SUBMAX 4 //Maximum Number of Sub Directories
|
||||
|
||||
#define LABLEN 6 //Maximum Label Length
|
||||
#define LABFMT "L_%04d" //Label Format
|
||||
#define LABSFX ":" //Label Suffix
|
||||
#define MAXLAB 15 //Maximum Number of Labels (Nesting Depth)
|
||||
#define LABLEN 6 //Maximum Internal Label Length
|
||||
#define LABFMT "L_%04d" //Internal Label Format
|
||||
#define LABSFX ":" //Internal Label Suffix
|
||||
#define MAXLAB 15 //Maximum Number of Internal Labels (Nesting Depth)
|
||||
#define LOCPFX "." //Local Variable Prefix
|
||||
|
||||
#define CPUOP "PROCESSOR" //Target CPU Pseudo-Operator
|
||||
|
@ -61,7 +63,7 @@ int savchr; //Holds nxtchr when switching input files
|
|||
|
||||
int wrdlen; //Length of Parsed Word
|
||||
char word[LINELEN]; //Word parsed from source file
|
||||
char uword[LINELEN]; //Word converted too uppercase
|
||||
char uword[LINELEN]; //Word converted to uppercase
|
||||
char cmtasm[LINELEN]; //Assembly Language Comment Text
|
||||
|
||||
char hdrnam[FNAMLEN]; //Header File Name
|
||||
|
|
21
src/expr.c
21
src/expr.c
|
@ -330,25 +330,34 @@ int prsxpf(char trmntr) {
|
|||
return prsxpp(trmntr, TRUE);
|
||||
}
|
||||
|
||||
/* Parse and compile integer expression */
|
||||
void prsxpi(char trmntr) {
|
||||
/* Parse and Compile Integer Expression *
|
||||
* (Address, Integer Literal, Variable, *
|
||||
* Struct Member, or Function) *
|
||||
* Args: trmntr - expression terminator *
|
||||
* asmxpr - assemble expression *
|
||||
* Sets: value - Parsed Value or Symbol */
|
||||
void prsxpi(char trmntr, int asmxpr) {
|
||||
skpspc();
|
||||
DEBUG("Parsing integer expression\n", 0)
|
||||
if (!chkadr(TRUE, FALSE)) {
|
||||
if (isnpre()) {
|
||||
DEBUG("Parsing Integer Literal\n", 0)
|
||||
int number = prsnum(0xFFFF);
|
||||
sprintf(value, "%d", number & 0xFF); asmlin("LDX", value);
|
||||
sprintf(value, "%d", number >> 8); asmlin("LDY", value);
|
||||
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) {
|
||||
if (!asmxpr) ERROR("Illegal Use of Function\n", 0, EXIT_FAILURE)
|
||||
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) {
|
||||
prcvri(); //Process Integer Variable
|
||||
if (asmxpr) prcvri(); //Process Integer Variable
|
||||
} else {
|
||||
ERROR("Illegal Variable %s In Integer Expression", value, EXIT_FAILURE)
|
||||
}
|
||||
|
|
|
@ -27,4 +27,4 @@ int prstrm(int alwint); //Parse Term in Expression
|
|||
void prsrxp(char trmntr); //Parse Rest of Expression
|
||||
int prsxpf(char trmntr); //Parse Expression in Function Call
|
||||
void prsxpr(char trmntr); //Parse Expression
|
||||
void prsxpi(char trmntr); //Parse Integer Expression
|
||||
void prsxpi(char trmntr, int asmxpr); //Parse Integer Expression
|
||||
|
|
16
src/label.c
16
src/label.c
|
@ -16,6 +16,21 @@
|
|||
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
|
||||
|
||||
/* Add Symbol to Symbol Table */
|
||||
void addsym(char *name, int type) {
|
||||
strcpy(symbol.name, name);
|
||||
symbol.type = type;
|
||||
memcpy(&symtbl[symcnt++], &symbol, sizeof(symbol));
|
||||
}
|
||||
|
||||
/* Find Symbol in Table */
|
||||
int fndsym(char *name) {
|
||||
for (int i=0; i<symcnt; i++)
|
||||
if (strcmp(symtbl[i].name, name))
|
||||
return symtbl[i].type;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Find Last Label of Specified Types *
|
||||
* Args: lbtyp1: First label type *
|
||||
* lbtyp2: Second label type *
|
||||
|
@ -53,6 +68,7 @@ void prslbl(void) {
|
|||
DEBUG("Parsing Label '%s''\n", word)
|
||||
CCMNT(nxtchr);
|
||||
skpchr(); //skip ':'
|
||||
addsym(word, SYMLBL);
|
||||
setlbl(word);
|
||||
}
|
||||
|
||||
|
|
15
src/label.h
15
src/label.h
|
@ -2,6 +2,18 @@
|
|||
* C02 Label Parsing, Generation, and Lookup Routines *
|
||||
******************************************************/
|
||||
|
||||
/* Symbol Types */
|
||||
enum symtyps {SYMREG=1, SYMLBL=2, SYMCON=4, SYMVAR=8, SYMFNC=16, CONVAR=128};
|
||||
|
||||
struct symstc {
|
||||
char name[SYMLEN]; //Symbol Name
|
||||
int type; //Symbol Type
|
||||
};
|
||||
|
||||
struct symstc symbol; //Symbol Record
|
||||
struct symstc symtbl[MAXSYM]; //Symbol Table
|
||||
int symcnt; //Number of Symbols
|
||||
|
||||
char curlbl[LABLEN+1]; //Most recently generated label
|
||||
char cmplbl[LABLEN+1]; //Label for Comparison
|
||||
char cndlbl[LABLEN+1]; //Label for Conditional Code
|
||||
|
@ -20,6 +32,9 @@ char lbltmp[LABLEN+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 addsym(char *name, int type); //Add Symbol to Table
|
||||
int fndsym(char *name); //Find Symbol in 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
|
||||
|
|
13
src/stmnt.c
13
src/stmnt.c
|
@ -129,7 +129,7 @@ void prcasi(char trmntr) {
|
|||
strcpy(xsnvar, vrname); //Set Assignment LSB
|
||||
strcpy(ysnvar, vrname); strcat(ysnvar, "+1"); //Set Assignment MSB
|
||||
ysnidx[0] = 0; //No Y Index
|
||||
prsxpi(trmntr);
|
||||
prsxpi(trmntr, TRUE); //Parse Integer Expression, Allowing Functions
|
||||
prcaxy();
|
||||
}
|
||||
|
||||
|
@ -298,11 +298,12 @@ void pelse(void) {
|
|||
/* parse and compile if statement */
|
||||
void pgoto(void) {
|
||||
DEBUG("Parsing GOTO statement\n", 0)
|
||||
int indrct = look('(');
|
||||
if (!chkadr(ADNONE, FALSE)) getwrd();
|
||||
if (indrct) {
|
||||
expect(')');
|
||||
ERROR("Indirect GOTO Not Implemented\n", 0, EXIT_FAILURE)
|
||||
if (look('(')) {
|
||||
DEBUG("Processing Indirect GOTO\n", 0);
|
||||
prsxpi(')', FALSE); //Parse Integer Expression w/o Assembly
|
||||
sprintf(word, "(%s)", value); //Set Indirect Argument
|
||||
} else {
|
||||
if (!chkadr(ADNONE, FALSE)) getwrd();
|
||||
}
|
||||
expect(';');
|
||||
asmlin("JMP", word);
|
||||
|
|
15
src/vars.c
15
src/vars.c
|
@ -65,18 +65,20 @@ int fndmbr(int idx, char *name) {
|
|||
* Args: alwreg - allow register name *
|
||||
* alwcon - allow const variable *
|
||||
* name - variable name */
|
||||
void chksym(int alwreg, int alwcon, char *name) {
|
||||
int chksym(int types, char *name) {
|
||||
if (strlen(name) == 1 && strchr("AXY", name[0])) {
|
||||
if (alwreg && valtyp != ARRAY) {
|
||||
if (types & SYMREG && valtyp < ARRAY) {
|
||||
valtyp = REGISTER;
|
||||
return;
|
||||
return SYMREG;
|
||||
}
|
||||
ERROR("Illegal reference to register %s\n", name, EXIT_FAILURE)
|
||||
}
|
||||
if (!fndvar(name))
|
||||
ERROR("Undeclared variable '%s' encountered\n", name, EXIT_FAILURE)
|
||||
if (!alwcon && (varble.modifr & MTCONST))
|
||||
if (fndvar(name)) {
|
||||
if (!(types &symvar)) ERROR("Illegal reference to variable %s\n", name, EXIT_FAILURE)
|
||||
if (!(types & CONVAR) && (varble.modifr & MTCONST))
|
||||
ERROR("Illegal use of const variable '%s'\n", name, EXIT_FAILURE)
|
||||
} else
|
||||
ERROR("Undeclared variable '%s' encountered\n", name, EXIT_FAILURE)
|
||||
}
|
||||
|
||||
/* Process struct member *
|
||||
|
@ -115,6 +117,7 @@ void prsmbr(char* name) {
|
|||
* valtyp - Identifier Type */
|
||||
void prsvar(int alwreg, int alwcon) {
|
||||
getwrd(); //Get Variable Name
|
||||
DEBUG("Parsing \"%s\" as variable or function\n", word)
|
||||
valtyp = gettyp(); //Determine Variable Type
|
||||
if (valtyp != FUNCTION) chksym(alwreg, alwcon, word);
|
||||
strcpy(value, word);
|
||||
|
|
Loading…
Reference in New Issue