forked from glen/fostr
Glen Whitney
2e49065031
Also adds parenthesization of fostr expressions. Recasts code generation in terms of bottomup processing of a local strategy. Resolves #1. Co-authored-by: Glen Whitney <glen@studioinfinity.org> Reviewed-on: glen/fostr#8 Co-Authored-By: Glen Whitney <glen@nobody@nowhere.net> Co-Committed-By: Glen Whitney <glen@nobody@nowhere.net>
38 lines
915 B
Plaintext
38 lines
915 B
Plaintext
module python
|
|
imports libstrategolib signatures/- util
|
|
|
|
signature
|
|
constructors
|
|
TopLevel: Ex -> Ex
|
|
|
|
rules
|
|
|
|
py: TopLevel(x) -> $[import sys
|
|
class StdioC:
|
|
def receives(self, v):
|
|
print(v, file=sys.stdout, end='')
|
|
return self
|
|
def forwards(data,strm):
|
|
strm.receives(data)
|
|
return data
|
|
Stdio = StdioC()
|
|
[x]]
|
|
|
|
py: Stdio() -> $[Stdio]
|
|
py: Int(x) -> x
|
|
py: Sum(x) -> $[sum([x])]
|
|
py: Receives(x, y) -> $[[x].receives([y])]
|
|
py: Enters(x, y) -> $[forwards([x],[y])]
|
|
|
|
pylist: x -> $<[<<join(|", ")>x>]>
|
|
|
|
strategies
|
|
|
|
// wrap with a toplevel, then generate code from the bottom up
|
|
python = !TopLevel(<id>); bottomup(try(py <+ pylist))
|
|
|
|
// 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
|