示例#1
0
        static Code code_builder_test2()
        {
            CodeBuilder cb = new CodeBuilder();

            FlatType string_type = cb.defineType("string");
            FlatType single_type = cb.defineType("single");

            Prototype print_prototype1 = cb.definePrototype(null, new Types(string_type), null);
            Prototype print_prototype2 = cb.definePrototype(null, new Types(single_type), null);
            Prototype sqrt_prototype = cb.definePrototype(null, new Types(single_type), new Types(single_type));

            Global print_global1 = cb.defineGlobal("print1", print_prototype1);
            Global print_global2 = cb.defineGlobal("print2", print_prototype2);
            Global sqrt_global = cb.defineGlobal("sqrt", sqrt_prototype);

            Operator neg = cb.defineOperator("neg", new Types(single_type), new Types(single_type));
            Operator add = cb.defineOperator("+", new Types(single_type), new Types(single_type, single_type));
            Operator sub = cb.defineOperator("-", new Types(single_type), new Types(single_type, single_type));
            Operator mul = cb.defineOperator("*", new Types(single_type), new Types(single_type, single_type));
            Operator div = cb.defineOperator("/", new Types(single_type), new Types(single_type, single_type));
            Operator div_assign = cb.defineOperator("/=", new Types(single_type), new Types(single_type));

            LambdaBuilder lb = cb.getLambdaBuilder("foo", null);
            Local a = lb.defineLocal("a", single_type);
            Local b = lb.defineLocal("b", single_type);
            Local c = lb.defineLocal("c", single_type);
            Local det = lb.defineLocal("det", single_type);
            Local n0 = lb.defineLocal("n0", single_type);
            Local n1 = lb.defineLocal("n1", single_type);
            Local n2 = lb.defineLocal("n2", single_type);
            Local n3 = lb.defineLocal("n3", single_type);
            Local n4 = lb.defineLocal("n4", single_type);

            FlatLiteral _2 = cb.defineLiteral(single_type, 2L);
            lb.addMove(new Lvalues(a), new Operands(_2));

            FlatLiteral _n3 = cb.defineLiteral(single_type, -3L);
            lb.addMove(new Lvalues(b), new Operands(_n3));

            FlatLiteral _n2 = cb.defineLiteral(single_type, -2L);
            lb.addMove(new Lvalues(c), new Operands(_n2));

            lb.addDo(neg, new Lvalues(n0), new Operands(b));
            lb.addDo(mul, new Lvalues(n1), new Operands(b, b));

            FlatLiteral _4 = cb.defineLiteral(single_type, 4L);
            lb.addDo(mul, new Lvalues(n2), new Operands(_4, a));

            lb.addDo(mul, new Lvalues(n2), new Operands(n2, c));
            lb.addDo(sub, new Lvalues(det), new Operands(n1, n2));

            lb.addCall(sqrt_global, new Lvalues(n4), new Operands(det));

            Local x1 = lb.defineLocal("x1", single_type);
            Local x2 = lb.defineLocal("x2", single_type);

            lb.addDo(add, new Lvalues(x1), new Operands(n0, n4));
            lb.addDo(div, new Lvalues(x1), new Operands(x1, n3));
            lb.addDo(sub, new Lvalues(x2), new Operands(n0, n4));
            lb.addDo(div_assign, new Lvalues(x2), new Operands(n3));

            Constant answer1 = cb.defineConstant(null, string_type, @"Answers to ABC formula are:"/*+System.Environment.NewLine*/);
            Constant answer2 = cb.defineConstant(null, string_type, @"x1 = ");
            Constant answer3 = cb.defineConstant(null, string_type, /*System.Environment.NewLine+*/@"x2 = ");
            Constant answer4 = cb.defineConstant(null, string_type, @""/*System.Environment.NewLine*/);

            lb.addCall(print_global1, null, new Operands(answer1));
            lb.addCall(print_global1, null, new Operands(answer2));
            lb.addCall(print_global2, null, new Operands(x1));
            lb.addCall(print_global1, null, new Operands(answer3));
            lb.addCall(print_global2, null, new Operands(x2));
            lb.addCall(print_global1, null, new Operands(answer4));

            cb.defineLambda(lb.getLambda(null));

            return cb.getCode();
        }
示例#2
0
        static Code code_builder_test1()
        {
            #if false
            ; hello world
            (code
                (type string)
                (prototype print_prototype (string) ())
                (global print print_prototype)
                (constant "Hello World!\\n" string)

                (lambda main
                    (call print_prototype print () ("Hello World!\\n"))))
            #endif
            CodeBuilder cb = new CodeBuilder();
            FlatType string_type = cb.defineType("string");
            Prototype print_prototype = cb.definePrototype(null, new Types(string_type), null);
            Global print_global = cb.defineGlobal("print", print_prototype);
            FlatLiteral hello_world = cb.defineLiteral(string_type, @"Hello World!"+System.Environment.NewLine);
            LambdaBuilder lb = cb.getLambdaBuilder("main", null);
            lb.addCall(print_global, null, new Operands(hello_world));
            cb.defineLambda(lb.getLambda(null));
            return cb.getCode();
        }