From 4e421c80952527aeb53df8e775f2c51f34e51273 Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Wed, 7 Mar 2018 12:00:33 -0500 Subject: [PATCH] Added enum keyword --- doc/c02.txt | 36 +++++++++++++++++++----------------- doc/c02vsC.txt | 5 +++-- src/dclrtn.c | 24 ++++++++++++++++++++++++ src/vars.c | 1 - test/const.c02 | 6 ++++-- test/test.c02 | 6 ++++++ 6 files changed, 56 insertions(+), 22 deletions(-) diff --git a/doc/c02.txt b/doc/c02.txt index cb04a0d..98f5b4c 100644 --- a/doc/c02.txt +++ b/doc/c02.txt @@ -167,23 +167,6 @@ Examples: 'A' 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 A string is a consecutive series of characters terminated by an ASCII null @@ -250,6 +233,25 @@ Examples: const #BITS = %01010101; 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 A declaration statement consists of type keyword (char or void) followed diff --git a/doc/c02vsC.txt b/doc/c02vsC.txt index 9519aff..2b894cc 100644 --- a/doc/c02vsC.txt +++ b/doc/c02vsC.txt @@ -22,8 +22,9 @@ In C02, all constant names are prefixed with a # symbol. ENUMERATION -Instead of the enum keyword, C02 uses the #enum directive. Values may -not be specified when defining enumerated constants. +The syntax of an enum statement in C02 is similar to standard C, except +that no type name is specified and values may not be explicitly assigned +to the enumerated constants. DECLARATIONS diff --git a/src/dclrtn.c b/src/dclrtn.c index 410b2ac..cd691a5 100644 --- a/src/dclrtn.c +++ b/src/dclrtn.c @@ -70,6 +70,29 @@ void pconst(int m) { 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*/ void pdecl(int m, int 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 result = TRUE; 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; diff --git a/src/vars.c b/src/vars.c index d4aaeab..2e426af 100644 --- a/src/vars.c +++ b/src/vars.c @@ -161,7 +161,6 @@ void addvar(int m, int t) { varcnt++; //Increment Variable Counter } - /* Write Variable Table */ void vartbl(void) { int i, j; diff --git a/test/const.c02 b/test/const.c02 index cbc45e9..2234feb 100644 --- a/test/const.c02 +++ b/test/const.c02 @@ -7,8 +7,10 @@ const #TRUE = $FF, #FALSE = 0; const #BITS = %01010101; const #ZED = 'Z'; -#enum SOLO -#enum ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN +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; diff --git a/test/test.c02 b/test/test.c02 index e285952..1703da3 100644 --- a/test/test.c02 +++ b/test/test.c02 @@ -11,6 +11,12 @@ const #TRUE = $FF, #FALSE = 0; const #BITS = %01010101; 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 */ char b , i; //byte type has been removed char c,d,f; //a char is an unsigned 8 bit number