Ejemplo n.º 1
0
    def test_PY_rdpp(self):
        from Skoarcery.dragonsets import FIRST, FOLLOW
        from Skoarcery.terminals import Empty

        fd = open("SkoarPyon/rdpp.py", "w")
        PY = emissions.PY
        PY.fd = fd

        # Header
        # Imports
        # class SkoarParseException
        # class SkoarParser:
        #     __init__
        #     fail
        self.code_start()

        PY.tab += 1
        N = nonterminals.nonterminals.values()

        # precompute desirables
        PY.method("init_desirables")
        for A in N:

            R = A.production_rules

            PY.nl()
            PY.cmt(str(A))

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                desires = FIRST(alpha)

                if Empty in desires:
                    desires.discard(Empty)
                    desires.update(FOLLOW(A))

                i = 0

                n = len(desires)
                PY.dict_set("self.desirables", str(P), "[", end="")
                for toke in desires:
                    PY.raw(toke.toker_name)
                    i += 1
                    if i != n:
                        if i % 5 == 0:
                            PY.raw(",\n")
                            PY.stmt("           ", end="")
                        else:
                            PY.raw(", ")

                else:
                    PY.raw("]\n")

        PY.end()

        # write each nonterminal as a function
        for A in N:

            R = A.production_rules

            #PY.cmt(str(A))
            PY.stmt("def " + A.name + "(self, parent):")
            PY.tab += 1
            PY.stmt("self.tab += 1")

            if A.intermediate:
                PY.var("noad", "parent")
            else:
                PY.var("noad", PY.v_new("SkoarNoad", PY.v_sym(A.name),
                                        "parent"))

            PY.nl()

            #PY.code_line("print('" + A.name + "')")

            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                PY.stmt("desires = " +
                        PY.v_dict_get("self.desirables", str(P)))

                PY.if_("self.toker.sees(desires)")

                #PY.print(str(P))

                for x in alpha:
                    if isinstance(x, Terminal):
                        PY.stmt("noad.add_toke('" + x.toker_name +
                                "', self.toker.burn(" + x.toker_name + "))")

                        #PY.print("burning: " + x.name)
                    else:
                        if x.intermediate:
                            PY.stmt("self." + x.name + "(noad)")
                        else:
                            PY.stmt("noad.add_noad(self." + x.name + "(noad))")
                else:
                    PY.return_("noad")
                    PY.tab -= 1
                    PY.nl()

            if A.derives_empty:
                PY.cmt("<e>")
                #PY.print("burning empty")
                PY.return_("noad")

            else:
                PY.cmt("Error State")
                PY.stmt("self.fail()")

            PY.tab -= 1
            PY.nl()

        PY.tab -= 1

        fd.close()
Ejemplo n.º 2
0
    def test_pyrdpp(self):
        from Skoarcery.dragonsets import FIRST, FOLLOW
        from Skoarcery.terminals import Empty

        fd = open("../SkoarPyon/rdpp.py", "w")
        PY = emissions.PY
        PY.fd = fd

        # Header
        # Imports
        # class SkoarParseException
        # class SkoarParser:
        #     __init__
        #     fail
        self.code_start()

        PY.tab += 1
        N = nonterminals.nonterminals.values()

        # precompute desirables
        PY.method("init_desirables")
        for A in N:

            R = A.production_rules

            PY.nl()
            PY.cmt(str(A))

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                desires = FIRST(alpha)

                if Empty in desires:
                    desires.discard(Empty)
                    desires.update(FOLLOW(A))

                i = 0

                n = len(desires)
                PY.dict_set("self.desirables", str(P), "[", end="")
                for toke in desires:
                    PY.raw(toke.toker_name)
                    i += 1
                    if i != n:
                        if i % 5 == 0:
                            PY.raw(",\n")
                            PY.stmt("           ", end="")
                        else:
                            PY.raw(", ")

                else:
                    PY.raw("]\n")

        PY.end()

        # write each nonterminal as a function
        for A in N:

            R = A.production_rules

            #PY.cmt(str(A))
            PY.stmt("def " + A.name + "(self, parent):")
            PY.tab += 1
            PY.stmt("self.tab += 1")

            if A.intermediate:
                PY.var("noad", "parent")
            else:
                PY.var("noad", PY.v_new("SkoarNoad", PY.v_sym(A.name), "parent"))

            PY.nl()

            #PY.code_line("print('" + A.name + "')")

            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                PY.stmt("desires = " + PY.v_dict_get("self.desirables", str(P)))

                PY.if_("self.toker.sees(desires)")

                #PY.print(str(P))

                for x in alpha:
                    if isinstance(x, Terminal):
                        PY.stmt("noad.add_toke('" + x.toker_name + "', self.toker.burn(" + x.toker_name + "))")

                        #PY.print("burning: " + x.name)
                    else:
                        if x.intermediate:
                            PY.stmt("self." + x.name + "(noad)")
                        else:
                            PY.stmt("noad.add_noad(self." + x.name + "(noad))")
                else:
                    PY.return_("noad")
                    PY.tab -= 1
                    PY.nl()

            if A.derives_empty:
                PY.cmt("<e>")
                #PY.print("burning empty")
                PY.return_("noad")

            else:
                PY.cmt("Error State")
                PY.stmt("self.fail()")

            PY.tab -= 1
            PY.nl()

        PY.tab -= 1

        fd.close()
Ejemplo n.º 3
0
    def test_sc_rdpp(self):
        from Skoarcery.dragonsets import FIRST, FOLLOW
        from Skoarcery.terminals import Empty

        fd = open("../../SuperCollider/Skoar/rdpp.sc", "w")
        ____SC = SC = emissions.SC
        SC.fd = fd

        # Header
        # Imports
        # class SkoarParseException
        # class SkoarParser:
        #     init
        #     fail
        self.code_start()

        SC.tab += 1
        N = nonterminals.nonterminals.values()

        # precompute desirables
        SC.method("init_desirables")
        for A in N:

            R = A.production_rules

            SC.nl()
            SC.cmt(str(A))

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                desires = FIRST(alpha)

                if Empty in desires:
                    desires.discard(Empty)
                    desires.update(FOLLOW(A))

                i = 0

                n = len(desires)
                SC.dict_set("desirables", str(P), "[", end="")
                for toke in desires:
                    SC.raw(toke.toker_name)
                    i += 1
                    if i != n:
                        if i % 5 == 0:
                            SC.raw(",\n")
                            SC.stmt("           ", end="")
                        else:
                            SC.raw(", ")

                else:
                    SC.raw("]);\n")

        SC.end()

        # write each nonterminal as a function
        for A in N:

            R = A.production_rules

            #SC.cmt(str(A))
            SC.method(A.name, "parent")

            if A.intermediate:
                SC.var("noad", "parent")
            else:
                SC.var("noad", SC.v_new("SkoarNoad", SC.v_sym(A.name), "parent"))

            SC.var("desires", SC.null)
            SC.nl()

            SC.stmt("deep = deep + 1")
            SC.if_("deep > 100")
            ____SC.stmt("this.fail_too_deep")
            SC.end_if()

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                SC.stmt("desires = " + SC.v_dict_get("desirables", str(P)))

                SC.cmt(str(P))

                SC.if_("toker.sees(desires).notNil")

                # debugging
                #SC.print(str(P))

                for x in alpha:
                    if isinstance(x, Terminal):
                        SC.stmt('noad.add_toke(' + SC.v_sym(x.toker_name) + ', toker.burn(' + x.toker_name + '))')

                        # debugging
                        #SC.print("burning: " + x.name)
                    else:
                        if x.intermediate:
                            SC.stmt(SC.this + "." + x.name + "(noad)")
                        else:
                            SC.stmt("noad.add_noad(this." + x.name + "(noad))")
                else:
                    SC.stmt("deep = deep - 1")
                    SC.return_("noad")

                SC.end_if()

            if A.derives_empty:
                SC.cmt("<e>")

                # debugging
                #SC.print("burning empty")

                SC.stmt("deep = deep - 1")
                SC.return_("noad")

            else:
                SC.cmt("Error State")
                SC.stmt("this.fail")

            SC.end()

        SC.end()

        fd.close()
Ejemplo n.º 4
0
    def test_sc_rdpp(self):
        from Skoarcery.dragonsets import FIRST, FOLLOW
        from Skoarcery.terminals import Empty

        fd = open("../../SuperCollider/Skoar/rdpp.sc", "w")
        ____SC = SC = emissions.SC
        SC.fd = fd

        # Header
        # Imports
        # class SkoarParseException
        # class SkoarParser:
        #     init
        #     fail
        self.code_start()

        SC.tab += 1
        N = nonterminals.nonterminals.values()

        # precompute desirables
        SC.method("init_desirables")
        for A in N:

            R = A.production_rules

            SC.nl()
            SC.cmt(str(A))

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                desires = FIRST(alpha)

                if Empty in desires:
                    desires.discard(Empty)
                    desires.update(FOLLOW(A))

                i = 0

                n = len(desires)
                SC.dict_set("desirables", str(P), "[", end="")
                for toke in desires:
                    SC.raw(toke.toker_name)
                    i += 1
                    if i != n:
                        if i % 5 == 0:
                            SC.raw(",\n")
                            SC.stmt("           ", end="")
                        else:
                            SC.raw(", ")

                else:
                    SC.raw("]);\n")

        SC.end()

        # write each nonterminal as a function
        for A in N:

            R = A.production_rules

            #SC.cmt(str(A))
            SC.method(A.name, "parent")

            if A.intermediate:
                SC.var("noad", "parent")
            else:
                SC.var("noad", SC.v_new("SkoarNoad", SC.v_sym(A.name),
                                        "parent"))

            SC.var("desires", SC.null)
            SC.nl()

            SC.stmt("deep = deep + 1")
            SC.if_("deep > 100")
            ____SC.stmt("this.fail_too_deep")
            SC.end_if()

            # each production
            for P in R:

                if P.derives_empty:
                    continue

                # A -> alpha
                alpha = P.production

                SC.stmt("desires = " + SC.v_dict_get("desirables", str(P)))

                SC.cmt(str(P))

                SC.if_("toker.sees(desires).notNil")

                # debugging
                #SC.print(str(P))

                for x in alpha:
                    if isinstance(x, Terminal):
                        SC.stmt('noad.add_toke(' + SC.v_sym(x.toker_name) +
                                ', toker.burn(' + x.toker_name + '))')

                        # debugging
                        #SC.print("burning: " + x.name)
                    else:
                        if x.intermediate:
                            SC.stmt(SC.this + "." + x.name + "(noad)")
                        else:
                            SC.stmt("noad.add_noad(this." + x.name + "(noad))")
                else:
                    SC.stmt("deep = deep - 1")
                    SC.return_("noad")

                SC.end_if()

            if A.derives_empty:
                SC.cmt("<e>")

                # debugging
                #SC.print("burning empty")

                SC.stmt("deep = deep - 1")
                SC.return_("noad")

            else:
                SC.cmt("Error State")
                SC.stmt("this.fail")

            SC.end()

        SC.end()

        fd.close()