polserver/testsuite/escript/interstring/interstring01.src
Kevin Eady e41f70d72e
Add support for interpolated strings ("interstrings") (#369)
* Update grammar to support interpolated strings

* Struct initializers, case switchs are regular strings

* Initial stub for interpolated string in AST
This splits the previous STRING_LITERAL terminal into a production,
stringLiteral, to handle REGULAR_STRING terminal and interpolatedString
production.

* Change interpolated strings to expressions

* Fix mode handling in grammar

* Grammar fixes
- Fix mode handling in lexer
- Interpolated strings only have one expression

* Can successfully parse, stub compile interstrings
$"He {there + print("hello")}";

* Add formatting string to grammar

* Fix tests, and add test stubs

* Rename curleys to brace; better AST generation

* initial work interstring instruction

* Initial work on formatted string instruction: emit

* Formatted string instruction: execution

* Add simple formatted string test

* Add formatted string test src

* Review changes #1
- Rename `FormattedString` to `FormatExpression`
- Rename overloaded `try_to_format()` to `get_formatted()`

* Review changes part 2
- Rename `InterpolatedString` ast node to `InterpolateString`

* Review changes part 3
- Modify `ins_interpolate_string`

* Review changes part 4
- Remove unused formal parameter

* Add negative tests

* Spruce up positive tests

* Fix braces, escape chars inside interstring

* Fix, add tests for escaped char and double brace

* Address differences for new lines in tests

* Update Escript version

* Make empty expression an error

* Add documentation for interpolated strings

* Quick touchup on docs
2021-03-11 22:48:16 +01:00

65 lines
1 KiB
Text

var t, id1, id2, uninit;
var n := 0;
function testPrint(num)
n += 1;
print(CStr(n) + ".");
print(TypeOf(t));
print(t);
endfunction
t := $""; // zero parts
testPrint(t);
t := $"foo"; // one part, string
testPrint(t);
t := $"{1}"; // one part, expression -> integer literal
testPrint(t);
id1 := 0xbad.beefp4;
t := $"{id1}"; // one part, expression -> identifier -> hex float literal
testPrint(t);
// #5
t := $"foo{""}"; // two parts
testPrint(t);
t := $"{1}bar"; // one part, expression -> integer literal
testPrint(t);
id1 := "foo";
id2 := "bar";
t := $"{id1}{id2}"; // two parts
testPrint(t);
t := $"{uninit}";
testPrint(t);
t := $"!{uninit}";
testPrint(t);
// #10
t := $"{uninit}!";
testPrint(t);
t := $"!{uninit}!";
testPrint(t);
t := $"{CAsc("a")}"; // expression -> function call, that returns a Long
testPrint(t);
t := $"{48879:#x}";
testPrint(t);
t := $"Hello {"string":#x}";
testPrint(t);
t := $"Hello, {@testPrint}";
testPrint(t);
t := $"Hello{$"\x61\x62\x63"}World";
testPrint(t);
t := $"Hello, {{{"_".join(array{1,2,3})}}}";
testPrint(t);