Quoting constructs: Difference between revisions

PascalABC.NET
(added Perl programming solution)
(PascalABC.NET)
 
(14 intermediate revisions by 6 users not shown)
Line 147:
AB"C
</pre>
=={{header|Binary Lambda Calculus}}==
The ability to embed arbitrary binary data of any length with zero overhead is one of the defining features of BLC, in which a closed lambda term is parsed from the start of the programs, and then applied to the rest of the program, making the latter the quoted part. Even the simple hello world program, which in BLC is <code> Hello, world!</code> follows this pattern, with the initial space encoding the lambda term <code>\x.x</code> for the identity function.
 
The restriction is that only one string of data can be so embedded. If we need to embed more pieces, then we can concatenate self-delimiting descriptions, which incur some logarithmic overhead, e.g. by use of the Levenshtein encoding.
 
=={{header|BQN}}==
 
Line 179 ⟶ 184:
** Strings <syntaxhighlight lang="bqn">"Hello World"
"Quoted "" String"</syntaxhighlight> any sequence of characters including newlines can be put inside a string. Quotes are escaped by typing two quotes.
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <iostream>
#include <string>
#include <vector>
 
int main() {
// C++ uses double quotes for strings and single quotes for characters.
std::string simple_string = "This is a simple string";
char letter = 'A';
std::cout << simple_string << " " << letter << std::endl;
 
// C++ can implement multiline strings.
std::string multiline_string = R"(
An example of multi-line string.
Text formatting is preserved.
This is a raw string literal, introduced in C++ 11.)";
std::cout << multiline_string << std::endl;
 
// C++'s primitive data types: bool, char, double, float, int, long, short,
// can be used to to store data, for example,
const int block_length = 64;
std::cout << "block length = " << block_length << std::endl;
 
// Vectors of these data types are also possible, for example,
std::vector<double> state = { 1.0, 2.0, 3.0 };
}
</syntaxhighlight>
{{ out }}
<pre>
This is a simple string A
 
An example of multi-line string.
Text formatting is preserved.
This is a raw string literal, introduced in C++ 11.
block length = 64
</pre>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module test {
@Inject Console console;
void run() {
// characters are single quoted
Char ch = 'x';
console.print( $"ch={ch.quoted()}");
 
// strings are double quoted
String greeting = "Hello";
console.print( $"greeting={greeting.quoted()}");
 
// multi-line strings use '|' as a left column
// the start of the first line escapes the '|' to indicate the start of the multiline
// a trailing escape indicates that the current line continues without a linefeed
String lines = \|first line
|second line\
| continued
;
console.print($|lines=
|{lines}
);
 
// the $"..." is a template string, containing {expressions}
// the multi-line form of the template string uses $|
String name = "Bob";
String msg = $|{greeting} {name},
|Have a nice day!
|{ch}{ch}{ch}
;
console.print($|msg=
|{msg}
);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
ch='x'
greeting="Hello"
lines=
first line
second line continued
msg=
Hello Bob,
Have a nice day!
xxx
</pre>
 
=={{header|FreeBASIC}}==
Line 362 ⟶ 456:
 
The use of an unbalanced right parenthesis as an escape character was inherited from APL. The double curly brace mechanism was a compromise between J's existing use of curly braces and visual conventions used in a variety of other languages.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
 
public final class QuotingConstructs {
 
public static void main(String[] args) {
// Java uses double quotes for strings and single quotes for characters.
String simple = "This is a simple string";
char letter = 'A';
// A Text Block is denoted by triple quotes.
String multiLineString = """
This is an example of multi-line string.
Text formatting is preserved.
Text blocks can be used for a multi-line string.
""";
System.out.println(multiLineString);
// Java's primitive data types: boolean, byte, char, double, float, int, long, short,
// can be used to to store data, for example,
final int blockLength = 64;
// Arrays or lists of these data types are possible, for example,
double[] state = new double[] { 1.0, 2.0, 3.0 };
// Custom data types can be stored in a record or a class, for example,
record Circle(int centreX, int centreY, double radius) {}
// A list of custom data types:
List<Circle> circles = List.of( new Circle(1, 2, 1.25), new Circle(-2, 3, 2.50) );
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
This is an example of multi-line string.
Text formatting is preserved.
Text blocks can be used for a multi-line string.
</pre>
 
=={{header|jq}}==
Line 372 ⟶ 508:
</syntaxhighlight>Long JSON strings can be broken up into smaller JSON strings and concatenated
using the infix "+" operator, e.g. <syntaxhighlight lang="jq">
"This is not such a "
+ "long string after all."</syntaxhighlight>
"Raw data", such as character strings that are not expressed as JSON strings,
Line 561 ⟶ 697:
('a', 1, true)
(x: 1, y: 2)</pre>
 
=={{header|PascalABC.NET}}==
Strings in PascalABC.NET are single-quoted:
<syntaxhighlight lang="delphi">
var s := 'Hello';
</syntaxhighlight>
 
Using quotes inside of a literal string:
<syntaxhighlight lang="delphi">
var s := 'd''Artagnan'; // d'Artagnan
</syntaxhighlight>
 
Using interpolated strings:
<syntaxhighlight lang="delphi">
var (a,b) := (3,5);
var s := $'{a} + {b} = {a + b}';
</syntaxhighlight>
 
Using multiline strings:
<syntaxhighlight lang="delphi">
var s := '''
This is a
multiline
string
''';
</syntaxhighlight>
 
 
=={{header|Perl}}==
Line 653 ⟶ 816:
{{"John", "Smith"}, 52389, 97.25}
{} -- the 0-element sequence</syntaxhighlight>
 
=={{header|Quackery}}==
 
In Quackery, everything is code except when it is data. The objects it supports are operators ("primitives" or virtual op-codes), numbers (bigints) and nests (dynamic arrays that can contain operators, numbers, and nests, delimits by <code>[</code> and <code>]</code>). The behaviour of a number is to put its value on the data stack - in essence, numbers are self-quoting. However, in common with operators and nests, they can be explicitly quoted by preceding them with <code>'</code> (pronounced "quote"). The behaviour of <code>'</code> is to put the quackery object that follows it on the data stack rather than evaluate it, which is the default behaviour for all objects.
 
Any object can be given a name during compilation, using the word <code>is</code>, which is a "builder"; a word that is executed during compilation. The behaviour of <code>is</code> is to add the named object to the compiler's dictionary of named objects. So, for example, the number <code>12</code> could be declared as a constant called <code>dozen</code> with the Quackscript <code>12 is dozen</code>. However is it preferable to restrict the use of <code>is</code> to naming nests, (i.e. <code>[ 12 ] is dozen</code>) so that the Quackery decompiler <code>unbuild</code> can differentiate between the constant <code>dozen</code> and the literal <code>12</code>.
 
(Aside: The word <code>builds</code> is analogous to <code>is</code>, but creates new builders. The Quackery compiler is extensible, so new quoting constructs can be created as required.)
 
Other relevant builders include <code>hex</code>, which parses the whitespace delimited string following it as a hexadecimal number, <code>char</code>, which will embed a non-whitespace character (represented by a number), and <code>$</code>, which will embed a string (represented by a nest of characters).
 
The behaviour of <code>$</code> is to consider the first non-whitespace character following it as the string's delimiter, and treat everything following it as the string to be embedded until it encounters a second instance of the delimiter. Typically the delimiter for a string is <code>"</code>, but any non-whitespace character is acceptable, so the following are equivalent: <code>$ "Hello World!"</code>, <code>$ 'Hello World!'</code>, <code>$ ZHello World!Z</code>.
 
The builder <code>constant</code> evaluates the object that precedes it at runtime and embeds it as a literal during compilation, so <code>[ 10 18 ** ] constant</code> compiles as <code>1000000000000000000</code>.
 
Also relevant are look-up tables and ancillary stacks.
 
The word <code>table</code> is used to create look-up tables. As an example, the behaviour of the nest <code>[ table 2 3 5 7 11 13 17 19 23 29 ]</code> is to take a number, n, in the range <code>0</code> to <code>9</code> (inclusive) from the data stack and put the corresponding small prime number on the data stack. <code>table</code> is not limited to returning numbers, it will handle any Quackery object, so can be considered as a quoting construct.
 
The word <code>stack</code> is used to create ancillary stacks, which, like the data stack, are places to hold data. They are the Quackery analogue to variables, but can be pre-loaded with data much like tables. As an example, this stack is preloaded with two items; there is an empty nest <code>[ ]</code> on the top of the stack, and beneath that, the number <code>123</code>: <code>[ stack 123 [ ] ]</code>.
 
=={{header|Raku}}==
Line 942 ⟶ 1,125:
 
Here are some examples of all this.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
// simple string literal
256

edits