-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add conditional/ternary operator #246
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1743,5 +1743,19 @@ describe('Parser', () => { | |
expect(parsed[2]).toBe(0); | ||
}); | ||
}); | ||
describe('Conditional Operator', () => { | ||
test('roll `1?5:0', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. Could you also add tests to check against ternaries with other value types (ie. Dice, math formulas etc), in condition, and the true / false expressions? |
||
const parsed = Parser.parse('1?5:0'); | ||
|
||
expect(parsed).toBeInstanceOf(Array); | ||
expect(parsed).toHaveLength(5); | ||
|
||
expect(parsed[0]).toBe(1); | ||
expect(parsed[1]).toEqual('?'); | ||
expect(parsed[2]).toBe(5); | ||
expect(parsed[3]).toEqual(':'); | ||
expect(parsed[4]).toBe(0); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,34 @@ describe('Rolling', () => { | |
// remove the spy | ||
spy.mockRestore(); | ||
}); | ||
|
||
test('roll `(d6>3)?1:0', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, looking good. Would you also be able to add tests to ensure that it correctly handles more advanced expressions as well? |
||
const rolls = new RollResults([4]); | ||
const spy = jest.spyOn(StandardDice.prototype, 'roll') | ||
.mockImplementationOnce(() => rolls); | ||
const roll = roller.roll('(d6>3)?1:0'); | ||
|
||
expect(roll).toBeInstanceOf(DiceRoll); | ||
expect(roll.notation).toEqual('(d6>3)?1:0'); | ||
|
||
expect(roll.rolls).toHaveLength(7); | ||
expect(roll.rolls[0]).toEqual('('); | ||
expect(roll.rolls[1]).toEqual(rolls); | ||
expect(roll.rolls[2]).toEqual(')'); | ||
expect(roll.rolls[3]).toEqual('?'); | ||
expect(roll.rolls[4]).toBe(1); | ||
expect(roll.rolls[5]).toEqual(':'); | ||
expect(roll.rolls[6]).toBe(0); | ||
|
||
expect(roll.total).toBe(1); | ||
expect(roll.output).toEqual('(d6>3)?1:0: ([4])?1:0 = 1'); | ||
expect(roll.minTotal).toEqual(0); | ||
expect(roll.maxTotal).toEqual(1); | ||
expect(roll.averageTotal).toEqual(0.5); | ||
|
||
// remove the spy | ||
spy.mockRestore(); | ||
}); | ||
}); | ||
|
||
describe('Group rolls', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would allow ternary expressions without the second / false expression, which would be invalid. eg.
? 10 :
I wonder if it might be worth moving this up to the
Expression
clause on line 148:rpg-dice-roller/src/parser/grammars/grammar.pegjs
Lines 148 to 162 in f1e0638
Possibly inside the
tail
.That way, we can ensure that it always has a
Factor
after the:
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion. I'll look into implementing it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After further investigation my understanding is that if the operator is
? 10 :
like in your example, the greater expression would be[factor] ? 10 : [factor]
. I might be missing something, but I don't see how missing the 'false' value is possible.This is indeed restricted by the fact that the 'false' value is a Factor, while the 'true' value may be an Expression. Could this be fixed with a more recursive definition of Expression? Something akin to
After changing this locally, the existing tests are still passing. I obviously do not know the project like you do, but what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true for that section, where we explicitly have a
Factor
after theOperator
, but it means that we'd need to ensure that whereverOperator
is used, we have aFactor
afterwards that is suitable to the ternary operator.Unfortunately, this isn't the case, as
Operator
is also used forIntegerOrExpression
:rpg-dice-roller/src/parser/grammars/grammar.pegjs
Lines 143 to 145 in f1e0638
Where ternary operators could cause issues if they include Dice rolls, because the system can't handle things like
(d8)d10
or1d(d10)
(Discussed in #206).Based on the change to
Operator
, this would be parsed as valid grammar, but will throw an error when run:I also think the
tail
part ofExpression
will now allow things like:Because it allow repetition, and there's no checks to ensure that the part before the
?
is actually a true/false expression (Although I'm not too sure we need to care about the latter).I think it needs to ensure the ternary can't be repeated like that or, if it is, the inner parts are surrounded in parenthesis. e,g:
After thinking about it for a while, I think it might actually be better to move it into the
Factor
definition. That way it's available either side of a normalOperator
. e.g.: