Exemplo n.º 1
0
def eval_plus(root: ASTNode, cnx=None) -> ASTNode:
    childs: List[ASTNode] = root.childs
    prev_code_l = ''
    prev_code_r = ''
    if childs[0].value:
        prev_code_l = childs[0].code
        izq = childs[0].value
    else:
        r = eval_expression(childs[0], cnx)
        if isinstance(r, ASTNode):
            prev_code_l = r.code
            izq = r.value
        else:
            izq = r

    if childs[1].value:
        prev_code_r = childs[0].code
        der = childs[1].value
    else:
        r = eval_expression(childs[1], cnx)
        if isinstance(r, ASTNode):
            prev_code_r = r.code
            der = r.value
        else:
            der = r

    root.code = "{}{}$t{} = {} {} {};\n".format(prev_code_l, prev_code_r,
                                                str(root.nid), izq, root.typee,
                                                der)
    root.value = "$t{}".format(str(root.nid))

    return root
Exemplo n.º 2
0
def evaluar_ast(ast: ASTNode):
    ast.code = ''
    value = ast.typee
    if isinstance(value, float) or isinstance(value, int):
        ast.value = ast.typee
        return ast
    elif isinstance(value, str):
        ast.value = "'{}'".format(ast.typee)
        return ast
    return 0
Exemplo n.º 3
0
    def __init__(self, node: ASTNode):
        node.code = ''
        node.value = None
        self.node = node
        self.name = node.childs[1].typee
        globales2.sym_table_fun.append(
            globales2.SymTable(self.name, node, node.line, node.col))
        for lista_instruc in node.childs[3]:
            if lista_instruc.typee == 'declaracion_registros':
                declaracion_registros = lista_instruc
                typee = declaracion_registros.childs[0].childs[0].typee  # int

                if 'tipo_declaracion2' == declaracion_registros.childs[
                        1].childs[0].typee:
                    tipo_declaracion2 = declaracion_registros.childs[1].childs[
                        0]

                    varname = tipo_declaracion2.childs[0].typee
                    root = eval_assign(tipo_declaracion2.childs[2], self.name)
                    uid = gen_id_temporal(root.value, self.name, varname)

                    prev = ''
                    if node.code:
                        prev = node.code

                    node.code = "{}{}\n".format(prev, root.code)

            if lista_instruc.typee == 'declaracion_structs':
                declaracion_registros = lista_instruc
                typee = declaracion_registros.childs[0].childs[0].typee  # int

                if 'tipo_declaracion2' == declaracion_registros.childs[
                        1].childs[0].typee:
                    tipo_declaracion2 = declaracion_registros.childs[1].childs[
                        0]

                    varname = tipo_declaracion2.childs[0].typee
                    root = eval_assign(tipo_declaracion2.childs[2], self.name)
                    uid = gen_id_temporal(root.value, self.name, varname)

                    prev = ''
                    if node.code:
                        prev = node.code

                    node.code = "{}{}\n".format(prev, root.code)

            if lista_instruc.typee == 'call_funcion':
                call_funcion = lista_instruc
                if 'printf' == call_funcion.childs[0].typee:
                    for exp in call_funcion.childs[1]:
                        root = eval_assign(exp, self.name)
                        prev = ''
                        if node.code:
                            prev = node.code

                        node.code = "{}{}{}\n".format(
                            prev, root.code, 'print($t{});'.format(exp.nid))

            if lista_instruc.typee == 'sentencia_etiqueta':
                sentencia_etiqueta = lista_instruc
                tag = sentencia_etiqueta.childs[0].typee
                prev = ''
                if node.code:
                    prev = node.code
                node.code = "{}{}:\n".format(prev, tag)
Exemplo n.º 4
0
def eval_expression(root: ASTNode, cnx=None):
    typee = root.typee
    exp_val = None

    if typee == '+' or typee == '-' \
            or typee == '*' \
            or typee == '/' \
            or typee == '%' \
            or typee == '&&' \
            or typee == '||' \
            or typee == 'xor' \
            or typee == '<' \
            or typee == '>' \
            or typee == '<=' \
            or typee == '>=' \
            or typee == '==' \
            or typee == '!=' \
            or typee == '&' \
            or typee == '|' \
            or typee == '^' \
            or typee == '<<' \
            or typee == '>>' \
            or typee == 'abs':
        exp_val = eval_plus(root, cnx)
    elif typee == 'expresion_number':
        data = root.childs[0].typee
        if data == 'access_arrayexp':
            exp_val = eval_expression(root.childs[0])
        # elif data == 'casteos':
        #     exp_val = cast_value(root.childs)
        elif data == 'array':
            exp_val = {}
        else:
            exp_val = evaluar_ast(root.childs[0])
    elif typee == 'expresion_id':
        data = root.childs[0]
        data.code = ''
        data.value = find_var(cnx, data.typee)
        exp_val = data
    elif typee == 'aum_dec':
        data = root.childs[0].childs[0]
        syym = root.childs[1].typee[:1]
        w_t = find_var(cnx, data.typee)
        data.code = '$t{nid} = {w_t};\n{w_t} = {w_t} {sym} 1;'.format(
            nid=root.nid, w_t=w_t, sym=syym)
        data.value = "$t{}".format(root.nid)
        exp_val = data

    if 'expresion_callfuncion' == typee:
        if root.childs[0].childs[0].typee == 'scanf':
            root.code = ''
            root.value = 'read()'
            exp_val = root
    # elif typee == 'access_arrayexp':
    #     key = root.childs[0].typee
    #     lista = root.childs[1]
    #     p_list = []
    #     for node in lista.childs:
    #         v = eval_expression(node)
    #         p_list.append(v)
    #     schema = get_id(key) or {}
    #     for i in range(0, len(p_list) - 1):
    #         elem = p_list[i]
    #         if schema.get(elem) is None:
    #             schema[elem] = {}
    #         schema = schema[elem]
    #     if schema is not None:
    #         exp_val = schema[p_list[len(p_list) - 1]]
    # elif typee == 'expresion_unaria':
    #     exp_val = eval_unaria(root.childs)
    # elif typee == 'exp_agrupacion':
    #     exp_val = eval_expression(root.childs[0])

    return exp_val