1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-07-02 15:29:28 +00:00

Added enum keyword

This commit is contained in:
Curtis F Kaylor 2018-03-07 12:00:33 -05:00
parent ff9576b391
commit 4e421c8095
6 changed files with 56 additions and 22 deletions

View File

@ -167,23 +167,6 @@ Examples:
'A' Character Literal 'A' Character Literal
'\'' Escaped Character Literal '\'' Escaped Character Literal
ENUMERATIONS
An enumeration is a sequential list of constants. Enumerations are used to
generate sets of related but distinct values.
An enumeration is defined using the #enum directive, followed by one or
more constants separated by commas.
Examples:
#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
Note: Values are automatically assigned to the constants in an enumeration.
The first constant following an #enum directive is assigned the value 0,
the second is assigned the value 1, and so on.
STRINGS STRINGS
A string is a consecutive series of characters terminated by an ASCII null A string is a consecutive series of characters terminated by an ASCII null
@ -250,6 +233,25 @@ Examples:
const #BITS = %01010101; const #BITS = %01010101;
const #ZED = 'Z'; const #ZED = 'Z';
ENUMERATIONS
An enumeration is a sequential list of constants. Enumerations are used to
generate sets of related but distinct values.
An enumeration is defined using an enum statement. When using the enum
keyword, it is followed by a { character, one or more constant names
separated by commas, and a } character. The enum statement is terminated
with a semicolon.
Examples:
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};
Note: Values are automatically assigned to the constants in an enumeration.
The first constant following an #enum directive is assigned the value 0,
the second is assigned the value 1, and so on.
DECLARATIONS DECLARATIONS
A declaration statement consists of type keyword (char or void) followed A declaration statement consists of type keyword (char or void) followed

View File

@ -22,8 +22,9 @@ In C02, all constant names are prefixed with a # symbol.
ENUMERATION ENUMERATION
Instead of the enum keyword, C02 uses the #enum directive. Values may The syntax of an enum statement in C02 is similar to standard C, except
not be specified when defining enumerated constants. that no type name is specified and values may not be explicitly assigned
to the enumerated constants.
DECLARATIONS DECLARATIONS

View File

@ -70,6 +70,29 @@ void pconst(int m) {
DEBUG("Constant Declaration Completed\n", 0) DEBUG("Constant Declaration Completed\n", 0)
} }
/* Parse Enum Declaration*/
void penum(int m) {
int enmval = 0;
DEBUG("Processing Enum Declarations\n", 0)
if (m != MTNONE) ERROR("Illegal Modifier %d in Enum Definition", m, EXIT_FAILURE)
expect('{');
do {
getwrd(); //get defined identifier
DEBUG("Enumerating '%s'\n", word)
strncpy(defnam[defcnt], word, VARLEN);
setlbl(word); //Set label Assembler Line
defval[defcnt++] = enmval; //Set Value
sprintf(value, "%d", enmval);
asmlin(EQUOP, value); //Write Definition
DEBUG("Defined as '%s'\n", value)
enmval++;
} while (look(','));
expect('}');
expect(';');
DEBUG("Enum Declaration Completed\n", 0)
}
/* Parse Variable/Function Declaration*/ /* Parse Variable/Function Declaration*/
void pdecl(int m, int t) { void pdecl(int m, int t) {
DEBUG("Processing variable declarations(s) of type %d\n", t) DEBUG("Processing variable declarations(s) of type %d\n", t)
@ -91,6 +114,7 @@ void pdecl(int m, int t) {
int ptype(int m) { int ptype(int m) {
int result = TRUE; int result = TRUE;
if (wordis("CONST")) pconst(m); //Parse 'const' declaration 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("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
else result = FALSE; else result = FALSE;

View File

@ -161,7 +161,6 @@ void addvar(int m, int t) {
varcnt++; //Increment Variable Counter varcnt++; //Increment Variable Counter
} }
/* Write Variable Table */ /* Write Variable Table */
void vartbl(void) { void vartbl(void) {
int i, j; int i, j;

View File

@ -7,8 +7,10 @@ const #TRUE = $FF, #FALSE = 0;
const #BITS = %01010101; const #BITS = %01010101;
const #ZED = 'Z'; const #ZED = 'Z';
#enum SOLO enum {SOLO};
#enum ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN 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 a = {#TRUE, #FALSE};
char b; char b;

View File

@ -11,6 +11,12 @@ const #TRUE = $FF, #FALSE = 0;
const #BITS = %01010101; const #BITS = %01010101;
const #ZED = 'Z'; const #ZED = 'Z';
/* Enumerations */
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};
/* Variable Types */ /* Variable Types */
char b , i; //byte type has been removed char b , i; //byte type has been removed
char c,d,f; //a char is an unsigned 8 bit number char c,d,f; //a char is an unsigned 8 bit number