2021-01-30 23:37:53 +00:00
|
|
|
module haskell
|
2021-02-01 01:46:31 +00:00
|
|
|
imports libstrategolib signatures/- util
|
2021-01-30 23:37:53 +00:00
|
|
|
|
|
|
|
signature
|
|
|
|
constructors
|
|
|
|
TopLevel: Ex -> Ex
|
|
|
|
|
|
|
|
rules
|
2021-02-01 01:46:31 +00:00
|
|
|
/* Approach: Generate code from the bottom up.
|
|
|
|
At every node, we create a pair of the implementation and
|
|
|
|
necessary preamble of IO actions.
|
|
|
|
We concatenate preambles as we go up.
|
|
|
|
Finally, at the toplevel we emit the preamble before returning the
|
|
|
|
final value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
hs: TopLevel((c,p)) -> $[import System.IO
|
2021-01-30 23:37:53 +00:00
|
|
|
data IOStream = StdIO
|
|
|
|
|
|
|
|
stdio :: IO IOStream
|
|
|
|
stdio = return StdIO
|
|
|
|
|
|
|
|
receives :: Show b => IO a -> b -> IO a
|
|
|
|
receives s d = do
|
|
|
|
temp <- s
|
|
|
|
putStr(show d)
|
|
|
|
return temp
|
|
|
|
|
|
|
|
main = do
|
2021-02-01 01:46:31 +00:00
|
|
|
[p]return [c]]
|
2021-01-30 23:37:53 +00:00
|
|
|
|
2021-02-01 01:46:31 +00:00
|
|
|
hs: Stdio() -> ("stdio", "")
|
|
|
|
hs: Int(x) -> (x, "")
|
|
|
|
hs: Sum((c,p)) -> ($[sum [c]], p)
|
|
|
|
hs: Receives((c, p), (d, s)) -> ($[[c] `receives` [d]], <conc-strings>(p,s))
|
|
|
|
hs: Enters((c, p), (d, s)) -> <hsenter>(c,d,<conc-strings>(p,s),<newname>"fos")
|
|
|
|
|
|
|
|
hsenter: (x, s, p, v) -> (v, <concat-strings>[$[[p]let [v] = [x]], "\n",
|
|
|
|
$[[s] `receives` [v]], "\n"])
|
|
|
|
|
|
|
|
hslist: x -> (<map(Fst); join(|", "); brack>x, <map(Snd); concat-strings>x)
|
|
|
|
brack: x -> $<[<x>]>
|
2021-01-30 23:37:53 +00:00
|
|
|
|
|
|
|
strategies
|
|
|
|
// wrap expression in a toplevel and then apply code generation
|
2021-02-01 01:46:31 +00:00
|
|
|
haskell = !TopLevel(<id>); bottomup(try(hs <+ hslist))
|
2021-01-30 23:37:53 +00:00
|
|
|
|
|
|
|
// Interface haskell code generation with editor services and file system
|
|
|
|
to-haskell: (selected, _, _, path, project-path) -> (filename, result)
|
|
|
|
with filename := <guarantee-extension(|"hs")> path
|
|
|
|
; result := <haskell> selected
|