fostr/trans/python.str
Glen Whitney 7c69b82484 feat WIP: checkpoint initial efforts on stream extraction
Adds ! operator and !! expression, with Python code gen and
  an item in tour.
  Still needs: other languages, check generated docs, and addition
  of standard input to test scheme.
2021-03-01 20:51:12 -08:00

41 lines
1002 B
Plaintext

module python
imports libstrategolib signatures/- util
rules
py: TopLevel(x) -> $[## Fostr preamble
import sys
class StdioC:
def gets(self, v):
print(v, file=sys.stdout, end='')
return self
def emit(self):
return input()
def to(data,strm):
strm.gets(data)
return data
Stdio = StdioC()
## End of preamble
[x]]
py: Stream() -> $[Stdio]
py: Int(x) -> x
py: LitString(x) -> $[r[x]]
py: EscString(x) -> x
py: Sum(x,y) -> $[[x] + [y]]
py: Concat(x,y) -> $[[x] + [y]]
py: Gets(x, y) -> $[[x].gets([y])]
py: To(x, y) -> $[to([x],[y])]
py: Emits(x) -> $[[x].emit()]
py: Terminate(x) -> $[[x];]
py: Sequence(l) -> <join(|"\n")>l
strategies
python = bottomup(try(py))
// Interface python code generation with editor services and file system
to-python: (selected, _, _, path, project-path) -> (filename, result)
with filename := <guarantee-extension(|"py")> path
; result := <python> selected