Skip to content

Commit 1766fb1

Browse files
committed
Remove alternative syntaxes for if, switch, for, while and do-while
This includes removal of keywords `*begin`, `*end` and `*then`.
1 parent 1e43ec0 commit 1766fb1

File tree

3 files changed

+38
-57
lines changed

3 files changed

+38
-57
lines changed

source/compiler/sc.h

-3
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ enum {
402402
/* reserved words (statements) */
403403
t__ADDRESSOF,
404404
tASSERT,
405-
tBEGIN,
406405
tBREAK,
407406
tCASE,
408407
tCHAR,
@@ -413,7 +412,6 @@ enum {
413412
tDO,
414413
tELSE,
415414
t__EMIT,
416-
tEND,
417415
tENUM,
418416
tEXIT,
419417
tFOR,
@@ -436,7 +434,6 @@ enum {
436434
tSTOCK,
437435
tSWITCH,
438436
tTAGOF,
439-
tTHEN,
440437
tWHILE,
441438

442439
/* compiler directives */

source/compiler/sc1.c

+35-51
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ static void destructsymbols(symbol *root,int level);
122122
static constvalue *find_constval_byval(constvalue_root *table,cell val);
123123
static symbol *fetchlab(char *name);
124124
static void statement(int *lastindent,int allow_decl);
125-
static void compound(int stmt_sameline,int starttok);
126-
static int test(int label,int parens,int invert);
125+
static void compound(int stmt_sameline);
126+
static int test(int label,int needparens,int invert);
127127
static int doexpr(int comma,int chkeffect,int allowarray,int mark_endexpr,
128128
int *tag,symbol **symptr,int chkfuncresult,cell *val);
129129
static void doassert(void);
@@ -205,12 +205,6 @@ typedef struct {
205205
OPCODE_PROC func;
206206
} EMIT_OPCODE;
207207

208-
enum {
209-
TEST_PLAIN, /* no parentheses */
210-
TEST_THEN, /* '(' <expr> ')' or <expr> 'then' */
211-
TEST_DO, /* '(' <expr> ')' or <expr> 'do' */
212-
TEST_OPT, /* '(' <expr> ')' or <expr> */
213-
};
214208
static int lastst = 0; /* last executed statement type */
215209
static int endlessloop= 0; /* nesting level of endless loop */
216210
static int rettype = 0; /* the type that a "return" expression should have */
@@ -5709,10 +5703,9 @@ static void statement(int *lastindent,int allow_decl)
57095703
} /* if */
57105704
break;
57115705
case '{':
5712-
case tBEGIN:
57135706
save=fline;
57145707
if (!matchtoken('}')) /* {} is the empty statement */
5715-
compound(save==fline,tok);
5708+
compound(save==fline);
57165709
/* lastst (for "last statement") does not change */
57175710
break;
57185711
case ';':
@@ -5810,24 +5803,23 @@ static void statement(int *lastindent,int allow_decl)
58105803
} /* switch */
58115804
}
58125805

5813-
static void compound(int stmt_sameline,int starttok)
5806+
static void compound(int stmt_sameline)
58145807
{
58155808
int indent=-1;
58165809
cell save_decl=declared;
58175810
int count_stmt=0;
58185811
int block_start=fline; /* save line where the compound block started */
5819-
int endtok;
58205812

58215813
/* if there is more text on this line, we should adjust the statement indent */
58225814
if (stmt_sameline) {
58235815
int i;
58245816
const unsigned char *p=lptr;
58255817
/* go back to the opening brace */
5826-
while (*p!=starttok) {
5818+
while (*p!='{') {
58275819
assert(p>pline);
58285820
p--;
58295821
} /* while */
5830-
assert(*p==starttok); /* it should be found */
5822+
assert(*p=='{'); /* it should be found */
58315823
/* go forward, skipping white-space */
58325824
p++;
58335825
while (*p<=' ' && *p!='\0')
@@ -5841,9 +5833,8 @@ static void compound(int stmt_sameline,int starttok)
58415833
stmtindent++;
58425834
} /* if */
58435835

5844-
endtok=(starttok=='{') ? '}' : tEND;
58455836
pc_nestlevel+=1; /* increase compound statement level */
5846-
while (matchtoken(endtok)==0){/* repeat until compound statement is closed */
5837+
while (matchtoken('}')==0) { /* repeat until compound statement is closed */
58475838
if (!freading){
58485839
error(30,block_start); /* compound block not closed at end of file */
58495840
break;
@@ -5966,12 +5957,12 @@ SC_FUNC int constexpr(cell *val,int *tag,symbol **symptr)
59665957
*
59675958
* Global references: sc_intest (altered, but restored upon termination)
59685959
*/
5969-
static int test(int label,int parens,int invert)
5960+
static int test(int label,int needparens,int invert)
59705961
{
59715962
int index,tok;
59725963
cell cidx;
59735964
int ident,tag;
5974-
int endtok;
5965+
int foundparen;
59755966
short save_intest;
59765967
cell constval;
59775968
symbol *sym;
@@ -5988,15 +5979,9 @@ static int test(int label,int parens,int invert)
59885979

59895980
save_intest=sc_intest;
59905981
sc_intest=TRUE;
5991-
endtok=0;
5992-
if (parens!=TEST_PLAIN) {
5993-
if (matchtoken('('))
5994-
endtok=')';
5995-
else if (parens==TEST_THEN)
5996-
endtok=tTHEN;
5997-
else if (parens==TEST_DO)
5998-
endtok=tDO;
5999-
} /* if */
5982+
foundparen=FALSE;
5983+
if (needparens)
5984+
foundparen=needtoken('(');
60005985
do {
60015986
stgget(&index,&cidx); /* mark position (of last expression) in
60025987
* code generator */
@@ -6009,8 +5994,12 @@ static int test(int label,int parens,int invert)
60095994
markexpr(sEXPR,NULL,0);
60105995
} /* if */
60115996
} while (tok); /* do */
6012-
if (endtok!=0)
6013-
needtoken(endtok);
5997+
if (needparens) {
5998+
if (foundparen)
5999+
needtoken(')');
6000+
else
6001+
matchtoken(')');
6002+
} /* if */
60146003
if (ident==iARRAY || ident==iREFARRAY) {
60156004
char *ptr=(sym!=NULL) ? sym->name : "-unknown-";
60166005
error(33,ptr); /* array must be indexed */
@@ -6060,7 +6049,7 @@ static int doif(void)
60606049
lastst=0; /* reset the last statement */
60616050
ifindent=stmtindent; /* save the indent of the "if" instruction */
60626051
flab1=getlabel(); /* get label number for false branch */
6063-
test(flab1,TEST_THEN,FALSE); /* get expression, branch to flab1 if false */
6052+
test(flab1,TRUE,FALSE); /* get expression, branch to flab1 if false */
60646053
statement(NULL,FALSE); /* if true, do a statement */
60656054
if (!matchtoken(tELSE)) { /* if...else ? */
60666055
setlabel(flab1); /* no, simple if..., print false label */
@@ -6115,7 +6104,7 @@ static int dowhile(void)
61156104
* so any assignments made inside the loop control expression
61166105
* could be cleaned up later */
61176106
pc_loopcond=tWHILE;
6118-
endlessloop=test(wq[wqEXIT],TEST_DO,FALSE);/* branch to wq[wqEXIT] if false */
6107+
endlessloop=test(wq[wqEXIT],TRUE,FALSE); /* branch to wq[wqEXIT] if false */
61196108
pc_loopcond=0;
61206109
pc_nestlevel--;
61216110
statement(NULL,FALSE); /* if so, do a statement */
@@ -6157,7 +6146,7 @@ static int dodo(void)
61576146
* so any assignments made inside the loop control expression
61586147
* could be cleaned up later */
61596148
pc_loopcond=tDO;
6160-
endlessloop=test(wq[wqEXIT],TEST_OPT,FALSE);
6149+
endlessloop=test(wq[wqEXIT],TRUE,FALSE);
61616150
pc_loopcond=0;
61626151
pc_nestlevel--;
61636152
clearassignments(pc_nestlevel+1);
@@ -6178,7 +6167,7 @@ static int dofor(void)
61786167
int wq[wqSIZE],skiplab;
61796168
cell save_decl;
61806169
int save_nestlevel,save_endlessloop,save_numloopvars;
6181-
int index,endtok;
6170+
int index;
61826171
int *ptr;
61836172
int loopline=fline;
61846173
symstate *loopvars=NULL;
@@ -6191,7 +6180,7 @@ static int dofor(void)
61916180

61926181
addwhile(wq);
61936182
skiplab=getlabel();
6194-
endtok= matchtoken('(') ? ')' : tDO;
6183+
needtoken('(');
61956184
pc_nestlevel++; /* temporarily increase the "compound statement" nesting level,
61966185
* so any assignments made inside the loop initialization, control
61976186
* expression and increment blocks could be cleaned up later */
@@ -6236,14 +6225,14 @@ static int dofor(void)
62366225
endlessloop=1;
62376226
} else {
62386227
pc_loopcond=tFOR;
6239-
endlessloop=test(wq[wqEXIT],TEST_PLAIN,FALSE);/* expression 2 (jump to wq[wqEXIT] if false) */
6228+
endlessloop=test(wq[wqEXIT],FALSE,FALSE); /* expression 2 (jump to wq[wqEXIT] if false) */
62406229
pc_loopcond=0;
62416230
needtoken(';');
62426231
} /* if */
62436232
stgmark((char)(sEXPRSTART+1)); /* mark start of 3th expression in stage */
6244-
if (!matchtoken(endtok)) {
6233+
if (!matchtoken(')')) {
62456234
doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE,NULL); /* expression 3 */
6246-
needtoken(endtok);
6235+
needtoken(')');
62476236
} /* if */
62486237
stgmark(sENDREORDER); /* mark end of reversed evaluation */
62496238
stgout(index);
@@ -6290,7 +6279,7 @@ static int doswitch(void)
62906279
{
62916280
int lbl_table,lbl_exit,lbl_case;
62926281
int swdefault,casecount;
6293-
int tok,endtok;
6282+
int tok;
62946283
int swtag,csetag;
62956284
int allterminal;
62966285
int enumsymcount;
@@ -6304,11 +6293,11 @@ static int doswitch(void)
63046293
char labelname[sNAMEMAX+1];
63056294
symstate *assignments=NULL;
63066295

6307-
endtok= matchtoken('(') ? ')' : tDO;
6296+
needtoken('(');
63086297
ident=doexpr(TRUE,FALSE,FALSE,FALSE,&swtag,NULL,TRUE,NULL); /* evaluate switch expression */
63096298
if (ident==iCONSTEXPR)
63106299
error(243); /* redundant code: switch control expression is constant */
6311-
needtoken(endtok);
6300+
needtoken(')');
63126301
/* generate the code for the switch statement, the label is the address
63136302
* of the case table (to be generated later).
63146303
*/
@@ -6327,12 +6316,7 @@ static int doswitch(void)
63276316
enumsym=NULL;
63286317
} /* if */
63296318

6330-
if (matchtoken(tBEGIN)) {
6331-
endtok=tEND;
6332-
} else {
6333-
endtok='}';
6334-
needtoken('{');
6335-
} /* if */
6319+
needtoken('{');
63366320
lbl_exit=getlabel(); /* get label number for jumping out of switch */
63376321
swdefault=FALSE;
63386322
allterminal=TRUE; /* assume that all cases end with terminal statements */
@@ -6437,13 +6421,13 @@ static int doswitch(void)
64376421
allterminal &= isterminal(lastst);
64386422
break;
64396423
default:
6440-
if (tok!=endtok) {
6424+
if (tok!='}') {
64416425
error(2);
64426426
indent_nowarn=TRUE; /* disable this check */
6443-
tok=endtok; /* break out of the loop after an error */
6427+
tok='}'; /* break out of the loop after an error */
64446428
} /* if */
64456429
} /* switch */
6446-
} while (tok!=endtok);
6430+
} while (tok!='}');
64476431
restoreassignments(pc_nestlevel+1,assignments);
64486432

64496433
if (enumsym!=NULL && swdefault==FALSE && enumsym->x.tags.unique-enumsymcount<=2) {
@@ -6528,7 +6512,7 @@ static void doassert(void)
65286512

65296513
if ((sc_debug & sCHKBOUNDS)!=0) {
65306514
flab1=getlabel(); /* get label number for "OK" branch */
6531-
test(flab1,TEST_PLAIN,TRUE);/* get expression and branch to flab1 if true */
6515+
test(flab1,FALSE,TRUE); /* get expression and branch to flab1 if true */
65326516
insert_dbgline(fline); /* make sure we can find the correct line number */
65336517
ffabort(xASSERTION);
65346518
setlabel(flab1);
@@ -8324,7 +8308,7 @@ static void dostate(void)
83248308
if (matchtoken('(')) {
83258309
flabel=getlabel(); /* get label number for "false" branch */
83268310
pc_docexpr=TRUE; /* attach expression as a documentation string */
8327-
test(flabel,TEST_PLAIN,FALSE);/* get expression, branch to flabel if false */
8311+
test(flabel,FALSE,FALSE); /* get expression, branch to flabel if false */
83288312
pc_docexpr=FALSE;
83298313
needtoken(')');
83308314
} else {

source/compiler/scvars.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ SC_VDEFINE char *sc_tokens[] = {
112112
"*=", "/=", "%=", "+=", "-=", "<<=", ">>>=", ">>=", "&=", "^=", "|=",
113113
"||", "&&", "==", "!=", "<=", ">=", "<<", ">>>", ">>", "++", "--",
114114
"...", "..",
115-
"__addressof", "assert", "*begin", "break", "case", "char", "const", "continue",
116-
"default", "defined", "do", "else", "__emit", "*end", "enum", "exit", "for",
115+
"__addressof", "assert", "break", "case", "char", "const", "continue",
116+
"default", "defined", "do", "else", "__emit", "enum", "exit", "for",
117117
"forward", "goto", "if", "__nameof", "native", "new", "operator", "__pragma",
118118
"public", "return", "sizeof", "sleep", "state", "static", "__static_assert",
119-
"__static_check", "stock", "switch", "tagof", "*then", "while",
119+
"__static_check", "stock", "switch", "tagof", "while",
120120
"#assert", "#define", "#else", "#elseif", "#emit", "#endif", "#endinput",
121121
"#endscript", "#error", "#file", "#if", "#include", "#line", "#pragma",
122122
"#tryinclude", "#undef", "#warning",

0 commit comments

Comments
 (0)