feat: Allow expressions to be terminated/sequenced by ;
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

Note that ultimately a terminated sequence may have
   a slightly different semantics (applying streams
   to `_|_`, most likely) but for now they don't.
This commit is contained in:
Glen Whitney 2021-02-10 12:47:34 -08:00
parent 991976d3a8
commit b9c8532899
10 changed files with 156 additions and 65 deletions

View file

@ -14,8 +14,8 @@ that writes the sum of the ASCII codes for 'H', 'W', and '!' to standard output:
/** md */ test emit_sum [[
stream << 72 + 87 + 33
]]/* **/ parse to TopLevel(Gets(Stream(),
Sum(Sum(Int("72"), Int("87")), Int("33"))))
]] /* **/
parse to TopLevel(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))))
/** writes
192**/
@ -59,7 +59,8 @@ is also left-associative, so that way we can chain insertions into a stream:
/** md */ test emit_twice [[
stream << 72 + 87 + 33 << 291
]]/* **/ parse to TopLevel(
]] /* **/
parse to TopLevel(
Gets(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))), Int("291")))
/** writes
192291**/
@ -76,7 +77,8 @@ with `>>`; both forms return the first argument, so the following writes "824":
/** md */ test enters_twice [[
(7 + 8 >> stream + 9) >> stream
]]/* **/ parse to TopLevel(
]] /* **/
parse to TopLevel(
To(Sum(Sum(Int("7"), To(Int("8"), Stream())), Int("9")), Stream()))
/** writes
824**/
@ -96,7 +98,8 @@ stream <<
7
+ 8 >> stream
+ 9
]]/* **/ parse to TopLevel(
]] /* **/
parse to TopLevel(
Gets(Stream(), Sum(Sum(Int("7"), To(Int("8"), Stream())), Int("9"))))
/** writes
824**/
@ -107,15 +110,17 @@ stream <<
```fostr
**/
/** md */ test enter_receive_bad_break [[
/** md */ test enter_receive_bad_continuation [[
(7 + 8 >> stream + 9)
>> (stream << 9 + 2)
]] /* **/ parse fails
]] /* **/
parse fails
/* Extra tests not in the tour */
test enter_receive [[
(7 + 8 >> stream + 9) >> (stream << 9 + 2)
]]/* **/ parse to TopLevel(
]] /* **/
parse to TopLevel(
To(Sum(Sum(Int("7"),To(Int("8"),Stream())),Int("9")),
Gets(Stream(),Sum(Int("9"),Int("2")))))
/** writes
@ -135,18 +140,20 @@ lines are evaluated in sequence. For example, the program
+ 96
99 + 12 >>
stream
]] /* **/ parse to TopLevel( Sequence(
[ Gets(Stream(), Sum(Int("72"), Int("87")))
, Gets(Stream(), Sum(Int("88"), Int("96")))
, Sum(Int("99"), To(Int("12"), Stream()))]))
]] /* **/
parse to TopLevel(Sequence([
Gets(Stream(), Sum(Int("72"), Int("87"))),
Gets(Stream(), Sum(Int("88"), Int("96"))),
Sum(Int("99"), To(Int("12"), Stream()))
]))
/** writes
15918412**/
/** md
```
will write 15918412. fostr enforces that successive expressions in sequence
must line up at the left, i.e., the following will not parse:
will write 15918412. The fostr parser enforces that successive expressions
in sequence align at the left; e.g., the following fails to parse:
```fostr
**/
@ -156,8 +163,61 @@ must line up at the left, i.e., the following will not parse:
stream << 88
+ 96
99 + 12 >> stream
]] /* **/ parse fails
]] /* **/
parse fails
/** md
```
Note you can optionally terminate an expression in a sequence with a semicolon,
and you may place multiple expressions on a single line if the earlier one(s)
are so terminated. So the following is OK:
```fostr
**/
/** md */ test emit_several [[
stream << 1 + 2; 3 >> stream
(4 + 5) >> stream; stream << 6;
stream << 7
stream << 8
+ (9+10);
11 + 12 >> stream; 13 >> stream
>> stream
]] /* **/
parse to TopLevel(Sequence([
ISequence(Prior([Terminate(Gets(Stream(), Sum(Int("1"), Int("2"))))]),
To(Int("3"), Stream())),
ISequence(Prior([Terminate(To(Sum(Int("4"), Int("5")), Stream()))]),
Terminate(Gets(Stream(), Int("6")))),
Gets(Stream(), Int("7")),
Terminate(Gets(Stream(), Sum(Int("8"), Sum(Int("9"), Int("10"))))),
ISequence(Prior([Terminate(Sum(Int("11"), To(Int("12"), Stream())))]),
To(To(Int("13"), Stream()), Stream()))
]))
/** writes
3396727121313**/
/** md
```
**/
test emit_several_desugar [[
stream << 1 + 2; 3 >> stream
(4 + 5) >> stream; stream << 6;
stream << 7
stream << 8
+ (9+10);
11 + 12 >> stream; 13 >> stream
>> stream
]] /* don't test */
run desugar-fostr to TopLevel(Sequence([
Terminate(Gets(Stream(), Sum(Int("1"), Int("2")))),
To(Int("3"), Stream()),
Terminate(To(Sum(Int("4"), Int("5")), Stream())),
Terminate(Gets(Stream(), Int("6"))),
Gets(Stream(), Int("7")),
Terminate(Gets(Stream(), Sum(Int("8"), Sum(Int("9"), Int("10"))))),
Terminate(Sum(Int("11"), To(Int("12"), Stream()))),
To(To(Int("13"), Stream()), Stream())
]))

7
tests/emit_several.fos Normal file
View file

@ -0,0 +1,7 @@
stream << 1 + 2; 3 >> stream
(4 + 5) >> stream; stream << 6;
stream << 7
stream << 8
+ (9+10);
11 + 12 >> stream; 13 >> stream
>> stream