From 8a0020b8d505e690e56a2414ef4db67d89ed44ce Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 19 Nov 2020 21:01:01 +0300 Subject: [PATCH 01/80] #164 fix --- .../java/org/eolang/compiler/Program.java | 4 ++- .../eolang/compiler/03-abstracts-float-up.xsl | 32 ++++++++++++------- .../compiler/errors/duplicate-names.xsl | 4 +-- .../packs/catches-name-duplicates.yaml | 4 ++- .../compiler/packs/flatten-abstracts.yaml | 13 +++++--- eo-maven-plugin/src/it/fibonacci/pom.xml | 19 +++++++++++ .../src/main/eo/org/eolang/examples/app.eo | 21 +++++++++--- .../main/java/org/eolang/examples/Main.java | 14 ++++++-- .../resources/org/eolang/maven/to-java.xsl | 14 ++++++-- 9 files changed, 95 insertions(+), 30 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index a1c34ca15c..a55137c803 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -102,6 +102,7 @@ public Program(final String nme, final Input ipt, final Output tgt) { public void compile() throws IOException { this.compile( new ListOf<>( + "errors/duplicate-names.xsl", "errors/broken-aliases.xsl", "errors/duplicate-aliases.xsl", "errors/one-body.xsl", @@ -113,7 +114,8 @@ public void compile() throws IOException { "errors/unknown-names.xsl", "03-abstracts-float-up.xsl", "04-rename-bases.xsl", - "05-wrap-method-calls.xsl" + "05-wrap-method-calls.xsl", + "errors/duplicate-names.xsl" ) ); } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl index ed1a17c93f..72a87e6b4a 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl @@ -28,6 +28,25 @@ SOFTWARE. + + + + + + + + + + $ + + + + $ + + + + + @@ -53,11 +72,7 @@ SOFTWARE. - - - $ - - + @@ -93,12 +108,7 @@ SOFTWARE. - - - - $ - - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl index 610c7f887c..9f5a292104 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl @@ -26,12 +26,12 @@ SOFTWARE. - + - + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml index 00e5b79a1c..158425ed73 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml @@ -1,9 +1,11 @@ xsls: - errors/duplicate-names.xsl tests: - - /program/errors[count(error)=1] + - /program/errors[count(error)=2] - /program/errors/error[@line='2'] - /program/objects/o[@name='first']/o[@name='x'] eo: | [x] > first second > x + + 18 > first diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml index b37a031a1a..3cb9f74f7f 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml @@ -2,13 +2,15 @@ xsls: - 01-add-refs.xsl - 03-abstracts-float-up.xsl - 04-rename-bases.xsl + - errors/duplicate-names.xsl tests: - - /program/objects[count(o)=3] + - /program/objects[count(o)=4] + - /program/errors[count(*)=0] - //o[@name='first'] - //o[@name='first']/o[@base='test' and @as='foo'] - - //o[@name='first$second'] - - //o[@name='first$second$third'] - - //o[@name='first$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] + - //o[@name='first$$second'] + - //o[@name='first$$second$third'] + - //o[@name='first$$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] eo: | [a b] > first test:foo @@ -18,3 +20,6 @@ eo: | [b c] > third no > yes (stdout a b c x).print + t + [] > third + "hello, world!" > msg diff --git a/eo-maven-plugin/src/it/fibonacci/pom.xml b/eo-maven-plugin/src/it/fibonacci/pom.xml index d81288a1c7..4c20d65505 100644 --- a/eo-maven-plugin/src/it/fibonacci/pom.xml +++ b/eo-maven-plugin/src/it/fibonacci/pom.xml @@ -49,6 +49,25 @@ SOFTWARE. + + org.codehaus.mojo + exec-maven-plugin + + + test + + java + + + + + org.eolang.examples.Main + + 14 + 377 + + + diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo b/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo index 39f16a77c1..44b0e3ff2b 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo +++ b/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo @@ -26,8 +26,19 @@ +alias sprintf org.eolang.txt.sprintf [args] > app - stdout - sprintf - "%dth Fibonacci number is %d" - (args.get 1) > !n - fibo n + and + stdout + sprintf + "%dth Fibonacci number is %d" + (args.get 0) > !n + fibo n + if + not + eq + args.get 1 > e + n + [] + sprintf > msg + "%d not equal to %d" + n + e diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java index ee77e956ef..4d7d6493ef 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java +++ b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java @@ -29,7 +29,7 @@ import org.eolang.sys.Entry; /** - * Eo entry point. + * EO entry point. * * @since 0.1 */ @@ -43,9 +43,17 @@ public final class Main { public static void main(final String... args) throws Exception { new EOapp( new ArgsOf( - new Entry("args", new EOarray(new ArgsOf((Object[]) args))) + new Entry( + "args", + new EOarray( + new ArgsOf( + new Entry("01", Long.parseLong(args[0])), + new Entry("02", Long.parseLong(args[1])) + ) + ) + ) ) - ).call(); + ).call() } } diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index c88e90e94b..75acd7bcde 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -178,9 +178,17 @@ SOFTWARE. - this.args.get(" - - ") + + + + + + + this.args.get(" + + ") + + From 17d0369b90ad9fa21f97a2ad0577068e9a709417 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 19 Nov 2020 21:21:04 +0300 Subject: [PATCH 02/80] #164: exceptions --- .../java/org/eolang/compiler/Program.java | 1 + .../eolang/compiler/03-abstracts-float-up.xsl | 9 +--- .../resources/org/eolang/maven/to-java.xsl | 42 +++++++++++++++---- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index a55137c803..004d28b0bc 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -99,6 +99,7 @@ public Program(final String nme, final Input ipt, final Output tgt) { * * @throws IOException If fails */ + @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void compile() throws IOException { this.compile( new ListOf<>( diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl index 72a87e6b4a..92aba5a8d7 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl @@ -30,7 +30,6 @@ SOFTWARE. - @@ -40,10 +39,6 @@ SOFTWARE. $ - - $ - - @@ -72,7 +67,7 @@ SOFTWARE. - + @@ -108,7 +103,7 @@ SOFTWARE. - + diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 75acd7bcde..5cff65ce2e 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -71,10 +71,7 @@ SOFTWARE. public final class - - implements Phi - - { + implements Phi { private final Args args; @@ -99,6 +96,37 @@ SOFTWARE. } + + @Override + + + + public Object call() throws Exception { + + + + throw new RuntimeException( + + + + + + + this.msg().call().toString() + + + String.format("Runtime exception at %s", this.getClass()) + + + + + + ); + + + } + + @@ -141,8 +169,8 @@ SOFTWARE. - public Object - + public Phi + (final Args a) @@ -157,7 +185,7 @@ SOFTWARE. - Object + Phi ; From fc648912f38e53ba09056f67a16cb5dde7bb4734 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 19 Nov 2020 22:15:59 +0300 Subject: [PATCH 03/80] #164: more --- .../eolang/compiler/03-abstracts-float-up.xsl | 19 +++++++++++++------ .../compiler/packs/flatten-abstracts.yaml | 14 ++++++++++---- .../eolang/compiler/packs/full-syntax.yaml | 1 + .../main/java/org/eolang/examples/Main.java | 2 +- .../resources/org/eolang/maven/to-java.xsl | 2 +- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl index 92aba5a8d7..aa104a16e4 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl @@ -32,9 +32,14 @@ SOFTWARE. - - - + + + + + + + + $ @@ -69,9 +74,11 @@ SOFTWARE. - - - + + + + + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml index 3cb9f74f7f..e978160b30 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml @@ -4,13 +4,14 @@ xsls: - 04-rename-bases.xsl - errors/duplicate-names.xsl tests: - - /program/objects[count(o)=4] + - /program/objects[count(o)=6] - /program/errors[count(*)=0] + - //objects[not(.//o[@name=''])] - //o[@name='first'] - //o[@name='first']/o[@base='test' and @as='foo'] - - //o[@name='first$$second'] - - //o[@name='first$$second$third'] - - //o[@name='first$$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] + - //o[@name='first$2$second'] + - //o[@name='first$2$second$third'] + - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] eo: | [a b] > first test:foo @@ -23,3 +24,8 @@ eo: | t [] > third "hello, world!" > msg + f + [] > third + "hello, world!" > msg + [] + "Failure" diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 7616c6e108..2d61387b61 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -13,6 +13,7 @@ tests: - //o[@name='hello' and @const] - //o[@as='i'] - //o[@base='.five'] + - //objects[not(.//o[@name=''])] eo: | # The purpose of this test case is to make # sure all possible syntax scenarios can diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java index 4d7d6493ef..9e5cb69a83 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java +++ b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java @@ -53,7 +53,7 @@ public static void main(final String... args) throws Exception { ) ) ) - ).call() + ).call(); } } diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 5cff65ce2e..a09a1954b6 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -97,10 +97,10 @@ SOFTWARE. } + @Override - public Object call() throws Exception { From 30783310764a75c58426c81fff6d9ff76f6d5a5b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 20 Nov 2020 20:45:06 +0300 Subject: [PATCH 04/80] #164: more --- .../java/org/eolang/compiler/Program.java | 13 ++-- .../{01-add-refs.xsl => 20-add-refs.xsl} | 0 ...lve-aliases.xsl => 30-resolve-aliases.xsl} | 0 ...float-up.xsl => 40-abstracts-float-up.xsl} | 0 .../org/eolang/compiler/42-vars-float-up.xsl | 66 +++++++++++++++++++ ...4-rename-bases.xsl => 50-rename-bases.xsl} | 0 ...hod-calls.xsl => 60-wrap-method-calls.xsl} | 2 +- .../org/eolang/compiler/errors/one-body.xsl | 2 +- .../java/org/eolang/compiler/PacksTest.java | 8 ++- .../org/eolang/compiler/packs/adds-refs.yaml | 3 +- .../compiler/packs/catches-two-bodies.yaml | 4 ++ .../compiler/packs/flatten-abstracts.yaml | 6 +- .../org/eolang/compiler/packs/float-vars.yaml | 15 +++++ .../org/eolang/compiler/packs/leap-year.yaml | 15 +---- .../compiler/packs/resolves-aliases.yaml | 5 +- .../org/eolang/compiler/packs/simple.yaml | 1 + .../eolang/compiler/packs/wraps-methods.yaml | 3 +- .../main/java/org/eolang/maven/ToJava.java | 12 +++- .../resources/org/eolang/maven/to-java.xsl | 2 +- 19 files changed, 126 insertions(+), 31 deletions(-) rename eo-compiler/src/main/resources/org/eolang/compiler/{01-add-refs.xsl => 20-add-refs.xsl} (100%) rename eo-compiler/src/main/resources/org/eolang/compiler/{02-resolve-aliases.xsl => 30-resolve-aliases.xsl} (100%) rename eo-compiler/src/main/resources/org/eolang/compiler/{03-abstracts-float-up.xsl => 40-abstracts-float-up.xsl} (100%) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl rename eo-compiler/src/main/resources/org/eolang/compiler/{04-rename-bases.xsl => 50-rename-bases.xsl} (100%) rename eo-compiler/src/main/resources/org/eolang/compiler/{05-wrap-method-calls.xsl => 60-wrap-method-calls.xsl} (94%) create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 004d28b0bc..19ece62c8b 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -110,12 +110,15 @@ public void compile() throws IOException { "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", - "01-add-refs.xsl", - "02-resolve-aliases.xsl", + "20-add-refs.xsl", + "30-resolve-aliases.xsl", + "40-abstracts-float-up.xsl", + "50-rename-bases.xsl", + "60-wrap-method-calls.xsl", + "42-vars-float-up.xsl", + "20-add-refs.xsl", + "50-rename-bases.xsl", "errors/unknown-names.xsl", - "03-abstracts-float-up.xsl", - "04-rename-bases.xsl", - "05-wrap-method-calls.xsl", "errors/duplicate-names.xsl" ) ); diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/01-add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl similarity index 100% rename from eo-compiler/src/main/resources/org/eolang/compiler/01-add-refs.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/02-resolve-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl similarity index 100% rename from eo-compiler/src/main/resources/org/eolang/compiler/02-resolve-aliases.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl similarity index 100% rename from eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl new file mode 100644 index 0000000000..f0d3f77de0 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/04-rename-bases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl similarity index 100% rename from eo-compiler/src/main/resources/org/eolang/compiler/04-rename-bases.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl similarity index 94% rename from eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl index a9aa2f7157..230a51bfc0 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl index 6881809e55..19ef7503e5 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl @@ -32,7 +32,7 @@ SOFTWARE. - + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 6b1376925e..96fdf987f3 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -59,7 +59,12 @@ public void testPacks(final String pack) throws Exception { new InputOf(String.format("%s\n", map.get("eo"))), new OutputTo(baos) ); - program.compile((Iterable) map.get("xsls")); + final Collection xsls = (Collection) map.get("xsls"); + if (xsls == null) { + program.compile(); + } else { + program.compile(xsls); + } for (final String xpath : (Iterable) map.get("tests")) { MatcherAssert.assertThat( XhtmlMatchers.xhtml(baos.toString()), @@ -82,6 +87,7 @@ private static Collection yamlPacks() { "catches-two-bodies.yaml", "catches-same-line-name.yaml", "adds-refs.yaml", + "float-vars.yaml", "flatten-abstracts.yaml", "resolves-aliases.yaml", "wraps-methods.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml index 880a8af5fc..7266fb666c 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml @@ -2,8 +2,9 @@ xsls: - errors/duplicate-names.xsl - errors/self-naming.xsl - errors/same-line-names.xsl - - 01-add-refs.xsl + - 20-add-refs.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=2] - //o[@base='x' and @ref='1'] - //o[@base='x' and @ref='4'] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml index 08ee6b74ea..d4fb4724b9 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml @@ -12,3 +12,7 @@ eo: | third > hey 23 33 + + [args] > main + (stdout "Hello!").print + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml index e978160b30..a48137cac9 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml @@ -1,7 +1,7 @@ xsls: - - 01-add-refs.xsl - - 03-abstracts-float-up.xsl - - 04-rename-bases.xsl + - 20-add-refs.xsl + - 40-abstracts-float-up.xsl + - 50-rename-bases.xsl - errors/duplicate-names.xsl tests: - /program/objects[count(o)=6] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml new file mode 100644 index 0000000000..dffef3d036 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -0,0 +1,15 @@ +xsls: + - 42-vars-float-up.xsl + - errors/duplicate-names.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=1] + - //o[@base='input' and @name='t' and @line='4' and @const and count(o)=1] + - //objects[count(.//o[@base='string'])=1] +eo: | + [] > x + one + two + input > !t + "Hello, world!" + three t diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml index 0b94345573..550e651a0d 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml @@ -1,18 +1,5 @@ -xsls: - - errors/reserved-atoms.xsl - - errors/self-naming.xsl - - errors/same-line-names.xsl - - errors/one-body.xsl - - errors/duplicate-aliases.xsl - - errors/broken-aliases.xsl - - errors/duplicate-names.xsl - - 01-add-refs.xsl - - 02-resolve-aliases.xsl - - errors/unknown-names.xsl - - 03-abstracts-float-up.xsl - - 04-rename-bases.xsl - - 05-wrap-method-calls.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=2] eo: | # This program is from the EOLANG original paper diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml index ae3b2e2490..6cbd489ced 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml @@ -1,7 +1,8 @@ xsls: - - 01-add-refs.xsl - - 02-resolve-aliases.xsl + - 20-add-refs.xsl + - 30-resolve-aliases.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=1] - /program/metas/meta[head='foo' and @line='4'] - /program/metas[count(meta)=1] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml index 43d8c6739b..c50dba8985 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml @@ -1,5 +1,6 @@ xsls: [] tests: + - /program/errors[count(*)=0] - /program[@name='simple.yaml'] - /program[@version != ''] - /program[@time != ''] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 631a2f2e35..1d1aa17d15 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -1,6 +1,7 @@ xsls: - - 05-wrap-method-calls.xsl + - 60-wrap-method-calls.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=1] - //o[@base='sprintf' and count(./o)=3] - //o[@base='.get' and count(./o)=2] diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java index ae13831ba0..9c16fcc256 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.List; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -94,7 +95,8 @@ public void compile() throws IOException { ) ) ).value(); - for (final XML error : out.nodes("/program/errors/error")) { + final List errors = out.nodes("/program/errors/error"); + for (final XML error : errors) { Logger.error( this, "[%s] %s", @@ -102,6 +104,14 @@ public void compile() throws IOException { error.xpath("text()").get(0) ); } + if (!errors.isEmpty()) { + throw new IllegalStateException( + String.format( + "There are %d errors, see log above", + errors.size() + ) + ); + } for (final XML file : out.nodes("/program/objects/o[java]")) { ToJava.save( this.dir.resolve( diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index a09a1954b6..a64e2ae127 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -56,7 +56,7 @@ SOFTWARE. - + From 521fa1f4284fceadb46c11a6816061d46b66336b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 20 Nov 2020 22:50:30 +0300 Subject: [PATCH 05/80] #164: spy --- .../java/org/eolang/compiler/Program.java | 82 +++++++++++++++---- .../java/org/eolang/maven/CompileMojo.java | 27 ++++-- .../resources/org/eolang/maven/to-java.xsl | 8 +- 3 files changed, 95 insertions(+), 22 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 19ece62c8b..974195e4b9 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -24,7 +24,8 @@ package org.eolang.compiler; import com.jcabi.log.Logger; -import com.jcabi.xml.XSLChain; +import com.jcabi.xml.XML; +import com.jcabi.xml.XSL; import com.jcabi.xml.XSLDocument; import java.io.IOException; import java.nio.file.Path; @@ -42,7 +43,6 @@ import org.cactoos.io.TeeInput; import org.cactoos.io.UncheckedInput; import org.cactoos.list.ListOf; -import org.cactoos.list.Mapped; import org.cactoos.scalar.LengthOf; import org.cactoos.scalar.Unchecked; import org.cactoos.text.TextOf; @@ -94,13 +94,22 @@ public Program(final String nme, final Input ipt, final Output tgt) { this.target = tgt; } + /** + * Compile it to XML and save (with default set of XSLs). + * @throws IOException If fails + */ + public void compile() throws IOException { + this.compile(new Program.Spy.None()); + } + /** * Compile it to XML and save (with default set of XSLs). * + * @param spy The spy * @throws IOException If fails */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void compile() throws IOException { + public void compile(final Program.Spy spy) throws IOException { this.compile( new ListOf<>( "errors/duplicate-names.xsl", @@ -111,16 +120,16 @@ public void compile() throws IOException { "errors/same-line-names.xsl", "errors/self-naming.xsl", "20-add-refs.xsl", - "30-resolve-aliases.xsl", "40-abstracts-float-up.xsl", - "50-rename-bases.xsl", "60-wrap-method-calls.xsl", "42-vars-float-up.xsl", "20-add-refs.xsl", "50-rename-bases.xsl", + "30-resolve-aliases.xsl", "errors/unknown-names.xsl", "errors/duplicate-names.xsl" - ) + ), + spy ); } @@ -131,6 +140,18 @@ public void compile() throws IOException { * @throws IOException If fails */ public void compile(final Iterable xsls) throws IOException { + this.compile(xsls, new Program.Spy.None()); + } + + /** + * Compile it to XML and save. + * + * @param xsls List of XSLs to apply + * @param spy The spy + * @throws IOException If fails + */ + public void compile(final Iterable xsls, + final Program.Spy spy) throws IOException { final String[] lines = new TextOf(this.input).asString().split("\n"); final ANTLRErrorListener errors = new BaseErrorListener() { // @checkstyle ParameterNumberCheck (10 lines) @@ -166,19 +187,21 @@ public void syntaxError(final Recognizer recognizer, parser.addErrorListener(errors); final XeListener xel = new XeListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); + XML dom = xel.xml(); + int index = 0; + for (final String doc : xsls) { + final XSL xsl = new XSLDocument( + Program.class.getResourceAsStream(doc) + ); + final XML after = xsl.transform(dom); + spy.push(index, doc, after); + ++index; + dom = after; + } new Unchecked<>( new LengthOf( new TeeInput( - new InputOf( - new XSLChain( - new Mapped<>( - node -> new XSLDocument( - Program.class.getResourceAsStream(node) - ), - xsls - ) - ).transform(xel.xml()).toString() - ), + new InputOf(dom.toString()), this.target ) ) @@ -186,4 +209,31 @@ public void syntaxError(final Recognizer recognizer, Logger.debug(this, "Input of %d EO lines compiled", lines.length); } + /** + * Spy. + * + * @since 0.1 + */ + public interface Spy { + /** + * New XSL produced. + * @param index The index of the XSL + * @param xsl The name of XSL + * @param xml The XML produced + */ + void push(int index, String xsl, XML xml); + + /** + * Empty spy. + * + * @since 0.1 + */ + final class None implements Program.Spy { + @Override + public void push(final int index, final String xsl, final XML xml) { + Logger.debug(this, "Parsed #%d via %s", index, xsl); + } + } + } + } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 30fccf22bc..4362e0d5d2 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -43,6 +43,7 @@ import org.cactoos.io.TeeInput; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; import org.eolang.compiler.Program; @@ -163,11 +164,27 @@ private void compile(final Path file) { ) ).value(); baos.reset(); - new Program( - name, - new InputOf(file), - new OutputTo(baos) - ).compile(); + new Program(name, new InputOf(file), new OutputTo(baos)).compile( + (index, xsl, dom) -> new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(dom.toString()), + new OutputTo( + this.targetDir.toPath() + .resolve("eo-compiler-steps") + .resolve(xml) + .resolve( + String.format( + "%02d-%s.xml", + index, + xsl.replaceAll("[^a-z0-9]", "-") + ) + ) + ) + ) + ) + ).value() + ); new IoChecked<>( new LengthOf( new TeeInput( diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index a64e2ae127..6b49027f50 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -23,6 +23,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + + + + @@ -53,7 +57,7 @@ SOFTWARE. - + @@ -209,7 +213,9 @@ SOFTWARE. + this. + (args) this.args.get(" From a73308a6603616401197cd36ed705df855499bbf Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 20 Nov 2020 23:03:32 +0300 Subject: [PATCH 06/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 6b49027f50..cee76d92c9 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -116,7 +116,7 @@ SOFTWARE. - this.msg().call().toString() + this.msg(new ArgsOf()).call().toString() String.format("Runtime exception at %s", this.getClass()) @@ -186,14 +186,6 @@ SOFTWARE. { - - - - Phi - - ; - - return @@ -226,10 +218,6 @@ SOFTWARE. - - - = - new Call(" ", @@ -255,10 +243,6 @@ SOFTWARE. - - - = - new (new ArgsOf( From 08bc35a58ebabc4d9b89d6d3ab0a79a7150b0b54 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 16:38:37 +0300 Subject: [PATCH 07/80] #164: more --- .../main/java/org/eolang/compiler/Pack.java | 102 ++++++++++++++++++ .../java/org/eolang/compiler/Program.java | 41 ++----- .../org/eolang/compiler/20-add-refs.xsl | 2 +- .../java/org/eolang/compiler/PacksTest.java | 2 +- 4 files changed, 112 insertions(+), 35 deletions(-) create mode 100644 eo-compiler/src/main/java/org/eolang/compiler/Pack.java diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java new file mode 100644 index 0000000000..c8598e6068 --- /dev/null +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -0,0 +1,102 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.compiler; + +import com.jcabi.xml.XSL; +import com.jcabi.xml.XSLDocument; +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import org.cactoos.list.ListOf; +import org.cactoos.list.Mapped; + +/** + * Pack of all XSL sheets. + * + * @since 0.1 + */ +public final class Pack implements Iterable { + + /** + * All of them. + */ + private final Collection sheets; + + /** + * Ctor, with the default sequence of sheets. + */ + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public Pack() { + this( + new ListOf<>( + "errors/duplicate-names.xsl", + "errors/broken-aliases.xsl", + "errors/duplicate-aliases.xsl", + "errors/one-body.xsl", + "errors/reserved-atoms.xsl", + "errors/same-line-names.xsl", + "errors/self-naming.xsl", + "20-add-refs.xsl", + "40-abstracts-float-up.xsl", + "60-wrap-method-calls.xsl", + "42-vars-float-up.xsl", + "20-add-refs.xsl", + "50-rename-bases.xsl", + "30-resolve-aliases.xsl", + "errors/unknown-names.xsl", + "errors/duplicate-names.xsl" + ) + ); + } + + /** + * Ctor. + * + * @param names Names of them in classpath + */ + public Pack(final Collection names) { + this.sheets = names; + } + + @Override + public Iterator iterator() { + return new Mapped( + doc -> new XSLDocument( + Program.class.getResourceAsStream(doc) + ), + this.sheets + ).iterator(); + } + + /** + * Make a copy, with an additional XSL. + * @param xsl Name in classpath + * @return New pack + */ + public Pack with(final String xsl) { + final Collection after = new LinkedList<>(this.sheets); + after.add(xsl); + return new Pack(after); + } +} diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 974195e4b9..037455f17b 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -26,7 +26,6 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; import com.jcabi.xml.XSL; -import com.jcabi.xml.XSLDocument; import java.io.IOException; import java.nio.file.Path; import org.antlr.v4.runtime.ANTLRErrorListener; @@ -42,7 +41,6 @@ import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; import org.cactoos.io.UncheckedInput; -import org.cactoos.list.ListOf; import org.cactoos.scalar.LengthOf; import org.cactoos.scalar.Unchecked; import org.cactoos.text.TextOf; @@ -110,27 +108,7 @@ public void compile() throws IOException { */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void compile(final Program.Spy spy) throws IOException { - this.compile( - new ListOf<>( - "errors/duplicate-names.xsl", - "errors/broken-aliases.xsl", - "errors/duplicate-aliases.xsl", - "errors/one-body.xsl", - "errors/reserved-atoms.xsl", - "errors/same-line-names.xsl", - "errors/self-naming.xsl", - "20-add-refs.xsl", - "40-abstracts-float-up.xsl", - "60-wrap-method-calls.xsl", - "42-vars-float-up.xsl", - "20-add-refs.xsl", - "50-rename-bases.xsl", - "30-resolve-aliases.xsl", - "errors/unknown-names.xsl", - "errors/duplicate-names.xsl" - ), - spy - ); + this.compile(new Pack(), spy); } /** @@ -139,7 +117,7 @@ public void compile(final Program.Spy spy) throws IOException { * @param xsls List of XSLs to apply * @throws IOException If fails */ - public void compile(final Iterable xsls) throws IOException { + public void compile(final Iterable xsls) throws IOException { this.compile(xsls, new Program.Spy.None()); } @@ -150,7 +128,7 @@ public void compile(final Iterable xsls) throws IOException { * @param spy The spy * @throws IOException If fails */ - public void compile(final Iterable xsls, + public void compile(final Iterable xsls, final Program.Spy spy) throws IOException { final String[] lines = new TextOf(this.input).asString().split("\n"); final ANTLRErrorListener errors = new BaseErrorListener() { @@ -189,12 +167,9 @@ public void syntaxError(final Recognizer recognizer, new ParseTreeWalker().walk(xel, parser.program()); XML dom = xel.xml(); int index = 0; - for (final String doc : xsls) { - final XSL xsl = new XSLDocument( - Program.class.getResourceAsStream(doc) - ); + for (final XSL xsl : xsls) { final XML after = xsl.transform(dom); - spy.push(index, doc, after); + spy.push(index, xsl, after); ++index; dom = after; } @@ -218,10 +193,10 @@ public interface Spy { /** * New XSL produced. * @param index The index of the XSL - * @param xsl The name of XSL + * @param xsl The XSL used * @param xml The XML produced */ - void push(int index, String xsl, XML xml); + void push(int index, XSL xsl, XML xml); /** * Empty spy. @@ -230,7 +205,7 @@ public interface Spy { */ final class None implements Program.Spy { @Override - public void push(final int index, final String xsl, final XML xml) { + public void push(final int index, final XSL xsl, final XML xml) { Logger.debug(this, "Parsed #%d via %s", index, xsl); } } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl index 1d34a4d568..17ffd3b2ec 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 96fdf987f3..87b702e6a4 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -63,7 +63,7 @@ public void testPacks(final String pack) throws Exception { if (xsls == null) { program.compile(); } else { - program.compile(xsls); + program.compile(new Pack(xsls)); } for (final String xpath : (Iterable) map.get("tests")) { MatcherAssert.assertThat( From 9e675e082cbb307967abeab547fe45b48fa52ada Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 16:51:40 +0300 Subject: [PATCH 08/80] #164: packs --- .../org/eolang/compiler/20-add-refs.xsl | 2 +- .../eolang/compiler/30-resolve-aliases.xsl | 2 +- .../eolang/compiler/40-abstracts-float-up.xsl | 2 +- .../java/org/eolang/maven/CompileMojo.java | 59 +++++++++++++------ 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl index 17ffd3b2ec..ad88d316f7 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl index d360030680..b1090c2f65 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl index aa104a16e4..14731db414 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 4362e0d5d2..00cd08b4bf 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -24,13 +24,16 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; +import com.jcabi.xml.XSL; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.List; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -165,25 +168,7 @@ private void compile(final Path file) { ).value(); baos.reset(); new Program(name, new InputOf(file), new OutputTo(baos)).compile( - (index, xsl, dom) -> new Unchecked<>( - new LengthOf( - new TeeInput( - new InputOf(dom.toString()), - new OutputTo( - this.targetDir.toPath() - .resolve("eo-compiler-steps") - .resolve(xml) - .resolve( - String.format( - "%02d-%s.xml", - index, - xsl.replaceAll("[^a-z0-9]", "-") - ) - ) - ) - ) - ) - ).value() + new CompileMojo.TargetSpy(xml) ); new IoChecked<>( new LengthOf( @@ -216,4 +201,40 @@ private void compile(final Path file) { Logger.info(this, "%s compiled to %s", file, this.generatedDir); } + /** + * The spy to log all results. + */ + private final class TargetSpy implements Program.Spy { + private final Path dir; + private TargetSpy(final String xml) { + this.dir = CompileMojo.this.targetDir.toPath() + .resolve("eo-compiler-steps") + .resolve(xml); + } + @Override + public void push(final int index, final XSL xsl, final XML xml) { + final List names = new XMLDocument( + xsl.toString() + ).xpath("/*/@id"); + final String file; + if (names.isEmpty()) { + file = String.format("%d", index); + } else { + file = names.get(0).replaceAll("[^a-z0-9]", "-"); + } + new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(xml.toString()), + new OutputTo( + this.dir.resolve( + String.format("%02d-%s.xml", index, file) + ) + ) + ) + ) + ).value(); + } + } + } From 9c0c0673d706b97cc7067f22fe27560aa1ef9d66 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 18:06:49 +0300 Subject: [PATCH 09/80] #164: const syntax changed --- .../antlr4/org/eolang/compiler/Program.g4 | 2 +- .../org/eolang/compiler/packs/float-vars.yaml | 2 +- .../eolang/compiler/packs/full-syntax.yaml | 4 ++-- .../org/eolang/compiler/packs/leap-year.yaml | 2 +- .../compiler/packs/resolves-aliases.yaml | 2 +- .../eolang/compiler/packs/wraps-methods.yaml | 2 +- .../java/org/eolang/maven/ToJavaTest.java | 4 ++-- .../test/resources/org/eolang/maven/fibo.eo | 23 ------------------- .../test/resources/org/eolang/maven/test.eo | 18 +++++++++++++++ 9 files changed, 27 insertions(+), 32 deletions(-) delete mode 100644 eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo create mode 100644 eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index 56c009ac13..893fae7687 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -86,8 +86,8 @@ suffix SPACE ARROW SPACE - CONST? NAME + CONST? ; method diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index dffef3d036..6b59e9945d 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -10,6 +10,6 @@ eo: | [] > x one two - input > !t + input > t! "Hello, world!" three t diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 2d61387b61..1b7cf60deb 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -26,13 +26,13 @@ eo: | 500.43 > one # This is just a simple string - "Hello, world!" > !hello + "Hello, world!" > hello! [x] > first second > hello third:foo > x f 12 false - (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > !a + (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! kid f:u z diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml index 550e651a0d..060068d82b 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml @@ -22,6 +22,6 @@ eo: | stdout "Enter a year:" stdout concat - (scanner stdin).nextInt > !year + (scanner stdin).nextInt > year! " is a leap year?" leap year:y diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml index 6cbd489ced..03605e6e2a 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml @@ -16,5 +16,5 @@ eo: | [args] > main and - (scanner stdin).nextLine > !line + (scanner stdin).nextLine > line! (stdout "You entered" line).print diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 1d1aa17d15..085fb599fc 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -12,5 +12,5 @@ eo: | stdout sprintf "%dth Fibonacci number is %d" - (args.get 1) > !n + (args.get 1) > n! fibo n diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java index e6db1d0f92..b96d708c53 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java @@ -59,7 +59,7 @@ public void compilesSimpleCodeToJava() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Program program = new Program( "test", - new ResourceOf("org/eolang/maven/fibo.eo"), + new ResourceOf("org/eolang/maven/test.eo"), new OutputTo(baos) ); program.compile(); @@ -70,7 +70,7 @@ public void compilesSimpleCodeToJava() throws Exception { this.temp.resolve("eo-to-java") ); tojava.compile(); - final Path file = this.temp.resolve(Paths.get("generated/EOfibo.java")); + final Path file = this.temp.resolve(Paths.get("eo-to-java/test.xml")); MatcherAssert.assertThat( Files.exists(file), Matchers.is(true) diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo deleted file mode 100644 index e9dc598068..0000000000 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo +++ /dev/null @@ -1,23 +0,0 @@ -# This is the header comment of this simple EO -# program, to be ignored. - -+package org.eolang.examples -+alias stdout org.eolang.io.stdout -+alias sprintf org.eolang.txt.sprintf -+meta This is just a test META -+meta2 And yet another one - -[n] > fibo - if - less n 2 - n - add - fibo (sub n 1) - fibo (sub n 2) - -[args] > app - stdout - sprintf - "%dth Fibonacci number is %d" - (args.get 1) > !n - fibo n diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo new file mode 100644 index 0000000000..18637a0b2e --- /dev/null +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo @@ -0,0 +1,18 @@ +# This is the header comment of this simple EO +# program, to be ignored. + ++package org.eolang.examples ++alias stdout org.eolang.io.stdout + +3.14 > pi + +[n] > alpha + a pi 666 > x + one + two x + body + "Hello, world!" > f! + +[] + alpha + "I love you" From 70c65c55e430fd7a511fae68b651c0c7444407a9 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 18:22:02 +0300 Subject: [PATCH 10/80] #164: funcs.xsl --- README.md | 2 +- .../main/java/org/eolang/compiler/Pack.java | 5 ++-- .../eolang/compiler/40-abstracts-float-up.xsl | 5 +--- .../org/eolang/compiler/42-vars-float-up.xsl | 7 ++--- .../org/eolang/compiler/50-rename-bases.xsl | 7 ++--- .../eolang/compiler/60-wrap-method-calls.xsl | 2 +- .../eolang/compiler/errors/broken-aliases.xsl | 2 +- .../compiler/errors/duplicate-aliases.xsl | 2 +- .../compiler/errors/duplicate-names.xsl | 2 +- .../org/eolang/compiler/errors/one-body.xsl | 2 +- .../eolang/compiler/errors/reserved-atoms.xsl | 2 +- .../compiler/errors/same-line-names.xsl | 2 +- .../eolang/compiler/errors/self-naming.xsl | 2 +- .../eolang/compiler/errors/unknown-names.xsl | 2 +- .../resources/org/eolang/compiler/funcs.xsl | 30 +++++++++++++++++++ eo-maven-plugin/pom.xml | 5 ++++ .../src/main/eo/org/eolang/examples/app.eo | 2 +- .../resources/org/eolang/maven/to-java.xsl | 6 ++-- .../java/org/eolang/maven/ToJavaTest.java | 5 ++++ .../test/resources/org/eolang/maven/test.eo | 2 +- 20 files changed, 62 insertions(+), 32 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl diff --git a/README.md b/README.md index 8210ea51ba..38268304c0 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ whether it's leap or not: stdout "Enter a year:" stdout concat - (scanner stdin).nextInt > !year + (scanner stdin).nextInt > year! " is a leap year?" leap year:y ``` diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index c8598e6068..b7479bc83f 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -23,6 +23,7 @@ */ package org.eolang.compiler; +import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XSL; import com.jcabi.xml.XSLDocument; import java.util.Collection; @@ -81,10 +82,10 @@ public Pack(final Collection names) { @Override public Iterator iterator() { - return new Mapped( + return new Mapped<>( doc -> new XSLDocument( Program.class.getResourceAsStream(doc) - ), + ).with(new ClasspathSources()), this.sheets ).iterator(); } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl index 14731db414..f19f0be5d9 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl @@ -24,10 +24,7 @@ SOFTWARE. --> - - - - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl index f0d3f77de0..7199185829 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl @@ -22,12 +22,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + - - - - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl index 0632102fd4..15fcb25006 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl @@ -22,12 +22,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + - - - - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl index 230a51bfc0..38d13a78ed 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl index 32ed949515..8c372620e6 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl index d147063820..51903d2c8c 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl index 9f5a292104..e18a85f07a 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl index 19ef7503e5..6f4c5467fa 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl index 4cb6ad23eb..7efca3d49c 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl index 39bd0767c0..39592421b6 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl index 5ea60abc4a..b395a5e82e 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl index d613960b23..19098e3111 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl new file mode 100644 index 0000000000..e262cc18b5 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl @@ -0,0 +1,30 @@ + + + + + + + + diff --git a/eo-maven-plugin/pom.xml b/eo-maven-plugin/pom.xml index 96f987c8bb..d2609e7074 100644 --- a/eo-maven-plugin/pom.xml +++ b/eo-maven-plugin/pom.xml @@ -103,6 +103,11 @@ SOFTWARE. 3.0.1 provided + + com.jcabi + jcabi-matchers + test + diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo b/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo index 44b0e3ff2b..d95296f324 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo +++ b/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo @@ -30,7 +30,7 @@ stdout sprintf "%dth Fibonacci number is %d" - (args.get 0) > !n + (args.get 0) > n! fibo n if not diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index cee76d92c9..944c525096 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -23,10 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - - - - + + diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java index b96d708c53..55f364ad10 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java @@ -24,6 +24,7 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.matchers.XhtmlMatchers; import com.jcabi.xml.XMLDocument; import java.io.ByteArrayOutputStream; import java.nio.file.Files; @@ -63,6 +64,10 @@ public void compilesSimpleCodeToJava() throws Exception { new OutputTo(baos) ); program.compile(); + MatcherAssert.assertThat( + XhtmlMatchers.xhtml(baos.toString()), + Matchers.not(XhtmlMatchers.hasXPath("/program/errors/error")) + ); Logger.info(this, "XML output: %s\n", baos.toString()); final ToJava tojava = new ToJava( new XMLDocument(baos.toString()), diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo index 18637a0b2e..0e11b755e3 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo @@ -9,7 +9,7 @@ [n] > alpha a pi 666 > x one - two x + two.f1.f2 x body "Hello, world!" > f! From 2bcce30b2b156fc0c2e0adc441e4f50e4bd7afa8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 19:18:03 +0300 Subject: [PATCH 11/80] #164: global nonames --- .../main/java/org/eolang/compiler/Pack.java | 4 +- .../eolang/compiler/errors/global-nonames.xsl | 44 +++++++++++++++++++ .../java/org/eolang/compiler/PacksTest.java | 1 + .../packs/catches-global-nonames.yaml | 9 ++++ .../main/java/org/eolang/maven/ToJava.java | 3 +- .../java/org/eolang/maven/ToJavaTest.java | 6 ++- .../org/eolang/maven/{test.eo => mess.eo} | 9 ++-- 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml rename eo-maven-plugin/src/test/resources/org/eolang/maven/{test.eo => mess.eo} (75%) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index b7479bc83f..32a3facaa4 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -27,6 +27,7 @@ import com.jcabi.xml.XSL; import com.jcabi.xml.XSLDocument; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import org.cactoos.list.ListOf; @@ -54,6 +55,7 @@ public Pack() { "errors/duplicate-names.xsl", "errors/broken-aliases.xsl", "errors/duplicate-aliases.xsl", + "errors/global-nonames.xsl", "errors/one-body.xsl", "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", @@ -77,7 +79,7 @@ public Pack() { * @param names Names of them in classpath */ public Pack(final Collection names) { - this.sheets = names; + this.sheets = Collections.unmodifiableCollection(names); } @Override diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl new file mode 100644 index 0000000000..46fcd0c8ad --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl @@ -0,0 +1,44 @@ + + + + + + + + + + + + The global object doesn't have a name + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 87b702e6a4..9d5a44878d 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -80,6 +80,7 @@ private static Collection yamlPacks() { "full-syntax.yaml", "catches-name-duplicates.yaml", "catches-alias-duplicates.yaml", + "catches-global-nonames.yaml", "catches-broken-aliases.yaml", "catches-unknown-names.yaml", "catches-self-naming.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml new file mode 100644 index 0000000000..7f0d565645 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/global-nonames.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='3'] +eo: | + 3.14 > pi + + fake diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java index 9c16fcc256..0320ef84a5 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java @@ -24,6 +24,7 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XML; import com.jcabi.xml.XSLDocument; import java.io.IOException; @@ -79,7 +80,7 @@ public ToJava(final XML input, final Path file, final Path tmp) { public void compile() throws IOException { final XML out = new XSLDocument( ToJava.class.getResourceAsStream("to-java.xsl") - ).transform(this.xml); + ).with(new ClasspathSources()).transform(this.xml); new IoChecked<>( new LengthOf( new TeeInput( diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java index 55f364ad10..8267715b9c 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java @@ -30,6 +30,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.ResourceOf; @@ -60,9 +61,12 @@ public void compilesSimpleCodeToJava() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final Program program = new Program( "test", - new ResourceOf("org/eolang/maven/test.eo"), + new ResourceOf("org/eolang/maven/mess.eo"), new OutputTo(baos) ); + program.compile(new ArrayList<>(0)); + Logger.info(this, "Parser output: %s\n", baos.toString()); + baos.reset(); program.compile(); MatcherAssert.assertThat( XhtmlMatchers.xhtml(baos.toString()), diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo similarity index 75% rename from eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo rename to eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 0e11b755e3..242e5ae982 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/test.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -10,9 +10,12 @@ a pi 666 > x one two.f1.f2 x - body - "Hello, world!" > f! + stdout + sprintf > f! + "Hello, " + x + 0x1fff [] alpha - "I love you" + 'Z' From cb6cae0880aee7380bc010e18e42b62420426b3e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 20:24:23 +0300 Subject: [PATCH 12/80] #164: syntax --- .../src/test/resources/org/eolang/maven/mess.eo | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 242e5ae982..780f5b82af 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -8,14 +8,13 @@ [n] > alpha a pi 666 > x - one + n two.f1.f2 x stdout sprintf > f! "Hello, " - x - 0x1fff + x 0x1fff n -[] +alpha > beta alpha 'Z' From 282fc2862108b4fb79a84cfd8d739486b4290b1a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 23 Nov 2020 23:39:26 +0300 Subject: [PATCH 13/80] #164: never colors --- .rultor.yml | 6 +++--- .../test/resources/org/eolang/maven/mess.eo | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.rultor.yml b/.rultor.yml index 9254853608..43b966d16f 100644 --- a/.rultor.yml +++ b/.rultor.yml @@ -12,9 +12,9 @@ install: | pdd --file=/dev/null merge: script: | - mvn clean install -Peo -Pqulice --errors --settings ../settings.xml + mvn clean install -Peo -Pqulice --errors --settings ../settings.xml -Dstyle.color=never release: script: |- - mvn versions:set "-DnewVersion=${tag}" + mvn versions:set "-DnewVersion=${tag}" -Dstyle.color=never git commit -am "${tag}" - mvn clean deploy -Peo -Psonatype --errors --settings ../settings.xml + mvn clean deploy -Peo -Psonatype --errors --settings ../settings.xml -Dstyle.color=never diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 780f5b82af..bd72943666 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -18,3 +18,24 @@ alpha > beta alpha 'Z' + + + +[n+] > alpha$x + a pi 666 + n+ + two.f1.f2 alpha$x + +[n+] > alpha$f! + sprintf + "Hello, " + alpha$x 0x1fff n+ + +[n] > alpha + alpha$x n:n+ + stdout + alpha$f n:n+ + +alpha > beta + alpha + 'Z' From e1895bcc188aa52e7aeeea46b5c8956b39f5c5f7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 25 Nov 2020 16:02:16 +0300 Subject: [PATCH 14/80] #164: refactor --- .../main/java/org/eolang/compiler/Pack.java | 8 +++---- .../{20-add-refs.xsl => add-refs.xsl} | 9 ++++++++ ...esolve-aliases.xsl => resolve-aliases.xsl} | 10 +++++++++ ...method-calls.xsl => wrap-method-calls.xsl} | 22 ++++++++++++++++--- .../org/eolang/compiler/packs/adds-refs.yaml | 4 +++- .../compiler/packs/flatten-abstracts.yaml | 2 +- .../compiler/packs/resolves-aliases.yaml | 4 ++-- .../eolang/compiler/packs/wraps-methods.yaml | 11 +++++----- .../test/resources/org/eolang/maven/mess.eo | 12 +++++----- 9 files changed, 59 insertions(+), 23 deletions(-) rename eo-compiler/src/main/resources/org/eolang/compiler/{20-add-refs.xsl => add-refs.xsl} (83%) rename eo-compiler/src/main/resources/org/eolang/compiler/{30-resolve-aliases.xsl => resolve-aliases.xsl} (87%) rename eo-compiler/src/main/resources/org/eolang/compiler/{60-wrap-method-calls.xsl => wrap-method-calls.xsl} (78%) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 32a3facaa4..3bb597ea55 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -60,13 +60,13 @@ public Pack() { "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", - "20-add-refs.xsl", + "add-refs.xsl", "40-abstracts-float-up.xsl", - "60-wrap-method-calls.xsl", + "wrap-method-calls.xsl", "42-vars-float-up.xsl", - "20-add-refs.xsl", + "add-refs.xsl", "50-rename-bases.xsl", - "30-resolve-aliases.xsl", + "resolve-aliases.xsl", "errors/unknown-names.xsl", "errors/duplicate-names.xsl" ) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl similarity index 83% rename from eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl index ad88d316f7..a207455673 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/20-add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl @@ -23,6 +23,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl similarity index 87% rename from eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl index b1090c2f65..2322bcc3ae 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/30-resolve-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl @@ -23,6 +23,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl similarity index 78% rename from eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl index 38d13a78ed..bba97db10a 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/60-wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl @@ -23,17 +23,33 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + - + - + + - + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml index 7266fb666c..60d07016d2 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml @@ -2,7 +2,7 @@ xsls: - errors/duplicate-names.xsl - errors/self-naming.xsl - errors/same-line-names.xsl - - 20-add-refs.xsl + - add-refs.xsl tests: - /program/errors[count(*)=0] - /program/objects[count(o)=2] @@ -12,6 +12,7 @@ tests: - //o[@base='a' and @line='9' and @ref='12'] - //o[@base='something' and @line='12' and @name='a' and not(@ref)] - //o[@base='x' and @line='16' and @ref='14'] + - //o[@base='first' and @line='17' and @ref='1'] eo: | [x a] > first (stdout x) @@ -29,3 +30,4 @@ eo: | [a] > x tt x 4 + first diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml index a48137cac9..d399815ef4 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml @@ -1,5 +1,5 @@ xsls: - - 20-add-refs.xsl + - add-refs.xsl - 40-abstracts-float-up.xsl - 50-rename-bases.xsl - errors/duplicate-names.xsl diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml index 03605e6e2a..d06c22cac5 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml @@ -1,6 +1,6 @@ xsls: - - 20-add-refs.xsl - - 30-resolve-aliases.xsl + - add-refs.xsl + - resolve-aliases.xsl tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 085fb599fc..24832df110 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -1,16 +1,17 @@ xsls: - - 60-wrap-method-calls.xsl + - wrap-method-calls.xsl tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] - //o[@base='sprintf' and count(./o)=3] - - //o[@base='.get' and count(./o)=2] - - //o[@base='.get']/o[1][@base='args'] - - //o[@base='.get']/o[2][@base='integer'] + - //o[@base='.foo' and count(./o)=1] + - //o[@base='.trim' and count(./o)=2] + - //o[@base='.trim']/o[1][@base='.get'] + - //o[@base='.trim']/o[2][@base='integer'] eo: | [args] > app stdout sprintf "%dth Fibonacci number is %d" - (args.get 1) > n! + (args.get.trim 1).foo.tohex > n! fibo n diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index bd72943666..21cf77f2aa 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -7,9 +7,11 @@ 3.14 > pi [n] > alpha - a pi 666 > x - n - two.f1.f2 x + [n+] > x + a pi 666 + n+ + two.f1.f2 x + x n:n+ stdout sprintf > f! "Hello, " @@ -21,10 +23,6 @@ alpha > beta -[n+] > alpha$x - a pi 666 - n+ - two.f1.f2 alpha$x [n+] > alpha$f! sprintf From 1c5a05c5e3ee5056f6f148611e0c1435e39bb937 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 25 Nov 2020 17:35:09 +0300 Subject: [PATCH 15/80] #164: vars float fix --- .../main/java/org/eolang/compiler/Pack.java | 2 +- .../eolang/compiler/40-abstracts-float-up.xsl | 2 +- .../org/eolang/compiler/50-rename-bases.xsl | 4 +- .../eolang/compiler/{funcs.xsl => _funcs.xsl} | 0 ...42-vars-float-up.xsl => vars-float-up.xsl} | 59 +++++++++++-------- .../org/eolang/compiler/packs/float-vars.yaml | 12 ++-- .../resources/org/eolang/maven/to-java.xsl | 2 +- 7 files changed, 48 insertions(+), 33 deletions(-) rename eo-compiler/src/main/resources/org/eolang/compiler/{funcs.xsl => _funcs.xsl} (100%) rename eo-compiler/src/main/resources/org/eolang/compiler/{42-vars-float-up.xsl => vars-float-up.xsl} (59%) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 3bb597ea55..508b8dd8ee 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -63,7 +63,7 @@ public Pack() { "add-refs.xsl", "40-abstracts-float-up.xsl", "wrap-method-calls.xsl", - "42-vars-float-up.xsl", + "vars-float-up.xsl", "add-refs.xsl", "50-rename-bases.xsl", "resolve-aliases.xsl", diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl index f19f0be5d9..10e1e8aa90 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl @@ -24,7 +24,7 @@ SOFTWARE. --> - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl index 15fcb25006..a7bf772bef 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl @@ -22,9 +22,9 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl similarity index 100% rename from eo-compiler/src/main/resources/org/eolang/compiler/funcs.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl similarity index 59% rename from eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl index 7199185829..be3c2d2ca3 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/42-vars-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl @@ -22,40 +22,53 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + + - - - - - - - - + + - - - - - - - - + + + + - - - + + - + - + + + + + + + - + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index 6b59e9945d..6cee36204a 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -1,15 +1,17 @@ xsls: - - 42-vars-float-up.xsl + - add-refs.xsl + - vars-float-up.xsl - errors/duplicate-names.xsl tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] - - //o[@base='input' and @name='t' and @line='4' and @const and count(o)=1] + - //o[@base='input' and @name='t' and @line='5' and @const and count(o)=1] - //objects[count(.//o[@base='string'])=1] eo: | [] > x one - two - input > t! - "Hello, world!" + [] > y + two + input > t! + "Hello, world!" three t diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 944c525096..3bf900a973 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -24,7 +24,7 @@ SOFTWARE. --> - + From 8829f4dc0a18411eaa0afd50cf63b263645fa6b2 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 25 Nov 2020 17:40:28 +0300 Subject: [PATCH 16/80] #164: polished --- eo-compiler/src/main/java/org/eolang/compiler/Pack.java | 8 ++------ .../resources/org/eolang/compiler/packs/leap-year.yaml | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 508b8dd8ee..9aaaa82b01 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -60,15 +60,11 @@ public Pack() { "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", + "resolve-aliases.xsl", "add-refs.xsl", - "40-abstracts-float-up.xsl", "wrap-method-calls.xsl", "vars-float-up.xsl", - "add-refs.xsl", - "50-rename-bases.xsl", - "resolve-aliases.xsl", - "errors/unknown-names.xsl", - "errors/duplicate-names.xsl" + "errors/unknown-names.xsl" ) ); } diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml index 060068d82b..29614945cf 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml @@ -1,6 +1,6 @@ tests: - /program/errors[count(*)=0] - - /program/objects[count(o)=2] + - /program/objects[count(o)=1] eo: | # This program is from the EOLANG original paper # and must produce a working command line tool From 59de8ef68b467cb17857b3feb7e86eb900c07bd2 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 25 Nov 2020 17:52:50 +0300 Subject: [PATCH 17/80] #164: clean --- .../main/java/org/eolang/compiler/Pack.java | 3 ++- .../test/resources/org/eolang/maven/mess.eo | 22 +++---------------- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 9aaaa82b01..131830a71a 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -60,10 +60,11 @@ public Pack() { "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", - "resolve-aliases.xsl", "add-refs.xsl", "wrap-method-calls.xsl", "vars-float-up.xsl", + "add-refs.xsl", + "resolve-aliases.xsl", "errors/unknown-names.xsl" ) ); diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 21cf77f2aa..27275bba2b 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -7,11 +7,11 @@ 3.14 > pi [n] > alpha - [n+] > x + [] > x a pi 666 - n+ + [] > n + wild two.f1.f2 x - x n:n+ stdout sprintf > f! "Hello, " @@ -21,19 +21,3 @@ alpha > beta alpha 'Z' - - - -[n+] > alpha$f! - sprintf - "Hello, " - alpha$x 0x1fff n+ - -[n] > alpha - alpha$x n:n+ - stdout - alpha$f n:n+ - -alpha > beta - alpha - 'Z' From a41ecf434cac054234ebbf936550802705f64187 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 25 Nov 2020 18:14:39 +0300 Subject: [PATCH 18/80] #164: flatten-abstracts removed --- .../eolang/compiler/40-abstracts-float-up.xsl | 143 ------------------ .../org/eolang/compiler/50-rename-bases.xsl | 57 ------- .../java/org/eolang/compiler/PacksTest.java | 1 - .../compiler/packs/flatten-abstracts.yaml | 31 ---- .../test/resources/org/eolang/maven/mess.eo | 1 - 5 files changed, 233 deletions(-) delete mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl delete mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl delete mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl deleted file mode 100644 index 10e1e8aa90..0000000000 --- a/eo-compiler/src/main/resources/org/eolang/compiler/40-abstracts-float-up.xsl +++ /dev/null @@ -1,143 +0,0 @@ - - - - - - - - - - - - - - - - - - - $ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl deleted file mode 100644 index a7bf772bef..0000000000 --- a/eo-compiler/src/main/resources/org/eolang/compiler/50-rename-bases.xsl +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - - - Can't find ancestor of " - - " at line - - - - - - - - - - - - - - - - - - - - - - diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 9d5a44878d..139a7a04e8 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -89,7 +89,6 @@ private static Collection yamlPacks() { "catches-same-line-name.yaml", "adds-refs.yaml", "float-vars.yaml", - "flatten-abstracts.yaml", "resolves-aliases.yaml", "wraps-methods.yaml", "leap-year.yaml" diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml deleted file mode 100644 index d399815ef4..0000000000 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ /dev/null @@ -1,31 +0,0 @@ -xsls: - - add-refs.xsl - - 40-abstracts-float-up.xsl - - 50-rename-bases.xsl - - errors/duplicate-names.xsl -tests: - - /program/objects[count(o)=6] - - /program/errors[count(*)=0] - - //objects[not(.//o[@name=''])] - - //o[@name='first'] - - //o[@name='first']/o[@base='test' and @as='foo'] - - //o[@name='first$2$second'] - - //o[@name='first$2$second$third'] - - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] -eo: | - [a b] > first - test:foo - a > yes - [x b] > second - a > no - [b c] > third - no > yes - (stdout a b c x).print - t - [] > third - "hello, world!" > msg - f - [] > third - "hello, world!" > msg - [] - "Failure" diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 27275bba2b..e85a10ae38 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -20,4 +20,3 @@ alpha > beta alpha 'Z' - From 5a22f5621d09d9825dc6280187192ab3d7959f9e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 27 Nov 2020 10:28:41 +0300 Subject: [PATCH 19/80] #164: vars fixed --- .../java/org/eolang/compiler/Program.java | 1 + .../org/eolang/compiler/vars-float-up.xsl | 19 +++++++++++++------ .../org/eolang/compiler/packs/float-vars.yaml | 8 +++++++- pom.xml | 5 +++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 037455f17b..78d821b832 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -166,6 +166,7 @@ public void syntaxError(final Recognizer recognizer, final XeListener xel = new XeListener(this.name); new ParseTreeWalker().walk(xel, parser.program()); XML dom = xel.xml(); + Logger.debug(this, "Raw XML:\n%s", dom.toString()); int index = 0; for (final XSL xsl : xsls) { final XML after = xsl.transform(dom); diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl index be3c2d2ca3..8a877df544 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl @@ -43,19 +43,23 @@ SOFTWARE. --> - + + + + + + - - + + - + - - + @@ -66,6 +70,9 @@ SOFTWARE. + + + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index 6cee36204a..006b2bc845 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -5,13 +5,19 @@ xsls: tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] - - //o[@base='input' and @name='t' and @line='5' and @const and count(o)=1] + - //o[@base='input' and @name='t' and @line='6' and @const and count(o)=2] - //objects[count(.//o[@base='string'])=1] + - //o[@name='y']/o[@name='t'] + - //o[@name='y']/o[@name='oops'] + - //objects[count(//o[@name='oops'])=1] eo: | [] > x + first > ff one [] > y two input > t! "Hello, world!" + [] > oops + 50 three t diff --git a/pom.xml b/pom.xml index 590d52638e..c1d1972c03 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,11 @@ SOFTWARE. cactoos 0.46 + + com.jcabi + jcabi-log + 0.19.0 + From a8cdc2226733f19e8da5456e51c3401418dbb47b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 27 Nov 2020 15:21:43 +0300 Subject: [PATCH 20/80] #164: eo:method --- .../src/main/java/org/eolang/compiler/Program.java | 8 +++++++- .../src/main/resources/org/eolang/compiler/_funcs.xsl | 4 ++++ .../resources/org/eolang/compiler/wrap-method-calls.xsl | 9 +++++---- pom.xml | 7 +------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 78d821b832..6333200c30 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -25,6 +25,7 @@ import com.jcabi.log.Logger; import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; import com.jcabi.xml.XSL; import java.io.IOException; import java.nio.file.Path; @@ -207,7 +208,12 @@ public interface Spy { final class None implements Program.Spy { @Override public void push(final int index, final XSL xsl, final XML xml) { - Logger.debug(this, "Parsed #%d via %s", index, xsl); + Logger.debug( + this, + "Parsed #%d via %s", + index, + new XMLDocument(xsl.toString()).xpath("/*/@id").get(0) + ); } } } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl index e262cc18b5..bb0fee26f1 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl @@ -27,4 +27,8 @@ SOFTWARE. + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl index bba97db10a..4a2840fc8f 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + - + + - + - + diff --git a/pom.xml b/pom.xml index c1d1972c03..12134d1066 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ SOFTWARE. com.jcabi parent - 0.50.9 + 0.51.1 org.eolang eo-parent @@ -95,11 +95,6 @@ SOFTWARE. cactoos 0.46 - - com.jcabi - jcabi-log - 0.19.0 - From 1589766db72428bef406d4ba53580b869efb1960 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 27 Nov 2020 16:09:14 +0300 Subject: [PATCH 21/80] #164: fix --- .../resources/org/eolang/compiler/vars-float-up.xsl | 2 +- .../org/eolang/compiler/packs/float-vars.yaml | 4 ++++ .../src/test/java/org/eolang/maven/ToJavaTest.java | 11 ++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl index 8a877df544..7aea6c79d4 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl @@ -59,7 +59,7 @@ SOFTWARE. - + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index 006b2bc845..8f485876c5 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -9,7 +9,9 @@ tests: - //objects[count(.//o[@base='string'])=1] - //o[@name='y']/o[@name='t'] - //o[@name='y']/o[@name='oops'] + - //o[@name='last' and not(@cut)] - //objects[count(//o[@name='oops'])=1] + - //objects[count(//o[@cut='0'])=0] eo: | [] > x first > ff @@ -21,3 +23,5 @@ eo: | [] > oops 50 three t + [] > last + three 1 diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java index 8267715b9c..6d60d589a8 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java @@ -25,7 +25,9 @@ import com.jcabi.log.Logger; import com.jcabi.matchers.XhtmlMatchers; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; +import com.jcabi.xml.XSL; import java.io.ByteArrayOutputStream; import java.nio.file.Files; import java.nio.file.Path; @@ -67,7 +69,14 @@ public void compilesSimpleCodeToJava() throws Exception { program.compile(new ArrayList<>(0)); Logger.info(this, "Parser output: %s\n", baos.toString()); baos.reset(); - program.compile(); + program.compile( + new Program.Spy() { + @Override + public void push(final int idx, final XSL xsl, final XML xml) { + Logger.debug(this, "Output:%s", xml); + } + } + ); MatcherAssert.assertThat( XhtmlMatchers.xhtml(baos.toString()), Matchers.not(XhtmlMatchers.hasXPath("/program/errors/error")) From 94ea6f4d3ef987ef55bcdd0e3466a787a49ae63c Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 30 Nov 2020 18:01:45 +0300 Subject: [PATCH 22/80] #164: globals to abstracts --- .../eolang/compiler/globals-to-abstracts.xsl | 56 +++ .../java/org/eolang/compiler/PacksTest.java | 1 + .../eolang/compiler/packs/fixes-globals.yaml | 15 + .../main/java/org/eolang/maven/ToJava.java | 17 +- .../resources/org/eolang/maven/to-java.xsl | 331 +++++++++--------- .../java/org/eolang/maven/ToJavaTest.java | 2 +- .../src/test/resources/log4j.properties | 1 + 7 files changed, 262 insertions(+), 161 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl new file mode 100644 index 0000000000..6bcae052a1 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 139a7a04e8..489a7fa889 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -87,6 +87,7 @@ private static Collection yamlPacks() { "catches-reserved-atoms.yaml", "catches-two-bodies.yaml", "catches-same-line-name.yaml", + "fixes-globals.yaml", "adds-refs.yaml", "float-vars.yaml", "resolves-aliases.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml new file mode 100644 index 0000000000..23eb3e7ecd --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml @@ -0,0 +1,15 @@ +xsls: + - globals-to-abstracts.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=2] + - //objects/o[@name='pi'] + - //objects/o[@name='b'] + - //objects[count(o[@base])=0] +eo: | + 3.1415926 > pi + + a > b + c + d + "Hello, world!" diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java index 0320ef84a5..4e3d36f4d3 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java @@ -26,6 +26,7 @@ import com.jcabi.log.Logger; import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XML; +import com.jcabi.xml.XSLChain; import com.jcabi.xml.XSLDocument; import java.io.IOException; import java.nio.file.Path; @@ -34,6 +35,8 @@ import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; +import org.cactoos.list.ListOf; +import org.cactoos.list.Mapped; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.LengthOf; @@ -78,9 +81,17 @@ public ToJava(final XML input, final Path file, final Path tmp) { * @throws IOException If fails */ public void compile() throws IOException { - final XML out = new XSLDocument( - ToJava.class.getResourceAsStream("to-java.xsl") - ).with(new ClasspathSources()).transform(this.xml); + final XML out = new XSLChain( + new Mapped<>( + doc -> new XSLDocument( + ToJava.class.getResourceAsStream(doc) + ).with(new ClasspathSources()), + new ListOf<>( + "/org/eolang/compiler/globals-to-abstracts.xsl", + "to-java.xsl" + ) + ) + ).transform(this.xml); new IoChecked<>( new LengthOf( new TeeInput( diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 3bf900a973..0b6300e44f 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -26,8 +26,7 @@ SOFTWARE. - - + @@ -56,7 +55,6 @@ SOFTWARE. - @@ -76,245 +74,264 @@ SOFTWARE. implements Phi { - private final Args args; - - - public + public static final + + 𝑥 = new - (final Args a) { + (); - + - - assert a.has(" + private Phi - "); + ; + private + + () { + + - this.args = a; + // Intentionally empty } - - - @Override - - - public Object call() throws Exception { - - - - throw new RuntimeException( - - - - - - - this.msg(new ArgsOf()).call().toString() - - - String.format("Runtime exception at %s", this.getClass()) - - - - - - ); - - - } - + + + + + + + + + + + + + + + + - - - } - - - /* - - * This file was auto-generated by eolang-maven-plugin - - * on - - . Don't edit it, - - * your changes will be discarded on the next build. - - * The sources were compiled to XML on + + + + public + + cp( + + + , + + final Phi + + + ) { - * - - by the EO compiler v. - - . + + + final + + _ = new + + (); - */ + + + + _. + + = + + ; + + + + + return _; + + } - - package - - ; + + + + @Override + + public Phi 𝜑() { - - + - - public Phi - - (final Args a) + + return + + + + + + - @Override + throw new RuntimeException( + + + + + + this.msg().𝜑().toString() + + + String.format("Runtime exception at %s", this.getClass()) + + + + - public Object call() throws Exception + ) - { + ; - + + } + + + + + + public Phi + + () { + + return - + ; - + } - - - - this. - - (args) - - - this.args.get(" - - ") - - + this. + + () - + new Call(" ", - + - , new ArgsOf( - - - - - - - - - )) + ) - - - - - new - (new ArgsOf( - - a - + .𝑥.cp( + + + + , - - - - - - - - + - - - - - - - )) - - - - - - new Entry(" - - - - - - ", + - ) - - , - - - - + + + ) + + + new EOstring( + " + + " + ) + new EOinteger( - L + L) + new EOinteger( - L + L) + new EOfloat( - - - " - - " + d) + new EOchar( ' - ' + ') + new EObool( + ) + + + package + + ; + + + + + /* + + * This file was auto-generated by eolang-maven-plugin + + * on + + . Don't edit it, + + * your changes will be discarded on the next build. + + * The sources were compiled to XML on + + * + + by the EO compiler v. + + . + + */ + + diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java index 6d60d589a8..1dc8c4767c 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java @@ -73,7 +73,7 @@ public void compilesSimpleCodeToJava() throws Exception { new Program.Spy() { @Override public void push(final int idx, final XSL xsl, final XML xml) { - Logger.debug(this, "Output:%s", xml); + Logger.trace(this, "Output:%s", xml); } } ); diff --git a/eo-maven-plugin/src/test/resources/log4j.properties b/eo-maven-plugin/src/test/resources/log4j.properties index f8675dabb6..256fae55c2 100644 --- a/eo-maven-plugin/src/test/resources/log4j.properties +++ b/eo-maven-plugin/src/test/resources/log4j.properties @@ -5,3 +5,4 @@ log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n log4j.logger.org.eolang=DEBUG +log4j.logger.org.eolang.compiler.Program=INFO From a47a6aeb81cad73ea4175a34d47aceb6561c79a1 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 30 Nov 2020 18:32:26 +0300 Subject: [PATCH 23/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 33 ++++++++++++++++--- .../test/resources/org/eolang/maven/mess.eo | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 0b6300e44f..da68a91f81 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -230,9 +230,25 @@ SOFTWARE. - this. - - () + + + + + + + .𝑥 + + + this. + + () + + + + + + + @@ -249,7 +265,16 @@ SOFTWARE. - .𝑥.cp( + .𝑥 + + + + + + + + + .cp( diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index e85a10ae38..ba6c16a22c 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -14,7 +14,7 @@ two.f1.f2 x stdout sprintf > f! - "Hello, " + "Hello, ".trim x 0x1fff n alpha > beta From af5ffc6ca22881ede8825114c467aac960a09b60 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 30 Nov 2020 19:02:28 +0300 Subject: [PATCH 24/80] #164: more --- .../main/java/org/eolang/examples/Main.java | 2 - .../resources/org/eolang/maven/to-java.xsl | 67 ++++---- .../src/main/java/org/eolang/EOadd.java | 2 - .../src/main/java/org/eolang/EOand.java | 1 - .../src/main/java/org/eolang/EOarray.java | 3 - eo-runtime/src/main/java/org/eolang/EOif.java | 1 - .../src/main/java/org/eolang/EOless.java | 1 - .../src/main/java/org/eolang/EOmul.java | 2 - eo-runtime/src/main/java/org/eolang/EOor.java | 1 - .../src/main/java/org/eolang/io/EOstdout.java | 1 - .../src/main/java/org/eolang/sys/Args.java | 63 -------- .../java/org/eolang/sys/ArgsException.java | 47 ------ .../src/main/java/org/eolang/sys/ArgsOf.java | 143 ------------------ .../src/main/java/org/eolang/sys/Call.java | 70 --------- .../src/main/java/org/eolang/sys/Entry.java | 70 --------- .../main/java/org/eolang/txt/EOsprintf.java | 1 - .../src/test/java/org/eolang/EOaddTest.java | 3 - .../src/test/java/org/eolang/EOifTest.java | 2 - .../src/test/java/org/eolang/EOlessTest.java | 2 - .../src/test/java/org/eolang/EOmulTest.java | 3 - .../java/org/eolang/txt/EOsprintfTest.java | 2 - 21 files changed, 36 insertions(+), 451 deletions(-) delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/Args.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/ArgsException.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/Call.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/Entry.java diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java index 9e5cb69a83..0fc5bb57ef 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java +++ b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java @@ -25,8 +25,6 @@ package org.eolang.examples; import org.eolang.EOarray; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; /** * EO entry point. diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index da68a91f81..0c283b4d92 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -109,13 +109,11 @@ SOFTWARE. - - - - - - - + + + + + } @@ -234,7 +232,7 @@ SOFTWARE. - + .𝑥 @@ -252,15 +250,19 @@ SOFTWARE. - new Call(" - - ", - ) + + () + + 1 + + + + @@ -274,26 +276,29 @@ SOFTWARE. - .cp( - - - - - , - - - - - - + + + .cp( + + + + + , + - - - - - - - ) + + + + + + + + + + + + ) + new EOstring( diff --git a/eo-runtime/src/main/java/org/eolang/EOadd.java b/eo-runtime/src/main/java/org/eolang/EOadd.java index 3d8bf16dfa..ef53aa912c 100644 --- a/eo-runtime/src/main/java/org/eolang/EOadd.java +++ b/eo-runtime/src/main/java/org/eolang/EOadd.java @@ -25,8 +25,6 @@ package org.eolang; import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; import org.eolang.sys.Phi; import org.eolang.sys.TypeMismatchException; diff --git a/eo-runtime/src/main/java/org/eolang/EOand.java b/eo-runtime/src/main/java/org/eolang/EOand.java index 29270e9262..dcd41cac23 100644 --- a/eo-runtime/src/main/java/org/eolang/EOand.java +++ b/eo-runtime/src/main/java/org/eolang/EOand.java @@ -25,7 +25,6 @@ package org.eolang; import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; import org.eolang.sys.Phi; /** diff --git a/eo-runtime/src/main/java/org/eolang/EOarray.java b/eo-runtime/src/main/java/org/eolang/EOarray.java index a4f006ef1d..7a5e3a8dd2 100644 --- a/eo-runtime/src/main/java/org/eolang/EOarray.java +++ b/eo-runtime/src/main/java/org/eolang/EOarray.java @@ -24,9 +24,6 @@ package org.eolang; -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; - /** * ARRAY. * diff --git a/eo-runtime/src/main/java/org/eolang/EOif.java b/eo-runtime/src/main/java/org/eolang/EOif.java index cdc5ef71c7..ebbe5de54d 100644 --- a/eo-runtime/src/main/java/org/eolang/EOif.java +++ b/eo-runtime/src/main/java/org/eolang/EOif.java @@ -24,7 +24,6 @@ package org.eolang; -import org.eolang.sys.Args; import org.eolang.sys.Phi; /** diff --git a/eo-runtime/src/main/java/org/eolang/EOless.java b/eo-runtime/src/main/java/org/eolang/EOless.java index 82e68115f0..8139f8921b 100644 --- a/eo-runtime/src/main/java/org/eolang/EOless.java +++ b/eo-runtime/src/main/java/org/eolang/EOless.java @@ -24,7 +24,6 @@ package org.eolang; -import org.eolang.sys.Args; import org.eolang.sys.Phi; import org.eolang.sys.TypeMismatchException; diff --git a/eo-runtime/src/main/java/org/eolang/EOmul.java b/eo-runtime/src/main/java/org/eolang/EOmul.java index 815138b858..ee1ac0a381 100644 --- a/eo-runtime/src/main/java/org/eolang/EOmul.java +++ b/eo-runtime/src/main/java/org/eolang/EOmul.java @@ -25,8 +25,6 @@ package org.eolang; import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; import org.eolang.sys.Phi; import org.eolang.sys.TypeMismatchException; diff --git a/eo-runtime/src/main/java/org/eolang/EOor.java b/eo-runtime/src/main/java/org/eolang/EOor.java index df7b20012e..fea480ab13 100644 --- a/eo-runtime/src/main/java/org/eolang/EOor.java +++ b/eo-runtime/src/main/java/org/eolang/EOor.java @@ -25,7 +25,6 @@ package org.eolang; import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; import org.eolang.sys.Phi; /** diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 2ca0f1aac1..5540da3545 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -24,7 +24,6 @@ package org.eolang.io; -import org.eolang.sys.Args; import org.eolang.sys.Phi; /** diff --git a/eo-runtime/src/main/java/org/eolang/sys/Args.java b/eo-runtime/src/main/java/org/eolang/sys/Args.java deleted file mode 100644 index 1214f98d1d..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Args.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -/** - * Arguments of an object. - * - * @since 0.1 - */ -public interface Args { - - /** - * Get one argument. - * @param key The name - * @return The argument - */ - Object get(String key); - - /** - * Get one argument and call it. - * @param key The name - * @param type The type of expected result - * @return The argument - * @param The type of result - * @throws Exception If fails - */ - T call(String key, Class type) throws Exception; - - /** - * This one exists? - * @param key The name - * @return TRUE if exists - */ - boolean has(String key); - - /** - * Get all keys. - * @return All keys - */ - Iterable keys(); -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java b/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java deleted file mode 100644 index 72746effb6..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -/** - * Args Exception. - * - * @since 0.1 - */ -public final class ArgsException extends RuntimeException { - - /** - * Serial ID. - */ - private static final long serialVersionUID = -6643350804302660951L; - - /** - * Ctor. - * @param reason The reason - */ - public ArgsException(final String reason) { - super(reason); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java b/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java deleted file mode 100644 index 52f5a985d6..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -import java.util.HashMap; -import java.util.Map; - -/** - * Arguments of an object. - * - * @since 0.1 - */ -public final class ArgsOf implements Args { - - /** - * The map. - */ - private final Map map; - - /** - * Ctor. - */ - public ArgsOf() { - this(new HashMap<>(0)); - } - - /** - * Ctor. - * @param items Items - */ - public ArgsOf(final Object... items) { - this(new ArgsOf(), ArgsOf.toEntries(items)); - } - - /** - * Ctor. - * @param entries Entries - */ - public ArgsOf(final Entry... entries) { - this(new ArgsOf(), entries); - } - - /** - * Ctor. - * @param before Args before - * @param entries Entries - */ - public ArgsOf(final Args before, final Entry... entries) { - this(ArgsOf.combine(before, entries)); - } - - /** - * Ctor. - * @param args The args - */ - private ArgsOf(final Map args) { - this.map = args; - } - - @Override - public Object get(final String key) { - final Object result = this.map.get(key); - if (result == null) { - throw new ArgsException( - String.format("The argument \"%s\" is absent", key) - ); - } - return result; - } - - @Override - public T call(final String key, final Class type) throws Exception { - Object result = this.get(key); - if (result instanceof Phi) { - result = Phi.class.cast(result).call(); - } - return type.cast(result); - } - - @Override - public boolean has(final String key) { - return this.map.containsKey(key); - } - - @Override - public Iterable keys() { - return this.map.keySet(); - } - - /** - * Combine them. - * @param before Map before - * @param entries Entries - * @return New map - */ - private static Map combine( - final Args before, final Entry... entries) { - final Map after = new HashMap<>(entries.length); - for (final String key : before.keys()) { - after.put(key, before.get(key)); - } - for (final Entry entry : entries) { - after.put(entry.key(), entry.value()); - } - return after; - } - - /** - * Array of objects to entries. - * @param items The items - * @return Array of entries - */ - private static Entry[] toEntries(final Object... items) { - final Entry[] array = new Entry[items.length]; - for (int idx = 0; idx < items.length; ++idx) { - array[idx] = new Entry(String.format("%02d", idx), items[idx]); - } - return array; - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Call.java b/eo-runtime/src/main/java/org/eolang/sys/Call.java deleted file mode 100644 index 2fdaeb287b..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Call.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -import java.lang.reflect.Method; - -/** - * Call. - * - * @since 0.1 - */ -public final class Call implements Phi { - - /** - * The name of the method to call. - */ - private final String method; - - /** - * The object. - */ - private final Object object; - - /** - * Arguments to pass. - */ - private final Args args; - - /** - * Ctor. - * @param mtd The method to call - * @param obj The object - * @param input Input arguments - */ - public Call(final String mtd, final Object obj, final Args input) { - this.method = mtd; - this.object = obj; - this.args = input; - } - - @Override - public Object call() throws Exception { - final Method mtd = this.object.getClass().getDeclaredMethod( - this.method, Args.class - ); - return mtd.invoke(this.object, this.args); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Entry.java b/eo-runtime/src/main/java/org/eolang/sys/Entry.java deleted file mode 100644 index 74c7cad87a..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Entry.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -/** - * Entry. - * - * @since 0.1 - */ -public final class Entry { - - /** - * The key. - */ - private final String akey; - - /** - * The value. - */ - private final Object avalue; - - /** - * Ctor. - * @param key The key - * @param value The value - */ - public Entry(final String key, final Object value) { - this.akey = key; - this.avalue = value; - } - - /** - * Get key. - * @return The key - */ - public String key() { - return this.akey; - } - - /** - * Get value. - * @return The value - */ - public Object value() { - return this.avalue; - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index c6525bae02..082e3ffc86 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -27,7 +27,6 @@ import java.util.Collection; import java.util.LinkedList; import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; import org.eolang.sys.Phi; /** diff --git a/eo-runtime/src/test/java/org/eolang/EOaddTest.java b/eo-runtime/src/test/java/org/eolang/EOaddTest.java index 3b8980cf8a..a4e0dd3034 100644 --- a/eo-runtime/src/test/java/org/eolang/EOaddTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOaddTest.java @@ -23,9 +23,6 @@ */ package org.eolang; -import org.eolang.sys.ArgsException; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; diff --git a/eo-runtime/src/test/java/org/eolang/EOifTest.java b/eo-runtime/src/test/java/org/eolang/EOifTest.java index 8cf12fb6fb..a5df2728ae 100644 --- a/eo-runtime/src/test/java/org/eolang/EOifTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOifTest.java @@ -23,8 +23,6 @@ */ package org.eolang; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; diff --git a/eo-runtime/src/test/java/org/eolang/EOlessTest.java b/eo-runtime/src/test/java/org/eolang/EOlessTest.java index d9a469db0f..58e50e93b9 100644 --- a/eo-runtime/src/test/java/org/eolang/EOlessTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOlessTest.java @@ -23,8 +23,6 @@ */ package org.eolang; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; diff --git a/eo-runtime/src/test/java/org/eolang/EOmulTest.java b/eo-runtime/src/test/java/org/eolang/EOmulTest.java index cd625ef717..4445714a6b 100644 --- a/eo-runtime/src/test/java/org/eolang/EOmulTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOmulTest.java @@ -23,9 +23,6 @@ */ package org.eolang; -import org.eolang.sys.ArgsException; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Assertions; diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index 77cbf7dab7..3980cbccf8 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -23,8 +23,6 @@ */ package org.eolang.txt; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; From 72beb508eb5177db5a5dac06aa69762f92b49b99 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 30 Nov 2020 19:58:15 +0300 Subject: [PATCH 25/80] #164: wip --- .../eolang/compiler/abstracts-float-up.xsl | 160 ++++++++++++++++++ .../java/org/eolang/compiler/PacksTest.java | 1 + .../compiler/packs/float-abstracts.yaml | 30 ++++ .../main/java/org/eolang/maven/ToJava.java | 1 + .../src/main/java/org/eolang/EOadd.java | 24 +-- .../src/main/java/org/eolang/io/EOstdout.java | 22 ++- .../java/org/eolang/sys/EObool.java} | 45 ++--- .../src/main/java/org/eolang/sys/EOfloat.java | 58 +++++++ .../main/java/org/eolang/sys/EOinteger.java | 58 +++++++ .../main/java/org/eolang/sys/EOstring.java | 58 +++++++ .../src/main/java/org/eolang/sys/Phi.java | 9 +- .../main/java/org/eolang/sys/Primitive.java | 63 +++++++ .../main/java/org/eolang/txt/EOsprintf.java | 41 ++--- .../sys/{CallTest.java => EOstringTest.java} | 19 +-- .../java/org/eolang/txt/EOsprintfTest.java | 17 +- 15 files changed, 524 insertions(+), 82 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml rename eo-runtime/src/{test/java/org/eolang/sys/ArgsOfTest.java => main/java/org/eolang/sys/EObool.java} (64%) create mode 100644 eo-runtime/src/main/java/org/eolang/sys/EOfloat.java create mode 100644 eo-runtime/src/main/java/org/eolang/sys/EOinteger.java create mode 100644 eo-runtime/src/main/java/org/eolang/sys/EOstring.java create mode 100644 eo-runtime/src/main/java/org/eolang/sys/Primitive.java rename eo-runtime/src/test/java/org/eolang/sys/{CallTest.java => EOstringTest.java} (78%) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl new file mode 100644 index 0000000000..88a1735abb --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl @@ -0,0 +1,160 @@ + + + + + + + + + + + + + + + + + + + + $ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 489a7fa889..f814b91d27 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -90,6 +90,7 @@ private static Collection yamlPacks() { "fixes-globals.yaml", "adds-refs.yaml", "float-vars.yaml", + "float-abstracts.yaml", "resolves-aliases.yaml", "wraps-methods.yaml", "leap-year.yaml" diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml new file mode 100644 index 0000000000..7c8723974f --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml @@ -0,0 +1,30 @@ +xsls: + - add-refs.xsl + - abstracts-float-up.xsl + - errors/duplicate-names.xsl +tests: + - /program/objects[count(o)=6] + - /program/errors[count(*)=0] + - //objects[not(.//o[@name=''])] + - //o[@name='first'] + - //o[@name='first']/o[@base='test' and @as='foo'] + - //o[@name='first$2$second'] + - //o[@name='first$2$second$third'] + - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a' and @ref='1'] +eo: | + [a b] > first + test:foo + a > yes + [x b] > second + a > no + [b c] > third + no > yes + (stdout a b c x).print + t + [] > third + "hello, world!" > msg + f + [] > third + "hello, world!" > msg + [] + "Failure" diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java index 4e3d36f4d3..a08b95f135 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java @@ -88,6 +88,7 @@ public void compile() throws IOException { ).with(new ClasspathSources()), new ListOf<>( "/org/eolang/compiler/globals-to-abstracts.xsl", + "/org/eolang/compiler/float-abstracts.xsl", "to-java.xsl" ) ) diff --git a/eo-runtime/src/main/java/org/eolang/EOadd.java b/eo-runtime/src/main/java/org/eolang/EOadd.java index ef53aa912c..7fd982de35 100644 --- a/eo-runtime/src/main/java/org/eolang/EOadd.java +++ b/eo-runtime/src/main/java/org/eolang/EOadd.java @@ -24,7 +24,6 @@ package org.eolang; -import org.cactoos.iterable.Sorted; import org.eolang.sys.Phi; import org.eolang.sys.TypeMismatchException; @@ -38,25 +37,30 @@ public final class EOadd implements Phi { /** * Args. */ - private final Args args; + private final Phi[] args; /** * Ctor. - * @param arg Args + * @param items Args */ - public EOadd(final Args arg) { - this.args = arg; + public EOadd(final Phi... items) { + this.args = items; } @Override - @SuppressWarnings("PMD.CyclomaticComplexity") public Object call() throws Exception { Object result = null; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; + for (final Phi arg : this.args) { + final Object val = arg.𝜑(); + if (!(val instanceof org.eolang.Primitive)) { + throw new TypeMismatchException( + String.format( + "Can't use ADD with non-data object %s", + val.getClass() + ) + ); } - final Object nxt = this.args.call(key, Object.class); + final org.eolang.Primitive if (!(nxt instanceof Long) && !(nxt instanceof Float)) { throw new TypeMismatchException( String.format( diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 5540da3545..4dabe2873e 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -24,6 +24,9 @@ package org.eolang.io; +import org.eolang.Primitive; +import org.eolang.sys.EObool; +import org.eolang.sys.EOstring; import org.eolang.sys.Phi; /** @@ -34,22 +37,23 @@ public final class EOstdout implements Phi { /** - * Args. + * The text to print. */ - private final Args args; + private final Phi text; /** * Ctor. - * @param arg Args + * @param txt The text to print */ - public EOstdout(final Args arg) { - this.args = arg; + public EOstdout(final Phi txt) { + this.text = txt; } @Override - @SuppressWarnings("PMD.SystemPrintln") - public Object call() throws Exception { - System.out.print(this.args.call("01", String.class)); - return true; + public Phi 𝜑() { + System.out.print( + new Primitive.End(this.text).take(EOstring.class).data() + ); + return new EObool(true); } } diff --git a/eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java b/eo-runtime/src/main/java/org/eolang/sys/EObool.java similarity index 64% rename from eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java rename to eo-runtime/src/main/java/org/eolang/sys/EObool.java index c0d0c162fb..f81db7e796 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java +++ b/eo-runtime/src/main/java/org/eolang/sys/EObool.java @@ -21,37 +21,38 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ + package org.eolang.sys; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +import org.eolang.Primitive; /** - * Test case for {@link ArgsOf}. + * BOOLEAN. + * * @since 0.1 */ -public final class ArgsOfTest { +public final class EObool implements Primitive { - @Test - public void createsArgs() { - final String key = "hey"; - MatcherAssert.assertThat( - new ArgsOf(new Entry(key, 1)).get(key), - Matchers.equalTo(1) - ); + /** + * The value. + */ + private final boolean value; + + /** + * Ctor. + * @param val The value + */ + public EObool(final boolean val) { + this.value = val; } - @Test - public void createsArgsWithBefore() { - final String key = "hello"; - MatcherAssert.assertThat( - new ArgsOf( - new ArgsOf(new Entry(key, 0)), - new Entry(key, -1) - ).get(key), - Matchers.equalTo(-1) - ); + @Override + public Phi 𝜑() { + throw new UnsupportedOperationException("#𝜑()"); } + @Override + public Boolean data() { + return this.value; + } } diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java b/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java new file mode 100644 index 0000000000..10a5f597fe --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang.sys; + +import org.eolang.Primitive; + +/** + * FLOAT. + * + * @since 0.1 + */ +public final class EOfloat implements Primitive { + + /** + * The value. + */ + private final double value; + + /** + * Ctor. + * @param val The value + */ + public EOfloat(final double val) { + this.value = val; + } + + @Override + public Phi 𝜑() { + throw new UnsupportedOperationException("#𝜑()"); + } + + @Override + public Double data() { + return this.value; + } +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java b/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java new file mode 100644 index 0000000000..6fdf421a1f --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang.sys; + +import org.eolang.Primitive; + +/** + * INTEGER. + * + * @since 0.1 + */ +public final class EOinteger implements Primitive { + + /** + * The value. + */ + private final long value; + + /** + * Ctor. + * @param val The value + */ + public EOinteger(final long val) { + this.value = val; + } + + @Override + public Phi 𝜑() { + throw new UnsupportedOperationException("#𝜑()"); + } + + @Override + public Long data() { + return this.value; + } +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOstring.java b/eo-runtime/src/main/java/org/eolang/sys/EOstring.java new file mode 100644 index 0000000000..e1e1302b63 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/sys/EOstring.java @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang.sys; + +import org.eolang.Primitive; + +/** + * STRING. + * + * @since 0.1 + */ +public final class EOstring implements Primitive { + + /** + * The value. + */ + private final String value; + + /** + * Ctor. + * @param val The value + */ + public EOstring(final String val) { + this.value = val; + } + + @Override + public Phi 𝜑() { + throw new UnsupportedOperationException("#𝜑()"); + } + + @Override + public String data() { + return this.value; + } +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Phi.java b/eo-runtime/src/main/java/org/eolang/sys/Phi.java index d3d3d93adb..5f524699f9 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/sys/Phi.java @@ -24,12 +24,15 @@ package org.eolang.sys; -import java.util.concurrent.Callable; - /** * Object with body. * * @since 0.1 */ -public interface Phi extends Callable { +public interface Phi { + /** + * Take the result. + * @return The result + */ + Phi 𝜑(); } diff --git a/eo-runtime/src/main/java/org/eolang/sys/Primitive.java b/eo-runtime/src/main/java/org/eolang/sys/Primitive.java new file mode 100644 index 0000000000..1b8ddb5cf0 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/sys/Primitive.java @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +import org.eolang.sys.Phi; +import org.eolang.sys.TypeMismatchException; + +/** + * Data primitive. + * + * @since 0.1 + */ +public interface Primitive extends Phi { + + /** + * Take the take out. + * @return The take + */ + T data(); + + /** + * End point. + * + * @since 0.1 + */ + final class End { + private final Phi phi; + public End(final Phi src) { + this.phi = src; + } + public > X take(final Class type) { + final Phi result = this.phi.𝜑(); + if (result instanceof Primitive) { + return type.cast(result); + } else { + throw new TypeMismatchException(""); + } + } + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 082e3ffc86..b27ea462a1 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,7 +26,8 @@ import java.util.Collection; import java.util.LinkedList; -import org.cactoos.iterable.Sorted; +import org.eolang.Primitive; +import org.eolang.sys.EOstring; import org.eolang.sys.Phi; /** @@ -37,34 +38,36 @@ public final class EOsprintf implements Phi { /** - * Args. + * The format. */ - private final Args args; + private final Phi format; + + /** + * Arguments. + */ + private final Phi[] arguments; /** * Ctor. - * @param arg Args + * @param fmt Format + * @param args Args */ - public EOsprintf(final Args arg) { - this.args = arg; + public EOsprintf(final Phi fmt, final Phi... args) { + this.format = fmt; + this.arguments = args; } @Override - @SuppressWarnings("PMD.SystemPrintln") - public Object call() throws Exception { + public Phi 𝜑() { final Collection items = new LinkedList<>(); - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - if ("01".equals(key)) { - continue; - } - items.add(this.args.call(key, Object.class)); + for (final Phi arg : this.arguments) { + items.add(new Primitive.End(arg).take(EOstring.class).data()); } - return String.format( - this.args.call("01", String.class), - items.toArray() + return new EOstring( + String.format( + new Primitive.End(this.format).take(EOstring.class).data(), + items.toArray() + ) ); } } diff --git a/eo-runtime/src/test/java/org/eolang/sys/CallTest.java b/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java similarity index 78% rename from eo-runtime/src/test/java/org/eolang/sys/CallTest.java rename to eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java index 37d9109188..1863768cca 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/CallTest.java +++ b/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java @@ -23,29 +23,24 @@ */ package org.eolang.sys; -import org.eolang.EOarray; +import org.eolang.Primitive; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; /** - * Test case for {@link Call}. + * Test case for {@link EOstring}. * * @since 0.1 */ -public final class CallTest { +public final class EOstringTest { @Test - public void makesCall() throws Exception { + public void encapsulatesString() { + final String key = "hey"; MatcherAssert.assertThat( - new Call( - "length", - new EOarray( - new ArgsOf(new Entry("01", 1)) - ), - new ArgsOf() - ).call(), - Matchers.equalTo(1) + new Primitive.End(new EOstring(key)).take(EOstring.class).data(), + Matchers.equalTo(key) ); } diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index 3980cbccf8..c7481645bb 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -23,6 +23,9 @@ */ package org.eolang.txt; +import org.eolang.Primitive; +import org.eolang.sys.EOinteger; +import org.eolang.sys.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -35,15 +38,15 @@ public final class EOsprintfTest { @Test - public void printsSimpleText() throws Exception { + public void printsSimpleText() { MatcherAssert.assertThat( - new EOsprintf( - new ArgsOf( - new Entry("01", "Hello, %s %d!"), - new Entry("02", "John"), - new Entry("03", 2) + new Primitive.End( + new EOsprintf( + new EOstring("Hello, %s %d!"), + new EOstring("John"), + new EOinteger(2L) ) - ).call(), + ).take(EOstring.class).data(), Matchers.equalTo("Hello, John 2!") ); } From 1f87b6cd42dd5eca786b770d4ef5412d4a6381b9 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 2 Dec 2020 16:38:29 +0300 Subject: [PATCH 26/80] #164: re --- README.md | 18 ++-- .../src/main/java/org/eolang/EOadd.java | 98 ------------------- .../src/main/java/org/eolang/EOand.java | 25 +++-- eo-runtime/src/main/java/org/eolang/EOif.java | 40 +++++--- .../src/main/java/org/eolang/EOmul.java | 94 ------------------ .../src/main/java/org/eolang/io/EOstdout.java | 20 +++- .../{EOless.java => sys/ArgsException.java} | 45 ++------- .../src/main/java/org/eolang/sys/EObool.java | 4 +- .../src/main/java/org/eolang/sys/EOfloat.java | 4 +- .../main/java/org/eolang/sys/EOinteger.java | 58 ++++++++++- .../main/java/org/eolang/sys/EOstring.java | 4 +- .../main/java/org/eolang/sys/Primitive.java | 13 ++- .../main/java/org/eolang/txt/EOsprintf.java | 2 +- .../eolang/{EOmulTest.java => EOandTest.java} | 44 +++++---- .../{EOaddTest.java => io/EOstdoutTest.java} | 46 ++++----- .../test/java/org/eolang/io/package-info.java | 30 ++++++ .../EOintegerTest.java} | 21 ++-- .../java/org/eolang/sys/EOstringTest.java | 1 - 18 files changed, 228 insertions(+), 339 deletions(-) delete mode 100644 eo-runtime/src/main/java/org/eolang/EOadd.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOmul.java rename eo-runtime/src/main/java/org/eolang/{EOless.java => sys/ArgsException.java} (51%) rename eo-runtime/src/test/java/org/eolang/{EOmulTest.java => EOandTest.java} (63%) rename eo-runtime/src/test/java/org/eolang/{EOaddTest.java => io/EOstdoutTest.java} (64%) create mode 100644 eo-runtime/src/test/java/org/eolang/io/package-info.java rename eo-runtime/src/test/java/org/eolang/{EOlessTest.java => sys/EOintegerTest.java} (79%) diff --git a/README.md b/README.md index 38268304c0..9ce883aa2a 100644 --- a/README.md +++ b/README.md @@ -79,18 +79,18 @@ whether it's leap or not: [args] > main [y] > leap - or - and - eq (mod y 4) 0 - not (eq (mod y 100) 0) - eq (mod y 400) 0 - and + or. + and. + eq. (mod. y 4) 0 + not. (eq. (mod. y 100) 0) + eq. (mod. y 400) 0 + and > @ stdout "Enter a year:" stdout - concat + sprintf + "%d is %sa leap year!" (scanner stdin).nextInt > year! - " is a leap year?" - leap year:y + if (leap year:y) "" "not " ``` More examples are [here](https://github.com/yegor256/eo/tree/master/eo-maven-plugin/src/it). diff --git a/eo-runtime/src/main/java/org/eolang/EOadd.java b/eo-runtime/src/main/java/org/eolang/EOadd.java deleted file mode 100644 index 7fd982de35..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOadd.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; - -/** - * ADD. - * - * @since 0.1 - */ -public final class EOadd implements Phi { - - /** - * Args. - */ - private final Phi[] args; - - /** - * Ctor. - * @param items Args - */ - public EOadd(final Phi... items) { - this.args = items; - } - - @Override - public Object call() throws Exception { - Object result = null; - for (final Phi arg : this.args) { - final Object val = arg.𝜑(); - if (!(val instanceof org.eolang.Primitive)) { - throw new TypeMismatchException( - String.format( - "Can't use ADD with non-data object %s", - val.getClass() - ) - ); - } - final org.eolang.Primitive - if (!(nxt instanceof Long) && !(nxt instanceof Float)) { - throw new TypeMismatchException( - String.format( - "Can't use ADD with %s", - nxt.getClass() - ) - ); - } - if (result != null && !nxt.getClass().equals(result.getClass())) { - throw new TypeMismatchException( - String.format( - "Can't ADD %s to %s", - result.getClass(), - nxt.getClass() - ) - ); - } - if (result == null) { - result = nxt; - } else { - if (result instanceof Long) { - result = Long.class.cast(result) + Long.class.cast(nxt); - } else { - result = Float.class.cast(result) + Float.class.cast(nxt); - } - } - } - if (result == null) { - throw new ArgsException( - "At least one argument required by ADD" - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOand.java b/eo-runtime/src/main/java/org/eolang/EOand.java index dcd41cac23..00babb8d3b 100644 --- a/eo-runtime/src/main/java/org/eolang/EOand.java +++ b/eo-runtime/src/main/java/org/eolang/EOand.java @@ -24,8 +24,9 @@ package org.eolang; -import org.cactoos.iterable.Sorted; +import org.eolang.sys.EObool; import org.eolang.sys.Phi; +import org.eolang.sys.Primitive; /** * AND. @@ -37,28 +38,26 @@ public final class EOand implements Phi { /** * Args. */ - private final Args args; + private final Phi[] args; /** * Ctor. - * @param arg Args + * @param items The items */ - public EOand(final Args arg) { - this.args = arg; + public EOand(final Phi... items) { + this.args = items; } @Override - public Object call() throws Exception { + public Phi 𝜑() { boolean result = true; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - result &= this.args.call(key, Boolean.class).equals(Boolean.TRUE); - if (!result) { + for (final Phi arg : this.args) { + if (!new Primitive.End(arg).take(EObool.class).data()) { + result = false; break; } } - return result; + return new EObool(result); } + } diff --git a/eo-runtime/src/main/java/org/eolang/EOif.java b/eo-runtime/src/main/java/org/eolang/EOif.java index ebbe5de54d..4b63459fa8 100644 --- a/eo-runtime/src/main/java/org/eolang/EOif.java +++ b/eo-runtime/src/main/java/org/eolang/EOif.java @@ -24,7 +24,9 @@ package org.eolang; +import org.eolang.sys.EObool; import org.eolang.sys.Phi; +import org.eolang.sys.Primitive; /** * IF. @@ -34,25 +36,41 @@ public final class EOif implements Phi { /** - * Args. + * The condition. */ - private final Args args; + private final Phi term; + + /** + * When TRUE. + */ + private final Phi positive; + + /** + * When FALSE. + */ + private final Phi negative; /** * Ctor. - * @param arg Args + * @param trm The term + * @param pos The positive + * @param neg The negative */ - public EOif(final Args arg) { - this.args = arg; + public EOif(final Phi trm, final Phi pos, final Phi neg) { + this.term = trm; + this.positive = pos; + this.negative = neg; } @Override - public Object call() throws Exception { - Object result = false; - if (this.args.call("01", Boolean.class).equals(Boolean.TRUE)) { - result = this.args.get("02"); - } else if (this.args.has("03")) { - result = this.args.get("03"); + public Phi 𝜑() { + final boolean yes = new Primitive.End(this.term) + .take(EObool.class).data(); + final Phi result; + if (yes) { + result = this.positive; + } else { + result = this.negative; } return result; } diff --git a/eo-runtime/src/main/java/org/eolang/EOmul.java b/eo-runtime/src/main/java/org/eolang/EOmul.java deleted file mode 100644 index ee1ac0a381..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOmul.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; - -/** - * MUL. - * - * @since 0.2 - */ -public final class EOmul implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOmul(final Args arg) { - this.args = arg; - } - - @Override - @SuppressWarnings("PMD.CyclomaticComplexity") - public Object call() throws Exception { - Object result = null; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - final Number nxt = this.args.call(key, Number.class); - if (!(nxt instanceof Long) && !(nxt instanceof Float)) { - throw new TypeMismatchException( - String.format( - "Can't use MUL with %s", - nxt.getClass() - ) - ); - } - if (result != null && !nxt.getClass().equals(result.getClass())) { - throw new TypeMismatchException( - String.format( - "Can't MUL %s to %s", - result.getClass(), - nxt.getClass() - ) - ); - } - if (result == null) { - result = nxt; - } else { - if (result instanceof Long) { - result = Long.class.cast(result) * Long.class.cast(nxt); - } else { - result = Float.class.cast(result) * Float.class.cast(nxt); - } - } - } - if (result == null) { - throw new ArgsException( - "At least one argument required by MUL" - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 4dabe2873e..0c57b1e9e8 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -24,10 +24,11 @@ package org.eolang.io; -import org.eolang.Primitive; +import java.io.PrintStream; import org.eolang.sys.EObool; import org.eolang.sys.EOstring; import org.eolang.sys.Phi; +import org.eolang.sys.Primitive; /** * Stdout. @@ -41,17 +42,32 @@ public final class EOstdout implements Phi { */ private final Phi text; + /** + * The print stream to print to. + */ + private final PrintStream stream; + /** * Ctor. * @param txt The text to print */ public EOstdout(final Phi txt) { + this(txt, System.out); + } + + /** + * Ctor. + * @param txt The text to print + * @param out The output stream + */ + public EOstdout(final Phi txt, final PrintStream out) { this.text = txt; + this.stream = out; } @Override public Phi 𝜑() { - System.out.print( + this.stream.print( new Primitive.End(this.text).take(EOstring.class).data() ); return new EObool(true); diff --git a/eo-runtime/src/main/java/org/eolang/EOless.java b/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java similarity index 51% rename from eo-runtime/src/main/java/org/eolang/EOless.java rename to eo-runtime/src/main/java/org/eolang/sys/ArgsException.java index 8139f8921b..47150d2cff 100644 --- a/eo-runtime/src/main/java/org/eolang/EOless.java +++ b/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java @@ -22,53 +22,26 @@ * SOFTWARE. */ -package org.eolang; - -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; +package org.eolang.sys; /** - * LESS. + * Something is wrong with arguments. * - * @since 0.2 + * @since 0.1 */ -public final class EOless implements Phi { +public final class ArgsException extends RuntimeException { /** - * Args. + * Serial ID. */ - private final Args args; + private static final long serialVersionUID = -6643350804302660951L; /** * Ctor. - * @param arg Args + * @param reason The reason */ - public EOless(final Args arg) { - this.args = arg; + public ArgsException(final String reason) { + super(reason); } - @Override - public Object call() throws Exception { - final Object left = this.args.call("01", Number.class); - final Object right = this.args.call("02", Number.class); - final boolean result; - if (left instanceof Long && right instanceof Long) { - result = Long.class.cast(left).compareTo( - Long.class.cast(right) - ) < 0; - } else if (left instanceof Float && right instanceof Float) { - result = Float.class.cast(left).compareTo( - Float.class.cast(right) - ) < 0; - } else { - throw new TypeMismatchException( - String.format( - "Can't compare %s with %s", - left.getClass(), - right.getClass() - ) - ); - } - return result; - } } diff --git a/eo-runtime/src/main/java/org/eolang/sys/EObool.java b/eo-runtime/src/main/java/org/eolang/sys/EObool.java index f81db7e796..53ee08caa5 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/sys/EObool.java @@ -24,8 +24,6 @@ package org.eolang.sys; -import org.eolang.Primitive; - /** * BOOLEAN. * @@ -48,7 +46,7 @@ public EObool(final boolean val) { @Override public Phi 𝜑() { - throw new UnsupportedOperationException("#𝜑()"); + return new EObool(this.data()); } @Override diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java b/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java index 10a5f597fe..06d8b19889 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java +++ b/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java @@ -24,8 +24,6 @@ package org.eolang.sys; -import org.eolang.Primitive; - /** * FLOAT. * @@ -48,7 +46,7 @@ public EOfloat(final double val) { @Override public Phi 𝜑() { - throw new UnsupportedOperationException("#𝜑()"); + return new EOfloat(this.data()); } @Override diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java b/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java index 6fdf421a1f..d0029a9998 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java +++ b/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java @@ -24,8 +24,6 @@ package org.eolang.sys; -import org.eolang.Primitive; - /** * INTEGER. * @@ -48,11 +46,65 @@ public EOinteger(final long val) { @Override public Phi 𝜑() { - throw new UnsupportedOperationException("#𝜑()"); + return new EOinteger(this.data()); } @Override public Long data() { return this.value; } + + /** + * Add a number. + * @param val The number + * @return New number + */ + public EOinteger add(final EOinteger val) { + return new EOinteger(this.value + val.data()); + } + + /** + * Subtract a number. + * @param val The number + * @return New number + */ + public EOinteger sub(final EOinteger val) { + return new EOinteger(this.value - val.data()); + } + + /** + * Multiply by the number. + * @param val The number + * @return New number + */ + public EOinteger mul(final EOinteger val) { + return new EOinteger(this.value * val.data()); + } + + /** + * Divide by the number. + * @param val The number + * @return New number + */ + public EOinteger div(final EOinteger val) { + return new EOinteger(this.value / val.data()); + } + + /** + * Inverse the sign. + * @return New number + */ + public EOinteger neg() { + return new EOinteger(-this.value); + } + + /** + * MOD operation. + * @param val The number + * @return New number + */ + public EOinteger mod(final EOinteger val) { + return new EOinteger(this.value + val.data()); + } + } diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOstring.java b/eo-runtime/src/main/java/org/eolang/sys/EOstring.java index e1e1302b63..d56e860e05 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EOstring.java +++ b/eo-runtime/src/main/java/org/eolang/sys/EOstring.java @@ -24,8 +24,6 @@ package org.eolang.sys; -import org.eolang.Primitive; - /** * STRING. * @@ -48,7 +46,7 @@ public EOstring(final String val) { @Override public Phi 𝜑() { - throw new UnsupportedOperationException("#𝜑()"); + return new EOstring(this.data()); } @Override diff --git a/eo-runtime/src/main/java/org/eolang/sys/Primitive.java b/eo-runtime/src/main/java/org/eolang/sys/Primitive.java index 1b8ddb5cf0..58a5659bd4 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/Primitive.java +++ b/eo-runtime/src/main/java/org/eolang/sys/Primitive.java @@ -22,10 +22,7 @@ * SOFTWARE. */ -package org.eolang; - -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; +package org.eolang.sys; /** * Data primitive. @@ -55,7 +52,13 @@ public > X take(final Class type) { if (result instanceof Primitive) { return type.cast(result); } else { - throw new TypeMismatchException(""); + throw new TypeMismatchException( + String.format( + "Can't cast from %s to %s", + result.getClass(), + type + ) + ); } } } diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index b27ea462a1..94fd884a51 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,9 +26,9 @@ import java.util.Collection; import java.util.LinkedList; -import org.eolang.Primitive; import org.eolang.sys.EOstring; import org.eolang.sys.Phi; +import org.eolang.sys.Primitive; /** * Sprintf. diff --git a/eo-runtime/src/test/java/org/eolang/EOmulTest.java b/eo-runtime/src/test/java/org/eolang/EOandTest.java similarity index 63% rename from eo-runtime/src/test/java/org/eolang/EOmulTest.java rename to eo-runtime/src/test/java/org/eolang/EOandTest.java index 4445714a6b..2aa1098fa1 100644 --- a/eo-runtime/src/test/java/org/eolang/EOmulTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOandTest.java @@ -23,43 +23,51 @@ */ package org.eolang; +import org.eolang.sys.EObool; +import org.eolang.sys.Primitive; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Test case for {@link AddOf}. + * Test case for {@link EOand}. * - * @since 0.2 + * @since 0.1 */ -public final class EOmulTest { +public final class EOandTest { @Test - public void mulsTwo() throws Exception { + public void checksTwo() { MatcherAssert.assertThat( - new EOmul( - new ArgsOf(new Entry("01", 1L), new Entry("02", -1L)) - ).call(), - Matchers.equalTo(-1L) + new Primitive.End( + new EOadd( + new EObool(true), + new EObool(false) + ) + ).take(EObool.class).data(), + Matchers.equalTo(false) ); } @Test - public void mulsOneArg() throws Exception { + public void checksOneArg() { MatcherAssert.assertThat( - new EOmul( - new ArgsOf(new Entry("01", 1L)) - ).call(), - Matchers.equalTo(1L) + new Primitive.End( + new EOadd( + new EObool(false) + ) + ).take(EObool.class).data(), + Matchers.equalTo(false) ); } @Test - public void mulsNoArgs() { - Assertions.assertThrows( - ArgsException.class, - () -> new EOmul(new ArgsOf()).call() + public void checksNoArgs() { + MatcherAssert.assertThat( + new Primitive.End( + new EOadd() + ).take(EObool.class).data(), + Matchers.equalTo(true) ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOaddTest.java b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java similarity index 64% rename from eo-runtime/src/test/java/org/eolang/EOaddTest.java rename to eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java index a4e0dd3034..b980f861f6 100644 --- a/eo-runtime/src/test/java/org/eolang/EOaddTest.java +++ b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java @@ -21,45 +21,37 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; +package org.eolang.io; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.eolang.Primitive; +import org.eolang.sys.EObool; +import org.eolang.sys.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Test case for {@link AddOf}. + * Test case for {@link EOstdout}. * * @since 0.1 */ -public final class EOaddTest { +public final class EOstdoutTest { @Test - public void addsTwo() throws Exception { + public void printsString() { + final String text = "Hello, друг!"; + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Primitive.End( + new EOstdout( + new EOstring(text), + new PrintStream(baos) + ) + ).take(EObool.class); MatcherAssert.assertThat( - new EOadd( - new ArgsOf(new Entry("01", 1L), new Entry("02", -1L)) - ).call(), - Matchers.equalTo(0L) - ); - } - - @Test - public void addsOneArg() throws Exception { - MatcherAssert.assertThat( - new EOadd( - new ArgsOf(new Entry("01", 1L)) - ).call(), - Matchers.equalTo(1L) - ); - } - - @Test - public void addsNoArgs() { - Assertions.assertThrows( - ArgsException.class, - () -> new EOadd(new ArgsOf()).call() + baos.toString(), + Matchers.equalTo(text) ); } diff --git a/eo-runtime/src/test/java/org/eolang/io/package-info.java b/eo-runtime/src/test/java/org/eolang/io/package-info.java new file mode 100644 index 0000000000..503d184c09 --- /dev/null +++ b/eo-runtime/src/test/java/org/eolang/io/package-info.java @@ -0,0 +1,30 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * EO system objects. + * + * @since 0.1 + */ +package org.eolang.io; diff --git a/eo-runtime/src/test/java/org/eolang/EOlessTest.java b/eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java similarity index 79% rename from eo-runtime/src/test/java/org/eolang/EOlessTest.java rename to eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java index 58e50e93b9..d48f193914 100644 --- a/eo-runtime/src/test/java/org/eolang/EOlessTest.java +++ b/eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java @@ -21,29 +21,26 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; +package org.eolang.sys; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; /** - * Test case for {@link EOless}. + * Test case for {@link EOinteger}. * - * @since 0.2 + * @since 0.1 */ -public final class EOlessTest { +public final class EOintegerTest { @Test - public void compares() throws Exception { + public void addsNumbers() { MatcherAssert.assertThat( - new EOless( - new ArgsOf( - new Entry("01", -1L), - new Entry("02", 1L) - ) - ).call(), - Matchers.equalTo(true) + new Primitive.End( + new EOinteger(1L) + ).take(EOstring.class).data(), + Matchers.equalTo(key) ); } diff --git a/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java b/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java index 1863768cca..a71994e684 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java +++ b/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java @@ -23,7 +23,6 @@ */ package org.eolang.sys; -import org.eolang.Primitive; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; From a4f66b32ab275fd4d60474098e520964ee2dcb0b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 2 Dec 2020 17:28:17 +0300 Subject: [PATCH 27/80] #164: dot suffix --- README.md | 2 +- .../antlr4/org/eolang/compiler/Program.g4 | 4 +++- .../main/java/org/eolang/compiler/Pack.java | 2 +- .../java/org/eolang/compiler/XeListener.java | 9 +++++++-- .../eolang/compiler/errors/broken-aliases.xsl | 3 +++ .../compiler/errors/duplicate-aliases.xsl | 3 +++ .../compiler/errors/duplicate-names.xsl | 3 +++ .../eolang/compiler/errors/global-nonames.xsl | 3 +++ .../{one-body.xsl => noname-attributes.xsl} | 17 +++++++++++------ .../eolang/compiler/errors/reserved-atoms.xsl | 5 ++++- .../compiler/errors/same-line-names.xsl | 3 +++ .../eolang/compiler/errors/self-naming.xsl | 3 +++ .../eolang/compiler/errors/unknown-names.xsl | 3 +++ .../java/org/eolang/compiler/PacksTest.java | 2 +- ...-bodies.yaml => catches-noname-attrs.yaml} | 5 +++-- .../packs/catches-reserved-atoms.yaml | 1 + .../org/eolang/compiler/packs/leap-year.yaml | 19 ++++++++++--------- 17 files changed, 63 insertions(+), 24 deletions(-) rename eo-compiler/src/main/resources/org/eolang/compiler/errors/{one-body.xsl => noname-attributes.xsl} (77%) rename eo-compiler/src/test/resources/org/eolang/compiler/packs/{catches-two-bodies.yaml => catches-noname-attrs.yaml} (69%) diff --git a/README.md b/README.md index 9ce883aa2a..9e7676ee7a 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ whether it's leap or not: eq. (mod. y 4) 0 not. (eq. (mod. y 100) 0) eq. (mod. y 400) 0 - and > @ + and. > @ stdout "Enter a year:" stdout sprintf diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index 893fae7687..ee5ebe54f6 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -86,7 +86,7 @@ suffix SPACE ARROW SPACE - NAME + name=( NAME | AT ) CONST? ; @@ -150,6 +150,8 @@ head | NAME | + NAME DOT + | data ; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 131830a71a..81bd637bba 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -56,7 +56,7 @@ public Pack() { "errors/broken-aliases.xsl", "errors/duplicate-aliases.xsl", "errors/global-nonames.xsl", - "errors/one-body.xsl", + "errors/noname-attributes.xsl", "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 5c302abf0a..8c328f2e31 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -181,7 +181,7 @@ public void exitTail(final ProgramParser.TailContext ctx) { @Override public void enterSuffix(final ProgramParser.SuffixContext ctx) { this.enter(); - this.dirs.attr("name", ctx.NAME().getText()); + this.dirs.attr("name", ctx.name.getText()); if (ctx.CONST() != null) { this.dirs.attr("const", ""); } @@ -208,7 +208,11 @@ public void exitMethod(final ProgramParser.MethodContext ctx) { public void enterHead(final ProgramParser.HeadContext ctx) { this.dirs.add("o").attr("line", ctx.getStart().getLine()); if (ctx.NAME() != null) { - this.dirs.attr("base", ctx.NAME().getText()); + String base = ctx.NAME().getText(); + if (ctx.DOT() != null) { + base = String.format(".%s", base); + } + this.dirs.attr("base", base); } if (ctx.AT() != null) { this.dirs.attr("base", "@"); @@ -284,6 +288,7 @@ public void enterData(final ProgramParser.DataContext ctx) { } else { throw new CompileException("Unknown data type"); } + this.dirs.attr("data", type); this.dirs.attr("base", type); this.dirs.set(data); } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl index 8c372620e6..355fdf81c5 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl @@ -29,6 +29,9 @@ SOFTWARE. + + broken-aliases + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl index 51903d2c8c..78f2024ba1 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl @@ -34,6 +34,9 @@ SOFTWARE. + + duplicate-aliases + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl index e18a85f07a..b4ea047b90 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl @@ -36,6 +36,9 @@ SOFTWARE. + + duplicate-names + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl index 46fcd0c8ad..eda86b1d22 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl @@ -28,6 +28,9 @@ SOFTWARE. + + global-nonames + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl similarity index 77% rename from eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl index 6f4c5467fa..1624d63ca7 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl @@ -22,18 +22,23 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + + + - - + + - - + + + + noname-attributes + @@ -43,7 +48,7 @@ SOFTWARE. " - may have only one body + has attribute without a name diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl index 7efca3d49c..ce22a1453d 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl @@ -27,9 +27,12 @@ SOFTWARE. - + + + reserved-atoms + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl index 39592421b6..3e634f73f2 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl @@ -35,6 +35,9 @@ SOFTWARE. + + same-line-names + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl index b395a5e82e..ebd1ffb4ce 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl @@ -28,6 +28,9 @@ SOFTWARE. + + self-naming + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl index 19098e3111..f3ca02db6e 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl @@ -28,6 +28,9 @@ SOFTWARE. + + unknown-names + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index f814b91d27..2491255ba3 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -85,7 +85,7 @@ private static Collection yamlPacks() { "catches-unknown-names.yaml", "catches-self-naming.yaml", "catches-reserved-atoms.yaml", - "catches-two-bodies.yaml", + "catches-noname-attrs.yaml", "catches-same-line-name.yaml", "fixes-globals.yaml", "adds-refs.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml similarity index 69% rename from eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml rename to eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml index d4fb4724b9..dcbeca5fab 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml @@ -1,8 +1,9 @@ xsls: - - errors/one-body.xsl + - errors/noname-attributes.xsl tests: - - /program/errors[count(error)=1] + - /program/errors[count(error)=2] - /program/errors/error[@line='1'] + - /program/errors/error[@line='8'] - //o[@base='first' and @line='2'] - //o[@base='second' and @line='3'] eo: | diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml index 327f80e231..7e200c19e2 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml @@ -6,5 +6,6 @@ eo: | [x] > first string > x "test" > works + "" > @ float > test 123 > string diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml index 29614945cf..d73fb64ded 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml @@ -1,6 +1,7 @@ tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] + - //o[@base='.and'] eo: | # This program is from the EOLANG original paper # and must produce a working command line tool @@ -13,15 +14,15 @@ eo: | [args] > main [y] > leap - or - and - eq (mod y 4) 0 - not (eq (mod y 100) 0) - eq (mod y 400) 0 - and + or. > @ + and. + eq. (mod. y 4) 0 + not. (eq. (mod. y 100) 0) + eq. (mod. y 400) 0 + and > @ stdout "Enter a year:" stdout - concat + sprintf + "%s is %sa leap year!" (scanner stdin).nextInt > year! - " is a leap year?" - leap year:y + if (leap year:y) "" "not " From f3bd4ec90acf9c9e608bb0467ead70eb2a66fe41 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 2 Dec 2020 20:16:13 +0300 Subject: [PATCH 28/80] #164: operator and reduce --- .../main/java/org/eolang/examples/Main.java | 2 - .../org/eolang/{sys => }/ArgsException.java | 2 +- .../eolang/{sys/Primitive.java => Data.java} | 24 +-- .../src/main/java/org/eolang/EOand.java | 63 -------- .../src/main/java/org/eolang/EOarray.java | 81 ---------- .../java/org/eolang/{sys => }/EObool.java | 12 +- .../java/org/eolang/{sys => }/EOfloat.java | 11 +- eo-runtime/src/main/java/org/eolang/EOif.java | 77 --------- .../src/main/java/org/eolang/EOinteger.java | 150 ++++++++++++++++++ eo-runtime/src/main/java/org/eolang/EOor.java | 61 ------- .../java/org/eolang/{sys => }/EOstring.java | 11 +- eo-runtime/src/main/java/org/eolang/Foo.java | 9 ++ .../src/main/java/org/eolang/Operator.java | 101 ++++++++++++ .../{sys => }/TypeMismatchException.java | 2 +- .../src/main/java/org/eolang/io/EOstdout.java | 19 +-- .../main/java/org/eolang/sys/EOinteger.java | 110 ------------- .../src/main/java/org/eolang/sys/Phi.java | 38 ----- .../java/org/eolang/sys/package-info.java | 30 ---- .../main/java/org/eolang/txt/EOsprintf.java | 27 ++-- .../src/test/java/org/eolang/EOandTest.java | 74 --------- .../src/test/java/org/eolang/EOifTest.java | 64 -------- .../org/eolang/{sys => }/EOintegerTest.java | 24 ++- .../org/eolang/{sys => }/EOstringTest.java | 4 +- .../test/java/org/eolang/io/EOstdoutTest.java | 8 +- .../java/org/eolang/sys/package-info.java | 30 ---- .../java/org/eolang/txt/EOsprintfTest.java | 10 +- 26 files changed, 334 insertions(+), 710 deletions(-) rename eo-runtime/src/main/java/org/eolang/{sys => }/ArgsException.java (98%) rename eo-runtime/src/main/java/org/eolang/{sys/Primitive.java => Data.java} (76%) delete mode 100644 eo-runtime/src/main/java/org/eolang/EOand.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOarray.java rename eo-runtime/src/main/java/org/eolang/{sys => }/EObool.java (88%) rename eo-runtime/src/main/java/org/eolang/{sys => }/EOfloat.java (88%) delete mode 100644 eo-runtime/src/main/java/org/eolang/EOif.java create mode 100644 eo-runtime/src/main/java/org/eolang/EOinteger.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOor.java rename eo-runtime/src/main/java/org/eolang/{sys => }/EOstring.java (87%) create mode 100644 eo-runtime/src/main/java/org/eolang/Foo.java create mode 100644 eo-runtime/src/main/java/org/eolang/Operator.java rename eo-runtime/src/main/java/org/eolang/{sys => }/TypeMismatchException.java (98%) delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/EOinteger.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/Phi.java delete mode 100644 eo-runtime/src/main/java/org/eolang/sys/package-info.java delete mode 100644 eo-runtime/src/test/java/org/eolang/EOandTest.java delete mode 100644 eo-runtime/src/test/java/org/eolang/EOifTest.java rename eo-runtime/src/test/java/org/eolang/{sys => }/EOintegerTest.java (72%) rename eo-runtime/src/test/java/org/eolang/{sys => }/EOstringTest.java (93%) delete mode 100644 eo-runtime/src/test/java/org/eolang/sys/package-info.java diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java index 0fc5bb57ef..1063e2d00e 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java +++ b/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java @@ -24,8 +24,6 @@ package org.eolang.examples; -import org.eolang.EOarray; - /** * EO entry point. * diff --git a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java b/eo-runtime/src/main/java/org/eolang/ArgsException.java similarity index 98% rename from eo-runtime/src/main/java/org/eolang/sys/ArgsException.java rename to eo-runtime/src/main/java/org/eolang/ArgsException.java index 47150d2cff..3abd57ae8c 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java +++ b/eo-runtime/src/main/java/org/eolang/ArgsException.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * Something is wrong with arguments. diff --git a/eo-runtime/src/main/java/org/eolang/sys/Primitive.java b/eo-runtime/src/main/java/org/eolang/Data.java similarity index 76% rename from eo-runtime/src/main/java/org/eolang/sys/Primitive.java rename to eo-runtime/src/main/java/org/eolang/Data.java index 58a5659bd4..65541ae254 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/Primitive.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -22,20 +22,20 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * Data primitive. * * @since 0.1 */ -public interface Primitive extends Phi { +public interface Data { /** * Take the take out. * @return The take */ - T data(); + T take(); /** * End point. @@ -43,19 +43,19 @@ public interface Primitive extends Phi { * @since 0.1 */ final class End { - private final Phi phi; - public End(final Phi src) { - this.phi = src; + private final Object object; + public End(final Object src) { + this.object = src; } - public > X take(final Class type) { - final Phi result = this.phi.𝜑(); - if (result instanceof Primitive) { - return type.cast(result); + @SuppressWarnings("unchecked") + public X take(final Class type) { + if (this.object instanceof Data) { + return ((Data) this.object).take(); } else { throw new TypeMismatchException( String.format( - "Can't cast from %s to %s", - result.getClass(), + "Can't cast from %s to Data<%s>", + this.object.getClass(), type ) ); diff --git a/eo-runtime/src/main/java/org/eolang/EOand.java b/eo-runtime/src/main/java/org/eolang/EOand.java deleted file mode 100644 index 00babb8d3b..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOand.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.eolang.sys.EObool; -import org.eolang.sys.Phi; -import org.eolang.sys.Primitive; - -/** - * AND. - * - * @since 0.1 - */ -public final class EOand implements Phi { - - /** - * Args. - */ - private final Phi[] args; - - /** - * Ctor. - * @param items The items - */ - public EOand(final Phi... items) { - this.args = items; - } - - @Override - public Phi 𝜑() { - boolean result = true; - for (final Phi arg : this.args) { - if (!new Primitive.End(arg).take(EObool.class).data()) { - result = false; - break; - } - } - return new EObool(result); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/EOarray.java b/eo-runtime/src/main/java/org/eolang/EOarray.java deleted file mode 100644 index 7a5e3a8dd2..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOarray.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -/** - * ARRAY. - * - * @since 0.1 - */ -public final class EOarray { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOarray(final Args arg) { - this.args = arg; - } - - /** - * The length of it. - * @param input Input args - * @return Length - */ - public Object length(final Args input) { - int size = 0; - for (final String key : this.args.keys()) { - if (key.charAt(0) != '0') { - continue; - } - ++size; - } - return size; - } - - /** - * Get element by index. - * @param input Input args - * @return The object - * @throws Exception If fails - */ - public Object get(final Args input) throws Exception { - final int index = input.call("01", Integer.class); - final Object result = this.args.call( - String.format("%02d", index), Object.class - ); - if (result == null) { - throw new ArgsException( - String.format("The item #%d is absent in the array", index) - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java similarity index 88% rename from eo-runtime/src/main/java/org/eolang/sys/EObool.java rename to eo-runtime/src/main/java/org/eolang/EObool.java index 53ee08caa5..cf1c427a43 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/EObool.java @@ -22,14 +22,14 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * BOOLEAN. * * @since 0.1 */ -public final class EObool implements Primitive { +public final class EObool implements Data { /** * The value. @@ -45,12 +45,8 @@ public EObool(final boolean val) { } @Override - public Phi 𝜑() { - return new EObool(this.data()); - } - - @Override - public Boolean data() { + public Boolean take() { return this.value; } + } diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java b/eo-runtime/src/main/java/org/eolang/EOfloat.java similarity index 88% rename from eo-runtime/src/main/java/org/eolang/sys/EOfloat.java rename to eo-runtime/src/main/java/org/eolang/EOfloat.java index 06d8b19889..55e4cf841d 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EOfloat.java +++ b/eo-runtime/src/main/java/org/eolang/EOfloat.java @@ -22,14 +22,14 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * FLOAT. * * @since 0.1 */ -public final class EOfloat implements Primitive { +public final class EOfloat implements Data { /** * The value. @@ -45,12 +45,7 @@ public EOfloat(final double val) { } @Override - public Phi 𝜑() { - return new EOfloat(this.data()); - } - - @Override - public Double data() { + public Double take() { return this.value; } } diff --git a/eo-runtime/src/main/java/org/eolang/EOif.java b/eo-runtime/src/main/java/org/eolang/EOif.java deleted file mode 100644 index 4b63459fa8..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOif.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.eolang.sys.EObool; -import org.eolang.sys.Phi; -import org.eolang.sys.Primitive; - -/** - * IF. - * - * @since 0.2 - */ -public final class EOif implements Phi { - - /** - * The condition. - */ - private final Phi term; - - /** - * When TRUE. - */ - private final Phi positive; - - /** - * When FALSE. - */ - private final Phi negative; - - /** - * Ctor. - * @param trm The term - * @param pos The positive - * @param neg The negative - */ - public EOif(final Phi trm, final Phi pos, final Phi neg) { - this.term = trm; - this.positive = pos; - this.negative = neg; - } - - @Override - public Phi 𝜑() { - final boolean yes = new Primitive.End(this.term) - .take(EObool.class).data(); - final Phi result; - if (yes) { - result = this.positive; - } else { - result = this.negative; - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOinteger.java b/eo-runtime/src/main/java/org/eolang/EOinteger.java new file mode 100644 index 0000000000..3321d3aee6 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOinteger.java @@ -0,0 +1,150 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +import org.cactoos.Scalar; +import org.cactoos.scalar.Unchecked; + +/** + * INTEGER. + * + * @since 0.1 + */ +public final class EOinteger implements Data { + + /** + * The value. + */ + private final Unchecked value; + + /** + * Ctor. + * @param val The value + */ + public EOinteger(final long val) { + this(() -> val); + } + + /** + * Ctor. + * @param val The value + */ + public EOinteger(final Scalar val) { + this.value = new Unchecked<>(val); + } + + @Override + public Long take() { + return this.value.value(); + } + + /** + * Add a number. + * + * new EOinteger(5).add().cp(5, 7, 7) + * + * @return New number + */ + public Operator add() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> { + long sum = start; + for (final Data arg : args) { + sum += arg.take(); + } + return sum; + } + ); + } + + /** + * Subtract a number. + * @return New number + */ + public Operator sub() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> start - args.get(0).take() + ); + } + + /** + * Multiply by the number. + * @return New number + */ + public Operator mul() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> { + long sum = start; + for (final Data arg : args) { + sum *= arg.take(); + } + return sum; + } + ); + } + + /** + * Divide by the number. + * @return New number + */ + public Operator div() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> start - args.get(0).take() + ); + } + + /** + * Inverse the sign. + * @return New number + */ + public Operator neg() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> -start + ); + } + + /** + * MOD operation. + * @return New number + */ + public Operator mod() { + return new Operator<>( + this, + EOinteger.class, + (start, args) -> -start + ); + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/EOor.java b/eo-runtime/src/main/java/org/eolang/EOor.java deleted file mode 100644 index fea480ab13..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOor.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Phi; - -/** - * OR. - * - * @since 0.1 - */ -public final class EOor implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOor(final Args arg) { - this.args = arg; - } - - @Override - public Object call() throws Exception { - boolean result = false; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - result |= this.args.call(key, Boolean.class).equals(Boolean.TRUE); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOstring.java b/eo-runtime/src/main/java/org/eolang/EOstring.java similarity index 87% rename from eo-runtime/src/main/java/org/eolang/sys/EOstring.java rename to eo-runtime/src/main/java/org/eolang/EOstring.java index d56e860e05..a863152b3d 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/EOstring.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring.java @@ -22,14 +22,14 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * STRING. * * @since 0.1 */ -public final class EOstring implements Primitive { +public final class EOstring implements Data { /** * The value. @@ -45,12 +45,7 @@ public EOstring(final String val) { } @Override - public Phi 𝜑() { - return new EOstring(this.data()); - } - - @Override - public String data() { + public String take() { return this.value; } } diff --git a/eo-runtime/src/main/java/org/eolang/Foo.java b/eo-runtime/src/main/java/org/eolang/Foo.java new file mode 100644 index 0000000000..208aa6a22a --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/Foo.java @@ -0,0 +1,9 @@ +package org.eolang; + +public final class Foo { + +// public long count(final File dir) { +// return Files.walk(dir.toPath()).count(); +// } + +} diff --git a/eo-runtime/src/main/java/org/eolang/Operator.java b/eo-runtime/src/main/java/org/eolang/Operator.java new file mode 100644 index 0000000000..d0c8dee3b9 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/Operator.java @@ -0,0 +1,101 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import org.cactoos.Scalar; +import org.cactoos.list.ListOf; + +/** + * One operation. + * + * @since 0.1 + */ +public final class Operator > { + + /** + * The start. + */ + private final Data start; + + /** + * The start. + */ + private final Class type; + + /** + * The reduce. + */ + private final Operator.Reduce reduce; + + /** + * Ctor. + * @param val The value + * @param tpe Type of result + * @param rdc Reduce + */ + public Operator(final Data val, final Class tpe, + final Operator.Reduce rdc) { + this.start = val; + this.type = tpe; + this.reduce = rdc; + } + + /** + * Make a copy of it. + * @param args The args + * @return Copy + */ + @SafeVarargs + public final T cp(final Data... args) { + try { + return this.type.getConstructor(Scalar.class).newInstance( + (Scalar) () -> this.reduce.calc( + this.start.take(), + new ListOf<>(args) + ) + ); + } catch (InstantiationException | IllegalAccessException + | InvocationTargetException | NoSuchMethodException ex) { + throw new IllegalStateException(ex); + } + } + + /** + * Reduce action. + * + * @since 0.1 + */ + public interface Reduce { + /** + * Do it. + * @param first First value + * @param args The args + * @return Result + */ + D calc(D first, List> args); + } +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java b/eo-runtime/src/main/java/org/eolang/TypeMismatchException.java similarity index 98% rename from eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java rename to eo-runtime/src/main/java/org/eolang/TypeMismatchException.java index d5f91dea53..5f914fe24b 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java +++ b/eo-runtime/src/main/java/org/eolang/TypeMismatchException.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** * TypeMismatch Exception. diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 0c57b1e9e8..b98152f6b5 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -25,22 +25,19 @@ package org.eolang.io; import java.io.PrintStream; -import org.eolang.sys.EObool; -import org.eolang.sys.EOstring; -import org.eolang.sys.Phi; -import org.eolang.sys.Primitive; +import org.eolang.Data; /** * Stdout. * * @since 0.1 */ -public final class EOstdout implements Phi { +public final class EOstdout implements Data { /** * The text to print. */ - private final Phi text; + private final Data text; /** * The print stream to print to. @@ -51,7 +48,7 @@ public final class EOstdout implements Phi { * Ctor. * @param txt The text to print */ - public EOstdout(final Phi txt) { + public EOstdout(final Data txt) { this(txt, System.out); } @@ -60,16 +57,16 @@ public EOstdout(final Phi txt) { * @param txt The text to print * @param out The output stream */ - public EOstdout(final Phi txt, final PrintStream out) { + public EOstdout(final Data txt, final PrintStream out) { this.text = txt; this.stream = out; } @Override - public Phi 𝜑() { + public Boolean take() { this.stream.print( - new Primitive.End(this.text).take(EOstring.class).data() + new Data.End(this.text).take(String.class) ); - return new EObool(true); + return true; } } diff --git a/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java b/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java deleted file mode 100644 index d0029a9998..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/EOinteger.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -/** - * INTEGER. - * - * @since 0.1 - */ -public final class EOinteger implements Primitive { - - /** - * The value. - */ - private final long value; - - /** - * Ctor. - * @param val The value - */ - public EOinteger(final long val) { - this.value = val; - } - - @Override - public Phi 𝜑() { - return new EOinteger(this.data()); - } - - @Override - public Long data() { - return this.value; - } - - /** - * Add a number. - * @param val The number - * @return New number - */ - public EOinteger add(final EOinteger val) { - return new EOinteger(this.value + val.data()); - } - - /** - * Subtract a number. - * @param val The number - * @return New number - */ - public EOinteger sub(final EOinteger val) { - return new EOinteger(this.value - val.data()); - } - - /** - * Multiply by the number. - * @param val The number - * @return New number - */ - public EOinteger mul(final EOinteger val) { - return new EOinteger(this.value * val.data()); - } - - /** - * Divide by the number. - * @param val The number - * @return New number - */ - public EOinteger div(final EOinteger val) { - return new EOinteger(this.value / val.data()); - } - - /** - * Inverse the sign. - * @return New number - */ - public EOinteger neg() { - return new EOinteger(-this.value); - } - - /** - * MOD operation. - * @param val The number - * @return New number - */ - public EOinteger mod(final EOinteger val) { - return new EOinteger(this.value + val.data()); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Phi.java b/eo-runtime/src/main/java/org/eolang/sys/Phi.java deleted file mode 100644 index 5f524699f9..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Phi.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.sys; - -/** - * Object with body. - * - * @since 0.1 - */ -public interface Phi { - /** - * Take the result. - * @return The result - */ - Phi 𝜑(); -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/package-info.java b/eo-runtime/src/main/java/org/eolang/sys/package-info.java deleted file mode 100644 index 3661289c42..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/package-info.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/** - * EO runtime, system. - * - * @since 0.2 - */ -package org.eolang.sys; diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 94fd884a51..b320c5420a 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,48 +26,45 @@ import java.util.Collection; import java.util.LinkedList; -import org.eolang.sys.EOstring; -import org.eolang.sys.Phi; -import org.eolang.sys.Primitive; +import org.eolang.Data; /** * Sprintf. * * @since 0.2 */ -public final class EOsprintf implements Phi { +public final class EOsprintf implements Data { /** * The format. */ - private final Phi format; + private final Data format; /** * Arguments. */ - private final Phi[] arguments; + private final Data[] arguments; /** * Ctor. * @param fmt Format * @param args Args */ - public EOsprintf(final Phi fmt, final Phi... args) { + public EOsprintf(final Data fmt, + final Data... args) { this.format = fmt; this.arguments = args; } @Override - public Phi 𝜑() { + public String take() { final Collection items = new LinkedList<>(); - for (final Phi arg : this.arguments) { - items.add(new Primitive.End(arg).take(EOstring.class).data()); + for (final Data arg : this.arguments) { + items.add(new Data.End(arg).take(Object.class)); } - return new EOstring( - String.format( - new Primitive.End(this.format).take(EOstring.class).data(), - items.toArray() - ) + return String.format( + new Data.End(this.format).take(String.class), + items.toArray() ); } } diff --git a/eo-runtime/src/test/java/org/eolang/EOandTest.java b/eo-runtime/src/test/java/org/eolang/EOandTest.java deleted file mode 100644 index 2aa1098fa1..0000000000 --- a/eo-runtime/src/test/java/org/eolang/EOandTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang; - -import org.eolang.sys.EObool; -import org.eolang.sys.Primitive; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link EOand}. - * - * @since 0.1 - */ -public final class EOandTest { - - @Test - public void checksTwo() { - MatcherAssert.assertThat( - new Primitive.End( - new EOadd( - new EObool(true), - new EObool(false) - ) - ).take(EObool.class).data(), - Matchers.equalTo(false) - ); - } - - @Test - public void checksOneArg() { - MatcherAssert.assertThat( - new Primitive.End( - new EOadd( - new EObool(false) - ) - ).take(EObool.class).data(), - Matchers.equalTo(false) - ); - } - - @Test - public void checksNoArgs() { - MatcherAssert.assertThat( - new Primitive.End( - new EOadd() - ).take(EObool.class).data(), - Matchers.equalTo(true) - ); - } - -} diff --git a/eo-runtime/src/test/java/org/eolang/EOifTest.java b/eo-runtime/src/test/java/org/eolang/EOifTest.java deleted file mode 100644 index a5df2728ae..0000000000 --- a/eo-runtime/src/test/java/org/eolang/EOifTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang; - -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link EOif}. - * - * @since 0.2 - */ -public final class EOifTest { - - @Test - public void compares() throws Exception { - MatcherAssert.assertThat( - new EOif( - new ArgsOf( - new Entry("01", true), - new Entry("02", -1), - new Entry("03", 0) - ) - ).call(), - Matchers.equalTo(-1) - ); - } - - @Test - public void comparesWithoutElse() throws Exception { - MatcherAssert.assertThat( - new EOif( - new ArgsOf( - new Entry("01", false), - new Entry("02", -1) - ) - ).call(), - Matchers.equalTo(false) - ); - } - -} diff --git a/eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java b/eo-runtime/src/test/java/org/eolang/EOintegerTest.java similarity index 72% rename from eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java rename to eo-runtime/src/test/java/org/eolang/EOintegerTest.java index d48f193914..ea7030fd10 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/EOintegerTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOintegerTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -37,10 +37,24 @@ public final class EOintegerTest { @Test public void addsNumbers() { MatcherAssert.assertThat( - new Primitive.End( - new EOinteger(1L) - ).take(EOstring.class).data(), - Matchers.equalTo(key) + new Data.End( + new EOinteger(1L).add().cp( + new EOinteger(5L), new EOinteger(-1L) + ) + ).take(Long.class), + Matchers.equalTo(5L) + ); + } + + @Test + public void multipliesNumbers() { + MatcherAssert.assertThat( + new Data.End( + new EOinteger(2L).mul().cp( + new EOinteger(4L), new EOinteger(-2L) + ) + ).take(Long.class), + Matchers.equalTo(-16L) ); } diff --git a/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java b/eo-runtime/src/test/java/org/eolang/EOstringTest.java similarity index 93% rename from eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java rename to eo-runtime/src/test/java/org/eolang/EOstringTest.java index a71994e684..3c50c43dbd 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/EOstringTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstringTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -38,7 +38,7 @@ public final class EOstringTest { public void encapsulatesString() { final String key = "hey"; MatcherAssert.assertThat( - new Primitive.End(new EOstring(key)).take(EOstring.class).data(), + new Data.End(new EOstring(key)).take(String.class), Matchers.equalTo(key) ); } diff --git a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java index b980f861f6..ca4214d86d 100644 --- a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java +++ b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java @@ -25,9 +25,9 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import org.eolang.Primitive; -import org.eolang.sys.EObool; -import org.eolang.sys.EOstring; +import org.eolang.Data; +import org.eolang.EObool; +import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -43,7 +43,7 @@ public final class EOstdoutTest { public void printsString() { final String text = "Hello, друг!"; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new Primitive.End( + new Data.End( new EOstdout( new EOstring(text), new PrintStream(baos) diff --git a/eo-runtime/src/test/java/org/eolang/sys/package-info.java b/eo-runtime/src/test/java/org/eolang/sys/package-info.java deleted file mode 100644 index 248f6771dc..0000000000 --- a/eo-runtime/src/test/java/org/eolang/sys/package-info.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/** - * EO system objects. - * - * @since 0.1 - */ -package org.eolang.sys; diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index c7481645bb..3ee0a2397a 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -23,9 +23,9 @@ */ package org.eolang.txt; -import org.eolang.Primitive; -import org.eolang.sys.EOinteger; -import org.eolang.sys.EOstring; +import org.eolang.Data; +import org.eolang.EOinteger; +import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -40,13 +40,13 @@ public final class EOsprintfTest { @Test public void printsSimpleText() { MatcherAssert.assertThat( - new Primitive.End( + new Data.End( new EOsprintf( new EOstring("Hello, %s %d!"), new EOstring("John"), new EOinteger(2L) ) - ).take(EOstring.class).data(), + ).take(String.class), Matchers.equalTo("Hello, John 2!") ); } From 3bf7292798aeadc2799a94587cac10a19f9b4b5d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 3 Dec 2020 12:48:59 +0300 Subject: [PATCH 29/80] #164: bytes --- .../antlr4/org/eolang/compiler/Program.g4 | 7 ++- .../java/org/eolang/compiler/XeListener.java | 20 +++---- .../eolang/compiler/errors/reserved-atoms.xsl | 41 ++++++++----- .../eolang/compiler/packs/all-data-types.yaml | 20 +++++++ .../packs/catches-reserved-atoms.yaml | 3 +- .../eolang/compiler/packs/full-syntax.yaml | 2 +- eo-runtime/src/main/java/org/eolang/Data.java | 6 +- .../src/main/java/org/eolang/EObool.java | 58 ++++++++++++++++++- .../src/main/java/org/eolang/EOfloat.java | 2 +- .../src/main/java/org/eolang/EOinteger.java | 22 +++---- .../src/main/java/org/eolang/EOstring.java | 2 +- eo-runtime/src/main/java/org/eolang/Foo.java | 9 --- .../src/main/java/org/eolang/io/EOstdout.java | 4 +- .../main/java/org/eolang/txt/EOsprintf.java | 6 +- .../src/test/java/org/eolang/EOboolTest.java | 50 ++++++++++++++++ .../test/java/org/eolang/EOintegerTest.java | 8 +-- .../test/java/org/eolang/EOstringTest.java | 2 +- .../test/java/org/eolang/io/EOstdoutTest.java | 3 +- .../java/org/eolang/txt/EOsprintfTest.java | 2 +- 19 files changed, 199 insertions(+), 68 deletions(-) create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml delete mode 100644 eo-runtime/src/main/java/org/eolang/Foo.java create mode 100644 eo-runtime/src/test/java/org/eolang/EOboolTest.java diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index ee5ebe54f6..ade475cc41 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -163,11 +163,13 @@ has data : + BYTES + | BOOL | STRING | - INTEGER + INT | FLOAT | @@ -212,10 +214,11 @@ EOL } ; +BYTES: (('0'..'9' | 'A'..'F') '-')+; BOOL: 'true' | 'false'; CHAR: '\'' (LETTER | DIGIT) '\''; STRING: '"' ('\\"' | ~'"')* '"'; -INTEGER: (PLUS | MINUS)? DIGIT+; +INT: (PLUS | MINUS)? DIGIT+; FLOAT: (PLUS | MINUS)? DIGIT+ DOT DIGIT+; HEX: '0x' (DIGIT | 'a'..'f')+; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 8c328f2e31..2a62b7eef0 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -255,12 +255,16 @@ public void exitHtail(final ProgramParser.HtailContext ctx) { this.dirs.up(); } + // @checkstyle ExecutableStatementCountCheck (100 lines) @Override @SuppressWarnings("PMD.ConfusingTernary") public void enterData(final ProgramParser.DataContext ctx) { final String type; final String data; - if (ctx.BOOL() != null) { + if (ctx.BYTES() != null) { + type = "bytes"; + data = ctx.getText().replace("-", " ").trim(); + } else if (ctx.BOOL() != null) { type = "bool"; data = Boolean.toString(Boolean.parseBoolean(ctx.getText())); } else if (ctx.CHAR() != null) { @@ -269,18 +273,14 @@ public void enterData(final ProgramParser.DataContext ctx) { } else if (ctx.FLOAT() != null) { type = "float"; data = Float.toString(Float.parseFloat(ctx.getText())); - } else if (ctx.INTEGER() != null) { + } else if (ctx.INT() != null) { type = "integer"; data = Long.toString(Long.parseLong(ctx.getText())); } else if (ctx.HEX() != null) { - type = "hex"; - data = String.format( - "0x%s", - Long.toString( - // @checkstyle MagicNumberCheck (2 line) - Long.parseLong(ctx.getText().substring(2), 16), - 16 - ) + type = "integer"; + data = Long.toString( + // @checkstyle MagicNumberCheck (1 line) + Long.parseLong(ctx.getText().substring(2), 16) ); } else if (ctx.STRING() != null) { type = "string"; diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl index ce22a1453d..533a025fc1 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl @@ -24,23 +24,34 @@ SOFTWARE. --> - + - - - - - reserved-atoms - - - - - You can't copy " - - " as a normal object - - + + + + reserved-atoms + + + + + You can't copy " + + " as a normal object + + + + + + reserved-atoms + + + + + You can't use " + + " as a name of your object, it's a reserved name + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml new file mode 100644 index 0000000000..b6bf93aa07 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml @@ -0,0 +1,20 @@ +xsls: [] +tests: + - /program/errors[count(*)=0] + - //o[@base='bytes' and @name='floatX' and @atom] + - //o[@base='bool' and @name='boolX' and @atom] + - //o[@base='int' and @name='intX' and @atom] + - //o[@base='float' and @name='floatX' and @atom] + - //o[@base='string' and @name='stringX' and @atom] +eo: | + 00-02-4F-1A-FF-01- > bytesX + + 3.1415926 > floatX + + 0x1ffff > integerX + + "Hello, друг! 谢谢!" > stringX + + 'z' > charX + + true > boolX diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml index 7e200c19e2..39fe72d866 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml @@ -1,11 +1,12 @@ xsls: - errors/reserved-atoms.xsl tests: - - /program/errors[count(error)=2] + - /program/errors[count(error)=3] eo: | [x] > first string > x "test" > works + withoutaname "" > @ float > test 123 > string diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 1b7cf60deb..5494195fc8 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -8,7 +8,7 @@ tests: - //o[@base='string'] - //o[@base='char' and text()='e'] - //o[@base='float' and text()='500.43'] - - //o[@base='hex' and text()='0x1f'] + - //o[@base='integer' and text()='31'] - //o[@base='string' and text()='Hello, world!'] - //o[@name='hello' and @const] - //o[@as='i'] diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 65541ae254..2a90c66afa 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -35,7 +35,7 @@ public interface Data { * Take the take out. * @return The take */ - T take(); + T 𝜑(); /** * End point. @@ -48,9 +48,9 @@ public End(final Object src) { this.object = src; } @SuppressWarnings("unchecked") - public X take(final Class type) { + public X 𝜑(final Class type) { if (this.object instanceof Data) { - return ((Data) this.object).take(); + return ((Data) this.object).𝜑(); } else { throw new TypeMismatchException( String.format( diff --git a/eo-runtime/src/main/java/org/eolang/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java index cf1c427a43..6dacac46e9 100644 --- a/eo-runtime/src/main/java/org/eolang/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/EObool.java @@ -45,8 +45,64 @@ public EObool(final boolean val) { } @Override - public Boolean take() { + public Boolean 𝜑() { return this.value; } + /** + * Makes a fork. + * + * new EObool(true).if().cp("left", "right") + * + * @return Result + */ + public EObool.OpIf eoif() { + return new EObool.OpIf<>(); + } + + /** + * Operator IF. + * + * @param The type of return + * @since 0.1 + */ + public final class OpIf { + public T cp(final T left, final T right) { + final T result; + if (EObool.this.𝜑()) { + result = left; + } else { + result =right; + } + return result; + } + } + +// /** +// * Makes a WHILE. +// * +// * new EObool(true).while().cp("result") +// * +// * @return Result +// */ +// public EObool.OpIf eowhile() { +// return new EObool.OpWhile<>(); +// } +// +// /** +// * Operator WHILE. +// * +// * @param The type of return +// * @since 0.1 +// */ +// public final class OpWhile > { +// public T cp(final T body) { +// final T result; +// while (EObool.this.𝜑()) { +// result = body.𝜑; +// } +// return result; +// } +// } + } diff --git a/eo-runtime/src/main/java/org/eolang/EOfloat.java b/eo-runtime/src/main/java/org/eolang/EOfloat.java index 55e4cf841d..53734cf538 100644 --- a/eo-runtime/src/main/java/org/eolang/EOfloat.java +++ b/eo-runtime/src/main/java/org/eolang/EOfloat.java @@ -45,7 +45,7 @@ public EOfloat(final double val) { } @Override - public Double take() { + public Double 𝜑() { return this.value; } } diff --git a/eo-runtime/src/main/java/org/eolang/EOinteger.java b/eo-runtime/src/main/java/org/eolang/EOinteger.java index 3321d3aee6..3857624a23 100644 --- a/eo-runtime/src/main/java/org/eolang/EOinteger.java +++ b/eo-runtime/src/main/java/org/eolang/EOinteger.java @@ -56,7 +56,7 @@ public EOinteger(final Scalar val) { } @Override - public Long take() { + public Long 𝜑() { return this.value.value(); } @@ -67,14 +67,14 @@ public Long take() { * * @return New number */ - public Operator add() { + public Operator eoadd() { return new Operator<>( this, EOinteger.class, (start, args) -> { long sum = start; for (final Data arg : args) { - sum += arg.take(); + sum += arg.𝜑(); } return sum; } @@ -85,11 +85,11 @@ public Operator add() { * Subtract a number. * @return New number */ - public Operator sub() { + public Operator eosub() { return new Operator<>( this, EOinteger.class, - (start, args) -> start - args.get(0).take() + (start, args) -> start - args.get(0).𝜑() ); } @@ -97,14 +97,14 @@ public Operator sub() { * Multiply by the number. * @return New number */ - public Operator mul() { + public Operator eomul() { return new Operator<>( this, EOinteger.class, (start, args) -> { long sum = start; for (final Data arg : args) { - sum *= arg.take(); + sum *= arg.𝜑(); } return sum; } @@ -115,11 +115,11 @@ public Operator mul() { * Divide by the number. * @return New number */ - public Operator div() { + public Operator eodiv() { return new Operator<>( this, EOinteger.class, - (start, args) -> start - args.get(0).take() + (start, args) -> start - args.get(0).𝜑() ); } @@ -127,7 +127,7 @@ public Operator div() { * Inverse the sign. * @return New number */ - public Operator neg() { + public Operator eoneg() { return new Operator<>( this, EOinteger.class, @@ -139,7 +139,7 @@ public Operator neg() { * MOD operation. * @return New number */ - public Operator mod() { + public Operator eomod() { return new Operator<>( this, EOinteger.class, diff --git a/eo-runtime/src/main/java/org/eolang/EOstring.java b/eo-runtime/src/main/java/org/eolang/EOstring.java index a863152b3d..fdd5b9a61c 100644 --- a/eo-runtime/src/main/java/org/eolang/EOstring.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring.java @@ -45,7 +45,7 @@ public EOstring(final String val) { } @Override - public String take() { + public String 𝜑() { return this.value; } } diff --git a/eo-runtime/src/main/java/org/eolang/Foo.java b/eo-runtime/src/main/java/org/eolang/Foo.java deleted file mode 100644 index 208aa6a22a..0000000000 --- a/eo-runtime/src/main/java/org/eolang/Foo.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.eolang; - -public final class Foo { - -// public long count(final File dir) { -// return Files.walk(dir.toPath()).count(); -// } - -} diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index b98152f6b5..7a3a927ba6 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -63,9 +63,9 @@ public EOstdout(final Data txt, final PrintStream out) { } @Override - public Boolean take() { + public Boolean 𝜑() { this.stream.print( - new Data.End(this.text).take(String.class) + new Data.End(this.text).𝜑(String.class) ); return true; } diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index b320c5420a..6635c96f3b 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -57,13 +57,13 @@ public EOsprintf(final Data fmt, } @Override - public String take() { + public String 𝜑() { final Collection items = new LinkedList<>(); for (final Data arg : this.arguments) { - items.add(new Data.End(arg).take(Object.class)); + items.add(new Data.End(arg).𝜑(Object.class)); } return String.format( - new Data.End(this.format).take(String.class), + new Data.End(this.format).𝜑(String.class), items.toArray() ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOboolTest.java b/eo-runtime/src/test/java/org/eolang/EOboolTest.java new file mode 100644 index 0000000000..e802ac03e2 --- /dev/null +++ b/eo-runtime/src/test/java/org/eolang/EOboolTest.java @@ -0,0 +1,50 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link EObool}. + * + * @since 0.1 + */ +public final class EOboolTest { + + @Test + public void makesFork() { + MatcherAssert.assertThat( + new Data.End( + new EObool(true).eoif().cp( + new EOstring("Hello"), + new EOstring("world") + ) + ).take(String.class), + Matchers.equalTo("Hello") + ); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/EOintegerTest.java b/eo-runtime/src/test/java/org/eolang/EOintegerTest.java index ea7030fd10..218c42050f 100644 --- a/eo-runtime/src/test/java/org/eolang/EOintegerTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOintegerTest.java @@ -38,10 +38,10 @@ public final class EOintegerTest { public void addsNumbers() { MatcherAssert.assertThat( new Data.End( - new EOinteger(1L).add().cp( + new EOinteger(1L).eoadd().cp( new EOinteger(5L), new EOinteger(-1L) ) - ).take(Long.class), + ).𝜑(Long.class), Matchers.equalTo(5L) ); } @@ -50,10 +50,10 @@ public void addsNumbers() { public void multipliesNumbers() { MatcherAssert.assertThat( new Data.End( - new EOinteger(2L).mul().cp( + new EOinteger(2L).eomul().cp( new EOinteger(4L), new EOinteger(-2L) ) - ).take(Long.class), + ).𝜑(Long.class), Matchers.equalTo(-16L) ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOstringTest.java b/eo-runtime/src/test/java/org/eolang/EOstringTest.java index 3c50c43dbd..595735426a 100644 --- a/eo-runtime/src/test/java/org/eolang/EOstringTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstringTest.java @@ -38,7 +38,7 @@ public final class EOstringTest { public void encapsulatesString() { final String key = "hey"; MatcherAssert.assertThat( - new Data.End(new EOstring(key)).take(String.class), + new Data.End(new EOstring(key)).𝜑(String.class), Matchers.equalTo(key) ); } diff --git a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java index ca4214d86d..c39b23987e 100644 --- a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java +++ b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java @@ -26,7 +26,6 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.eolang.Data; -import org.eolang.EObool; import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -48,7 +47,7 @@ public void printsString() { new EOstring(text), new PrintStream(baos) ) - ).take(EObool.class); + ).𝜑(Boolean.class); MatcherAssert.assertThat( baos.toString(), Matchers.equalTo(text) diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index 3ee0a2397a..cfbf539573 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -46,7 +46,7 @@ public void printsSimpleText() { new EOstring("John"), new EOinteger(2L) ) - ).take(String.class), + ).𝜑(String.class), Matchers.equalTo("Hello, John 2!") ); } From 4ccd89fdabd4b1db64c9a092167cf32017e02f59 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 3 Dec 2020 13:27:46 +0300 Subject: [PATCH 30/80] #164: syntax fix --- .../main/antlr4/org/eolang/compiler/Program.g4 | 4 +++- .../java/org/eolang/compiler/XeListener.java | 4 ++-- .../java/org/eolang/compiler/PacksTest.java | 1 + .../eolang/compiler/packs/all-data-types.yaml | 17 ++++++++++------- .../org/eolang/compiler/packs/full-syntax.yaml | 2 +- .../eolang/compiler/packs/wraps-methods.yaml | 2 +- .../main/resources/org/eolang/maven/to-java.xsl | 7 +------ 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index ade475cc41..ab497559b5 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -214,7 +214,9 @@ EOL } ; -BYTES: (('0'..'9' | 'A'..'F') '-')+; +fragment BYTE: [0-9A-F][0-9A-F] MINUS; +BYTES: BYTE (BYTE* [0-9A-F][0-9A-F])?; + BOOL: 'true' | 'false'; CHAR: '\'' (LETTER | DIGIT) '\''; STRING: '"' ('\\"' | ~'"')* '"'; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 2a62b7eef0..08746f5b88 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -274,10 +274,10 @@ public void enterData(final ProgramParser.DataContext ctx) { type = "float"; data = Float.toString(Float.parseFloat(ctx.getText())); } else if (ctx.INT() != null) { - type = "integer"; + type = "int"; data = Long.toString(Long.parseLong(ctx.getText())); } else if (ctx.HEX() != null) { - type = "integer"; + type = "int"; data = Long.toString( // @checkstyle MagicNumberCheck (1 line) Long.parseLong(ctx.getText().substring(2), 16) diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 2491255ba3..8b6f4be27b 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -87,6 +87,7 @@ private static Collection yamlPacks() { "catches-reserved-atoms.yaml", "catches-noname-attrs.yaml", "catches-same-line-name.yaml", + "all-data-types.yaml", "fixes-globals.yaml", "adds-refs.yaml", "float-vars.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml index b6bf93aa07..df334f82be 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml @@ -1,17 +1,20 @@ xsls: [] tests: - /program/errors[count(*)=0] - - //o[@base='bytes' and @name='floatX' and @atom] - - //o[@base='bool' and @name='boolX' and @atom] - - //o[@base='int' and @name='intX' and @atom] - - //o[@base='float' and @name='floatX' and @atom] - - //o[@base='string' and @name='stringX' and @atom] + - //objects[count(o[@base='bytes'])=2] + - //o[@base='bytes' and @name='bytesX' and @data] + - //o[@base='bool' and @name='boolX' and @data] + - //o[@base='int' and @name='intX' and @data] + - //o[@base='float' and @name='floatX' and @data] + - //o[@base='string' and @name='stringX' and @data] eo: | - 00-02-4F-1A-FF-01- > bytesX + 01- > bytesX + + 00-02-4F-1A-FF-01 > bytesX 3.1415926 > floatX - 0x1ffff > integerX + 0x1ffff > intX "Hello, друг! 谢谢!" > stringX diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 5494195fc8..a2cabcb388 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -8,7 +8,7 @@ tests: - //o[@base='string'] - //o[@base='char' and text()='e'] - //o[@base='float' and text()='500.43'] - - //o[@base='integer' and text()='31'] + - //o[@base='int' and text()='31'] - //o[@base='string' and text()='Hello, world!'] - //o[@name='hello' and @const] - //o[@as='i'] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 24832df110..80af7868c8 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -7,7 +7,7 @@ tests: - //o[@base='.foo' and count(./o)=1] - //o[@base='.trim' and count(./o)=2] - //o[@base='.trim']/o[1][@base='.get'] - - //o[@base='.trim']/o[2][@base='integer'] + - //o[@base='.trim']/o[2][@base='int'] eo: | [args] > app stdout diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 0c283b4d92..00dce53a6c 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -307,12 +307,7 @@ SOFTWARE. " ) - - new EOinteger( - - L) - - + new EOinteger( L) From 8734a57a34f0cd59a34e8a6b385d635e7e39695a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 3 Dec 2020 19:22:55 +0300 Subject: [PATCH 31/80] #164: if works --- .../eolang/compiler/packs/all-data-types.yaml | 1 + eo-runtime/src/main/java/org/eolang/Data.java | 28 +------ .../src/main/java/org/eolang/EObool.java | 78 +++++++++---------- .../src/main/java/org/eolang/EOfloat.java | 21 +++-- .../org/eolang/{EOinteger.java => EOint.java} | 40 +++++----- .../src/main/java/org/eolang/EOstring.java | 21 +++-- .../src/main/java/org/eolang/Operator.java | 2 +- .../src/main/java/org/eolang/io/EOstdout.java | 34 +++----- .../main/java/org/eolang/txt/EOsprintf.java | 39 +++------- .../src/test/java/org/eolang/EOboolTest.java | 18 +++-- .../{EOintegerTest.java => EOintTest.java} | 20 ++--- .../test/java/org/eolang/EOstringTest.java | 10 ++- .../test/java/org/eolang/io/EOstdoutTest.java | 11 +-- .../java/org/eolang/txt/EOsprintfTest.java | 15 ++-- 14 files changed, 155 insertions(+), 183 deletions(-) rename eo-runtime/src/main/java/org/eolang/{EOinteger.java => EOint.java} (78%) rename eo-runtime/src/test/java/org/eolang/{EOintegerTest.java => EOintTest.java} (78%) diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml index df334f82be..2b208f8e68 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml @@ -7,6 +7,7 @@ tests: - //o[@base='int' and @name='intX' and @data] - //o[@base='float' and @name='floatX' and @data] - //o[@base='string' and @name='stringX' and @data] + - //o[@base='char' and @name='charX' and @data] eo: | 01- > bytesX diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 2a90c66afa..687948a69a 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -35,32 +35,6 @@ public interface Data { * Take the take out. * @return The take */ - T 𝜑(); - - /** - * End point. - * - * @since 0.1 - */ - final class End { - private final Object object; - public End(final Object src) { - this.object = src; - } - @SuppressWarnings("unchecked") - public X 𝜑(final Class type) { - if (this.object instanceof Data) { - return ((Data) this.object).𝜑(); - } else { - throw new TypeMismatchException( - String.format( - "Can't cast from %s to Data<%s>", - this.object.getClass(), - type - ) - ); - } - } - } + T get(); } diff --git a/eo-runtime/src/main/java/org/eolang/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java index 6dacac46e9..c8d06ad1e7 100644 --- a/eo-runtime/src/main/java/org/eolang/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/EObool.java @@ -24,29 +24,42 @@ package org.eolang; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import org.cactoos.Scalar; +import org.cactoos.scalar.Unchecked; + /** * BOOLEAN. * * @since 0.1 */ -public final class EObool implements Data { +public class EObool implements Data { /** * The value. */ - private final boolean value; + private final Unchecked value; /** * Ctor. * @param val The value */ public EObool(final boolean val) { - this.value = val; + this(() -> val); + } + + /** + * Ctor. + * @param val The value + */ + public EObool(final Scalar val) { + this.value = new Unchecked<>(val); } @Override - public Boolean 𝜑() { - return this.value; + public final Boolean get() { + return this.value.value(); } /** @@ -66,43 +79,28 @@ public EObool.OpIf eoif() { * @param The type of return * @since 0.1 */ + @SuppressWarnings("unchecked") public final class OpIf { - public T cp(final T left, final T right) { - final T result; - if (EObool.this.𝜑()) { - result = left; - } else { - result =right; + public Data cp(final Data left, final Data right) { + try { + final Constructor> ctor = + (Constructor>) left.getClass() + .getDeclaredConstructor(Scalar.class); + return ctor.newInstance( + (Scalar) () -> { + final T result; + if (EObool.this.get()) { + result = (T) left.get(); + } else { + result = (T) right.get(); + } + return result; + } + ); + } catch (NoSuchMethodException | IllegalAccessException + | InstantiationException | InvocationTargetException ex) { + throw new IllegalStateException(ex); } - return result; } } - -// /** -// * Makes a WHILE. -// * -// * new EObool(true).while().cp("result") -// * -// * @return Result -// */ -// public EObool.OpIf eowhile() { -// return new EObool.OpWhile<>(); -// } -// -// /** -// * Operator WHILE. -// * -// * @param The type of return -// * @since 0.1 -// */ -// public final class OpWhile > { -// public T cp(final T body) { -// final T result; -// while (EObool.this.𝜑()) { -// result = body.𝜑; -// } -// return result; -// } -// } - } diff --git a/eo-runtime/src/main/java/org/eolang/EOfloat.java b/eo-runtime/src/main/java/org/eolang/EOfloat.java index 53734cf538..e3d7955301 100644 --- a/eo-runtime/src/main/java/org/eolang/EOfloat.java +++ b/eo-runtime/src/main/java/org/eolang/EOfloat.java @@ -24,28 +24,39 @@ package org.eolang; +import org.cactoos.Scalar; +import org.cactoos.scalar.Unchecked; + /** * FLOAT. * * @since 0.1 */ -public final class EOfloat implements Data { +public class EOfloat implements Data { /** * The value. */ - private final double value; + private final Unchecked value; /** * Ctor. * @param val The value */ public EOfloat(final double val) { - this.value = val; + this(() -> val); + } + + /** + * Ctor. + * @param val The value + */ + public EOfloat(final Scalar val) { + this.value = new Unchecked<>(val); } @Override - public Double 𝜑() { - return this.value; + public final Double get() { + return this.value.value(); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOinteger.java b/eo-runtime/src/main/java/org/eolang/EOint.java similarity index 78% rename from eo-runtime/src/main/java/org/eolang/EOinteger.java rename to eo-runtime/src/main/java/org/eolang/EOint.java index 3857624a23..faa7cb7655 100644 --- a/eo-runtime/src/main/java/org/eolang/EOinteger.java +++ b/eo-runtime/src/main/java/org/eolang/EOint.java @@ -32,7 +32,7 @@ * * @since 0.1 */ -public final class EOinteger implements Data { +public final class EOint implements Data { /** * The value. @@ -43,7 +43,7 @@ public final class EOinteger implements Data { * Ctor. * @param val The value */ - public EOinteger(final long val) { + public EOint(final long val) { this(() -> val); } @@ -51,12 +51,12 @@ public EOinteger(final long val) { * Ctor. * @param val The value */ - public EOinteger(final Scalar val) { + public EOint(final Scalar val) { this.value = new Unchecked<>(val); } @Override - public Long 𝜑() { + public Long get() { return this.value.value(); } @@ -67,14 +67,14 @@ public EOinteger(final Scalar val) { * * @return New number */ - public Operator eoadd() { + public Operator eoadd() { return new Operator<>( this, - EOinteger.class, + EOint.class, (start, args) -> { long sum = start; for (final Data arg : args) { - sum += arg.𝜑(); + sum += arg.get(); } return sum; } @@ -85,11 +85,11 @@ public Operator eoadd() { * Subtract a number. * @return New number */ - public Operator eosub() { + public Operator eosub() { return new Operator<>( this, - EOinteger.class, - (start, args) -> start - args.get(0).𝜑() + EOint.class, + (start, args) -> start - args.get(0).get() ); } @@ -97,14 +97,14 @@ public Operator eosub() { * Multiply by the number. * @return New number */ - public Operator eomul() { + public Operator eomul() { return new Operator<>( this, - EOinteger.class, + EOint.class, (start, args) -> { long sum = start; for (final Data arg : args) { - sum *= arg.𝜑(); + sum *= arg.get(); } return sum; } @@ -115,11 +115,11 @@ public Operator eomul() { * Divide by the number. * @return New number */ - public Operator eodiv() { + public Operator eodiv() { return new Operator<>( this, - EOinteger.class, - (start, args) -> start - args.get(0).𝜑() + EOint.class, + (start, args) -> start - args.get(0).get() ); } @@ -127,10 +127,10 @@ public Operator eodiv() { * Inverse the sign. * @return New number */ - public Operator eoneg() { + public Operator eoneg() { return new Operator<>( this, - EOinteger.class, + EOint.class, (start, args) -> -start ); } @@ -139,10 +139,10 @@ public Operator eoneg() { * MOD operation. * @return New number */ - public Operator eomod() { + public Operator eomod() { return new Operator<>( this, - EOinteger.class, + EOint.class, (start, args) -> -start ); } diff --git a/eo-runtime/src/main/java/org/eolang/EOstring.java b/eo-runtime/src/main/java/org/eolang/EOstring.java index fdd5b9a61c..429a98500b 100644 --- a/eo-runtime/src/main/java/org/eolang/EOstring.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring.java @@ -24,28 +24,39 @@ package org.eolang; +import org.cactoos.Scalar; +import org.cactoos.scalar.Unchecked; + /** * STRING. * * @since 0.1 */ -public final class EOstring implements Data { +public class EOstring implements Data { /** * The value. */ - private final String value; + private final Unchecked value; /** * Ctor. * @param val The value */ public EOstring(final String val) { - this.value = val; + this(() -> val); + } + + /** + * Ctor. + * @param val The value + */ + public EOstring(final Scalar val) { + this.value = new Unchecked<>(val); } @Override - public String 𝜑() { - return this.value; + public final String get() { + return this.value.value(); } } diff --git a/eo-runtime/src/main/java/org/eolang/Operator.java b/eo-runtime/src/main/java/org/eolang/Operator.java index d0c8dee3b9..2a5db02163 100644 --- a/eo-runtime/src/main/java/org/eolang/Operator.java +++ b/eo-runtime/src/main/java/org/eolang/Operator.java @@ -74,7 +74,7 @@ public final T cp(final Data... args) { try { return this.type.getConstructor(Scalar.class).newInstance( (Scalar) () -> this.reduce.calc( - this.start.take(), + this.start.get(), new ListOf<>(args) ) ); diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 7a3a927ba6..7d21691396 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -26,23 +26,14 @@ import java.io.PrintStream; import org.eolang.Data; +import org.eolang.EObool; /** * Stdout. * * @since 0.1 */ -public final class EOstdout implements Data { - - /** - * The text to print. - */ - private final Data text; - - /** - * The print stream to print to. - */ - private final PrintStream stream; +public final class EOstdout extends EObool { /** * Ctor. @@ -54,19 +45,16 @@ public EOstdout(final Data txt) { /** * Ctor. - * @param txt The text to print - * @param out The output stream + * @param stream The text to print + * @param text The output stream */ - public EOstdout(final Data txt, final PrintStream out) { - this.text = txt; - this.stream = out; - } - - @Override - public Boolean 𝜑() { - this.stream.print( - new Data.End(this.text).𝜑(String.class) + public EOstdout(final Data text, final PrintStream stream) { + super( + () -> { + stream.print(text.get()); + return true; + } ); - return true; } + } diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 6635c96f3b..8d81a7f6a9 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -27,44 +27,29 @@ import java.util.Collection; import java.util.LinkedList; import org.eolang.Data; +import org.eolang.EOstring; /** * Sprintf. * * @since 0.2 */ -public final class EOsprintf implements Data { - - /** - * The format. - */ - private final Data format; - - /** - * Arguments. - */ - private final Data[] arguments; +public final class EOsprintf extends EOstring { /** * Ctor. - * @param fmt Format + * @param format Format * @param args Args */ - public EOsprintf(final Data fmt, - final Data... args) { - this.format = fmt; - this.arguments = args; - } - - @Override - public String 𝜑() { - final Collection items = new LinkedList<>(); - for (final Data arg : this.arguments) { - items.add(new Data.End(arg).𝜑(Object.class)); - } - return String.format( - new Data.End(this.format).𝜑(String.class), - items.toArray() + public EOsprintf(final Data format, final Data... args) { + super( + () -> { + final Collection items = new LinkedList<>(); + for (final Data arg : args) { + items.add(arg.get()); + } + return String.format(format.get(), items.toArray()); + } ); } } diff --git a/eo-runtime/src/test/java/org/eolang/EOboolTest.java b/eo-runtime/src/test/java/org/eolang/EOboolTest.java index e802ac03e2..553c679ec2 100644 --- a/eo-runtime/src/test/java/org/eolang/EOboolTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOboolTest.java @@ -34,15 +34,21 @@ */ public final class EOboolTest { + @Test + public void instanceWithScalar() { + MatcherAssert.assertThat( + new EObool(() -> true).get(), + Matchers.equalTo(true) + ); + } + @Test public void makesFork() { MatcherAssert.assertThat( - new Data.End( - new EObool(true).eoif().cp( - new EOstring("Hello"), - new EOstring("world") - ) - ).take(String.class), + new EObool(true).eoif().cp( + new EOstring("Hello"), + new EOstring("world") + ).get(), Matchers.equalTo("Hello") ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOintegerTest.java b/eo-runtime/src/test/java/org/eolang/EOintTest.java similarity index 78% rename from eo-runtime/src/test/java/org/eolang/EOintegerTest.java rename to eo-runtime/src/test/java/org/eolang/EOintTest.java index 218c42050f..8db9dacfb7 100644 --- a/eo-runtime/src/test/java/org/eolang/EOintegerTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOintTest.java @@ -28,20 +28,18 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link EOinteger}. + * Test case for {@link EOint}. * * @since 0.1 */ -public final class EOintegerTest { +public final class EOintTest { @Test public void addsNumbers() { MatcherAssert.assertThat( - new Data.End( - new EOinteger(1L).eoadd().cp( - new EOinteger(5L), new EOinteger(-1L) - ) - ).𝜑(Long.class), + new EOint(1L).eoadd().cp( + new EOint(5L), new EOint(-1L) + ).get(), Matchers.equalTo(5L) ); } @@ -49,11 +47,9 @@ public void addsNumbers() { @Test public void multipliesNumbers() { MatcherAssert.assertThat( - new Data.End( - new EOinteger(2L).eomul().cp( - new EOinteger(4L), new EOinteger(-2L) - ) - ).𝜑(Long.class), + new EOint(2L).eomul().cp( + new EOint(4L), new EOint(-2L) + ).get(), Matchers.equalTo(-16L) ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOstringTest.java b/eo-runtime/src/test/java/org/eolang/EOstringTest.java index 595735426a..7ad1616c5b 100644 --- a/eo-runtime/src/test/java/org/eolang/EOstringTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstringTest.java @@ -38,9 +38,17 @@ public final class EOstringTest { public void encapsulatesString() { final String key = "hey"; MatcherAssert.assertThat( - new Data.End(new EOstring(key)).𝜑(String.class), + new EOstring(key).get(), Matchers.equalTo(key) ); } + @Test + public void instanceWithScalar() { + MatcherAssert.assertThat( + new EOstring(() -> "Hi").get(), + Matchers.equalTo("Hi") + ); + } + } diff --git a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java index c39b23987e..58f8989031 100644 --- a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java +++ b/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java @@ -25,7 +25,6 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import org.eolang.Data; import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -42,12 +41,10 @@ public final class EOstdoutTest { public void printsString() { final String text = "Hello, друг!"; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new Data.End( - new EOstdout( - new EOstring(text), - new PrintStream(baos) - ) - ).𝜑(Boolean.class); + new EOstdout( + new EOstring(text), + new PrintStream(baos) + ).get(); MatcherAssert.assertThat( baos.toString(), Matchers.equalTo(text) diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index cfbf539573..83e1fbf3e5 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -23,8 +23,7 @@ */ package org.eolang.txt; -import org.eolang.Data; -import org.eolang.EOinteger; +import org.eolang.EOint; import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -40,13 +39,11 @@ public final class EOsprintfTest { @Test public void printsSimpleText() { MatcherAssert.assertThat( - new Data.End( - new EOsprintf( - new EOstring("Hello, %s %d!"), - new EOstring("John"), - new EOinteger(2L) - ) - ).𝜑(String.class), + new EOsprintf( + new EOstring("Hello, %s %d!"), + new EOstring("John"), + new EOint(2L) + ).get(), Matchers.equalTo("Hello, John 2!") ); } From 1dae640050789f50c48e1d988335f6cdfc936a8f Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 3 Dec 2020 22:31:09 +0300 Subject: [PATCH 32/80] #164: polished --- eo-runtime/src/main/java/org/eolang/Data.java | 2 ++ .../src/main/java/org/eolang/EObool.java | 28 +++++++++++++++---- .../src/main/java/org/eolang/EOint.java | 2 +- .../src/main/java/org/eolang/Operator.java | 13 ++++++--- .../src/main/java/org/eolang/io/EOstdout.java | 2 +- .../src/test/java/org/eolang/EOboolTest.java | 7 +++-- .../src/test/java/org/eolang/EOintTest.java | 5 ++-- 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 687948a69a..7bdedf1de6 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -27,12 +27,14 @@ /** * Data primitive. * + * @param Type of return * @since 0.1 */ public interface Data { /** * Take the take out. + * * @return The take */ T get(); diff --git a/eo-runtime/src/main/java/org/eolang/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java index c8d06ad1e7..65692f74a4 100644 --- a/eo-runtime/src/main/java/org/eolang/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/EObool.java @@ -65,11 +65,13 @@ public final Boolean get() { /** * Makes a fork. * - * new EObool(true).if().cp("left", "right") + * new EObool(true).if().copy("left", "right") * + * @param Type of result * @return Result + * @checkstyle NonStaticMethodCheck (3 lines) */ - public EObool.OpIf eoif() { + public final EObool.OpIf eoif() { return new EObool.OpIf<>(); } @@ -80,8 +82,16 @@ public EObool.OpIf eoif() { * @since 0.1 */ @SuppressWarnings("unchecked") - public final class OpIf { - public Data cp(final Data left, final Data right) { + public final class OpIf { + /** + * Make a copy. + * + * @param left Left branch (positive) + * @param right Right branch (negative) + * @return Data with result + * @checkstyle MethodNameCheck (3 lines) + */ + public Data copy(final Data left, final Data right) { try { final Constructor> ctor = (Constructor>) left.getClass() @@ -97,9 +107,15 @@ public Data cp(final Data left, final Data right) { return result; } ); - } catch (NoSuchMethodException | IllegalAccessException + } catch (final NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) { - throw new IllegalStateException(ex); + throw new IllegalStateException( + String.format( + "Can't make a copy of %s", + left.getClass() + ), + ex + ); } } } diff --git a/eo-runtime/src/main/java/org/eolang/EOint.java b/eo-runtime/src/main/java/org/eolang/EOint.java index faa7cb7655..313027d930 100644 --- a/eo-runtime/src/main/java/org/eolang/EOint.java +++ b/eo-runtime/src/main/java/org/eolang/EOint.java @@ -63,7 +63,7 @@ public Long get() { /** * Add a number. * - * new EOinteger(5).add().cp(5, 7, 7) + * new EOinteger(5).add().copy(5, 7, 7) * * @return New number */ diff --git a/eo-runtime/src/main/java/org/eolang/Operator.java b/eo-runtime/src/main/java/org/eolang/Operator.java index 2a5db02163..0faf7a54f0 100644 --- a/eo-runtime/src/main/java/org/eolang/Operator.java +++ b/eo-runtime/src/main/java/org/eolang/Operator.java @@ -32,9 +32,11 @@ /** * One operation. * + * @param Type of result + * @param Type of data * @since 0.1 */ -public final class Operator > { +public final class Operator> { /** * The start. @@ -66,11 +68,12 @@ public Operator(final Data val, final Class tpe, /** * Make a copy of it. + * * @param args The args * @return Copy */ @SafeVarargs - public final T cp(final Data... args) { + public final T copy(final Data... args) { try { return this.type.getConstructor(Scalar.class).newInstance( (Scalar) () -> this.reduce.calc( @@ -78,7 +81,7 @@ public final T cp(final Data... args) { new ListOf<>(args) ) ); - } catch (InstantiationException | IllegalAccessException + } catch (final InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new IllegalStateException(ex); } @@ -87,11 +90,13 @@ public final T cp(final Data... args) { /** * Reduce action. * + * @param Type of result * @since 0.1 */ - public interface Reduce { + public interface Reduce { /** * Do it. + * * @param first First value * @param args The args * @return Result diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index 7d21691396..ca911571cd 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -45,8 +45,8 @@ public EOstdout(final Data txt) { /** * Ctor. - * @param stream The text to print * @param text The output stream + * @param stream The text to print */ public EOstdout(final Data text, final PrintStream stream) { super( diff --git a/eo-runtime/src/test/java/org/eolang/EOboolTest.java b/eo-runtime/src/test/java/org/eolang/EOboolTest.java index 553c679ec2..eb578e83e9 100644 --- a/eo-runtime/src/test/java/org/eolang/EOboolTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOboolTest.java @@ -44,12 +44,13 @@ public void instanceWithScalar() { @Test public void makesFork() { + final String left = "Hello!"; MatcherAssert.assertThat( - new EObool(true).eoif().cp( - new EOstring("Hello"), + new EObool(true).eoif().copy( + new EOstring(left), new EOstring("world") ).get(), - Matchers.equalTo("Hello") + Matchers.equalTo(left) ); } diff --git a/eo-runtime/src/test/java/org/eolang/EOintTest.java b/eo-runtime/src/test/java/org/eolang/EOintTest.java index 8db9dacfb7..de980b41f8 100644 --- a/eo-runtime/src/test/java/org/eolang/EOintTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOintTest.java @@ -31,13 +31,14 @@ * Test case for {@link EOint}. * * @since 0.1 + * @checkstyle MagicNumberCheck (500 lines) */ public final class EOintTest { @Test public void addsNumbers() { MatcherAssert.assertThat( - new EOint(1L).eoadd().cp( + new EOint(1L).eoadd().copy( new EOint(5L), new EOint(-1L) ).get(), Matchers.equalTo(5L) @@ -47,7 +48,7 @@ public void addsNumbers() { @Test public void multipliesNumbers() { MatcherAssert.assertThat( - new EOint(2L).eomul().cp( + new EOint(2L).eomul().copy( new EOint(4L), new EOint(-2L) ).get(), Matchers.equalTo(-16L) From 2f1469857d726a1a0baf81aafdf8adec6bcc0671 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 4 Dec 2020 16:30:34 +0300 Subject: [PATCH 33/80] #164: extra check --- .../compiler/errors/abstract-decoratee.xsl | 49 ++++++++++++++++++ .../eolang/compiler/errors/broken-aliases.xsl | 1 + .../compiler/errors/duplicate-aliases.xsl | 1 + .../compiler/errors/duplicate-names.xsl | 1 + .../eolang/compiler/errors/global-nonames.xsl | 1 + .../eolang/compiler/errors/reserved-atoms.xsl | 1 + .../compiler/errors/same-line-names.xsl | 1 + .../eolang/compiler/errors/self-naming.xsl | 1 + .../eolang/compiler/errors/unknown-names.xsl | 1 + .../java/org/eolang/compiler/PacksTest.java | 1 + .../packs/catches-abstract-decoratee.yaml | 9 ++++ .../main/java/org/eolang/maven/ToJava.java | 2 +- .../resources/org/eolang/maven/to-java.xsl | 51 ------------------- .../test/resources/org/eolang/maven/mess.eo | 6 +-- 14 files changed, 71 insertions(+), 55 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl new file mode 100644 index 0000000000..038bb5e4c1 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl @@ -0,0 +1,49 @@ + + + + + + + + + + + + abstract-decoratee + + + + + Abstract object can't be used as a decoratee + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl index 355fdf81c5..879f128a42 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl index 78f2024ba1..9253b65305 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl index b4ea047b90..54786d0e1d 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl index eda86b1d22..e2b57c0d76 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl index 533a025fc1..f71cb1cb75 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl index 3e634f73f2..3808cdc824 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl index ebd1ffb4ce..5341521255 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl index f3ca02db6e..06f8da982e 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 8b6f4be27b..afb0f1c7c6 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -79,6 +79,7 @@ private static Collection yamlPacks() { "simple.yaml", "full-syntax.yaml", "catches-name-duplicates.yaml", + "catches-abstract-decoratee.yaml", "catches-alias-duplicates.yaml", "catches-global-nonames.yaml", "catches-broken-aliases.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml new file mode 100644 index 0000000000..71848adb5c --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/abstract-decoratee.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='2'] +eo: | + [] > main + [] > @ + hello > test diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java index a08b95f135..ae77a602ae 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java @@ -88,7 +88,7 @@ public void compile() throws IOException { ).with(new ClasspathSources()), new ListOf<>( "/org/eolang/compiler/globals-to-abstracts.xsl", - "/org/eolang/compiler/float-abstracts.xsl", + "/org/eolang/compiler/abstracts-float-up.xsl", "to-java.xsl" ) ) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 00dce53a6c..43075af323 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -99,11 +99,6 @@ SOFTWARE. } - - - - - @@ -159,52 +154,6 @@ SOFTWARE. } - - - - @Override - - - public Phi 𝜑() { - - - - - - return - - - - - - - - - throw new RuntimeException( - - - - - - - this.msg().𝜑().toString() - - - String.format("Runtime exception at %s", this.getClass()) - - - - - - ) - - - ; - - - } - - diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index ba6c16a22c..24d1b9a92c 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -8,11 +8,11 @@ [n] > alpha [] > x - a pi 666 + a pi 666 > pp [] > n - wild + wild > tff two.f1.f2 x - stdout + stdout > z sprintf > f! "Hello, ".trim x 0x1fff n From 144d6fba987031d19f6adb4145ad02cb4c5d5efd Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 7 Dec 2020 10:50:42 +0300 Subject: [PATCH 34/80] #164: atom syntax --- .../antlr4/org/eolang/compiler/Program.g4 | 11 +++++- .../java/org/eolang/compiler/XeListener.java | 25 +++++++++---- .../eolang/compiler/packs/full-syntax.yaml | 5 ++- .../resources/org/eolang/maven/to-java.xsl | 8 +++-- .../test/resources/org/eolang/maven/mess.eo | 35 ++++++++++--------- .../src/main/java/org/eolang/EObool.java | 13 +++++-- .../src/main/java/org/eolang/io/EOstdout.java | 2 +- .../main/java/org/eolang/txt/EOsprintf.java | 2 +- 8 files changed, 70 insertions(+), 31 deletions(-) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index ab497559b5..e72d1bcfee 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -68,11 +68,18 @@ object abstraction : LSQ - (NAME (SPACE NAME)*)? + (argument (SPACE argument)*)? RSQ + ATOM? suffix? ; +argument + : + NAME + DOTS? + ; + tail : EOL @@ -181,7 +188,9 @@ data COMMENT: HASH ~[\r\n]*; META: PLUS NAME (SPACE ~[\r\n]+)?; +DOTS: '...'; CONST: '!'; +ATOM: '?'; COLON: ':'; ARROW: '>'; PLUS: '+'; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 08746f5b88..0596ba7504 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -154,18 +154,31 @@ public void exitObject(final ProgramParser.ObjectContext ctx) { @Override public void enterAbstraction(final ProgramParser.AbstractionContext ctx) { this.dirs.add("o").attr("line", ctx.getStart().getLine()); - for (final TerminalNode attr : ctx.NAME()) { - this.dirs.add("o") - .attr("line", ctx.getStart().getLine()) - .attr("name", attr.getText()) - .up(); + if (ctx.ATOM() != null) { + this.dirs.attr("atom", ""); } this.dirs.up(); } @Override public void exitAbstraction(final ProgramParser.AbstractionContext ctx) { - // This method is created by ANTLR and can't be removed + } + + @Override + public void enterArgument(final ProgramParser.ArgumentContext ctx) { + this.enter(); + this.dirs.add("o") + .attr("line", ctx.getStart().getLine()) + .attr("name", ctx.NAME().getText()); + if (ctx.DOTS() != null) { + this.dirs.attr("vararg", ""); + } + this.dirs.up().up(); + } + + @Override + public void exitArgument(final ProgramParser.ArgumentContext ctx) { + // nothing } @Override diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index a2cabcb388..e841edc09f 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -3,7 +3,7 @@ tests: - /program/license[text()!=''] - /program/metas[count(meta)=3] - /program/metas/meta[head='foo' and tail=''] - - /program/objects[count(o)=5] + - /program/objects[count(o)=6] - //o[@base='bool'] - //o[@base='string'] - //o[@base='char' and text()='e'] @@ -14,6 +14,7 @@ tests: - //o[@as='i'] - //o[@base='.five'] - //objects[not(.//o[@name=''])] + - //o[@atom and @name='atom' and count(o)=2 and o[@vararg and @name='a']] eo: | # The purpose of this test case is to make # sure all possible syntax scenarios can @@ -28,6 +29,8 @@ eo: | # This is just a simple string "Hello, world!" > hello! + [tt a...]? > atom + [x] > first second > hello third:foo > x diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 43075af323..01efd67787 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -69,9 +69,13 @@ SOFTWARE. import org.eolang.sys.*; - public final class + public class - implements Phi { + + extends + + + { public static final diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 24d1b9a92c..2aef3636c2 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -4,19 +4,22 @@ +package org.eolang.examples +alias stdout org.eolang.io.stdout -3.14 > pi - -[n] > alpha - [] > x - a pi 666 > pp - [] > n - wild > tff - two.f1.f2 x - stdout > z - sprintf > f! - "Hello, ".trim - x 0x1fff n - -alpha > beta - alpha - 'Z' +# 3.14 > pi +# +# [n] > alpha +# [] > x +# a pi 666 > pp +# [] > n +# wild > tff +# two.f1.f2 x +# stdout > z +# sprintf > f! +# "Hello, ".trim +# x 0x1fff n +# +# alpha > beta +# alpha +# 'Z' +[] > main + stdout > @ + "Hello, world!" diff --git a/eo-runtime/src/main/java/org/eolang/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java index 65692f74a4..f12229ddb4 100644 --- a/eo-runtime/src/main/java/org/eolang/EObool.java +++ b/eo-runtime/src/main/java/org/eolang/EObool.java @@ -72,7 +72,7 @@ public final Boolean get() { * @checkstyle NonStaticMethodCheck (3 lines) */ public final EObool.OpIf eoif() { - return new EObool.OpIf<>(); + return new EObool.OpIf<>(this.value); } /** @@ -82,7 +82,14 @@ public final EObool.OpIf eoif() { * @since 0.1 */ @SuppressWarnings("unchecked") - public final class OpIf { + public final class OpIf extends EObool { + /** + * Ctor. + * @param val The value + */ + public OpIf(final Scalar val) { + super(val); + } /** * Make a copy. * @@ -99,7 +106,7 @@ public Data copy(final Data left, final Data right) { return ctor.newInstance( (Scalar) () -> { final T result; - if (EObool.this.get()) { + if (this.get()) { result = (T) left.get(); } else { result = (T) right.get(); diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java index ca911571cd..33a9bfee06 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java @@ -33,7 +33,7 @@ * * @since 0.1 */ -public final class EOstdout extends EObool { +public class EOstdout extends EObool { /** * Ctor. diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 8d81a7f6a9..877ce2046d 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -34,7 +34,7 @@ * * @since 0.2 */ -public final class EOsprintf extends EOstring { +public class EOsprintf extends EOstring { /** * Ctor. From e456cad7d6ead8f58fb6c82718da971812f98111 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 7 Dec 2020 10:56:42 +0300 Subject: [PATCH 35/80] #164: catch --- .../eolang/compiler/errors/middle-varargs.xsl | 50 +++++++++++++++++++ .../java/org/eolang/compiler/PacksTest.java | 1 + .../compiler/packs/catches-middle-vararg.yaml | 9 ++++ 3 files changed, 60 insertions(+) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl new file mode 100644 index 0000000000..2449e6d66b --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl @@ -0,0 +1,50 @@ + + + + + + + + + + + middle-varags + + + + + Varargs param + + must be the last one + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index afb0f1c7c6..b8e69e9fe6 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -83,6 +83,7 @@ private static Collection yamlPacks() { "catches-alias-duplicates.yaml", "catches-global-nonames.yaml", "catches-broken-aliases.yaml", + "catches-middle-vararg.yaml", "catches-unknown-names.yaml", "catches-self-naming.yaml", "catches-reserved-atoms.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml new file mode 100644 index 0000000000..a470b21e54 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/middle-varargs.xsl +tests: + - /program/errors[count(error)=1] +eo: | + [x a... b] > bad + withoutaname + + [x a...] > good From 7e3e40c366b782d147d06dcac6a2b9c36c53e4d7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 8 Dec 2020 21:23:06 +0300 Subject: [PATCH 36/80] #164: more tests --- .../main/java/org/eolang/compiler/Pack.java | 4 +- .../java/org/eolang/compiler/Program.java | 83 ++------- .../main/java/org/eolang/compiler/Syntax.java | 150 +++++++++++++++ .../java/org/eolang/compiler/PacksTest.java | 7 +- .../{ProgramTest.java => SyntaxTest.java} | 14 +- .../java/org/eolang/maven/CompileMojo.java | 172 +++++++++--------- .../java/org/eolang/maven/OptimizeMojo.java | 143 +++++++++++++++ .../main/java/org/eolang/maven/ParseMojo.java | 153 ++++++++++++++++ .../main/java/org/eolang/maven/TargetSpy.java | 56 ++++++ .../main/java/org/eolang/maven/ToJava.java | 166 ----------------- .../resources/org/eolang/maven/to-java.xsl | 21 ++- .../org/eolang/maven/CompileMojoTest.java | 75 +++----- .../src/test/java/org/eolang/maven/Mojo.java | 81 +++++++++ .../org/eolang/maven/OptimizeMojoTest.java | 75 ++++++++ .../java/org/eolang/maven/ParseMojoTest.java | 94 ++++++++++ .../java/org/eolang/maven/ToJavaTest.java | 104 ----------- .../test/resources/org/eolang/maven/mess.eo | 6 +- 17 files changed, 910 insertions(+), 494 deletions(-) create mode 100644 eo-compiler/src/main/java/org/eolang/compiler/Syntax.java rename eo-compiler/src/test/java/org/eolang/compiler/{ProgramTest.java => SyntaxTest.java} (90%) create mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java create mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java create mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java delete mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java create mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java create mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java create mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java delete mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 81bd637bba..fc31a65415 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -56,7 +56,6 @@ public Pack() { "errors/broken-aliases.xsl", "errors/duplicate-aliases.xsl", "errors/global-nonames.xsl", - "errors/noname-attributes.xsl", "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", @@ -65,7 +64,8 @@ public Pack() { "vars-float-up.xsl", "add-refs.xsl", "resolve-aliases.xsl", - "errors/unknown-names.xsl" + "errors/unknown-names.xsl", + "errors/noname-attributes.xsl" ) ); } diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 6333200c30..00560320f6 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -29,22 +29,12 @@ import com.jcabi.xml.XSL; import java.io.IOException; import java.nio.file.Path; -import org.antlr.v4.runtime.ANTLRErrorListener; -import org.antlr.v4.runtime.BaseErrorListener; -import org.antlr.v4.runtime.CharStreams; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.cactoos.Input; import org.cactoos.Output; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; -import org.cactoos.io.UncheckedInput; import org.cactoos.scalar.LengthOf; import org.cactoos.scalar.Unchecked; -import org.cactoos.text.TextOf; /** * Program. @@ -55,46 +45,38 @@ public final class Program { /** - * The name of it. + * XML to optimize. */ - private final String name; + private final XML source; /** - * Text to parse. - */ - private final Input input; - - /** - * Target to save XML to. + * Target to save XMLs to. */ private final Output target; /** * Ctor. * - * @param nme The name of it - * @param ipt Input text + * @param dom XML to optimize * @param file The file to write the XML to */ - public Program(final String nme, final Input ipt, final Path file) { - this(nme, ipt, new OutputTo(file)); + public Program(final XML dom, final Path file) { + this(dom, new OutputTo(file)); } /** * Ctor. * - * @param nme The name of it - * @param ipt Input text + * @param dom XML to optimize * @param tgt Target */ - public Program(final String nme, final Input ipt, final Output tgt) { - this.name = nme; - this.input = ipt; + public Program(final XML dom, final Output tgt) { + this.source = dom; this.target = tgt; } /** - * Compile it to XML and save (with default set of XSLs). + * Compile it to XMLs and save (with default set of XSLs). * @throws IOException If fails */ public void compile() throws IOException { @@ -131,59 +113,22 @@ public void compile(final Iterable xsls) throws IOException { */ public void compile(final Iterable xsls, final Program.Spy spy) throws IOException { - final String[] lines = new TextOf(this.input).asString().split("\n"); - final ANTLRErrorListener errors = new BaseErrorListener() { - // @checkstyle ParameterNumberCheck (10 lines) - @Override - public void syntaxError(final Recognizer recognizer, - final Object symbol, final int line, - final int position, final String msg, - final RecognitionException error) { - throw new CompileException( - String.format( - "[%d:%d] %s: \"%s\"", - line, position, msg, - // @checkstyle AvoidInlineConditionalsCheck (1 line) - lines.length < line ? "EOF" : lines[line - 1] - ), - error - ); - } - }; - final ProgramLexer lexer = - new ProgramLexer( - CharStreams.fromStream( - new UncheckedInput(this.input).stream() - ) - ); - lexer.removeErrorListeners(); - lexer.addErrorListener(errors); - final ProgramParser parser = - new ProgramParser( - new CommonTokenStream(lexer) - ); - parser.removeErrorListeners(); - parser.addErrorListener(errors); - final XeListener xel = new XeListener(this.name); - new ParseTreeWalker().walk(xel, parser.program()); - XML dom = xel.xml(); - Logger.debug(this, "Raw XML:\n%s", dom.toString()); int index = 0; + XML before = this.source; for (final XSL xsl : xsls) { - final XML after = xsl.transform(dom); + final XML after = xsl.transform(before); spy.push(index, xsl, after); ++index; - dom = after; + before = after; } new Unchecked<>( new LengthOf( new TeeInput( - new InputOf(dom.toString()), + new InputOf(before.toString()), this.target ) ) ).value(); - Logger.debug(this, "Input of %d EO lines compiled", lines.length); } /** diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java b/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java new file mode 100644 index 0000000000..45fc89b9bc --- /dev/null +++ b/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java @@ -0,0 +1,150 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.compiler; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XML; +import java.io.IOException; +import java.nio.file.Path; +import org.antlr.v4.runtime.ANTLRErrorListener; +import org.antlr.v4.runtime.BaseErrorListener; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.cactoos.Input; +import org.cactoos.Output; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.io.UncheckedInput; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; +import org.cactoos.text.TextOf; + +/** + * Syntax parser. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class Syntax { + + /** + * The name of it. + */ + private final String name; + + /** + * Text to parse. + */ + private final Input input; + + /** + * Target to save XML to. + */ + private final Output target; + + /** + * Ctor. + * + * @param nme The name of it + * @param ipt Input text + * @param file The file to write the XML to + */ + public Syntax(final String nme, final Input ipt, final Path file) { + this(nme, ipt, new OutputTo(file)); + } + + /** + * Ctor. + * + * @param nme The name of it + * @param ipt Input text + * @param tgt Target + */ + public Syntax(final String nme, final Input ipt, final Output tgt) { + this.name = nme; + this.input = ipt; + this.target = tgt; + } + + /** + * Compile it to XML and save. + * + * @return XML parsed + * @throws IOException If fails + */ + public XML parse() throws IOException { + final String[] lines = new TextOf(this.input).asString().split("\n"); + final ANTLRErrorListener errors = new BaseErrorListener() { + // @checkstyle ParameterNumberCheck (10 lines) + @Override + public void syntaxError(final Recognizer recognizer, + final Object symbol, final int line, + final int position, final String msg, + final RecognitionException error) { + throw new CompileException( + String.format( + "[%d:%d] %s: \"%s\"", + line, position, msg, + // @checkstyle AvoidInlineConditionalsCheck (1 line) + lines.length < line ? "EOF" : lines[line - 1] + ), + error + ); + } + }; + final ProgramLexer lexer = + new ProgramLexer( + CharStreams.fromStream( + new UncheckedInput(this.input).stream() + ) + ); + lexer.removeErrorListeners(); + lexer.addErrorListener(errors); + final ProgramParser parser = + new ProgramParser( + new CommonTokenStream(lexer) + ); + parser.removeErrorListeners(); + parser.addErrorListener(errors); + final XeListener xel = new XeListener(this.name); + new ParseTreeWalker().walk(xel, parser.program()); + final XML dom = xel.xml(); + Logger.debug(this, "Raw XML:\n%s", dom.toString()); + new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(dom.toString()), + this.target + ) + ) + ).value(); + Logger.debug(this, "Input of %d EO lines compiled", lines.length); + return dom; + } + +} diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index b8e69e9fe6..35174b9ae2 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -24,6 +24,7 @@ package org.eolang.compiler; import com.jcabi.matchers.XhtmlMatchers; +import com.jcabi.xml.XMLDocument; import java.io.ByteArrayOutputStream; import java.util.Collection; import java.util.Map; @@ -54,10 +55,14 @@ public void testPacks(final String pack) throws Exception { ) ); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( + new Syntax( pack, new InputOf(String.format("%s\n", map.get("eo"))), new OutputTo(baos) + ).parse(); + final Program program = new Program( + new XMLDocument(baos.toString()), + new OutputTo(baos) ); final Collection xsls = (Collection) map.get("xsls"); if (xsls == null) { diff --git a/eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java b/eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java similarity index 90% rename from eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java rename to eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java index 4578eccf30..8d03becb30 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java @@ -38,17 +38,17 @@ * * @since 0.1 */ -public final class ProgramTest { +public final class SyntaxTest { @Test public void compilesSimpleCode() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( - "test-works", + final Syntax syntax = new Syntax( + "test", new ResourceOf("org/eolang/compiler/fibonacci.eo"), new OutputTo(baos) ); - program.compile(); + syntax.parse(); MatcherAssert.assertThat( XhtmlMatchers.xhtml( new String(baos.toByteArray()) @@ -62,12 +62,12 @@ public void failsOnBrokenSyntax() { Assertions.assertThrows( CompileException.class, () -> { - final Program program = new Program( - "broken-test", + final Syntax syntax = new Syntax( + "test-it", new InputOf("this code is definitely wrong"), new DeadOutput() ); - program.compile(); + syntax.parse(); } ); } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 00cd08b4bf..c71345841d 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -24,15 +24,16 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; -import com.jcabi.xml.XSL; -import java.io.ByteArrayOutputStream; +import com.jcabi.xml.XSLChain; +import com.jcabi.xml.XSLDocument; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; +import java.nio.file.Paths; import java.util.List; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; @@ -44,12 +45,12 @@ import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; +import org.cactoos.list.ListOf; +import org.cactoos.list.Mapped; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.LengthOf; -import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; -import org.eolang.compiler.Program; import org.slf4j.impl.StaticLoggerBinder; /** @@ -77,8 +78,7 @@ public final class CompileMojo extends AbstractMojo { * @checkstyle MemberNameCheck (7 lines) */ @Parameter( - required = false, - readonly = false, + required = true, defaultValue = "${project.build.directory}/generated-sources/eo" ) private transient File generatedDir; @@ -88,34 +88,20 @@ public final class CompileMojo extends AbstractMojo { * @checkstyle MemberNameCheck (7 lines) */ @Parameter( - required = false, - readonly = false, + required = true, defaultValue = "${project.build.directory}" ) private transient File targetDir; - /** - * Directory in which .eo files are located. - * @checkstyle MemberNameCheck (7 lines) - */ - @Parameter( - required = false, - readonly = false, - defaultValue = "${project.basedir}/src/main/eo" - ) - private transient File sourcesDirectory; - @Override public void execute() throws MojoFailureException { StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); if (this.generatedDir.mkdirs()) { Logger.info(this, "Gen directory created: %s", this.generatedDir); } - if (this.targetDir.mkdirs()) { - Logger.info(this, "Target directory created: %s", this.targetDir); - } + final Path dir = this.targetDir.toPath().resolve("eo/optimize"); try { - Files.walk(this.sourcesDirectory.toPath()) + Files.walk(dir) .filter(file -> !file.toFile().isDirectory()) .forEach(this::compile); } catch (final IOException ex) { @@ -123,7 +109,7 @@ public void execute() throws MojoFailureException { new UncheckedText( new FormattedText( "Can't list EO files in %s", - this.sourcesDirectory + dir ) ).asString(), ex @@ -139,54 +125,70 @@ public void execute() throws MojoFailureException { } /** - * Compile one EO file. - * @param file EO file + * Compile one XML file. + * + * @param file XML file */ private void compile(final Path file) { - final String name = file.toString().substring( - this.sourcesDirectory.toString().length() + 1 - ); + final Path temp = this.targetDir.toPath().resolve("eo/temp"); try { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new Program( - name, - new InputOf(file), - new OutputTo(baos) - ).compile(new ArrayList<>(0)); - final String xml = String.format("%s.xml", name); - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(baos.toString()), - new OutputTo( - this.targetDir.toPath() - .resolve("eo-compiler-raw") - .resolve(xml) - ) + final XML xml = new XMLDocument(file); + final XML out = new XSLChain( + new Mapped<>( + doc -> new XSLDocument( + CompileMojo.class.getResourceAsStream(doc) + ).with(new ClasspathSources()), + new ListOf<>( + "/org/eolang/compiler/globals-to-abstracts.xsl", + "/org/eolang/compiler/abstracts-float-up.xsl", + "to-java.xsl" ) ) - ).value(); - baos.reset(); - new Program(name, new InputOf(file), new OutputTo(baos)).compile( - new CompileMojo.TargetSpy(xml) - ); + ).transform(xml); new IoChecked<>( new LengthOf( new TeeInput( - new InputOf(baos.toString()), + new InputOf(out.toString()), new OutputTo( - this.targetDir.toPath() - .resolve("eo-compiler") - .resolve(xml) + temp.resolve( + String.format( + "%s.xml", + xml.xpath("/program/@name").get(0) + ) + ) ) ) ) ).value(); - new ToJava( - new XMLDocument(baos.toString()), - this.generatedDir.toPath(), - this.targetDir.toPath().resolve("eo-to-java") - ).compile(); + final List errors = out.nodes("/program/errors/error"); + for (final XML error : errors) { + Logger.error( + this, + "[%s] %s", + error.xpath("@line").get(0), + error.xpath("text()").get(0) + ); + } + if (!errors.isEmpty()) { + throw new IllegalStateException( + String.format( + "There are %d errors, see log above", + errors.size() + ) + ); + } + for (final XML java : out.nodes("/program/objects/o[java]")) { + CompileMojo.save( + this.generatedDir.toPath().resolve( + Paths.get( + String.format( + "%s.java", java.xpath("@name").get(0) + ) + ) + ), + java.xpath("java/text()").get(0) + ); + } } catch (final IOException ex) { throw new IllegalStateException( new UncheckedText( @@ -202,39 +204,27 @@ private void compile(final Path file) { } /** - * The spy to log all results. + * Save one Java file. + * @param path The path + * @param content The content + * @throws IOException If fails */ - private final class TargetSpy implements Program.Spy { - private final Path dir; - private TargetSpy(final String xml) { - this.dir = CompileMojo.this.targetDir.toPath() - .resolve("eo-compiler-steps") - .resolve(xml); - } - @Override - public void push(final int index, final XSL xsl, final XML xml) { - final List names = new XMLDocument( - xsl.toString() - ).xpath("/*/@id"); - final String file; - if (names.isEmpty()) { - file = String.format("%d", index); - } else { - file = names.get(0).replaceAll("[^a-z0-9]", "-"); - } - new Unchecked<>( - new LengthOf( - new TeeInput( - new InputOf(xml.toString()), - new OutputTo( - this.dir.resolve( - String.format("%02d-%s.xml", index, file) - ) - ) - ) + private static void save(final Path path, final String content) + throws IOException { + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(content), + path ) - ).value(); - } + ) + ).value(); + Logger.info( + CompileMojo.class, + "Saved %d chars to %s", + content.length(), + path.toAbsolutePath() + ); } } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java new file mode 100644 index 0000000000..e73c1ff55f --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -0,0 +1,143 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XMLDocument; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.eolang.compiler.Program; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Optimize XML files. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "optimize", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class OptimizeMojo extends AbstractMojo { + + /** + * Maven project. + */ + @Parameter(defaultValue = "${project}") + private MavenProject project; + + /** + * From directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + final Path dir = this.targetDir.toPath().resolve("eo/parse"); + try { + Files.walk(dir) + .filter(file -> !file.toFile().isDirectory()) + .forEach(this::optimize); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list XML files in %s", + dir + ) + ).asString(), + ex + ); + } + } + + /** + * Optimize XML file after parsing. + * @param file EO file + */ + private void optimize(final Path file) { + final String name = file.toString().substring( + this.targetDir.toString().length() + 2 + "eo/parse".length() + ); + final Path dir = this.targetDir.toPath() + .resolve("eo/steps") + .resolve(name); + try { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Program( + new XMLDocument(file), new OutputTo(baos) + ).compile(new TargetSpy(dir)); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(baos.toString()), + new OutputTo( + this.targetDir.toPath() + .resolve("eo/optimize") + .resolve(name) + ) + ) + ) + ).value(); + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't compile %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + Logger.info(this, "%s optimized to %s", file, dir); + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java new file mode 100644 index 0000000000..ae6fd997b4 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java @@ -0,0 +1,153 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import com.jcabi.log.Logger; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.eolang.compiler.Syntax; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Parse EO to XML. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "parse", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class ParseMojo extends AbstractMojo { + + /** + * Maven project. + */ + @Parameter(defaultValue = "${project}") + private MavenProject project; + + /** + * Target directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + /** + * Directory in which .eo files are located. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.basedir}/src/main/eo" + ) + private transient File sourcesDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + if (this.targetDir.mkdirs()) { + Logger.info(this, "Target directory created: %s", this.targetDir); + } + try { + Files.walk(this.sourcesDir.toPath()) + .filter(file -> !file.toFile().isDirectory()) + .forEach(this::parse); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list EO files in %s", + this.sourcesDir + ) + ).asString(), + ex + ); + } + } + + /** + * Parse EO file to XML. + * @param file EO file + */ + private void parse(final Path file) { + final String name = file.toString().substring( + this.sourcesDir.toString().length() + 1 + ); + final String xml = String.format("%s.xml", name); + final Path path = this.targetDir.toPath() + .resolve("eo/parse") + .resolve(xml); + try { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Syntax( + name, + new InputOf(file), + new OutputTo(baos) + ).parse(); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(baos.toString()), + new OutputTo(path) + ) + ) + ).value(); + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't parse %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + Logger.info(this, "%s parsed to %s", file, path); + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java new file mode 100644 index 0000000000..13b716ad7e --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java @@ -0,0 +1,56 @@ +package org.eolang.maven; + +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import com.jcabi.xml.XSL; +import java.nio.file.Path; +import java.util.List; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; +import org.eolang.compiler.Program; + +/** + * The spy to log all results. + */ +final class TargetSpy implements Program.Spy { + /** + * The dir. + */ + private final Path dir; + + /** + * Ctor. + * @param target The path + */ + TargetSpy(final Path target) { + this.dir = target; + } + + @Override + public void push(final int index, final XSL xsl, final XML xml) { + final List names = new XMLDocument( + xsl.toString() + ).xpath("/*/@id"); + final String file; + if (names.isEmpty()) { + file = String.format("%d", index); + } else { + file = names.get(0).replaceAll("[^a-z0-9]", "-"); + } + new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(xml.toString()), + new OutputTo( + this.dir.resolve( + String.format("%02d-%s.xml", index, file) + ) + ) + ) + ) + ).value(); + } +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java deleted file mode 100644 index ae77a602ae..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven; - -import com.jcabi.log.Logger; -import com.jcabi.xml.ClasspathSources; -import com.jcabi.xml.XML; -import com.jcabi.xml.XSLChain; -import com.jcabi.xml.XSLDocument; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import org.cactoos.io.InputOf; -import org.cactoos.io.OutputTo; -import org.cactoos.io.TeeInput; -import org.cactoos.list.ListOf; -import org.cactoos.list.Mapped; -import org.cactoos.scalar.IoChecked; -import org.cactoos.scalar.LengthOf; - -/** - * ToJava. - * - * @since 0.1 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -public final class ToJava { - - /** - * The XML with EO parsed already. - */ - private final XML xml; - - /** - * Directory to save Java files to. - */ - private final Path dir; - - /** - * Directory to save temp files to. - */ - private final Path temp; - - /** - * Ctor. - * - * @param input Input text - * @param file The file to write the XML to - * @param tmp Temp dir - */ - public ToJava(final XML input, final Path file, final Path tmp) { - this.xml = input; - this.dir = file; - this.temp = tmp; - } - - /** - * Compile it to XML and save. - * @throws IOException If fails - */ - public void compile() throws IOException { - final XML out = new XSLChain( - new Mapped<>( - doc -> new XSLDocument( - ToJava.class.getResourceAsStream(doc) - ).with(new ClasspathSources()), - new ListOf<>( - "/org/eolang/compiler/globals-to-abstracts.xsl", - "/org/eolang/compiler/abstracts-float-up.xsl", - "to-java.xsl" - ) - ) - ).transform(this.xml); - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(out.toString()), - new OutputTo( - this.temp.resolve( - String.format( - "%s.xml", - this.xml.xpath("/program/@name").get(0) - ) - ) - ) - ) - ) - ).value(); - final List errors = out.nodes("/program/errors/error"); - for (final XML error : errors) { - Logger.error( - this, - "[%s] %s", - error.xpath("@line").get(0), - error.xpath("text()").get(0) - ); - } - if (!errors.isEmpty()) { - throw new IllegalStateException( - String.format( - "There are %d errors, see log above", - errors.size() - ) - ); - } - for (final XML file : out.nodes("/program/objects/o[java]")) { - ToJava.save( - this.dir.resolve( - Paths.get( - String.format( - "%s.java", file.xpath("@name").get(0) - ) - ) - ), - file.xpath("java/text()").get(0) - ); - } - } - - /** - * Save one Java file. - * @param path The path - * @param content The content - * @throws IOException If fails - */ - private static void save(final Path path, final String content) - throws IOException { - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(content), - path - ) - ) - ).value(); - Logger.info( - ToJava.class, - "Saved %d chars to %s", - content.length(), - path.toAbsolutePath() - ); - } - -} diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 01efd67787..c8e7e6ce7e 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -86,7 +86,7 @@ SOFTWARE. - private Phi + private Object ; @@ -125,9 +125,9 @@ SOFTWARE. cp( - , + , - final Phi + final Object ) { @@ -161,8 +161,15 @@ SOFTWARE. - public Phi - + public Object + + + _origin + + + + + () { @@ -189,6 +196,10 @@ SOFTWARE. .𝑥 + + this. + + this. diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index afe1bdd8b9..2608f4a8f3 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -23,6 +23,7 @@ */ package org.eolang.maven; +import com.jcabi.log.Logger; import java.nio.file.Files; import java.nio.file.Path; import org.apache.maven.plugin.testing.AbstractMojoTestCase; @@ -31,12 +32,10 @@ import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; import org.cactoos.scalar.LengthOf; -import org.eolang.compiler.CompileException; +import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; /** * Test case for {@link CompileMojo}. @@ -47,62 +46,44 @@ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class CompileMojoTest extends AbstractMojoTestCase { - /** - * Temp dir for tests. - * @checkstyle VisibilityModifierCheck (4 lines) - */ - @TempDir - public Path temp; - @Test public void testSimpleCompilation() throws Exception { - final CompileMojo mojo = new CompileMojo(); - final Path src = this.temp.resolve("src"); - this.setVariableValueToObject(mojo, "sourcesDirectory", src.toFile()); + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); new LengthOf( new TeeInput( new InputOf( - "[args] > main\n (stdout \"Hello!\").print\n" + "[args] > main\n (stdout \"Hello!\").print > x\n" ), new OutputTo(src.resolve("main.eo")) ) ).value(); - final Path target = this.temp.resolve("target"); - this.setVariableValueToObject( - mojo, "targetDir", target.toFile() - ); - final Path generated = this.temp.resolve("generated"); - this.setVariableValueToObject( - mojo, "generatedDir", generated.toFile() - ); - this.setVariableValueToObject(mojo, "project", new MavenProjectStub()); - mojo.execute(); + final Path target = temp.resolve("target"); + final Path generated = temp.resolve("generated"); + new Mojo<>(ParseMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .execute(); + new Mojo<>(CompileMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .with("generatedDir", generated.toFile()) + .execute(); + final Path java = generated.resolve("EOmain.java"); MatcherAssert.assertThat( - Files.exists(generated.resolve("EOmain.java")), + Files.exists(java), Matchers.is(true) ); - } - - @Test - public void testCrashOnInvalidSyntax() throws Exception { - final CompileMojo mojo = new CompileMojo(); - final Path src = this.temp.resolve("src"); - this.setVariableValueToObject(mojo, "sourcesDirectory", src.toFile()); - this.setVariableValueToObject( - mojo, "generatedDir", this.temp.resolve("generated").toFile() - ); - this.setVariableValueToObject( - mojo, "targetDir", this.temp.resolve("target").toFile() - ); - new LengthOf( - new TeeInput( - new InputOf("something is wrong here"), - new OutputTo(src.resolve("f.eo")) - ) - ).value(); - Assertions.assertThrows( - CompileException.class, - mojo::execute + final String code = new TextOf(new InputOf(java)).asString(); + Logger.info(this, "Java output:\n%s", code); + MatcherAssert.assertThat( + code, + Matchers.containsString("class EOmain") ); } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java new file mode 100644 index 0000000000..dc195d30b0 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java @@ -0,0 +1,81 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.codehaus.plexus.util.ReflectionUtils; + +/** + * Mutable mojo builder. + * + * @since 0.1 + */ +final class Mojo { + + private final Class type; + + private final Map attrs; + + Mojo(final Class tpe) { + this.type = tpe; + this.attrs = new HashMap<>(0); + } + + public Mojo with(final String attr, final Object value) { + this.attrs.put(attr, value); + return this; + } + + public void execute() { + try { + final AbstractMojo mojo = this.type.newInstance(); + for (final Map.Entry ent : this.attrs.entrySet()) { + final Field field = + ReflectionUtils.getFieldByNameIncludingSuperclasses( + ent.getKey(), this.type); + if (field == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" of %s in %s", + ent.getKey(), + ent.getValue().getClass().getCanonicalName(), + this.type.getCanonicalName() + ) + ); + } + field.setAccessible(true); + field.set(mojo, ent.getValue()); + } + mojo.execute(); + } catch (MojoExecutionException | MojoFailureException | InstantiationException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java new file mode 100644 index 0000000000..d4498f2c3e --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java @@ -0,0 +1,75 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link OptimizeMojo}. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class OptimizeMojoTest extends AbstractMojoTestCase { + + @Test + public void testSimpleOptimize() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + new LengthOf( + new TeeInput( + new InputOf( + "[args] > main\n (stdout \"Hello!\").print\n" + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + final Path target = temp.resolve("target"); + new Mojo<>(ParseMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .execute(); + MatcherAssert.assertThat( + Files.exists(target.resolve("eo/steps/main.eo.xml")), + Matchers.is(true) + ); + } + +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java new file mode 100644 index 0000000000..50bb116594 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java @@ -0,0 +1,94 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.eolang.compiler.CompileException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link ParseMojo}. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class ParseMojoTest extends AbstractMojoTestCase { + + @Test + public void testSimpleCompilation() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + final Path target = temp.resolve("target"); + new LengthOf( + new TeeInput( + new InputOf( + "[args] > main\n (stdout \"Hello!\").print\n" + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + new Mojo<>(ParseMojo.class) + .with("project", new MavenProjectStub()) + .with("sourcesDir", src.toFile()) + .with("targetDir", target.toFile()) + .execute(); + MatcherAssert.assertThat( + Files.exists(target.resolve("eo/parse/main.eo.xml")), + Matchers.is(true) + ); + } + + @Test + public void testCrashOnInvalidSyntax() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + final Path target = temp.resolve("target"); + new LengthOf( + new TeeInput( + new InputOf("something is wrong here"), + new OutputTo(src.resolve("f.eo")) + ) + ).value(); + Assertions.assertThrows( + CompileException.class, + () -> new Mojo<>(ParseMojo.class) + .with("project", new MavenProjectStub()) + .with("sourcesDir", src.toFile()) + .with("targetDir", target.toFile()) + .execute() + ); + } + +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java deleted file mode 100644 index 1dc8c4767c..0000000000 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.eolang.maven; - -import com.jcabi.log.Logger; -import com.jcabi.matchers.XhtmlMatchers; -import com.jcabi.xml.XML; -import com.jcabi.xml.XMLDocument; -import com.jcabi.xml.XSL; -import java.io.ByteArrayOutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import org.cactoos.io.InputOf; -import org.cactoos.io.OutputTo; -import org.cactoos.io.ResourceOf; -import org.cactoos.text.TextOf; -import org.eolang.compiler.Program; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -/** - * Test case for {@link ToJava}. - * - * @since 1.0 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -public final class ToJavaTest { - - /** - * Temp dir for tests. - * @checkstyle VisibilityModifierCheck (4 lines) - */ - @TempDir - public Path temp; - - @Test - public void compilesSimpleCodeToJava() throws Exception { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( - "test", - new ResourceOf("org/eolang/maven/mess.eo"), - new OutputTo(baos) - ); - program.compile(new ArrayList<>(0)); - Logger.info(this, "Parser output: %s\n", baos.toString()); - baos.reset(); - program.compile( - new Program.Spy() { - @Override - public void push(final int idx, final XSL xsl, final XML xml) { - Logger.trace(this, "Output:%s", xml); - } - } - ); - MatcherAssert.assertThat( - XhtmlMatchers.xhtml(baos.toString()), - Matchers.not(XhtmlMatchers.hasXPath("/program/errors/error")) - ); - Logger.info(this, "XML output: %s\n", baos.toString()); - final ToJava tojava = new ToJava( - new XMLDocument(baos.toString()), - this.temp.resolve("generated"), - this.temp.resolve("eo-to-java") - ); - tojava.compile(); - final Path file = this.temp.resolve(Paths.get("eo-to-java/test.xml")); - MatcherAssert.assertThat( - Files.exists(file), - Matchers.is(true) - ); - final String java = new TextOf(new InputOf(file)).asString(); - Logger.info(this, "Java output: %s\n", java); - MatcherAssert.assertThat( - java, - Matchers.containsString("public final class EOfibo") - ); - } - -} diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 2aef3636c2..41af573722 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -20,6 +20,8 @@ # alpha > beta # alpha # 'Z' -[] > main - stdout > @ +[n x] > test + "dude!" > @ + n.hello > f "Hello, world!" + x From dbb6fb336d5ab6882cc07d6ed61fd711b60fd36f Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 12:11:51 +0300 Subject: [PATCH 37/80] #164: polished --- .../java/org/eolang/compiler/XeListener.java | 1 + .../java/org/eolang/compiler/PacksTest.java | 8 +- eo-maven-plugin/src/it/fibonacci/pom.xml | 3 + eo-maven-plugin/src/it/simple/pom.xml | 3 + .../java/org/eolang/maven/OptimizeMojo.java | 15 +- .../main/java/org/eolang/maven/ParseMojo.java | 7 - .../main/java/org/eolang/maven/PullMojo.java | 152 ++++++++++++++++++ .../main/java/org/eolang/maven/TargetSpy.java | 25 +++ .../resources/org/eolang/maven/to-java.xsl | 15 +- .../org/eolang/maven/CompileMojoTest.java | 5 +- .../src/test/java/org/eolang/maven/Mojo.java | 39 ++++- .../org/eolang/maven/OptimizeMojoTest.java | 6 +- .../java/org/eolang/maven/ParseMojoTest.java | 6 +- .../java/org/eolang/maven/PullMojoTest.java | 81 ++++++++++ 14 files changed, 319 insertions(+), 47 deletions(-) create mode 100644 eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java create mode 100644 eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 0596ba7504..d629a0ec50 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -162,6 +162,7 @@ public void enterAbstraction(final ProgramParser.AbstractionContext ctx) { @Override public void exitAbstraction(final ProgramParser.AbstractionContext ctx) { + // Nothing here } @Override diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 35174b9ae2..8ed3a30b25 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -24,6 +24,7 @@ package org.eolang.compiler; import com.jcabi.matchers.XhtmlMatchers; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import java.io.ByteArrayOutputStream; import java.util.Collection; @@ -60,10 +61,9 @@ public void testPacks(final String pack) throws Exception { new InputOf(String.format("%s\n", map.get("eo"))), new OutputTo(baos) ).parse(); - final Program program = new Program( - new XMLDocument(baos.toString()), - new OutputTo(baos) - ); + final XML xml = new XMLDocument(baos.toString()); + baos.reset(); + final Program program = new Program(xml, new OutputTo(baos)); final Collection xsls = (Collection) map.get("xsls"); if (xsls == null) { program.compile(); diff --git a/eo-maven-plugin/src/it/fibonacci/pom.xml b/eo-maven-plugin/src/it/fibonacci/pom.xml index 4c20d65505..b7412b2ffa 100644 --- a/eo-maven-plugin/src/it/fibonacci/pom.xml +++ b/eo-maven-plugin/src/it/fibonacci/pom.xml @@ -44,6 +44,9 @@ SOFTWARE. + parse + optimize + pull compile diff --git a/eo-maven-plugin/src/it/simple/pom.xml b/eo-maven-plugin/src/it/simple/pom.xml index c72892e76f..1e4ede170a 100644 --- a/eo-maven-plugin/src/it/simple/pom.xml +++ b/eo-maven-plugin/src/it/simple/pom.xml @@ -50,6 +50,9 @@ SOFTWARE. + parse + optimize + pull compile diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index e73c1ff55f..65b893baa6 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -36,7 +36,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; -import org.apache.maven.project.MavenProject; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -61,12 +60,6 @@ ) public final class OptimizeMojo extends AbstractMojo { - /** - * Maven project. - */ - @Parameter(defaultValue = "${project}") - private MavenProject project; - /** * From directory. * @checkstyle MemberNameCheck (7 lines) @@ -84,7 +77,7 @@ public void execute() throws MojoFailureException { try { Files.walk(dir) .filter(file -> !file.toFile().isDirectory()) - .forEach(this::optimize); + .forEach(file -> this.optimize(dir, file)); } catch (final IOException ex) { throw new MojoFailureException( new UncheckedText( @@ -100,11 +93,13 @@ public void execute() throws MojoFailureException { /** * Optimize XML file after parsing. + * + * @param home Where it was found * @param file EO file */ - private void optimize(final Path file) { + private void optimize(final Path home, final Path file) { final String name = file.toString().substring( - this.targetDir.toString().length() + 2 + "eo/parse".length() + home.toString().length() + 1 ); final Path dir = this.targetDir.toPath() .resolve("eo/steps") diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java index ae6fd997b4..96f6fea6b2 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java @@ -35,7 +35,6 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; -import org.apache.maven.project.MavenProject; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -60,12 +59,6 @@ ) public final class ParseMojo extends AbstractMojo { - /** - * Maven project. - */ - @Parameter(defaultValue = "${project}") - private MavenProject project; - /** * Target directory. * @checkstyle MemberNameCheck (7 lines) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java new file mode 100644 index 0000000000..9754287b6a --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java @@ -0,0 +1,152 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XMLDocument; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Pull EO XML files. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "pull", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class PullMojo extends AbstractMojo { + + /** + * From directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + final Path dir = this.targetDir.toPath().resolve("eo/optimize"); + try { + Files.walk(dir) + .filter(file -> !file.toFile().isDirectory()) + .forEach(this::pull); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list XML files in %s", + dir + ) + ).asString(), + ex + ); + } + } + + /** + * Pull all deps found in XML file. + * + * @param file EO file + */ + private void pull(final Path file) { + try { + final String xpath = String.join( + "", + "//o[@base and contains(@base, '.') ", + "and not(starts-with(@base, '.'))]/@base" + ); + for (final String name : new XMLDocument(file).xpath(xpath)) { + this.pull(name); + } + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't pull %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + } + + /** + * Pull one dep. + * + * @param name Name of the object, e.g. "org.eolang.io.stdout" + * @throws IOException If fails + */ + private void pull(final String name) throws IOException { + final String res = String.format("/%s.eo.xml", name.replace(".", "/")); + final InputStream input = this.getClass().getResourceAsStream(res); + if (input == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" in classpath", + res + ) + ); + } + final Path path = this.targetDir.toPath() + .resolve("eo/pull") + .resolve(String.format("%s.eo.xml", name.replace(".", "/"))); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(input), + new OutputTo(path) + ) + ) + ).value(); + Logger.info(this, "%s pulled to %s", name, path); + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java index 13b716ad7e..5adc6d35bc 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ package org.eolang.maven; import com.jcabi.xml.XML; @@ -14,6 +37,8 @@ /** * The spy to log all results. + * + * @since 0.1 */ final class TargetSpy implements Program.Spy { /** diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index c8e7e6ce7e..1fdfb13b73 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -26,11 +26,14 @@ SOFTWARE. - + + + + @@ -80,7 +83,9 @@ SOFTWARE. public static final - 𝑥 = new + + + = new (); @@ -194,7 +199,8 @@ SOFTWARE. - .𝑥 + . + this. @@ -231,7 +237,8 @@ SOFTWARE. - .𝑥 + . + diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index 2608f4a8f3..30e27b236f 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -26,7 +26,6 @@ import com.jcabi.log.Logger; import java.nio.file.Files; import java.nio.file.Path; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; @@ -44,7 +43,7 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class CompileMojoTest extends AbstractMojoTestCase { +public final class CompileMojoTest { @Test public void testSimpleCompilation() throws Exception { @@ -61,12 +60,10 @@ public void testSimpleCompilation() throws Exception { final Path target = temp.resolve("target"); final Path generated = temp.resolve("generated"); new Mojo<>(ParseMojo.class) - .with("project", new MavenProjectStub()) .with("targetDir", target.toFile()) .with("sourcesDir", src.toFile()) .execute(); new Mojo<>(OptimizeMojo.class) - .with("project", new MavenProjectStub()) .with("targetDir", target.toFile()) .execute(); new Mojo<>(CompileMojo.class) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java index dc195d30b0..d2915a61a1 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java @@ -24,41 +24,61 @@ package org.eolang.maven; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.codehaus.plexus.util.ReflectionUtils; /** * Mutable mojo builder. * + * @param Type of mojo * @since 0.1 */ -final class Mojo { +final class Mojo { + /** + * The type of mojo. + */ private final Class type; + /** + * All attributes. + */ private final Map attrs; + /** + * Ctor. + * + * @param tpe The type + */ Mojo(final Class tpe) { this.type = tpe; this.attrs = new HashMap<>(0); } + /** + * Add one more attribute and return self. + * + * @param attr The name + * @param value The value + * @return Itself + */ public Mojo with(final String attr, final Object value) { this.attrs.put(attr, value); return this; } + /** + * Execute it. + */ public void execute() { try { - final AbstractMojo mojo = this.type.newInstance(); + final AbstractMojo mojo = this.type.getConstructor().newInstance(); for (final Map.Entry ent : this.attrs.entrySet()) { - final Field field = - ReflectionUtils.getFieldByNameIncludingSuperclasses( - ent.getKey(), this.type); + final Field field = this.type.getDeclaredField(ent.getKey()); if (field == null) { throw new IllegalStateException( String.format( @@ -73,8 +93,11 @@ public void execute() { field.set(mojo, ent.getValue()); } mojo.execute(); - } catch (MojoExecutionException | MojoFailureException | InstantiationException | IllegalAccessException e) { - throw new IllegalStateException(e); + } catch (final MojoExecutionException | MojoFailureException + | InstantiationException | IllegalAccessException + | NoSuchMethodException | InvocationTargetException + | NoSuchFieldException ex) { + throw new IllegalStateException(ex); } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java index d4498f2c3e..48969a1fa0 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java @@ -25,8 +25,6 @@ import java.nio.file.Files; import java.nio.file.Path; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -42,7 +40,7 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class OptimizeMojoTest extends AbstractMojoTestCase { +public final class OptimizeMojoTest { @Test public void testSimpleOptimize() throws Exception { @@ -58,12 +56,10 @@ public void testSimpleOptimize() throws Exception { ).value(); final Path target = temp.resolve("target"); new Mojo<>(ParseMojo.class) - .with("project", new MavenProjectStub()) .with("targetDir", target.toFile()) .with("sourcesDir", src.toFile()) .execute(); new Mojo<>(OptimizeMojo.class) - .with("project", new MavenProjectStub()) .with("targetDir", target.toFile()) .execute(); MatcherAssert.assertThat( diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java index 50bb116594..91bcbd0549 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java @@ -25,8 +25,6 @@ import java.nio.file.Files; import java.nio.file.Path; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -44,7 +42,7 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class ParseMojoTest extends AbstractMojoTestCase { +public final class ParseMojoTest { @Test public void testSimpleCompilation() throws Exception { @@ -60,7 +58,6 @@ public void testSimpleCompilation() throws Exception { ) ).value(); new Mojo<>(ParseMojo.class) - .with("project", new MavenProjectStub()) .with("sourcesDir", src.toFile()) .with("targetDir", target.toFile()) .execute(); @@ -84,7 +81,6 @@ public void testCrashOnInvalidSyntax() throws Exception { Assertions.assertThrows( CompileException.class, () -> new Mojo<>(ParseMojo.class) - .with("project", new MavenProjectStub()) .with("sourcesDir", src.toFile()) .with("targetDir", target.toFile()) .execute() diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java new file mode 100644 index 0000000000..9d76a984cf --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java @@ -0,0 +1,81 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.eolang.maven; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link PullMojo}. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class PullMojoTest { + + @Test + @Disabled + public void testSimpleOptimize() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + new LengthOf( + new TeeInput( + new InputOf( + String.join( + "\n", + "+alias stdout org.eolang.io.stdout", + "", + "[args] > main\n (stdout \"Hello!\").print\n" + ) + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + final Path target = temp.resolve("target"); + new Mojo<>(ParseMojo.class) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("targetDir", target.toFile()) + .execute(); + new Mojo<>(PullMojo.class) + .with("targetDir", target.toFile()) + .execute(); + MatcherAssert.assertThat( + Files.exists(target.resolve("eo/pull/org/stdout/io/stdout.eo.xml")), + Matchers.is(true) + ); + } + +} From 87995757bdde217d0a4104d85d0c517715342948 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 12:15:40 +0300 Subject: [PATCH 38/80] #164: show listing in XML --- eo-compiler/src/main/java/org/eolang/compiler/XeListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index d629a0ec50..d0e4178a8e 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -83,6 +83,7 @@ public void enterProgram(final ProgramParser.ProgramContext ctx) { DateTimeFormatter.ISO_INSTANT ) ) + .add("listing").set(ctx.getText()).up() .add("errors").up(); } From 1c2df02aacb7dca94fa0620a192d47539816d131 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 12:25:29 +0300 Subject: [PATCH 39/80] #164: disable --- eo-maven-plugin/src/{it => it-disabled}/README.md | 0 .../fibonacci/invoker.properties | 0 .../src/{it => it-disabled}/fibonacci/pom.xml | 0 .../src/main/eo/org/eolang/examples/app.eo | 0 .../src/main/eo/org/eolang/examples/fibonacci.eo | 0 .../src/main/java/org/eolang/examples/Main.java | 0 .../{it => it-disabled}/fibonacci/verify.groovy | 0 .../src/{it => it-disabled}/settings.xml | 0 .../{it => it-disabled}/simple/invoker.properties | 0 .../src/{it => it-disabled}/simple/pom.xml | 0 .../simple/src/main/eo/pixel.eo | 0 .../src/{it => it-disabled}/simple/verify.groovy | 0 .../main/java/org/eolang/maven/OptimizeMojo.java | 14 ++++++++------ 13 files changed, 8 insertions(+), 6 deletions(-) rename eo-maven-plugin/src/{it => it-disabled}/README.md (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/invoker.properties (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/pom.xml (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/src/main/eo/org/eolang/examples/app.eo (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/src/main/java/org/eolang/examples/Main.java (100%) rename eo-maven-plugin/src/{it => it-disabled}/fibonacci/verify.groovy (100%) rename eo-maven-plugin/src/{it => it-disabled}/settings.xml (100%) rename eo-maven-plugin/src/{it => it-disabled}/simple/invoker.properties (100%) rename eo-maven-plugin/src/{it => it-disabled}/simple/pom.xml (100%) rename eo-maven-plugin/src/{it => it-disabled}/simple/src/main/eo/pixel.eo (100%) rename eo-maven-plugin/src/{it => it-disabled}/simple/verify.groovy (100%) diff --git a/eo-maven-plugin/src/it/README.md b/eo-maven-plugin/src/it-disabled/README.md similarity index 100% rename from eo-maven-plugin/src/it/README.md rename to eo-maven-plugin/src/it-disabled/README.md diff --git a/eo-maven-plugin/src/it/fibonacci/invoker.properties b/eo-maven-plugin/src/it-disabled/fibonacci/invoker.properties similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/invoker.properties rename to eo-maven-plugin/src/it-disabled/fibonacci/invoker.properties diff --git a/eo-maven-plugin/src/it/fibonacci/pom.xml b/eo-maven-plugin/src/it-disabled/fibonacci/pom.xml similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/pom.xml rename to eo-maven-plugin/src/it-disabled/fibonacci/pom.xml diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/app.eo similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/app.eo diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/java/org/eolang/examples/Main.java similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/java/org/eolang/examples/Main.java diff --git a/eo-maven-plugin/src/it/fibonacci/verify.groovy b/eo-maven-plugin/src/it-disabled/fibonacci/verify.groovy similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/verify.groovy rename to eo-maven-plugin/src/it-disabled/fibonacci/verify.groovy diff --git a/eo-maven-plugin/src/it/settings.xml b/eo-maven-plugin/src/it-disabled/settings.xml similarity index 100% rename from eo-maven-plugin/src/it/settings.xml rename to eo-maven-plugin/src/it-disabled/settings.xml diff --git a/eo-maven-plugin/src/it/simple/invoker.properties b/eo-maven-plugin/src/it-disabled/simple/invoker.properties similarity index 100% rename from eo-maven-plugin/src/it/simple/invoker.properties rename to eo-maven-plugin/src/it-disabled/simple/invoker.properties diff --git a/eo-maven-plugin/src/it/simple/pom.xml b/eo-maven-plugin/src/it-disabled/simple/pom.xml similarity index 100% rename from eo-maven-plugin/src/it/simple/pom.xml rename to eo-maven-plugin/src/it-disabled/simple/pom.xml diff --git a/eo-maven-plugin/src/it/simple/src/main/eo/pixel.eo b/eo-maven-plugin/src/it-disabled/simple/src/main/eo/pixel.eo similarity index 100% rename from eo-maven-plugin/src/it/simple/src/main/eo/pixel.eo rename to eo-maven-plugin/src/it-disabled/simple/src/main/eo/pixel.eo diff --git a/eo-maven-plugin/src/it/simple/verify.groovy b/eo-maven-plugin/src/it-disabled/simple/verify.groovy similarity index 100% rename from eo-maven-plugin/src/it/simple/verify.groovy rename to eo-maven-plugin/src/it-disabled/simple/verify.groovy diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 65b893baa6..2392e4c73d 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -109,18 +109,21 @@ private void optimize(final Path home, final Path file) { new Program( new XMLDocument(file), new OutputTo(baos) ).compile(new TargetSpy(dir)); + final Path target = this.targetDir.toPath() + .resolve("eo/optimize") + .resolve(name); new IoChecked<>( new LengthOf( new TeeInput( new InputOf(baos.toString()), - new OutputTo( - this.targetDir.toPath() - .resolve("eo/optimize") - .resolve(name) - ) + new OutputTo(target) ) ) ).value(); + Logger.info( + this, "%s optimized to %s, all steps are in %s", + file, target, dir + ); } catch (final IOException ex) { throw new IllegalStateException( new UncheckedText( @@ -132,7 +135,6 @@ private void optimize(final Path home, final Path file) { ex ); } - Logger.info(this, "%s optimized to %s", file, dir); } } From c2d7939bce5d89e44e10a1b1d8a50191464b549e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 15:09:34 +0300 Subject: [PATCH 40/80] #164: new syntax --- .../antlr4/org/eolang/compiler/Program.g4 | 5 +- .../java/org/eolang/compiler/XeListener.java | 4 +- .../eolang/compiler/packs/full-syntax.yaml | 2 +- eo-objects/LICENSE.txt | 21 ------- eo-objects/pom.xml | 59 ------------------- eo-objects/src/main/eo/org/eolang/Not.eo | 27 --------- eo-objects/src/main/eo/org/eolang/Sub.eo | 29 --------- eo-runtime/src/main/eo/org/eolang/array.eo | 13 ++++ eo-runtime/src/main/eo/org/eolang/bool.eo | 2 + eo-runtime/src/main/eo/org/eolang/int.eo | 12 ++++ .../src/main/eo/org/eolang/io/stdout.eo | 2 + eo-runtime/src/main/eo/org/eolang/string.eo | 2 + pom.xml | 1 - 13 files changed, 36 insertions(+), 143 deletions(-) delete mode 100644 eo-objects/LICENSE.txt delete mode 100644 eo-objects/pom.xml delete mode 100644 eo-objects/src/main/eo/org/eolang/Not.eo delete mode 100644 eo-objects/src/main/eo/org/eolang/Sub.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/array.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/bool.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/int.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/io/stdout.eo create mode 100644 eo-runtime/src/main/eo/org/eolang/string.eo diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index e72d1bcfee..d971760a0d 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -70,8 +70,7 @@ abstraction LSQ (argument (SPACE argument)*)? RSQ - ATOM? - suffix? + (suffix (SPACE SLASH NAME)?)? ; argument @@ -190,7 +189,7 @@ META: PLUS NAME (SPACE ~[\r\n]+)?; DOTS: '...'; CONST: '!'; -ATOM: '?'; +SLASH: '/'; COLON: ':'; ARROW: '>'; PLUS: '+'; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index d0e4178a8e..56a290c8b5 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -155,8 +155,8 @@ public void exitObject(final ProgramParser.ObjectContext ctx) { @Override public void enterAbstraction(final ProgramParser.AbstractionContext ctx) { this.dirs.add("o").attr("line", ctx.getStart().getLine()); - if (ctx.ATOM() != null) { - this.dirs.attr("atom", ""); + if (ctx.SLASH() != null) { + this.dirs.attr("atom", ctx.NAME()); } this.dirs.up(); } diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index e841edc09f..3e41e7cbed 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -29,7 +29,7 @@ eo: | # This is just a simple string "Hello, world!" > hello! - [tt a...]? > atom + [tt a...] > atom /int [x] > first second > hello diff --git a/eo-objects/LICENSE.txt b/eo-objects/LICENSE.txt deleted file mode 100644 index 0fad6fb4d6..0000000000 --- a/eo-objects/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016-2020 Yegor Bugayenko - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/eo-objects/pom.xml b/eo-objects/pom.xml deleted file mode 100644 index 3de3e395ed..0000000000 --- a/eo-objects/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - 4.0.0 - - org.eolang - eo-parent - 1.0-SNAPSHOT - - eo-objects - jar - eo-objects - eolang supplementary objects - - - org.eolang - eo-runtime - ${project.version} - - - - - - org.eolang - eo-maven-plugin - ${project.version} - - - - compile - - - - - - - diff --git a/eo-objects/src/main/eo/org/eolang/Not.eo b/eo-objects/src/main/eo/org/eolang/Not.eo deleted file mode 100644 index ae0b969cf2..0000000000 --- a/eo-objects/src/main/eo/org/eolang/Not.eo +++ /dev/null @@ -1,27 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016-2020 Yegor Bugayenko -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -+package org.eolang - -# Returns TRUE if argument is FALSE and the other way around -[x] > not - if x false true diff --git a/eo-objects/src/main/eo/org/eolang/Sub.eo b/eo-objects/src/main/eo/org/eolang/Sub.eo deleted file mode 100644 index 4d93262356..0000000000 --- a/eo-objects/src/main/eo/org/eolang/Sub.eo +++ /dev/null @@ -1,29 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016-2020 Yegor Bugayenko -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -+package org.eolang - -# Calculates algebraic difference between x and y -[x y] > sub - add - x - mul y -1 diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo new file mode 100644 index 0000000000..6ee6bf0205 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -0,0 +1,13 @@ +[b] > array + + # Reduce from start "a" using the function "f" + [a f] > reduce /a + + # Map with index + [f] > mapi /array + + # Map without index + [f] > map + mapi + [x i] + f x diff --git a/eo-runtime/src/main/eo/org/eolang/bool.eo b/eo-runtime/src/main/eo/org/eolang/bool.eo new file mode 100644 index 0000000000..5abdf472de --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/bool.eo @@ -0,0 +1,2 @@ +[b] > bool + [t f] > if /t diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo new file mode 100644 index 0000000000..895202a4f2 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -0,0 +1,12 @@ +[b] > int + [] > neg | int + [x...] > add /int + [x...] > sub + x.reduce + $ + [a b] + a.add b.neg + [x...] > mul /int + [x...] > div /int + [x] > mod /int + diff --git a/eo-runtime/src/main/eo/org/eolang/io/stdout.eo b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo new file mode 100644 index 0000000000..12671e7027 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo @@ -0,0 +1,2 @@ +[text] > stdout + [] > @ /bool diff --git a/eo-runtime/src/main/eo/org/eolang/string.eo b/eo-runtime/src/main/eo/org/eolang/string.eo new file mode 100644 index 0000000000..1b7f155d95 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/string.eo @@ -0,0 +1,2 @@ +[b] > string + [] > trim /string diff --git a/pom.xml b/pom.xml index 12134d1066..cc0e5f5a3d 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,6 @@ SOFTWARE. eo-compiler eo-maven-plugin eo-archetype - eo-objects eo-runtime EO Language From c19015131642b6d2ee50ffb2001b9bc2c7f4dfda Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 16:02:58 +0300 Subject: [PATCH 41/80] #164: check --- .../compiler/errors/not-empty-atoms.xsl | 50 +++++++++++++++++++ .../java/org/eolang/compiler/PacksTest.java | 1 + .../packs/catches-nonempty-atoms.yaml | 10 ++++ 3 files changed, 61 insertions(+) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl new file mode 100644 index 0000000000..b86c7d77a8 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl @@ -0,0 +1,50 @@ + + + + + + + + + + + not-empty-atoms + + + + + Atoms + + may not have any attributes + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 8ed3a30b25..b2e00e00d6 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -89,6 +89,7 @@ private static Collection yamlPacks() { "catches-global-nonames.yaml", "catches-broken-aliases.yaml", "catches-middle-vararg.yaml", + "catches-nonempty-atoms.yaml", "catches-unknown-names.yaml", "catches-self-naming.yaml", "catches-reserved-atoms.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml new file mode 100644 index 0000000000..f07a790f66 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml @@ -0,0 +1,10 @@ +xsls: + - errors/not-empty-atoms.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='3'] +eo: | + [] > test /int + + [] > foo /string + a > hey From edad6ace9a1b70743f3f62d4e6b60415674a9455 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 16:31:10 +0300 Subject: [PATCH 42/80] #164: empty lines allowed --- .../src/main/antlr4/org/eolang/compiler/Program.g4 | 6 ++---- .../org/eolang/compiler/packs/full-syntax.yaml | 13 ++++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index d971760a0d..b17ca7c782 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -31,13 +31,11 @@ program license : (COMMENT EOL)+ - EOL ; metas : (META EOL)+ - EOL ; objects @@ -46,7 +44,6 @@ objects (COMMENT EOL)* object EOL - EOL? )+ ; @@ -67,6 +64,7 @@ object abstraction : + (COMMENT EOL)* LSQ (argument (SPACE argument)*)? RSQ @@ -204,7 +202,7 @@ AT: '@'; HASH: '#'; EOL : - [\r\n] + [\r\n]+ SPACE* { int tabs = getText().replaceAll("[\r\n]+", "").length() / 2; diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 3e41e7cbed..571abb8649 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -3,7 +3,7 @@ tests: - /program/license[text()!=''] - /program/metas[count(meta)=3] - /program/metas/meta[head='foo' and tail=''] - - /program/objects[count(o)=6] + - /program/objects[count(o)=7] - //o[@base='bool'] - //o[@base='string'] - //o[@base='char' and text()='e'] @@ -31,11 +31,15 @@ eo: | [tt a...] > atom /int + # This is very good object [x] > first second > hello third:foo > x f 12 false (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! + # This object is also very good + [] + hey > you kid f:u z @@ -55,3 +59,10 @@ eo: | test me now:i (f (f (f (f 1)))).f + + [] > ooo + # This is one + [] > o1 + + # This is two + [] > o2 From 2e7d63347f16547c798807138a1be8f0389ba48a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 16:47:41 +0300 Subject: [PATCH 43/80] #164: more --- .../src/main/resources/org/eolang/maven/to-java.xsl | 2 -- .../test/java/org/eolang/maven/CompileMojoTest.java | 9 ++++----- eo-runtime/src/main/eo/org/eolang/int.eo | 13 ++++++++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 1fdfb13b73..7e7d1881db 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -69,8 +69,6 @@ SOFTWARE. import org.eolang.*; - import org.eolang.sys.*; - public class diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index 30e27b236f..3f75eba6cb 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -29,6 +29,7 @@ import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; +import org.cactoos.io.ResourceOf; import org.cactoos.io.TeeInput; import org.cactoos.scalar.LengthOf; import org.cactoos.text.TextOf; @@ -51,10 +52,8 @@ public void testSimpleCompilation() throws Exception { final Path src = temp.resolve("src"); new LengthOf( new TeeInput( - new InputOf( - "[args] > main\n (stdout \"Hello!\").print > x\n" - ), - new OutputTo(src.resolve("main.eo")) + new ResourceOf("/org/eolang/maven/mess.eo"), + new OutputTo(src.resolve("mess.eo")) ) ).value(); final Path target = temp.resolve("target"); @@ -71,7 +70,7 @@ public void testSimpleCompilation() throws Exception { .with("targetDir", target.toFile()) .with("generatedDir", generated.toFile()) .execute(); - final Path java = generated.resolve("EOmain.java"); + final Path java = generated.resolve("EOmess.java"); MatcherAssert.assertThat( Files.exists(java), Matchers.is(true) diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index 895202a4f2..ef4ef584f6 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -1,12 +1,23 @@ [b] > int - [] > neg | int + # Change the sign of the number + [] > neg /int + + # Add a few more numbers to the current one [x...] > add /int + + # Subtract some numbers from the current one [x...] > sub x.reduce $ [a b] a.add b.neg + + # Multiply this one by some other numbers [x...] > mul /int + + # Divide this one by others [x...] > div /int + + # Calculate MOD [x] > mod /int From 36f6809d3afb4841c69cdd4ffdc2826cb6f6437a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 17:00:19 +0300 Subject: [PATCH 44/80] #164: compiles --- eo-maven-plugin/pom.xml | 4 ++-- .../src/main/resources/org/eolang/maven/to-java.xsl | 6 +++--- .../src/test/java/org/eolang/maven/CompileMojoTest.java | 4 ++-- eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/eo-maven-plugin/pom.xml b/eo-maven-plugin/pom.xml index d2609e7074..ec5548ebdf 100644 --- a/eo-maven-plugin/pom.xml +++ b/eo-maven-plugin/pom.xml @@ -160,8 +160,8 @@ SOFTWARE. qulice-maven-plugin - checkstyle:/src/it/.* - pmd:/src/it/.* + checkstyle:/src/it.* + pmd:/src/it.* duplicatefinder:.* xml:/src/test/resources/META-INF/maven/plugin.xml diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 7e7d1881db..e651704511 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -57,7 +57,7 @@ SOFTWARE. - + @@ -125,7 +125,7 @@ SOFTWARE. public - cp( + _copy( , @@ -247,7 +247,7 @@ SOFTWARE. - .cp( + ._copy( diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index 3f75eba6cb..441a33eccb 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -52,7 +52,7 @@ public void testSimpleCompilation() throws Exception { final Path src = temp.resolve("src"); new LengthOf( new TeeInput( - new ResourceOf("/org/eolang/maven/mess.eo"), + new ResourceOf("org/eolang/maven/mess.eo"), new OutputTo(src.resolve("mess.eo")) ) ).value(); @@ -79,7 +79,7 @@ public void testSimpleCompilation() throws Exception { Logger.info(this, "Java output:\n%s", code); MatcherAssert.assertThat( code, - Matchers.containsString("class EOmain") + Matchers.containsString("class EOmess") ); } diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo index 41af573722..3c40d90aa7 100644 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -20,8 +20,8 @@ # alpha > beta # alpha # 'Z' -[n x] > test - "dude!" > @ +[n x] > mess + [a] > test /int n.hello > f "Hello, world!" x From cb27e0d7d7e5f0f3db9e904e5147d106eac809bf Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 17:01:11 +0300 Subject: [PATCH 45/80] #164: dep off --- eo-maven-plugin/pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/eo-maven-plugin/pom.xml b/eo-maven-plugin/pom.xml index ec5548ebdf..59a7a70039 100644 --- a/eo-maven-plugin/pom.xml +++ b/eo-maven-plugin/pom.xml @@ -42,12 +42,6 @@ SOFTWARE. eo-compiler ${project.version} - - org.eolang - eo-runtime - ${project.version} - provided - org.antlr antlr4-runtime From b6a926f278588023b43d59b10693a5af42ba9401 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 17:05:01 +0300 Subject: [PATCH 46/80] #164: self --- .../antlr4/org/eolang/compiler/Program.g4 | 3 +++ .../eolang/compiler/packs/full-syntax.yaml | 1 + eo-runtime/pom.xml | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index b17ca7c782..6f177927aa 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -152,6 +152,8 @@ head : AT | + SELF + | NAME | NAME DOT @@ -190,6 +192,7 @@ CONST: '!'; SLASH: '/'; COLON: ':'; ARROW: '>'; +SELF: '$'; PLUS: '+'; MINUS: '-'; SPACE: ' '; diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 571abb8649..03313a6c95 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -34,6 +34,7 @@ eo: | # This is very good object [x] > first second > hello + $.add 5 > i third:foo > x f 12 false (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! diff --git a/eo-runtime/pom.xml b/eo-runtime/pom.xml index c078feb51d..92b9891b3b 100644 --- a/eo-runtime/pom.xml +++ b/eo-runtime/pom.xml @@ -39,4 +39,23 @@ SOFTWARE. cactoos + + + + org.eolang + eo-maven-plugin + ${project.version} + + + + parse + optimize + pull + compile + + + + + + From 984882d4f0bba9e48670fdd2c4d8e95c15e71562 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 17:09:57 +0300 Subject: [PATCH 47/80] #164: reserved atoms are welcome --- .../main/java/org/eolang/compiler/Pack.java | 3 +- .../eolang/compiler/errors/reserved-atoms.xsl | 64 ------------------- .../java/org/eolang/compiler/PacksTest.java | 1 - .../packs/catches-reserved-atoms.yaml | 12 ---- 4 files changed, 2 insertions(+), 78 deletions(-) delete mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl delete mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index fc31a65415..ceab6adf55 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -52,11 +52,12 @@ public final class Pack implements Iterable { public Pack() { this( new ListOf<>( + "errors/not-empty-atoms.xsl", + "errors/middle-varargs.xsl", "errors/duplicate-names.xsl", "errors/broken-aliases.xsl", "errors/duplicate-aliases.xsl", "errors/global-nonames.xsl", - "errors/reserved-atoms.xsl", "errors/same-line-names.xsl", "errors/self-naming.xsl", "add-refs.xsl", diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl deleted file mode 100644 index f71cb1cb75..0000000000 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - reserved-atoms - - - - - You can't copy " - - " as a normal object - - - - - - reserved-atoms - - - - - You can't use " - - " as a name of your object, it's a reserved name - - - - - - - - - - diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index b2e00e00d6..5368e9f0c7 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -92,7 +92,6 @@ private static Collection yamlPacks() { "catches-nonempty-atoms.yaml", "catches-unknown-names.yaml", "catches-self-naming.yaml", - "catches-reserved-atoms.yaml", "catches-noname-attrs.yaml", "catches-same-line-name.yaml", "all-data-types.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml deleted file mode 100644 index 39fe72d866..0000000000 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml +++ /dev/null @@ -1,12 +0,0 @@ -xsls: - - errors/reserved-atoms.xsl -tests: - - /program/errors[count(error)=3] -eo: | - [x] > first - string > x - "test" > works - withoutaname - "" > @ - float > test - 123 > string From 685a1b48a7ae98545bf1418c755d58706f12a6ba Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 18:13:55 +0300 Subject: [PATCH 48/80] #164: show error --- .../src/main/java/org/eolang/maven/CompileMojo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index c71345841d..aa0d4bdeac 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -172,8 +172,8 @@ private void compile(final Path file) { if (!errors.isEmpty()) { throw new IllegalStateException( String.format( - "There are %d errors, see log above", - errors.size() + "There are %d errors in %s, see log above", + errors.size(), file ) ); } From 8c4b5afe5aa95a096efa00ae5cf967698a7adbe3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 18:19:02 +0300 Subject: [PATCH 49/80] #164: typo --- eo-compiler/src/main/java/org/eolang/compiler/XeListener.java | 3 +++ eo-runtime/src/main/eo/org/eolang/array.eo | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 56a290c8b5..3af9b7c555 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -232,6 +232,9 @@ public void enterHead(final ProgramParser.HeadContext ctx) { if (ctx.AT() != null) { this.dirs.attr("base", "@"); } + if (ctx.SELF() != null) { + this.dirs.attr("base", "$"); + } } @Override diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo index 6ee6bf0205..9527c39afc 100644 --- a/eo-runtime/src/main/eo/org/eolang/array.eo +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -8,6 +8,6 @@ # Map without index [f] > map - mapi + mapi > @ [x i] f x From fe38f2729a6393a625236fe73a00b826ab7e1cd3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 18:49:18 +0300 Subject: [PATCH 50/80] #164: resolving --- .../main/java/org/eolang/compiler/Pack.java | 1 + .../eolang/compiler/add-default-package.xsl | 45 +++++++++++++++++++ .../org/eolang/compiler/resolve-aliases.xsl | 1 - .../java/org/eolang/compiler/PacksTest.java | 1 + .../compiler/packs/adds-default-package.yaml | 20 +++++++++ .../compiler/packs/resolves-aliases.yaml | 4 +- eo-runtime/src/main/eo/org/eolang/int.eo | 2 +- 7 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl create mode 100644 eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index ceab6adf55..618f6c493c 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -65,6 +65,7 @@ public Pack() { "vars-float-up.xsl", "add-refs.xsl", "resolve-aliases.xsl", + "add-default-package.xsl", "errors/unknown-names.xsl", "errors/noname-attributes.xsl" ) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl new file mode 100644 index 0000000000..4dfd09dfb5 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl @@ -0,0 +1,45 @@ + + + + + + + + + org.eolang. + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl index 2322bcc3ae..9025cc17ff 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl @@ -60,7 +60,6 @@ SOFTWARE. - org.eolang. diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 5368e9f0c7..136ef5cf26 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -97,6 +97,7 @@ private static Collection yamlPacks() { "all-data-types.yaml", "fixes-globals.yaml", "adds-refs.yaml", + "adds-default-package.yaml", "float-vars.yaml", "float-abstracts.yaml", "resolves-aliases.yaml", diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml new file mode 100644 index 0000000000..27084d6dee --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml @@ -0,0 +1,20 @@ +xsls: + - add-refs.xsl + - resolve-aliases.xsl + - add-default-package.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=1] + - //o[@base='org.eolang.and' and @line='7'] + - //o[@base='$'] +eo: | + +alias stdout org.eolang.io.stdout + +alias stdin org.eolang.io.stdin + +alias scanner org.eolang.txt.scanner + +foo Some other meta + + [args] > main + and + (scanner stdin).nextLine > line! + (stdout "You entered" line).print + $ diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml index d06c22cac5..1dd395c989 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml @@ -7,7 +7,8 @@ tests: - /program/metas/meta[head='foo' and @line='4'] - /program/metas[count(meta)=1] - //o[@base='org.eolang.io.stdout' and @line='9'] - - //o[@base='org.eolang.and' and @line='7'] + - //o[@base='and' and @line='7'] + - //o[@base='$'] eo: | +alias stdout org.eolang.io.stdout +alias stdin org.eolang.io.stdin @@ -18,3 +19,4 @@ eo: | and (scanner stdin).nextLine > line! (stdout "You entered" line).print + $ diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index ef4ef584f6..7fa83b668b 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -7,7 +7,7 @@ # Subtract some numbers from the current one [x...] > sub - x.reduce + x.reduce > @ $ [a b] a.add b.neg From e324f6aadf3314ebd21804389be168c4a8f9f89f Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 20:03:16 +0300 Subject: [PATCH 51/80] #164: fix of floats --- .../java/org/eolang/compiler/Program.java | 4 +-- .../resources/org/eolang/compiler/_funcs.xsl | 2 +- .../eolang/compiler/abstracts-float-up.xsl | 9 +----- .../java/org/eolang/compiler/PacksTest.java | 2 ++ .../compiler/packs/float-abstracts.yaml | 5 +++- .../java/org/eolang/maven/CompileMojo.java | 26 ++++++----------- .../java/org/eolang/maven/OptimizeMojo.java | 10 +++++-- .../resources/org/eolang/maven/to-java.xsl | 28 +++++++++++++++---- 8 files changed, 47 insertions(+), 39 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 00560320f6..9fd3720c72 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -109,10 +109,8 @@ public void compile(final Iterable xsls) throws IOException { * * @param xsls List of XSLs to apply * @param spy The spy - * @throws IOException If fails */ - public void compile(final Iterable xsls, - final Program.Spy spy) throws IOException { + public void compile(final Iterable xsls, final Program.Spy spy) { int index = 0; XML before = this.source; for (final XSL xsl : xsls) { diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl index bb0fee26f1..b2efe22696 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl @@ -25,7 +25,7 @@ SOFTWARE. - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl index 88a1735abb..6536f4f949 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl @@ -85,17 +85,10 @@ SOFTWARE. + - - - - - - - - diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 136ef5cf26..ab1fb201b3 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -23,6 +23,7 @@ */ package org.eolang.compiler; +import com.jcabi.log.Logger; import com.jcabi.matchers.XhtmlMatchers; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; @@ -70,6 +71,7 @@ public void testPacks(final String pack) throws Exception { } else { program.compile(new Pack(xsls)); } + Logger.debug(this, "Output XML:\n%s", baos.toString()); for (final String xpath : (Iterable) map.get("tests")) { MatcherAssert.assertThat( XhtmlMatchers.xhtml(baos.toString()), diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml index 7c8723974f..8efab14d84 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml @@ -3,9 +3,11 @@ xsls: - abstracts-float-up.xsl - errors/duplicate-names.xsl tests: - - /program/objects[count(o)=6] + - /program/objects[count(o)=7] - /program/errors[count(*)=0] - //objects[not(.//o[@name=''])] + - //o[@name='first$2$native' and @atom='int'] + - //o[@base='first$2$native' and @atom='int'] - //o[@name='first'] - //o[@name='first']/o[@base='test' and @as='foo'] - //o[@name='first$2$second'] @@ -15,6 +17,7 @@ eo: | [a b] > first test:foo a > yes + [] > native /int [x b] > second a > no [b c] > third diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index aa0d4bdeac..483e35021c 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -27,7 +27,6 @@ import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; -import com.jcabi.xml.XSLChain; import com.jcabi.xml.XSLDocument; import java.io.File; import java.io.IOException; @@ -44,12 +43,12 @@ import org.apache.maven.project.MavenProject; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; +import org.cactoos.io.ResourceOf; import org.cactoos.io.TeeInput; -import org.cactoos.list.ListOf; -import org.cactoos.list.Mapped; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.LengthOf; import org.cactoos.text.FormattedText; +import org.cactoos.text.TextOf; import org.cactoos.text.UncheckedText; import org.slf4j.impl.StaticLoggerBinder; @@ -130,21 +129,14 @@ public void execute() throws MojoFailureException { * @param file XML file */ private void compile(final Path file) { - final Path temp = this.targetDir.toPath().resolve("eo/temp"); + final Path temp = this.targetDir.toPath().resolve("eo/compile"); try { final XML xml = new XMLDocument(file); - final XML out = new XSLChain( - new Mapped<>( - doc -> new XSLDocument( - CompileMojo.class.getResourceAsStream(doc) - ).with(new ClasspathSources()), - new ListOf<>( - "/org/eolang/compiler/globals-to-abstracts.xsl", - "/org/eolang/compiler/abstracts-float-up.xsl", - "to-java.xsl" - ) - ) - ).transform(xml); + final XML out = new XSLDocument( + new TextOf( + new ResourceOf("org/eolang/maven/to-java.xsl") + ).asString() + ).with(new ClasspathSources()).transform(xml); new IoChecked<>( new LengthOf( new TeeInput( @@ -182,7 +174,7 @@ private void compile(final Path file) { this.generatedDir.toPath().resolve( Paths.get( String.format( - "%s.java", java.xpath("@name").get(0) + "%s.java", java.xpath("@java-name").get(0) ) ) ), diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 2392e4c73d..0a0261891b 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -43,6 +43,7 @@ import org.cactoos.scalar.LengthOf; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; +import org.eolang.compiler.Pack; import org.eolang.compiler.Program; import org.slf4j.impl.StaticLoggerBinder; @@ -106,9 +107,12 @@ private void optimize(final Path home, final Path file) { .resolve(name); try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new Program( - new XMLDocument(file), new OutputTo(baos) - ).compile(new TargetSpy(dir)); + new Program(new XMLDocument(file), new OutputTo(baos)).compile( + new Pack() + .with("globals-to-abstracts.xsl") + .with("abstracts-float-up.xsl"), + new TargetSpy(dir) + ); final Path target = this.targetDir.toPath() .resolve("eo/optimize") .resolve(name); diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index e651704511..68a96999b4 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -55,14 +55,19 @@ SOFTWARE. - + + + + + + + + + - - - - + @@ -164,12 +169,22 @@ SOFTWARE. - public Object + public + + + + + + Object + + + _origin + eo @@ -206,6 +221,7 @@ SOFTWARE. this. + eo () From af0de1bf354c13dced6da6ec6a36acef96fdda6b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 20:13:20 +0300 Subject: [PATCH 52/80] #164: typos --- eo-runtime/src/main/eo/org/eolang/array.eo | 2 +- eo-runtime/src/main/eo/org/eolang/int.eo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo index 9527c39afc..bfdf0eb01e 100644 --- a/eo-runtime/src/main/eo/org/eolang/array.eo +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -10,4 +10,4 @@ [f] > map mapi > @ [x i] - f x + f x > @ diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index 7fa83b668b..0acbe25e18 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -10,7 +10,7 @@ x.reduce > @ $ [a b] - a.add b.neg + a.add b.neg > @ # Multiply this one by some other numbers [x...] > mul /int From c93dab9d78bc81f6427665c54369bb12021e9798 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 9 Dec 2020 23:33:40 +0300 Subject: [PATCH 53/80] #164: more --- .../main/java/org/eolang/compiler/Pack.java | 1 + .../eolang/compiler/abstracts-float-up.xsl | 14 ++++- .../eolang/compiler/errors/broken-refs.xsl | 55 +++++++++++++++++++ .../compiler/packs/float-abstracts.yaml | 2 + .../java/org/eolang/maven/CompileMojo.java | 11 ++-- .../java/org/eolang/maven/OptimizeMojo.java | 3 +- 6 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java index 618f6c493c..bd7f35cf1f 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -66,6 +66,7 @@ public Pack() { "add-refs.xsl", "resolve-aliases.xsl", "add-default-package.xsl", + "errors/broken-refs.xsl", "errors/unknown-names.xsl", "errors/noname-attributes.xsl" ) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl index 6536f4f949..f02a5d6576 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl @@ -84,11 +84,18 @@ SOFTWARE. + - + + + + + + + @@ -101,6 +108,8 @@ SOFTWARE. + + . @@ -115,6 +124,7 @@ SOFTWARE. + @@ -132,6 +142,8 @@ SOFTWARE. + + . diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl new file mode 100644 index 0000000000..409b9ba540 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -0,0 +1,55 @@ + + + + + + + + + + + + + broken-refs + + + + + The object " + + " at line + + is absent, but is referenced + + + + + + + + + + + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml index 8efab14d84..91c4eadacb 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml @@ -1,7 +1,9 @@ xsls: - add-refs.xsl - abstracts-float-up.xsl + - errors/same-line-names.xsl - errors/duplicate-names.xsl + - errors/broken-refs.xsl tests: - /program/objects[count(o)=7] - /program/errors[count(*)=0] diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 483e35021c..304a0aeddf 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -132,6 +132,7 @@ private void compile(final Path file) { final Path temp = this.targetDir.toPath().resolve("eo/compile"); try { final XML xml = new XMLDocument(file); + final String name = xml.xpath("/program/@name").get(0); final XML out = new XSLDocument( new TextOf( new ResourceOf("org/eolang/maven/to-java.xsl") @@ -142,12 +143,7 @@ private void compile(final Path file) { new TeeInput( new InputOf(out.toString()), new OutputTo( - temp.resolve( - String.format( - "%s.xml", - xml.xpath("/program/@name").get(0) - ) - ) + temp.resolve(String.format("%s.xml", name)) ) ) ) @@ -156,7 +152,8 @@ private void compile(final Path file) { for (final XML error : errors) { Logger.error( this, - "[%s] %s", + "[%s:%s] %s", + name, error.xpath("@line").get(0), error.xpath("text()").get(0) ); diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 0a0261891b..26003cd296 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -110,7 +110,8 @@ private void optimize(final Path home, final Path file) { new Program(new XMLDocument(file), new OutputTo(baos)).compile( new Pack() .with("globals-to-abstracts.xsl") - .with("abstracts-float-up.xsl"), + .with("abstracts-float-up.xsl") + .with("errors/broken-refs.xsl"), new TargetSpy(dir) ); final Path target = this.targetDir.toPath() From f1e1a9fccf0374934d444e1c4bdc044213293c5e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 11 Dec 2020 11:06:32 +0300 Subject: [PATCH 54/80] #164: fixes --- .../java/org/eolang/compiler/XeListener.java | 10 ++++- .../eolang/compiler/errors/broken-refs.xsl | 4 +- .../org/eolang/compiler/vars-float-up.xsl | 2 +- .../org/eolang/compiler/packs/adds-refs.yaml | 10 ++++- .../org/eolang/compiler/packs/float-vars.yaml | 9 +++- .../java/org/eolang/maven/CompileMojo.java | 5 ++- .../java/org/eolang/maven/OptimizeMojo.java | 4 ++ .../main/java/org/eolang/maven/TargetSpy.java | 2 + .../org/eolang/maven/CompileMojoTest.java | 42 +++++++++++++++---- 9 files changed, 69 insertions(+), 19 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 3af9b7c555..7220f718b8 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -55,6 +55,11 @@ public final class XeListener implements ProgramListener { */ private final Directives dirs; + /** + * When we start. + */ + private final long start; + /** * Ctor. * @param nme Tha name of it @@ -62,6 +67,7 @@ public final class XeListener implements ProgramListener { public XeListener(final String nme) { this.name = nme; this.dirs = new Directives(); + this.start = System.nanoTime(); } /** @@ -89,7 +95,9 @@ public void enterProgram(final ProgramParser.ProgramContext ctx) { @Override public void exitProgram(final ProgramParser.ProgramContext ctx) { - this.dirs.up(); + this.dirs + .attr("ms", (System.nanoTime() - this.start) / (1000L * 1000L)) + .up(); } @Override diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl index 409b9ba540..283775a209 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -39,9 +39,7 @@ SOFTWARE. The object " - " at line - - is absent, but is referenced + " is absent, but is referenced diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl index 7aea6c79d4..d0f72eff89 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl @@ -59,7 +59,7 @@ SOFTWARE. - + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml index 60d07016d2..af50f03039 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml @@ -5,7 +5,8 @@ xsls: - add-refs.xsl tests: - /program/errors[count(*)=0] - - /program/objects[count(o)=2] + - /program/objects[count(o)=3] + - //objects[not(//o[@ref and @base='@'])] - //o[@base='x' and @ref='1'] - //o[@base='x' and @ref='4'] - //o[@base='x' and @line='11' and @ref='4'] @@ -22,7 +23,7 @@ eo: | one x two - a + a > @ three x something > a @@ -31,3 +32,8 @@ eo: | tt x 4 first + + [] + one + [f] + 1 > ooo diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index 8f485876c5..19c38e3506 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -4,7 +4,7 @@ xsls: - errors/duplicate-names.xsl tests: - /program/errors[count(*)=0] - - /program/objects[count(o)=1] + - /program/objects[count(o)=2] - //o[@base='input' and @name='t' and @line='6' and @const and count(o)=2] - //objects[count(.//o[@base='string'])=1] - //o[@name='y']/o[@name='t'] @@ -12,6 +12,8 @@ tests: - //o[@name='last' and not(@cut)] - //objects[count(//o[@name='oops'])=1] - //objects[count(//o[@cut='0'])=0] + - //o[@name='aaa' and not(@cut)] + - //o[@name='ooo' and not(@cut)] eo: | [] > x first > ff @@ -25,3 +27,8 @@ eo: | three t [] > last three 1 + + [] + one + [aaa] + 1 > ooo diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 304a0aeddf..a5341d7faa 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -152,10 +152,11 @@ private void compile(final Path file) { for (final XML error : errors) { Logger.error( this, - "[%s:%s] %s", + "[%s:%s] %s (%s)", name, error.xpath("@line").get(0), - error.xpath("text()").get(0) + error.xpath("text()").get(0), + error.xpath("@check").get(0) ); } if (!errors.isEmpty()) { diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 26003cd296..1d1c4b6861 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -129,6 +129,10 @@ private void optimize(final Path home, final Path file) { this, "%s optimized to %s, all steps are in %s", file, target, dir ); + Logger.debug( + this, "Optimized XML saved to %s:\n%s", + target, baos.toString() + ); } catch (final IOException ex) { throw new IllegalStateException( new UncheckedText( diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java index 5adc6d35bc..d0fbbb2b26 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java @@ -23,6 +23,7 @@ */ package org.eolang.maven; +import com.jcabi.log.Logger; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import com.jcabi.xml.XSL; @@ -77,5 +78,6 @@ public void push(final int index, final XSL xsl, final XML xml) { ) ) ).value(); + Logger.debug(this, "Step #%d by %s:\n%s", index, file, xml); } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index 441a33eccb..b8886b96c4 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -26,7 +26,9 @@ import com.jcabi.log.Logger; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.cactoos.Input; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.ResourceOf; @@ -48,12 +50,37 @@ public final class CompileMojoTest { @Test public void testSimpleCompilation() throws Exception { + final String java = this.compile( + new ResourceOf("org/eolang/maven/mess.eo"), + "EOmess.java" + ); + MatcherAssert.assertThat(java, Matchers.containsString("class EOmess")); + } + + @Test + public void testRealCompilation() throws Exception { + final String java = this.compile( + new InputOf(Paths.get("eo-runtime/src/main/eo/org/eolang/array.eo")), + "EOarray.java" + ); + MatcherAssert.assertThat(java, Matchers.containsString("class")); + } + + /** + * Compile EO to Java. + * @param code EO sources + * @param file The file to return + * @return All Java code + * @throws Exception If fails + */ + private String compile(final Input code, + final String file) throws Exception { final Path temp = Files.createTempDirectory("eo"); final Path src = temp.resolve("src"); new LengthOf( new TeeInput( - new ResourceOf("org/eolang/maven/mess.eo"), - new OutputTo(src.resolve("mess.eo")) + code, + new OutputTo(src.resolve("code.eo")) ) ).value(); final Path target = temp.resolve("target"); @@ -70,17 +97,14 @@ public void testSimpleCompilation() throws Exception { .with("targetDir", target.toFile()) .with("generatedDir", generated.toFile()) .execute(); - final Path java = generated.resolve("EOmess.java"); + final Path java = generated.resolve(file); MatcherAssert.assertThat( Files.exists(java), Matchers.is(true) ); - final String code = new TextOf(new InputOf(java)).asString(); - Logger.info(this, "Java output:\n%s", code); - MatcherAssert.assertThat( - code, - Matchers.containsString("class EOmess") - ); + final String out = new TextOf(new InputOf(java)).asString(); + Logger.info(this, "Java output:\n%s", out); + return out; } } From 6a30d6ebb84efe05a2f6730750cf43976576472d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 11 Dec 2020 11:21:11 +0300 Subject: [PATCH 55/80] #164: more --- .../eolang/compiler/errors/broken-refs.xsl | 2 +- .../java/org/eolang/maven/CompileMojo.java | 19 ---------------- .../java/org/eolang/maven/OptimizeMojo.java | 22 +++++++++++++++++++ .../resources/org/eolang/maven/to-java.xsl | 9 ++++++++ 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl index 283775a209..3405b34fe1 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -29,7 +29,7 @@ SOFTWARE. - + broken-refs diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index a5341d7faa..3bd9ebb5cf 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -148,25 +148,6 @@ private void compile(final Path file) { ) ) ).value(); - final List errors = out.nodes("/program/errors/error"); - for (final XML error : errors) { - Logger.error( - this, - "[%s:%s] %s (%s)", - name, - error.xpath("@line").get(0), - error.xpath("text()").get(0), - error.xpath("@check").get(0) - ); - } - if (!errors.isEmpty()) { - throw new IllegalStateException( - String.format( - "There are %d errors in %s, see log above", - errors.size(), file - ) - ); - } for (final XML java : out.nodes("/program/objects/o[java]")) { CompileMojo.save( this.generatedDir.toPath().resolve( diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 1d1c4b6861..766a7ad393 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -24,12 +24,14 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -133,6 +135,26 @@ private void optimize(final Path home, final Path file) { this, "Optimized XML saved to %s:\n%s", target, baos.toString() ); + final List errors = new XMLDocument(baos.toString()) + .nodes("/program/errors/error"); + for (final XML error : errors) { + Logger.error( + this, + "[%s:%s] %s (%s)", + name, + error.xpath("@line").get(0), + error.xpath("text()").get(0), + error.xpath("@check").get(0) + ); + } + if (!errors.isEmpty()) { + throw new IllegalStateException( + String.format( + "There are %d errors in %s, see log above", + errors.size(), file + ) + ); + } } catch (final IOException ex) { throw new IllegalStateException( new UncheckedText( diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 68a96999b4..fe1bec916b 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -219,6 +219,15 @@ SOFTWARE. this. + + + Can't find what " + + : + + " is pointing to + + this. eo From ea63b7f7fcc104d4cdcd3654cb3a68bf50b9ff3b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Sat, 12 Dec 2020 00:33:45 +0300 Subject: [PATCH 56/80] #164: fixes --- .../eolang/compiler/abstracts-float-up.xsl | 23 +++------ .../org/eolang/compiler/add-refs.xsl | 17 +++++-- .../eolang/compiler/errors/broken-refs.xsl | 13 +++++ .../org/eolang/compiler/remove-levels.xsl | 47 +++++++++++++++++++ .../org/eolang/compiler/remove-refs.xsl | 35 ++++++++++++++ .../compiler/packs/float-abstracts.yaml | 13 +++-- .../java/org/eolang/maven/OptimizeMojo.java | 2 + 7 files changed, 127 insertions(+), 23 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl index f02a5d6576..e63e86044f 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl @@ -84,9 +84,8 @@ SOFTWARE. - - + @@ -103,20 +102,12 @@ SOFTWARE. - - + - - - - - . - - - + - + + + @@ -137,9 +128,9 @@ SOFTWARE. - - + - + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl index a207455673..2bde74f14c 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl @@ -36,11 +36,20 @@ SOFTWARE. - - - - + + + + + Attribute @line is absent at " + + " + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl index 3405b34fe1..c36ab87914 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -27,6 +27,19 @@ SOFTWARE. + + + + broken-refs + + + + + The ref at " + + " is empty + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl new file mode 100644 index 0000000000..4da312d39b --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl new file mode 100644 index 0000000000..a1f74d31b9 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl @@ -0,0 +1,35 @@ + + + + + + + + + + + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml index 91c4eadacb..8f5af66aef 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml @@ -1,11 +1,13 @@ xsls: - - add-refs.xsl + - remove-refs.xsl - abstracts-float-up.xsl + - remove-levels.xsl + - add-refs.xsl - errors/same-line-names.xsl - errors/duplicate-names.xsl - errors/broken-refs.xsl tests: - - /program/objects[count(o)=7] + - /program/objects[count(o)=9] - /program/errors[count(*)=0] - //objects[not(.//o[@name=''])] - //o[@name='first$2$native' and @atom='int'] @@ -14,7 +16,8 @@ tests: - //o[@name='first']/o[@base='test' and @as='foo'] - //o[@name='first$2$second'] - //o[@name='first$2$second$third'] - - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a' and @ref='1'] + - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a'] + - //o[@line='20.19' and @name='aa'] eo: | [a b] > first test:foo @@ -33,3 +36,7 @@ eo: | "hello, world!" > msg [] "Failure" + + [aa] > ooo + [bbb] > fff + aa.test > a diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 766a7ad393..e69850ab3f 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -112,7 +112,9 @@ private void optimize(final Path home, final Path file) { new Program(new XMLDocument(file), new OutputTo(baos)).compile( new Pack() .with("globals-to-abstracts.xsl") + .with("remove-refs.xsl") .with("abstracts-float-up.xsl") + .with("add-refs.xsl") .with("errors/broken-refs.xsl"), new TargetSpy(dir) ); From 240831c6ef290a24494889b945d8ce5a7e132aa1 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 14 Dec 2020 08:59:00 +0300 Subject: [PATCH 57/80] #164: simplified ANTLR --- .../src/main/antlr4/org/eolang/compiler/Program.g4 | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index 6f177927aa..b0bb1dc9b2 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -227,15 +227,11 @@ fragment BYTE: [0-9A-F][0-9A-F] MINUS; BYTES: BYTE (BYTE* [0-9A-F][0-9A-F])?; BOOL: 'true' | 'false'; -CHAR: '\'' (LETTER | DIGIT) '\''; +CHAR: '\'' [0-9a-zA-Z] '\''; STRING: '"' ('\\"' | ~'"')* '"'; -INT: (PLUS | MINUS)? DIGIT+; -FLOAT: (PLUS | MINUS)? DIGIT+ DOT DIGIT+; -HEX: '0x' (DIGIT | 'a'..'f')+; +INT: (PLUS | MINUS)? [0-9]+; +FLOAT: (PLUS | MINUS)? [0-9]+ DOT [0-9]+; +HEX: '0x' [0-9a-f]+; -NAME: LO (LETTER | DIGIT)*; +NAME: [a-z][a-z0-9_A-Z]*; -LETTER: (HI | LO); -HI: [A-Z]; -LO: [a-z]; -DIGIT: [0-9]; From 3c060a2a140fe9a0be8e0c3d0a955dd12f2c16f8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 15 Dec 2020 22:58:42 +0300 Subject: [PATCH 58/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 230 +++++++++--------- 1 file changed, 113 insertions(+), 117 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index fe1bec916b..7728c242e4 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -31,9 +31,18 @@ SOFTWARE. - - - + + + + + + + + + + + + @@ -83,15 +92,6 @@ SOFTWARE. { - - public static final - - - - = new - - (); - private Object @@ -111,28 +111,38 @@ SOFTWARE. } + + - - - - - } - - - + + public - _copy( - - + _copy() { + + + + return new + + (); + + + } + + + + + public void _init( + + , final Object @@ -140,44 +150,25 @@ SOFTWARE. ) { - - - final - - _ = new - - (); - - - + + - _. + this. = ; - - return _; - - } - + public - - - - - - Object - - + @@ -190,43 +181,59 @@ SOFTWARE. () { - - - return + - ; + + + return ret; } - + + - + + + + Can't find what " + + : + + " is pointing to + + + + + + Object + + + + + + + + = + new - . - + () this. - - - - Can't find what " - - : - - " is pointing to - + + ._copy() + this. @@ -235,65 +242,54 @@ SOFTWARE. () - - - - - - - - - - - - - - - () - - 1 - - - - - - - - - . - - - - - - - - - - - - ._copy( - - - - - , - + ; + // " + + " at line # + + + + + + _ + + + + + + + + + + + + + + ._init( + + + , - - - - - - - + + _ + + ); - - ) + + + + + + + + + ( + ) + new EOstring( " From 76bd57bd7ac25be3e82a8cebcd12a511d638b73d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 16 Dec 2020 00:18:31 +0300 Subject: [PATCH 59/80] #164: moe --- .../resources/org/eolang/maven/to-java.xsl | 86 ++++++++++++------- .../org/eolang/maven/CompileMojoTest.java | 2 +- eo-runtime/src/main/eo/org/eolang/array.eo | 9 +- 3 files changed, 62 insertions(+), 35 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 7728c242e4..f4e1bf6685 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -31,12 +31,12 @@ SOFTWARE. - + - + @@ -92,25 +92,8 @@ SOFTWARE. { - - - private Object - - ; - - - - private - - () { - - - - // Intentionally empty - - - } - + + @@ -122,6 +105,29 @@ SOFTWARE. + + + + public Object + + ; + + + + + + public + + (final Object p) { + + + + this._parent = p; + + + } + + public @@ -130,9 +136,24 @@ SOFTWARE. - return new - (); + _ = new + + (this._parent); + + + + + _. + + = this. + + ; + + + + + return _; } @@ -141,7 +162,7 @@ SOFTWARE. public void _init( - + , @@ -150,7 +171,7 @@ SOFTWARE. ) { - + this. @@ -200,7 +221,8 @@ SOFTWARE. - + + Can't find what " @@ -212,11 +234,11 @@ SOFTWARE. - - Object + + - + Object @@ -226,7 +248,7 @@ SOFTWARE. new - () + (this) this. @@ -248,7 +270,7 @@ SOFTWARE. " at line # - + _ @@ -267,7 +289,7 @@ SOFTWARE. ._init( - + , diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index b8886b96c4..ef902a27fa 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -61,7 +61,7 @@ public void testSimpleCompilation() throws Exception { public void testRealCompilation() throws Exception { final String java = this.compile( new InputOf(Paths.get("eo-runtime/src/main/eo/org/eolang/array.eo")), - "EOarray.java" + "EOarray$EOmap.java" ); MatcherAssert.assertThat(java, Matchers.containsString("class")); } diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo index bfdf0eb01e..fbd9743b9c 100644 --- a/eo-runtime/src/main/eo/org/eolang/array.eo +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -3,10 +3,15 @@ # Reduce from start "a" using the function "f" [a f] > reduce /a - # Map with index + # Map with index. Here "f" must be an abstract + # object with two free attributes. The first + # one for the element of the array, the second one + # for the index. [f] > mapi /array - # Map without index + # Map without index. Here "f" must be an abstract + # object with one free attribute, for the element + # of the array. [f] > map mapi > @ [x i] From 361304bc75b26bb0b5f1214979c9c813a4d33e89 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 16 Dec 2020 09:00:07 +0300 Subject: [PATCH 60/80] #164: eols and tabs --- .../java/org/eolang/maven/CompileMojo.java | 12 +- .../resources/org/eolang/maven/to-java.xsl | 157 ++++++++++-------- 2 files changed, 96 insertions(+), 73 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 3bd9ebb5cf..6fbe62fe72 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -30,10 +30,11 @@ import com.jcabi.xml.XSLDocument; import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.List; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -174,6 +175,15 @@ private void compile(final Path file) { Logger.info(this, "%s compiled to %s", file, this.generatedDir); } + void sf() { + try { + Method m = this.getClass().getMethod("ss"); + m.invoke(this); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException(e); + } + } + /** * Save one Java file. * @param path The path diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index f4e1bf6685..d15ef79181 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -25,12 +25,20 @@ SOFTWARE. - - - + + + + + + + + + + + @@ -66,6 +74,17 @@ SOFTWARE. + + + + + _origin + + + + + + @@ -78,27 +97,23 @@ SOFTWARE. - + import org.eolang.*; - - - public class + + + public final class - - extends - - { - + - + } @@ -107,60 +122,68 @@ SOFTWARE. - + public Object ; - + - + public (final Object p) { - - - + this._parent = p; - - + } - + + + + + public Object _call(final String m) { + + final Method method = this.getClass().getMethod(m); + + final Method method = this.getClass().getMethod(m); + + final Method method = this.getClass().getMethod(m); + + final Method method = this.getClass().getMethod(m); + + + } + - + public _copy() { - - - + _ = new (this._parent); - + - - + _. = this. ; - + - - + return _; - - + } - + - + public void _init( @@ -170,20 +193,19 @@ SOFTWARE. ) { - + - - + this. = ; - + - + } - + @@ -191,31 +213,23 @@ SOFTWARE. public - - - _origin - - - eo - - - + () { - + - + - + return ret; - + } - + @@ -259,8 +273,7 @@ SOFTWARE. this. - eo - + () @@ -269,7 +282,7 @@ SOFTWARE. " at line # - + @@ -280,13 +293,13 @@ SOFTWARE. - + - + ._init( @@ -298,7 +311,7 @@ SOFTWARE. ); - + @@ -344,31 +357,31 @@ SOFTWARE. package ; - - + + /* - + * This file was auto-generated by eolang-maven-plugin - + * on . Don't edit it, - + * your changes will be discarded on the next build. - + * The sources were compiled to XML on - + * by the EO compiler v. . - + */ - - + + From f68af9f706d371da5d1a9e1f9f76091167c7e2ea Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 16 Dec 2020 22:54:15 +0300 Subject: [PATCH 61/80] #164: more --- .../java/org/eolang/maven/CompileMojo.java | 11 -- .../resources/org/eolang/maven/to-java.xsl | 107 +++++++++--------- 2 files changed, 54 insertions(+), 64 deletions(-) diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 6fbe62fe72..7f6897e927 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -30,8 +30,6 @@ import com.jcabi.xml.XSLDocument; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -175,15 +173,6 @@ private void compile(final Path file) { Logger.info(this, "%s compiled to %s", file, this.generatedDir); } - void sf() { - try { - Method m = this.getClass().getMethod("ss"); - m.invoke(this); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - throw new IllegalStateException(e); - } - } - /** * Save one Java file. * @param path The path diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index d15ef79181..d149072403 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -74,7 +74,7 @@ SOFTWARE. - + @@ -107,30 +107,49 @@ SOFTWARE. { - - - - - + + + + + + } - - - - public Object - - ; - - + + + public Call<Object> + + ; + + + + + public Call<Object> + + = () -> { + + + + + + + + + return ret; + + }; + + private Object _parent; + public (final Object p) { @@ -141,19 +160,24 @@ SOFTWARE. - + public Object _call(final String m) { - final Method method = this.getClass().getMethod(m); - - final Method method = this.getClass().getMethod(m); - - final Method method = this.getClass().getMethod(m); - - final Method method = this.getClass().getMethod(m); + try { + + final Field field = + + .class.getField(m); + + return field.get(this); + } catch (final NoSuchFieldException ex) { + + return this._origin._call(m); } + + } @@ -190,16 +214,16 @@ SOFTWARE. , final Object - + ) { this. - - = - + + = () -> + ; @@ -207,30 +231,6 @@ SOFTWARE. } - - - - public - - - - () { - - - - - - - - - - - return ret; - - - } - - @@ -265,15 +265,16 @@ SOFTWARE. (this) - this. - + this._call(" + + ") ._copy() this. - + () From ec2bc8407633a28d93a5ac071a38e10920f07aae Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 19:59:49 +0300 Subject: [PATCH 62/80] #164: fix --- .../java/org/eolang/compiler/Program.java | 27 ++++++++++++------- .../java/org/eolang/compiler/XeListener.java | 1 + .../resources/org/eolang/compiler/_funcs.xsl | 4 --- .../eolang/compiler/add-default-package.xsl | 2 +- .../compiler/errors/not-empty-atoms.xsl | 2 +- .../org/eolang/compiler/wrap-method-calls.xsl | 6 ++--- .../java/org/eolang/compiler/PacksTest.java | 4 +-- .../org/eolang/compiler/packs/float-vars.yaml | 4 +-- .../eolang/compiler/packs/wraps-methods.yaml | 1 + .../java/org/eolang/maven/CompileMojo.java | 1 + .../resources/org/eolang/maven/to-java.xsl | 17 ++++++++---- 11 files changed, 42 insertions(+), 27 deletions(-) diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index 9fd3720c72..bfdaa261ce 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -27,7 +27,6 @@ import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import com.jcabi.xml.XSL; -import java.io.IOException; import java.nio.file.Path; import org.cactoos.Output; import org.cactoos.io.InputOf; @@ -77,9 +76,8 @@ public Program(final XML dom, final Output tgt) { /** * Compile it to XMLs and save (with default set of XSLs). - * @throws IOException If fails */ - public void compile() throws IOException { + public void compile() { this.compile(new Program.Spy.None()); } @@ -87,10 +85,9 @@ public void compile() throws IOException { * Compile it to XML and save (with default set of XSLs). * * @param spy The spy - * @throws IOException If fails */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void compile(final Program.Spy spy) throws IOException { + public void compile(final Program.Spy spy) { this.compile(new Pack(), spy); } @@ -98,9 +95,8 @@ public void compile(final Program.Spy spy) throws IOException { * Compile it to XML and save. * * @param xsls List of XSLs to apply - * @throws IOException If fails */ - public void compile(final Iterable xsls) throws IOException { + public void compile(final Iterable xsls) { this.compile(xsls, new Program.Spy.None()); } @@ -149,13 +145,26 @@ public interface Spy { * @since 0.1 */ final class None implements Program.Spy { + @Override + public void push(final int index, final XSL xsl, final XML xml) { + // Nothing + } + } + + /** + * Empty spy. + * + * @since 0.1 + */ + final class Verbose implements Program.Spy { @Override public void push(final int index, final XSL xsl, final XML xml) { Logger.debug( this, - "Parsed #%d via %s", + "Parsed #%d via %s\n%s", index, - new XMLDocument(xsl.toString()).xpath("/*/@id").get(0) + new XMLDocument(xsl.toString()).xpath("/*/@id").get(0), + xml ); } } diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 7220f718b8..d617cbdf4a 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -218,6 +218,7 @@ public void exitSuffix(final ProgramParser.SuffixContext ctx) { @Override public void enterMethod(final ProgramParser.MethodContext ctx) { this.dirs.add("o") + .attr("method", "") .attr("line", ctx.getStart().getLine()) .attr("base", ctx.getText()).up(); } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl index b2efe22696..bda02203f3 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl @@ -27,8 +27,4 @@ SOFTWARE. - - - - diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl index 4dfd09dfb5..032cf89df5 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl @@ -22,7 +22,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + - + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl index 4a2840fc8f..f97124aec8 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl @@ -40,19 +40,19 @@ SOFTWARE. --> - + - + - + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index ab1fb201b3..cf8f78dc3e 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -67,9 +67,9 @@ public void testPacks(final String pack) throws Exception { final Program program = new Program(xml, new OutputTo(baos)); final Collection xsls = (Collection) map.get("xsls"); if (xsls == null) { - program.compile(); + program.compile(new Program.Spy.Verbose()); } else { - program.compile(new Pack(xsls)); + program.compile(new Pack(xsls), new Program.Spy.Verbose()); } Logger.debug(this, "Output XML:\n%s", baos.toString()); for (final String xpath : (Iterable) map.get("tests")) { diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml index 19c38e3506..4db9b82989 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -28,7 +28,7 @@ eo: | [] > last three 1 - [] + [ppp] one - [aaa] + [aaa] > kkk 1 > ooo diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 80af7868c8..a5073a4d47 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -3,6 +3,7 @@ xsls: tests: - /program/errors[count(*)=0] - /program/objects[count(o)=1] + - //objects[not(o[@method])] - //o[@base='sprintf' and count(./o)=3] - //o[@base='.foo' and count(./o)=1] - //o[@base='.trim' and count(./o)=2] diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 7f6897e927..340e62a8aa 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -137,6 +137,7 @@ private void compile(final Path file) { new ResourceOf("org/eolang/maven/to-java.xsl") ).asString() ).with(new ClasspathSources()).transform(xml); + Logger.debug(this, "Raw Java output of %s:\n%s", file, out); new IoChecked<>( new LengthOf( new TeeInput( diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index d149072403..81f594ac65 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -132,7 +132,7 @@ SOFTWARE. public Call<Object> - = () -> { + = () -> { @@ -169,11 +169,18 @@ SOFTWARE. .class.getField(m); - return field.get(this); + return ((Call<Object>) field.get(this)).call(); } catch (final NoSuchFieldException ex) { - return this._origin._call(m); + + + return this._origin._call(m); + + + throw new IllegalStateException("no attr"); + + } @@ -222,7 +229,7 @@ SOFTWARE. this. - = () -> + = () -> ; @@ -298,7 +305,7 @@ SOFTWARE. - + From 92228a20637a51d308f0aa15cfd226780e742fff Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 20:34:05 +0300 Subject: [PATCH 63/80] #164: fixes --- eo-compiler/src/main/java/org/eolang/compiler/XeListener.java | 1 + .../src/test/java/org/eolang/maven/CompileMojoTest.java | 3 +-- .../src/test/java/org/eolang/maven/OptimizeMojoTest.java | 2 +- eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) create mode 120000 eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index d617cbdf4a..511b310f0e 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -96,6 +96,7 @@ public void enterProgram(final ProgramParser.ProgramContext ctx) { @Override public void exitProgram(final ProgramParser.ProgramContext ctx) { this.dirs + // @checkstyle MagicNumber (1 line) .attr("ms", (System.nanoTime() - this.start) / (1000L * 1000L)) .up(); } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index ef902a27fa..4e2fc76a36 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -26,7 +26,6 @@ import com.jcabi.log.Logger; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.cactoos.Input; import org.cactoos.io.InputOf; @@ -60,7 +59,7 @@ public void testSimpleCompilation() throws Exception { @Test public void testRealCompilation() throws Exception { final String java = this.compile( - new InputOf(Paths.get("eo-runtime/src/main/eo/org/eolang/array.eo")), + new ResourceOf("org/eolang/maven/array.eo"), "EOarray$EOmap.java" ); MatcherAssert.assertThat(java, Matchers.containsString("class")); diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java index 48969a1fa0..a61f2b69fe 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java @@ -49,7 +49,7 @@ public void testSimpleOptimize() throws Exception { new LengthOf( new TeeInput( new InputOf( - "[args] > main\n (stdout \"Hello!\").print\n" + "[args] > main\n (stdout \"Hello!\").print > @\n" ), new OutputTo(src.resolve("main.eo")) ) diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo new file mode 120000 index 0000000000..bcf97e5312 --- /dev/null +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo @@ -0,0 +1 @@ +../../../../../../../eo-runtime/src/main/eo/org/eolang/array.eo \ No newline at end of file From d951ff782338bd4d65d01e88ad9bcac163aad2a5 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 20:46:27 +0300 Subject: [PATCH 64/80] #164: fix --- .../resources/org/eolang/compiler/errors/unknown-names.xsl | 2 +- .../org/eolang/compiler/packs/catches-unknown-names.yaml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl index 06f8da982e..6c8c107205 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl @@ -27,7 +27,7 @@ SOFTWARE. - + unknown-names diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml index 6aa3fad816..ae9646590c 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml @@ -5,5 +5,6 @@ tests: - /program/errors/error[@line='2'] - //o[@base='test' and @line='2'] eo: | - [] - test + [] > fff + test > @ + $ > t From 4c7db00c770a54abe78f2efa664312851ce800d7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 21:05:03 +0300 Subject: [PATCH 65/80] #164: each --- .../java/org/eolang/compiler/Program.java | 9 ++++- .../resources/org/eolang/compiler/_each.xsl | 40 +++++++++++++++++++ .../java/org/eolang/maven/OptimizeMojo.java | 5 ++- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index bfdaa261ce..673ba9e6ba 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -24,9 +24,11 @@ package org.eolang.compiler; import com.jcabi.log.Logger; +import com.jcabi.xml.ClasspathSources; import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; import com.jcabi.xml.XSL; +import com.jcabi.xml.XSLDocument; import java.nio.file.Path; import org.cactoos.Output; import org.cactoos.io.InputOf; @@ -107,10 +109,15 @@ public void compile(final Iterable xsls) { * @param spy The spy */ public void compile(final Iterable xsls, final Program.Spy spy) { + final XSL each = new XSLDocument( + Program.class.getResourceAsStream("/org/eolang/compiler/_each.xsl") + ).with(new ClasspathSources()); int index = 0; XML before = this.source; for (final XSL xsl : xsls) { - final XML after = xsl.transform(before); + final XML after = each.with("step", index).transform( + xsl.transform(before) + ); spy.push(index, xsl, after); ++index; before = after; diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl new file mode 100644 index 0000000000..2217a784fd --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index e69850ab3f..96a92c5333 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -142,11 +142,12 @@ private void optimize(final Path home, final Path file) { for (final XML error : errors) { Logger.error( this, - "[%s:%s] %s (%s)", + "[%s:%s] %s (%s:%s)", name, error.xpath("@line").get(0), error.xpath("text()").get(0), - error.xpath("@check").get(0) + error.xpath("@check").get(0), + error.xpath("@step").get(0) ); } if (!errors.isEmpty()) { From 15ba379814758488b84373c7cecbb6fff6c8fc48 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 21:10:19 +0300 Subject: [PATCH 66/80] #164: show ref --- .../main/resources/org/eolang/compiler/errors/broken-refs.xsl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl index c36ab87914..f47ae6f65b 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -52,7 +52,9 @@ SOFTWARE. The object " - " is absent, but is referenced + " is absent, but is referenced as " + + " From 3902b6dca8b7f6a55b626566d5fe679b449ef22b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 21:34:01 +0300 Subject: [PATCH 67/80] #164: fix --- .../resources/org/eolang/compiler/add-refs.xsl | 7 +++++++ .../org/eolang/compiler/errors/broken-refs.xsl | 15 +++++++++++++++ .../org/eolang/compiler/packs/adds-refs.yaml | 1 + .../main/java/org/eolang/maven/OptimizeMojo.java | 1 + .../main/resources/org/eolang/maven/to-java.xsl | 6 +++--- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl index 2bde74f14c..74090fcb7f 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl @@ -46,6 +46,13 @@ SOFTWARE. " + + + Duplicate names inside " + + " + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl index f47ae6f65b..f3c6d99efd 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -27,6 +27,21 @@ SOFTWARE. + + + + broken-refs + + + + + The ref at " + + " is wrongly formatted: " + + " + + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml index af50f03039..334216737e 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml @@ -3,6 +3,7 @@ xsls: - errors/self-naming.xsl - errors/same-line-names.xsl - add-refs.xsl + - errors/broken-refs.xsl tests: - /program/errors[count(*)=0] - /program/objects[count(o)=3] diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java index 96a92c5333..ea0d1ccb87 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -114,6 +114,7 @@ private void optimize(final Path home, final Path file) { .with("globals-to-abstracts.xsl") .with("remove-refs.xsl") .with("abstracts-float-up.xsl") + .with("remove-levels.xsl") .with("add-refs.xsl") .with("errors/broken-refs.xsl"), new TargetSpy(dir) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 81f594ac65..83841dc91f 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -194,13 +194,13 @@ SOFTWARE. _copy() { - _ = new + _c = new (this._parent); - _. + _c. = this. @@ -208,7 +208,7 @@ SOFTWARE. - return _; + return _c; } From 9bff30ace35992f0d4fd160d5a535cdb323b3090 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 22:17:50 +0300 Subject: [PATCH 68/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 44 ++++++++++------- eo-runtime/src/main/java/org/eolang/Attr.java | 41 ++++++++++++++++ eo-runtime/src/main/java/org/eolang/Phi.java | 49 +++++++++++++++++++ 3 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/Attr.java create mode 100644 eo-runtime/src/main/java/org/eolang/Phi.java diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 83841dc91f..753ddd8f96 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -72,7 +72,7 @@ SOFTWARE. - + @@ -105,7 +105,7 @@ SOFTWARE. public final class - { + implements Phi { @@ -123,14 +123,14 @@ SOFTWARE. - public Call<Object> + public Attr ; - public Call<Object> + public Attr = () -> { @@ -148,11 +148,11 @@ SOFTWARE. - private Object _parent; + private Attr _parent; public - (final Object p) { + (final Attr p) { this._parent = p; @@ -161,24 +161,32 @@ SOFTWARE. - public Object _call(final String m) { + public Phi _attr(final String a) { try { - final Field field = + final java.lang.reflect.Field field = - .class.getField(m); + .class.getField(a); - return ((Call<Object>) field.get(this)).call(); + return Phi.class.cast(field.get(this)); - } catch (final NoSuchFieldException ex) { + } catch (final NoSuchFieldException | IllegalAccessException ex) { - return this._origin._call(m); + return this._origin.get()._attr(a); - throw new IllegalStateException("no attr"); + throw new IllegalStateException( + + String.format( + + "No attribute \"%s\"", a + + ) + + ) @@ -201,9 +209,9 @@ SOFTWARE. _c. - + = this. - + ; @@ -220,7 +228,7 @@ SOFTWARE. , - final Object + final Phi ) { @@ -259,7 +267,7 @@ SOFTWARE. - Object + Phi @@ -272,7 +280,7 @@ SOFTWARE. (this) - this._call(" + this._attr(" ") diff --git a/eo-runtime/src/main/java/org/eolang/Attr.java b/eo-runtime/src/main/java/org/eolang/Attr.java new file mode 100644 index 0000000000..4ae514b5db --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/Attr.java @@ -0,0 +1,41 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +/** + * Attribute. + * + * @since 0.1 + */ +public interface Attr { + + /** + * Take the object out. + * + * @return The object + */ + Phi get(); + +} diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java new file mode 100644 index 0000000000..556df74864 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -0,0 +1,49 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +/** + * Callback. + * + * @since 0.1 + */ +public interface Phi { + + /** + * Get the attribute. + * + * @param name The name of the attribute to call + * @return The object + */ + Phi _attr(String name); + + /** + * Make a copy. + * + * @return The object + */ + Phi _copy(); + +} From 422113145a2581133bf8a6ac16a1a84905e28403 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 22:24:33 +0300 Subject: [PATCH 69/80] #164: fixes --- .../src/main/resources/org/eolang/compiler/remove-levels.xsl | 2 +- eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl index 4da312d39b..5b82df498d 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl @@ -41,7 +41,7 @@ SOFTWARE. - + diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 753ddd8f96..6d737e1476 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -186,7 +186,7 @@ SOFTWARE. ) - ) + ); From ca9b1e96e3b6fab2728069badd6de1e54e57d56f Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 18 Dec 2020 23:50:57 +0300 Subject: [PATCH 70/80] #164: types finding --- .../resources/org/eolang/maven/to-java.xsl | 67 ++++++++++++++----- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 6d737e1476..855cd0ab7c 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -46,6 +46,9 @@ SOFTWARE. + + + @@ -152,6 +155,14 @@ SOFTWARE. public + (final Phi p) { + + this(() -> p); + + } + + public + (final Attr p) { this._parent = p; @@ -246,13 +257,13 @@ SOFTWARE. } - + - + Can't find what " @@ -263,7 +274,7 @@ SOFTWARE. - + @@ -274,6 +285,9 @@ SOFTWARE. = + + this + new @@ -299,7 +313,39 @@ SOFTWARE. " at line # - + + + + + + + + + + + + _base + + + + + + base = + + _base._attr(" + + "); + + + + + + + + + + + _ @@ -313,7 +359,7 @@ SOFTWARE. - + @@ -330,17 +376,6 @@ SOFTWARE. - - - - - - - - - ( - ) - new EOstring( " From 0b6eaaaa4722ede6328d519890f772f35cfd56f5 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Sat, 19 Dec 2020 00:04:04 +0300 Subject: [PATCH 71/80] #164: fix --- .../src/main/resources/org/eolang/maven/to-java.xsl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 855cd0ab7c..17d38c8cab 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -273,6 +273,7 @@ SOFTWARE. + final @@ -285,7 +286,7 @@ SOFTWARE. = - + this @@ -335,6 +336,7 @@ SOFTWARE. _base._attr(" "); + From 4051e70947029bcb91780610610cd572a20dd2f9 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Sat, 19 Dec 2020 00:16:04 +0300 Subject: [PATCH 72/80] #164: more --- .../src/main/resources/org/eolang/maven/to-java.xsl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 17d38c8cab..0664247820 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -331,10 +331,13 @@ SOFTWARE. - base = + + final Phi + + = _base._attr(" - + "); @@ -366,7 +369,7 @@ SOFTWARE. ._init( - + , From 3ade8417c21b40a9e335270a2465a2d85b8955ee Mon Sep 17 00:00:00 2001 From: yegor256 Date: Sat, 19 Dec 2020 00:37:16 +0300 Subject: [PATCH 73/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 77 +++++++++++-------- eo-runtime/src/main/java/org/eolang/Phi.java | 16 ++++ 2 files changed, 60 insertions(+), 33 deletions(-) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 0664247820..79021421a3 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -119,7 +119,7 @@ SOFTWARE. - + } @@ -170,7 +170,9 @@ SOFTWARE. } - + + + @Override public Phi _attr(final String a) { @@ -186,7 +188,7 @@ SOFTWARE. - return this._origin.get()._attr(a); + return this._origin.get()._attr(attr); throw new IllegalStateException( @@ -234,26 +236,29 @@ SOFTWARE. - public void _init( - - - , - - final Phi - - - ) { - - - - this. - - = () -> - - ; - - - + @Override + + public void _init(final String attr, final Phi phi) { + + try { + + + .class.getField(a).set(this, phi); + + } catch (final NoSuchFieldException | IllegalAccessException ex) { + + throw new IllegalStateException( + + String.format( + + "No attribute \"%s\"", a + + ) + + ); + + } + } @@ -364,22 +369,28 @@ SOFTWARE. - + ._init( - - - , - - - _ - - + + + " + + " + + + + + + , + + _ + ); - + new EOstring( diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index 556df74864..7568930809 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -31,6 +31,22 @@ */ public interface Phi { + /** + * Set the attribute. + * + * @param name The name of the attribute to call + * @param phi The object + */ + void _init(String name, Phi phi); + + /** + * Set the attribute. + * + * @param index Index of it + * @param phi The object + */ + void _init(int index, Phi phi); + /** * Get the attribute. * From f97fc8d655d8432e72adce76907bde0c89bb4ffa Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 16:44:28 +0300 Subject: [PATCH 74/80] #164: question --- .../antlr4/org/eolang/compiler/Program.g4 | 2 + .../java/org/eolang/compiler/XeListener.java | 3 + .../eolang/compiler/packs/full-syntax.yaml | 4 +- .../resources/org/eolang/maven/to-java.xsl | 203 ++++-------------- .../src/test/resources/log4j.properties | 1 + eo-runtime/src/main/eo/org/eolang/array.eo | 2 + eo-runtime/src/main/eo/org/eolang/bool.eo | 5 +- eo-runtime/src/main/eo/org/eolang/int.eo | 2 + .../src/main/eo/org/eolang/io/stdout.eo | 2 + eo-runtime/src/main/eo/org/eolang/string.eo | 2 + eo-runtime/src/main/java/org/eolang/Data.java | 21 +- .../src/main/java/org/eolang/EObool.java | 129 ----------- .../src/main/java/org/eolang/EOfloat.java | 62 ------ .../src/main/java/org/eolang/EOint.java | 150 ------------- .../src/main/java/org/eolang/EOstring.java | 62 ------ .../src/main/java/org/eolang/Operator.java | 106 --------- eo-runtime/src/main/java/org/eolang/Phi.java | 101 +++++++-- .../org/eolang/TypeMismatchException.java | 47 ---- .../EOstdout$EOorigin.java} | 29 +-- .../src/main/java/org/eolang/io/EOstdout.java | 60 ------ .../main/java/org/eolang/txt/EOsprintf.java | 2 - ...utTest.java => EOstdout$EOoriginTest.java} | 7 +- .../java/org/eolang/txt/EOsprintfTest.java | 2 - 23 files changed, 180 insertions(+), 824 deletions(-) delete mode 100644 eo-runtime/src/main/java/org/eolang/EObool.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOfloat.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOint.java delete mode 100644 eo-runtime/src/main/java/org/eolang/EOstring.java delete mode 100644 eo-runtime/src/main/java/org/eolang/Operator.java delete mode 100644 eo-runtime/src/main/java/org/eolang/TypeMismatchException.java rename eo-runtime/src/main/java/org/eolang/{ArgsException.java => io/EOstdout$EOorigin.java} (74%) delete mode 100644 eo-runtime/src/main/java/org/eolang/io/EOstdout.java rename eo-runtime/src/test/java/org/eolang/io/{EOstdoutTest.java => EOstdout$EOoriginTest.java} (93%) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index b0bb1dc9b2..185a7e6d06 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -91,6 +91,7 @@ suffix ARROW SPACE name=( NAME | AT ) + QUESTION? CONST? ; @@ -189,6 +190,7 @@ META: PLUS NAME (SPACE ~[\r\n]+)?; DOTS: '...'; CONST: '!'; +QUESTION: '?'; SLASH: '/'; COLON: ':'; ARROW: '>'; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 511b310f0e..8adb505d32 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -209,6 +209,9 @@ public void enterSuffix(final ProgramParser.SuffixContext ctx) { if (ctx.CONST() != null) { this.dirs.attr("const", ""); } + if (ctx.QUESTION() != null) { + this.dirs.attr("rw", ""); + } } @Override diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 03313a6c95..61b428590b 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -33,9 +33,9 @@ eo: | # This is very good object [x] > first - second > hello + second > hello? $.add 5 > i - third:foo > x + third:foo > x?! f 12 false (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! # This object is also very good diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 79021421a3..9dd71fe698 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -75,13 +75,13 @@ SOFTWARE. - + - _origin + origin @@ -108,158 +108,55 @@ SOFTWARE. public final class - implements Phi { + extends Phi { - - - - - - - - - } - - - public Attr - - ; - - - - - public Attr - - = () -> { - - - - - - - - - return ret; - - }; - - - private Attr _parent; - - public - - (final Phi p) { - - this(() -> p); - - } - public - (final Attr p) { + () { - this._parent = p; - - } + super( + + + , + + " + + " + + ); - - + + + + + - @Override - - public Phi _attr(final String a) { - - try { - - final java.lang.reflect.Field field = - - .class.getField(a); - - return Phi.class.cast(field.get(this)); - - } catch (final NoSuchFieldException | IllegalAccessException ex) { - - - - return this._origin.get()._attr(attr); - - - throw new IllegalStateException( - - String.format( - - "No attribute \"%s\"", a - - ) - - ); - - - - } - - } + }; - - - public - - _copy() { - - - _c = new - - (this._parent); - - - - _c. - - = this. - - ; - - + - return _c; - - } + this.put(" + + ", () -> { - - - - @Override - - public void _init(final String attr, final Phi phi) { - - try { - - - .class.getField(a).set(this, phi); - - } catch (final NoSuchFieldException | IllegalAccessException ex) { - - throw new IllegalStateException( - - String.format( - - "No attribute \"%s\"", a - - ) - - ); + + + + + + + + return ret; - } - - } + }); @@ -278,16 +175,7 @@ SOFTWARE. - final - - - - - - Phi - - - + final Phi = @@ -297,20 +185,15 @@ SOFTWARE. new - (this) + () - - this._attr(" + + this.get(" ") - ._copy() + .copy() - - - this. - - () ; @@ -341,9 +224,9 @@ SOFTWARE. = - _base._attr(" - - "); + _base.get(" + + ").copy(); @@ -373,7 +256,7 @@ SOFTWARE. - ._init( + .put( " @@ -384,7 +267,7 @@ SOFTWARE. - , + , () -> _ diff --git a/eo-maven-plugin/src/test/resources/log4j.properties b/eo-maven-plugin/src/test/resources/log4j.properties index 256fae55c2..56991bfea6 100644 --- a/eo-maven-plugin/src/test/resources/log4j.properties +++ b/eo-maven-plugin/src/test/resources/log4j.properties @@ -6,3 +6,4 @@ log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n log4j.logger.org.eolang=DEBUG log4j.logger.org.eolang.compiler.Program=INFO +log4j.logger.org.eolang.maven.TargetSpy=INFO diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo index fbd9743b9c..0f5ee04e8f 100644 --- a/eo-runtime/src/main/eo/org/eolang/array.eo +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -1,3 +1,5 @@ ++package org.eolang + [b] > array # Reduce from start "a" using the function "f" diff --git a/eo-runtime/src/main/eo/org/eolang/bool.eo b/eo-runtime/src/main/eo/org/eolang/bool.eo index 5abdf472de..5aa84a377d 100644 --- a/eo-runtime/src/main/eo/org/eolang/bool.eo +++ b/eo-runtime/src/main/eo/org/eolang/bool.eo @@ -1,2 +1,5 @@ -[b] > bool ++package org.eolang + +[] > bool + true > b? [t f] > if /t diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index 0acbe25e18..4caabf9f0b 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -1,3 +1,5 @@ ++package org.eolang + [b] > int # Change the sign of the number [] > neg /int diff --git a/eo-runtime/src/main/eo/org/eolang/io/stdout.eo b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo index 12671e7027..d9842f7dc4 100644 --- a/eo-runtime/src/main/eo/org/eolang/io/stdout.eo +++ b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo @@ -1,2 +1,4 @@ ++package org.eolang.io + [text] > stdout [] > @ /bool diff --git a/eo-runtime/src/main/eo/org/eolang/string.eo b/eo-runtime/src/main/eo/org/eolang/string.eo index 1b7f155d95..b167058167 100644 --- a/eo-runtime/src/main/eo/org/eolang/string.eo +++ b/eo-runtime/src/main/eo/org/eolang/string.eo @@ -1,2 +1,4 @@ ++package org.eolang + [b] > string [] > trim /string diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 7bdedf1de6..9fac1cf80e 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -25,18 +25,23 @@ package org.eolang; /** - * Data primitive. + * A data container. * - * @param Type of return * @since 0.1 */ public interface Data { - /** - * Take the take out. - * - * @return The take - */ - T get(); + T take(); + + public class Take { + private final Phi phi; + public Take(final Phi src) { + this.phi = src; + } + @SuppressWarnings("unchecked") + public T take(final Class type) { + return ((Data) type.cast(this.phi)).take(); + } + } } diff --git a/eo-runtime/src/main/java/org/eolang/EObool.java b/eo-runtime/src/main/java/org/eolang/EObool.java deleted file mode 100644 index f12229ddb4..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EObool.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import org.cactoos.Scalar; -import org.cactoos.scalar.Unchecked; - -/** - * BOOLEAN. - * - * @since 0.1 - */ -public class EObool implements Data { - - /** - * The value. - */ - private final Unchecked value; - - /** - * Ctor. - * @param val The value - */ - public EObool(final boolean val) { - this(() -> val); - } - - /** - * Ctor. - * @param val The value - */ - public EObool(final Scalar val) { - this.value = new Unchecked<>(val); - } - - @Override - public final Boolean get() { - return this.value.value(); - } - - /** - * Makes a fork. - * - * new EObool(true).if().copy("left", "right") - * - * @param Type of result - * @return Result - * @checkstyle NonStaticMethodCheck (3 lines) - */ - public final EObool.OpIf eoif() { - return new EObool.OpIf<>(this.value); - } - - /** - * Operator IF. - * - * @param The type of return - * @since 0.1 - */ - @SuppressWarnings("unchecked") - public final class OpIf extends EObool { - /** - * Ctor. - * @param val The value - */ - public OpIf(final Scalar val) { - super(val); - } - /** - * Make a copy. - * - * @param left Left branch (positive) - * @param right Right branch (negative) - * @return Data with result - * @checkstyle MethodNameCheck (3 lines) - */ - public Data copy(final Data left, final Data right) { - try { - final Constructor> ctor = - (Constructor>) left.getClass() - .getDeclaredConstructor(Scalar.class); - return ctor.newInstance( - (Scalar) () -> { - final T result; - if (this.get()) { - result = (T) left.get(); - } else { - result = (T) right.get(); - } - return result; - } - ); - } catch (final NoSuchMethodException | IllegalAccessException - | InstantiationException | InvocationTargetException ex) { - throw new IllegalStateException( - String.format( - "Can't make a copy of %s", - left.getClass() - ), - ex - ); - } - } - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOfloat.java b/eo-runtime/src/main/java/org/eolang/EOfloat.java deleted file mode 100644 index e3d7955301..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOfloat.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.cactoos.Scalar; -import org.cactoos.scalar.Unchecked; - -/** - * FLOAT. - * - * @since 0.1 - */ -public class EOfloat implements Data { - - /** - * The value. - */ - private final Unchecked value; - - /** - * Ctor. - * @param val The value - */ - public EOfloat(final double val) { - this(() -> val); - } - - /** - * Ctor. - * @param val The value - */ - public EOfloat(final Scalar val) { - this.value = new Unchecked<>(val); - } - - @Override - public final Double get() { - return this.value.value(); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOint.java b/eo-runtime/src/main/java/org/eolang/EOint.java deleted file mode 100644 index 313027d930..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOint.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.cactoos.Scalar; -import org.cactoos.scalar.Unchecked; - -/** - * INTEGER. - * - * @since 0.1 - */ -public final class EOint implements Data { - - /** - * The value. - */ - private final Unchecked value; - - /** - * Ctor. - * @param val The value - */ - public EOint(final long val) { - this(() -> val); - } - - /** - * Ctor. - * @param val The value - */ - public EOint(final Scalar val) { - this.value = new Unchecked<>(val); - } - - @Override - public Long get() { - return this.value.value(); - } - - /** - * Add a number. - * - * new EOinteger(5).add().copy(5, 7, 7) - * - * @return New number - */ - public Operator eoadd() { - return new Operator<>( - this, - EOint.class, - (start, args) -> { - long sum = start; - for (final Data arg : args) { - sum += arg.get(); - } - return sum; - } - ); - } - - /** - * Subtract a number. - * @return New number - */ - public Operator eosub() { - return new Operator<>( - this, - EOint.class, - (start, args) -> start - args.get(0).get() - ); - } - - /** - * Multiply by the number. - * @return New number - */ - public Operator eomul() { - return new Operator<>( - this, - EOint.class, - (start, args) -> { - long sum = start; - for (final Data arg : args) { - sum *= arg.get(); - } - return sum; - } - ); - } - - /** - * Divide by the number. - * @return New number - */ - public Operator eodiv() { - return new Operator<>( - this, - EOint.class, - (start, args) -> start - args.get(0).get() - ); - } - - /** - * Inverse the sign. - * @return New number - */ - public Operator eoneg() { - return new Operator<>( - this, - EOint.class, - (start, args) -> -start - ); - } - - /** - * MOD operation. - * @return New number - */ - public Operator eomod() { - return new Operator<>( - this, - EOint.class, - (start, args) -> -start - ); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/EOstring.java b/eo-runtime/src/main/java/org/eolang/EOstring.java deleted file mode 100644 index 429a98500b..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOstring.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import org.cactoos.Scalar; -import org.cactoos.scalar.Unchecked; - -/** - * STRING. - * - * @since 0.1 - */ -public class EOstring implements Data { - - /** - * The value. - */ - private final Unchecked value; - - /** - * Ctor. - * @param val The value - */ - public EOstring(final String val) { - this(() -> val); - } - - /** - * Ctor. - * @param val The value - */ - public EOstring(final Scalar val) { - this.value = new Unchecked<>(val); - } - - @Override - public final String get() { - return this.value.value(); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/Operator.java b/eo-runtime/src/main/java/org/eolang/Operator.java deleted file mode 100644 index 0faf7a54f0..0000000000 --- a/eo-runtime/src/main/java/org/eolang/Operator.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -import java.lang.reflect.InvocationTargetException; -import java.util.List; -import org.cactoos.Scalar; -import org.cactoos.list.ListOf; - -/** - * One operation. - * - * @param Type of result - * @param Type of data - * @since 0.1 - */ -public final class Operator> { - - /** - * The start. - */ - private final Data start; - - /** - * The start. - */ - private final Class type; - - /** - * The reduce. - */ - private final Operator.Reduce reduce; - - /** - * Ctor. - * @param val The value - * @param tpe Type of result - * @param rdc Reduce - */ - public Operator(final Data val, final Class tpe, - final Operator.Reduce rdc) { - this.start = val; - this.type = tpe; - this.reduce = rdc; - } - - /** - * Make a copy of it. - * - * @param args The args - * @return Copy - */ - @SafeVarargs - public final T copy(final Data... args) { - try { - return this.type.getConstructor(Scalar.class).newInstance( - (Scalar) () -> this.reduce.calc( - this.start.get(), - new ListOf<>(args) - ) - ); - } catch (final InstantiationException | IllegalAccessException - | InvocationTargetException | NoSuchMethodException ex) { - throw new IllegalStateException(ex); - } - } - - /** - * Reduce action. - * - * @param Type of result - * @since 0.1 - */ - public interface Reduce { - /** - * Do it. - * - * @param first First value - * @param args The args - * @return Result - */ - D calc(D first, List> args); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index 7568930809..2c15708569 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -24,42 +24,113 @@ package org.eolang; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** - * Callback. + * A simple object. * * @since 0.1 */ -public interface Phi { +public class Phi { /** - * Set the attribute. + * Bound attributes. + */ + private final Map bound; + + /** + * Names of free attributes. + */ + private final List free; + + /** + * Ctor. + */ + public Phi(final String... names) { + this(new HashMap<>(0), new ArrayList<>(Arrays.asList(names))); + } + + /** + * Ctor. + */ + private Phi(final Map bnd, final List names) { + this.bound = new HashMap<>(bnd); + this.free = names; + } + + /** + * Make a copy. * - * @param name The name of the attribute to call - * @param phi The object + * @return A copy */ - void _init(String name, Phi phi); + public final Phi copy() { + return new Phi(this.bound, this.free); + } /** * Set the attribute. * - * @param index Index of it - * @param phi The object + * @param name The name of the attribute to set + * @param phi The value to set */ - void _init(int index, Phi phi); + public final void put(final String name, final Attr phi) { + if (!this.free.contains(name)) { + throw new IllegalStateException( + String.format( + "Attribute \"%s\" is not free here", name + ) + ); + } + if (this.bound.containsKey(name)) { + throw new IllegalStateException( + String.format( + "Attribute \"%s\" is already bound", name + ) + ); + } + this.bound.put(name, phi); + } /** - * Get the attribute. + * Set the attribute by position. * - * @param name The name of the attribute to call - * @return The object + * @param pos The position of it + * @param phi The value to set */ - Phi _attr(String name); + public final void put(final int pos, final Attr phi) { + this.bound.put(this.free.get(pos), phi); + } /** - * Make a copy. + * Get the attribute. * + * @param name The name of the attribute to call * @return The object */ - Phi _copy(); + public final Phi get(final String name) { + Phi attr = this.bound.get(name).get(); + if (attr == null) { + final Attr origin = this.bound.get("_origin"); + if (origin == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" attribute", name + ) + ); + } + attr = origin.get().get(name); + } + return attr; + } + + protected T data(final Class type) { + throw new UnsupportedOperationException( + "No data here" + ); + } } diff --git a/eo-runtime/src/main/java/org/eolang/TypeMismatchException.java b/eo-runtime/src/main/java/org/eolang/TypeMismatchException.java deleted file mode 100644 index 5f914fe24b..0000000000 --- a/eo-runtime/src/main/java/org/eolang/TypeMismatchException.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang; - -/** - * TypeMismatch Exception. - * - * @since 0.1 - */ -public final class TypeMismatchException extends RuntimeException { - - /** - * Serial ID. - */ - private static final long serialVersionUID = -6643350804302660951L; - - /** - * Ctor. - * @param reason The reason - */ - public TypeMismatchException(final String reason) { - super(reason); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/ArgsException.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java similarity index 74% rename from eo-runtime/src/main/java/org/eolang/ArgsException.java rename to eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java index 3abd57ae8c..472942c6eb 100644 --- a/eo-runtime/src/main/java/org/eolang/ArgsException.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java @@ -22,26 +22,27 @@ * SOFTWARE. */ -package org.eolang; +package org.eolang.io; + +import org.eolang.Data; +import org.eolang.EObool; +import org.eolang.Phi; /** - * Something is wrong with arguments. + * Stdout. * * @since 0.1 */ -public final class ArgsException extends RuntimeException { - - /** - * Serial ID. - */ - private static final long serialVersionUID = -6643350804302660951L; +public class EOstdout$EOorigin extends Phi { - /** - * Ctor. - * @param reason The reason - */ - public ArgsException(final String reason) { - super(reason); + public EOstdout$EOorigin() { + super("text"); + this.put("origin", () -> { + System.out.print( + new Data.Take(this.get("text")).take(String.class) + ); + return new EObool(); + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout.java deleted file mode 100644 index 33a9bfee06..0000000000 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.eolang.io; - -import java.io.PrintStream; -import org.eolang.Data; -import org.eolang.EObool; - -/** - * Stdout. - * - * @since 0.1 - */ -public class EOstdout extends EObool { - - /** - * Ctor. - * @param txt The text to print - */ - public EOstdout(final Data txt) { - this(txt, System.out); - } - - /** - * Ctor. - * @param text The output stream - * @param stream The text to print - */ - public EOstdout(final Data text, final PrintStream stream) { - super( - () -> { - stream.print(text.get()); - return true; - } - ); - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 877ce2046d..e3220fab84 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,8 +26,6 @@ import java.util.Collection; import java.util.LinkedList; -import org.eolang.Data; -import org.eolang.EOstring; /** * Sprintf. diff --git a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java b/eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java similarity index 93% rename from eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java rename to eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java index 58f8989031..1729b62e94 100644 --- a/eo-runtime/src/test/java/org/eolang/io/EOstdoutTest.java +++ b/eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java @@ -25,23 +25,22 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; /** - * Test case for {@link EOstdout}. + * Test case for {@link EOstdout$EOorigin}. * * @since 0.1 */ -public final class EOstdoutTest { +public final class EOstdout$EOoriginTest { @Test public void printsString() { final String text = "Hello, друг!"; final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new EOstdout( + new EOstdout$EOorigin( new EOstring(text), new PrintStream(baos) ).get(); diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java index 83e1fbf3e5..b5a6e0f31d 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java @@ -23,8 +23,6 @@ */ package org.eolang.txt; -import org.eolang.EOint; -import org.eolang.EOstring; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; From a026377d4eaf556095c3e1df2577d15ee6bd425a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 18:22:16 +0300 Subject: [PATCH 75/80] #164: more --- .../resources/org/eolang/maven/to-java.xsl | 65 +++++++++---------- .../org/eolang/maven/CompileMojoTest.java | 2 +- eo-runtime/src/main/eo/org/eolang/bool.eo | 2 +- eo-runtime/src/main/eo/org/eolang/int.eo | 4 +- eo-runtime/src/main/eo/org/eolang/string.eo | 3 +- eo-runtime/src/main/java/org/eolang/Data.java | 16 ++++- .../main/java/org/eolang/EOstring$EOtrim.java | 51 +++++++++++++++ eo-runtime/src/main/java/org/eolang/Phi.java | 15 +++-- .../java/org/eolang/io/EOstdout$EOorigin.java | 6 +- .../main/java/org/eolang/txt/EOsprintf.java | 20 +++++- 10 files changed, 134 insertions(+), 50 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index 9dd71fe698..ba7da330ed 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -122,7 +122,7 @@ SOFTWARE. () { super( - + , @@ -164,16 +164,6 @@ SOFTWARE. - - - - Can't find what " - - : - - " is pointing to - - final Phi @@ -187,21 +177,26 @@ SOFTWARE. () - + this.get(" ") .copy() + + + new + + () ; - // " - - " at line # - + + + + @@ -275,31 +270,33 @@ SOFTWARE. - - new EOstring( - " - - " - ) + + + + + + .put("data", () -> new Data.Value< + + ); + - - new EOinteger( + + String>(" - L) + ") - - new EOfloat( + + Integer>( - d) + ) - - new EOchar( - ' + + Boolean>( - ') + ) - - new EObool( + + Double>( ) diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index 4e2fc76a36..85e6316e91 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -60,7 +60,7 @@ public void testSimpleCompilation() throws Exception { public void testRealCompilation() throws Exception { final String java = this.compile( new ResourceOf("org/eolang/maven/array.eo"), - "EOarray$EOmap.java" + "EOarray.java" ); MatcherAssert.assertThat(java, Matchers.containsString("class")); } diff --git a/eo-runtime/src/main/eo/org/eolang/bool.eo b/eo-runtime/src/main/eo/org/eolang/bool.eo index 5aa84a377d..d85dc4ac7e 100644 --- a/eo-runtime/src/main/eo/org/eolang/bool.eo +++ b/eo-runtime/src/main/eo/org/eolang/bool.eo @@ -1,5 +1,5 @@ +package org.eolang [] > bool - true > b? + true > data? [t f] > if /t diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index 4caabf9f0b..9e3cbe0827 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -1,6 +1,8 @@ +package org.eolang -[b] > int +[] > int + 0 > data? + # Change the sign of the number [] > neg /int diff --git a/eo-runtime/src/main/eo/org/eolang/string.eo b/eo-runtime/src/main/eo/org/eolang/string.eo index b167058167..ff4e0be89b 100644 --- a/eo-runtime/src/main/eo/org/eolang/string.eo +++ b/eo-runtime/src/main/eo/org/eolang/string.eo @@ -1,4 +1,5 @@ +package org.eolang -[b] > string +[] > string + "" > data? [] > trim /string diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 9fac1cf80e..13749f73b2 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -33,14 +33,26 @@ public interface Data { T take(); - public class Take { + final class Value extends Phi implements Data { + private final T val; + public Value(final T value) { + super(); + this.val = value; + } + @Override + public T take() { + return this.val; + } + } + + final class Take { private final Phi phi; public Take(final Phi src) { this.phi = src; } @SuppressWarnings("unchecked") public T take(final Class type) { - return ((Data) type.cast(this.phi)).take(); + return ((Data) type.cast(this.phi.get("data"))).take(); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java new file mode 100644 index 0000000000..b564373943 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java @@ -0,0 +1,51 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +/** + * Created with IntelliJ IDEA. + * @author Yegor Bugayenko (yegor@tpc2.com) + * @version $ + * @since 1.0 + */ +public class EOstring$EOtrim extends Phi { + + public EOstring$EOtrim() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOstring(); + out.put( + "eo_data", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_data") + ).take(String.class).trim() + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index 2c15708569..719f28cb7e 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -85,13 +85,7 @@ public final void put(final String name, final Attr phi) { ) ); } - if (this.bound.containsKey(name)) { - throw new IllegalStateException( - String.format( - "Attribute \"%s\" is already bound", name - ) - ); - } + this.free.remove(name); this.bound.put(name, phi); } @@ -112,6 +106,13 @@ public final void put(final int pos, final Attr phi) { * @return The object */ public final Phi get(final String name) { + if (!this.free.isEmpty()) { + throw new IllegalStateException( + String.format( + "The object is still abstract, can't get(%s)", name + ) + ); + } Phi attr = this.bound.get(name).get(); if (attr == null) { final Attr origin = this.bound.get("_origin"); diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java index 472942c6eb..dac1ce3d33 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java @@ -36,10 +36,12 @@ public class EOstdout$EOorigin extends Phi { public EOstdout$EOorigin() { - super("text"); + super(); this.put("origin", () -> { System.out.print( - new Data.Take(this.get("text")).take(String.class) + new Data.Take( + this.get("eo_text") + ).take(String.class) ); return new EObool(); }); diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index e3220fab84..480535cbc7 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,13 +26,31 @@ import java.util.Collection; import java.util.LinkedList; +import org.eolang.Data; +import org.eolang.Phi; /** * Sprintf. * * @since 0.2 */ -public class EOsprintf extends EOstring { +public class EOsprintf extends Phi { + + public EOstring$EOtrim() { + super("text", ??); + this.put("origin", () -> { + final Phi out = new org.eolang.EOstring(); + out.put( + "eo_data", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_data") + ).take(String.class).trim() + ) + ); + return out; + }); + } /** * Ctor. From ac947c630421e2c8eac16873e03390061de8be37 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 19:16:54 +0300 Subject: [PATCH 76/80] #164: antlr fix --- .../antlr4/org/eolang/compiler/Program.g4 | 13 +++++++--- .../java/org/eolang/compiler/XeListener.java | 26 +++++++++++++------ .../eolang/compiler/packs/full-syntax.yaml | 4 +-- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index 185a7e6d06..c0aef84b86 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -66,13 +66,20 @@ abstraction : (COMMENT EOL)* LSQ - (argument (SPACE argument)*)? + (attribute (SPACE attribute)*)? RSQ (suffix (SPACE SLASH NAME)?)? ; -argument +attribute : + label + ; + +label + : + AT + | NAME DOTS? ; @@ -90,7 +97,7 @@ suffix SPACE ARROW SPACE - name=( NAME | AT ) + label QUESTION? CONST? ; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 8adb505d32..17b1fd5fcb 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -176,20 +176,31 @@ public void exitAbstraction(final ProgramParser.AbstractionContext ctx) { } @Override - public void enterArgument(final ProgramParser.ArgumentContext ctx) { + public void enterAttribute(final ProgramParser.AttributeContext ctx) { this.enter(); - this.dirs.add("o") - .attr("line", ctx.getStart().getLine()) - .attr("name", ctx.NAME().getText()); + this.dirs.add("o").attr("line", ctx.getStart().getLine()); + } + + @Override + public void exitAttribute(final ProgramParser.AttributeContext ctx) { + this.dirs.up().up(); + } + + @Override + public void enterLabel(final ProgramParser.LabelContext ctx) { + if (ctx.AT() != null) { + this.dirs.attr("name", ctx.AT().getText()); + } + if (ctx.NAME() != null) { + this.dirs.attr("name", ctx.NAME().getText()); + } if (ctx.DOTS() != null) { this.dirs.attr("vararg", ""); } - this.dirs.up().up(); } @Override - public void exitArgument(final ProgramParser.ArgumentContext ctx) { - // nothing + public void exitLabel(final ProgramParser.LabelContext ctx) { } @Override @@ -205,7 +216,6 @@ public void exitTail(final ProgramParser.TailContext ctx) { @Override public void enterSuffix(final ProgramParser.SuffixContext ctx) { this.enter(); - this.dirs.attr("name", ctx.name.getText()); if (ctx.CONST() != null) { this.dirs.attr("const", ""); } diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 61b428590b..77b15dfde8 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -35,12 +35,12 @@ eo: | [x] > first second > hello? $.add 5 > i - third:foo > x?! + third:foo > x...?! f 12 false (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! # This object is also very good [] - hey > you + hey > you... kid f:u z From b5d854f6baf9d2d7cf4154d9552795f7e81d81be Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 21:24:50 +0300 Subject: [PATCH 77/80] #164: more objs --- eo-runtime/src/main/eo/org/eolang/bool.eo | 2 +- eo-runtime/src/main/eo/org/eolang/int.eo | 26 ++++----- eo-runtime/src/main/eo/org/eolang/string.eo | 2 +- .../main/java/org/eolang/EOarray$EOmapi.java | 54 +++++++++++++++++++ .../java/org/eolang/EOarray$EOreduce.java} | 45 +++++++--------- .../java/org/eolang/EObool$EOif.java} | 26 +++++++-- .../java/org/eolang/EOint$EOadd.java} | 39 ++++++++------ .../java/org/eolang/EOint$EOmod.java} | 48 ++++++++--------- .../src/main/java/org/eolang/EOint$EOmul.java | 54 +++++++++++++++++++ .../java/org/eolang/EOint$EOneg.java} | 26 +++++++-- .../java/org/eolang/EOint$EOpow.java} | 45 ++++++++-------- .../main/java/org/eolang/EOstring$EOtrim.java | 9 ++-- .../main/java/org/eolang/txt/EOsprintf.java | 49 +++++++++-------- ...ringTest.java => EOstring$EOtrimTest.java} | 19 +++---- 14 files changed, 296 insertions(+), 148 deletions(-) create mode 100644 eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java rename eo-runtime/src/{test/java/org/eolang/EOboolTest.java => main/java/org/eolang/EOarray$EOreduce.java} (64%) rename eo-runtime/src/{test/java/org/eolang/txt/package-info.java => main/java/org/eolang/EObool$EOif.java} (70%) rename eo-runtime/src/{test/java/org/eolang/txt/EOsprintfTest.java => main/java/org/eolang/EOint$EOadd.java} (65%) rename eo-runtime/src/{test/java/org/eolang/EOintTest.java => main/java/org/eolang/EOint$EOmod.java} (63%) create mode 100644 eo-runtime/src/main/java/org/eolang/EOint$EOmul.java rename eo-runtime/src/{test/java/org/eolang/io/package-info.java => main/java/org/eolang/EOint$EOneg.java} (69%) rename eo-runtime/src/{test/java/org/eolang/io/EOstdout$EOoriginTest.java => main/java/org/eolang/EOint$EOpow.java} (63%) rename eo-runtime/src/test/java/org/eolang/{EOstringTest.java => EOstring$EOtrimTest.java} (79%) diff --git a/eo-runtime/src/main/eo/org/eolang/bool.eo b/eo-runtime/src/main/eo/org/eolang/bool.eo index d85dc4ac7e..b0695a592a 100644 --- a/eo-runtime/src/main/eo/org/eolang/bool.eo +++ b/eo-runtime/src/main/eo/org/eolang/bool.eo @@ -1,5 +1,5 @@ +package org.eolang [] > bool - true > data? + true > self? [t f] > if /t diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo index 9e3cbe0827..7ca6f47218 100644 --- a/eo-runtime/src/main/eo/org/eolang/int.eo +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -1,27 +1,27 @@ +package org.eolang [] > int - 0 > data? + 0 > self? # Change the sign of the number [] > neg /int - # Add a few more numbers to the current one - [x...] > add /int + # Add to the current one + [x] > add /int - # Subtract some numbers from the current one - [x...] > sub - x.reduce > @ - $ - [a b] - a.add b.neg > @ + # Subtract from the current one + [x] > sub + add x.neg > @ - # Multiply this one by some other numbers - [x...] > mul /int + # Multiply this one by another int + [x] > mul /int - # Divide this one by others - [x...] > div /int + # Divide this one by another int + [x] > div + mul x.pow -1 > @ # Calculate MOD [x] > mod /int + # Make it power x + [x] > pow /int diff --git a/eo-runtime/src/main/eo/org/eolang/string.eo b/eo-runtime/src/main/eo/org/eolang/string.eo index ff4e0be89b..9238c8426b 100644 --- a/eo-runtime/src/main/eo/org/eolang/string.eo +++ b/eo-runtime/src/main/eo/org/eolang/string.eo @@ -1,5 +1,5 @@ +package org.eolang [] > string - "" > data? + "" > self? [] > trim /string diff --git a/eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java new file mode 100644 index 0000000000..bf8ceab1a8 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +/** + * IF. + * + * @since 1.0 + */ +public class EOarray$EOmapi extends Phi { + + public EOarray$EOmapi() { + super(); + this.put("origin", () -> { + final Phi[] array = new Data.Take( + this.get("eo_self") + ).take(Phi[].class); + for (long idx = 0; idx < array.length; ++idx) { + final Phi before = array[(int) idx]; + final Phi after = this.get("eo_f").copy(); + after.put(0, () -> before); + final long index = idx; + after.put(1, () -> new Data.Value<>(index)); + array[(int) idx] = after; + } + final Phi out = new org.eolang.EOarray(); + out.put("eo_self", () -> new Data.Value<>(array)); + return out; + }); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/EOboolTest.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java similarity index 64% rename from eo-runtime/src/test/java/org/eolang/EOboolTest.java rename to eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java index eb578e83e9..61dc8c27a3 100644 --- a/eo-runtime/src/test/java/org/eolang/EOboolTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java @@ -21,37 +21,32 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EObool}. + * IF. * - * @since 0.1 + * @since 1.0 */ -public final class EOboolTest { - - @Test - public void instanceWithScalar() { - MatcherAssert.assertThat( - new EObool(() -> true).get(), - Matchers.equalTo(true) - ); - } +public class EOarray$EOreduce extends Phi { - @Test - public void makesFork() { - final String left = "Hello!"; - MatcherAssert.assertThat( - new EObool(true).eoif().copy( - new EOstring(left), - new EOstring("world") - ).get(), - Matchers.equalTo(left) - ); + public EOarray$EOreduce() { + super(); + this.put("origin", () -> { + final Phi[] array = new Data.Take( + this.get("eo_self") + ).take(Phi[].class); + Phi out = this.get("eo_a"); + for (final Phi arg : array) { + final Phi before = out; + final Phi after = this.get("eo_f").copy(); + after.put(0, () -> before); + after.put(1, () -> arg); + out = after; + } + return out; + }); } } diff --git a/eo-runtime/src/test/java/org/eolang/txt/package-info.java b/eo-runtime/src/main/java/org/eolang/EObool$EOif.java similarity index 70% rename from eo-runtime/src/test/java/org/eolang/txt/package-info.java rename to eo-runtime/src/main/java/org/eolang/EObool$EOif.java index 4321ef6281..ee0a14a01b 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/package-info.java +++ b/eo-runtime/src/main/java/org/eolang/EObool$EOif.java @@ -22,9 +22,29 @@ * SOFTWARE. */ +package org.eolang; + /** - * EO runtime, texts. + * IF. * - * @since 0.2 + * @since 1.0 */ -package org.eolang.txt; +public class EObool$EOif extends Phi { + + public EObool$EOif() { + super(); + this.put("origin", () -> { + final Boolean term = new Data.Take( + this.get("eo_self") + ).take(Boolean.class); + final Phi out; + if (term) { + out = this.get("eo_t"); + } else { + out = this.get("eo_f"); + } + return out; + }); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/main/java/org/eolang/EOint$EOadd.java similarity index 65% rename from eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOadd.java index b5a6e0f31d..f2d66b50bf 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOadd.java @@ -21,29 +21,34 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.txt; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EOsprintf}. + * ADD. * - * @since 0.2 + * @since 1.0 */ -public final class EOsprintfTest { +public class EOint$EOadd extends Phi { - @Test - public void printsSimpleText() { - MatcherAssert.assertThat( - new EOsprintf( - new EOstring("Hello, %s %d!"), - new EOstring("John"), - new EOint(2L) - ).get(), - Matchers.equalTo("Hello, John 2!") - ); + public EOint$EOadd() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) + + + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) + ); + return out; + }); } } diff --git a/eo-runtime/src/test/java/org/eolang/EOintTest.java b/eo-runtime/src/main/java/org/eolang/EOint$EOmod.java similarity index 63% rename from eo-runtime/src/test/java/org/eolang/EOintTest.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOmod.java index de980b41f8..8aae5c4c45 100644 --- a/eo-runtime/src/test/java/org/eolang/EOintTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOmod.java @@ -21,38 +21,36 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EOint}. + * MOD. * - * @since 0.1 - * @checkstyle MagicNumberCheck (500 lines) + * @since 1.0 */ -public final class EOintTest { +public class EOint$EOmod extends Phi { - @Test - public void addsNumbers() { - MatcherAssert.assertThat( - new EOint(1L).eoadd().copy( - new EOint(5L), new EOint(-1L) - ).get(), - Matchers.equalTo(5L) - ); - } + public EOint$EOmod() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + Math.floorMod( + new Data.Take( + this.get("eo_self") + ).take(Long.class), + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) - @Test - public void multipliesNumbers() { - MatcherAssert.assertThat( - new EOint(2L).eomul().copy( - new EOint(4L), new EOint(-2L) - ).get(), - Matchers.equalTo(-16L) - ); + ) + ); + return out; + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java b/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java new file mode 100644 index 0000000000..f4849c90ba --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.eolang; + +/** + * MUL. + * + * @since 1.0 + */ +public class EOint$EOmul extends Phi { + + public EOint$EOmul() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) + * + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/io/package-info.java b/eo-runtime/src/main/java/org/eolang/EOint$EOneg.java similarity index 69% rename from eo-runtime/src/test/java/org/eolang/io/package-info.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOneg.java index 503d184c09..1f05afa723 100644 --- a/eo-runtime/src/test/java/org/eolang/io/package-info.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOneg.java @@ -22,9 +22,29 @@ * SOFTWARE. */ +package org.eolang; + /** - * EO system objects. + * NEG. * - * @since 0.1 + * @since 1.0 */ -package org.eolang.io; +public class EOint$EOneg extends Phi { + + public EOint$EOneg() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) * -1L + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java b/eo-runtime/src/main/java/org/eolang/EOint$EOpow.java similarity index 63% rename from eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOpow.java index 1729b62e94..a6eee52601 100644 --- a/eo-runtime/src/test/java/org/eolang/io/EOstdout$EOoriginTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOpow.java @@ -21,33 +21,36 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.io; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EOstdout$EOorigin}. + * POW. * - * @since 0.1 + * @since 1.0 */ -public final class EOstdout$EOoriginTest { +public class EOint$EOpow extends Phi { - @Test - public void printsString() { - final String text = "Hello, друг!"; - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new EOstdout$EOorigin( - new EOstring(text), - new PrintStream(baos) - ).get(); - MatcherAssert.assertThat( - baos.toString(), - Matchers.equalTo(text) - ); + public EOint$EOpow() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + Math.pow( + new Data.Take( + this.get("eo_self") + ).take(Long.class), + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) + + ) + ); + return out; + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java index b564373943..420cdb1093 100644 --- a/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java @@ -25,9 +25,8 @@ package org.eolang; /** - * Created with IntelliJ IDEA. - * @author Yegor Bugayenko (yegor@tpc2.com) - * @version $ + * TRIM. + * * @since 1.0 */ public class EOstring$EOtrim extends Phi { @@ -37,10 +36,10 @@ public class EOstring$EOtrim extends Phi { this.put("origin", () -> { final Phi out = new org.eolang.EOstring(); out.put( - "eo_data", + "eo_self", () -> new Data.Value<>( new Data.Take( - this.get("eo_data") + this.get("eo_self") ).take(String.class).trim() ) ); diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index 480535cbc7..ba0ddfaaf6 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -27,6 +27,8 @@ import java.util.Collection; import java.util.LinkedList; import org.eolang.Data; +import org.eolang.EObool; +import org.eolang.EOstring; import org.eolang.Phi; /** @@ -36,36 +38,41 @@ */ public class EOsprintf extends Phi { - public EOstring$EOtrim() { - super("text", ??); + public EOsprintf() { + super("eo_format", "eo_args..."); this.put("origin", () -> { final Phi out = new org.eolang.EOstring(); + final String format = new Data.Take( + this.get("eo_format") + ).take(String.class); + final Phi[] args = new Data.Take( + this.get("eo_args") + ).take(Phi[].class); + final Collection items = new LinkedList<>(); + for (final Phi arg : args) { + items.add(EOsprintf.toArg(arg)); + } out.put( - "eo_data", + "eo_self", () -> new Data.Value<>( - new Data.Take( - this.get("eo_data") - ).take(String.class).trim() + String.format(format, items.toArray()) ) ); return out; }); } - /** - * Ctor. - * @param format Format - * @param args Args - */ - public EOsprintf(final Data format, final Data... args) { - super( - () -> { - final Collection items = new LinkedList<>(); - for (final Data arg : args) { - items.add(arg.get()); - } - return String.format(format.get(), items.toArray()); - } - ); + private static Object toArg(final Phi phi) { + final Object result; + final Data.Take take = new Data.Take(phi.get("eo_self")); + if (phi instanceof EObool) { + result = take.take(Boolean.class); + } else if (phi instanceof EOstring) { + result = take.take(String.class); + } else { + throw new UnsupportedOperationException(""); + } + return result; } + } diff --git a/eo-runtime/src/test/java/org/eolang/EOstringTest.java b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java similarity index 79% rename from eo-runtime/src/test/java/org/eolang/EOstringTest.java rename to eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java index 7ad1616c5b..acddc1d414 100644 --- a/eo-runtime/src/test/java/org/eolang/EOstringTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java @@ -32,22 +32,15 @@ * * @since 0.1 */ -public final class EOstringTest { +public final class EOstring$EOtrimTest { @Test - public void encapsulatesString() { - final String key = "hey"; + public void trimsString() { + final Phi phi = new EOstring$EOtrim(); + phi.put("eo_text", () -> new Data.Value<>("Hello, world! ")); MatcherAssert.assertThat( - new EOstring(key).get(), - Matchers.equalTo(key) - ); - } - - @Test - public void instanceWithScalar() { - MatcherAssert.assertThat( - new EOstring(() -> "Hi").get(), - Matchers.equalTo("Hi") + new Data.Take(new EOstring$EOtrim()).take(String.class), + Matchers.equalTo("Hello, world!") ); } From 863f2148c904e459bad05e6675ccb545a85fb41e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 22:12:08 +0300 Subject: [PATCH 78/80] #164: test fixed --- eo-runtime/src/main/java/org/eolang/Data.java | 2 +- .../main/java/org/eolang/EOstring$EOtrim.java | 2 +- eo-runtime/src/main/java/org/eolang/Phi.java | 33 +++++++------------ .../java/org/eolang/EOstring$EOtrimTest.java | 4 +-- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Data.java b/eo-runtime/src/main/java/org/eolang/Data.java index 13749f73b2..8f77250b06 100644 --- a/eo-runtime/src/main/java/org/eolang/Data.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -52,7 +52,7 @@ public Take(final Phi src) { } @SuppressWarnings("unchecked") public T take(final Class type) { - return ((Data) type.cast(this.phi.get("data"))).take(); + return type.cast(((Data) Data.class.cast(this.phi)).take()); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java index 420cdb1093..57e71ab294 100644 --- a/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java @@ -39,7 +39,7 @@ public class EOstring$EOtrim extends Phi { "eo_self", () -> new Data.Value<>( new Data.Take( - this.get("eo_self") + this.get("eo_text+") ).take(String.class).trim() ) ); diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index 719f28cb7e..a44a172eec 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -78,14 +78,14 @@ public final Phi copy() { * @param phi The value to set */ public final void put(final String name, final Attr phi) { - if (!this.free.contains(name)) { + if (this.bound.containsKey(name) && !this.free.contains(name)) { throw new IllegalStateException( String.format( - "Attribute \"%s\" is not free here", name + "The attribute \"%s\" is already bound in %s", + name, this.getClass().getCanonicalName() ) ); } - this.free.remove(name); this.bound.put(name, phi); } @@ -106,32 +106,23 @@ public final void put(final int pos, final Attr phi) { * @return The object */ public final Phi get(final String name) { - if (!this.free.isEmpty()) { - throw new IllegalStateException( - String.format( - "The object is still abstract, can't get(%s)", name - ) - ); - } - Phi attr = this.bound.get(name).get(); + final Attr attr = this.bound.get(name); + final Phi phi; if (attr == null) { - final Attr origin = this.bound.get("_origin"); + final Attr origin = this.bound.get("origin"); if (origin == null) { throw new IllegalStateException( String.format( - "Can't find \"%s\" attribute", name + "Can't find \"%s\" attr and there is no origin in %s", + name, this.getClass().getCanonicalName() ) ); } - attr = origin.get().get(name); + phi = origin.get().get(name); + } else { + phi = attr.get(); } - return attr; - } - - protected T data(final Class type) { - throw new UnsupportedOperationException( - "No data here" - ); + return phi; } } diff --git a/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java index acddc1d414..62facaa321 100644 --- a/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java @@ -37,9 +37,9 @@ public final class EOstring$EOtrimTest { @Test public void trimsString() { final Phi phi = new EOstring$EOtrim(); - phi.put("eo_text", () -> new Data.Value<>("Hello, world! ")); + phi.put("eo_text+", () -> new Data.Value<>("Hello, world! ")); MatcherAssert.assertThat( - new Data.Take(new EOstring$EOtrim()).take(String.class), + new Data.Take(phi.get("eo_self")).take(String.class), Matchers.equalTo("Hello, world!") ); } From c0a9b1eb0bf7b00de5202443234bdc885c1598e9 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 22:28:18 +0300 Subject: [PATCH 79/80] #164: inherit --- .../main/resources/org/eolang/maven/to-java.xsl | 4 ++++ eo-runtime/src/main/java/org/eolang/Phi.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index ba7da330ed..ea9778cac8 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -247,6 +247,10 @@ SOFTWARE. + + + .inherit(this); + diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java index a44a172eec..ecb54af725 100644 --- a/eo-runtime/src/main/java/org/eolang/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -99,6 +99,20 @@ public final void put(final int pos, final Attr phi) { this.bound.put(this.free.get(pos), phi); } + /** + * Inherit attributes from parent object. + * + * @param parent The parent + */ + public final void inherit(final Phi parent) { + for (final Map.Entry attr : parent.bound.entrySet()) { + this.put( + String.format("%s+", attr.getKey()), + attr.getValue() + ); + } + } + /** * Get the attribute. * From 28e722cc72bcb834034ab6ff29e1040849f6684b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 23 Dec 2020 22:31:16 +0300 Subject: [PATCH 80/80] #164: clean --- eo-runtime/pom.xml | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/eo-runtime/pom.xml b/eo-runtime/pom.xml index 92b9891b3b..5d9597af1c 100644 --- a/eo-runtime/pom.xml +++ b/eo-runtime/pom.xml @@ -33,12 +33,6 @@ SOFTWARE. jar eo-runtime eolang runtime library - - - org.cactoos - cactoos - - @@ -58,4 +52,23 @@ SOFTWARE. + + + qulice + + + + com.qulice + qulice-maven-plugin + + + checkstyle:.* + pmd:.* + + + + + + +