Reputation: 1189
I am writing a small compiler for a java-like language, except that every thing is object in this language, even the primitive type.
Consider the following code:
class Main inherits IO {
fib(x : Int) : Int {
if x < 2
then x
else fib(x - 1) + fib(x - 2)
fi
};
main() : Object {
out_int(fib(10))
};
};
In my implementation, everything that pass into a function is a copy of the reference to that object. (so 10 is an integer object). So the fib
function will also return an integer object.
However, fib(x - 1) + fib(x - 2)
requires two function's results to be rvalue, so it can properly perform addition.
I couldn't come up with a automatic way to handle this problem: how to find a way so that the compiler knows when to generate code for a lvalue and when for a rvalue?
Upvotes: 1
Views: 104
Reputation: 3891
I think that you can just ignore the difference. Consider +
as a function that takes two references to integer objects a,b
and returns the reference to a new object whose value is the sum of a
and b
. This function must, of course, be implemented by the compiler but apart from that it should be similar to other functions.
Upvotes: 1