Newer
Older
tac2grc / src / portNaming.py
@lukas lukas on 14 Aug 2022 5 KB initial commit
import re
import os
import json
from defaultBlockTypes import defaultBlockTypes

INPUT_FUNCTIONS = [
        "tacStdBlockGetInput",
        "tacGraviMatrixGetPointerInput"
        ]

OUTPUT_FUNCTIONS = [
        "tacStdBlockSetOutput",
        "tacGraviMatrixSetPointerOutput"
        ]

def getFiles(blockType, sourceDirectories):
    files = []
    for sourceDirectory in sourceDirectories:
        files += ["%s/%s" % (data[0], file)
                for data in os.walk(sourceDirectory) 
                for file in data[2] if re.match(".*tac%sBlock.c" % blockType.name.replace("Rm", "Rmn"), file) != None]
    return files

def resolvePortNames(blockTypes, sourceDirectories):
    missingBlocks = []
    multipleDefinitions = []
    for blockType in blockTypes:
        if blockType.name in defaultBlockTypes:
            continue
        files = getFiles(blockType, sourceDirectories)
        if len(files) == 0:
            print("WARNING: no source file for block type %s found" % blockType.name)
            missingBlocks.append(blockType.name)
            continue
        if len(files) > 1:
            multipleDefinitions.append(blockType.name)
            print("WARNING: more than one matching source file for block type %s found, skipping" % blockType.name)
            continue
        print("reading file %s" % files[0])
        with open(files[0], "r") as file:
            content = "\n".join(file.readlines())
        for function in INPUT_FUNCTIONS:
            mapInputs(content, function, blockType.inputs)
        for function in OUTPUT_FUNCTIONS:
            mapOutputs(content, function, blockType.outputs)
        for entry in re.findall("\\w+ *= *\\*?\\(?pSelf->input\\[\\d+\\]\\)?", content):
            name = re.search("^\\w+", entry).group()
            position = int(re.search("(?<=\\[)\\d+(?=\\])", entry).group())
            blockType.inputs.mapName(position, name, 0)
        for entry in re.findall("pSelf->output\\[\\d+\\] *= *\\w+", content):
            name = re.search("\\w+$", entry).group()
            position = int(re.search("(?<=\\[)\\d+(?=\\])", entry).group())
            blockType.outputs.mapName(position, name, 0)
    if not os.path.exists("errors.json") or os.stat("errors.json").st_size == 0:
        content = {"missing_blocks": [], "multiple_definitions": []}
    else:
        with open("errors.json", "r") as file:
            content = json.load(file)
    with open("errors.json", "w+") as file:
        for blockTypeName in missingBlocks:
            if not blockTypeName in content["missing_blocks"]:
                content["missing_blocks"].append(blockTypeName)
        for blockTypeName in multipleDefinitions:
            if not blockTypeName in content["multiple_definitions"]:
                content["multiple_definitions"].append(blockTypeName)
        json.dump(content, file, indent=2)
        file.close()

def mapInputs(content, function, ports):
    for entry in re.findall("\\w+ *= *%s\\(pSelf *, *\\d+\\);" % function, content):
        name = re.search("^\\w+", entry).group()
        position = int(re.search("\\d+(?= *\\) *;)", entry).group())
        ports.mapName(position, name, 0)
    for entry in re.findall("\\w+->\\w+\\[\\d+\\] *= *%s\\(pSelf *, *\\d+\\);" % function, content):
        name = re.search("(?<=\\->)\\w+", entry).group()
        position = int(re.search("\\d+(?= *\\) *;)", entry).group())
        index = int(re.search("(?<=\\[)\\d+(?=\\])", entry).group())
        ports.mapName(position, name, index)
    for entry in re.findall("\\w+\\[\\d+\\] *= *%s\\(pSelf *, *\\d+\\);" % function, content):
        name = re.search("^\\w+", entry).group()
        position = int(re.search("\\d+(?= *\\) *;)", entry).group())
        index = int(re.search("(?<=\\[)\\d+(?=\\])", entry).group())
        ports.mapName(position, name, index)

def mapOutputs(content, function, ports):
    for entry in re.findall("%s\\(pSelf *, *\\d+ *, *pSelfParam->\\w+\\);" % function, content):
        name = re.search("\\w+(?=\\);$)", entry).group()
        position = int(re.search("\\d+(?= *, *pSelfParam->\\w+\\);$)", entry).group())
        ports.mapName(position, name, 0)
    for entry in re.findall("%s\\(pSelf *, *\\d+ *, *\\w+\\[\\d+\\]\\);" % function, content):
        name = re.search("\\w+(?=\\[\\d+\\]\\);$)", entry).group()
        position = int(re.search("\\d+(?= *, *\\w+\\[\\d+\\]\\);$)", entry).group())
        index = int(re.search("(?<=\\[)\\d+(?=\\])", entry).group())
        ports.mapName(position, name, index)
    for entry in re.findall("%s\\(pSelf *, *\\d+ *, *pSelfParam->\\w+\\[\\d+\\]\\);" % function, content):
        name = re.search("(?<=\\->)\\w+", entry).group()
        position = int(re.search("\\d+(?= *, *pSelfParam->\\w+\\[\\d+\\]\\);$)", entry).group())
        index = int(re.search("(?<=\\[)\\d+(?=\\]\\);$)", entry).group())
        ports.mapName(position, name, index)
    for entry in re.findall("%s\\(pSelf *, *\\d+ *, *\\w+\\);" % function, content):
        name = re.search("\\w+(?= *\\);$)", entry).group()
        position = int(re.search("\\d+(?= *, *\\w+\\);$)", entry).group())
        index = 0
        ports.mapName(position, name, index)