Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            LineEditor lineEditor = new LineEditor("DiceShell");

            string input;

            while ((input = lineEditor.Edit("DiceShell $ ", "")) != null)
            {
                if (string.IsNullOrEmpty(input))
                {
                    continue;
                }

                try
                {
                    AntlrInputStream        inputStream       = new AntlrInputStream(input);
                    DiceLexer               diceLexer         = new DiceLexer(inputStream);
                    CommonTokenStream       commonTokenStream = new CommonTokenStream(diceLexer);
                    DiceParser              diceParser        = new DiceParser(commonTokenStream);
                    DiceParser.ShellContext context           = diceParser.shell();
                    DiceVisitor             visitor           = new DiceVisitor();

                    int result = (int)visitor.Visit(context);

                    Console.WriteLine(string.Format("[{0:HH:mm:ss}] {1}\n", DateTime.Now, result));
                }
                catch (Exception)
                {
                    Console.WriteLine("Parsing Error\n");
                }
            }
        }
Exemplo n.º 2
0
        private static DiceParser Setup(string text)
        {
            AntlrInputStream  inputStream       = new AntlrInputStream(text);
            DiceLexer         diceLexer         = new DiceLexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(diceLexer);
            DiceParser        diceParser        = new DiceParser(commonTokenStream);

            return(diceParser);
        }
Exemplo n.º 3
0
 public static IEnumerable <(double, string)> EvaluateExpression(string exp)
 {
     try
     {
         Console.WriteLine($"Evaluating: {exp}");
         var inputStream = new AntlrInputStream(exp);
         var lexer       = new DiceLexer(inputStream);
         var tokenStream = new CommonTokenStream(lexer);
         var parser      = new DiceParser(tokenStream);
         var context     = parser.request();
         var evaluator   = new RequestEvaluator();
         return(evaluator.Visit(context));
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
         return(new[] { (0.0, "Failed to parse and evaluate") });
Exemplo n.º 4
0
 static Dice()
 {
     Lexer  = new DiceLexer(new AntlrInputStream(string.Empty));
     Parser = new DiceParser(new CommonTokenStream(Lexer));
 }