From f789ed94fdce08c849420cb24ba372b5fac566d8 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sun, 28 Feb 2021 22:21:56 -0800 Subject: [PATCH 01/10] feat: Add OCaml code generation Also start using nailgun to speed up code generation. Resolves #6. --- .drone.yml | 16 +++++++++++ .gitignore | 6 +++++ bin/fosgen | 15 ++++++++++- bin/generate_test_code | 2 +- bin/let_sun_shine | 26 ++++++++++++++++++ editor/Generation.esv | 1 + tests/basic.spt | 2 +- trans/fostr.str | 1 + trans/haskell.str | 2 +- trans/ocaml.str | 61 ++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 128 insertions(+), 4 deletions(-) create mode 100755 bin/let_sun_shine create mode 100644 trans/ocaml.str diff --git a/.drone.yml b/.drone.yml index 452f163..fdeea06 100644 --- a/.drone.yml +++ b/.drone.yml @@ -29,6 +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 - 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' + - name: setup_gen + image: maven + 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 .. - bin/fosgen tests/emit_sum.fos - name: extract_tests image: xonsh/xonsh @@ -55,6 +67,10 @@ steps: image: haskell commands: - bin/run_tests runghc hs + - name: ocaml_tests + image: ocaml/opam + commands: + - bin/run_tests ocaml ml volumes: - name: lib diff --git a/.gitignore b/.gitignore index 80a4a3c..35e6d95 100644 --- a/.gitignore +++ b/.gitignore @@ -12,10 +12,16 @@ .pydevproject +a.out + *.aterm /site +bin/ng tests/extracted/* tests/*.js tests/*.py tests/*.hs +tests/*.ml +tests/*.cmi +tests/*.cmo adhoc* diff --git a/bin/fosgen b/bin/fosgen index c5af48b..910c7d9 100755 --- a/bin/fosgen +++ b/bin/fosgen @@ -5,6 +5,7 @@ erro() { printf "%s\n" "$*" >&2; } ##### Set defaults: SUPPRESS_ERR=YES +USE_NAILGUN=YES LANGUAGE=Python ##### Extract command line options: @@ -14,18 +15,23 @@ do -h|--help) echo echo "Usage:" - echo " fosgen [-d] [-l LANGUAGE] FILE" + echo " fosgen [-d] [-j] [-l LANGUAGE] FILE" echo echo "Writes to standard output the code generated from the fostr" echo "program in FILE, targeting the specified LANGUAGE (which" echo "defaults to Python)." echo 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 ;; -d) SUPPRESS_ERR='' ;; + -j) + USE_NAILGUN='' + ;; -l) shift LANGUAGE="$1" @@ -67,5 +73,12 @@ then exec 2>/dev/null fi +if [[ $USE_NAILGUN ]] +then + $BINDIR/let_sun_shine + $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 exit $? diff --git a/bin/generate_test_code b/bin/generate_test_code index 2c443d6..0d151a8 100755 --- a/bin/generate_test_code +++ b/bin/generate_test_code @@ -4,7 +4,7 @@ failed=0 for dir in tests/extracted/*; 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 ... bin/fosgen -l $language $file if [[ $? -ne 0 ]]; then diff --git a/bin/let_sun_shine b/bin/let_sun_shine new file mode 100755 index 0000000..aa8df53 --- /dev/null +++ b/bin/let_sun_shine @@ -0,0 +1,26 @@ +#!/bin/bash + +BINDIR=$(dirname $BASH_SOURCE) + +if $BINDIR/ng sunshine --help +then + # echo "sun already shining." +else + # echo "disperse the clouds." + 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 + java -jar $SUNJAR server >/dev/null 2>&1 & + sleep 5 + $BINDIR/ng sunshine load -l $PROJDIR -l $MVN_REPO +fi diff --git a/editor/Generation.esv b/editor/Generation.esv index 3d71fe2..9e5051c 100644 --- a/editor/Generation.esv +++ b/editor/Generation.esv @@ -4,3 +4,4 @@ menus action: "Python" = to-python action: "Javascript" = to-javascript action: "Haskell" = to-haskell + action: "OCaml" = to-ocaml diff --git a/tests/basic.spt b/tests/basic.spt index 215c83f..592150b 100644 --- a/tests/basic.spt +++ b/tests/basic.spt @@ -108,7 +108,7 @@ again. That way we can use the general (left-associative) parse to TopLevel( Gets(Gets(Gets(Gets(DefGets(LitString("'Two and '")),Int("2")), LitString("' make '")),Sum(Int("2"),Int("2"))), - EscString("\"./n\""))) + EscString("\".\n\""))) /** writes Two and 2 make 4. **/ diff --git a/trans/fostr.str b/trans/fostr.str index d9a585a..91194d6 100644 --- a/trans/fostr.str +++ b/trans/fostr.str @@ -6,6 +6,7 @@ imports pp outline analysis + ocaml haskell javascript python diff --git a/trans/haskell.str b/trans/haskell.str index 0e26050..0b08716 100644 --- a/trans/haskell.str +++ b/trans/haskell.str @@ -10,7 +10,7 @@ rules rule. 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 - (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 an (original) term and a term with value strings at each child, and be certain to return a value string. diff --git a/trans/ocaml.str b/trans/ocaml.str new file mode 100644 index 0000000..11fcdca --- /dev/null +++ b/trans/ocaml.str @@ -0,0 +1,61 @@ +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 } + let rec stdio = { + getS = (fun s -> print_string s; stdio) + };; + (* End of preamble *) + + [x]] + + ml: (_, Stream()) -> $[stdio] + ml: (_, Int(x)) -> x + ml: (_, LitString(x)) -> $[{|[x]|}] + ml: (_, EscString(x)) -> x + ml: (_, Sum(x,y)) -> $[[x] + [y]] + ml: (Gets(_,yn), Gets(x, y)) + -> $[([x]).getS ([(yn,y)])] + ml: (To(xn,_), To(x, y)) + -> $[let _fto = ([x]) in (ignore (([y]).getS ([(xn,"_fto")])); _fto)] + ml: (_, Terminate(x)) -> x + ml: (_, Sequence(l)) -> l + + ml_seq: [x] -> x + ml_seq: [x | xs ] -> $[ignore ([x]); +[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 x + + ml_str: (node, code) -> $[[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 := path + ; result := selected From d1c3ed34814e1e853581820523d08dfb09d18a28 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 09:16:50 -0800 Subject: [PATCH 02/10] fix: try different image for compiling Nailgun client Also mention OCaml code generation in docs --- .drone.yml | 2 +- tests/basic.spt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.drone.yml b/.drone.yml index fdeea06..fb12161 100644 --- a/.drone.yml +++ b/.drone.yml @@ -30,7 +30,7 @@ steps: - 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' - name: setup_gen - image: maven + image: silkeh/clang volumes: - name: m2 path: /root/.m2 diff --git a/tests/basic.spt b/tests/basic.spt index 592150b..a5e98c0 100644 --- a/tests/basic.spt +++ b/tests/basic.spt @@ -59,8 +59,8 @@ For example, this snippet generates the following Python: It generates nearly identical code in this simple example for Javascript (just with `"Hello, world!"` in place of `r'Hello, world!'`), although it generates a different -preamble defining Stdio for each language. (Currently, Haskell code -generation is also 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...", From e3ff1f259e0b39de110f432a153a761a3d406eba Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 09:29:46 -0800 Subject: [PATCH 03/10] fix: OK, try the gcc image, see if that will do it --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index fb12161..eea527f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -30,7 +30,7 @@ steps: - 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' - name: setup_gen - image: silkeh/clang + image: gcc volumes: - name: m2 path: /root/.m2 From 87a6fbc64299ea218c31476ee691308e0591069f Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 09:50:33 -0800 Subject: [PATCH 04/10] fix: eliminate empty then block in bash script --- bin/let_sun_shine | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/let_sun_shine b/bin/let_sun_shine index aa8df53..84dfffe 100755 --- a/bin/let_sun_shine +++ b/bin/let_sun_shine @@ -5,6 +5,7 @@ BINDIR=$(dirname $BASH_SOURCE) if $BINDIR/ng sunshine --help then # echo "sun already shining." + : else # echo "disperse the clouds." SUNJAR="$BINDIR/../lib/sunshine.jar" From b1f87428f56d05872f451e6af8d70e5c469883c1 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 10:06:44 -0800 Subject: [PATCH 05/10] feat: -d option to fosgen now enables nailgun diagnostics --- .drone.yml | 2 +- bin/fosgen | 7 ++++++- bin/let_sun_shine | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.drone.yml b/.drone.yml index eea527f..813be68 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,7 @@ steps: - cd ../bin - ln -s ../nailgun/nailgun-client/target/ng . - cd .. - - bin/fosgen tests/emit_sum.fos + - bin/fosgen -d tests/emit_sum.fos - name: extract_tests image: xonsh/xonsh commands: diff --git a/bin/fosgen b/bin/fosgen index 910c7d9..b03656b 100755 --- a/bin/fosgen +++ b/bin/fosgen @@ -75,7 +75,12 @@ fi if [[ $USE_NAILGUN ]] then - $BINDIR/let_sun_shine + 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 diff --git a/bin/let_sun_shine b/bin/let_sun_shine index 84dfffe..cb3c67a 100755 --- a/bin/let_sun_shine +++ b/bin/let_sun_shine @@ -1,13 +1,21 @@ #!/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 - # echo "sun already shining." - : + if [[ $1 ]] + then + echo "sun already shining." + fi else - # echo "disperse the clouds." + if [[ $1 ]] + then + echo "disperse the clouds." + fi SUNJAR="$BINDIR/../lib/sunshine.jar" PROJDIR="$BINDIR/.." if [[ ! $MVN_REPO ]]; then @@ -21,7 +29,12 @@ else echo "MVN_REPO to its full path and re-run." exit 1 fi - java -jar $SUNJAR server >/dev/null 2>&1 & + 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 From dc19df8811ef795ffb0ed589da456b93f64f07db Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 10:21:33 -0800 Subject: [PATCH 06/10] test: Move fosgen test into a step where java is available --- .drone.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 813be68..fad4086 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,6 @@ steps: - cd ../bin - ln -s ../nailgun/nailgun-client/target/ng . - cd .. - - bin/fosgen -d tests/emit_sum.fos - name: extract_tests image: xonsh/xonsh commands: @@ -53,7 +52,8 @@ steps: path: /drone/lib - name: 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 - name: python_tests image: python:slim From 221d85ebbe1713287e20fb1e789e15e9c3526221 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 10:39:18 -0800 Subject: [PATCH 07/10] test: add permissions to extracted test directory The hope is this will prevent the permission errors in the OCaml tests --- .drone.yml | 1 + bin/extract_tests.xsh | 1 + 2 files changed, 2 insertions(+) diff --git a/.drone.yml b/.drone.yml index fad4086..e1363ac 100644 --- a/.drone.yml +++ b/.drone.yml @@ -70,6 +70,7 @@ steps: - name: ocaml_tests image: ocaml/opam commands: + - ls -als tests/extracted - bin/run_tests ocaml ml volumes: diff --git a/bin/extract_tests.xsh b/bin/extract_tests.xsh index 7cf131a..1d8c59f 100644 --- a/bin/extract_tests.xsh +++ b/bin/extract_tests.xsh @@ -19,6 +19,7 @@ EXP = 'expect' for path in TEST_LIST: destdir = pf"{DESTINATION}/{path.stem}" mkdir -p @(destdir) + chmod ugo+rwx @(destdir) contents = path.read_text() tests = re.split(r'test\s*(.+?)\s*\[\[.*?\n', contents)[1:] testit = iter(tests) From 3effe65f0185eda809ca030f9e9bc86df5287c42 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 11:14:53 -0800 Subject: [PATCH 08/10] test: try to get ocaml initialized by opam for testing --- .drone.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.drone.yml b/.drone.yml index e1363ac..90b928b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -71,6 +71,8 @@ steps: image: ocaml/opam commands: - ls -als tests/extracted + - opam init + - eval $(opam env) - bin/run_tests ocaml ml volumes: From cc89ad1e93a0e5da1080e30e0c45af53a850ffd0 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 1 Mar 2021 20:40:35 +0000 Subject: [PATCH 09/10] Add OCaml code generation (#24) Also start using nailgun to speed up code generation. Resolves #6. Co-authored-by: Glen Whitney Reviewed-on: https://code.studioinfinity.org/glen/fostr/pulls/24 Co-Authored-By: Glen Whitney Co-Committed-By: Glen Whitney --- .drone.yml | 23 ++++++++++++++-- .gitignore | 6 +++++ bin/extract_tests.xsh | 1 + bin/fosgen | 20 +++++++++++++- bin/generate_test_code | 2 +- bin/let_sun_shine | 40 +++++++++++++++++++++++++++ editor/Generation.esv | 1 + tests/basic.spt | 6 ++--- trans/fostr.str | 1 + trans/haskell.str | 2 +- trans/ocaml.str | 61 ++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 155 insertions(+), 8 deletions(-) create mode 100755 bin/let_sun_shine create mode 100644 trans/ocaml.str diff --git a/.drone.yml b/.drone.yml index 452f163..90b928b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -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 - 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' - - 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 image: xonsh/xonsh commands: @@ -41,7 +52,8 @@ steps: path: /drone/lib - name: 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 - name: python_tests image: python:slim @@ -55,6 +67,13 @@ steps: image: haskell commands: - 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: - name: lib diff --git a/.gitignore b/.gitignore index 80a4a3c..35e6d95 100644 --- a/.gitignore +++ b/.gitignore @@ -12,10 +12,16 @@ .pydevproject +a.out + *.aterm /site +bin/ng tests/extracted/* tests/*.js tests/*.py tests/*.hs +tests/*.ml +tests/*.cmi +tests/*.cmo adhoc* diff --git a/bin/extract_tests.xsh b/bin/extract_tests.xsh index 7cf131a..1d8c59f 100644 --- a/bin/extract_tests.xsh +++ b/bin/extract_tests.xsh @@ -19,6 +19,7 @@ EXP = 'expect' for path in TEST_LIST: destdir = pf"{DESTINATION}/{path.stem}" mkdir -p @(destdir) + chmod ugo+rwx @(destdir) contents = path.read_text() tests = re.split(r'test\s*(.+?)\s*\[\[.*?\n', contents)[1:] testit = iter(tests) diff --git a/bin/fosgen b/bin/fosgen index c5af48b..b03656b 100755 --- a/bin/fosgen +++ b/bin/fosgen @@ -5,6 +5,7 @@ erro() { printf "%s\n" "$*" >&2; } ##### Set defaults: SUPPRESS_ERR=YES +USE_NAILGUN=YES LANGUAGE=Python ##### Extract command line options: @@ -14,18 +15,23 @@ do -h|--help) echo echo "Usage:" - echo " fosgen [-d] [-l LANGUAGE] FILE" + echo " fosgen [-d] [-j] [-l LANGUAGE] FILE" echo echo "Writes to standard output the code generated from the fostr" echo "program in FILE, targeting the specified LANGUAGE (which" echo "defaults to Python)." echo 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 ;; -d) SUPPRESS_ERR='' ;; + -j) + USE_NAILGUN='' + ;; -l) shift LANGUAGE="$1" @@ -67,5 +73,17 @@ then exec 2>/dev/null 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 exit $? diff --git a/bin/generate_test_code b/bin/generate_test_code index 2c443d6..0d151a8 100755 --- a/bin/generate_test_code +++ b/bin/generate_test_code @@ -4,7 +4,7 @@ failed=0 for dir in tests/extracted/*; 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 ... bin/fosgen -l $language $file if [[ $? -ne 0 ]]; then diff --git a/bin/let_sun_shine b/bin/let_sun_shine new file mode 100755 index 0000000..cb3c67a --- /dev/null +++ b/bin/let_sun_shine @@ -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 diff --git a/editor/Generation.esv b/editor/Generation.esv index 3d71fe2..9e5051c 100644 --- a/editor/Generation.esv +++ b/editor/Generation.esv @@ -4,3 +4,4 @@ menus action: "Python" = to-python action: "Javascript" = to-javascript action: "Haskell" = to-haskell + action: "OCaml" = to-ocaml diff --git a/tests/basic.spt b/tests/basic.spt index 215c83f..a5e98c0 100644 --- a/tests/basic.spt +++ b/tests/basic.spt @@ -59,8 +59,8 @@ For example, this snippet generates the following Python: It generates nearly identical code in this simple example for Javascript (just with `"Hello, world!"` in place of `r'Hello, world!'`), although it generates a different -preamble defining Stdio for each language. (Currently, Haskell code -generation is also 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...", @@ -108,7 +108,7 @@ again. That way we can use the general (left-associative) parse to TopLevel( Gets(Gets(Gets(Gets(DefGets(LitString("'Two and '")),Int("2")), LitString("' make '")),Sum(Int("2"),Int("2"))), - EscString("\"./n\""))) + EscString("\".\n\""))) /** writes Two and 2 make 4. **/ diff --git a/trans/fostr.str b/trans/fostr.str index d9a585a..91194d6 100644 --- a/trans/fostr.str +++ b/trans/fostr.str @@ -6,6 +6,7 @@ imports pp outline analysis + ocaml haskell javascript python diff --git a/trans/haskell.str b/trans/haskell.str index 0e26050..0b08716 100644 --- a/trans/haskell.str +++ b/trans/haskell.str @@ -10,7 +10,7 @@ rules rule. 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 - (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 an (original) term and a term with value strings at each child, and be certain to return a value string. diff --git a/trans/ocaml.str b/trans/ocaml.str new file mode 100644 index 0000000..11fcdca --- /dev/null +++ b/trans/ocaml.str @@ -0,0 +1,61 @@ +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 } + let rec stdio = { + getS = (fun s -> print_string s; stdio) + };; + (* End of preamble *) + + [x]] + + ml: (_, Stream()) -> $[stdio] + ml: (_, Int(x)) -> x + ml: (_, LitString(x)) -> $[{|[x]|}] + ml: (_, EscString(x)) -> x + ml: (_, Sum(x,y)) -> $[[x] + [y]] + ml: (Gets(_,yn), Gets(x, y)) + -> $[([x]).getS ([(yn,y)])] + ml: (To(xn,_), To(x, y)) + -> $[let _fto = ([x]) in (ignore (([y]).getS ([(xn,"_fto")])); _fto)] + ml: (_, Terminate(x)) -> x + ml: (_, Sequence(l)) -> l + + ml_seq: [x] -> x + ml_seq: [x | xs ] -> $[ignore ([x]); +[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 x + + ml_str: (node, code) -> $[[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 := path + ; result := selected From 7feddbcfbedce0ed2623ccc6f7b338c41bdf78e7 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sat, 13 Mar 2021 19:30:23 +0000 Subject: [PATCH 10/10] Extraction from streams (#25) 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 Reviewed-on: https://code.studioinfinity.org/glen/fostr/pulls/25 Co-Authored-By: Glen Whitney Co-Committed-By: Glen Whitney --- bin/extract_tests.xsh | 8 ++++++++ bin/run_tests | 10 ++++++++-- mkdocs.yml | 2 +- syntax/fostr.sdf3 | 5 ++++- tests/basic.spt | 36 +++++++++++++++++++++++++++++++++++- trans/desugar.str | 1 + trans/haskell.str | 10 ++++++++++ trans/javascript.str | 23 ++++++++++++++++++++--- trans/ocaml.str | 11 ++++++++--- trans/python.str | 4 ++++ trans/statics.stx | 7 +++++++ 11 files changed, 106 insertions(+), 11 deletions(-) diff --git a/bin/extract_tests.xsh b/bin/extract_tests.xsh index 1d8c59f..422acb1 100644 --- a/bin/extract_tests.xsh +++ b/bin/extract_tests.xsh @@ -13,6 +13,9 @@ DESTINATION = 'tests/extracted' # Extension for extracted files: EXT = 'fos' +# Extension for desired input: +INP = 'in' + # Extension for expectations: EXP = 'expect' @@ -34,6 +37,11 @@ for path in TEST_LIST: expath = destdir / f"{name}.{EXT}" expath.write_text(example) 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():]) if xm: xpath = destdir / f"{name}.{EXP}" diff --git a/bin/run_tests b/bin/run_tests index 90ccaa7..da49485 100755 --- a/bin/run_tests +++ b/bin/run_tests @@ -9,8 +9,14 @@ diffed=0 for dir in tests/extracted/*; do for file in $dir/*.$ext; do ((total++)) - $command $file > $file.out - if [[ $? -ne 0 ]]; then + if [[ -f ${file%.*}.in ]]; 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. ((failed++)) else diff --git a/mkdocs.yml b/mkdocs.yml index e316a98..c2d5242 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -9,7 +9,7 @@ plugins: - search - semiliterate: ignore_folders: [target, lib] - exclude_extensions: ['.o', '.hi'] + exclude_extensions: ['.o', '.hi', '.cmi', '.cmo'] extract_standard_markdown: terminate: theme: diff --git a/syntax/fostr.sdf3 b/syntax/fostr.sdf3 index 8624b58..f782466 100644 --- a/syntax/fostr.sdf3 +++ b/syntax/fostr.sdf3 @@ -42,10 +42,13 @@ context-free syntax Ex.EscString = STRING Ex.Stream = Ex.Sum = < + > {left} + Ex.Concat = < ++ > {left} Ex.Gets = [[Ex] << [Ex]] {left} Ex.DefGets = [<<< [Ex]] Ex.To = [[Ex] >> [Ex]] {left} Ex.DefTo = [[Ex] >>>] + Ex.Emits = <!> + Ex.DefEmits = Ex = <()> {bracket} @@ -53,7 +56,7 @@ context-free priorities Ex.To > Ex.DefTo - > Ex.Sum + > {Ex.Sum Ex.Concat} > Ex.DefGets > Ex.Gets, diff --git a/tests/basic.spt b/tests/basic.spt index a5e98c0..3d5a4a6 100644 --- a/tests/basic.spt +++ b/tests/basic.spt @@ -293,4 +293,38 @@ test emit_several_default [[ >>> ]] parse succeeds /** 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. +**/ diff --git a/trans/desugar.str b/trans/desugar.str index 7276f38..b22ebd8 100644 --- a/trans/desugar.str +++ b/trans/desugar.str @@ -17,6 +17,7 @@ rules defStream: DefGets(x) -> Gets(Stream(), x) defStream: DefTo(x) -> To(x, Stream()) + defStream: DefEmits() -> Emits(Stream()) strategies diff --git a/trans/haskell.str b/trans/haskell.str index 0b08716..0e0eb15 100644 --- a/trans/haskell.str +++ b/trans/haskell.str @@ -23,6 +23,7 @@ rules import System.IO data IOStream = StdIO + -- Danger: These currently assume the stream is StdIO gets :: Show b => a -> b -> IO a gets s d = do putStr(show d) @@ -33,6 +34,10 @@ rules putStr(d) return s + emit s = do + l <- getLine + return (l ++ "\n") + main = do [()]return [val]] @@ -41,6 +46,7 @@ rules hs: (_, LitString(x)) -> x hs: (_, EscString(x)) -> x hs: (_, Sum(x, y)) -> $[([x] + [y])] + hs: (_, Concat(x, y)) -> $[([x] ++ [y])] hs: (Gets(_, xn), Gets(s, x)) -> v with v := "_fostr_get" @@ -52,6 +58,10 @@ rules hs_gets: (s, xn, x ) -> $[[s] [xn] [x]] hs_getOp = get-type; (?STRING() < !"`getsStr`" + !"`gets`") + hs: (_, Emits(s)) -> v + with v := "_fostr_emitted" + ; [$[[v] <- emit [s]]] + hs: (_, Terminate(x)) -> $[[x];;] hs: (_, Sequence(l)) -> l /* One drawback of using paramorphism is we have to handle lists diff --git a/trans/javascript.str b/trans/javascript.str index 3a847e2..9b596ad 100644 --- a/trans/javascript.str +++ b/trans/javascript.str @@ -3,24 +3,41 @@ imports libstrategolib signatures/- util rules 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 = { - 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) { strm.gets(data); return data; } + + const _fostr_body = async () => { // End of preamble - [x]] + [x] + + // Fostr coda + _fostr_rl.close() + } + _fostr_body(); + ] + with line := "[line]" js: Stream() -> $[Stdio] js: Int(x) -> x js: LitString(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: To(x, y) -> $[to([x],[y])] + js: Emits(x) -> $[(await [x].emit())] js: Terminate(x) -> x js: Sequence(l) -> l diff --git a/trans/ocaml.str b/trans/ocaml.str index 11fcdca..049dbf4 100644 --- a/trans/ocaml.str +++ b/trans/ocaml.str @@ -12,9 +12,10 @@ imports libstrategolib signatures/- util signature/TYPE analysis rules ml: (_, TopLevel(x)) -> $[(* fostr preamble *) - type stream = { getS: string -> stream } + type stream = { getS: string -> stream; emitS: unit -> string } let rec stdio = { - getS = (fun s -> print_string s; stdio) + getS = (fun s -> print_string s; stdio); + emitS = (fun () -> (read_line ()) ^ "\n"); };; (* End of preamble *) @@ -24,11 +25,15 @@ rules ml: (_, Int(x)) -> x ml: (_, LitString(x)) -> $[{|[x]|}] ml: (_, EscString(x)) -> x - ml: (_, Sum(x,y)) -> $[[x] + [y]] + ml: (_, Sum(x, y)) -> $[[x] + [y]] + ml: (_, Concat(x, y)) -> $[[x] ^ [y]] + ml: (Gets(_,yn), Gets(x, y)) -> $[([x]).getS ([(yn,y)])] ml: (To(xn,_), To(x, y)) -> $[let _fto = ([x]) in (ignore (([y]).getS ([(xn,"_fto")])); _fto)] + ml: (_, Emits(s)) -> $[[s].emitS ()] + ml: (_, Terminate(x)) -> x ml: (_, Sequence(l)) -> l diff --git a/trans/python.str b/trans/python.str index 6449417..f42b1cb 100644 --- a/trans/python.str +++ b/trans/python.str @@ -8,6 +8,8 @@ rules def gets(self, v): print(v, file=sys.stdout, end='') return self + def emit(self): + return input() + "\n" # Python inconsistently strips when using input def to(data,strm): strm.gets(data) return data @@ -21,8 +23,10 @@ rules py: LitString(x) -> $[r[x]] py: EscString(x) -> x py: Sum(x,y) -> $[[x] + [y]] + py: Concat(x,y) -> $[[x] + [y]] py: Gets(x, y) -> $[[x].gets([y])] py: To(x, y) -> $[to([x],[y])] + py: Emits(x) -> $[[x].emit()] py: Terminate(x) -> $[[x];] py: Sequence(l) -> l diff --git a/trans/statics.stx b/trans/statics.stx index 340a1df..ce99bf7 100644 --- a/trans/statics.stx +++ b/trans/statics.stx @@ -222,6 +222,13 @@ This pattern lets us specify error messages. 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 ```