示例#1
0
文件: Program.cs 项目: fossabot/csly
        private static void TestFactorial()
        {
            var whileParser = new WhileParser();
            var builder     = new ParserBuilder <WhileToken, WhileAST>();
            var Parser      = builder.BuildParser(whileParser, ParserType.EBNF_LL_RECURSIVE_DESCENT, "statement");

            ;

            var program = @"
(
    r:=1;
    i:=1;
    while i < 11 do 
    (";

            program += "\nprint \"r=\".r;\n";
            program += "r := r * i;\n";
            program += "print \"r=\".r;\n";
            program += "print \"i=\".i;\n";
            program += "i := i + 1 \n);\n";
            program += "return r)\n";
            var result      = Parser.Result.Parse(program);
            var interpreter = new Interpreter();
            var context     = interpreter.Interprete(result.Result);

            var compiler = new WhileCompiler();
            var code     = compiler.TranspileToCSharp(program);
            var f        = compiler.CompileToFunction(program);

            ;
        }
示例#2
0
        private static void TestFactorial()
        {
            var whileParser = new WhileParserGeneric();
            var builder     = new ParserBuilder <WhileTokenGeneric, WhileAST>();
            var Parser      = builder.BuildParser(whileParser, ParserType.EBNF_LL_RECURSIVE_DESCENT, "statement");

            var program = @"
(
    r:=1;
    i:=1;
    while i < 11 do 
    (";

            //program += "\nprint \"r=\".r;\n";
            program += "r := r * i;\n";
            // program += "print \"r=\".r;\n";
            // program += "print \"i=\".i;\n";
            program += "i := i + 1 \n);\n";
            program += "return r)\n";
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    var result      = Parser.Result.Parse(program);
                    var interpreter = new Interpreter();
                    var context     = interpreter.Interprete(result.Result);

                    var compiler = new WhileCompiler();
                    var code     = compiler.TranspileToCSharp(program);
                    var f        = compiler.CompileToFunction(program);
                    int r        = f();
                    if (r != 3628800)
                    {
                        throw new Exception("erreur " + r);
                    }
                }
                catch (Exception e)
                {
                    ;
                }
            }
        }
示例#3
0
        public void TestFactorialProgramExecAsIL()
        {
            var program  = @"
(
    r:=1;
    i:=1;
    while i < 11 do 
    ( 
    r := r * i;
    print """".r;
    print """".i;
    i := i + 1 );
return r
)";
            var compiler = new WhileCompiler();
            var func     = compiler.CompileToFunction(program);

            Assert.NotNull(func);
            var f = func();

            Assert.Equal(3628800, f);
        }