• Hello everyone,

    We're happy to announce that our forum is back online! We needed to take some time off to discuss and decide on how to handle sensitive topics in our community.

    The team has posted an in-depth comment regarding a recent incident and our stance on politics and sexuality in our forum. We also added a dedicated paragraph about this in the forum guidelines. This is part of our commitment to maintain a welcoming and inclusive environment for all our users. Your understanding and continued support in building a creative and respectful community are highly appreciated.

    Thank you, the CX Dev-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:
So it does the same like Chr does?
 
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 :)
 
Ok I am sold. :)
 
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:
Will try to test it on the weekend!
 
sorry, had no time to test. Will hopefully soon.
 
@PixelPaladin It works great. Thank you! A push request including some docs from you would be cool.
 
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.
 
Implemented in GitHub. Thank you!
 
Back
Top Bottom