diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/pre/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/pre/to-java.xsl index 6c2943e4bd..f9fafcf169 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/pre/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/pre/to-java.xsl @@ -246,11 +246,6 @@ SOFTWARE. - - - ret = new PhConst(ret); - - return ret; @@ -268,11 +263,6 @@ SOFTWARE. - - - ret = new PhConst(ret); - - return ret; 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 d5ec32bafc..59bda6ec6b 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 @@ -17,7 +17,7 @@ 666 two.f1.f2 x stdout > z - sprintf > f! + sprintf > f "Hello, ".trim x 0x1fff n diff --git a/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java b/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java index 032c403b9c..38a029935a 100644 --- a/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java +++ b/eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java @@ -104,6 +104,7 @@ public final class ParsingTrain extends TrEnvelope { "/org/eolang/parser/errors/unused-aliases.xsl", "/org/eolang/parser/warnings/unit-test-without-phi.xsl", "/org/eolang/parser/explicit-data.xsl", + "/org/eolang/parser/const-to-dataized.xsl", "/org/eolang/parser/set-locators.xsl", }; diff --git a/eo-parser/src/main/resources/org/eolang/parser/const-to-dataized.xsl b/eo-parser/src/main/resources/org/eolang/parser/const-to-dataized.xsl new file mode 100644 index 0000000000..14e2127a43 --- /dev/null +++ b/eo-parser/src/main/resources/org/eolang/parser/const-to-dataized.xsl @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-parser/src/test/resources/org/eolang/parser/packs/const-to-dataized.yaml b/eo-parser/src/test/resources/org/eolang/parser/packs/const-to-dataized.yaml new file mode 100644 index 0000000000..053e50da8c --- /dev/null +++ b/eo-parser/src/test/resources/org/eolang/parser/packs/const-to-dataized.yaml @@ -0,0 +1,20 @@ +tests: + - //o[@base='.as-bytes' and @line and @pos and @name='x' and o[@base='org.eolang.dataized' and o[@base='org.eolang.int' and not(@name)]]] + - //o[@base='.as-bytes' and @line and @pos and @name='m' and o[@base='org.eolang.dataized' and o[@abstract and @name='m-3' and count(o)=2]]] + - //o[@base='.as-bytes' and @line and @pos and @name='z' and o[@base='org.eolang.dataized' and o[@base='.o' and not(@name) and count(o)=1]]] + - //o[@base='.as-bytes' and @line and @pos and @name='y' and o[@base='org.eolang.dataized' and o[@base='org.eolang.c' and not(@name)]]] + - //objects[count(//o[@const])=0] +eo: | + # This is the default 64+ symbols comment in front of abstract object. + [] > foo + int > x! + # This is the default 64+ symbols comment in front of named abstract object. + [] > m! + a + b + c 1 2 > y! + .o > z! + # This is the default 64+ symbols comment in front of named abstract object. + [] > m-1 + # This is the default 64+ symbols comment in front of named abstract object. + [] > m-2 diff --git a/eo-runtime/src/main/java/org/eolang/PhConst.java b/eo-runtime/src/main/java/org/eolang/PhConst.java deleted file mode 100644 index 97bbf3d210..0000000000 --- a/eo-runtime/src/main/java/org/eolang/PhConst.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * 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.util.concurrent.atomic.AtomicReference; - -/** - * Object that eliminates duplication of data calculation. - * When this object is accessed for an attribute, it: - * 1. dataizes the encapsulated one - * 2. makes new instance of {@link EOorg.EOeolang.EObytes} with given data (bytes) - * 3. caches it - * 4. behaves as cached {@link EOorg.EOeolang.EObytes} - * @since 0.16 - */ -@Versionized -public final class PhConst implements Phi { - - /** - * The object being wrapped by const decorator. - */ - private final Phi wrapped; - - /** - * Bytes retrieved from the wrapped object. - */ - private final AtomicReference bytes; - - /** - * Ctor. - * @param phi The origin wrapped phi - */ - public PhConst(final Phi phi) { - this(phi, null); - } - - /** - * Ctor for copying. - * @param phi The origin wrapped phi - * @param bts EObytes - */ - private PhConst(final Phi phi, final Phi bts) { - this.wrapped = phi; - this.bytes = new AtomicReference<>(bts); - } - - @Override - public boolean equals(final Object obj) { - return this.wrapped.equals(obj); - } - - @Override - public int hashCode() { - return this.wrapped.hashCode(); - } - - @Override - public String toString() { - return String.format("!%s", this.wrapped); - } - - @Override - public String φTerm() { - return String.format("%s!", this.wrapped.φTerm()); - } - - @Override - public Phi copy() { - return new PhConst(this.wrapped.copy(), this.primitive()); - } - - @Override - public Phi take(final String name) { - return this.primitive().take(name); - } - - @Override - public boolean put(final int pos, final Phi object) { - return this.primitive().put(pos, object); - } - - @Override - public boolean put(final String name, final Phi object) { - return this.primitive().put(name, object); - } - - @Override - public String locator() { - return this.wrapped.locator(); - } - - @Override - public String forma() { - return this.wrapped.forma(); - } - - @Override - public void attach(final byte[] data) { - this.primitive().attach(data); - } - - @Override - public byte[] delta() { - return this.primitive().delta(); - } - - /** - * Cached primitive object that was retrieved from dataization of wrapped one. - * @return EObytes object - */ - private Phi primitive() { - synchronized (this.bytes) { - if (this.bytes.get() == null) { - this.bytes.set( - new Data.ToPhi( - new Dataized(this.wrapped).take() - ) - ); - } - } - return this.bytes.get(); - } -} diff --git a/eo-runtime/src/test/java/org/eolang/PhConstTest.java b/eo-runtime/src/test/java/org/eolang/PhConstTest.java deleted file mode 100644 index 1a648c6ac4..0000000000 --- a/eo-runtime/src/test/java/org/eolang/PhConstTest.java +++ /dev/null @@ -1,308 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * 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 EOorg.EOeolang.EOerror; -import java.security.SecureRandom; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Test case for @link PhConst}. - * @since 0.16 - */ -final class PhConstTest { - @Test - void makesDataObjectConstant() { - MatcherAssert.assertThat( - "Const data object should be dataizable", - new Dataized( - new PhConst( - new Data.ToPhi("Hello, world!") - ) - ).take(String.class), - Matchers.startsWith("Hello") - ); - } - - @Test - void behavesAsBytes() { - MatcherAssert.assertThat( - "Const object should have 'as-int' attribute which is from 'bytes' object", - new Dataized(new PhConst(new Data.ToPhi(42L)).take("as-int")).take(Long.class), - Matchers.equalTo(42L) - ); - } - - @Test - void caclulatesPhiOnlyOnce() { - final Dummy dummy = new PhConstTest.Dummy("any"); - final Phi phi = new PhConst(dummy); - for (int idx = 0; idx < 10; ++idx) { - MatcherAssert.assertThat( - new Dataized(phi).take(Long.class), - Matchers.is(1L) - ); - } - MatcherAssert.assertThat( - dummy.count, - Matchers.equalTo(1) - ); - } - - @Test - void doesNotHaveAttributesOfDecoratedPhi() { - final Phi phi = new PhConst(new PhConstTest.Dummy("attr")); - Assertions.assertThrows( - EOerror.ExError.class, - () -> phi.take("attr"), - "Const object should behave as bytes, not as decorated object" - ); - } - - @Test - void dataizesSimpleRandomToConst() { - final Phi rnd = new PhConstTest.Rnd(Phi.Φ); - MatcherAssert.assertThat( - new Dataized(rnd).take(Double.class), - Matchers.not( - Matchers.equalTo(new Dataized(rnd).take(Double.class)) - ) - ); - final Phi cnst = new PhConst(rnd); - MatcherAssert.assertThat( - new Dataized(cnst).take(Double.class), - Matchers.equalTo(new Dataized(cnst).take(Double.class)) - ); - } - - @Test - void dataizesRandomToConst() { - final Phi rnd = new PhConst(new PhConstTest.Rnd(Phi.Φ)); - final Phi eql = rnd.take("eq").copy(); - eql.put(0, rnd); - MatcherAssert.assertThat( - new Dataized(eql).take(Boolean.class), - Matchers.equalTo(true) - ); - } - - @Test - void doesNotRecalculateAfterCopying() { - final Phi rnd = new PhConst(new PhConstTest.Rnd(Phi.Φ)); - MatcherAssert.assertThat( - "Const object should not be recalculated after copying", - new Dataized(rnd).take(Long.class), - Matchers.equalTo( - new Dataized(rnd.copy()).take(Long.class) - ) - ); - } - - @Test - void keepsConstMultiLayers() { - final Phi phi = new PhWith( - new Envelope(Phi.Φ), - 0, - new PhWith( - new Envelope(Phi.Φ), - 0, - new PhConst(new Rnd(Phi.Φ)) - ) - ); - MatcherAssert.assertThat( - new Dataized(phi).take(Double.class), - Matchers.equalTo(new Dataized(phi).take(Double.class)) - ); - } - - @Test - void dataizesOnlyOnceViaEnvelopes() { - final Dummy dummy = new PhConstTest.Dummy("x"); - final Phi phi = new PhConst( - new PhWith( - new Envelope(Phi.Φ), - 0, - new PhWith( - new Envelope(Phi.Φ), - 0, - new PhWith( - new Envelope(Phi.Φ), - 0, dummy - ) - ) - ) - ); - phi.take("eq"); - new Dataized(phi).take(Long.class); - MatcherAssert.assertThat( - dummy.count, - Matchers.equalTo(1) - ); - } - - @Test - void dataizesOnlyOnceViaMethods() { - final Dummy dummy = new PhConstTest.Dummy("x"); - final Phi phi = new PhConst( - new PhWith( - new Envelope(Phi.Φ), - 0, - new PhWith( - new Envelope(Phi.Φ), - 0, - new PhWith( - new Envelope(Phi.Φ), - 0, dummy - ) - ) - ) - ); - new Dataized(phi).take(); - new Dataized(phi).take(); - MatcherAssert.assertThat( - dummy.count, - Matchers.equalTo(1) - ); - } - - @Test - void usesStringRepresentationOfWrapped() { - final Phi phi = new PhConstTest.Dummy("some"); - MatcherAssert.assertThat( - "Const object should use string representation of wrapped object", - new PhConst(phi).toString(), - Matchers.equalTo(String.format("!%s", phi)) - ); - } - - @Test - void usesTermRepresentationOfWrapped() { - final Phi phi = new PhConstTest.Dummy("some"); - MatcherAssert.assertThat( - "Const object should use term representation of wrapped object", - new PhConst(phi).φTerm(), - Matchers.equalTo(String.format("%s!", phi.φTerm())) - ); - } - - @Test - void usesLocatorOfWrapped() { - final Phi phi = new PhConstTest.Dummy("some"); - MatcherAssert.assertThat( - "Const object should use locator of wrapped object", - new PhConst(phi).locator(), - Matchers.equalTo(phi.locator()) - ); - } - - @Test - void usesFormOfWrapped() { - final Phi phi = new PhConstTest.Dummy("some"); - MatcherAssert.assertThat( - "Const object should use form of wrapped object", - new PhConst(phi).forma(), - Matchers.equalTo(phi.forma()) - ); - } - - @Test - void equalsToWrapped() { - final Phi phi = new PhConstTest.Dummy("some"); - MatcherAssert.assertThat( - "Const object should use 'equals' of wrapped object", - new PhConst(phi), - Matchers.equalTo(phi) - ); - } - - /** - * Rnd. - * @since 1.0 - */ - private static class Rnd extends PhDefault { - /** - * Ctor. - * @param sigma Sigma - */ - Rnd(final Phi sigma) { - super(sigma); - this.add( - "φ", - new AtComposite( - this, - self -> new Data.ToPhi(new SecureRandom().nextDouble()) - ) - ); - } - } - - /** - * Dummy Phi. - * @since 1.0 - */ - private static class Dummy extends PhDefault { - /** - * Count. - */ - private int count; - - /** - * Ctor. - * @param name Name - */ - Dummy(final String name) { - super(); - this.add( - "φ", - new AtComposite( - this, - self -> { - ++this.count; - return new Data.ToPhi(1L); - } - ) - ); - this.add(name, new AtComposite(this, rho -> this.take(Attr.PHI))); - } - } - - /** - * Phi envelope. - * @since 1.0 - */ - private static class Envelope extends PhDefault { - /** - * Ctor. - * @param sigma Sigma - */ - Envelope(final Phi sigma) { - super(sigma); - this.add("x", new AtVoid("x")); - this.add("φ", new AtComposite(this, rho -> rho.take("x"))); - } - } -}