cobalt icon indicating copy to clipboard operation
cobalt copied to clipboard

Attach information to specify if a assignment/method def is within a class or method

Open Michael2109 opened this issue 6 years ago • 9 comments

If an assignment/method definition is within a class or inside a method we need to generate code differently. In order for us to generate code differently we need extra information to be attached to methods and assignments or to restructure the AST.

E.g.

let x = 10

let y() = do
    let z = do
        # Do something here
        20
    z

This is the equivalent of this in Java (roughly),

private int x()
{
    return 10;
}
private int y() 
{
    return z$();
}
private static int z$()
{
    // Do something here
    return 20;
}

We currently have is that x, y, and z are all stored in the AST as either Assign or MethodDef. The problem is that for us to generate code we need to know if the value and method defs are nested inside methods are not. A solution to this is that any method/value definitions nested in a method need to be shifted to be within the class instead. Then code generation should work the same. We will need to create an extra stage to do this for us.

Michael2109 avatar May 20 '18 20:05 Michael2109

I am not sure if i understand correctly, but would not such shift alter the semantics?

FilipJanitor avatar May 21 '18 20:05 FilipJanitor

In what way? Either way there is no such thing as a nested method when generating byte code so we need have them be generated like normal methods. The only way I see this working is by changing the AST structure and then generating the code.

Michael2109 avatar May 21 '18 21:05 Michael2109

Well, when nesting methods or classes I would expect the scope to change compared to top level ones.

FilipJanitor avatar May 21 '18 21:05 FilipJanitor

Okay so the issue is scope? The idea is we make them private so they are completely inaccessible from the outside. In terms of other methods inside the class trying to access it the compiler would throw an error if it is outside of scope. It's more a restriction built into the compiler.

Michael2109 avatar May 21 '18 21:05 Michael2109

And how about the nested methods/assignments accessing some objects from outer scope? Probably some kind of lexical scoping is necessary for this.

FilipJanitor avatar May 21 '18 21:05 FilipJanitor

It would be no different from when you define variables? Scoping should work the same I think. Do you have an example?

Michael2109 avatar May 21 '18 22:05 Michael2109

Again - I apologize if I am talking about something different from what you mean by this issue. As for the example, here is a material I used when learning for my compiler course which uses a nice tangled deep nesting example, There was another material with even trickier example but I cant find it. Basically, it is necessary to be able to determine which actual values of x and y are used during some deep nesting.

FilipJanitor avatar May 22 '18 09:05 FilipJanitor

I'm a bit unsure of how this would cause any problems. All of the error checks would be made before we restructure the AST so scoping and everything should be dealt with normally. Of course I might be missing something. I can't have a full read of what you attached yet but will a bit later. EDIT: Actually there is a big problem with this. The analysis is okay I think but in terms of accessing values outside of the method I don't know how it would work. I'll look up how best to do this,

Michael2109 avatar May 22 '18 09:05 Michael2109

This explains how Scala deals with nested methods. Also shows that Scala has an option to show Java code which could be useful. https://stackoverflow.com/questions/2482326/the-cost-of-nested-methods I wonder if nested methods should be avoided for now. If we are focusing on code generation we should be able to get everything else ready and then work on the AST structure later.

Michael2109 avatar May 22 '18 14:05 Michael2109