Execute semantic actions for optionals only if the optional exists

This commit is contained in:
Dane Johnson 2022-01-02 11:34:47 -06:00
parent c3b7cc58f0
commit bf55252b9a
2 changed files with 13 additions and 15 deletions

View File

@ -25,7 +25,7 @@ struct Choice {
char *id; char *id;
statchange_t *statchange; statchange_t *statchange;
Choice(char*, statcheck_t *statcheck, statchange_t *statchange); Choice(int);
}; };
typedef std::map<int, Choice*> ChoiceList; typedef std::map<int, Choice*> ChoiceList;

View File

@ -60,20 +60,18 @@ Ending = 'THE END' Newline
ChoiceList = c:Choice l:ChoiceList { $$ = l; (*$$.choices)[c.choice->option] = c.choice; } ChoiceList = c:Choice l:ChoiceList { $$ = l; (*$$.choices)[c.choice->option] = c.choice; }
| c:Choice { $$.choices = new ChoiceList(); (*$$.choices)[c.choice->option] = c.choice; } | c:Choice { $$.choices = new ChoiceList(); (*$$.choices)[c.choice->option] = c.choice; }
Choice = b:Bullet f:Freetext r:Redirect Choice = b:Bullet
{ f:Freetext { b.choice->flavor = f.string; }
$$ = r; (ck:StatCheck { b.choice->statcheck = ck.statcheck; })?
$$.choice->option = b.num; '[' i:Identifier ']' { b.choice->id = i.string; } -
$$.choice->flavor = f.string; (cg:StatChange { b.choice->statchange = cg.statchange; })?
} Newline { $$ = b; }
;
Bullet = < [0-9]+ > ')' - { $$.num = atoi(yytext); } Bullet = < [0-9]+ > ')' - { $$.choice = new Choice(atoi(yytext)); }
Freetext = < ([^[<])+ > { $$.string = strndup(yytext, yyleng); } Freetext = < ([^[<])+ > { $$.string = strndup(yytext, yyleng); }
Redirect = ck:StatCheck? '[' i:Identifier ']' - cg:StatChange? Newline
{ $$.choice = new Choice(i.string, ck.statcheck, cg.statchange); }
StatCheck = '<' n:StatName v:StatVal r:StatRel '>' - StatCheck = '<' n:StatName v:StatVal r:StatRel '>' -
{ $$.statcheck = new statcheck_t{n.string, v.num, r.num}; } { $$.statcheck = new statcheck_t{n.string, v.num, r.num}; }
@ -96,10 +94,10 @@ Newline = '\r\n' | '\r' | '\n'
%% %%
Choice::Choice(char *id, statcheck_t *statcheck, statchange_t *statchange) { Choice::Choice(int option) {
this->id = id; this->option = option;
this->statcheck = statcheck; this->statcheck = NULL;
this->statchange = statchange; this->statchange = NULL;
} }
Footer::Footer() { Footer::Footer() {