Skip to content

Semantic type analyser brainstorming #247

Description

@PeterTillema

Heya! So, I've been working on a MATLAB to Python/NumPy converter the past few months, using your execellent tools for this. As you said multiple times in issues/code, you need to get a semantic type analyser working. So, my idea was to share some ideas about analysing and code generation.

For example, what would happen if you have the following (nonsense) code:

fileDir = [pwd filesep 'dir' filesep]
if ~exist(fileDir)
    disp(fileDir(2))
end

If you translate that to Python, you would expect something like

fileDir = np.array([[os.getcwd(), os.filesep, 'dir', os.filesep]])  # note: 2D-array!
if not os.path.exists(fileDir):
    print(fileDir[1])

which of course doesn't work right, as os.path.exists can't take a NumPy array. You can change the initialization, but the array might be used as a proper array, and then the code breaks. So what are your thoughts about this?

Also, indexing. Yay. A single index in MATLAB is perfectly fine, but logically won't get the same results as with NumPy. What kind of code should be generated do you think?


Regarding my semantic type analyser, I have one main function, which looks like this:

    def visit(self, tree, relation=None):
        name = 'visit_' + tree.__class__.__name__
        if hasattr(self, name):
            getattr(self, name)(tree, relation)
        else:
            warnings.warn(
                'Couldn\'t check node ' + tree.__class__.__name__ + ' during the SemanticTypeAnalyser pass')

which visits the right node, other functions in the same class. So far, everything is fine. But as you said, it is a hard task, for example, how do you deal with types and sizes? How do you track them/store them in a tree? When are you visiting functions? How do you handle array indexing, or function calls?


I may share my entire type analyser/code generator if you would like :) So far, it produces syntactically correct (as in, whitespace, comments), but when running the output program you get a ton of errors. For example, I can't properly detect if you call a function or index an array, so it always uses () rather than [] if necessary, which obviously won't work.

So, TL;DR: I'm curious what your thoughts are about this :) (and then especially the semantic type analyser). Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions