From 6d2aa15a3f369d65ae56449d566de56667bfe177 Mon Sep 17 00:00:00 2001 From: VVWVV Date: Mon, 23 Jan 2017 14:20:33 +0300 Subject: [PATCH 1/2] #134: Proposal: enum limited to single file scope Signed-off-by: VVWVV --- source/compiler/sc1.c | 19 ++++++++++++++++++- source/compiler/sc5.c | 3 ++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index af736bd..04f40bc 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -2777,6 +2777,12 @@ static void decl_enum(int vclass) cell increment,multiplier; constvalue *enumroot; symbol *enumsym; + int static_token=matchtoken(tSTATIC); + short filenum; + + filenum=fcurrent; + if (static_token && vclass==sLOCAL) + error(92); /* get an explicit tag, if any (we need to remember whether an explicit * tag was passed, even if that explicit tag was "_:", so we cannot call @@ -2820,8 +2826,13 @@ static void decl_enum(int vclass) if (strlen(enumname)>0) { /* already create the root symbol, so the fields can have it as their "parent" */ enumsym=add_constant(enumname,0,vclass,tag); - if (enumsym!=NULL) + if (enumsym!=NULL) { enumsym->usage |= uENUMROOT; + + /* for enum static */ + if (static_token) + enumsym->fnumber=filenum; + } /* start a new list for the element names */ if ((enumroot=(constvalue*)malloc(sizeof(constvalue)))==NULL) error(103); /* insufficient memory (fatal error) */ @@ -2831,6 +2842,7 @@ static void decl_enum(int vclass) enumroot=NULL; } /* if */ + needtoken('{'); /* go through all constants */ value=0; /* default starting value */ @@ -2868,6 +2880,11 @@ static void decl_enum(int vclass) sym->dim.array.length=size; sym->dim.array.level=0; sym->parent=enumsym; + + /* for enum static */ + if (static_token) + sym->fnumber=filenum; + /* add the constant to a separate list as well */ if (enumroot!=NULL) { sym->usage |= uENUMFIELD; diff --git a/source/compiler/sc5.c b/source/compiler/sc5.c index c4a5543..4dc56c4 100644 --- a/source/compiler/sc5.c +++ b/source/compiler/sc5.c @@ -128,7 +128,8 @@ static char *errmsg[] = { /*088*/ "public variables and local variables may not have states (symbol \"%s\")\n", /*089*/ "state variables may not be initialized (symbol \"%s\")\n", /*090*/ "public functions may not return arrays (symbol \"%s\")\n", -/*091*/ "ambiguous constant; tag override is required (symbol \"%s\")\n" +/*091*/ "ambiguous constant; tag override is required (symbol \"%s\")\n", +/*092*/ "enumerations with the static operator may not be declared in a compound block\n" }; static char *fatalmsg[] = { From 0dd5a652aa40564089f220c8928dfbd6c435151d Mon Sep 17 00:00:00 2001 From: VVWVV Date: Mon, 23 Jan 2017 18:24:52 +0300 Subject: [PATCH 2/2] Add support of 'static enum' and 'enum static' Signed-off-by: VVWVV --- source/compiler/sc1.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 04f40bc..44aadaf 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -89,7 +89,7 @@ static void declglb(char *firstname,int firsttag,int fpublic,int fstatic, int stock,int fconst); static int declloc(int fstatic); static void decl_const(int table); -static void decl_enum(int table); +static void decl_enum(int table,int enum_static); static cell needsub(int *tag,constvalue **enumroot); static void initials(int ident,int tag,cell *size,int dim[],int numdim, constvalue *enumroot); @@ -1600,22 +1600,26 @@ static void parse(void) declglb(NULL,0,fpublic,fstatic,fstock,fconst); break; case tSTATIC: - /* This can be a static function or a static global variable; we know - * which of the two as soon as we have parsed up to the point where an - * opening paranthesis of a function would be expected. To back out after - * deciding it was a declaration of a static variable after all, we have - * to store the symbol name and tag. - */ - if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) { - assert(!fpublic); - declfuncvar(fpublic,fstatic,fstock,fconst); + if (matchtoken(tENUM)) { + decl_enum(sGLOBAL,TRUE); + } else { + /* This can be a static function or a static global variable; we know + * which of the two as soon as we have parsed up to the point where an + * opening paranthesis of a function would be expected. To back out after + * deciding it was a declaration of a static variable after all, we have + * to store the symbol name and tag. + */ + if (getclassspec(tok,&fpublic,&fstatic,&fstock,&fconst)) { + assert(!fpublic); + declfuncvar(fpublic,fstatic,fstock,fconst); + } /* if */ } /* if */ break; case tCONST: decl_const(sGLOBAL); break; case tENUM: - decl_enum(sGLOBAL); + decl_enum(sGLOBAL,matchtoken(tSTATIC)); break; case tPUBLIC: /* This can be a public function or a public variable; see the comment @@ -2768,7 +2772,7 @@ static void decl_const(int vclass) /* decl_enum - declare enumerated constants * */ -static void decl_enum(int vclass) +static void decl_enum(int vclass,int enum_static) { char enumname[sNAMEMAX+1],constname[sNAMEMAX+1]; cell val,value,size; @@ -2777,11 +2781,10 @@ static void decl_enum(int vclass) cell increment,multiplier; constvalue *enumroot; symbol *enumsym; - int static_token=matchtoken(tSTATIC); short filenum; filenum=fcurrent; - if (static_token && vclass==sLOCAL) + if (enum_static && vclass==sLOCAL) error(92); /* get an explicit tag, if any (we need to remember whether an explicit @@ -2830,7 +2833,7 @@ static void decl_enum(int vclass) enumsym->usage |= uENUMROOT; /* for enum static */ - if (static_token) + if (enum_static) enumsym->fnumber=filenum; } /* start a new list for the element names */ @@ -2882,7 +2885,7 @@ static void decl_enum(int vclass) sym->parent=enumsym; /* for enum static */ - if (static_token) + if (enum_static) sym->fnumber=filenum; /* add the constant to a separate list as well */ @@ -5070,7 +5073,7 @@ static void statement(int *lastindent,int allow_decl) decl_const(sLOCAL); break; case tENUM: - decl_enum(sLOCAL); + decl_enum(sLOCAL,FALSE); break; default: /* non-empty expression */ sc_allowproccall=optproccall;