Beispiel #1
0
        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);

            ;
        }
Beispiel #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)
                {
                    ;
                }
            }
        }