Merge pull request 'feat: add <<< and >>> abbreviating operations to default stream' (#21) from default_gets into main
All checks were successful
continuous-integration/drone/push Build is passing

Resolves #16.

Reviewed-on: #21
This commit is contained in:
Glen Whitney 2021-02-18 21:48:23 +00:00
commit bfe3f86116
8 changed files with 37 additions and 12 deletions

View File

@ -42,14 +42,18 @@ context-free syntax
Ex.Stream = <stream> Ex.Stream = <stream>
Ex.Sum = <<Ex> + <Ex>> {left} Ex.Sum = <<Ex> + <Ex>> {left}
Ex.Gets = [[Ex] << [Ex]] {left} Ex.Gets = [[Ex] << [Ex]] {left}
Ex.DefGets = [<<< [Ex]]
Ex.To = [[Ex] >> [Ex]] {left} Ex.To = [[Ex] >> [Ex]] {left}
Ex.DefTo = [[Ex] >>>]
Ex = <(<Ex>)> {bracket} Ex = <(<Ex>)> {bracket}
context-free priorities context-free priorities
Ex.To Ex.To
> Ex.DefTo
> Ex.Sum > Ex.Sum
> Ex.DefGets
> Ex.Gets, > Ex.Gets,
// prevent cycle: no singletons // prevent cycle: no singletons

View File

@ -231,3 +231,15 @@ run desugar-fostr to TopLevel(Sequence([
Terminate(Sum(Int("11"), To(Int("12"), Stream()))), Terminate(Sum(Int("11"), To(Int("12"), Stream()))),
To(To(Int("13"), Stream()), Stream()) To(To(Int("13"), Stream()), Stream())
])) ]))
test emit_several_default [[
<<< 1 + 2; 3 >>>
(4 + 5) >>> >> stream; stream << 6;
<<< 7 << 75
<<< 8
+ (9+10);
11 + 12 >>>; 13 >>>
>>>
]] parse succeeds
/** writes
3399677527121313*/

View File

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

View File

@ -1 +1 @@
stream << 'Hello, world!' <<< 'Hello, world!'

View File

@ -15,7 +15,9 @@ rules
seqFlatten: Sequence(l) -> Sequence(<mapconcat(?Sequence(<id>) <+ ![<id>])>l) seqFlatten: Sequence(l) -> Sequence(<mapconcat(?Sequence(<id>) <+ ![<id>])>l)
defStream: DefGets(x) -> Gets(Stream(), x)
defStream: DefTo(x) -> To(x, Stream())
strategies strategies
desugar-fostr = bottomup(try(deISe <+ seqFlatten)) desugar-fostr = bottomup(try(defStream <+ deISe <+ seqFlatten))

View File

@ -19,7 +19,8 @@ rules
returning the final value. returning the final value.
*/ */
hs: (_, TopLevel(val)) -> $[import System.IO hs: (_, TopLevel(val)) -> $[-- Preamble from fostr
import System.IO
data IOStream = StdIO data IOStream = StdIO
gets :: Show b => a -> b -> IO a gets :: Show b => a -> b -> IO a

View File

@ -2,13 +2,16 @@ module javascript
imports libstrategolib signatures/- util imports libstrategolib signatures/- util
rules rules
js: TopLevel(x) -> $[const Stdio = { js: TopLevel(x) -> $[// Fostr preamble
const Stdio = {
gets: v => { process.stdout.write(String(v)); return Stdio; }, gets: v => { process.stdout.write(String(v)); return Stdio; },
} }
function to(data, strm) { function to(data, strm) {
strm.gets(data); strm.gets(data);
return data; return data;
} }
// End of preamble
[x]] [x]]
js: Stream() -> $[Stdio] js: Stream() -> $[Stdio]

View File

@ -2,7 +2,8 @@ module python
imports libstrategolib signatures/- util imports libstrategolib signatures/- util
rules rules
py: TopLevel(x) -> $[import sys py: TopLevel(x) -> $[## Fostr preamble
import sys
class StdioC: class StdioC:
def gets(self, v): def gets(self, v):
print(v, file=sys.stdout, end='') print(v, file=sys.stdout, end='')
@ -11,6 +12,8 @@ rules
strm.gets(data) strm.gets(data)
return data return data
Stdio = StdioC() Stdio = StdioC()
## End of preamble
[x]] [x]]
py: Stream() -> $[Stdio] py: Stream() -> $[Stdio]