fostr/trans/javascript.str
Glen Whitney 904f651897
All checks were successful
continuous-integration/drone/push Build is passing
feat: Add syntax for string literals
Also rudimentary code generation. The difficulty
  is that for Haskell generation, we need to know
  whether an expression is a string or in to send it
  to standard output. So we will need to begin
  implementation of a type system for fostr.
2021-02-11 10:02:43 -08:00

40 lines
1.3 KiB
Plaintext

module javascript
imports libstrategolib signatures/- util
rules
js: TopLevel(x) -> $[const Stdio = {
gets: v => { process.stdout.write(String(v)); return Stdio; },
}
function to(data, strm) {
strm.gets(data);
return data;
}
[x]]
js: Stream() -> $[Stdio]
js: Int(x) -> x
js: LitString(x) -> $['[<string-as-chars(escape-chars(JavaEscape))>x]']
js: Sum(x,y) -> $[[x] + [y]]
js: Gets(x, y) -> $[[x].gets([y])]
js: To(x, y) -> $[to([x],[y])]
js: Terminate(x) -> x
js: Sequence(l) -> <join(|";\n")>l
/* Characters we need to escape in Javascript string constants */
Jscape: ['\t' | cs ] -> ['\', 't' | cs ]
/* I think I can just use ASCII constants for characters... */
Jscape: [ 0 | cs ] -> ['\', '0' | cs ]
Jscape: [ 8 | cs ] -> ['\', 'b' | cs ] // Backspace
Jscape: [ 11 | cs ] -> ['\', 'v' | cs ] // Vertical tab
Jscape: [ 12 | cs ] -> ['\', 'f' | cs ] // Form feed
strategies
JavaEscape = Escape <+ Jscape
javascript = bottomup(try(js))
// Interface javascript code generation with editor services and file system
to-javascript: (selected, _, _, path, project-path) -> (filename, result)
with filename := <guarantee-extension(|"js")> path
; result := <javascript> selected