cobalt
cobalt copied to clipboard
Attach information to specify if a assignment/method def is within a class or method
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.
I am not sure if i understand correctly, but would not such shift alter the semantics?
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.
Well, when nesting methods or classes I would expect the scope to change compared to top level ones.
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.
And how about the nested methods/assignments accessing some objects from outer scope? Probably some kind of lexical scoping is necessary for this.
It would be no different from when you define variables? Scoping should work the same I think. Do you have an example?
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.
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,
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.