Compare commits

..

5 Commits

Author SHA1 Message Date
7feddbcfbe Extraction from streams (#25)
All checks were successful
continuous-integration/drone/push Build is passing
Add the ! postfix operator and !! expression.
  Also add the ++ string concatenation operator.
  Also allow specification of standard input in the test scheme.

  Resolves #7, #18.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: glen/fostr#25
Co-Authored-By: Glen Whitney <glen@nobody@nowhere.net>
Co-Committed-By: Glen Whitney <glen@nobody@nowhere.net>
2021-03-13 19:30:23 +00:00
cc89ad1e93 Add OCaml code generation (#24)
All checks were successful
continuous-integration/drone/push Build is passing
Also start using nailgun to speed up code generation.

  Resolves #6.

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Reviewed-on: glen/fostr#24
Co-Authored-By: Glen Whitney <glen@nobody@nowhere.net>
Co-Committed-By: Glen Whitney <glen@nobody@nowhere.net>
2021-03-01 20:40:35 +00:00
380177b274 Merge pull request 'docs: Finally get the tour to start from the real helloworld' (#23) from hw_tour into main
All checks were successful
continuous-integration/drone/push Build is passing
Also improves the testing situation for the features to date.
  Resolves #17.

Reviewed-on: glen/fostr#23
2021-02-19 17:30:18 +00:00
f9c6e04c8c docs: Finally get the tour to start from the real helloworld
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Also improves the testing situation for the features to date.
  Resolves #17.
2021-02-19 08:37:14 -08:00
5ef816610b Merge pull request 'feat: Add double-quoted string constants with escapes' (#22) from string_escape into main
All checks were successful
continuous-integration/drone/push Build is passing
Resolves #20.

Reviewed-on: glen/fostr#22
2021-02-19 04:09:38 +00:00
18 changed files with 356 additions and 63 deletions

View File

@ -29,7 +29,18 @@ steps:
- java -jar /drone/lib/spt/org.metaborg.spt.cmd/target/org.metaborg.spt.cmd* -l . -s /drone/lib/spt/org.metaborg.meta.lang.spt -t tests - java -jar /drone/lib/spt/org.metaborg.spt.cmd/target/org.metaborg.spt.cmd* -l . -s /drone/lib/spt/org.metaborg.meta.lang.spt -t tests
- mkdir -p lib - mkdir -p lib
- curl -o lib/sunshine.jar -L 'http://artifacts.metaborg.org/service/local/artifact/maven/redirect?r=snapshots&g=org.metaborg&a=org.metaborg.sunshine2&v=LATEST' - curl -o lib/sunshine.jar -L 'http://artifacts.metaborg.org/service/local/artifact/maven/redirect?r=snapshots&g=org.metaborg&a=org.metaborg.sunshine2&v=LATEST'
- bin/fosgen tests/emit_sum.fos - name: setup_gen
image: gcc
volumes:
- name: m2
path: /root/.m2
commands:
- git clone https://github.com/facebook/nailgun.git
- cd nailgun
- make
- cd ../bin
- ln -s ../nailgun/nailgun-client/target/ng .
- cd ..
- name: extract_tests - name: extract_tests
image: xonsh/xonsh image: xonsh/xonsh
commands: commands:
@ -41,7 +52,8 @@ steps:
path: /drone/lib path: /drone/lib
- name: m2 - name: m2
path: /root/.m2 path: /root/.m2
commands: commands: # Note we first make sure that fosgen is working
- bin/fosgen -d tests/emit_sum.fos
- bin/generate_test_code - bin/generate_test_code
- name: python_tests - name: python_tests
image: python:slim image: python:slim
@ -55,6 +67,13 @@ steps:
image: haskell image: haskell
commands: commands:
- bin/run_tests runghc hs - bin/run_tests runghc hs
- name: ocaml_tests
image: ocaml/opam
commands:
- ls -als tests/extracted
- opam init
- eval $(opam env)
- bin/run_tests ocaml ml
volumes: volumes:
- name: lib - name: lib

6
.gitignore vendored
View File

@ -12,10 +12,16 @@
.pydevproject .pydevproject
a.out
*.aterm *.aterm
/site /site
bin/ng
tests/extracted/* tests/extracted/*
tests/*.js tests/*.js
tests/*.py tests/*.py
tests/*.hs tests/*.hs
tests/*.ml
tests/*.cmi
tests/*.cmo
adhoc* adhoc*

View File

@ -13,12 +13,16 @@ DESTINATION = 'tests/extracted'
# Extension for extracted files: # Extension for extracted files:
EXT = 'fos' EXT = 'fos'
# Extension for desired input:
INP = 'in'
# Extension for expectations: # Extension for expectations:
EXP = 'expect' EXP = 'expect'
for path in TEST_LIST: for path in TEST_LIST:
destdir = pf"{DESTINATION}/{path.stem}" destdir = pf"{DESTINATION}/{path.stem}"
mkdir -p @(destdir) mkdir -p @(destdir)
chmod ugo+rwx @(destdir)
contents = path.read_text() contents = path.read_text()
tests = re.split(r'test\s*(.+?)\s*\[\[.*?\n', contents)[1:] tests = re.split(r'test\s*(.+?)\s*\[\[.*?\n', contents)[1:]
testit = iter(tests) testit = iter(tests)
@ -33,6 +37,11 @@ for path in TEST_LIST:
expath = destdir / f"{name}.{EXT}" expath = destdir / f"{name}.{EXT}"
expath.write_text(example) expath.write_text(example)
echo Wrote @(expath) echo Wrote @(expath)
im = re.search(r'/\*\*\s+accepts.*?\n([\s\S]*?)\*\*/', details[em.end():])
if im:
ipath = destdir / f"{name}.{INP}"
ipath.write_text(im[1])
echo " ...and" @(ipath)
xm = re.search(r'/\*\*\s+writes.*?\n([\s\S]*?)\*\*/', details[em.end():]) xm = re.search(r'/\*\*\s+writes.*?\n([\s\S]*?)\*\*/', details[em.end():])
if xm: if xm:
xpath = destdir / f"{name}.{EXP}" xpath = destdir / f"{name}.{EXP}"

View File

@ -5,6 +5,7 @@ erro() { printf "%s\n" "$*" >&2; }
##### Set defaults: ##### Set defaults:
SUPPRESS_ERR=YES SUPPRESS_ERR=YES
USE_NAILGUN=YES
LANGUAGE=Python LANGUAGE=Python
##### Extract command line options: ##### Extract command line options:
@ -14,18 +15,23 @@ do
-h|--help) -h|--help)
echo echo
echo "Usage:" echo "Usage:"
echo " fosgen [-d] [-l LANGUAGE] FILE" echo " fosgen [-d] [-j] [-l LANGUAGE] FILE"
echo echo
echo "Writes to standard output the code generated from the fostr" echo "Writes to standard output the code generated from the fostr"
echo "program in FILE, targeting the specified LANGUAGE (which" echo "program in FILE, targeting the specified LANGUAGE (which"
echo "defaults to Python)." echo "defaults to Python)."
echo echo
echo "The -d option writes diagnostic output to standard error." echo "The -d option writes diagnostic output to standard error."
echo "The -j option uses the Spoofax Sunshine JAR directly, rather"
echo "than via nailgun."
exit exit
;; ;;
-d) -d)
SUPPRESS_ERR='' SUPPRESS_ERR=''
;; ;;
-j)
USE_NAILGUN=''
;;
-l) -l)
shift shift
LANGUAGE="$1" LANGUAGE="$1"
@ -67,5 +73,17 @@ then
exec 2>/dev/null exec 2>/dev/null
fi fi
if [[ $USE_NAILGUN ]]
then
if [[ $SUPPRESS_ERR ]]
then
$BINDIR/let_sun_shine
else
$BINDIR/let_sun_shine noisy
fi
$BINDIR/ng sunshine transform -p $PROJDIR -n $LANGUAGE -i $PROGRAM
exit $?
fi
java -jar $SUNJAR transform -p $PROJDIR -l $PROJDIR -l $MVN_REPO -n $LANGUAGE -i $PROGRAM java -jar $SUNJAR transform -p $PROJDIR -l $PROJDIR -l $MVN_REPO -n $LANGUAGE -i $PROGRAM
exit $? exit $?

View File

@ -4,7 +4,7 @@ failed=0
for dir in tests/extracted/*; do for dir in tests/extracted/*; do
for file in $dir/*.fos; do for file in $dir/*.fos; do
for language in Python Javascript Haskell; do for language in Python Javascript Haskell OCaml; do
echo bin/fosgen -l ${language%.*} $file ... echo bin/fosgen -l ${language%.*} $file ...
bin/fosgen -l $language $file bin/fosgen -l $language $file
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then

40
bin/let_sun_shine Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash
# Helper for fosgen, not intended to be used directly
# With an argument, print diagnostic output
BINDIR=$(dirname $BASH_SOURCE)
if $BINDIR/ng sunshine --help
then
if [[ $1 ]]
then
echo "sun already shining."
fi
else
if [[ $1 ]]
then
echo "disperse the clouds."
fi
SUNJAR="$BINDIR/../lib/sunshine.jar"
PROJDIR="$BINDIR/.."
if [[ ! $MVN_REPO ]]; then
MVN_REPO="$HOME/.m2/repository"
fi
if [[ ! -d $MVN_REPO ]]; then
MVN_REPO="/root/.m2/repository"
fi
if [[ ! -d $MVN_REPO ]]; then
echo "Cannot find your Maven repository. Please set environment variable"
echo "MVN_REPO to its full path and re-run."
exit 1
fi
if [[ $1 ]]
then
java -jar $SUNJAR server &
else
java -jar $SUNJAR server >/dev/null 2>&1 &
fi
sleep 5
$BINDIR/ng sunshine load -l $PROJDIR -l $MVN_REPO
fi

View File

@ -9,8 +9,14 @@ diffed=0
for dir in tests/extracted/*; do for dir in tests/extracted/*; do
for file in $dir/*.$ext; do for file in $dir/*.$ext; do
((total++)) ((total++))
$command $file > $file.out if [[ -f ${file%.*}.in ]]; then
if [[ $? -ne 0 ]]; then cat ${file%.*}.in | $command $file > $file.out
result=$?
else
$command $file > $file.out
result=$?
fi
if [[ $result -ne 0 ]]; then
echo ERROR: $command $file failed. echo ERROR: $command $file failed.
((failed++)) ((failed++))
else else

View File

@ -4,3 +4,4 @@ menus
action: "Python" = to-python action: "Python" = to-python
action: "Javascript" = to-javascript action: "Javascript" = to-javascript
action: "Haskell" = to-haskell action: "Haskell" = to-haskell
action: "OCaml" = to-ocaml

View File

@ -9,7 +9,7 @@ plugins:
- search - search
- semiliterate: - semiliterate:
ignore_folders: [target, lib] ignore_folders: [target, lib]
exclude_extensions: ['.o', '.hi'] exclude_extensions: ['.o', '.hi', '.cmi', '.cmo']
extract_standard_markdown: extract_standard_markdown:
terminate: <!-- /md --> terminate: <!-- /md -->
theme: theme:

View File

@ -42,10 +42,13 @@ context-free syntax
Ex.EscString = STRING Ex.EscString = STRING
Ex.Stream = <stream> Ex.Stream = <stream>
Ex.Sum = <<Ex> + <Ex>> {left} Ex.Sum = <<Ex> + <Ex>> {left}
Ex.Concat = <<Ex> ++ <Ex>> {left}
Ex.Gets = [[Ex] << [Ex]] {left} Ex.Gets = [[Ex] << [Ex]] {left}
Ex.DefGets = [<<< [Ex]] Ex.DefGets = [<<< [Ex]]
Ex.To = [[Ex] >> [Ex]] {left} Ex.To = [[Ex] >> [Ex]] {left}
Ex.DefTo = [[Ex] >>>] Ex.DefTo = [[Ex] >>>]
Ex.Emits = <<Ex>!>
Ex.DefEmits = <!!>
Ex = <(<Ex>)> {bracket} Ex = <(<Ex>)> {bracket}
@ -53,7 +56,7 @@ context-free priorities
Ex.To Ex.To
> Ex.DefTo > Ex.DefTo
> Ex.Sum > {Ex.Sum Ex.Concat}
> Ex.DefGets > Ex.DefGets
> Ex.Gets, > Ex.Gets,

View File

@ -16,15 +16,21 @@ Title: A whirlwind tour of fostr
## Whirlwind tour ## Whirlwind tour
fostr is just in its infancy, so it's not yet even ready for There seems only to be one way to start a tour like this. So here goes:
Hello, World. The best we can offer now is this little snippet
that writes the sum of the ASCII codes for 'H', 'W', and '!' to standard output:
```fostr ```fostr
**/ **/
/** md */ test emit_sum [[ /** md */ test hello_world [[
stream << 72 + 87 + 33 <<< 'Hello, world!'
]] /* **/ ]] /* **/
parse to TopLevel(DefGets(LitString("'Hello, world!'")))
/** writes
Hello, world!**/
// Prior proto-hello-world, no longer in the tour.
test emit_sum [[
stream << 72 + 87 + 33
]]
parse to TopLevel(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33")))) parse to TopLevel(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))))
/** writes /** writes
192**/ 192**/
@ -33,7 +39,7 @@ parse to TopLevel(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))))
``` ```
At the moment, there are only two ways to run a file containing fostr code At the moment, there are only two ways to run a file containing fostr code
(you can find the above in `tests/emit_sum.fos`). They both start by (you can find the above in `tests/hw.fos`). They both start by
cloning this fostr project. Then, either: cloning this fostr project. Then, either:
1. Open the project in Eclipse and build it, visit your program file, 1. Open the project in Eclipse and build it, visit your program file,
@ -46,30 +52,70 @@ cloning this fostr project. Then, either:
For example, this snippet generates the following Python: For example, this snippet generates the following Python:
```python ```python
{! ../tests/emit_sum.py extract: {! ../tests/hw.py extract:
start: 'Stdio\s=' start: 'Stdio\s='
!} !}
``` ```
(which writes "192" to standard output); it also generates identical code in It generates nearly identical code in
this simple example for this simple example for Javascript (just with `"Hello, world!"`
Javascript, although it generates a different preamble defining Stdio in each in place of `r'Hello, world!'`), although it generates a different
case. (Haskell code generation is also currently supported.) preamble defining Stdio for each language. (Currently, Haskell and OCaml
code generation are also supported.)
There's not much to break down in such a tiny program as this, but let's do
it. The prefix operator `<<<` could be read as "the default stream receives...",
and unsurprisingly in a main program the default stream is standard input and
output. And `'Hello, world!'` is a literal string constant; what you see is
what you get. The only detail to know is that such constants must occur
within a single line of your source file. So depending on how you
ran the program and how closely you looked at its output,
you may have noticed this program does not write a newline at the end
of its message. Nothing is ever implicitly sent to a stream. So if you want
newlines, you should switch to a (double-quoted) string that allows
the usual array of escape sequences:
```fostr
**/
/** md */ test hello_esc_world [[
<<< "Hello,\t\tworld!\n\n"
]] /* **/
parse to TopLevel(DefGets(EscString("\"Hello,\t\tworld!\n\n\"")))
/** writes
Hello, world!
**/
/** md
```
(We threw in two of each so you could clearly see them in the output if
you run this program.)
### Everything has a value ### Everything has a value
As mentioned in the [Introduction](../README.md), everything in a fostr As mentioned in the [Introduction](../README.md), everything in a fostr
program (including the entire program itself) is an expression and has program (including the entire program itself) is an expression and has
a value. So what's the value of that expression above? Well, appropriately a value. So what's the value of that expression above? Well, for convenience,
enough, `stream` is our the value of a stream receiving an item is (generally) just the stream back
first example of a stream, and for convenience, the value of a stream again. That way we can use the general (left-associative)
receiving an item is (usually) just the stream back again. The `<<` operator `_stream_ << _value_` operator to chain insertions into a stream:
is also left-associative, so that way we can chain insertions into a stream:
```fostr ```fostr
**/ **/
/** md */ test emit_twice [[ /** md */ test state_obvious [[
stream << 72 + 87 + 33 << 291 <<< 'Two and ' << 2 << ' make ' << 2+2 << ".\n"
]] /* **/ ]] /* **/
parse to TopLevel(
Gets(Gets(Gets(Gets(DefGets(LitString("'Two and '")),Int("2")),
LitString("' make '")),Sum(Int("2"),Int("2"))),
EscString("\".\n\"")))
/** writes
Two and 2 make 4.
**/
test emit_twice [[
stream << 72 + 87 + 33 << 291
]]
parse to TopLevel( parse to TopLevel(
Gets(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))), Int("291"))) Gets(Gets(Stream(), Sum(Sum(Int("72"), Int("87")), Int("33"))), Int("291")))
/** writes /** writes
@ -77,24 +123,28 @@ parse to TopLevel(
/** md /** md
``` ```
Running this program produces a nice palindromic output: "192291".
And because sometimes you want to emphasize the value and propagate that And because sometimes you want to emphasize the value and propagate that
instead of the stream, you can also write these expressions "the other way" instead of the stream, you can also write these expressions "the other way"
with `>>`; both forms return the first argument, so the following writes "824": with `>>>` for sending to the default stream or `>>` in general; these forms
(generally) return the value sent, so the following writes "824":
```fostr ```fostr
**/ **/
/** md */ test enters_twice [[ /** md */ test enters_twice [[
(7 + 8 >> stream + 9) >> stream (7 + 8 >> stream + 9) >>>
]] /* **/ ]] /* **/
parse to TopLevel( parse to TopLevel(
To(Sum(Sum(Int("7"), To(Int("8"), Stream())), Int("9")), Stream())) DefTo(Sum(Sum(Int("7"), To(Int("8"), Stream())), Int("9"))))
/** writes /** writes
824**/ 824**/
/** md /** md
``` ```
Two things are worth noting here: the default stream can always be referred to
directly via the identifier `stream`, and the precedences of `<<` and `>>` are
different so that generally full expressions go to a stream with `<<` but
just individual terms are sent with `>>`.
### Layout in fostr ### Layout in fostr
@ -104,13 +154,13 @@ lines are indented from the start of the initial line:
**/ **/
/** md */ test receive_enter_break [[ /** md */ test receive_enter_break [[
stream << <<<
7 7
+ 8 >> stream + 8 >>>
+ 9 + 9
]] /* **/ ]] /* **/
parse to TopLevel( parse to TopLevel(
Gets(Stream(), Sum(Sum(Int("7"), To(Int("8"), Stream())), Int("9")))) DefGets(Sum(Sum(Int("7"), DefTo(Int("8"))), Int("9"))))
/** writes /** writes
824**/ 824**/
@ -121,8 +171,8 @@ parse to TopLevel(
**/ **/
/** md */ test enter_receive_bad_continuation [[ /** md */ test enter_receive_bad_continuation [[
(7 + 8 >> stream + 9) (7 + 8 >>> + 9)
>> (stream << 9 + 2) >> (<<< 9 + 2)
]] /* **/ ]] /* **/
parse fails parse fails
@ -145,16 +195,17 @@ lines are evaluated in sequence. For example, the program
**/ **/
/** md */ test emit_thrice [[ /** md */ test emit_thrice [[
stream << 72 + 87 <<< 72 + 87
stream << 88 <<< 88
+ 96 + 96
99 + 12 >> 99 + 12
stream >>>
]] /* **/ ]] /* **/
parse to TopLevel(Sequence([ parse to TopLevel(Sequence([
Gets(Stream(), Sum(Int("72"), Int("87"))), DefGets(Sum(Int("72"), Int("87"))),
Gets(Stream(), Sum(Int("88"), Int("96"))), DefGets(Sum(Int("88"), Int("96"))),
Sum(Int("99"), To(Int("12"), Stream())) Sum(Int("99"), DefTo(Int("12")))
])) ]))
/** writes /** writes
15918412**/ 15918412**/
@ -169,10 +220,10 @@ in sequence align at the left; e.g., the following fails to parse:
**/ **/
/** md */ test emit_thrice_bad_alignment [[ /** md */ test emit_thrice_bad_alignment [[
stream << 72 + 87 <<< 72 + 87
stream << 88 <<< 88
+ 96 + 96
99 + 12 >> stream 99 + 12 >>>
]] /* **/ ]] /* **/
parse fails parse fails
@ -187,23 +238,23 @@ are so terminated. So the following is OK:
**/ **/
/** md */ test emit_several [[ /** md */ test emit_several [[
stream << 1 + 2; 3 >> stream <<< 1 + 2; 3 >>>
(4 + 5) >> stream; stream << 6; (4 + 5) >>>; stream << 6;
stream << 7 <<< 7
stream << 8 <<< 8
+ (9+10); + (9+10);
11 + 12 >> stream; 13 >> stream 11 + 12 >>>; 13 >>>
>> stream >>>
]] /* **/ ]] /* **/
parse to TopLevel(Sequence([ parse to TopLevel(Sequence([
ISequence(Prior([Terminate(Gets(Stream(), Sum(Int("1"), Int("2"))))]), ISequence(Prior([Terminate(DefGets(Sum(Int("1"), Int("2"))))]),
To(Int("3"), Stream())), DefTo(Int("3"))),
ISequence(Prior([Terminate(To(Sum(Int("4"), Int("5")), Stream()))]), ISequence(Prior([Terminate(DefTo(Sum(Int("4"), Int("5"))))]),
Terminate(Gets(Stream(), Int("6")))), Terminate(Gets(Stream(), Int("6")))),
Gets(Stream(), Int("7")), DefGets(Int("7")),
Terminate(Gets(Stream(), Sum(Int("8"), Sum(Int("9"), Int("10"))))), Terminate(DefGets(Sum(Int("8"), Sum(Int("9"), Int("10"))))),
ISequence(Prior([Terminate(Sum(Int("11"), To(Int("12"), Stream())))]), ISequence(Prior([Terminate(Sum(Int("11"), DefTo(Int("12"))))]),
To(To(Int("13"), Stream()), Stream())) DefTo(DefTo(Int("13"))))
])) ]))
/** writes /** writes
3396727121313**/ 3396727121313**/
@ -242,4 +293,38 @@ test emit_several_default [[
>>> >>>
]] parse succeeds ]] parse succeeds
/** writes /** writes
3399677527121313*/ 3399677527121313**/
/** md
### Streams are bidirectional
So far we have only sent items to a stream. But we can extract them from
streams as well, with the `!` postfix operator. `!!` all by itself abbreviates
`stream!`, i.e., extraction from the standard stream. For example,
```fostr
**/
/** md */ test custom_hw [[
<<< "What is your name?\n"
<<< 'Hello, ' ++ !!
]] /* **/
parse to TopLevel(Sequence([
DefGets(EscString("\"What is your name?\n\"")),
DefGets(Concat(LitString("'Hello, '"),DefEmits()))
]))
/** accepts
Kilroy
**/
/** writes
What is your name?
Hello, Kilroy
**/
/** md
```
queries users for their name and then writes a customized greeting. It also
illustrates the use of `++` for string concatenation, as opposed to `+` for
(numerical) addition.
**/

View File

@ -17,6 +17,7 @@ rules
defStream: DefGets(x) -> Gets(Stream(), x) defStream: DefGets(x) -> Gets(Stream(), x)
defStream: DefTo(x) -> To(x, Stream()) defStream: DefTo(x) -> To(x, Stream())
defStream: DefEmits() -> Emits(Stream())
strategies strategies

View File

@ -6,6 +6,7 @@ imports
pp pp
outline outline
analysis analysis
ocaml
haskell haskell
javascript javascript
python python

View File

@ -10,7 +10,7 @@ rules
rule. rule.
C) We will use bottomup-para to traverse the full AST with the C) We will use bottomup-para to traverse the full AST with the
transformation from A so that we have access to the original expression transformation from A so that we have access to the original expression
(and get get the Statix-associated type when we need to). (and can get the Statix-associated type when we need to).
Hence the transformation in (A) must actually take a pair of Hence the transformation in (A) must actually take a pair of
an (original) term and a term with value strings at each child, an (original) term and a term with value strings at each child,
and be certain to return a value string. and be certain to return a value string.
@ -23,6 +23,7 @@ rules
import System.IO import System.IO
data IOStream = StdIO data IOStream = StdIO
-- Danger: These currently assume the stream is StdIO
gets :: Show b => a -> b -> IO a gets :: Show b => a -> b -> IO a
gets s d = do gets s d = do
putStr(show d) putStr(show d)
@ -33,6 +34,10 @@ rules
putStr(d) putStr(d)
return s return s
emit s = do
l <- getLine
return (l ++ "\n")
main = do main = do
[<Preactions>()]return [val]] [<Preactions>()]return [val]]
@ -41,6 +46,7 @@ rules
hs: (_, LitString(x)) -> <haskLitString>x hs: (_, LitString(x)) -> <haskLitString>x
hs: (_, EscString(x)) -> x hs: (_, EscString(x)) -> x
hs: (_, Sum(x, y)) -> $[([x] + [y])] hs: (_, Sum(x, y)) -> $[([x] + [y])]
hs: (_, Concat(x, y)) -> $[([x] ++ [y])]
hs: (Gets(_, xn), Gets(s, x)) -> v hs: (Gets(_, xn), Gets(s, x)) -> v
with v := <newname>"_fostr_get" with v := <newname>"_fostr_get"
@ -52,6 +58,10 @@ rules
hs_gets: (s, xn, x ) -> $[[s] [<hs_getOp>xn] [x]] hs_gets: (s, xn, x ) -> $[[s] [<hs_getOp>xn] [x]]
hs_getOp = get-type; (?STRING() < !"`getsStr`" + !"`gets`") hs_getOp = get-type; (?STRING() < !"`getsStr`" + !"`gets`")
hs: (_, Emits(s)) -> v
with v := <newname>"_fostr_emitted"
; <add-preactions>[$[[v] <- emit [s]]]
hs: (_, Terminate(x)) -> $[[x];;] hs: (_, Terminate(x)) -> $[[x];;]
hs: (_, Sequence(l)) -> <last>l hs: (_, Sequence(l)) -> <last>l
/* One drawback of using paramorphism is we have to handle lists /* One drawback of using paramorphism is we have to handle lists

View File

@ -3,24 +3,41 @@ imports libstrategolib signatures/- util
rules rules
js: TopLevel(x) -> $[// Fostr preamble js: TopLevel(x) -> $[// Fostr preamble
const _fostr_readline = require('readline');
const _fostr_events = require('events');
const _fostr_rl = _fostr_readline.createInterface({input: process.stdin});
const Stdio = { const Stdio = {
gets: v => { process.stdout.write(String(v)); return Stdio; }, gets: v => { process.stdout.write(String(v)); return Stdio; },
emit: async () => {
const [line] = await _fostr_events.once(_fostr_rl, 'line');
return line + "\n"; }
} }
function to(data, strm) { function to(data, strm) {
strm.gets(data); strm.gets(data);
return data; return data;
} }
const _fostr_body = async () => {
// End of preamble // End of preamble
[x]] [x]
// Fostr coda
_fostr_rl.close()
}
_fostr_body();
]
with line := "[line]"
js: Stream() -> $[Stdio] js: Stream() -> $[Stdio]
js: Int(x) -> x js: Int(x) -> x
js: LitString(x) -> <javaLitString>x js: LitString(x) -> <javaLitString>x
js: EscString(x) -> x js: EscString(x) -> x
js: Sum(x,y) -> $[[x] + [y]] js: Sum(x, y) -> $[[x] + [y]]
js: Concat(x, y) -> $[[x] + [y]]
js: Gets(x, y) -> $[[x].gets([y])] js: Gets(x, y) -> $[[x].gets([y])]
js: To(x, y) -> $[to([x],[y])] js: To(x, y) -> $[to([x],[y])]
js: Emits(x) -> $[(await [x].emit())]
js: Terminate(x) -> x js: Terminate(x) -> x
js: Sequence(l) -> <join(|";\n")>l js: Sequence(l) -> <join(|";\n")>l

66
trans/ocaml.str Normal file
View File

@ -0,0 +1,66 @@
module ocaml
imports libstrategolib signatures/- util signature/TYPE analysis
/* Note will use bottomup-para to traverse the full AST so that
we have access to the original expression (and can get the
Statix-associated type when we need to).
This means that every one of our local rules must take a pair
of an original term and a term with every child replaced by
its generated code.
*/
rules
ml: (_, TopLevel(x)) -> $[(* fostr preamble *)
type stream = { getS: string -> stream; emitS: unit -> string }
let rec stdio = {
getS = (fun s -> print_string s; stdio);
emitS = (fun () -> (read_line ()) ^ "\n");
};;
(* End of preamble *)
[x]]
ml: (_, Stream()) -> $[stdio]
ml: (_, Int(x)) -> x
ml: (_, LitString(x)) -> $[{|[<un-single-quote>x]|}]
ml: (_, EscString(x)) -> x
ml: (_, Sum(x, y)) -> $[[x] + [y]]
ml: (_, Concat(x, y)) -> $[[x] ^ [y]]
ml: (Gets(_,yn), Gets(x, y))
-> $[([x]).getS ([<ml_str>(yn,y)])]
ml: (To(xn,_), To(x, y))
-> $[let _fto = ([x]) in (ignore (([y]).getS ([<ml_str>(xn,"_fto")])); _fto)]
ml: (_, Emits(s)) -> $[[s].emitS ()]
ml: (_, Terminate(x)) -> x
ml: (_, Sequence(l)) -> <ml_seq>l
ml_seq: [x] -> x
ml_seq: [x | xs ] -> $[ignore ([x]);
[<ml_seq>xs]]
/* One drawback of using paramorphism is we have to handle lists
explicitly:
*/
ml: (_, []) -> []
ml: (_, [x | xs]) -> [x | xs]
/* Another drawback of using paramorphism is at the very leaves we have
to undouble the tuple:
*/
ml: (x, x) -> x where <is-string>x
ml_str: (node, code) -> $[[<ml_string_cast>node]([code])]
strategies
ml_string_cast = get-type; (?INT() < !"string_of_int" + !"")
ocaml = bottomup-para(try(ml))
// Interface ocaml code generation with editor services and file system
to-ocaml: (selected, _, _, path, project-path) -> (filename, result)
with filename := <guarantee-extension(|"ml")> path
; result := <ocaml> selected

View File

@ -8,6 +8,8 @@ rules
def gets(self, v): def gets(self, v):
print(v, file=sys.stdout, end='') print(v, file=sys.stdout, end='')
return self return self
def emit(self):
return input() + "\n" # Python inconsistently strips when using input
def to(data,strm): def to(data,strm):
strm.gets(data) strm.gets(data)
return data return data
@ -21,8 +23,10 @@ rules
py: LitString(x) -> $[r[x]] py: LitString(x) -> $[r[x]]
py: EscString(x) -> x py: EscString(x) -> x
py: Sum(x,y) -> $[[x] + [y]] py: Sum(x,y) -> $[[x] + [y]]
py: Concat(x,y) -> $[[x] + [y]]
py: Gets(x, y) -> $[[x].gets([y])] py: Gets(x, y) -> $[[x].gets([y])]
py: To(x, y) -> $[to([x],[y])] py: To(x, y) -> $[to([x],[y])]
py: Emits(x) -> $[[x].emit()]
py: Terminate(x) -> $[[x];] py: Terminate(x) -> $[[x];]
py: Sequence(l) -> <join(|"\n")>l py: Sequence(l) -> <join(|"\n")>l

View File

@ -222,6 +222,13 @@ This pattern lets us specify error messages.
type_Ex(e2) == STREAM() | error $[Items may only be sent to Streams.]@e2. type_Ex(e2) == STREAM() | error $[Items may only be sent to Streams.]@e2.
/* **/ /* **/
ty_Ex(Concat(e1, e2)) = STRING() :-
type_Ex(e1) == STRING() | error $[Expression [e1] not String in concat.]@e1,
type_Ex(e2) == STRING() | error $[Expression [e2] not String in concat.]@e2.
ty_Ex(Emits(e)) = STRING() :- // At the moment, only stream is stdio
type_Ex(e) == STREAM() | error $[Only Streams may emit items.]@e.
/** md /** md
``` ```