Quoting constructs: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 11: Line 11:
The following uses VASM syntax for quoting constructs. There is no built-in support for interpolation, escape characters, etc. What constitutes as an escape character depends on the code that is using embedded strings. A function that needs this embedded data can take as an argument a pointer to the data, which can easily be obtained by loading the low byte and high byte of the label into consecutive zero page RAM locations.
The following uses VASM syntax for quoting constructs. There is no built-in support for interpolation, escape characters, etc. What constitutes as an escape character depends on the code that is using embedded strings. A function that needs this embedded data can take as an argument a pointer to the data, which can easily be obtained by loading the low byte and high byte of the label into consecutive zero page RAM locations.


<lang 6502asm>LookUpTable: db $00,$03,$06,$09,$12 ;a sequence of pre-defined bytes
<syntaxhighlight lang="6502asm">LookUpTable: db $00,$03,$06,$09,$12 ;a sequence of pre-defined bytes


MyString: db "Hello World!",0 ;a null-terminated string
MyString: db "Hello World!",0 ;a null-terminated string


GraphicsData: incbin "C:\game\gfx\tilemap.chr" ;a file containing the game's graphics</lang>
GraphicsData: incbin "C:\game\gfx\tilemap.chr" ;a file containing the game's graphics</syntaxhighlight>


Most 6502 assemblers use Motorola syntax, which uses the following conventions:
Most 6502 assemblers use Motorola syntax, which uses the following conventions:
Line 23: Line 23:


6502 Assembly uses <code>db</code> or <code>byte</code> for 8-bit data and <code>dw</code> or <code>word</code> for 16-bit data. 16-bit values are written by the programmer in big-endian, but stored little-endian. For example, the following two data blocks are equivalent. You can write it either way, but the end result is the same.
6502 Assembly uses <code>db</code> or <code>byte</code> for 8-bit data and <code>dw</code> or <code>word</code> for 16-bit data. 16-bit values are written by the programmer in big-endian, but stored little-endian. For example, the following two data blocks are equivalent. You can write it either way, but the end result is the same.
<lang 6502asm>dw $ABCD
<syntaxhighlight lang="6502asm">dw $ABCD
db $CD,$AB</lang>
db $CD,$AB</syntaxhighlight>


Most assemblers support "C-like" operators, and there are a few additional ones:
Most assemblers support "C-like" operators, and there are a few additional ones:
Line 31: Line 31:
These two operators are most frequently used with labeled memory addresses, like so:
These two operators are most frequently used with labeled memory addresses, like so:


<lang 6502asm>lookup_table_lo:
<syntaxhighlight lang="6502asm">lookup_table_lo:
byte <Table00,<Table01,<Table02
byte <Table00,<Table01,<Table02
lookup_table_hi:
lookup_table_hi:
byte >Table00,>Table01,>Table02</lang>
byte >Table00,>Table01,>Table02</syntaxhighlight>




Line 44: Line 44:
* Multiple values can be put on the same line, separated by commas. <code>DC._</code> only needs to be before the first data value on that line. Or, you can put each value on its own line. Both are valid and have the same end result when the code is assembled. You should always follow byte data with EVEN which will add an extra byte of padding if the total number of bytes before it was odd. This is necessary for your code to comply with the CPU's alignment rules.
* Multiple values can be put on the same line, separated by commas. <code>DC._</code> only needs to be before the first data value on that line. Or, you can put each value on its own line. Both are valid and have the same end result when the code is assembled. You should always follow byte data with EVEN which will add an extra byte of padding if the total number of bytes before it was odd. This is necessary for your code to comply with the CPU's alignment rules.


<lang 68000devpac>ByteData:
<syntaxhighlight lang="68000devpac">ByteData:
DC.B $01,$02,$03,$04,$05
DC.B $01,$02,$03,$04,$05
even
even
Line 56: Line 56:
MyString:
MyString:
DC.B "Hello World!",0 ;a null terminator will not be automatically placed.
DC.B "Hello World!",0 ;a null terminator will not be automatically placed.
even</lang>
even</syntaxhighlight>


<code>DS._</code> represents a sequence of space. The number after it specifies how many bytes/words/longs' worth of zeroes to place. Some assemblers support values besides zero, others do not.
<code>DS._</code> represents a sequence of space. The number after it specifies how many bytes/words/longs' worth of zeroes to place. Some assemblers support values besides zero, others do not.
<lang 68000devpac>DS.B 8 ;8 bytes, each equals 0
<syntaxhighlight lang="68000devpac">DS.B 8 ;8 bytes, each equals 0
DS.W 16 ;16 words, each equals zero
DS.W 16 ;16 words, each equals zero
DS.L 20 ;20 longs, each equals zero</lang>
DS.L 20 ;20 longs, each equals zero</syntaxhighlight>


In addition to constants, a label can also be specified. If a label is defined with an <code>EQU</code> statement, the label will be replaced with the assigned value during the assembly process.
In addition to constants, a label can also be specified. If a label is defined with an <code>EQU</code> statement, the label will be replaced with the assigned value during the assembly process.


<lang 68000devpac>ScreenSize equ $1200
<syntaxhighlight lang="68000devpac">ScreenSize equ $1200


MOVE.W (MyData),D0
MOVE.W (MyData),D0


MyData
MyData
DC.W ScreenSize</lang>
DC.W ScreenSize</syntaxhighlight>


Code labels, on the other hand, get replaced with the memory address they point to. This can be used to make a lookup table of various data areas, functions, etc. Since there is no "24-bit" data constant directive, you'll have to use <code>DC.L</code> for code labels. The top byte will always be zero in this case.
Code labels, on the other hand, get replaced with the memory address they point to. This can be used to make a lookup table of various data areas, functions, etc. Since there is no "24-bit" data constant directive, you'll have to use <code>DC.L</code> for code labels. The top byte will always be zero in this case.


<lang 68000devpac>Printstring:
<syntaxhighlight lang="68000devpac">Printstring:
;insert your code here
;insert your code here


FunctionTable:
FunctionTable:
DC.L PrintString ;represents the address of the function "PrintString"</lang>
DC.L PrintString ;represents the address of the function "PrintString"</syntaxhighlight>


Most constants can be derived from compile-time expressions, which are useful for explaining what the data actually means. The expressions are evaluated during the assembly process, and the resulting object code will have these calculations already completed, so your program doesn't have to waste time doing them. Most "C-like" operators are supported, but as always the exact syntax depends on your assembler. Parentheses will aid the assembler in getting these correct, but sometimes it still doesn't do what you expect.
Most constants can be derived from compile-time expressions, which are useful for explaining what the data actually means. The expressions are evaluated during the assembly process, and the resulting object code will have these calculations already completed, so your program doesn't have to waste time doing them. Most "C-like" operators are supported, but as always the exact syntax depends on your assembler. Parentheses will aid the assembler in getting these correct, but sometimes it still doesn't do what you expect.


<lang 68000devpac>DC.B $0200>>3 ;evaluates to $0040. As long as the final result fits within the designated storage size, you're good.
<syntaxhighlight lang="68000devpac">DC.B $0200>>3 ;evaluates to $0040. As long as the final result fits within the designated storage size, you're good.
DC.W 4+5 ;evaluates to $0009
DC.W 4+5 ;evaluates to $0009
DC.W (40*30)-1 ;evaluates to $1199
DC.W (40*30)-1 ;evaluates to $1199
DC.L MyFunction+4 ;evaluates to the address of MyFunction, plus 4.</lang>
DC.L MyFunction+4 ;evaluates to the address of MyFunction, plus 4.</syntaxhighlight>


We can use this technique to get the length of a region of data, which the assembler can calculate for us.
We can use this technique to get the length of a region of data, which the assembler can calculate for us.
<lang 68000devpac>TilemapCollision:
<syntaxhighlight lang="68000devpac">TilemapCollision:
DC.B $11,$11,$11,$11,$11,$11,$11,$11,$11,$11
DC.B $11,$11,$11,$11,$11,$11,$11,$11,$11,$11
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
DC.B $10,$00,$00,$00,$00,$00,$00,$00,$00,$01
Line 108: Line 108:
;gets the length of this region of memory, minus 1, into D0.
;gets the length of this region of memory, minus 1, into D0.
; Again, even though the "operands" of this expression are longs,
; Again, even though the "operands" of this expression are longs,
; their difference fits in 16 bits and that's all that matters.</lang>
; their difference fits in 16 bits and that's all that matters.</syntaxhighlight>




Line 123: Line 123:
=== Quoted constructs within expressions ===
=== Quoted constructs within expressions ===


<lang>? 0 : ? -326.12E-5 : ? HELLO : ? "HELLO" : ? "HELLO</lang>
<syntaxhighlight lang="text">? 0 : ? -326.12E-5 : ? HELLO : ? "HELLO" : ? "HELLO</syntaxhighlight>
The literal HELLO is interpreted as a variable name, and it's value 0 is printed.
The literal HELLO is interpreted as a variable name, and it's value 0 is printed.
{{out}}
{{out}}
Line 134: Line 134:
</pre>
</pre>
=== Quoted constructs within DATA statements ===
=== Quoted constructs within DATA statements ===
<lang> 10 DATA 0,-326.12E-5,HELLO,"HELLO","HELLO
<syntaxhighlight lang="text"> 10 DATA 0,-326.12E-5,HELLO,"HELLO","HELLO
20 READ A%: PRINT A%: READ A: PRINT A: READ A$: PRINT A$: READ A$: PRINT A$: READ A$: PRINT A$
20 READ A%: PRINT A%: READ A: PRINT A: READ A$: PRINT A$: READ A$: PRINT A$: READ A$: PRINT A$
30 DATA AB"C
30 DATA AB"C
40 READ A$: PRINT A$</lang>
40 READ A$: PRINT A$</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 152: Line 152:


* Character
* Character
<lang bqn>'a'
<syntaxhighlight lang="bqn">'a'
'b'
'b'
@</lang>
@</syntaxhighlight>
<code>@</code> is a symbol that represents the null character. Characters can contain a newline(<code>@+10</code> is recommended, however).
<code>@</code> is a symbol that represents the null character. Characters can contain a newline(<code>@+10</code> is recommended, however).


* Number
* Number
<lang bqn>123
<syntaxhighlight lang="bqn">123
1.23
1.23
123E5
123E5
¯1234
¯1234
π</lang>
π</syntaxhighlight>
<code>∞</code>, <code>¯∞</code> and <code>π</code> are constants which represent infinity, negative infinity and pi.
<code>∞</code>, <code>¯∞</code> and <code>π</code> are constants which represent infinity, negative infinity and pi.


Line 175: Line 175:


* Array: consists of any of the above.
* Array: consists of any of the above.
** Regular array notation <lang bqn>⟨1, 2, 3⟩</lang> You can nest arrays in arrays. Separators can be <code>,</code>, <code>⋄</code> and newline.
** Regular array notation <syntaxhighlight lang="bqn">⟨1, 2, 3⟩</syntaxhighlight> You can nest arrays in arrays. Separators can be <code>,</code>, <code>⋄</code> and newline.
** Stranding <lang bqn>1‿2‿3</lang> any expression which doesn't fit in a single atom must be put in parentheses.
** Stranding <syntaxhighlight lang="bqn">1‿2‿3</syntaxhighlight> any expression which doesn't fit in a single atom must be put in parentheses.
** Strings <lang bqn>"Hello World"
** Strings <syntaxhighlight lang="bqn">"Hello World"
"Quoted "" String"</lang> any sequence of characters including newlines can be put inside a string. Quotes are escaped by typing two quotes.
"Quoted "" String"</syntaxhighlight> any sequence of characters including newlines can be put inside a string. Quotes are escaped by typing two quotes.


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Ring}}
{{trans|Ring}}
<lang freebasic>'In FB there is no substr function, then
<syntaxhighlight lang="freebasic">'In FB there is no substr function, then
'Function taken fron the https://www.freebasic.net/forum/index.php
'Function taken fron the https://www.freebasic.net/forum/index.php


Line 212: Line 212:
Print !"quoted text:\n"; substr(text(n),"'",""); !"\n"
Print !"quoted text:\n"; substr(text(n),"'",""); !"\n"
Next n
Next n
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>Same as Ring input.</pre>
<pre>Same as Ring input.</pre>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 294: Line 294:
}
}
fmt.Println(os.Expand("There are ${NUMBER} quoting ${TYPES} in Go.", mapper))
fmt.Println(os.Expand("There are ${NUMBER} quoting ${TYPES} in Go.", mapper))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 319: Line 319:
* A sequence of numbers, for example <tt>1 2 3</tt>
* A sequence of numbers, for example <tt>1 2 3</tt>
* A sequence of characters, for example <tt>'1 2 3'</tt>
* A sequence of characters, for example <tt>'1 2 3'</tt>
* A newline terminated multiline script, for example: <lang J>0 :0
* A newline terminated multiline script, for example: <syntaxhighlight lang="j">0 :0
1 2 3
1 2 3
4 5 6
4 5 6
)</lang>
)</syntaxhighlight>
* (in recent J versions), an embeddable newline terminated multiline script, for example: <lang J>{{)n
* (in recent J versions), an embeddable newline terminated multiline script, for example: <syntaxhighlight lang="j">{{)n
1 2 3
1 2 3
4 5 6
4 5 6
}}</lang>
}}</syntaxhighlight>


Note that a multiline <code>{{)n</code> construct discards the leading newline, but the construct can also be used for single line strings (more concise than <code>'</code> delimited strings when <code>'</code> appears multiple times in the string). For example <code><nowiki>{{)n1 2 3}}</nowiki></code> is the same value as <code>'1 2 3'</code>.
Note that a multiline <code>{{)n</code> construct discards the leading newline, but the construct can also be used for single line strings (more concise than <code>'</code> delimited strings when <code>'</code> appears multiple times in the string). For example <code><nowiki>{{)n1 2 3}}</nowiki></code> is the same value as <code>'1 2 3'</code>.
Line 336: Line 336:
The multiline scripts are special cases of the mechanisms for defining verbs, adverbs and conjunctions (what might be called functions or macros or operators or procedures in other languages) which instead provide the raw characters of the definition. The old form (beginning with <tt>0 : 0</tt> and ending with a line containing a single right parenthesis and no other displayable characters) is different from the new form (beginning with <tt>{{)n</tt> and ending with a line which has <tt>}}</tt> and no other characters preceding it) in the way that any following part of a surrounding sentence is arranged. These values of <tt>A</tt> would be equivalent:
The multiline scripts are special cases of the mechanisms for defining verbs, adverbs and conjunctions (what might be called functions or macros or operators or procedures in other languages) which instead provide the raw characters of the definition. The old form (beginning with <tt>0 : 0</tt> and ending with a line containing a single right parenthesis and no other displayable characters) is different from the new form (beginning with <tt>{{)n</tt> and ending with a line which has <tt>}}</tt> and no other characters preceding it) in the way that any following part of a surrounding sentence is arranged. These values of <tt>A</tt> would be equivalent:


<lang J>NB. no trailing linefeed
<syntaxhighlight lang="j">NB. no trailing linefeed
A=: '1 2 3'
A=: '1 2 3'


Line 347: Line 347:
A=: {{)n
A=: {{)n
1 2 3
1 2 3
}}-.LF</lang>
}}-.LF</syntaxhighlight>


Also, the <nowiki>{{}}</nowiki> forms are nestable. So, for example, this would also define an equivalent value for <tt>A</tt>:
Also, the <nowiki>{{}}</nowiki> forms are nestable. So, for example, this would also define an equivalent value for <tt>A</tt>:


<syntaxhighlight lang="j">{{
<lang J>{{
{{
{{
A=: {{)n
A=: {{)n
Line 357: Line 357:
}}-.LF
}}-.LF
}}''
}}''
}}''</lang>
}}''</syntaxhighlight>


Here, we are defining verbs inline and immediately evaluating them (by providing an argument (which is ignored because it is not referenced)).
Here, we are defining verbs inline and immediately evaluating them (by providing an argument (which is ignored because it is not referenced)).
Line 367: Line 367:
of such values. Such data can be included in a jq program wherever an expression is allowed, but
of such values. Such data can be included in a jq program wherever an expression is allowed, but
in a jq program, consecutive JSON values must be specified using "," as a separator, as shown in this snippet:
in a jq program, consecutive JSON values must be specified using "," as a separator, as shown in this snippet:
<lang jq>def data:
<syntaxhighlight lang="jq">def data:
"A string", 1, {"a":0}, [1,2,[3]]
"A string", 1, {"a":0}, [1,2,[3]]
;
;
</lang>Long JSON strings can be broken up into smaller JSON strings and concatenated
</syntaxhighlight>Long JSON strings can be broken up into smaller JSON strings and concatenated
using the infix "+" operator, e.g. <lang jq>
using the infix "+" operator, e.g. <syntaxhighlight lang="jq">
"This is not such a"
"This is not such a"
+ "long string after all."</lang>
+ "long string after all."</syntaxhighlight>
"Raw data", such as character strings that are not expressed as JSON strings,
"Raw data", such as character strings that are not expressed as JSON strings,
cannot be included in jq programs textually but must be "imported" in some manner, e.g. from
cannot be included in jq programs textually but must be "imported" in some manner, e.g. from
Line 386: Line 386:


* String literals are delimited by double quotes or triple double quotes:
* String literals are delimited by double quotes or triple double quotes:
<lang julia>
<syntaxhighlight lang="julia">
julia> str = "Hello, world.\n"
julia> str = "Hello, world.\n"
"Hello, world.\n"
"Hello, world.\n"
Line 393: Line 393:
a newline"""
a newline"""
"Contains \"quote\" characters and \na newline"
"Contains \"quote\" characters and \na newline"
</syntaxhighlight>
</lang>


* Both single and triple quoted strings are may contain interpolated values. Triple-quoted strings are also dedented to the level of the least-indented line. This is useful for defining strings within code that is indented. For example:
* Both single and triple quoted strings are may contain interpolated values. Triple-quoted strings are also dedented to the level of the least-indented line. This is useful for defining strings within code that is indented. For example:
<lang julia>
<syntaxhighlight lang="julia">
julia> str = """
julia> str = """
Hello,
Hello,
Line 402: Line 402:
"""
"""
" Hello,\n world.\n"
" Hello,\n world.\n"
</syntaxhighlight>
</lang>
* Julia allows interpolation into string literals using $:
* Julia allows interpolation into string literals using $:
<lang julia>
<syntaxhighlight lang="julia">
julia> "$greet, $whom.\n"
julia> "$greet, $whom.\n"
"Hello, world.\n"
"Hello, world.\n"
</syntaxhighlight>
</lang>
* The shortest complete expression after the $ is taken as the expression whose value is to be interpolated into the string. Thus, you can interpolate any expression into a string using parentheses:
* The shortest complete expression after the $ is taken as the expression whose value is to be interpolated into the string. Thus, you can interpolate any expression into a string using parentheses:
<lang julia>
<syntaxhighlight lang="julia">
julia> "1 + 2 = $(1 + 2)"
julia> "1 + 2 = $(1 + 2)"
"1 + 2 = 3"
"1 + 2 = 3"
</syntaxhighlight>
</lang>
* Julia reserves the single quote ' for character literals, not for strings:
* Julia reserves the single quote ' for character literals, not for strings:
<lang julia>
<syntaxhighlight lang="julia">
julia> 'π'
julia> 'π'
'π': Unicode U+03C0 (category Ll: Letter, lowercase)
'π': Unicode U+03C0 (category Ll: Letter, lowercase)
</syntaxhighlight>
</lang>
* Julia requires commands sent to functions such as run() be surrounded by backticks. Such expressions create a Cmd object, which is used for running a child process from Julia:
* Julia requires commands sent to functions such as run() be surrounded by backticks. Such expressions create a Cmd object, which is used for running a child process from Julia:
<lang julia>
<syntaxhighlight lang="julia">
julia> mycommand = `echo hello`
julia> mycommand = `echo hello`
`echo hello`
`echo hello`
Line 428: Line 428:
julia> run(mycommand);
julia> run(mycommand);
hello
hello
</syntaxhighlight>
</lang>
* Julia uses the colon : in metaprogramming for quoting symbols and other code:
* Julia uses the colon : in metaprogramming for quoting symbols and other code:
<lang julia>
<syntaxhighlight lang="julia">
julia> a = :+
julia> a = :+
:+
:+
Line 454: Line 454:
julia> eval(c)
julia> eval(c)
5
5
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
Lua has three string definition syntaxes: single- and double-quotes, which are equivalent; and long-bracket pairs [[ ]] which may span multiple lines. Long-bracket pairs may be specified to an arbitrary depth, which may be useful for quoting Lua source code itself (which might use long-brackets). Lua strings are variable-length arrays of bytes, not 0-terminated (as in C), so may contain aribitrary raw binary data. Commonly escaped characters and octal\hexadecimal notation are supported.
Lua has three string definition syntaxes: single- and double-quotes, which are equivalent; and long-bracket pairs [[ ]] which may span multiple lines. Long-bracket pairs may be specified to an arbitrary depth, which may be useful for quoting Lua source code itself (which might use long-brackets). Lua strings are variable-length arrays of bytes, not 0-terminated (as in C), so may contain aribitrary raw binary data. Commonly escaped characters and octal\hexadecimal notation are supported.
<lang lua>s1 = "This is a double-quoted 'string' with embedded single-quotes."
<syntaxhighlight lang="lua">s1 = "This is a double-quoted 'string' with embedded single-quotes."
s2 = 'This is a single-quoted "string" with embedded double-quotes.'
s2 = 'This is a single-quoted "string" with embedded double-quotes.'
s3 = "this is a double-quoted \"string\" with escaped double-quotes."
s3 = "this is a double-quoted \"string\" with escaped double-quotes."
Line 479: Line 479:
print(s8)
print(s8)
print(s9) -- with audible "bell" from \7 if supported by os
print(s9) -- with audible "bell" from \7 if supported by os
print("some raw binary:", #s9, s9:byte(5), s9:byte(12), s9:byte(17))</lang>
print("some raw binary:", #s9, s9:byte(5), s9:byte(12), s9:byte(17))</syntaxhighlight>
{{out}}
{{out}}
<pre>This is a double-quoted 'string' with embedded single-quotes.
<pre>This is a double-quoted 'string' with embedded single-quotes.
Line 504: Line 504:
Tuples literals are defined as a list of values between parentheses. Field names may be specified by preceding a value by the name followed by a colon.<br/>
Tuples literals are defined as a list of values between parentheses. Field names may be specified by preceding a value by the name followed by a colon.<br/>


<syntaxhighlight lang="nim">
<lang Nim>
echo "A simple string."
echo "A simple string."
echo "A simple string including tabulation special character \\t: \t."
echo "A simple string including tabulation special character \\t: \t."
Line 542: Line 542:
# Tuples.
# Tuples.
echo ('a', 1, true) # Tuple without explicit field names.
echo ('a', 1, true) # Tuple without explicit field names.
echo (x: 1, y: 2) # Tuple with two int fields "x" and "y".</lang>
echo (x: 1, y: 2) # Tuple with two int fields "x" and "y".</syntaxhighlight>


{{out}}
{{out}}
Line 570: Line 570:
Back-ticks and triple-quotes are used for multi-line strings, without backslash interpretation, eg
Back-ticks and triple-quotes are used for multi-line strings, without backslash interpretation, eg


<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">t123</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`
<span style="color: #008080;">constant</span> <span style="color: #000000;">t123</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`
one
one
Line 576: Line 576:
three
three
`</span>
`</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


or (entirely equivalent, except the following can contain back-ticks which the above cannot, and vice versa for triple quotes)
or (entirely equivalent, except the following can contain back-ticks which the above cannot, and vice versa for triple quotes)


<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">t123</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
<span style="color: #008080;">constant</span> <span style="color: #000000;">t123</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
one
one
Line 586: Line 586:
three
three
"""</span>
"""</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


Both are also equivalent to the top double-quote one-liner. Note that a single leading '\n' is automatically stripped.<br>
Both are also equivalent to the top double-quote one-liner. Note that a single leading '\n' is automatically stripped.<br>
Line 593: Line 593:
You can also declare hexadecimal strings, eg
You can also declare hexadecimal strings, eg


<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #000000;">x</span><span style="color: #008000;">"1 2 34 5678_AbC"</span> <span style="color: #000080;font-style:italic;">-- same as {0x01, 0x02, 0x34, 0x56, 0x78, 0xAB, 0x0C}
<span style="color: #000000;">x</span><span style="color: #008000;">"1 2 34 5678_AbC"</span> <span style="color: #000080;font-style:italic;">-- same as {0x01, 0x02, 0x34, 0x56, 0x78, 0xAB, 0x0C}
-- note however it displays as {1,2,52,86,120,171,12}
-- note however it displays as {1,2,52,86,120,171,12}
-- whereas x"414243" displays as "ABC" (as all chars)</span>
-- whereas x"414243" displays as "ABC" (as all chars)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


Literal [http://phix.x10.mx/docs/html/sequences.htm sequences] are represented with curly braces, and can be nested to any depth, eg
Literal [http://phix.x10.mx/docs/html/sequences.htm sequences] are represented with curly braces, and can be nested to any depth, eg
<lang Phix>{2, 3, 5, 7, 11, 13, 17, 19}
<syntaxhighlight lang="phix">{2, 3, 5, 7, 11, 13, 17, 19}
{1, 2, {3, 3, 3}, 4, {5, {6}}}
{1, 2, {3, 3, 3}, 4, {5, {6}}}
{{"John", "Smith"}, 52389, 97.25}
{{"John", "Smith"}, 52389, 97.25}
{} -- the 0-element sequence</lang>
{} -- the 0-element sequence</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 691: Line 691:


The different types (or styles) of incorporating quoted constructs are a largely matter of style.
The different types (or styles) of incorporating quoted constructs are a largely matter of style.
<lang rexx>/*REXX program demonstrates various ways to express a string of characters or numbers.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates various ways to express a string of characters or numbers.*/
a= 'This is one method of including a '' (an apostrophe) within a string.'
a= 'This is one method of including a '' (an apostrophe) within a string.'
b= "This is one method of including a ' (an apostrophe) within a string."
b= "This is one method of including a ' (an apostrophe) within a string."
Line 751: Line 751:
/*the variable L (with an */
/*the variable L (with an */
/*intervening blank between each */
/*intervening blank between each */
/*variable's value. */</lang><br><br>
/*variable's value. */</syntaxhighlight><br><br>


=={{header|Ring}}==
=={{header|Ring}}==
{{incomplete|Ring|<u>Explain</u> where they would likely be used, what their primary use is, what limitations they have and why one might be preferred over another. Is one style interpolating and another not? Are there restrictions on the size of the quoted data? The type? The format?}}
{{incomplete|Ring|<u>Explain</u> where they would likely be used, what their primary use is, what limitations they have and why one might be preferred over another. Is one style interpolating and another not? Are there restrictions on the size of the quoted data? The type? The format?}}
<syntaxhighlight lang="ring">
<lang Ring>
text = list(3)
text = list(3)


Line 767: Line 767:
see "quoted text:" + nl + str + nl + nl
see "quoted text:" + nl + str + nl + nl
next
next
</syntaxhighlight>
</lang>
{{out}}
{{out}}
text for quoting: <br>
text for quoting: <br>
Line 787: Line 787:
====Characters====
====Characters====
Character literals are prefixed by a "$". Conceptionally, they are the elements of strings, although effectively only the codePoint is stored in strings. But when accessing a string element, instances of Character are used. Characters can be asked for being uppercase, lowercase, etc.
Character literals are prefixed by a "$". Conceptionally, they are the elements of strings, although effectively only the codePoint is stored in strings. But when accessing a string element, instances of Character are used. Characters can be asked for being uppercase, lowercase, etc.
<lang smalltalk>$a
<syntaxhighlight lang="smalltalk">$a
$日</lang>
$日</syntaxhighlight>


====Strings====
====Strings====
String literals are enclosed in single quotes. Conceptionally, they holde instances of Character as element, but actually the underlying storage representation is chosen to be space effective. Typically, underneath are classes like SingleByteString, TwoByteString and FourByteString, but this is transparent to the programmer. Strings can hold any Unicode character; UTF8 is only used when strings are exchanged with the external world (which is good, as it makes operations like stringLength much easier).
String literals are enclosed in single quotes. Conceptionally, they holde instances of Character as element, but actually the underlying storage representation is chosen to be space effective. Typically, underneath are classes like SingleByteString, TwoByteString and FourByteString, but this is transparent to the programmer. Strings can hold any Unicode character; UTF8 is only used when strings are exchanged with the external world (which is good, as it makes operations like stringLength much easier).
<lang smalltalk>'hello'
<syntaxhighlight lang="smalltalk">'hello'
'日本語 </lang>
'日本語 </syntaxhighlight>


Traditional Smalltalk-80 does not support any escapes inside strings, which is inconvenient, occasionally.<br>Smalltalk/X supports an extended syntax for C-like strings:
Traditional Smalltalk-80 does not support any escapes inside strings, which is inconvenient, occasionally.<br>Smalltalk/X supports an extended syntax for C-like strings:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang smalltalk>c'hello\nthis\tis a C string\0x0D'</lang>
<syntaxhighlight lang="smalltalk">c'hello\nthis\tis a C string\0x0D'</syntaxhighlight>
and also embedded expressions:
and also embedded expressions:
<lang smalltalk>e'hello world; it is now {Time now}\n'</lang>
<syntaxhighlight lang="smalltalk">e'hello world; it is now {Time now}\n'</syntaxhighlight>


====Arrays====
====Arrays====
Literal arrays are written as #(...), where the elements are space separated; each literal array element can be any type of literal again, optionally omitting the '#'-character:
Literal arrays are written as #(...), where the elements are space separated; each literal array element can be any type of literal again, optionally omitting the '#'-character:
<lang smalltalk>#( 1 1.234 (1/2) foo 'hello' #(9 8 7) (99 88 77) $λ '日本語' true [1 2 3] false)</lang>
<syntaxhighlight lang="smalltalk">#( 1 1.234 (1/2) foo 'hello' #(9 8 7) (99 88 77) $λ '日本語' true [1 2 3] false)</syntaxhighlight>
Here, the third element is a fraction, followed by the symbol #'foo', two arrays, a character, another string, the boolean true, a byteArray and the boolean false.
Here, the third element is a fraction, followed by the symbol #'foo', two arrays, a character, another string, the boolean true, a byteArray and the boolean false.


====ByteArrays====
====ByteArrays====
A dense collection of byte valued integers is written as #[..]. Conceptionally, they are arrays of integer values in the range 0..255, but use only one byte per element of storage. They are typically used for bulk storage such as bitmap images, or when exchanging such with external functions.
A dense collection of byte valued integers is written as #[..]. Conceptionally, they are arrays of integer values in the range 0..255, but use only one byte per element of storage. They are typically used for bulk storage such as bitmap images, or when exchanging such with external functions.
<lang smalltalk>#[ 1 2 16rFF 2r0101010 ]</lang>
<syntaxhighlight lang="smalltalk">#[ 1 2 16rFF 2r0101010 ]</syntaxhighlight>


====Symbols====
====Symbols====
These are like symbol atoms in Lisp/Scheme, written as #'...' (i.e. like a string with hash prefix).
These are like symbol atoms in Lisp/Scheme, written as #'...' (i.e. like a string with hash prefix).
If the characters do not contain special characters or are of the form allowed for a message selector, the quotes can be omitted. Symbols are often used as key in dictionaries, especially for message selectors and global/namespace name bindings. They can be quickly compared using "==", which is. a pointer compare (identity) instead of "=" which is compares the contents (equality).
If the characters do not contain special characters or are of the form allowed for a message selector, the quotes can be omitted. Symbols are often used as key in dictionaries, especially for message selectors and global/namespace name bindings. They can be quickly compared using "==", which is. a pointer compare (identity) instead of "=" which is compares the contents (equality).
<lang smalltalk>#'foo'
<syntaxhighlight lang="smalltalk">#'foo'
#'foo bar baz'
#'foo bar baz'
#foo. " same as #'foo' "
#foo. " same as #'foo' "
#'++'
#'++'
#++ " same as #'++' "
#++ " same as #'++' "
#a:b:c: " same as #'a:b:c:' "</lang>
#a:b:c: " same as #'a:b:c:' "</syntaxhighlight>


====Blocks====
====Blocks====
Line 825: Line 825:
Blocks thus represent a piece of code which can be stored in an instance variable, passed as argument or returned from a method.
Blocks thus represent a piece of code which can be stored in an instance variable, passed as argument or returned from a method.
<br>Block syntax is very compact:
<br>Block syntax is very compact:
<lang smalltalk>[ expression . expression ... expression ]</lang>
<syntaxhighlight lang="smalltalk">[ expression . expression ... expression ]</syntaxhighlight>
or for a block with arguments:
or for a block with arguments:
<lang smalltalk>[:arg1 :arg2 :... :argN | expression . expression ... expression ]</lang>
<syntaxhighlight lang="smalltalk">[:arg1 :arg2 :... :argN | expression . expression ... expression ]</syntaxhighlight>
Blocks are one of the fundamental building blocks of Smalltalk (no pun here), as the language (Compiler) does not specify any syntax for control structures. Control structures like if, while, etc. are all implemented as library functions, and defined eg. in the Boolean, Block or Collection classes.
Blocks are one of the fundamental building blocks of Smalltalk (no pun here), as the language (Compiler) does not specify any syntax for control structures. Control structures like if, while, etc. are all implemented as library functions, and defined eg. in the Boolean, Block or Collection classes.
<br>
<br>
If you have a block at hand, it can be evaluated by sending it a "value"message:
If you have a block at hand, it can be evaluated by sending it a "value"message:
<lang smalltalk>aBlock value. "evaluate the block, passing no argument"
<syntaxhighlight lang="smalltalk">aBlock value. "evaluate the block, passing no argument"
anotherBlock value:1 value:2. "evaluate the block, passing two arguments"</lang>
anotherBlock value:1 value:2. "evaluate the block, passing two arguments"</syntaxhighlight>
The most basic implementation of such a control structure is found in the Boolean subclasses True and False, which implement eg. "<tt>ifTrue:arg</tt>" and "<tt>ifFalse:</tt>". Here are those two as concrete example:
The most basic implementation of such a control structure is found in the Boolean subclasses True and False, which implement eg. "<tt>ifTrue:arg</tt>" and "<tt>ifFalse:</tt>". Here are those two as concrete example:
<lang smalltalk>in the True class:
<syntaxhighlight lang="smalltalk">in the True class:
ifTrue: aBlock
ifTrue: aBlock
^ aBlock value "I am true, so I evaluate the block"
^ aBlock value "I am true, so I evaluate the block"
Line 840: Line 840:
in the False class:
in the False class:
ifTrue: aBlock
ifTrue: aBlock
^ nil "I am false, so I ignore the block"</lang>
^ nil "I am false, so I ignore the block"</syntaxhighlight>
Thus, the expression <tt>"someBoolean ifTrue:[ 'hello print' ]"</tt> will either evaluate the lambda or not, depending on the someBoolean receiver.
Thus, the expression <tt>"someBoolean ifTrue:[ 'hello print' ]"</tt> will either evaluate the lambda or not, depending on the someBoolean receiver.
Obviously, you can teach other objects on how to respond to "value" messages and then use them as if they where blocks.
Obviously, you can teach other objects on how to respond to "value" messages and then use them as if they where blocks.
Line 851: Line 851:
====Inline Object====
====Inline Object====
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang smalltalk>#{
<syntaxhighlight lang="smalltalk">#{
foo: <someConstant>
foo: <someConstant>
bar: <someConstant>
bar: <someConstant>
}</lang>
}</syntaxhighlight>
Generates a literal constant instance of an anonymous class, with two instance vars: foo and bar.
Generates a literal constant instance of an anonymous class, with two instance vars: foo and bar.
The object is dumb in that it only provides getter and setter functions. These are used eg. when returning structured multiple values from a method.
The object is dumb in that it only provides getter and setter functions. These are used eg. when returning structured multiple values from a method.
Line 862: Line 862:
Similar to byteArrays, there are dense arrays of ints, floats, doubles or bits (i.e. they use much less memory compared to regular arrays, which hold pointers to their elements). They are also perfect when calling out to C-language functions. The syntax is analogous to the Scheme language's syntax:
Similar to byteArrays, there are dense arrays of ints, floats, doubles or bits (i.e. they use much less memory compared to regular arrays, which hold pointers to their elements). They are also perfect when calling out to C-language functions. The syntax is analogous to the Scheme language's syntax:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang>#u16( 1 2 3 ). " an array of unsigned int16s "
<syntaxhighlight lang="text">#u16( 1 2 3 ). " an array of unsigned int16s "
#u32( 1 2 3 ). " an array of unsigned int32s "
#u32( 1 2 3 ). " an array of unsigned int32s "
#u64( 1 2 3 ). " an array of unsigned int64s "
#u64( 1 2 3 ). " an array of unsigned int64s "
Line 872: Line 872:
#f64( -1 2.0 3 ). " an array of float64s "
#f64( -1 2.0 3 ). " an array of float64s "
#b( 1 0 1 1 0 0 ). " an array of bits "
#b( 1 0 1 1 0 0 ). " an array of bits "
#B( true false true true ). " an array of booleans "</lang>
#B( true false true true ). " an array of booleans "</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
Line 893: Line 893:


Here are some examples of all this.
Here are some examples of all this.
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


// simple string literal
// simple string literal
Line 922: Line 922:
"""
"""
System.print(r)
System.print(r)
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 944: Line 944:
the null terminator is the only way the CPU knows it will end (assuming that your "putS" routine uses a null terminator.)
the null terminator is the only way the CPU knows it will end (assuming that your "putS" routine uses a null terminator.)


<lang z80>MyString:
<syntaxhighlight lang="z80">MyString:
byte "Hello World",0 ;a null-terminated string
byte "Hello World",0 ;a null-terminated string
LookupTable:
LookupTable:
byte &03,&06,&09,&0C ;a pre-defined sequence of bytes (similar in concept to enum in C)
byte &03,&06,&09,&0C ;a pre-defined sequence of bytes (similar in concept to enum in C)
TileGfx:
TileGfx:
incbin "Z:\game\gfx\tilemap.bmp" ;a file containing bitmap graphics data</lang>
incbin "Z:\game\gfx\tilemap.bmp" ;a file containing bitmap graphics data</syntaxhighlight>


For most Z80 assemblers, the following are standard:
For most Z80 assemblers, the following are standard:
Line 956: Line 956:


Z80 Assembly uses db or byte for 8-bit data and dw or word for 16-bit data. 16-bit values are written by the programmer in big-endian, but stored little-endian. For example, the following two data blocks are equivalent. You can write it either way, but the end result is the same.
Z80 Assembly uses db or byte for 8-bit data and dw or word for 16-bit data. 16-bit values are written by the programmer in big-endian, but stored little-endian. For example, the following two data blocks are equivalent. You can write it either way, but the end result is the same.
<lang z80>word $ABCD
<syntaxhighlight lang="z80">word $ABCD
word $CD,$AB</lang>
word $CD,$AB</syntaxhighlight>


Most assemblers support "C-like" operators, and there are a few additional ones:
Most assemblers support "C-like" operators, and there are a few additional ones:
Line 964: Line 964:
* > or HIGH() means "The high byte of." For example, <$78AB evaluates to 78.
* > or HIGH() means "The high byte of." For example, <$78AB evaluates to 78.
These two operators are most frequently used with labeled memory addresses, like so:
These two operators are most frequently used with labeled memory addresses, like so:
<lang z80>lookup_table_lo:
<syntaxhighlight lang="z80">lookup_table_lo:
byte <Table00,<Table01,<Table02
byte <Table00,<Table01,<Table02
lookup_table_hi:
lookup_table_hi:
byte >Table00,>Table01,>Table02</lang>
byte >Table00,>Table01,>Table02</syntaxhighlight>


The <code>incbin</code> directive can be used for embedding raw graphics data, text, or music.
The <code>incbin</code> directive can be used for embedding raw graphics data, text, or music.
Line 989: Line 989:
A #<<<" beginning tag prepends a " to the block.
A #<<<" beginning tag prepends a " to the block.
For example:
For example:
<lang zkl>#<<<
<syntaxhighlight lang="zkl">#<<<
text:=
text:=
"
"
A
A
";
";
#<<<</lang>
#<<<</syntaxhighlight>


is parsed as text:="\nA\n";
is parsed as text:="\nA\n";