当前位置: 首页>>代码示例>>C#>>正文


C# Interpreter.Run方法代码示例

本文整理汇总了C#中Interpreter.Run方法的典型用法代码示例。如果您正苦于以下问题:C# Interpreter.Run方法的具体用法?C# Interpreter.Run怎么用?C# Interpreter.Run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Interpreter的用法示例。


在下文中一共展示了Interpreter.Run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        public static void Main(string[] args)
        {
            Console.WriteLine("BASIC {0}", Environment.OSVersion);
            Console.WriteLine("Exit with Ctrl+Z or Ctrl+D");
            Console.WriteLine();

            Interpreter interpreter = new Interpreter(Console.In);
            interpreter.Run();
        }
开发者ID:ngeor,项目名称:compilers,代码行数:9,代码来源:Program.cs

示例2: AndPushesFalseIfStackContainsTrueAndFalse

        public void AndPushesFalseIfStackContainsTrueAndFalse()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.AND);

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { }, null, 0);
            interpreter.Stack.Push(1);
            interpreter.Stack.Push(0);
            interpreter.Run();

            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(0, interpreter.Stack.Pop());
            Assert.AreEqual(1, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:14,代码来源:InterpreterTests.cs

示例3: RunProgramMethod

        protected override void RunProgramMethod(string program)
        {
            try
            {
                // Run the program.
                _bf = new Interpreter(program, null, (b) =>
                {
                    _console.Append((char)b);
                });

                _bf.Run(_maxIterationCount);
            }
            catch
            {
            }
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:16,代码来源:StringOptimizedFitness.cs

示例4: AddWorks

        public void AddWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_INT);
            bytecode.AddRange(BitConverter.GetBytes(23L));
            bytecode.Add(Bytecode.PUSH_INT);
            bytecode.AddRange(BitConverter.GetBytes(170L));
            bytecode.Add(Bytecode.ADD);

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 0);
            interpreter.Run();

            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(193, interpreter.Stack.Pop());
            Assert.AreEqual(19, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:16,代码来源:InterpreterTests.cs

示例5: GetFitnessMethod

        protected override double GetFitnessMethod(string program)
        {
            // Run the source code.
            try
            {
                // Run the program.
                _bf = new Interpreter(program, null, (b) =>
                {
                    _console.Append((char)b);
                });

                _bf.Run(_maxIterationCount);
            }
            catch
            {
                Fitness--;
            }

            Output = _console.ToString();

            // Order bonus.
            for (int i = 0; i < _targetString.Length; i++)
            {
                if (_console.Length > i)
                {
                    Fitness += 256 - Math.Abs(_console[i] - _targetString[i]);
                }
            }

            // Length bonus (percentage of 100).
            Fitness += 10 * ((_targetString.Length - Math.Abs(_console.Length - _targetString.Length)) / _targetString.Length);

            _fitness += Fitness;

            // Check for solution.
            if (!IsFitnessAchieved())
            {
                // Bonus for less operations to optimize the code.
                _fitness += ((_maxIterationCount - _bf.m_Ticks) / 20.0);
            }

            Ticks = _bf.m_Ticks;

            return _fitness;
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:45,代码来源:StringStrictFitness.cs

示例6: RunProgramMethod

        protected override void RunProgramMethod(string program)
        {
            for (int i = 0; i < 99; i++)
            {
                // Get input from the user.
                Console.WriteLine();
                Console.Write(">: ");
                string line = Console.ReadLine();
                int index = 0;

                _console.Clear();

                try
                {
                    // Run the program.
                    Interpreter bf = new Interpreter(program, () =>
                    {
                        byte b;

                        // Send the next character.
                        if (index < line.Length)
                        {
                            b = (byte)line[index++];
                        }
                        else
                        {
                            b = 0;
                        }

                        return b;
                    },
                    (b) =>
                    {
                        _console.Append((char)b);
                    });

                    bf.Run(_maxIterationCount);
                }
                catch
                {
                }

                Console.WriteLine(_console.ToString());
            }
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:45,代码来源:CsvSplitFitness.cs

示例7: RunProgramMethod

        protected override void RunProgramMethod(string program)
        {
            for (int i = 0; i < 99; i++)
            {
                // Get input from the user.
                _console.Clear();

                try
                {
                    // Run the program.
                    Interpreter bf = new Interpreter(program, () =>
                    {
                        Console.WriteLine();
                        Console.Write(">: ");

                        return Byte.Parse(Console.ReadLine());
                    },
                    (b) =>
                    {
                        Console.Write((char)b);
                    });

                    bf.Run(_maxIterationCount);
                }
                catch
                {
                }

                Console.WriteLine();
                Console.WriteLine("Game Over");
            }
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:32,代码来源:NumberGuessFitness.cs

示例8: RunProgramMethod

        protected override void RunProgramMethod(string program)
        {
            for (int i = 0; i < 99; i++)
            {
                // Get input from the user.
                Console.WriteLine();
                Console.Write(">: ");
                byte startingValue = Byte.Parse(Console.ReadLine());
                int index = 0;
                int index2 = 0;

                _console.Clear();

                try
                {
                    // Run the program.
                    Interpreter bf = new Interpreter(program, () =>
                    {
                        byte b;

                        // Send the next character.
                        if (index++ == 0)
                        {
                            b = startingValue;
                        }
                        else
                        {
                            b = 255;
                        }

                        return b;
                    },
                    (b) =>
                    {
                        // The program correctly solves the problem, however the output is in byte format. For example: 5b => 598 (where 98 = 'b'). We need to format the output for humans to read by leaving the numeric values as byte and the text values as char.
                        if (index2++ % (_targetString.Length + 1) == 0)
                        {
                            // Append numeric byte value.
                            _console.Append(b);
                        }
                        else
                        {
                            // Append text.
                            _console.Append((char)b);
                        }
                    });

                    bf.Run(_maxIterationCount);
                }
                catch
                {
                }

                Console.WriteLine(_console.ToString());
            }
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:56,代码来源:BottlesOfBeerFitness.cs

示例9: RunProgramMethod

        protected override void RunProgramMethod(string program)
        {
            for (int i = 0; i < 99; i++)
            {
                try
                {
                    int state = 0;

                    // Run the program.
                    Interpreter bf = new Interpreter(program, () =>
                    {
                        if (state == 0)
                        {
                            state++;
                            Console.WriteLine();
                            Console.Write(">: ");
                            byte b = Byte.Parse(Console.ReadLine());
                            return b;
                        }
                        else if (state == 1)
                        {
                            state++;
                            Console.WriteLine();
                            Console.Write(">: ");
                            byte b = Byte.Parse(Console.ReadLine());
                            return b;
                        }
                        else
                        {
                            return 0;
                        }
                    },
                    (b) =>
                    {
                        Console.Write(b);
                    });

                    bf.Run(_maxIterationCount);
                }
                catch
                {
                }
            }
        }
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:44,代码来源:TimesThreeFitness.cs

示例10: GetFitnessMethod

        protected override double GetFitnessMethod(string program)
        {
            string name = "";
            string targetStringName = "";
            int state = 0;
            double countBonus = 0;
            double penalty = 0;
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < _trainingCount; i++)
            {
                switch (i)
                {
                    case 0: name = "s"; break;
                    case 1: name = "me"; break;
                    case 2: name = "jay"; break;
                    case 3: name = "kory"; break;
                    case 4: name = "jamie"; break;
                };

                sb.Clear();
                sb.Append(_targetString);
                sb.Append(name);
                targetStringName = sb.ToString();

                try
                {
                    state = 0;
                    _console.Clear();

                    // Run the program.
                    _bf = new Interpreter(program, () =>
                    {
                        if (state > 0 && state < name.Length + 2)
                        {
                            if (state < name.Length + 1)
                            {
                                // We've output 2 bytes, we're ready to send input.
                                return (byte)name[state++ - 1];
                            }
                            else
                            {
                                // Send terminator character.
                                return 0;
                            }
                        }
                        else
                        {
                            // Not ready for input.
                            penalty++;

                            return 255;
                        }
                    },
                    (b) =>
                    {
                        _console.Append((char)b);

                        // Output the first half of the phrase before looking for input.
                        if (state == 0 && _console.Length >= _targetString.Length)
                        {
                            // We've output two bytes, let input come through.
                            state = 1;
                        }
                    });
                    _bf.Run(_maxIterationCount);
                }
                catch
                {
                }

                _output.Append(_console.ToString());
                _output.Append(",");

                // Order bonus.
                for (int j = 0; j < targetStringName.Length; j++)
                {
                    if (_console.Length > j)
                    {
                        Fitness += 256 - Math.Abs(_console[j] - targetStringName[j]);
                    }
                }

                // Make the AI wait until a solution is found without the penalty (too many input characters).
                Fitness -= penalty;

                // Check for solution.
                IsFitnessAchieved();

                // Bonus for less operations to optimize the code.
                countBonus += ((_maxIterationCount - _bf.m_Ticks) / 20.0);

                Ticks += _bf.m_Ticks;
            }

            if (_fitness != Double.MaxValue)
            {
                _fitness = Fitness + countBonus;
            }

//.........这里部分代码省略.........
开发者ID:ipepe,项目名称:AI-Programmer,代码行数:101,代码来源:HelloUserFitness.cs

示例11: PushStringWorks

        public void PushStringWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_STRING);
            bytecode.AddRange(BitConverter.GetBytes(4568));

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 0);
            interpreter.Run();
            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(4568, interpreter.Stack.Pop());
            Assert.AreEqual(5, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:12,代码来源:InterpreterTests.cs

示例12: PushStringVarWorks

        public void PushStringVarWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_STRING_VAR);
            bytecode.AddRange(BitConverter.GetBytes(3));

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 4);
            interpreter.Variables[3] = 25;

            interpreter.Run();
            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(25, interpreter.Stack.Pop());
            Assert.AreEqual(5, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:14,代码来源:InterpreterTests.cs

示例13: PushFalseWorks

        public void PushFalseWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_FALSE);

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 0);
            interpreter.Run();
            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(0, interpreter.Stack.Pop());
            Assert.AreEqual(1, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:11,代码来源:InterpreterTests.cs

示例14: PushBooleanVarWorks

        public void PushBooleanVarWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_BOOLEAN_VAR);
            bytecode.AddRange(BitConverter.GetBytes(8));

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 9);
            interpreter.Variables[8] = 1;

            interpreter.Run();
            Assert.AreEqual(1, interpreter.Stack.Count);
            Assert.AreEqual(1, interpreter.Stack.Pop());
            Assert.AreEqual(5, interpreter.PC);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:14,代码来源:InterpreterTests.cs

示例15: PrintStringWorks

        public void PrintStringWorks()
        {
            var bytecode = new List<byte>();
            bytecode.Add(Bytecode.PUSH_STRING);
            bytecode.AddRange(BitConverter.GetBytes(1));
            bytecode.Add(Bytecode.PRINT_STRING);

            var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "hello", "world" }, null, 0);
            var output = new List<string>();
            interpreter.SetPrinter((string str) => output.Add(str));
            interpreter.Run();

            Assert.AreEqual(0, interpreter.Stack.Count);
            Assert.AreEqual(6, interpreter.PC);
            Assert.AreEqual(1, output.Count);
            Assert.AreEqual("world", output[0]);
        }
开发者ID:Valtis,项目名称:CompilerCourseProject,代码行数:17,代码来源:InterpreterTests.cs


注:本文中的Interpreter.Run方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。