Friday, April 29, 2005

Constant symbols

I'm working right now in the implementation of constant symbols. This is how constants are declared in Freya:
MathHelper = class
public
const Pi: Double := 3.141592653589;
const E = 2.718281828459;
end;
Constant can be declared inside a method implementation too:
method TrySave;
const
MAX_ATTEMPTS = 3;
begin
// ...
end;
Local constants are easy to implement, since they only exist at compile time, and member constants don't use them in their own definitions. On the other hand, member constants require a careful implementation. The expression in a constant declaration may contain references to other constants, as long as these references don't cause circular dependences. This is a simple example:
Trigonometry = static class
public
const PiOver2 = MathHelper.Pi / 2.0;
// ...
end;

MathHelper = class
public
const Pi: Double := 3.141592653589;
// ...
end;
I think I'll handle constant definitions right before performing "body resolution" on the Abstract Syntax Tree. Probably, I'll gather all constants from the compiling project in a single list, to execute a topological sort on this list. I have a vague idea about a brute force algorithm for topological sort, but I'm afraid I'll have to check my copy of The Art of Computer Programming for a better approach.

Labels:

0 Comments:

Post a Comment

<< Home