83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
from .processor_utils import get_scl_representation, format_variable_name,get_target_scl_name
|
|
|
|
|
|
# TODO: Import necessary functions from processor_utils
|
|
# Example: from .processor_utils import get_scl_representation, format_variable_name
|
|
# Or: import processors.processor_utils as utils
|
|
|
|
# TODO: Define constants if needed (e.g., SCL_SUFFIX) or import them
|
|
SCL_SUFFIX = "_scl"
|
|
|
|
# --- Function code starts ---
|
|
def process_math(instruction, network_id, scl_map, access_map, data):
|
|
"""
|
|
Genera SCL para operaciones matemáticas (SUB, MUL, DIV).
|
|
"""
|
|
instr_uid = instruction["instruction_uid"]
|
|
instr_type = instruction["type"] # SUB, MUL, DIV
|
|
if instr_type.endswith(SCL_SUFFIX) or "_error" in instr_type:
|
|
return False
|
|
|
|
# Mapa de tipos a operadores SCL
|
|
op_map = {"SUB": "-", "MUL": "*", "DIV": "/"}
|
|
scl_operator = op_map.get(instr_type)
|
|
if not scl_operator:
|
|
instruction["scl"] = f"// ERROR: Operación matemática no soportada: {instr_type}"
|
|
instruction["type"] += "_error"
|
|
return True
|
|
|
|
# Obtener EN, IN1, IN2
|
|
en_input = instruction["inputs"].get("en")
|
|
in1_info = instruction["inputs"].get("in1")
|
|
in2_info = instruction["inputs"].get("in2")
|
|
en_scl = get_scl_representation(en_input, network_id, scl_map, access_map) if en_input else "TRUE"
|
|
in1_scl = get_scl_representation(in1_info, network_id, scl_map, access_map)
|
|
in2_scl = get_scl_representation(in2_info, network_id, scl_map, access_map)
|
|
|
|
if en_scl is None or in1_scl is None or in2_scl is None:
|
|
return False # Dependencias no listas
|
|
|
|
# Obtener destino 'out'
|
|
target_scl = get_target_scl_name(instruction, "out", network_id, default_to_temp=True)
|
|
if target_scl is None:
|
|
instruction["scl"] = f"// ERROR: {instr_type} {instr_uid} sin destino 'out'."
|
|
instruction["type"] += "_error"
|
|
return True
|
|
|
|
# Formatear operandos si son variables
|
|
op1 = format_variable_name(in1_scl) if in1_info and in1_info.get("type") == "variable" else in1_scl
|
|
op2 = format_variable_name(in2_scl) if in2_info and in2_info.get("type") == "variable" else in2_scl
|
|
|
|
# Añadir paréntesis si es necesario (especialmente para expresiones)
|
|
op1 = f"({op1})" if (" " in op1 or "+" in op1 or "-" in op1 or "*" in op1 or "/" in op1) and not op1.startswith("(") else op1
|
|
op2 = f"({op2})" if (" " in op2 or "+" in op2 or "-" in op2 or "*" in op2 or "/" in op2) and not op2.startswith("(") else op2
|
|
|
|
# Generar SCL
|
|
scl_core = f"{target_scl} := {op1} {scl_operator} {op2};"
|
|
scl_final = f"IF {en_scl} THEN\n {scl_core}\nEND_IF;" if en_scl != "TRUE" else scl_core
|
|
|
|
instruction["scl"] = scl_final
|
|
instruction["type"] = instr_type + SCL_SUFFIX
|
|
|
|
# Actualizar mapa SCL
|
|
map_key_out = (network_id, instr_uid, "out")
|
|
scl_map[map_key_out] = target_scl
|
|
map_key_eno = (network_id, instr_uid, "eno")
|
|
scl_map[map_key_eno] = en_scl
|
|
return True
|
|
|
|
# --- Procesador NOT ---
|
|
|
|
# --- Function code ends ---
|
|
|
|
# --- Processor Information Function ---
|
|
def get_processor_info():
|
|
"""Devuelve la información para operaciones matemáticas (excepto ADD, MOD)."""
|
|
# Esta función maneja SUB, MUL, DIV.
|
|
return [
|
|
{'type_name': 'sub', 'processor_func': process_math, 'priority': 4}, # Resta
|
|
{'type_name': 'mul', 'processor_func': process_math, 'priority': 4}, # Multiplicación
|
|
{'type_name': 'div', 'processor_func': process_math, 'priority': 4} # División
|
|
]
|