mirror of
https://github.com/RevCurtisP/C02.git
synced 2025-04-22 00:37:19 +00:00
Added declaration modifier const
This commit is contained in:
parent
de4124e3b3
commit
fe62927246
@ -82,3 +82,4 @@ void setcmt(char *s); //Set comment to string
|
||||
#define SCMNT(str) if (gencmt) {setcmt(str);}
|
||||
#define ACMNT(str) if (gencmt) {addcmt(str);}
|
||||
#define CCMNT(chr) if (gencmt) {chrcmt(chr);}
|
||||
#define LCMNT(str) if (gencmt) {setcmt(str); cmtlin();}
|
||||
|
25
src/dclrtn.c
25
src/dclrtn.c
@ -115,22 +115,29 @@ void pdecl(int m, int t) {
|
||||
* Args: m - Modifier Type */
|
||||
int ptype(int m) {
|
||||
int result = TRUE;
|
||||
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("CHAR")) pdecl(m, VTCHAR); //Parse 'char' declaration
|
||||
else if (wordis("VOID")) pdecl(m, VTVOID); //Parse 'void' declaration
|
||||
else result = FALSE;
|
||||
if (wordis("STRUCT")) pstrct(m); //Parse 'const' declaration
|
||||
else if (wordis("ENUM")) penum(m); //Parse 'enum' declaration
|
||||
else if (wordis("CHAR")) pdecl(m, VTCHAR); //Parse 'char' declaration
|
||||
else if (wordis("VOID")) pdecl(m, VTVOID); //Parse 'void' declaration
|
||||
else result = FALSE;
|
||||
return result;
|
||||
}
|
||||
|
||||
int pmtype(int m) {
|
||||
getwrd();
|
||||
if (m == MTALGN && wordis("CONST")) {m = m | MTCONST; getwrd();}
|
||||
DEBUG("Parsing type %s\n", word)
|
||||
return ptype(m);
|
||||
}
|
||||
|
||||
/* Check for and Parse Modifier */
|
||||
int pmodfr(void) {
|
||||
DEBUG("Parsing modifier '%s'\n", word)
|
||||
int result = TRUE;
|
||||
if (wordis("ALIGNED")) { getwrd(); ptype(MTALGN); }
|
||||
else if (wordis("ZEROPAGE")) { getwrd(); ptype(MTZP); }
|
||||
else if (wordis("ALIAS")) { getwrd(); ptype(MTALS); }
|
||||
if (wordis("ALIAS")) { pmtype(MTALS); }
|
||||
else if (wordis("ALIGNED")) { pmtype(MTALGN); }
|
||||
else if (wordis("CONST")) { pmtype(MTCONST); }
|
||||
else if (wordis("ZEROPAGE")) { pmtype(MTZP); }
|
||||
else result = FALSE;
|
||||
return result;
|
||||
}
|
||||
|
14
src/expr.c
14
src/expr.c
@ -33,11 +33,11 @@ void poptrm(void) {
|
||||
* Args: alwreg - allow registers *
|
||||
* Sets: value - the value (as a string) *
|
||||
* valtyp - value type */
|
||||
void prsval(int alwreg) {
|
||||
void prsval(int alwreg, int alwcon) {
|
||||
DEBUG("Parsing value\n", 0)
|
||||
skpspc();
|
||||
if (islpre()) prslit(); //Parse Literal
|
||||
else if (isalph()) prsvar(alwreg); //Parse Variable
|
||||
else if (isalph()) prsvar(alwreg, alwcon); //Parse Variable
|
||||
else expctd("literal or variable");
|
||||
DEBUG("Parsed value of type %d\n", valtyp)
|
||||
skpspc();
|
||||
@ -55,7 +55,7 @@ void prcmns(void) {
|
||||
* "" if no index defined */
|
||||
void prsidx(int clbrkt) {
|
||||
expect('[');
|
||||
prsval(TRUE); //Parse Value, Allowing Registers
|
||||
prsval(TRUE, TRUE); //Parse Value, Allowing Registers
|
||||
DEBUG("Parsed array index '%s'\n", value)
|
||||
if (clbrkt) expect(']');
|
||||
}
|
||||
@ -113,7 +113,7 @@ void chkidx(void) {
|
||||
* Sets: term - the term (as a string) */
|
||||
void prstrm(void) {
|
||||
DEBUG("Parsing term\n", 0)
|
||||
prsval(FALSE); //Parse Term - Disallow Registers
|
||||
prsval(FALSE, TRUE); //Parse Term - Disallow Registers
|
||||
if (valtyp == FUNCTION) ERROR("Function call only allowed in first term\n", 0, EXIT_FAILURE)
|
||||
strcpy(term, value);
|
||||
DEBUG("Parsed term %s\n", term)
|
||||
@ -140,7 +140,7 @@ void prcadr(int adract, char* symbol) {
|
||||
void prsadr(int adract) {
|
||||
DEBUG("Parsing address\n", 0)
|
||||
if (isnpre()) prsnum(0xFFFF);
|
||||
else prsvar(FALSE);
|
||||
else prsvar(FALSE, TRUE);
|
||||
prcadr(adract, value); //Compile Address Reference
|
||||
}
|
||||
|
||||
@ -177,7 +177,7 @@ void prsfnc(char trmntr) {
|
||||
if (!look('*')) prsxpr(0);
|
||||
if (look(',') && !chkadr(0)) {
|
||||
if (!look('*')) { prstrm(); asmlin("LDY", term); }
|
||||
if (look(',')) { prsval(FALSE); asmlin("LDX", value); }
|
||||
if (look(',')) { prsval(FALSE, TRUE); asmlin("LDX", value); }
|
||||
}
|
||||
}
|
||||
expect(')');
|
||||
@ -201,7 +201,7 @@ void prcftm(void) {
|
||||
/* Parse first term of expession *
|
||||
* First term can include function calls */
|
||||
void prsftm(void) {
|
||||
prsval(TRUE); //Parse Value, Allowing Registers
|
||||
prsval(TRUE, TRUE); //Parse Value, Allowing Registers
|
||||
prcftm();
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ int isdec(void) {return inbtwn('0', '9');}
|
||||
int iscpre(void) {return match('#');}
|
||||
int ishexd(void) {return TF(isdec() || inbtwn('A', 'Z'));}
|
||||
int islpre(void) {return TF(isbpre() || iscpre() || isszop() || isxfop());}
|
||||
int isnl(void) {return TF(match('\n') || match('\r'));}
|
||||
int isnl(void) {return TF(match('\n') || match('\r') || match(EOF));}
|
||||
int isnpre(void) {return TF(isdec() || match('$') || match('%'));}
|
||||
int isoper(void) {return TF(strchr("+-&|^", nxtchr));}
|
||||
int ispopr(void) {return TF(strchr("+-<>", nxtchr));}
|
||||
|
@ -113,7 +113,7 @@ int getidx(char* idx) {
|
||||
|
||||
/* Process Assignment Variable(s) */
|
||||
void prcvar(char trmntr) {
|
||||
chksym(TRUE, word);
|
||||
chksym(TRUE, FALSE, word);
|
||||
strcpy(asnvar, word); //save variable to assign to
|
||||
if (valtyp == VARIABLE && match('.')) prsmbr(asnvar);
|
||||
asntyp = valtyp; //Set Assigned Varable Type
|
||||
@ -131,14 +131,14 @@ void prcvar(char trmntr) {
|
||||
}
|
||||
if (look(',')) {
|
||||
if (asntyp == REGISTER) ERROR("Register %s not allowed in plural assignment\n", asnvar, EXIT_FAILURE)
|
||||
prsvar(FALSE); //get variable name
|
||||
prsvar(FALSE, FALSE); //get variable name
|
||||
strcpy(ysnvar, word);
|
||||
DEBUG("Set STY variable to %s\n", ysnvar)
|
||||
if (valtyp == ARRAY) ysnivt = getidx(ysnidx); //Get Array Index and Type
|
||||
else ysnidx[0] = 0;
|
||||
DEBUG("Set STY index to '%s'", ysnidx) DETAIL(" and type to %d\n", ysnivt)
|
||||
if (look(',')) {
|
||||
prsvar(FALSE); //get variable name
|
||||
prsvar(FALSE, FALSE); //get variable name
|
||||
strcpy(xsnvar, word);
|
||||
DEBUG("Set STX variable to %s\n", xsnvar)
|
||||
//if (valtyp == ARRAY) ERROR("Array element not allowed in third assignment\n", 0, EXIT_FAILURE)
|
||||
|
48
src/vars.c
48
src/vars.c
@ -53,8 +53,9 @@ int fndmbr(int idx, char *name) {
|
||||
/* Check for variable *
|
||||
* Generates error if variable is undefined *
|
||||
* Args: alwreg - allow register name *
|
||||
* alwcon - allow const variable *
|
||||
* name - variable name */
|
||||
void chksym(int alwreg, char *name) {
|
||||
void chksym(int alwreg, int alwcon, char *name) {
|
||||
if (strlen(name) == 1 && strchr("AXY", name[0])) {
|
||||
if (alwreg && valtyp != ARRAY) {
|
||||
valtyp = REGISTER;
|
||||
@ -64,6 +65,8 @@ void chksym(int alwreg, char *name) {
|
||||
}
|
||||
if (!fndvar(name))
|
||||
ERROR("Undeclared variable '%s' encountered\n", name, EXIT_FAILURE)
|
||||
if (!alwcon && (varmod[varidx] & MTCONST))
|
||||
ERROR("Illegal use of const variable '%s'\n", name, EXIT_FAILURE)
|
||||
}
|
||||
|
||||
/* Parse next word as struct member *
|
||||
@ -87,10 +90,10 @@ void prsmbr(char* name) {
|
||||
* Args: alwreg - Allow Register Names *
|
||||
* Sets: value - Identifier Name *
|
||||
* valtyp - Identifier Type */
|
||||
void prsvar(int alwreg) {
|
||||
void prsvar(int alwreg, int alwcon) {
|
||||
getwrd(); //Get Variable Name
|
||||
valtyp = gettyp(); //Determine Variable Type
|
||||
if (valtyp != FUNCTION) chksym(alwreg, word);
|
||||
if (valtyp != FUNCTION) chksym(alwreg, alwcon, word);
|
||||
strcpy(value, word);
|
||||
DEBUG("Parsed variable '%s'\n", value)
|
||||
if (valtyp == VARIABLE && match('.')) prsmbr(value);
|
||||
@ -100,7 +103,7 @@ void prsvar(int alwreg) {
|
||||
* Parameters: alwary - Allow Array Reference *
|
||||
* Sets: vrname - operand for LDA/STA/LDY/STY */
|
||||
void reqvar(int alwary) {
|
||||
prsvar(FALSE);
|
||||
prsvar(FALSE, TRUE);
|
||||
if (!alwary && valtyp != VARIABLE) expctd("Variable");
|
||||
}
|
||||
|
||||
@ -191,9 +194,9 @@ void setdat(void) {
|
||||
}
|
||||
|
||||
/* Parse and store variable data */
|
||||
void prsdat(void) {
|
||||
DEBUG("Checking for variable data\n", 0)
|
||||
if (!look('=')) { datlen[varcnt] = 0; return; }
|
||||
void prsdat(int m) {
|
||||
if ((m & MTCONST) == 0) ERROR("Initialization allowed only on variables declared CONST\n", 0, EXIT_FAILURE);
|
||||
DEBUG("Parsing variable data\n", 0)
|
||||
skpspc();
|
||||
if (islpre()) {dtype = DTBYTE; prslit(); } //Parse Data Literal
|
||||
else if (match('"')) prsdts(); //Parse Data String
|
||||
@ -216,23 +219,23 @@ void setvar(int m, int t) {
|
||||
}
|
||||
|
||||
/* Parse and Compile Variable Declaration *
|
||||
* Uses: word - variable name */
|
||||
* Uses: word - variable name */
|
||||
void addvar(int m, int t) {
|
||||
strcpy(vrname, word); //Save Variable Name
|
||||
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) {
|
||||
if (m & MTZP) {
|
||||
setlbl(vrname);
|
||||
sprintf(word, "$%hhX", zpaddr++);
|
||||
asmlin(EQUOP, word);
|
||||
strcpy(value, "*"); //Set Variable to Non Allocated
|
||||
}
|
||||
else if (m == MTALS) {
|
||||
else if (m & MTALS) {
|
||||
setlbl(vrname);
|
||||
skpspc();
|
||||
expect('=');
|
||||
skpspc();
|
||||
if (isnpre()) prsnum(0xFFFF); else prsvar(FALSE);
|
||||
if (isnpre()) prsnum(0xFFFF); else prsvar(FALSE, FALSE);
|
||||
asmlin(EQUOP, word);
|
||||
strcpy(value, "*"); //Set Variable to Non Allocated
|
||||
}
|
||||
@ -253,22 +256,24 @@ void addvar(int m, int t) {
|
||||
if (!alcvar) strcpy(value, "*");
|
||||
}
|
||||
setvar(m, t); //Add to Variable Table
|
||||
if (m < MTZP && t != VTSTRUCT ) prsdat(); //Parse Variable Data
|
||||
if (look('=')) prsdat(m); //Parse Variable Data
|
||||
varcnt++; //Increment Variable Counter
|
||||
}
|
||||
|
||||
/* Write Variable Table */
|
||||
void vartbl(void) {
|
||||
/* Write Variable Definitions *
|
||||
* Args: m = write CONST vars flag */
|
||||
void vardef(int m) {
|
||||
int i, j;
|
||||
DEBUG("Writing Variable Table\n", 0)
|
||||
fprintf(logfil, "\n%-31s %s %s %s %s\n", "Variable", "Type", "Size", "Struct", "Data");
|
||||
fprintf(logfil, "\n%-31s %s %s %s %s\n", "Variable", "Mod", "Type", "Size", "Struct", "Data");
|
||||
dlen = 0;
|
||||
for (i=0; i<varcnt; i++) {
|
||||
fprintf(logfil, "%-31s %4d %4s %6d %1d-%d\n", varnam[i], vartyp[i], varsiz[i], varstc[i], dattyp[i], datlen[i]);
|
||||
if ((varmod[i] & MTCONST) != m) continue;
|
||||
fprintf(logfil, "%-8s %3d %4d %4s %6d %1d-%d\n", varnam[i], varmod[i], vartyp[i], varsiz[i], varstc[i], dattyp[i], datlen[i]);
|
||||
strcpy(lblasm, varnam[i]);
|
||||
DEBUG("Set Label to '%s'\n", lblasm)
|
||||
if (strcmp(varsiz[i], "*") == 0) continue;
|
||||
if (varmod[i] == MTALGN) {
|
||||
if (varmod[i] & MTALGN) {
|
||||
DEBUG("Aligning variable '%s'\n", varnam[i])
|
||||
asmlin(ALNOP, "256");
|
||||
}
|
||||
@ -296,6 +301,15 @@ void vartbl(void) {
|
||||
vrwrtn = TRUE;
|
||||
}
|
||||
|
||||
/* Write Variable Table */
|
||||
void vartbl(void) {
|
||||
LCMNT("Variables declared CONST")
|
||||
vardef(MTCONST); //Write CONST Definitions
|
||||
//Emit Segment Mnemonic for RAM Variables here
|
||||
LCMNT("Writable Variables")
|
||||
vardef(0); //Write All Other Variables
|
||||
}
|
||||
|
||||
/* Parse and Compile Struct Declaration */
|
||||
void addstc(void) {
|
||||
if (!fndstc(word)) ERROR("Undefined Struct '%s\n", word,EXIT_FAILURE)
|
||||
|
11
src/vars.h
11
src/vars.h
@ -48,7 +48,12 @@ int dsize; //Total Data Length
|
||||
|
||||
enum dtypes {DTBYTE, DTSTR, DTARRY}; //Variable Data Types
|
||||
|
||||
enum mtypes {MTNONE, MTALGN, MTZP, MTALS, MTLOCAL}; //Variable Modifier Types
|
||||
/*Variable Modifier Types (Bit Mask) */
|
||||
#define MTNONE 0 //No Modifier
|
||||
#define MTCONST 1 //Constant
|
||||
#define MTZP 2 //Zero Page
|
||||
#define MTALS 4 //Alias
|
||||
#define MTALGN 128 //Aligned
|
||||
|
||||
int symdef(char *name); //Is Variable defined (TRUE or FALSE)
|
||||
int zpaddr; //Current Zero-Page Address
|
||||
@ -56,12 +61,12 @@ int zpaddr; //Current Zero-Page Address
|
||||
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, int alwcon, char *name); //Error if Variable not defined
|
||||
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); //Parse Variable
|
||||
void prsvar(int alwreg, int alwcon); //Parse Variable
|
||||
void prsmbr(char* name); //Parse Struct Member
|
||||
int psizof(void); //Parse SizeOf Operator
|
||||
int pidxof(void); //Parse IndexOf Operator
|
||||
|
@ -1,20 +1,28 @@
|
||||
/* Test C02 define directive */
|
||||
|
||||
#pragma origin 1000
|
||||
#pragma origin 1024
|
||||
|
||||
const #TRUE = $FF, #FALSE = 0;
|
||||
#define TRUE $FF
|
||||
#define FALSE 0
|
||||
|
||||
const #BITS = %01010101;
|
||||
const #ZED = 'Z';
|
||||
#define BITS %01010101
|
||||
#define ZED 'Z'
|
||||
|
||||
enum {SOLO};
|
||||
enum {BLACK, WHITE, RED, CYAN, PURPLE, GREEN, BLUE, YELLOW};
|
||||
enum {NONE, FIRST, SECOND, THIRD};
|
||||
enum {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN};
|
||||
|
||||
char a = {#TRUE, #FALSE};
|
||||
char b;
|
||||
char f = #FALSE;
|
||||
char t = #TRUE;
|
||||
//char b = #TRUE; - Error
|
||||
char b,i;
|
||||
aligned const char v = {0,1,2,3,4,5,6,7,8,9};
|
||||
aligned char m[255];
|
||||
const char tandf = {#TRUE, #FALSE};
|
||||
const char t = #TRUE, f = #FALSE;
|
||||
|
||||
b = #TRUE;
|
||||
b = tandf[i];
|
||||
|
||||
//t = #FALSE; - Errors
|
||||
//b, t = test();
|
||||
//b, i, t = test();
|
Loading…
x
Reference in New Issue
Block a user