public async Task <IEnumerable <IExecutionState> > Prepare(IEnumerable <Yolol.Grammar.AST.Program> programs, string done = ":done")
        {
            var network = new DefaultValueDeviceNetwork();

            return(from program in programs
                   select new InterpreterState(program, done, network));
        }
Beispiel #2
0
        public async Task RunYolol([Remainder] string input)
        {
            var code = input.ExtractYololCodeBlock();

            if (code == null)
            {
                await ReplyAsync(@"Failed to parse a yolol program from message - ensure you have enclosed your solution in triple backticks \`\`\`like this\`\`\`");

                return;
            }

            var result = Yolol.Grammar.Parser.ParseProgram(code);

            if (!result.IsOk)
            {
                await ReplyAsync(result.Err.ToString());

                return;
            }

            var network = new DefaultValueDeviceNetwork();
            var state   = new MachineState(network);
            var done    = state.GetVariable(":done");

            // Run lines until completion indicator is set or execution time limit is exceeded
            var limit = 0;
            var pc    = 0;

            while (!done.Value.ToBool() && limit++ < 2000)
            {
                try
                {
                    // If line if blank, just move to the next line
                    if (pc >= result.Ok.Lines.Count)
                    {
                        pc++;
                    }
                    else
                    {
                        pc = result.Ok.Lines[pc].Evaluate(pc, state);
                    }
                }
                catch (ExecutionException)
                {
                    pc++;
                }

                // loop around if program counter goes over max
                if (pc >= 20)
                {
                    pc = 0;
                }
            }

            // Print out the final machine state
            var embed = state.ToEmbed(network, limit, pc + 1).Build();

            await ReplyAsync(embed : embed);
        }