Literals/String: Difference between revisions

Add Factor example
(Add Factor example)
Line 526:
"\"The quick brown fox jumps over the lazy dog.\"".
</lang>
 
=={{header|Factor}}==
A basic character:
<lang factor>CHAR: a</lang>
Characters are Unicode code points (integers in the range <tt>[0-2,097,152]</tt>).
 
<code>CHAR:</code> is a parsing word that takes a literal character, escape code, or Unicode code point name and adds a Unicode code point to the parse tree.
<lang factor>CHAR: x ! 120
CHAR: \u000032 ! 50
CHAR: \u{exclamation-mark} ! 33
CHAR: exclamation-mark ! 33
CHAR: ugaritic-letter-samka ! 66450</lang>
 
Strings are represented as fixed-size mutable sequences of Unicode code points.
 
A basic string:
<lang factor>"Hello, world!"</lang>
 
We can take a look under the hood:
<lang factor>"Hello, world!" { } like ! { 72 101 108 108 111 44 32 119 111 114 108 100 33 }</lang>
 
Both <code>CHAR:</code> and strings support the following escape codes:
 
{| class="wikitable"
|-
! Escape code
! Meaning
|-
| <tt>\\</tt>
| <tt>\</tt>
|-
| <tt>\s</tt>
| a space
|-
| <tt>\t</tt>
| a tab
|-
| <tt>\n</tt>
| a newline
|-
| <tt>\r</tt>
| a carriage return
|-
| <tt>\b</tt>
| a backspace (ASCII 8)
|-
| <tt>\v</tt>
| a vertical tab (ASCII 11)
|-
| <tt>\f</tt>
| a form feed (ASCII 12)
|-
| <tt>\0</tt>
| a null byte (ASCII 0)
|-
| <tt>\e</tt>
| escape (ASCII 27)
|-
| <tt>\"</tt>
| <tt>"</tt>
|-
| <tt>\xxx</tt>
| The Unicode code point with hexadecimal number <tt>xxx</tt>
|-
| <tt>\uxxxxxx</tt>
| The Unicode code point with hexadecimal number <tt>xxxxxx</tt>
|-
| <tt>\u{name}</tt>
| The Unicode code point named <tt>name</tt>
|}
 
Some examples of strings with escape codes:
<lang factor>"Line one\nLine two" print</lang>
{{out}}
<pre>
Line one
Line two
</pre>
Putting quotation marks into a string:
<lang factor>"\"Hello,\" she said." print</lang>
{{out}}
<pre>
"Hello," she said.
</pre>
Strings can span multiple lines. Newlines are inserted where they occur in the literal.
<lang factor>"2\u{superscript-two} = 4
2\u{superscript-three} = 8
2\u{superscript-four} = 16" print</lang>
{{out}}
<pre>
2² = 4
2³ = 8
2⁴ = 16
</pre>
The <code>multiline</code> vocabulary provides support for verbatim strings and here-strings.
 
A verbatim string:
<lang factor>USE: multiline
[[ escape codes \t are literal \\ in here
but newlines \u{plus-minus-sign} are still
inserted " for each line the string \" spans.]] print</lang>
{{out}}
<pre>
escape codes \t are literal \\ in here
but newlines \u{plus-minus-sign} are still
inserted " for each line the string \" spans.
</pre>
Note that the space after <code>[[</code> is necessary for the Factor parser to recognize it as a word. <code>"</code> is one of very few special cases where this is not necessary. The space will not be counted as part of the string.
 
A here-string:
<lang factor>USE: multiline
HEREDOC: END
Everything between the line above
and the final line (a user-defined token)
is parsed into a string where whitespace
is significant.
END
print</lang>
{{out}}
<pre>
Everything between the line above
and the final line (a user-defined token)
is parsed into a string where whitespace
is significant.
 
</pre>
<code>STRING:</code> is similar to <code>HEREDOC:</code> except instead of immediately placing the string on the data stack, it defines a word that places the string on the data stack when called.
<lang factor>USE: multiline
STRING: random-stuff
ABC
123
"x y z
;
random-stuff print</lang>
{{out}}
<pre>
ABC
123
"x y z
</pre>
 
Finally, the <code>interpolate</code> vocabulary provides support for interpolating lexical variables, dynamic variables, and data stack values into strings.
<lang factor>USING: interpolate locals namespaces ;
 
"Sally" "name" set
"bicycle"
"home"
 
[let
 
"crying" :> a
 
[I ${name} crashed her ${1}. Her ${1} broke.
${name} ran ${} ${a}.
I]
 
]</lang>
{{out}}
<pre>
Sally crashed her bicycle. Her bicycle broke.
Sally ran home crying.
</pre>
<code>${}</code> consumes values from the stack. With a number <tt>n</tt> inside, you can reference (and re-reference!) the data stack value <tt>n</tt> places from the top of the data stack.
 
=={{header|Forth}}==
1,827

edits