Home > Software design >  ANTLR4: Re-visiting parse rules after the whole ast is visited
ANTLR4: Re-visiting parse rules after the whole ast is visited

Time:04-08

I am currently implementing generic functions for my own language, but I got stuck and currently have the following problem:

Generic functions can get called from another source file (another parser instance). Let's assume we have a generic function in source file B and we call it from source file A, which imports source file B. When this happens, I need to type-check the body of the function (source file B) once again for every distinct manifestation of concrete types, derived from the function call (source file A). For that, I need to visit the body of the function in source file B potentially multiple times.

Source file B:

type T dyn;

public p printFormat<T>(T element) {
    printf("Test");
}

Source file A:

import "source-b" as b;

f<int> main() {
    b.printFormat<double>(1.123);
    b.printFormat<int>(543);
    b.printFormat<string[]>({"Hello", "World"});
}

I tried to realize that approach by putting the code for analyzing the function body and its children in an inner function and call it every time I encounter a call to that particular function from anywhere (also from other source files). This seems not to work for some reason. I always get a segmentation fault. Maybe this is because the whole tree was already visited once?

For additional context: enter image description here

  • Related