Compilation error for negative constant on right side of an operation
Hi GPC crew, The following sample code does not compile properly with GPC (20050331). Program Real; Var R1, R2 : Real; Begin R1 := 4.21; R2 := R1 * -1.0; WriteLn(R2); End. GPC output: real.pas:6: error: syntax error before `-' The problem occurs with negative integer constants and any operator (+-*/) as well. Strange enough, if I modify the code for: R1 := 4.21; R2 := -1.0; R2 := R1 * R2; Or even for: R2 := -1.0 * R1; The code compiles and the output is as expected. It looks like one can assign a negative constant to a variable but cannot use it as an operand if it is on the right side. This is not a problem for multiplying but for the other operators... Could this be a bug? Thanks Pascal Viandier pascal@accovia.com
Pascal Viandier wrote:
The following sample code does not compile properly with GPC (20050331).
Program Real; Var R1, R2 : Real; Begin  R1 := 4.21; R2 := R1 * -1.0; WriteLn(R2); End.
GPC output: real.pas:6: error: syntax error before `-'
The problem occurs with negative integer constants and any operator (+-*/) as well.
Strange enough, if I modify the code for: R1 := 4.21; R2 := -1.0; R2 := R1 * R2;
Or even for: R2 := -1.0 * R1;
The code compiles and the output is as expected. It looks like one can assign a negative constant to a variable but cannot use it as an operand if it is on the right side. This is not a problem for multiplying but for the other operators... Could this be a bug?
No, it's a simple parentheses. The correct Pascal syntax is: R1 * (-1.0) Borland Pascal has a different (non-standard) precedence for unary minus, so this works there. GPC does this with `{$borland-pascal}' (not by default, as it may cause non-standard results in other cases). Frank -- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/, 7977168E GPC To-Do list, latest features, fixed bugs: http://www.gnu-pascal.de/todo.html GPC download signing key: ACB3 79B2 7EB2 B7A7 EFDE D101 CD02 4C9D 0FE0 E5E8
Frank Heckenbach wrote:
Pascal Viandier wrote:
The following sample code does not compile properly with GPC (20050331).
Program Real; Var R1, R2 : Real; Begin R1 := 4.21; R2 := R1 * -1.0; WriteLn(R2); End.
GPC output: real.pas:6: error: syntax error before `-'
The problem occurs with negative integer constants and any operator (+-*/) as well.
Strange enough, if I modify the code for: R1 := 4.21; R2 := -1.0; R2 := R1 * R2;
Or even for: R2 := -1.0 * R1;
The code compiles and the output is as expected. It looks like one can assign a negative constant to a variable but cannot use it as an operand if it is on the right side. This is not a problem for multiplying but for the other operators... Could this be a bug?
No, it's a simple parentheses. The correct Pascal syntax is:
R1 * (-1.0)
Borland Pascal has a different (non-standard) precedence for unary minus, so this works there. GPC does this with `{$borland-pascal}' (not by default, as it may cause non-standard results in other cases).
Then, I think we need the following patch for --mac-pascal --- gpc-lex.c.orig Wed Aug 17 19:15:27 2005 +++ gpc-lex.c Wed Aug 17 19:15:52 2005 @@ -654,7 +654,7 @@ /* `+' and `-' have different precedence in BP than in Pascal. To handle this we have to use different tokens. */ - if (co->pascal_dialect & B_D_PASCAL) + if (co->pascal_dialect & B_D_M_PASCAL) switch (value) { case '+': value = LEX_BPPLUS; break; Regards, Adriaan van Os
Adriaan van Os wrote:
Borland Pascal has a different (non-standard) precedence for unary minus, so this works there. GPC does this with `{$borland-pascal}' (not by default, as it may cause non-standard results in other cases).
Then, I think we need the following patch for --mac-pascal
OK, applying. Frank -- Frank Heckenbach, frank@g-n-u.de, http://fjf.gnu.de/, 7977168E GPC To-Do list, latest features, fixed bugs: http://www.gnu-pascal.de/todo.html GPC download signing key: ACB3 79B2 7EB2 B7A7 EFDE D101 CD02 4C9D 0FE0 E5E8
participants (3)
-
Adriaan van Os -
Frank Heckenbach -
Pascal Viandier