1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-29 01:49:19 +00:00

Added struct declarations

This commit is contained in:
Curtis F Kaylor 2018-03-08 08:23:05 -05:00
parent df34d89252
commit fd6a96afb5
5 changed files with 139 additions and 40 deletions

View File

@ -4,9 +4,13 @@
#define FNAMLEN 255 //Maximum File Name Length #define FNAMLEN 255 //Maximum File Name Length
#define LINELEN 255 //Maximum Input/Output Line Length #define LINELEN 255 //Maximum Input/Output Line Length
#define CONLEN 6 //Maximum Definition Text Length #define CONLEN 6 //Maximum Constant Name Length
#define MAXCON 255 //Maximum Number of Definitions #define MAXCON 255 //Maximum Number of Constants
#define VARLEN 6 //Maximum Variable Length #define STCLEN 6 //Maximum Struct Name Length
#define MAXSTC 32 //Maximum Number of Stuctures
#define STMLEN 6 //Maximum Struct Member Name Length
#define MAXSTM 255 //Maximum Number of Stucture Members
#define VARLEN 6 //Maximum Variable Name Length
#define MAXVAR 255 //Maximum Number of Variables #define MAXVAR 255 //Maximum Number of Variables
#define MAXFNS 16 //Maximum Functions in Stack #define MAXFNS 16 //Maximum Functions in Stack
#define DATASPC 2048 //Space to Allocate for Variable Data #define DATASPC 2048 //Space to Allocate for Variable Data

View File

@ -92,6 +92,14 @@ void penum(int m) {
DEBUG("Enum Declaration Completed\n", 0) DEBUG("Enum Declaration Completed\n", 0)
} }
/* Parse Enum Declaration*/
void pstrct(int m) {
DEBUG("Processing Struct Declarations\n", 0)
getwrd(); //Parse Structure Name
if (look('{')) defstc(); //Parse Struct Definition
else addstc(); //Parse and Compile Struct Declaration
SCMNT(""); //Clear Assembler Comment
}
/* Parse Variable/Function Declaration*/ /* Parse Variable/Function Declaration*/
void pdecl(int m, int t) { void pdecl(int m, int t) {
@ -113,7 +121,8 @@ void pdecl(int m, int t) {
/* Check for and Parse Type Keyword */ /* Check for and Parse Type Keyword */
int ptype(int m) { int ptype(int m) {
int result = TRUE; int result = TRUE;
if (wordis("CONST")) pconst(m); //Parse 'const' declaration if (wordis("STRUCT")) pstrct(m); //Parse 'const' declaration
else if (wordis("CONST")) pconst(m); //Parse 'const' declaration
else if (wordis("ENUM")) penum(m); //Parse 'enum' declaration else if (wordis("ENUM")) penum(m); //Parse 'enum' declaration
else if (wordis("CHAR")) pdecl(m, VTCHAR); //Parse 'char' declaration else if (wordis("CHAR")) pdecl(m, VTCHAR); //Parse 'char' declaration
else if (wordis("VOID")) pdecl(m, VTVOID); //Parse 'void' declaration else if (wordis("VOID")) pdecl(m, VTVOID); //Parse 'void' declaration

View File

@ -4,7 +4,7 @@
#define TF(x) (x) ? TRUE : FALSE; #define TF(x) (x) ? TRUE : FALSE;
enum stypes {LITERAL, VARIABLE, REGISTER, ARRAY, FUNCTION}; //Symbol Types enum stypes {LITERAL, VARIABLE, REGISTER, ARRAY, STRUCT, FUNCTION}; //Symbol Types
enum etypes {ETDEF, ETMAC}; //Definition Types enum etypes {ETDEF, ETMAC}; //Definition Types
char nxtwrd[LINELEN]; //Next Word (from DEFINE lookup) char nxtwrd[LINELEN]; //Next Word (from DEFINE lookup)
@ -14,10 +14,10 @@ int valtyp; //Value Type
char oper; //Arithmetic or Bitwise Operator char oper; //Arithmetic or Bitwise Operator
int litval; //Value of Parsed Literal int litval; //Value of Parsed Literal
char connam[MAXCON+1][CONLEN+1]; //Definition Name Table char connam[MAXCON+1][CONLEN+1]; //Constant Name Table
int conval[MAXCON+1]; //Definition Value Table int conval[MAXCON+1]; //Constant Value Table
int concnt; //Number of Definitions Defined int concnt; //Number of Constants Defined
int conidx; //Index into Definition Tables int conidx; //Index into Constant Tables
int invasc; //Invert ASCII Flag int invasc; //Invert ASCII Flag
int mskasc; //Set High Bit Flag int mskasc; //Set High Bit Flag

View File

@ -20,7 +20,7 @@
* varcnt if not found * * varcnt if not found *
* Returns: TRUE if found, otherwise FALSE */ * Returns: TRUE if found, otherwise FALSE */
int fndvar(char *name) { int fndvar(char *name) {
DEBUG("Looking up variable '%s'\n", word) DEBUG("Looking up variable '%s'\n", name)
for (varidx=0; varidx<varcnt; varidx++) for (varidx=0; varidx<varcnt; varidx++)
if (strcmp(varnam[varidx], name) == 0) return TRUE; if (strcmp(varnam[varidx], name) == 0) return TRUE;
return FALSE; return FALSE;
@ -120,14 +120,15 @@ void prsdat(void) {
} }
/* Add Variable to Variable table * /* Add Variable to Variable table *
* Uses: word - variable name * * Uses: vrname - variable name *
* value - variable size */ * value - variable size */
void setvar(int m, int t) { void setvar(int m, int t) {
DEBUG("Added variable '%s' ", word); DEBUG("Added variable '%s' ", vrname);
strncpy(varnam[varcnt], vrname, VARLEN); strncpy(varnam[varcnt], vrname, VARLEN);
varmod[varcnt] = m; varmod[varcnt] = m;
vartyp[varcnt] = t; vartyp[varcnt] = t;
strncpy(varsiz[varcnt], value, 3); strncpy(varsiz[varcnt], value, 3);
varstc[varcnt] = (t == VTSTRUCT) ? stcidx : -1;
DETAIL("at index %d\n", varcnt); DETAIL("at index %d\n", varcnt);
} }
@ -135,7 +136,7 @@ void setvar(int m, int t) {
* Uses: word - variable name */ * Uses: word - variable name */
void addvar(int m, int t) { void addvar(int m, int t) {
strcpy(vrname, word); //Save Variable Name strcpy(vrname, word); //Save Variable Name
if (fndvar(vrname)) ERROR("Duplicate declaration of variable '%s\n", word,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 (t == VTVOID) ERROR("Illegal Variable Type\n", 0, EXIT_FAILURE)
if (m == MTZP) { if (m == MTZP) {
setlbl(vrname); setlbl(vrname);
@ -144,9 +145,10 @@ void addvar(int m, int t) {
strcpy(value, "*"); //Set Variable Type to Zero Page strcpy(value, "*"); //Set Variable Type to Zero Page
} }
else { else {
DEBUG("Checking for array definition\n", 0) if (t == VTSTRUCT) {
value[0] = 0; DEBUG("Setting variable size to %d\n", stcsiz[stcidx])
if (match('[')) { sprintf(value, "%d", stcsiz[stcidx]);
} else if (match('[')) {
skpchr(); skpchr();
if (alcvar) { if (alcvar) {
DEBUG("Parsing array size\n", 0) DEBUG("Parsing array size\n", 0)
@ -154,10 +156,11 @@ void addvar(int m, int t) {
} }
expect(']'); expect(']');
} }
else value[0] = 0;
if (!alcvar) strcpy(value, "*"); if (!alcvar) strcpy(value, "*");
setvar(m, t); //Add to Variable Table setvar(m, t); //Add to Variable Table
} }
if (m != MTZP) prsdat(); //Parse Variable Data if (m != MTZP && t != VTSTRUCT ) prsdat(); //Parse Variable Data
varcnt++; //Increment Variable Counter varcnt++; //Increment Variable Counter
} }
@ -199,3 +202,74 @@ void vartbl(void) {
} }
vrwrtn = TRUE; vrwrtn = TRUE;
} }
/* Lookup structure name in struct table *
* Sets: sctidx = index into sctnam array *
* sctcnt if not found *
* Returns: TRUE if found, otherwise FALSE */
int fndstc(char *name) {
DEBUG("Looking up struct '%s'\n", name)
for (stcidx=0; stcidx<stccnt; stcidx++)
if (strcmp(stcnam[stcidx], name) == 0) return TRUE;
return FALSE;
}
/* Lookup structure member name in table *
* Sets: stmidx = index into stmnam array *
* stmcnt if not found *
* Returns: TRUE if found, otherwise FALSE */
int fndstm(int stcidx, char *name) {
DEBUG("Looking up member '%s'\n", word)
for (stmidx=0; stmidx<stmcnt; stmidx++)
if (stmstc[stmidx] != stcidx) continue;
if (strcmp(stmnam[stmidx], name) == 0) return TRUE;
return FALSE;
}
/* Parse and Compile Struct Declaration */
void addstc(void) {
if (!fndstc(word)) ERROR("Undefined Struct '%s\n", word,EXIT_FAILURE)
getwrd(); //Get Variable Name
addvar(MTNONE, VTSTRUCT);
expect(';');
}
/* Parse Struct Definition *
* Uses: word - Struct Name */
void defstc(void) {
DEBUG("Parsing Struct Definition\n", 0)
if (fndstc(word)) ERROR("Duplicate Declaration of Struct '%s\n", word,EXIT_FAILURE)
strncpy(stcnam[stccnt], word, STCLEN);
DEBUG("Added struct '%s' ", word); DETAIL("at index %d\n", stccnt);
stclen = 0; //Initialize Struct Length
do {
getwrd(); //Get Member Name
if (wordis("CHAR")) getwrd(); //Skip Optional Type Declaration
if (fndstm(stccnt, word)) ERROR("Duplicate Declaration of Struct Member '%s\n", word,EXIT_FAILURE)
DEBUG("Adding member %s ", word) DETAIL("at index %d\n", stmcnt);
strncpy(stmnam[stmcnt], word, STMLEN);
DEBUG("Checking for array definition\n", 0)
if (match('[')) {
skpchr();
stmtyp[stmcnt] = ARRAY;
DEBUG("Parsing array size\n", 0)
stmsiz[stmcnt] = prsnum(0xFF) + 1;
expect(']');
}
else {
stmtyp[stmcnt] = VARIABLE;
stmsiz[stmcnt] = 1;
}
DEBUG("Set member type to %d", stmtyp[stmcnt]) DETAIL(" and size to %d\n", stmsiz[stmcnt]);
stclen += stmsiz[stmcnt];
expect(';');
stmcnt++;
} while (!look('}'));
expect(';');
//set stcsiz[
if (stclen > 256) ERROR("Structure Size %d Exceeds Limit of 256 bytes.\n", stclen, EXIT_FAILURE);
stcsiz[stccnt] = stclen;
DEBUG("Set struct size to %d\n", stclen);
stccnt++;
DEBUG("Struct Declaration Completed\n", 0)
}

View File

@ -3,20 +3,30 @@
*************************************/ *************************************/
/* Variable Table */ /* Variable Table */
char varnam[MAXVAR+1][VARLEN+1]; //Variable Name Table char varnam[MAXVAR+1][VARLEN+1]; //Variable Name Table
char varmod[MAXVAR+1]; //Variable Modifier char varmod[MAXVAR+1]; //Variable Modifier
char vartyp[MAXVAR+1]; //Variable Type char vartyp[MAXVAR+1]; //Variable Type
char varsiz[MAXVAR+1][4]; //Variable Array char varsiz[MAXVAR+1][4]; //Variable Array Size
int varcnt; //Number of Variables in Table char varstc[MAXVAR+1]; //Variable Struct Type
int varidx; //Index into Variable Tables int varcnt; //Number of Variables in Table
char vrname[MAXVAR+1]; //Variable Name int varidx; //Index into Variable Tables
int vrwrtn; //Variables Written Flag char vrname[MAXVAR+1]; //Variable Name
int vrwrtn; //Variables Written Flag
/* char stcnam[MAXSTC+1][STCLEN+1]; //Structure Name Table
int varidx; //Index into Variable Table int stcsiz[MAXSTC+1]; //Structure Size Table
int vrtype; //Variable Type int stccnt; //Number of Structs Defined
*/ int stcidx; //Index into Struct Tables
enum vtypes {VTVOID, VTCHAR}; //Variable Types int stclen; //Size of Current Struct
char stmnam[MAXSTM+1][STCLEN+1]; //Structure Member Name Table
int stmstc[MAXSTM+1]; //Structure Member Parent Struct
char stmtyp[MAXVAR+1]; //Structure Member Variable Type
int stmsiz[MAXVAR+1]; //Structure Member Array Size
int stmcnt; //Number of Struct Members Defined
int stmidx; //Index into Struct Member Tables
enum vtypes {VTVOID, VTCHAR, VTSTRUCT}; //Variable Types
char datvar[DATASPC+1]; //Variable Data Storage char datvar[DATASPC+1]; //Variable Data Storage
char datlen[MAXVAR+1]; //Variable Data Length char datlen[MAXVAR+1]; //Variable Data Length
@ -39,14 +49,16 @@ char prmtrx[VARLEN+1]; //Function Parameter X
char prmtry[VARLEN+1]; //Function Parameter Y char prmtry[VARLEN+1]; //Function Parameter Y
int prmcnt; //Number of Parameters int prmcnt; //Number of Parameters
void addvar(int m, int t); //Parse and Compile Variable Declaration 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, char *name); //Error if Variable not defined void chksym(int alwreg, char *name); //Error if Variable not defined
void prsdts(); //Parse Data String void prsdts(); //Parse Data String
void setdat(); //Set Variable Data void setdat(); //Set Variable Data
void setvar(int m, int t); //Set Variable Name and Size void setvar(int m, int t); //Set Variable Name and Size
void prsdts(); //Parse Data String void prsdts(); //Parse Data String
void prsvar(int alwreg); //Parse Variable void prsvar(int alwreg); //Parse Variable
void reqvar(int alwary); //Require and Parse Variable Name void reqvar(int alwary); //Require and Parse Variable Name
void setdat(); //Store variable data void setdat(); //Store variable data
void setvar(int m, int t); //Add Variable to Variable table void setvar(int m, int t); //Add Variable to Variable table
void vartbl(); //Create Variable Table void vartbl(); //Create Variable Table