Is it possible to have intermediate types?
TamaBaka opened this issue · 3 comments
I actually haven't formally learned Lexer concepts before, so I don't know if this violates the principles behind Lexers. But I've been banging my head on the documentation trying to understand how I would represent something like a ternary. I got it working by having it return an object, but I wanted to know if there was something that I missed that would let me skip the boxing/unboxing.
e.g.
"TRUE ? 'Yes' : 'No'"
expr: boolexpr QMARK [d] strexpr COLON [d] strexpr
string finalExpr(bool a1, string a2, string a3)
boolexpr: BOOLVAL
bool boolExpr(Token a1)
strexpr: QUOTE [d] STRLIKE QUOTE [d]
string strExpr(Token a1)
enums:
BOOLVAL = keyword (TRUE), keyword (FALSE)
QMARK = sugar(?)
COLON = sugar(:)
QUOTE = sugar(')
STRLIKE = identifier(AlphaId)
Output will be a string.
If I tried making it like the above, I'd get an error during the build step where finalExpr expects a string for boolexpr. Is it possible for me to make it so that the parser will permit a bool for boolexpr?
Hello @TamaBaka ,
Could you please join your C# code. I have hard time to understand how your parser works and what is you final purpose.
As I understand it i would try something like
[Production("expr: BOOLVALQMARK [d] strexpr COLON [d] strexpr")]
string finalExpr(Token<yourTokenEnum> condition, string thenValue , string elseValue) {
if (condition.Value == "yes"
return thenValue;
else
return elseValue;
}
removing the boolexpr rule, which remove the need to return a bool instead of a string.
Olivier
@TamaBaka ,
CSLY has a "fully and strongly typed approach" so you can not change the type as you want it. but indeed you can use a boxing type as you suggested
Thank you