Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}
And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"People can have the Model T in any colour -- so long as it's black." - Henry Ford
I've updated my patch (attached), which allows for parentheses groupings such as :
const foo = (1<<2) | (2 & 5);
-Sterling
Quoted text · 12 lines
Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it." - Richard Feynman
Ok, attached is the updated patch, the result of a conversation between myself and Zeev.
*) Class constants are allowed to be operands to constants. However, constants must exist within the current class only, and they must be defined before usage, so for example:
class simple {
const second = 1;
const minute = 60 * second;
const hour = 60 * minute;
const day = 24 * hour;
}
Would work.
However:
class simple {
const minute = second * 60;
const second = 1;
}
Wouldn't, and neither would:
class simple {
const foo = "bar";
}
class simple1 {
const baz = "bar" . foo;
}
Currently, referencing the current class also doesn't work, ie,
class simple {
const bar = "baz";
const foo = simple::bar . "naz";
}
Although that could be added.
It is also important to note that const is now fully compile time, therefore const cannot, for example, reference a define() which is called above (because define() is runtime).
Also, this patch removes the ability to use operators in array initialization, as a bunch of people seem to have found that behaviour distasteful. Patch attached.
Quoted text · 15 lines
I've updated my patch (attached), which allows for parentheses groupings such as :
const foo = (1<<2) | (2 & 5);
-Sterling
Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"Nothing is particularly hard if you divide it into small jobs." - Henry Ford
Yargh, a little debug info got into the last patch.
Attached is the last patch, minus my little debug stuff.
Quoted text · 45 lines
Ok, attached is the updated patch, the result of a conversation between myself and Zeev.
*) Class constants are allowed to be operands to constants. However, constants must exist within the current class only, and they must be defined before usage, so for example:
class simple {
const second = 1;
const minute = 60 * second;
const hour = 60 * minute;
const day = 24 * hour;
}Would work.
However:
class simple {
const minute = second * 60;
const second = 1;
}Wouldn't, and neither would:
class simple {
const foo = "bar";
}class simple1 {
const baz = "bar" . foo;
}Currently, referencing the current class also doesn't work, ie,
class simple {
const bar = "baz";
const foo = simple::bar . "naz";
}Although that could be added.
It is also important to note that const is now fully compile time, therefore const cannot, for example, reference a
define()which is called above (becausedefine()is runtime).Also, this patch removes the ability to use operators in array initialization, as a bunch of people seem to have found that behaviour distasteful. Patch attached.
I've updated my patch (attached), which allows for parentheses groupings such as :
const foo = (1<<2) | (2 & 5);
-Sterling
Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"I can't give you a brain, so I'll give you a diploma"
- The Great Oz, The Wizard of Oz
The last patch introduced a couple of bugs:
-
global constants weren't allowed, this includes true, false, null, etc.
-
it overrode the default behaviour for var $variable = someconstant;
since we're folding these, such initialization cannot reasonably happen (big BC break).
Attached is a new patch (hopefully the last one), which fixes these bugs and adds the new functionality.
-Sterling
Quoted text · 46 lines
Yargh, a little debug info got into the last patch.
Attached is the last patch, minus my little debug stuff.
Ok, attached is the updated patch, the result of a conversation between myself and Zeev.
*) Class constants are allowed to be operands to constants. However, constants must exist within the current class only, and they must be defined before usage, so for example:
class simple {
const second = 1;
const minute = 60 * second;
const hour = 60 * minute;
const day = 24 * hour;
}Would work.
However:
class simple {
const minute = second * 60;
const second = 1;
}Wouldn't, and neither would:
class simple {
const foo = "bar";
}class simple1 {
const baz = "bar" . foo;
}Currently, referencing the current class also doesn't work, ie,
class simple {
const bar = "baz";
const foo = simple::bar . "naz";
}Although that could be added.
It is also important to note that const is now fully compile time, therefore const cannot, for example, reference a
define()which is called above (becausedefine()is runtime).Also, this patch removes the ability to use operators in array initialization, as a bunch of people seem to have found that behaviour distasteful. Patch attached.
I've updated my patch (attached), which allows for parentheses groupings such as :
const foo = (1<<2) | (2 & 5);
-Sterling
Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it." - Richard Feynman
Quoted text · 10 lines
However:
class simple {
const minute = second * 60;
const second = 1;
}Wouldn't, and neither would:
class simple {
const foo = "bar";
}So, only scalars could be initialized consts?
yes, that's how its always worked.
-Sterling
"Reductionists like to take things apart. The rest of us are just trying to get it together."
- Larry Wall, Programming Perl, 3rd Edition
Nice patch, but one problem - it allows users to use constants as a part of those expressions... Since the values of those are not known until compile time, we have an issue there. Technically we can disallow such constants from being used in those expressions, but I'm not sure how well we can explain this (you can use constants, but not in expressions?) I don't know, maybe it's an ok thing to say.
Zeev
At 04:06 08/04/2003, Sterling Hughes wrote:
Quoted text · 12 lines
Hi,
The attached patch allows constant expressions within class variables and constants. I came into this problem when I wanted the following sequence:
class foo {
const A = 1<<0;
const B = 1<<1;
const C = 1<<2;
}And the current parser doesn't allow such rules. I therefore added the attached patch which allows operators to be used on constants within these expressions (and also within array declarations). This is achieved by folding statements such as '1<<0' into their equivalent values at compile time. The following operators are supported: << >> | & + - * / . % ^ ~ xor
Please review it, and let me know if its ok to commit. :)
-Sterling
--
"People can have the Model T in any colour -- so long as it's black." - Henry Ford