Пример #1
0
        public override GrammarParseResult VisitModifierRoll(ModifierRollContext context)
        {
            if (context == null)
            {
                return(GrammarParseResult.Unsuccessful(context.GetText()));
            }

            Debug.WriteLine($"VisitModifierRoll \"{context.GetText()}\"");

            int numAdvantage = context.ADVANTAGE().Length - context.DISADVANTAGE().Length;
            GrammarParseResult childDiceSides = VisitExpression(context.expression());

            childDiceSides.Label = "Dice Sides";
            int diceSides = childDiceSides.Value;

            GrammarParseResult result = new GrammarParseResult(context.GetText());
            string             advStr;

            if (numAdvantage == 0)
            {
                advStr = String.Empty;
            }
            else if (numAdvantage > 0)
            {
                advStr = "Highest of ";
            }
            else
            {
                advStr = "Lowest of ";
            }

            result.EvaluatedText = $"{advStr}{Math.Abs(numAdvantage)+1}d{diceSides}";
            result.Children.Add(childDiceSides);

            DiceResult diceResult = DiceUtil.RollAdvantage(numAdvantage, diceSides);

            result.Value  = diceResult.Total;
            result.Output = String.Join(", ", diceResult.Values);

            return(result);
        }
Пример #2
0
        private ExpressionContext expression(int _p)
        {
            ParserRuleContext _parentctx = Context;
            int _parentState             = State;
            ExpressionContext _localctx  = new ExpressionContext(Context, _parentState);
            ExpressionContext _prevctx   = _localctx;
            int _startState = 6;

            EnterRecursionRule(_localctx, 6, RULE_expression, _p);
            int _la;

            try {
                int _alt;
                EnterOuterAlt(_localctx, 1);
                {
                    State = 55;
                    ErrorHandler.Sync(this);
                    switch (TokenStream.LA(1))
                    {
                    case SUBTRACT:
                    {
                        _localctx = new UnaryMinusExprContext(_localctx);
                        Context   = _localctx;
                        _prevctx  = _localctx;

                        State = 40; Match(SUBTRACT);
                        State = 41; expression(8);
                    }
                    break;

                    case DICE:
                    case ADVANTAGE:
                    case DISADVANTAGE:
                    {
                        _localctx = new ModifierRollContext(_localctx);
                        Context   = _localctx;
                        _prevctx  = _localctx;
                        State     = 45;
                        ErrorHandler.Sync(this);
                        _la = TokenStream.LA(1);
                        while (_la == ADVANTAGE || _la == DISADVANTAGE)
                        {
                            {
                                {
                                    State = 42;
                                    _la   = TokenStream.LA(1);
                                    if (!(_la == ADVANTAGE || _la == DISADVANTAGE))
                                    {
                                        ErrorHandler.RecoverInline(this);
                                    }
                                    else
                                    {
                                        ErrorHandler.ReportMatch(this);
                                        Consume();
                                    }
                                }
                            }
                            State = 47;
                            ErrorHandler.Sync(this);
                            _la = TokenStream.LA(1);
                        }
                        State = 48; Match(DICE);
                        State = 49; ((ModifierRollContext)_localctx).sides = expression(7);
                    }
                    break;

                    case L_PAREN:
                    {
                        _localctx = new ParenExprContext(_localctx);
                        Context   = _localctx;
                        _prevctx  = _localctx;
                        State     = 50; Match(L_PAREN);
                        State     = 51; expression(0);
                        State     = 52; Match(R_PAREN);
                    }
                    break;

                    case INTEGER:
                    {
                        _localctx = new NumberExprContext(_localctx);
                        Context   = _localctx;
                        _prevctx  = _localctx;
                        State     = 54; Match(INTEGER);
                    }
                    break;

                    default:
                        throw new NoViableAltException(this);
                    }
                    Context.Stop = TokenStream.LT(-1);
                    State        = 71;
                    ErrorHandler.Sync(this);
                    _alt = Interpreter.AdaptivePredict(TokenStream, 8, Context);
                    while (_alt != 2 && _alt != global::Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER)
                    {
                        if (_alt == 1)
                        {
                            if (ParseListeners != null)
                            {
                                TriggerExitRuleEvent();
                            }
                            _prevctx = _localctx;
                            {
                                State = 69;
                                ErrorHandler.Sync(this);
                                switch (Interpreter.AdaptivePredict(TokenStream, 7, Context))
                                {
                                case 1:
                                {
                                    _localctx = new SumRollContext(new ExpressionContext(_parentctx, _parentState));
                                    ((SumRollContext)_localctx).num = _prevctx;
                                    PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    State = 57;
                                    if (!(Precpred(Context, 6)))
                                    {
                                        throw new FailedPredicateException(this, "Precpred(Context, 6)");
                                    }
                                    State = 58; Match(DICE);
                                    State = 59; ((SumRollContext)_localctx).sides = expression(7);
                                }
                                break;

                                case 2:
                                {
                                    _localctx = new PowExprContext(new ExpressionContext(_parentctx, _parentState));
                                    ((PowExprContext)_localctx).lhs = _prevctx;
                                    PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    State = 60;
                                    if (!(Precpred(Context, 5)))
                                    {
                                        throw new FailedPredicateException(this, "Precpred(Context, 5)");
                                    }
                                    State = 61; ((PowExprContext)_localctx).op = Match(POW);
                                    State = 62; ((PowExprContext)_localctx).rhs = expression(5);
                                }
                                break;

                                case 3:
                                {
                                    _localctx = new MultiplicationExprContext(new ExpressionContext(_parentctx, _parentState));
                                    ((MultiplicationExprContext)_localctx).lhs = _prevctx;
                                    PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    State = 63;
                                    if (!(Precpred(Context, 4)))
                                    {
                                        throw new FailedPredicateException(this, "Precpred(Context, 4)");
                                    }
                                    State = 64;
                                    ((MultiplicationExprContext)_localctx).op = TokenStream.LT(1);
                                    _la = TokenStream.LA(1);
                                    if (!((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << MULTIPLY) | (1L << DIVIDE) | (1L << MOD))) != 0)))
                                    {
                                        ((MultiplicationExprContext)_localctx).op = ErrorHandler.RecoverInline(this);
                                    }
                                    else
                                    {
                                        ErrorHandler.ReportMatch(this);
                                        Consume();
                                    }
                                    State = 65; ((MultiplicationExprContext)_localctx).rhs = expression(5);
                                }
                                break;

                                case 4:
                                {
                                    _localctx = new AdditiveExprContext(new ExpressionContext(_parentctx, _parentState));
                                    ((AdditiveExprContext)_localctx).lhs = _prevctx;
                                    PushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    State = 66;
                                    if (!(Precpred(Context, 3)))
                                    {
                                        throw new FailedPredicateException(this, "Precpred(Context, 3)");
                                    }
                                    State = 67;
                                    ((AdditiveExprContext)_localctx).op = TokenStream.LT(1);
                                    _la = TokenStream.LA(1);
                                    if (!(_la == ADD || _la == SUBTRACT))
                                    {
                                        ((AdditiveExprContext)_localctx).op = ErrorHandler.RecoverInline(this);
                                    }
                                    else
                                    {
                                        ErrorHandler.ReportMatch(this);
                                        Consume();
                                    }
                                    State = 68; ((AdditiveExprContext)_localctx).rhs = expression(4);
                                }
                                break;
                                }
                            }
                        }
                        State = 73;
                        ErrorHandler.Sync(this);
                        _alt = Interpreter.AdaptivePredict(TokenStream, 8, Context);
                    }
                }
            }
            catch (RecognitionException re) {
                _localctx.exception = re;
                ErrorHandler.ReportError(this, re);
                ErrorHandler.Recover(this, re);
            }
            finally {
                UnrollRecursionContexts(_parentctx);
            }
            return(_localctx);
        }