Implemented Character Literals

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
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:

Holzchopf

Moderator
Staff member
Joined
Jul 31, 2017
Location
Bern, Switzerland
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!
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
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
 

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Location
Germany
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
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
@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 :)
 

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
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:

PixelPaladin

Active Member
CX Code Contributor
3rd Party Module Dev
Joined
Aug 27, 2017
Location
Germany
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.
 
Top Bottom