Esempio n. 1
0
    def __init__(self, array_sticker: tuple, card_number: int):
        card_id = "foreach_loop"
        card_type = [True, False, False, False, False, True, 0]
        super(ForeachLoop, self).__init__(card_id, card_type, card_number)

        (sticker_type, sticker_value) = array_sticker
        self.array_sticker = Sticker(sticker_type, sticker_value)
Esempio n. 2
0
    def __init__(self, variable_name: str, card_number: int):
        card_id = "variable_setter"
        card_type = [True, True, False, False, False, True, 0]
        super(VariableSetter, self).__init__(card_id, card_type, card_number)

        self.sticker_variable = Sticker("Variable", variable_name)
        self.code = ""
Esempio n. 3
0
    def __init__(self, print_data: tuple, card_number: int):
        card_id = "print"
        card_type = [True, True, False, False, False, False, 0]
        super(Display, self).__init__(card_id, card_type, card_number)

        (sticker_type, sticker_value) = print_data
        self.print_data = Sticker(sticker_type, sticker_value)
        self.code = ""
Esempio n. 4
0
    def __init__(self, array_name: str, card_number: int):
        '''
        array_name : Name of array Variable
        '''

        card_id = "array_setter"
        card_type = [True, True, False, False, False, True, 0]
        super(ArraySetter, self).__init__(card_id, card_type, card_number)

        self.sticker_array = Sticker("Array", array_name)
Esempio n. 5
0
 def __init__(self, expression, card_number: int = 0, loc: int = 0):
     self.expression = []
     for item in expression[loc]:
         (sticker_type, sticker_value) = item
         sticker = Sticker(sticker_type, sticker_value)
         self.expression.append(sticker)
     card_id = "expression"
     card_type = [False, False, True, False, False, False, 0]
     super(Expression, self).__init__(card_id, card_type, card_number)
     self.code = ""
Esempio n. 6
0
    def __init__(self, number_list:list, card_number:int):
        '''
        number_list : list of tuples (sticker_type, sticker_value)
            sticker_type : "number"
            sticker_value : "/some_number/"
        '''
        card_id = "array_numbers"
        card_type = [False, False, True, False, False, False, 0]
        super(ArrayNumbers, self).__init__(card_id, card_type, card_number)

        self.number_list = []
        for item in number_list:
            (sticker_type, sticker_value) = item
            sticker =Sticker(sticker_type,sticker_value)
            self.number_list.append(sticker)
Esempio n. 7
0
        if position is None:
            self.children.append(child)
            self.card_dict["children"].append(child.generate_card())
        else:
            self.children.insert(position, child)
            self.card_dict["children"].insert(position, child.generate_card())


if __name__ == "__main__":
    test_card = TestStatement(0)

    print("Card: \n", test_card.generate_card())
    print("Code: \n", test_card.generate_code())

    test_card_ex1 = Expression([[("variable", "count"), ("operator", "+"),
                                 ("number", "2")]], 1)
    test_card_ex2 = Expression([[("variable", "apple"), ("operator", "-"),
                                 ("number", "2")]], 2)
    test_sticker = Sticker("conditional_operator", "==")
    cond_list = [test_card_ex1, test_sticker, test_card_ex2]
    test_card_cond = Condition(cond_list, 3)

    test_card_ch1 = Display(("text", "Hello World!"), 0)
    test_card.add_child(test_card_ch1, None)
    test_card_ch2 = Display(("variable", "count"), 0)
    test_card.add_child(test_card_ch2, None)

    test_card.set_external_dependant(test_card_cond)

    print("Card: \n", test_card.generate_card())
    print("Code: \n", test_card.generate_code())
Esempio n. 8
0
    def update_state(self, natural_sentence: str):
        '''
        This function receives a natural sentence 
        and inserts it to the tree and store it to the json file
        '''

        # Do nothing if statement is empty
        if natural_sentence == "":
            return

        # dictionary for storing stickers
        d = {}

        # print(self.store.print_stack())

        # parse the natural sentence
        command_type, command, d = self.p.parse(natural_sentence)

        if command_type == "insert":

            # parse through the trie
            code = self.t.traverseTree(self.root, command, len(command), 0)

            print("+" * 10)
            print(code[0], code[1])
            print(command)

            ### Terminal and leaf of trie ###
            # Generate card and add to program
            if code[0] is not None and code[1] is None:

                if code[0] == "variable_setter":

                    c = VariableSetter(d["sticker_value"],
                                       self.store.new_card_number)
                    self.store.insert_card(c)
                    self.store.current_neighbour = c

                if code[0] == "array_setter":

                    c = ArraySetter(d["sticker_value"],
                                    self.store.new_card_number)
                    self.store.insert_card(c)
                    self.store.current_neighbour = c

                if code[0] == "print":
                    print(d)
                    c = Display((d["sticker_type"], d["sticker_value"]),
                                self.store.new_card_number)
                    self.store.insert_card(c)

                if code[0] == "test_statement":
                    c = TestStatement(self.store.new_card_number)
                    # print(c.card_number)
                    self.store.insert_card(c)
                    # change current parent
                    self.store.push_parent(c)
                    self.store.current_neighbour = c

                if code[0] == "while_loop":
                    c = WhileLoop(self.store.new_card_number)
                    self.store.insert_card(c)
                    # change current parent
                    self.store.push_parent(c)
                    self.store.current_neighbour = c

            ### Terminal but not leaf ###
            # Undecidable whether to create card or not
            elif code[0] is not None and code[1] is not None:
                print("Undecidable")
                pass

            ### Non-terminal and not leaf ###
            # Returns a suggestion
            elif code[1] is not None:
                pass
                # print("Suggestions: ",code[1])

        elif command_type == "create":
            self.store.variable_list.append(d["variable_name"])

        elif command_type == "navigate":

            if command == "POP_PARENT":
                self.store.pop_parent()
                self.updated = True

            elif command == "GOTO":
                self.store.current_position, _ = self.store.goto_card_by_number(
                    d["card_number"], self.store.root)
                print("GOTO Executed")
                self.updated = True

        elif command_type == "delete":
            print("DDDDD", d["card_number"])
            self.store.delete_card_by_number(d["card_number"])
            self.updated = True

        else:
            (exp_tuples, isComplete,
             expression_type) = self.e.parseExpression(command)
            if isComplete:

                # print(self.store.new_card_number)
                # parent_card = self.store.get_card_by_number(self.store.new_card_number-1, self.root)
                # _, parent_card = self.store.goto_card_by_number(self.store.new_card_number-1,self.store.root)
                # print(parent_card.card_id)
                print(exp_tuples)

                if len(exp_tuples) == 1:

                    if expression_type == "array":
                        c = ArrayNumbers(exp_tuples[0],
                                         self.store.new_card_number)
                    else:
                        c = Expression(exp_tuples, self.store.new_card_number)

                elif len(exp_tuples) == 3:

                    e1 = Expression(exp_tuples, self.store.new_card_number, 0)
                    self.store.insert_card_externally()

                    s = Sticker(exp_tuples[1][0], exp_tuples[1][1])

                    e2 = Expression(exp_tuples, self.store.new_card_number, 2)
                    self.store.insert_card_externally()

                    c = Condition([e1, s, e2], self.store.new_card_number)

                self.store.insert_external_dependant(c)
Esempio n. 9
0
                    "val_type":"internal_dependant",
                    "dependant" : item.generate_card()
                }
                self.card_dict["display"].append(expression_text)
        
        self.card_dict["external_dependant"] = {}
        self.card_dict["children"] = []

        return self.card_dict

    def generate_code(self, nesting_level = 0):
        self.code = ""
        for item in self.condition_list:
            if isinstance(item, Sticker):
                self.code += " " + item.sticker_value
            else:
                self.code += item.generate_code()

        return self.code

if __name__ == "__main__":
    test_card_ex1 = Expression([[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")],[("logical_operator","and")],[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")]],1,0)
    test_card_ex2 = Expression([[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")],[("logical_operator","and")],[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")]],2,2)
    test_sticker1 = Sticker("conditional_operator","==")
    test_sticker2 = Sticker("conditional_operator","and")
    cond_list = [test_card_ex1,test_sticker1,test_card_ex2, test_sticker2,test_card_ex1,test_sticker1,test_card_ex2]
    test_card = Condition(cond_list,3)
    print(test_card.generate_card())
    print(test_card.generate_code())

    # [[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")],[("logical_operator","and")],[("variable","count"),("operator", "+"),("number", "2")],[("conditional_operator","==")],[("variable","apple"),("operator", "-"),("number", "2")]]