コード例 #1
0
    def translate_tree(self, t: Tree) -> Tree:

        # print(t)

        if t.data == "mll":
            return Tree(t.data, self.transform(t.children))

        if t.data == "pyt":
            m = apply(t, lambda x: x, clean_tok)
            m.children = m.children + [Token("WS", "\n")]
            return m

        if t.data == "model":
            return self.translate_model(t)

        if t.data == "comp":
            return Tree(t.data, escape(t.children))

        if t.data == "comment":
            return None

        if t.data == "parmac":
            # cprint("entro in parmac","yellow")
            self.insert_parmac(t)

        if t.data == "summa":
            # cprint("entro in parmac","yellow")
            from mll.simple_model import SimpleModel
            from mll.dispatcher import Dispatcher
            rest = Dispatcher(self).translate_e(
                Tree("e", [
                    Token("ID",
                          clean_tok(t.children[2]).value),
                    Tree("e", [Token("ID",
                                     clean_tok(t.children[0]).value)])
                ]))
            print(rest)
            m = apply([
                Token("ID",
                      clean_tok(t.children[0]).value),
                Token("EQ", "=")
            ] + rest.children, lambda x: x, self.substitute_model)
            print(m)
            m = m + [Token("WS", "\n\n")]
            return m

        # if t.data == "macro_mod":
        #     create_macro_mod(self,t)
        #
        # if t.data == "macro_exp":
        #     create_macro_exp(self,t)
        #
        # if t.data == "macro_pip":
        #     create_macro_pip(self,t)

        if t.data == "macro":
            create_macro_pip(self, t)
コード例 #2
0
    def traduce_forks(self, t: Tree):

        # non esistono i modelli che sono funzioni quindi non si mette mai la riga sotto, salverò in un altro luogo
        # self.mll.models[clean_tok(t.children[0]).value] = t
        # esempio

        # cprint("ATTENTION FORK","red")
        self.mll.function_trees[clean_tok(t.children[0]).value] = t

        t.children[2:] = escape(t.children[2:])
        t.children[2:] = self.mll.put_macros(t.children[2:])
        t.children[2:] = apply(t.children[2:], lambda x:x, clean_tok )

        # print("FORKED tree after putmacros:",t)

        # t.children[2:] = apply(t.children[2:], lambda x: x, self.mll.solve_parmac)
        # t = apply(t, lambda x: x, clean_tok)
        apply(t, lambda x: x, self.mll.select_imported_libraries)

        t.children[2:] = apply(t.children[2:], lambda x: x, self.mll.substitute_model)
        # t = apply(t, lambda x: x, self.mll.solve_parmac)

        branches = t.children[2:]
        branches = list(group(branches, "PI"))

        a = []
        for branch in branches:
            a = a + self.traduce_branch(branch)

        branches = a

        b = self.mll.current_bindings[len(self.mll.current_bindings)-1]

        #  resetto tutto quando faccio il return
        self.mll.current_bindings = []
        self.mll.current_branch = 0

        return Tree(t.data,
                    [
                        Token("ID", "def "), Token("ID", clean_tok(t.children[0]).value), Token("ID", "(x)"), Token("COLON", ":")
                    ] +
                    self.mll.add_tab_top_level(filter(lambda x: x is not None, branches)) +
                    [
                        Token("ID", "return "), Token("ID", b), Token("WS", "\n\n")
                    ]
                    )
コード例 #3
0
    def put_macros(self, t):

        # print(t)

        if isinstance(t, Token):

            #########################################################
            #           caso parmac
            #########################################################

            if clean_tok(t).value in self.parmacs.keys():
                # print("---before",clean_tok(t).value,"; after",self.parmacs[clean_tok(t).value])
                return self.parmacs[clean_tok(t).value]

            #########################################################
            #           caso macro
            #########################################################

            if clean_tok(t).value in self.macros.keys():
                # print("---before", clean_tok(t).value, "; after", self.macros[clean_tok(t).value])
                m = escape(self.macros[clean_tok(t).value])
                return self.put_macros(m)

            #########################################################
            #           caso pmacro
            #########################################################

            if clean_tok(t).value[:3] in self.macros.keys():
                m = self.macros[clean_tok(t).value[:3]]
                m = copy.deepcopy(m)
                m = escape(m)
                export: str = clean_tok(t).value[3:]

                # se quindi c'è solo la grandezza del filtro
                if len(export) == 2:
                    m.children[0].children[1:1] = [
                        Tree("e", [Token("ID", export)])
                    ]

                return self.put_macros(m)

            # print("---last return", clean_tok(t).value)
            return clean_tok(t)

        if isinstance(t, Tree):
            return Tree(t.data, self.put_macros(t.children))

        if isinstance(t, list):
            t = escape(t)
            return [self.put_macros(x) for x in t]

        raise Exception("caso inaspettato", t, type(t))
コード例 #4
0
    def insert_parmac(self, t: Tree):
        id = t.children[0].value
        a = t.children[2:]
        a = flatten(group(a, "OR"))

        # print(a)

        for i in a:
            self.parmacs[clean_tok(i).value] = Tree("comp", [
                Token("ID", id),
                Token("EQ", "="),
                Token("ID", "'" + i.value + "'")
            ])
コード例 #5
0
    def traduce_simple_model(self, t: Tree):

        print("confirm simple")

        t.children[2:] = self.mll.put_macros(t.children[2:])
        t.children[2:] = apply(t.children[2:], lambda x: x, clean_tok)
        t.children[2:] = Dispatcher(self.mll,
                                    "simple").transform(t.children[2:])

        print(t.children[2:])

        apply(t.children[2:], lambda x: x, self.mll.select_imported_libraries)
        # g = apply(t.children[2:], lambda x: x, self.mll.contained_in_imported_libraries)
        # print("names that can be imported", g)
        # l = add_params(t.children[2:], g, self.mll)
        # print("modified list", l)
        #print(locals())
        if not self.mll.isInner:
            t.children[2:] = apply(t.children[2:], lambda x: x,
                                   contained_in_imported_libraries_mod,
                                   self.mll)
            print("mods to initial vector: " + str(t.children[2:]))

        self.mll.models[clean_tok(t.children[0]).value] = t
        t.children[2:] = apply(t.children[2:], lambda x: x,
                               self.mll.substitute_model)

        self.mll.ordered_models.append(clean_tok(t.children[0]).value)

        return Tree(t.data, [
            Token("ID", "models"),
            Token("LSP", "["),
            Token("SQ", "'"),
            clean_tok(t.children[0]),
            Token("SQ", "'"),
            Token("LSP", "]"),
            Token("EQ", "=")
        ] + t.children[2:] + [Token("WS", "\n\n")])
    def traduce_sequential(self, t: Tree):

        t.children[2:] = self.mll.put_macros(t.children[2:])

        apply(t, lambda x: x, self.mll.select_imported_libraries)

        t.children[2:] = apply(t.children[2:], lambda x: x,
                               self.mll.substitute_model)

        self.mll.models[clean_tok(t.children[0]).value] = t
        self.mll.select_imported_libraries(Token("ID", "Sequential"))

        self.mll.ordered_models.append(clean_tok(t.children[0]).value)

        branches = map(
            Dispatcher(self.mll, "sequential").transform, t.children[2:])

        # print(branches)
        # print(descend_left(Tree("e",branches)))
        # print(self.mll.env.keys())
        # print("Conv2D" in self.mll.env.keys())

        # cprint("ATTENTION SEQ", "red")

        #va in self loop, fixare
        if self.mll.isInner == False:
            #se il tipo può essere direttamente inferito:
            a = 1
            try:
                # se il modello è complesso allora darà errore che non esiste perchè non è ancora stato elaborato
                a = MLL(
                    "model:" +
                    clean_tok(descend_left(branches[0])).value.replace(
                        "models['", "").replace("']", "") + "\n\n",
                    self.mll.env).inner().execute().last_model()
            except:
                # a = MLL("model:"+clean_tok(descend_left(branches[0])).value.replace("models['","").replace("']","")+"\n\n", MLL()).inner().execute().last_model()
                # print(descend_left(t.children[2]))
                b = self.mll.function_trees[clean_tok(
                    descend_left(t.children[2])).value.replace(
                        "models['", "").replace("']", "")].children[2:][0]
                b = b.children[0]
                # print(b)
                a = MLL(
                    "model:" + clean_tok(b).value.replace(
                        "models['", "").replace("']", "") + "\n\n",
                    self.mll.env).inner().execute().last_model()

            # print(a)

            if ("classifier" in str(a)
                    or "regressor" in str(a)) and "sklearn" in str(a):
                # è un modello di stacking
                pass

                return Tree(
                    t.data, [
                        Token("ID", "models"),
                        Token("LSP", "["),
                        Token("SQ", "'"),
                        clean_tok(t.children[0]),
                        Token("SQ", "'"),
                        Token("LSP", "]"),
                        Token("EQ", "="),
                        Token("ID", "StackingClassifier"),
                        Token("LP", "("),
                        Token("ID", "classifiers"),
                        Token("EQ", "="),
                        Token("LSP", "["),
                        Token("LSP", "[")
                    ] + branches +
                    [Token("RSP", "]"),
                     Token("RP", ")"),
                     Token("WS", "\n\n")])

            if "classifier" not in str(a).lower() and "regressor" not in str(
                    a).lower() and "sklearn" in str(a).lower():
                # è un modello di pipeline
                self.mll.select_imported_libraries(Token("ID", "Pipeline"))
                return Tree(
                    t.data,
                    [
                        Token("ID", "models"),
                        Token("LSP", "["),
                        Token("SQ", "'"),
                        clean_tok(t.children[0]),
                        Token("SQ", "'"),
                        Token("LSP", "]"),
                        Token("EQ", "="),
                        Token("ID", "Pipeline"),
                        Token("LP", "("),
                        Token("LSP", "[")
                    ]
                    # devo trasformare da [a,b,c] -> [("a",a),("b",b),("c",c)]
                    + branches +
                    [Token("RSP", "]"),
                     Token("RP", ")"),
                     Token("WS", "\n\n")])

        # in alternativa è un modello sequenziale
        return Tree(t.data, [
            Token("ID", "models"),
            Token("LSP", "["),
            Token("SQ", "'"),
            clean_tok(t.children[0]),
            Token("SQ", "'"),
            Token("LSP", "]"),
            Token("EQ", "="),
            Token("ID", "Sequential"),
            Token("LP", "("),
            Token("LSP", "[")
        ] + branches +
                    [Token("RSP", "]"),
                     Token("RP", ")"),
                     Token("WS", "\n\n")])
コード例 #7
0
    def traduce_layers(self, t: Tree, opt=""):
        # (layer + ( layer + layer ))

        if isinstance(t,list):
            t=t[0]

        # cprint("before", "blue")
        # print(t.children)
        # print(list_types_list(t.children))
        # print(len(t.children))

        #####################################################################
        #           e + e
        #####################################################################

        if match(t.children, [1], ["PLUS"]) and len(t.children) == 3:
            if opt == "first":
                # print("fsx:", t.children[0])
                # print("fdx:", t.children[2])
                return Tree(t.data, [Tree(t.children[0].data, [self.traduce_layers(t.children[0], "first")]),
                                     Tree(t.children[2].data, [self.traduce_layers(t.children[2])])])
            else:
                # print("sx:", t.children[0])
                # print("dx:", t.children[2])
                return Tree(t.data, [Tree(t.children[0].data, [self.traduce_layers(t.children[0])]),
                                     Tree(t.children[2].data, [self.traduce_layers(t.children[2])])])

        #####################################################################
        #           ( [e]+ )
        #####################################################################

        # e ::= LP e RP
        if match(t.children, [0, len(t.children) - 1], ["LP", "RP"]):
            return Tree(t.data, [Token("ID", alphabet[self.mll.current_branch - 1]), Token("EQ", "="),
                                 Token("LP", "(")] +
                        [Dispatcher(self.mll,"forked").transform(t.children[1])] +[Token("RP", ")"), Token("LP", "("), Token("ID", alphabet[self.mll.current_branch - 1]), Token("RP", ")"),
                            Token("WS", "\n\t")])

        #####################################################################
        #           ID ->
        #####################################################################

        if match(t.children, [0,1], ["ID","AR"]) and len(t.children) == 2:
            # preparati per dare 2 bindings
            # print("sono della ID AR len2")
            self.mll.current_branch -=1
            self.mll.current_bindings = self.mll.current_bindings[:len(self.mll.current_bindings)-1]
            self.mll.current_binding_name = t.children[0]
            return None

        #####################################################################
        #           ID -> ID ID
        #####################################################################

        if match(t.children, [0,1,2,3], ["ID","AR","ID","ID"]) and len(t.children) == 4:
            # concatena 2 concatenazioni che sono state bindate
            # print("sono della ID AR ID ID len4")
            return Tree(t.data,
                        [
                            Token("ID", alphabet[self.mll.current_branch - 1]),
                            Token("EQ", "="), t.children[0],
                            Token("LP", "("),
                            Token("RP", ")"),
                            Token("LP", "("),
                            Token("LSP", "["),
                            Token("ID", clean_tok(t.children[2]).value),
                            Token("CO", ","),
                            Token("ID", clean_tok(t.children[3]).value),
                            Token("RSP", "]"),
                            Token("RP", ")"),
                            Token("WS", "\n\t")
                        ])

        #####################################################################
        #           e, e, e
        #####################################################################

        t.children[1:] = map(Dispatcher(self.mll,"forked").transform, t.children[1:], "CO", ",")

        #####################################################################
        #           ID e+ w/o "+" and "->"
        #####################################################################

        if match(t.children, [0], ["ID"]) and len(t.children) > 1 and not match(t.children, [], ["PLUS"]) and not match(t.children, [], ["AR"]):
            if clean_tok(t.children[0]).value == "assign":
                return Tree(t.data, [Token("ID", alphabet[self.mll.current_branch - 1]), Token("EQ", "="), t.children[0],
                                     Token("LP", "(")] + t.children[1:] + [
                                Token("RP", ")"),
                                Token("WS", "\n\t")])

            if opt == "first":
                return Tree(t.data, [Token("ID", alphabet[self.mll.current_branch - 1]), Token("EQ", "="), t.children[0],
                                     Token("LP", "(")] + t.children[1:] + [
                                Token("RP", ")"), Token("LP", "("), Token("ID", "x"), Token("RP", ")"),
                                Token("WS", "\n\t")])
            else:
                return Tree(t.data,
                            [
                                Token("ID", alphabet[self.mll.current_branch - 1]),
                                Token("EQ", "="), t.children[0],
                                Token("LP", "(")] +
                            t.children[1:] +
                            [
                                Token("RP", ")"),
                                Token("LP", "("),
                                Token("ID", alphabet[self.mll.current_branch - 1]),
                                Token("RP", ")"),
                                Token("WS", "\n\t")
                            ])

        ###############################################################################
        #           ID and no binding for forks and name is operations and not alias
        ###############################################################################

        if match(t.children, [0], ["ID"]) and len(t.children) == 1 and self.mll.current_binding_name is not None and clean_tok(t.children[0]).value not in self.mll.models:
            a = str(self.mll.current_bindings[:len(self.mll.current_bindings) - 1]).replace("'", "").replace("x,", "")
            # è sempre x il branch corente perchè vogliamo che il branch bindato sia stealth
            self.mll.current_bindings = ["x"]

            return Tree(t.data,
                        [
                            Token("ID", clean_tok(self.mll.current_binding_name)),
                            Token("EQ", "="), t.children[0],
                            Token("LP", "("),
                            Token("RP", ")"),
                            Token("LP", "("),
                            Token("ID", a),
                            Token("RP", ")"),
                            Token("WS", "\n\t")
                        ])

        #####################################################################
        #           ID and binding for forks exists and name is alias
        #####################################################################

        # print(match(t.children, [0], ["ID"]))
        # print(len(t.children) == 1)
        # print(self.mll.current_binding_name is not None)
        # print(clean_tok(t.children[0]).value in self.mll.models if isinstance(t.children[0],Token) else "Tree")

        if match(t.children, [0], ["ID"]) and len(t.children) == 1 and self.mll.current_binding_name is not None and clean_tok(t.children[0]).value.replace("models['","").replace("']","") in self.mll.models.keys():
            # a = str(self.mll.current_bindings[:len(self.mll.current_bindings) - 1]).replace("'", "").replace("x,", "")
            # è sempre x il branch corente perchè vogliamo che il branch bindato sia stealth

            return Tree(t.data,
                        [
                            Token("ID", clean_tok(alphabet[self.mll.current_branch - 1])),
                            Token("EQ", "="), t.children[0]
                        ])

        # print("entro nel secondo lock", t.children)
        # print(match(t.children, [0], ["ID"]))
        # print(len(t.children) == 1)
        # print(self.mll.current_binding_name is None)
        # print(clean_tok(t.children[0]).value in self.mll.models if isinstance(t.children[0], Token) else "Tree")

        #####################################################################
        #           ID and binding for forks non-exists and name is alias
        #####################################################################

        if match(t.children, [0], ["ID"]) and len(t.children) == 1 and self.mll.current_binding_name is None and clean_tok(t.children[0]).value.replace("models['","").replace("']","") in self.mll.models.keys():
            # a = str(self.mll.current_bindings[:len(self.mll.current_bindings) - 1]).replace("'", "").replace("x,", "")
            # è sempre x il branch corente perchè vogliamo che il branch bindato sia stealth

            return Tree(t.data,
                        [
                            Token("ID", alphabet[self.mll.current_branch - 1]),
                            Token("EQ", "="), t.children[0], Token("WS", "\n\t")
                        ])

        #####################################################################
        #           ID and no binding name for forks
        #####################################################################

        if match(t.children, [0], ["ID"]) and len(t.children) == 1 and self.mll.current_binding_name is None:
            a = str(self.mll.current_bindings[:len(self.mll.current_bindings) - 1]).replace("'", "").replace("x,", "")
            self.mll.current_bindings = ["x"]

            return Tree(t.data,
                        [
                            Token("ID", "x"),
                            Token("EQ", "="), t.children[0],
                            Token("LP", "("),
                            Token("RP", ")"),
                            Token("LP", "("),
                            Token("ID", a),
                            Token("RP", ")"),
                            Token("WS", "\n\t")
                        ])

        #####################################################################
        #           AT e become a=f()(a)
        #####################################################################

        if match(t.children, [0], ["AT"]):
            return Tree(t.data, [Token("ID", alphabet[self.mll.current_branch - 1]), Token("EQ", "="), t.children[1],
                                 Token("LP", "("),
                            Token("RP", ")"), Token("LP", "("), Token("ID", alphabet[self.mll.current_branch - 1]), Token("RP", ")"),
                            Token("WS", "\n\t")])

        # cprint("after", "red")
        # print(t.children)
        # print(len(t.children))
        # print(list_types_list(t.children))
        # print("-------------------------------------")

        return Tree(t.data, [self.traduce_layers(t.children, opt)])
コード例 #8
0
 def contained_in_imported_libraries(self, t: Token) -> bool:
     s = clean_tok(t).value
     if s in self.available_libraries.keys():
         return True
     return False
コード例 #9
0
 def select_imported_libraries(self, t: Token) -> None:
     s = clean_tok(t).value
     if s in self.available_libraries.keys():
         if self.available_libraries[s] not in self.used_libraries:
             self.used_libraries.append(self.available_libraries[s])
コード例 #10
0
 def substitute_model(self, t: Token):
     if clean_tok(t).value in self.models.keys():
         return Token("ID", "models['" + clean_tok(t).value + "']")
     else:
         return t
コード例 #11
0
 def solve_parmac(self, t):
     if clean_tok(t).value in self.parmacs.keys():
         return self.parmacs[clean_tok(t).value]
     else:
         return t
コード例 #12
0
 def solve_macro(self, t):
     return self.macros[clean_tok(t).value]