Lazarus

Free Pascal => General => Topic started by: Avinash on May 14, 2021, 08:53:34 pm

Title: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: Avinash on May 14, 2021, 08:53:34 pm
In Borland Pascal programs two consecutive pluses were often used when writing string expressions in several lines
Code: Pascal  [Select][+][-]
  1. S := some_long_string_expression +
  2.      + some_long_string_expression +
  3.      + some_long_string_expression;

It looks good and improves the readability of the code.
FPC doesn't understand more than one plus for strings (although it does for numbers).
To minimize the editing of old code when adapting to FPC, I partially solved the issue by operator overloading:
Code: Pascal  [Select][+][-]
  1.   Operator + (const S: String): String; Inline;
  2.   begin
  3.     Result := S;
  4.   end;

But two problems remained unresolved:
(a) this does not work for definitions of multi-line string constants
(b) it doesn't work in expressions like
Code: Pascal  [Select][+][-]
  1. S := some_long_string_expression +
  2.      + CHAR + some_long_string_expression;

I was unable to overload the operator for Char in a similar way (Ā«Impossible operator overloadĀ»).

It is not difficult to correct the code a little, but maybe some solutions are still possible?
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: winni on May 14, 2021, 09:08:48 pm
Hi!

You don't need to overload anything. It is all there.

Code: Pascal  [Select][+][-]
  1. const  s = 'abc'+'d'+'efg' +#32 +'hij';
  2. var p : pchar;
  3.       msg : string;
  4.  
  5. begin
  6. p := ' XYZ';
  7. msg :=  s + p;
  8. showMessage (msg);
  9. end;
  10.  

Winni
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: MarkMLl on May 14, 2021, 10:44:39 pm
I'm one of the noisier people around here when it comes to asking for language extensions, but I've never seen anything like this before and wish I hadn't today.

Strings aren't numbers, they aren't signed, and + isn't defined as a unary operator on them. If it was, it would be far more useful as an alias of StrToInt().

MarkMLl
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: winni on May 14, 2021, 10:54:35 pm
Hi!
Code: Pascal  [Select][+][-]
  1.  
  2. String3 := String1 + String2;

This horrible Syntax was introduced by Turbo Pascal and stolen from Basic.

The original Pascal Syntax - which also exists n fpc - is

Code: Pascal  [Select][+][-]
  1. String3 := concat (String1,String2);
  2.  


A lot of bad ideas where introduced by Turbo Pascal like the mini sets with only 256 elements.

Or the "else" instead of "otherwise" in the case statement.

And the Unit concept was not their idea but stolen from UCSD Pascal. There the Units were called Library.

High time to get rid of that trash.

Winni
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: MarkMLl on May 14, 2021, 11:17:34 pm
@Winni I'm happy enough with operators, and in fact would prefer to see more flexibility in being able to define and use them. However in retrospect + as concatenation has- IMO- turned out to be a very bad idea, since it's meant that the language can't easily also use + for vector summation.

I came across a book in the university library (i.e. quite a few years ago) which discussed monadic and dyadic operators, but didn't really get the idea until I started tinkering with APL comparatively recently. The practical upshot is that an operator not appearring to make sense in the current context is not sufficient grounds to build an "ignore this" rule into the compiler: call it a syntax error but don't just drop it.

MarkMLl
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: winni on May 15, 2021, 12:27:49 am
@MarkMLI

Do you mean https://dl.acm.org/doi/abs/10.1145/390009.804450 (https://dl.acm.org/doi/abs/10.1145/390009.804450)

Gosh - The last time I worked with APL was on an Apple ][ clone. They had implemented the APL symbols in their "machine code" - copying the Apple ROM was forbidden.

Never became a good friend of APL. But in the young days you try and try ...

Winni
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: marcov on May 15, 2021, 12:48:39 am
One can argue that this is exploitation of an unintended BP bug.

Correct and move on IMHO.
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: MarkMLl on May 15, 2021, 09:31:54 am
Do you mean https://dl.acm.org/doi/abs/10.1145/390009.804450 (https://dl.acm.org/doi/abs/10.1145/390009.804450)

Gosh - The last time I worked with APL was on an Apple ][ clone. They had implemented the APL symbols in their "machine code" - copying the Apple ROM was forbidden.

Yes, but looking at https://en.wikipedia.org/wiki/APL_syntax_and_symbols#Monadic_functions (and ignoring the character set issue) one can see that many of the operators are used for both monadic and dyadic functions. I /think/ that in practice one can parse a language with dyadic and both pre- and postfix operators provided that the same character is used for no more than two of those (e.g. ^ for monadic dereference and dyadic xor, and ! for monadic factorial and monadic logical-negation, and - for dyadic subtraction and monadic numeric-negation).

I've come across an interesting anecdote where looking at a problem "the APL way" suggested a novel solution, and I've used it to post-process database results before PostgreSQL had analytic functions. But unfortunately the vast majority of people who recommend it have no concept of the fact that a computer might have limited memory.

MarkMLl
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: PascalDragon on May 16, 2021, 05:19:56 pm
In Borland Pascal programs two consecutive pluses were often used when writing string expressions in several lines

Delphi doesn't support this, so I believe they consider that a bug as well.

So just like marcov said: fix that in your code and move on.
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: Handoko on May 16, 2021, 06:03:06 pm
Does that really exist in any Pascal dialect? I never knew about it, where can I read the documentation about the double pluses syntax?
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: lucamar on May 16, 2021, 06:54:47 pm
Does that really exist in any Pascal dialect? I never knew about it, where can I read the documentation about the double pluses syntax?

Nowhere. I don't think they did it on purpose; it's probably just a gimmick of their parser.
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: Bart on May 16, 2021, 08:02:32 pm
Does that really exist in any Pascal dialect?

I also could not believe it, but it compiles in TP 6.0.
Yikes!

Bart
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: Avinash on May 17, 2021, 04:58:11 am
Virtual Pascal and TMT Pascal compile this as well.
I think this is a matter of ease of understanding the code; that a programming language is not only a cold theory about syntax, but also the presence of a certain human interface. After all, the language is made to facilitate interaction with the machine, not for the intrinsic value of following some formal logic.
So we can write without a semicolon
Code: Pascal  [Select][+][-]
  1. Repeat Delay(1) Until KeyPressed;
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: MarkMLl on May 17, 2021, 08:01:14 am
Virtual Pascal and TMT Pascal compile this as well.
I think this is a matter of ease of understanding the code; that a programming language is not only a cold theory about syntax, but also the presence of a certain human interface. After all, the language is made to facilitate interaction with the machine, not for the intrinsic value of following some formal logic.
So we can write without a semicolon
Code: Pascal  [Select][+][-]
  1. Repeat Delay(1) Until KeyPressed;

That is because the semi-colon is DEFINED as being a statement SEPARATOR, and until is not a statement. It's exactly the same as not having a semi-colon before else.

You're not going to get + + implemented.

MarkMLl
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: Avinash on May 17, 2021, 08:48:10 am
...So the semicolon is defined in such a way that we can write....
Title: Re: Incompatibility with Borland Pascal in expressions like "string + + string"
Post by: marcov on May 17, 2021, 09:29:54 am
...So the semicolon is defined in such a way that we can write....

And that (semicolon behaviour) is a documented feature. The double plus is purely empirical. That is the difference.

TinyPortal © 2005-2018