Recent

Author Topic: Incompatibility with Borland Pascal in expressions like "string + + string"  (Read 4597 times)

Avinash

  • New Member
  • *
  • Posts: 41
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?
« Last Edit: May 14, 2021, 08:58:13 pm by Avinash »

winni

  • Hero Member
  • *****
  • Posts: 2478
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

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 2478
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

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
@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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 2478
@MarkMLI

Do you mean 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

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9437
  • FPC developer.
One can argue that this is exploitation of an unintended BP bug.

Correct and move on IMHO.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
Do you mean 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 3170
  • Compiler Developer
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.

Handoko

  • Hero Member
  • *****
  • Posts: 4311
  • My goal: build my own game engine using Lazarus
Does that really exist in any Pascal dialect? I never knew about it, where can I read the documentation about the double pluses syntax?

lucamar

  • Hero Member
  • *****
  • Posts: 4135
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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 4342
    • Bart en Mariska's Webstek
Does that really exist in any Pascal dialect?

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

Bart

Avinash

  • New Member
  • *
  • Posts: 41
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;
« Last Edit: May 17, 2021, 05:00:20 am by Avinash »

MarkMLl

  • Hero Member
  • *****
  • Posts: 2841
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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Avinash

  • New Member
  • *
  • Posts: 41
...So the semicolon is defined in such a way that we can write....

 

TinyPortal © 2005-2018