Bookstore

Recent

Author Topic: [SOLVED] Assign control characters to constant fields...  (Read 611 times)

GypsyPrince

  • Jr. Member
  • **
  • Posts: 80
[SOLVED] Assign control characters to constant fields...
« on: March 24, 2020, 10:14:48 am »
This method does not work for assigning control character values to constants.

Code: Pascal  [Select]
  1. Const CTL_NULL = Chr(0)

The reason is doesn't work is because Chr() is a function and functions can't be assigned values via functions. .NET has a workaround for this by allowing you to set a variable field as "read only" so that it may be used as a constant onced assigned its value.
 
Since there are no literal values for control characters, does anyone know if Free Pascal might also have some type of workaround for this limitation?
« Last Edit: March 24, 2020, 07:33:31 pm by GypsyPrince »
May the (Mass × Acceleration) be with you!

fred

  • Full Member
  • ***
  • Posts: 162
Re: Assign control characters to constant fields...
« Reply #1 on: March 24, 2020, 10:30:40 am »
Code: Pascal  [Select]
  1.     Const CTL_NULL = #0;

See https://www.freepascal.org/docs-html/ref/refse8.html

Edit: added link.
« Last Edit: March 24, 2020, 10:39:45 am by fred »
Started with OmegaSoft Pascal on OS-9/68k , now Lazarus 2.0.6 / FPC 3.0.4 on Windows 7

GypsyPrince

  • Jr. Member
  • **
  • Posts: 80
Re: Assign control characters to constant fields...
« Reply #2 on: March 24, 2020, 06:15:30 pm »
Just for clarification...

a.) Can this same method be used to assign a literal value for any character?

Example:

Code: Pascal  [Select]
  1. Const
  2.     CHR_EXCLMTN = #33; // !
  3.     CHR_QSTNMRK = #63; // ?

b.) Can you provide me a link to any documentation where I can learn more about using this method?

Thanks!
May the (Mass × Acceleration) be with you!

fred

  • Full Member
  • ***
  • Posts: 162
Re: Assign control characters to constant fields...
« Reply #3 on: March 24, 2020, 06:34:24 pm »
Quote
a.) Can this same method be used to assign a literal value for any character?
Yes

Quote
b.) Can you provide me a link to any documentation where I can learn more about using this method?
Already in my first post, see https://www.freepascal.org/docs-html/ref/refse8.html

Since it is a number you can also use hex numbers like #$0A instead of #10
It was already in Pascal over 30 years ago when I looked in old Omegasoft Pascal doumrentation.
Started with OmegaSoft Pascal on OS-9/68k , now Lazarus 2.0.6 / FPC 3.0.4 on Windows 7

lucamar

  • Hero Member
  • *****
  • Posts: 2572
Re: Assign control characters to constant fields...
« Reply #4 on: March 24, 2020, 06:34:54 pm »
a.) Can this same method be used to assign a literal value for any character?

Yes, it can be used for any character code form #00 to #255 (IIRC), and you can even use normal hexadecimal notation, i.e. form #$00 to #$FF.

Quote
b.) Can you provide me a link to any documentation where I can learn more about using this method?

AFAIK, the already given link to Character Strings in the Reference guide is all there is. And it's really all there is to it; it's just a notation to add easily non-printable and dificult-to-type characters, consisting of a '#' plus an unsigned integer (which itself has its own sintax, which can be seen in the same Reference, section Numbers)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

GypsyPrince

  • Jr. Member
  • **
  • Posts: 80
Re: Assign control characters to constant fields...
« Reply #5 on: March 24, 2020, 07:32:27 pm »
@fred and @lucamar

Quote
The string consists of standard, 8-bit ASCII characters or Unicode (normally UTF-8 encoded) characters. The control string can be used to specify characters which cannot be typed on a keyboard, such as #27 for the escape character.

Thanks to both of you! This obscure little passage on that page you linked is exactly the information I was looking for. Very helpful! Also important is for me to know that Pascal calls them 'Character Strings' rather than just 'Characters', and 'Control Strings' rather than 'Control Characters'.  The nomenclature is important when trying to comprehend a new language/development system.
May the (Mass × Acceleration) be with you!

lucamar

  • Hero Member
  • *****
  • Posts: 2572
Re: Assign control characters to constant fields...
« Reply #6 on: March 24, 2020, 07:54:16 pm »
Also important is for me to know that Pascal calls them 'Character Strings' rather than just 'Characters', and 'Control Strings' rather than 'Control Characters'.  The nomenclature is important when trying to comprehend a new language/development system.

Yes, it's important, so dont be misguided ;)

When the docs talk of "character Strings" they meant exactly that: strings made up of characters ... even it it's just one! :)

And "control string" refers, again, exactly to that: the string in the source code used to represent a character by its code, so e.g. #27 is a three-char control string representing, in the source, a single character <Esc>.

Note that while fairly exact and complete, FPC and Lazarus documentation isn't written by native English speakers nor paid technical writers, so despite all their care you'll find sometimes some expressions which are not quite kosher in normal English usage. Just try to understand the underlying meaning (it's usually very easy) and keep going on.

Or, if you feel you found an egregious error, log a bug against the documentation with the correct wording/information and feel proud of participating in an free/open source project ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 1489
Re: [SOLVED] Assign control characters to constant fields...
« Reply #7 on: March 24, 2020, 09:47:38 pm »
This method does not work for assigning control character values to constants.
Code: Pascal  [Select]
  1. Const CTL_NULL = Chr(0)
What if you just try it?
It works quite well. Some functions the compiler can calculate at the compile stage and substitute the result.

GypsyPrince

  • Jr. Member
  • **
  • Posts: 80
Re: [SOLVED] Assign control characters to constant fields...
« Reply #8 on: March 25, 2020, 07:28:28 am »
@ASerge

The expression

Quote
Const CTL_NULL = Chr(0)

raises an error on my machine when I compile. Most languages do not allow you to use functions to assign values to constants, so I like to remain consistent in my practices.

@lucamar

Quote
Note that while fairly exact and complete, FPC and Lazarus documentation isn't written by native English speakers nor paid technical writers, so despite all their care you'll find sometimes some expressions which are not quite kosher in normal English usage.

I am finding this to be very true. The guys who develop FreePascal/Lazarus and their respective documentation are brilliant.  The onus is on me to try and figure out what they're trying to tell me in the documentation.
May the (Mass × Acceleration) be with you!

avk

  • Sr. Member
  • ****
  • Posts: 256
    • my self-education project
Re: [SOLVED] Assign control characters to constant fields...
« Reply #9 on: March 25, 2020, 08:17:47 am »
@ASerge

The expression

Quote
Const CTL_NULL = Chr(0)

raises an error on my machine when I compile.
Perhaps because it should have been written
Code: Pascal  [Select]
  1. Const CTL_NULL = Chr(0);
?

PascalDragon

  • Hero Member
  • *****
  • Posts: 1088
  • Compiler Developer
Re: Assign control characters to constant fields...
« Reply #10 on: March 25, 2020, 09:38:52 am »
a.) Can this same method be used to assign a literal value for any character?

Yes, it can be used for any character code form #00 to #255 (IIRC), and you can even use normal hexadecimal notation, i.e. form #$00 to #$FF.

Two byte characters are supported as well for WideChars:

Code: Pascal  [Select]
  1. const
  2.   MyChar = #$2705; // WHITE HEAVY CHECK MARK

Please note that 3.2 and newer also supports characters outside the BMP (e.g. Emojis or anything else with a code point >= $10000), however these won't be characters then, but strings:

Code: Pascal  [Select]
  1. const
  2.   MyEmoji = #$1F602; // FACE WITH TEARS OF JOY

@ASerge

The expression

Quote
Const CTL_NULL = Chr(0)

raises an error on my machine when I compile. Most languages do not allow you to use functions to assign values to constants, so I like to remain consistent in my practices.

As avk wrote the following works:

Code: Pascal  [Select]
  1. const
  2.   CTL_NULL = Chr(0);

Also Chr is not a normal function, it's a so called compiler intrinsic (of the constant variant, in contrast to e.g. Writeln) and as such it can be used inside constants as well. There are a few other intrinsics that allow the same (e.g. Abs, Sqrt, Int, etc.).

winni

  • Hero Member
  • *****
  • Posts: 1112
Re: [SOLVED] Assign control characters to constant fields...
« Reply #11 on: March 25, 2020, 10:34:55 pm »
Hi!

Most languages ....

Code: Pascal  [Select]
  1.  Const CTL_NULL = Chr(0)
  2.  

That works fine with fpc 3.04 .
It seems you have an old compiler version.

The trick is called: update.

Winni

winni

  • Hero Member
  • *****
  • Posts: 1112
Re: [SOLVED] Assign control characters to constant fields...
« Reply #12 on: March 26, 2020, 01:37:59 am »
Cars will never prevail!!!

May  g/2 * sqr(t) be with you!

Winni

440bx

  • Hero Member
  • *****
  • Posts: 1558
Re: [SOLVED] Assign control characters to constant fields...
« Reply #13 on: March 26, 2020, 02:38:39 am »
@ASerge

The expression

Quote
Const CTL_NULL = Chr(0)

raises an error on my machine when I compile. Most languages do not allow you to use functions to assign values to constants, so I like to remain consistent in my practices.
Just for the record, while Chr looks like a function, it isn't a function.  It works like a typecast and, most languages that implement typecasting will cast a constant (as long as the cast is valid.) In both FPC and Delphi, the following is identical and compiles just fine (no function calls involving the constants)
Code: Pascal  [Select]
  1. program TestChr;
  2.  
  3. const
  4.   a = chr(36);
  5.   b = char(36);  // same as above
  6.  
  7. begin
  8.   writeln(a);
  9.   writeln(b);
  10. end.
  11.  
produces the following :
Code: ASM  [Select]
  1. TestChr.lpr:8                             writeln(a);
  2.  
  3.  e8195a0000               callq  0x100006e90 <fpc_get_output>
  4.  4889c3                   mov    %rax,%rbx
  5.  4889da                   mov    %rbx,%rdx
  6.  
  7.  41b824000000             mov    $0x24,%r8d  <--- a = chr(36)
  8.  
  9.  b900000000               mov    $0x0,%ecx
  10.  e8f35d0000               callq  0x100007280 <fpc_write_text_char>
  11.  e86e220000               callq  0x100003700 <fpc_iocheck>
  12.  4889d9                   mov    %rbx,%rcx
  13.  e8565b0000               callq  0x100006ff0 <fpc_writeln_end>
  14.  e861220000               callq  0x100003700 <fpc_iocheck>
  15.  
  16. TestChr.lpr:9                             writeln(b);
  17.  
  18.  e8ec590000               callq  0x100006e90 <fpc_get_output>
  19.  4889c3                   mov    %rax,%rbx
  20.  4889da                   mov    %rbx,%rdx
  21.  
  22.  41b824000000             mov    $0x24,%r8d  <--- b = char(36)
  23.  
  24.  b900000000               mov    $0x0,%ecx
  25.  e8c65d0000               callq  0x100007280 <fpc_write_text_char>
  26.  e841220000               callq  0x100003700 <fpc_iocheck>
  27.  4889d9                   mov    %rbx,%rcx
  28.  e8295b0000               callq  0x100006ff0 <fpc_writeln_end>    
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

ASBzone

  • Sr. Member
  • ****
  • Posts: 323
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: [SOLVED] Assign control characters to constant fields...
« Reply #14 on: March 26, 2020, 04:00:31 am »
Okay, one last time for the comprehension impaired...

"...I like to remain consistent in my practices."

So, I don't care the reason why it doesn't work. I'm not going to use the Chr() function to assign values to constants.

What if the underlying reason it doesn't work is because there is something wrong in your installation that will affect features and functionality that you do care about?
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.7 r62681 / FPC v3.2.0-beta-r44391 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1909 (Build 18363.720)
Other Systems: Windows 10 Pro x64, Version 1909 or greater