Exemplo n.º 1
0
 def p_assignment(self, p):
     'assignment : lvalue TOKEN_ASSIGNMENT exp TOKEN_SEMICOLON'
     p[0] = NoneTerminal(p)
     if p[3].place != '':
         p[0].code = p[1].value + " = " + p[3].place + ";"
         p[0].address = len(self.codes)
         self.codes.append(p[0].code)
         p[0].code = p[3].code + p[0].code
     elif p[3].value != '':
         p[0].code = p[1].value + " = " + p[3].value + ";"
     print(self.codes)
     print('assignment : lvalue TOKEN_ASSIGNMENT exp TOKEN_SEMICOLON')
Exemplo n.º 2
0
def three_operation_code(p, cl):
    p[0] = NoneTerminal(p)
    reg = Register('real')
    p[0].place = reg.place
    temp1 = ''
    if p[2].place != '':
        temp1 = p[2].place
    else:
        temp1 = p[2].value
    # p[0].code = p[1].code + p[3].code +  p[0].place + " = " + p[1].value + " " + p[2] + " " + p[3].value() + ";\n"
    p[0].code = p[0].place + " = " + p[1] + temp1 +  ";"
    # if p[2].value!='':
    #     if p[1] == '~':
    #         p[0].value = str(~ int(p[2].value))
    #     elif p[1] == '-':
    #         p[0].value = str(- int(p[2].value))
    #     elif p[1] == '!':
    #         p2 = int(p[2].value)
    #         if p2 != 0:
    #             p[0].value = str(0)
    #         else:
    #             p[0].value = str(random.randint(1,30))
    #     print('++++')
    #     # print(p[0].value)
    cl.append(p[0].code)
Exemplo n.º 3
0
    def p_item1(self, p):  #handled
        'item1 : TOKEN_ID TOKEN_ASSIGNMENT exp'
        p[0] = NoneTerminal(p)
        # p[0].label = p[1]
        # p[0].value = p[3].value
        # p[0].place = p[3].place
        # p[0].type = p[3].type
        p[0].code = p[1] + " = " + p[3].get_value()
        p[0].vars = [p[1]]
        # p[0].code = p[3].label + ' = ' + p[0].get_value() + ';'
        self.codes.append(p[0].code)
        print('p0 code')
        print(p[0].code)
        self.symbol_table.append([p[0].label, p[0].value])

        print('item1 -> TOKEN_ID TOKEN_ASSIGNMENT exp')
Exemplo n.º 4
0
    def p_logical_operation_and(self, p):
        'logical_operation : exp TOKEN_AND exp'
        p[0] = NoneTerminal(p)
        true_label = Label()
        print(p[1].code)
        print('=====+')
        self.back_patch_true(p[1], true_label)
        p[0].code = p[
            1].code + true_label.label + ": // logical calculation (AND)\n " + p[
                3].code

        # m = p[1].m
        # self.backpatch(p[1].true_list, m)
        # p[0].false_list = p[1].false_list + p[3].false_list
        # p[0].true_list = p[3].true_list

        print('logical_operation : exp TOKEN_AND exp')
Exemplo n.º 5
0
def four_boolean_code(p,cl):
    p[0] = NoneTerminal(p)
    reg = Register('real')
    p[0].place = reg.place
    temp1 = ''
    temp2 = ''
    print('this p1')
    print(p[1])
    if p[1].place != '':
        temp1 = p[1].place
    else:
        temp1 = p[1].value
    if p[3].place != '':
        temp2 = p[3].place
    else:
        temp2 = p[3].value
    # p[0].code = p[1].code + p[3].code +  p[0].place + " = " + p[1].value + " " + p[2] + " " + p[3].value() + ";\n"
    p[0].code = 'L' + str(len(cl)) + ': ' + p[0].place + " = " + temp1 + " " + p[2] + " " + temp2 + ";"
Exemplo n.º 6
0
def four_operation_code(p, cl):
    p[0] = NoneTerminal(p)
    reg = Register('real')
    p[0].place = reg.place
    temp1 = ''
    temp2 = ''
    print('this p1')
    print(p[1].get_value())
    temp1 = p[1].get_value()
    temp2 = p[3].get_value()
    # p[0].code = p[1].code + p[3].code +  p[0].place + " = " + p[1].value + " " + p[2] + " " + p[3].value() + ";\n"
    p[0].code =  p[0].place + " = " + temp1 + " " + p[2] + " " + temp2 + ";"
    # if p[3].value!='' and p[1].value!='':
    #     if p[2] == '*':
    #         print('sss')
    #         print(int(p[1].value) * int(p[3].value))
    #         p[0].value = str(int(p[1].value) * int(p[3].value))
    #     elif p[2] == '+':
    #         p[0].value = str(int(p[1].value) + int(p[3].value))
    #     elif p[2] == '-':
    #         p[0].value = str(int(p[1].value) - int(p[3].value))
    #     elif p[2] == '/':
    #         p[0].value = str(int(p[1].value) / int(p[3].value))
    #     elif p[2] == '%':
    #         p[0].value = str(int(p[1].value) % int(p[3].value))
    #     elif p[2] == '^':
    #         p[0].value = str(int(p[1].value) ** int(p[3].value))
    #     elif p[2] == '|':
    #         p[0].value = str(int(p[1].value) | int(p[3].value))
    #     elif p[2] == '&':
    #         p[0].value = str(int(p[1].value) & int(p[3].value))
    #     elif p[2] == '<<':
    #         p[0].value = str(int(p[1].value) << int(p[3].value))
    #     elif p[2] == '>>':
    #         p[0].value = str(int(p[1].value) >> int(p[3].value))

    cl.append(p[0].code)
Exemplo n.º 7
0
 def p_exp_logical_op(self, p):
     'exp : logical_operation'
     p[0] = NoneTerminal(p)
     p[0].code = p[1].code
     p[0].place = p[1].get_value()
     print('exp : logical_operation')