String literals
Hybrix represents text as strings made of characters. A character is a single letter or digit or punctuation mark or graphical glyph. Each character is represented by a single byte, forming an array of bytes. The system definition for string therefore looks like this:
type string is byte[]
Recall that a byte is an integer number between 0 and 255. Hybrix provides a standard mapping of characters for each number, called HASCII. It is based on the ASCII (American Standard Code for Information Interchange) and ISO/IEC 8859-1 standards, but with some differences for control characters and graphical glyphs.
NOTE: Some programming languages use a zero byte to indicate the end of the string. This is not needed in Hybrix, because arrays already track their size.
Escape sequences​
Hybrix text strings are enclosed in double quotes, for example:
module main
func start()
var s: string
"Hello, world!" -> s
end func
end module
Using your keyboard, it can be difficult to type certain characters such as ¢ or ★. Control codes such as green or clear screen do not have a standard glyph that we can show in the code. To handle these cases, string literals can include escape sequences enclosed in { } curly braces. In particular, {quote} is used to escape a double-quote (") which would otherwise terminate the string.
For example:
module main
func start()
var s: string
console::init()
"{star} The cost is 50{cent}" -> s
# The printed output will look like:
#
# ★ The cost is 50¢
#
console::print(s)
# Skip two lines
console::print("{n}{n}")
# The printed output will look like:
#
# This "word" is in quotes
#
console::print("This {quote}word{quote} is in quotes")
end func
end module
You can also escape any character by using its hex value. For example, "{$41}{$42}{$43}" is equivalent to "ABC".
The full list of escape sequences can be found in the HASCII table reference sheet.
Character literals​
Individual Hybrix characters are byte values. Normally the literal value for a byte is a decimal or hexadecimal number, for example:
module main
func start()
var a: byte, b: byte
a <- 97
b <- $0a # $0a is hexadecimal for 10
end func
end module
You can also specify a byte value using a character literal. The syntax is similar to string literals, except:
- It must represent exactly one HASCII character (one byte).
- It uses single quotes (
') instead of double quotes ("). - To specify a single-quote character, escape it as
{apos}instead of{quote}. ('{quote}'and'"'are also valid character literals.)
A character literal produces the HASCII byte value for that character.
For example, the above code can be written equivalently as:
module main
func start()
var a: byte, b: byte
a <- 'a' # The character "a" has HASCII code 97
b <- '{n}' # The newline control character has HASCII code 10
end func
end module