public object VisitLiteralExpr(Expr.Literal expr) { if (expr.IsSubstitution) { if (!(expr.Value is string)) { throw new InterpreterError("Substitution must be a string."); } if (_context == null || !(_context.ContainsKey(expr.Value.ToString()))) { throw new InterpreterError($"Context does not contain key '{expr.Value}'"); } expr.Value = _context[expr.Value.ToString()]; expr.IsSubstitution = false; } return(expr.Value); }
public string VisitLiteralExpr(Expr.Literal expr) { if (expr.Value is double d) { return(string.IsNullOrEmpty(expr.Label) ? d.ToString(NumberFormatInfo.InvariantInfo) : $"{d.ToString(NumberFormatInfo.InvariantInfo)}[{expr.Label}]"); } if (expr.IsSubstitution) { if (!(expr.Value is string)) { throw new Parser.ParserException("Substitution must be a string."); } if (_context == null || !_context.ContainsKey(expr.Value.ToString())) { return($"(@ {expr.Value} !KEYNOTFOUND!)"); } return($"(@ {expr.Value} {_context[expr.Value.ToString()]})"); } return(string.IsNullOrEmpty(expr.Label) ? expr.Value.ToString() : $"{expr.Value}[{expr.Label}]"); }
public string VisitLiteralExpr(Expr.Literal expr) { return(string.IsNullOrEmpty(expr.Label) ? expr.Value.ToString() : $"{expr.Value}[{expr.Label}]"); }
private void GetDicePoolModifiers(out Expr.DicePool.DicePoolMod mod, out Expr.DicePool.ModOperator modOp, out Expr modValue) { Token tMod = Consume(TokenType.Identifier, "Expect dice pool modifier after dice pool."); switch (tMod.Lexeme) { case "kh": mod = Expr.DicePool.DicePoolMod.KeepHighest; break; case "kl": mod = Expr.DicePool.DicePoolMod.KeepLowest; break; case "dh": mod = Expr.DicePool.DicePoolMod.DropHighest; break; case "dl": mod = Expr.DicePool.DicePoolMod.DropLowest; break; case "cs": mod = Expr.DicePool.DicePoolMod.CountSuccesses; break; default: throw new ParserException($"Expected dice pool modifier, got '{tMod}'."); } // Now there should be a operator Token tModOp = Peek(); modValue = null; switch (tModOp.Type) { case TokenType.Equal: modOp = Expr.DicePool.ModOperator.Equal; Advance(); break; case TokenType.Less: modOp = Expr.DicePool.ModOperator.Less; Advance(); break; case TokenType.LessEqual: modOp = Expr.DicePool.ModOperator.LessEqual; Advance(); break; case TokenType.Greater: modOp = Expr.DicePool.ModOperator.Greater; Advance(); break; case TokenType.GreaterEqual: modOp = Expr.DicePool.ModOperator.GreaterEqual; Advance(); break; case TokenType.Number: modOp = Expr.DicePool.ModOperator.Equal; modValue = Primary(); break; default: modOp = Expr.DicePool.ModOperator.Equal; modValue = new Expr.Literal(1.0d); break; //throw new ParserException($"Expected dice mod operator, got '{tModOp}'."); } if (modValue == null) { modValue = Call(); } if (mod == Expr.DicePool.DicePoolMod.KeepHighest || mod == Expr.DicePool.DicePoolMod.KeepLowest || mod == Expr.DicePool.DicePoolMod.DropHighest || mod == Expr.DicePool.DicePoolMod.DropLowest) { if (modOp != Expr.DicePool.ModOperator.Equal) { throw new ParserException($"{mod} only makes sense with operator equal."); } } }
private void GetDiceModifiers(Expr faces, out Expr.Dice.DiceMod mod, out Expr.Dice.ModOperator modOp, out Expr modValue) { Token tMod = Previous(); switch (tMod.Lexeme) { case "r": mod = Expr.Dice.DiceMod.ReRoll; break; case "!": case "x": mod = Expr.Dice.DiceMod.Explode; break; case "!!": case "cx": mod = Expr.Dice.DiceMod.CompoundExplode; break; case "cs": mod = Expr.Dice.DiceMod.CountSuccesses; break; case "ms": mod = Expr.Dice.DiceMod.MarginOfSuccess; break; case "kh": mod = Expr.Dice.DiceMod.KeepHighest; break; case "kl": mod = Expr.Dice.DiceMod.KeepLowest; break; case "dh": mod = Expr.Dice.DiceMod.DropHighest; break; case "dl": mod = Expr.Dice.DiceMod.DropLowest; break; default: throw new ParserException($"Expected dice modifier, got '{tMod}'."); } // Now there should be a operator Token tModOp = Peek(); modValue = null; switch (tModOp.Type) { case TokenType.Equal: modOp = Expr.Dice.ModOperator.Equal; Advance(); break; case TokenType.Less: modOp = Expr.Dice.ModOperator.Less; Advance(); break; case TokenType.LessEqual: modOp = Expr.Dice.ModOperator.LessEqual; Advance(); break; case TokenType.Greater: modOp = Expr.Dice.ModOperator.Greater; Advance(); break; case TokenType.GreaterEqual: modOp = Expr.Dice.ModOperator.GreaterEqual; Advance(); break; case TokenType.Number: modOp = Expr.Dice.ModOperator.Equal; modValue = Primary(); break; default: if (mod == Expr.Dice.DiceMod.CountSuccesses) { modOp = Expr.Dice.ModOperator.Equal; modValue = faces; } else { modOp = Expr.Dice.ModOperator.Equal; modValue = new Expr.Literal(1.0d); } break; } if (modValue == null) { modValue = Call(); } if (mod == Expr.Dice.DiceMod.KeepHighest || mod == Expr.Dice.DiceMod.KeepLowest || mod == Expr.Dice.DiceMod.DropHighest || mod == Expr.Dice.DiceMod.DropLowest) { if (modOp != Expr.Dice.ModOperator.Equal) { throw new ParserException($"{mod} only makes sense with operator equal."); } } }