예제 #1
0
        static string Example5()
        {
            NamedReg x1 = new NamedReg();
            x1.name = "x1";

            NamedReg x2 = new NamedReg();
            x2.name = "x2";

            IdList idl1 = new IdList();
            idl1.Add(x1);
            idl1.Add(x2);

            LocalDecl ld1 = new LocalDecl();
            ld1.type = new NumType();
            ld1.id_list = idl1;

            AtomExprList ael1 = new AtomExprList();
            ael1.Add(x1);
            ael1.Add(x2);

            ReturnStmt rs1 = new ReturnStmt();
            rs1.rv = ael1;

            StmtList sl1 = new StmtList();
            sl1.Add(ld1);
            sl1.Add(rs1);

            Sub abc = new Sub("abc", sl1);

            Pirate p = new Pirate();
            p.Add(abc);

            StringWriter sw = new StringWriter();
            PirateWriter pv = new PirateWriter(sw);

            DynamicVisitor.accept(p, pv);

            return sw.ToString();
        }
예제 #2
0
        static string Example6()
        {
            AtomExprList ael1 = new AtomExprList();
            Call c1 = new Call("foo", ael1);

            CallStmt cs1 = new CallStmt(c1);

            NumLiteral n1 = new NumLiteral(3.14);
            TmpNumReg tnr0 = new TmpNumReg(0);
            Assign a1 = new Assign(tnr0, n1);

            TmpIntReg tir0 = new TmpIntReg(0);

            IntLiteral i1 = new IntLiteral(42);
            StringLiteral s1 = new StringLiteral("hi");

            AtomExprList ael2 = new AtomExprList();
            ael2.Add(tir0);
            ael2.Add(i1);
            ael2.Add(s1);

            Call c2 = new Call("bar", ael2);
            CallStmt cs2 = new CallStmt(c2);

            NamedReg a = new NamedReg("a");
            LocalDecl ld1 = new LocalDecl(new IntType(), a);

            NamedReg b = new NamedReg("b");
            LocalDecl ld2 = new LocalDecl(new NumType(), b);

            NamedReg c = new NamedReg("c");
            LocalDecl ld3 = new LocalDecl(new StringType(), c);

            TmpNumReg tnr2 = new TmpNumReg(2);
            NumLiteral n2 = new NumLiteral(2.7);
            Assign a2 = new Assign(tnr2, n2);

            StringLiteral s2 = new StringLiteral("hello yourself");
            AtomExprList ael3 = new AtomExprList();
            ael3.Add(tnr2);
            ael3.Add(s2);
            Call c3 = new Call("baz", ael3);

            RegList rl4 = new RegList();
            rl4.Add(a);
            rl4.Add(b);
            rl4.Add(c);

            Assign a3 = new Assign(rl4, c3);

            StmtList sl1 = new StmtList();
            sl1.Add(cs1);
            sl1.Add(a1);
            sl1.Add(cs2);
            sl1.Add(ld1);
            sl1.Add(ld2);
            sl1.Add(ld3);
            sl1.Add(a2);
            sl1.Add(a3);

            Sub main = new Sub("main", sl1);

            StringLiteral s3 = new StringLiteral("Foo!\n");
            Call c4 = new Call("print", s3);
            CallStmt cs3 = new CallStmt(c4);

            StmtList sl2 = new StmtList();
            sl2.Add(cs3);

            Sub foo = new Sub("foo", sl2);

            NamedReg i = new NamedReg("i");
            ParamDecl pd1 = new ParamDecl(new NumType(), i);

            NamedReg answer = new NamedReg("answer");
            ParamDecl pd2 = new ParamDecl(new IntType(), answer);

            NamedReg message = new NamedReg("message");
            ParamDecl pd3 = new ParamDecl(new StringType(), message);

            StringLiteral s4 = new StringLiteral("Bar!\n");
            Call print1 = new Call("print", s4);
            CallStmt cs4 = new CallStmt(print1);

            Call print2 = new Call("print", i);
            CallStmt cs5 = new CallStmt(print2);

            StringLiteral s5 = new StringLiteral("\n");
            Call print3 = new Call("print", s5);
            CallStmt cs6 = new CallStmt(print3);

            Call print4 = new Call("print", answer);
            CallStmt cs7 = new CallStmt(print4);

            CallStmt cs8 = new CallStmt(print3);

            Call print5 = new Call("print", message);
            CallStmt cs9 = new CallStmt(print5);

            StmtList sl3 = new StmtList();
            sl3.Add(pd1);
            sl3.Add(pd2);
            sl3.Add(pd3);
            sl3.Add(cs4);
            sl3.Add(cs5);
            sl3.Add(cs6);
            sl3.Add(cs7);
            sl3.Add(cs8);
            sl3.Add(cs9);

            Sub bar = new Sub("bar", sl3);

            NamedReg e = new NamedReg("e");
            ParamDecl pd4 = new ParamDecl(new NumType(), e);

            NamedReg msg = new NamedReg("msg");
            ParamDecl pd5 = new ParamDecl(new StringType(), msg);

            StringLiteral s6 = new StringLiteral("Baz!\n");
            Call print7 = new Call("print", s6);
            CallStmt cs10 = new CallStmt(print7);

            Call print8 = new Call("print", e);
            CallStmt cs11 = new CallStmt(print8);

            Call print9 = new Call("print", s5);
            CallStmt cs12 = new CallStmt(print9);

            Call print10 = new Call("print", msg);
            CallStmt cs13 = new CallStmt(print10);

            AtomExprList ael4 = new AtomExprList();
            ael4.Add(new IntLiteral(1000));
            ael4.Add(new NumLiteral(1.23));
            ael4.Add(new StringLiteral("hi from baz"));
            ReturnStmt rs1 = new ReturnStmt(ael4);

            StmtList sl4 = new StmtList();
            sl4.Add(pd4);
            sl4.Add(pd5);
            sl4.Add(cs10);
            sl4.Add(cs11);
            sl4.Add(cs12);
            sl4.Add(cs13);
            sl4.Add(rs1);

            Sub baz = new Sub("baz", sl4);

            Pirate p = new Pirate();
            p.Add(main);
            p.Add(foo);
            p.Add(bar);
            p.Add(baz);

            StringWriter sw = new StringWriter();
            PirateWriter pv = new PirateWriter(sw);

            DynamicVisitor.accept(p, pv);

            return sw.ToString();
        }