• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Implemented Character Literals

PixelPaladin

New member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Messages
110
I had the idea to add character literals to cerberus since a while. Until now there were two ways to get the value of a character:

1. write it as a string literal and get the first character of that string:
Code:
Print "*"[0]

2. look up the value of the character in a Unicode table:
Code:
Print 42 'value of '*'

So I added character literals to my version of trans (within my testing environment). Since the apostrophe character is already used for comments I used back-ticks for character literals:
Code:
Print `*`

Note: This feature does not conflict with any existing code. It is also very easy to integrate into the trans tool. The compiler translates character literals directly to integer values. It is also possible to use them in computations, in arrays and anywhere else – and it supports all escape characters
Code:
Print `~u00E6` * -`~n` * -10 + `*`
' => 23042

Print String.FromChars([`H`,`e`,`l`,`l`,`o`,`!`])
' => Hello!
I also had to add a new escape character for back-ticks (which can be used in character literals and within strings):
Code:
Print `~g`
' => 96

Print "this is a back-tick -> ~g"

The only downside I can see at the moment is that it is a new language feature – so it is not supported by any ide at the moment (which means: no syntax highlighting).

So … is there any interest in such a feature? Or any ideas or feedback?
 
Last edited by a moderator:
Interest? Yes. Feedback? The example for how to use them in computation seems a bit over sophisticated to me. I prefer samples where I can understand easily every step:
Code:
Print `~u00E6` * -10
(Just saying in case someone is going to add them in the language docs)

But overall: Great, thanks!
 
So it does the same like Chr does?

Blitz Max's Chr() function created a string of length 1 from a character value (same as String.FromChar() in cerberus-x). Character literals are more like The Asc() function. They represent the integer value of a characer (Asc("*") = 42). However character literals translate directly to the integer value of the character while Asc() is a function call (at least i think so).

It works like character literals in C or C++:
Code:
c/c++:
int c;
c = 'x';

cerberus-x:
Local c:Int
c = `x`

@Holzchopf: I know, my examples tend to be messy some times.
Code:
Print `*`
' => 42

Print -`*`
' => -42

Print `*` * 10
' => 420

Print `c` - `a`
' => 2

Print `~u0003` + `~u0002`
' => 5
 
Isn't that similar to this?

Code:
Function Main:Int()
  Local s:String = "A"
  Print (s+"="+s[0])   ' Prints 65
  s = "Ä"
  Print (s+"="+s[0])   ' Prints 196
  Return 0
End

Or is your solution more flexible?

This works too...
Code:
Print ("*"[0]*10)   '  Prints 420
 
@MikeHeart: Well it is a feature that is used in many c/c++ programs – and `c` is shorter than "c"[0]. Character literals are useful when writing code for parsing text (command line tools, interpreters for scripting languages, compilers, ...). Also transcc knows instantly what to do: translate it into an integer value. "c"[0] on the other hand is a string combined with an index expression – and at some point transcc recognizes, that it can simplify the expression to a single value. I am not really sure if the speedup of character literals compared to the current workaround is perceptible, but there definitely exists a speedup :)
 
Hey everyone! I had not much time within the last weeks but I finally found time to uploaded the character-literal feature on GitHub. If anyone wants to test it here is the link:

https://github.com/PixelPaladin/cerberus/tree/feature-character-literals

And here is a short example:

Code:
Function Main:Int()
  
   Print String.FromChars([`H`,`e`,`l`,`l`,`o`,`,`,` `,`W`,`o`,`r`,`l`,`d`,`!`])
  
   Print "The answer is " + `*`
  
   Print "this is a backtick -> ~g"
  
   Return 0
  
End
 
Last edited by a moderator:
Okay, I made a pull request. I also changed the new escape character for backticks from ~b to ~g (for grave accent) since \b is a common escape sequence for the backspace character in other languages.
 
Back
Top Bottom