What is a compile time expression?
Author |
Message |
Cezna
|
Posted: Sun Jun 20, 2010 8:18 am Post subject: What is a compile time expression? |
|
|
I am trying to make an array with initial values that are picture id's.
Here is the code I am having trouble with:
Turing: |
var coin : array 1 .. 6 of pointer to Coin := init (penny, nickle, dime, quarter, loonie, toonie)
var coin_pic : array 1 .. 6 of int := init (penny_pic_front, nickle_pic_front, dime_pic_front,
quarter_pic_front, loonie_pic_front, toonie_pic_front)
var coin_x : array 1 .. 6 of int := init (450, 400, 450, 400, 450, 400)
var coin_y : array 1 .. 6 of int := init (310, 310, 350, 350, 400, 400)
|
The penny, nickle, dime... in the coin array are all pointers to the class Coin.
The penny_pic_front, nickle_pic_front... in the coin_pic array are all picture id's.
The bottom two arrays work fine.
When I try to run this, I get an error on the first two arrays saying "Compile time expression expected".
I have come across this error many times in the past, but have never known what it meant.
I have usually just been able to work around it, but in this case, it would be very difficult to do this differently.
So, my question is, what is a compile time expression, and how do I use it (or is this just something that can't be done in Turing)? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Sun Jun 20, 2010 10:51 am Post subject: RE:What is a compile time expression? |
|
|
A compile time expression is evaluated when the code is compiled (turned from text into commands the computer understands). Runtime expressions are evaluated when the program executes.
Something like "var a := b + c" is a runtime expression because the compiler does not know what b and c are when it compiles. Likewise, "var a := 2 + 3" will be converted into "var a := 5" because the compiler knows what 2 + 3 is.
I don't know how Turing handles pointers and whatnot, but your problem is prolly your init command. Don't think Turing can take variables (or pointers perhaps) as parameters to init. |
|
|
|
|
|
2goto1
|
Posted: Sun Jun 20, 2010 12:11 pm Post subject: RE:What is a compile time expression? |
|
|
Insectoid's thought is likely spot on. As Insectoid has indicated "var a:= 2 + 3" is a compile time expression. 2 and 3 are literal values which makes life easy for the Turing compiler to validate their type and thus validate the entire expression. I would suspect that any expression in Turing that is comprised of literal values like this is considered a compile time expression. This restriction would have different strengths and weaknesses to it. I would guess that the restriction exists because of the Turing language's target audience.
Unrelated to that question, what are you finding would be extremely difficult about approaching it differently, and how would you normally have worked around it in the past?
2goto1 |
|
|
|
|
|
DemonWasp
|
Posted: Sun Jun 20, 2010 12:58 pm Post subject: RE:What is a compile time expression? |
|
|
The problem here is actually fairly involved. The init expression in Turing is assigning the value of the array in the data loaded into memory before the program starts running. In a compiled language, this would be like the data block in ELF or its equivalent in Windows.
This means that since the values of those variables have not yet been computed, they cannot be used.
This is not a limitation unique to Turing. C (and presumably all other compiled languages) have the same "problem". For example, there's this program in C:
code: |
int a = rand();
int main () {
int foo[] = {1,2,3,a};
}
|
Which causes gcc to report "error: initializer element is not constant". If you remove rand() and replace it with 4, of course, gcc recognizes that it's a constant expression and lets you use it, which Turing does not.
The simple solution to this problem is to perform the initialization manually after creating the array. |
|
|
|
|
|
Cezna
|
Posted: Sun Jun 20, 2010 5:46 pm Post subject: RE:What is a compile time expression? |
|
|
In the past, I would justhave hardcoded everything, but in this case, I will need something like 50 lines of code for mouse tracking and buttons in waht was supposed to be a short program.
That's what I thought at least. Since posting, I have been going over other ways I could shorten the code involved, and I think I may have come up with another way. |
|
|
|
|
|
|
|