Esempio n. 1
0
 def p_exp_parenthesis_exp(self, p):
     """exp : LRB exp RRB"""
     print("exp : LRB exp RRB")
     print(p[2])
     p[0] = NonTerminal()
     if p[2] is None:
         p[0].place = self.find_place()
     else:
         p[0].place = p[2].get_value()
Esempio n. 2
0
 def generate_exp_array_code(self, p, temp, temp2, temp3):
     p[0] = NonTerminal()
     p[0].place = temp
     p[0].address = p[3].address
     self.variables += 'int ' + temp + ';\n'
     self.variables += 'int ' + temp2 + ';\n'
     self.variables += 'int ' + temp3 + ';\n'
     p[0].code = p[3].code + temp2 + ' = ' + p[3].get_value(
     ) + ' + 1;\n' + temp3 + ' = ' + p[
         1] + " + " + temp2 + ';\n' + temp + " = arr[" + temp3 + "];\n"
Esempio n. 3
0
 def boolean_expression(self, p, temp, place1='', place3=''):
     p[0] = NonTerminal()
     p[0].place = temp
     op1_place = place1
     op3_place = place3
     if place1 == '':
         op1_place = p[1].get_value()
     if place3 == '':
         op3_place = p[3].get_value()
     self.add_nonTerminal_operation(p[0].place, op1_place, op3_place, p[2])
Esempio n. 4
0
 def generate_arithmetic_code(self, p, temp, place1='', place3=''):
     p[0] = NonTerminal()
     p[0].place = temp
     p[0].code = p[0].place + " = "
     op1_place = place1
     op3_place = place3
     if place1 == '':
         op1_place = p[1].get_value()
     if place3 == '':
         op3_place = p[3].get_value()
     p[0].code += op1_place + " " + p[2] + " " + op3_place
     code = '\tint ' + temp + ';\n\t' + p[0].code + ';\n'
     self.file.write(code)
     self.lineCounter += 2
     self.add_nonTerminal_operation(p[0].place, op1_place, op3_place, p[2])
Esempio n. 5
0
    def generate_main_code(self, p, q1, q2):
        p[0] = NonTerminal()
        p[0].code = """#include <stdio.h>
#include <setjmp.h>

union jmp_buffer_union
{
    jmp_buf env_in_buf;
    struct {
        int env[64];	
    }env_in_int;
}env;

int arr[(int)1e6];
int arr_p = 0;
int stack_p = (int)1e6 - 1;
int val, index;

#define forward_jmp(position)								\\
    val = 0;												\\
    val=setjmp(env.env_in_buf); 							\\
    if(!val)			 									\\
        for(index = 0;index < 64;index++)								\\
            arr[position + index] = env.env_in_int.env[index]

#define back_jmp(position) 									\\
    for(index = 0;index < 64;index++)									\\
        env.env_in_int.env[index] = arr[position + index];		\\
    longjmp(env.env_in_buf, 1)

"""
        if p[5].address:
            q1 = p[5].address
        p[1].next_list_back_patch(q1)
        p[5].next_list_back_patch(q2)
        if p[5].address:
            p[0].code += self.variables + "\nmain()\n{\n" + p[1].code + p[
                5].code + q2 + ": return 0;\n}"
        else:
            p[0].code += self.variables + "\nmain()\n{\n" + p[
                1].code + q1 + ": " + p[5].code + q2 + ": return 0;\n}"
        print(f'Symbol Table of main')
        print('Name\tType\tAddress\tSize')
        func_stack = p[5].stack
        for i in range(len(func_stack)):
            print(f'{func_stack[i]}\t\tint\t\t{self.current_address}\t\t4')
            self.current_address += 4
        self.output_file.write(p[0].code)
Esempio n. 6
0
 def generate_exp_arithmetic_code(self, p, temp, temp2, temp3, q1, q2, q3,
                                  q4, q6, q7):
     p[0] = NonTerminal()
     p[0].place = temp
     self.variables += 'int ' + temp + ';\n'
     self.variables += 'int ' + temp2 + ';\n'
     self.variables += 'int ' + temp3 + ';\n'
     if p[3].address:
         q7 = p[3].address
     if p[1].code:
         p[0].address = p[1].address
     elif p[3].code:
         p[0].address = q7
     else:
         p[0].address = q6
     if isinstance(p[1], StatementTerminal):
         p[0].next_list = p[1].next_list
     if isinstance(p[1], LogicTerminal):
         p[1].true_list_back_patch(q1)
         p[1].false_list_back_patch(q2)
         if p[3].code:
             next_label = q7
         else:
             next_label = q6
         p[0].code = p[
             1].code + q1 + ": " + temp2 + " = 1;\ngoto " + next_label + ";\n" + q2 + ": " + temp2 + " = 0;\n"
         p[1].place = temp2
     else:
         p[0].code = p[1].code
     if p[3].code and not p[3].address:
         p[0].code += q7 + ": "
     if isinstance(p[3], StatementTerminal):
         p[0].next_list = p[3].next_list
     if isinstance(p[3], LogicTerminal):
         p[3].true_list_back_patch(q3)
         p[3].false_list_back_patch(q4)
         p[0].code += p[
             3].code + q3 + ": " + temp3 + " = 1;\ngoto " + q6 + ';\n' + q4 + ": " + temp3 + " = 0;\n"
         p[3].place = temp3
     else:
         p[0].code += p[3].code
     p[0].code += q6 + ": " + temp + " = " + p[1].get_value(
     ) + " " + p[2] + " " + p[3].get_value() + ";\n"
Esempio n. 7
0
 def not_assign(self, p, temp, exp_place=''):
     print("%%%%%%")
     # print(p[1])
     print(p[2])
     print("%%%%%%")
     p[0] = NonTerminal()
     p[0].place = temp
     op1_place = exp_place
     if exp_place == '':
         val = p[2].get_value()
         if val.startswith('B'):
             val = self.get_bool_result()
         op1_place = val
     p[0].code = p[0].place + ' = -' + str(op1_place)
     print(p[0].code)
     code = '\tint ' + temp + ';\n\t' + p[0].code + ';\n'
     self.file.write(code)
     self.lineCounter += 2
     print("#### ", op1_place)
     self.add_nonTerminal_assign_not(p[0].place, op1_place)
     self.delete_bool_vars()
Esempio n. 8
0
 def generate_exp_sub_code(self, p, temp, temp2, q1, q2, q3):
     p[0] = NonTerminal()
     p[0].place = temp
     if p[2].code:
         p[0].address = p[2].address
     else:
         p[0].address = q3
     self.variables += 'int ' + temp + ';\n'
     self.variables += 'int ' + temp2 + ';\n'
     if isinstance(p[2], StatementTerminal):
         p[0].next_list = p[2].next_list
     if isinstance(p[2], LogicTerminal):
         p[2].true_list_back_patch(q1)
         p[2].false_list_back_patch(q2)
         p[0].code = p[
             2].code + q1 + ': ' + temp2 + ' = 1;\ngoto ' + q3 + ';\n' + q2 + ': ' + temp2 + ' = 0;\n'
         p[2].place = temp2
     else:
         p[0].code = p[2].code
     p[0].code += q3 + ': ' + temp + ' = ' + p[1] + ' ' + p[2].get_value(
     ) + ';\n'
Esempio n. 9
0
 def generate_const_arithmetic_code(self, p):
     p[0] = NonTerminal()
     p[0].value = p[1]
Esempio n. 10
0
 def generate_exp_ID_code(self, p):
     p[0] = NonTerminal()
     p[0].place = p[1]
Esempio n. 11
0
 def generate_arithmetic_code(self, p, temp):
     p[0] = NonTerminal()
     p[0].place = temp
     p[0].code = p[0].place + " = "
     p[0].code += str(p[1].get_value()) + " " + p[2] + " " + str(
         p[3].get_value())
Esempio n. 12
0
 def p_exp_false(self, p):
     """exp : FALSE"""
     print("exp : FALSE")
     p[0] = NonTerminal()
     p[0].value = '0'
Esempio n. 13
0
 def p_exp_true(self, p):
     """exp : TRUE"""
     print("exp : TRUE")
     p[0] = NonTerminal()
     p[0].value = '1'
Esempio n. 14
0
 def p_exp_float(self, p):
     """exp : FLOATNUMBER"""
     print("exp : FLOATNUMBER")
     p[0] = NonTerminal()
     p[0].value = str(int(p[1]))
Esempio n. 15
0
 def p_exp_int(self, p):
     """exp : INTEGERNUMBER"""
     print("exp : INTEGERNUMBER")
     self.array_length = p[1]
     p[0] = NonTerminal()
     p[0].value = str(p[1])
Esempio n. 16
0
 def p_exp_integer(self, p):
     "exp : INTEGER"
     p[0] = NonTerminal()
     p[0].value = p[1]