87 lines
1.9 KiB
C#
87 lines
1.9 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Dungeoneer.Lexing {
|
|
|
|
public abstract class Token {
|
|
|
|
public virtual string Format() { return Expression(); }
|
|
public abstract string Expression();
|
|
|
|
}
|
|
|
|
public class LiteralToken : Token {
|
|
public string Content { get; private set; }
|
|
|
|
public LiteralToken(string text) { Content = text; }
|
|
|
|
public override string Expression() { return Content; }
|
|
|
|
}
|
|
|
|
public class DiceToken : Token {
|
|
public int Count { get; private set; }
|
|
public int Sides { get; private set; }
|
|
private List<int> _result = null;
|
|
public List<int> Result {
|
|
get {
|
|
if(_result == null) {
|
|
_result = new List<int>();
|
|
for(int i = 0; i < Count; i++)
|
|
_result.Add(Util.Roll(Sides));
|
|
}
|
|
return _result;
|
|
}
|
|
}
|
|
public int? _sum = null;
|
|
public int Sum {
|
|
get {
|
|
if(!_sum.HasValue) {
|
|
_sum = 0;
|
|
foreach(var roll in Result)
|
|
_sum += roll;
|
|
}
|
|
return _sum.Value;
|
|
}
|
|
}
|
|
|
|
public DiceToken(int count, int sides) {
|
|
Count = count;
|
|
Sides = sides;
|
|
}
|
|
public DiceToken(Match match) {
|
|
Count = int.Parse(match.Groups[1].Value);
|
|
Sides = int.Parse(match.Groups[2].Value);
|
|
}
|
|
|
|
private const string Style = "\x1b[34;1m";
|
|
private static readonly Regex Pattern = new Regex(@"(\d+)d(\d+)");
|
|
|
|
public override string Format() {
|
|
var output = $"{Style}( ";
|
|
var result = Result;
|
|
for(int i = 0; i < Count; i++)
|
|
if(i < Count - 1)
|
|
output += $"{result[i]}, ";
|
|
else
|
|
output += $"{result[i]} ){Dungeoneer.Format.Reset}";
|
|
return output;
|
|
}
|
|
public override string Expression() { return Sum.ToString(); }
|
|
|
|
public static Match Match(string text) { return Pattern.Match(text); }
|
|
|
|
}
|
|
|
|
/*public class DcToken : Token {
|
|
|
|
private const string Style = "\x1b[31m";
|
|
private static readonly Regex Pattern = new Regex(@"dc\:(\d+)");
|
|
|
|
public static Match Match(string text) { return Pattern.Match(text); }
|
|
|
|
public override string Expression() { return ""; }
|
|
|
|
}*/
|
|
|
|
}
|
|
|