2021-01-30 23:37:53 +00:00
|
|
|
module javascript
|
2021-02-01 01:46:31 +00:00
|
|
|
imports libstrategolib signatures/- util
|
2021-01-30 23:37:53 +00:00
|
|
|
|
|
|
|
rules
|
2021-02-18 20:18:47 +00:00
|
|
|
js: TopLevel(x) -> $[// Fostr preamble
|
2021-03-13 05:49:34 +00:00
|
|
|
const _fostr_readline = require('readline');
|
|
|
|
const _fostr_events = require('events');
|
|
|
|
const _fostr_rl = _fostr_readline.createInterface({input: process.stdin});
|
2021-02-18 20:18:47 +00:00
|
|
|
const Stdio = {
|
2021-03-13 05:49:34 +00:00
|
|
|
gets: v => { process.stdout.write(String(v)); return Stdio; },
|
|
|
|
emit: async () => {
|
|
|
|
const [line] = await _fostr_events.once(_fostr_rl, 'line');
|
|
|
|
return line + "\n"; }
|
2021-01-30 23:37:53 +00:00
|
|
|
}
|
2021-02-06 05:11:41 +00:00
|
|
|
function to(data, strm) {
|
|
|
|
strm.gets(data);
|
2021-02-01 01:46:31 +00:00
|
|
|
return data;
|
|
|
|
}
|
2021-03-13 05:49:34 +00:00
|
|
|
|
|
|
|
const _fostr_body = async () => {
|
2021-02-18 20:18:47 +00:00
|
|
|
// End of preamble
|
|
|
|
|
2021-03-13 05:49:34 +00:00
|
|
|
[x]
|
|
|
|
|
|
|
|
// Fostr coda
|
|
|
|
_fostr_rl.close()
|
|
|
|
}
|
|
|
|
_fostr_body();
|
|
|
|
]
|
|
|
|
with line := "[line]"
|
2021-01-30 23:37:53 +00:00
|
|
|
|
2021-02-10 20:47:34 +00:00
|
|
|
js: Stream() -> $[Stdio]
|
|
|
|
js: Int(x) -> x
|
2021-02-18 04:17:05 +00:00
|
|
|
js: LitString(x) -> <javaLitString>x
|
2021-02-19 03:41:54 +00:00
|
|
|
js: EscString(x) -> x
|
2021-03-13 05:49:34 +00:00
|
|
|
js: Sum(x, y) -> $[[x] + [y]]
|
|
|
|
js: Concat(x, y) -> $[[x] + [y]]
|
2021-02-10 20:47:34 +00:00
|
|
|
js: Gets(x, y) -> $[[x].gets([y])]
|
|
|
|
js: To(x, y) -> $[to([x],[y])]
|
2021-03-13 05:49:34 +00:00
|
|
|
js: Emits(x) -> $[(await [x].emit())]
|
2021-02-10 20:47:34 +00:00
|
|
|
js: Terminate(x) -> x
|
|
|
|
js: Sequence(l) -> <join(|";\n")>l
|
2021-01-30 23:37:53 +00:00
|
|
|
|
2021-02-18 04:17:05 +00:00
|
|
|
/* 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
|
|
|
|
|
2021-02-01 01:46:31 +00:00
|
|
|
strategies
|
2021-02-18 04:17:05 +00:00
|
|
|
javaLitString = un-single-quote
|
|
|
|
; string-as-chars(escape-chars(Escape <+ Jscape))
|
|
|
|
; single-quote
|
2021-02-01 08:29:00 +00:00
|
|
|
|
2021-02-06 05:11:41 +00:00
|
|
|
javascript = bottomup(try(js))
|
2021-01-30 23:37:53 +00:00
|
|
|
|
|
|
|
// 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
|