Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 9, 2024
2 parents 29def27 + 5abfeb4 commit 6f50ce2
Show file tree
Hide file tree
Showing 25 changed files with 239 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ SOFTWARE.
<xsl:text>super(sigma);</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:variable name="type" select="concat(//meta[head='package']/tail, '.', @name)"/>
<xsl:apply-templates select="attr">
<xsl:with-param name="class" select="."/>
<xsl:with-param name="indent">
Expand Down
12 changes: 5 additions & 7 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOtry.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ private static class PhTry implements Phi {
this.func = new TryExecute(body, ctch);
}

@Override
public void attach(final byte[] data) {
this.func.accept(phi -> phi.attach(data));
}

@Override
public byte[] delta() {
return new TryReturn<byte[]>(
Expand All @@ -120,13 +125,6 @@ public Phi take(final String name) {
).apply(phi -> phi.take(name));
}

@Override
public Phi take(final String name, final Phi rho) {
return new TryReturn<Phi>(
this.body, this.ctch, this.last
).apply(phi -> phi.take(name, rho));
}

@Override
public void put(final int pos, final Phi object) {
this.func.accept(phi -> phi.put(pos, object));
Expand Down
6 changes: 6 additions & 0 deletions eo-runtime/src/main/java/org/eolang/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public interface Bytes {
*/
<T extends Number> T asNumber(Class<T> type);

/**
* Convert to string.
* @return String.
*/
String asString();

/**
* Get bytes itself.
* @return Bytes.
Expand Down
15 changes: 15 additions & 0 deletions eo-runtime/src/main/java/org/eolang/BytesOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ public <T extends Number> T asNumber(final Class<T> type) {
return type.cast(res);
}

@Override
public String asString() {
final StringBuilder out = new StringBuilder(0);
for (final byte bte : this.data) {
if (out.length() > 0) {
out.append('-');
}
out.append(String.format("%02X", bte));
}
if (this.data.length == 0) {
out.append("--");
}
return out.toString();
}

@Override
public byte[] take() {
return Arrays.copyOf(this.data, this.data.length);
Expand Down
31 changes: 21 additions & 10 deletions eo-runtime/src/main/java/org/eolang/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
*/
@Versionized
public interface Data {
/**
* Attach data to the object.
* @param data Data.
* @todo #2931:60min Change the data storage architecture. Current implementation allows the
* presence of two methods for data manipulations: {@link Data#attach(byte[])} to set data and
* {@link Data#delta()} to get data; which does not seem to be object oriented. It also
* requires every object to have reserved place for possible injected data. In our case, every
* {@link PhDefault} has {@link PhDefault#data} variable. It would be much better to have this
* data only inside some decorator. The main difficulty here is - child attributes of
* decorated object should know that their \rho is decorated and contains data.
*/
void attach(byte[] data);

/**
* Take the data.
Expand Down Expand Up @@ -87,11 +99,6 @@ public Phi take(final String name) {
return this.object.take(name);
}

@Override
public Phi take(final String name, final Phi rho) {
return this.object.take(name, rho);
}

@Override
public void put(final int pos, final Phi obj) {
this.object.put(pos, obj);
Expand Down Expand Up @@ -122,6 +129,11 @@ public String toString() {
return this.object.toString();
}

@Override
public void attach(final byte[] data) {
this.object.attach(data);
}

@Override
public byte[] delta() {
return this.object.delta();
Expand Down Expand Up @@ -171,15 +183,14 @@ private static Phi toPhi(final Object obj) {
)
);
}
final Phi object;
if (delta) {
object = new PhData(phi, bytes);
phi.attach(bytes);
} else {
final Phi bts = new PhData(eolang.take("bytes").copy(), bytes);
final Phi bts = eolang.take("bytes").copy();
bts.attach(bytes);
phi.put(0, bts);
object = phi;
}
return object;
return phi;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion eo-runtime/src/main/java/org/eolang/Dataized.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public byte[] take() {
String.join("", Collections.nCopies(before, "·")),
this.phi.locator(),
this.phi.toString().replaceAll("[\n\t]", ""),
new PhData(Phi.Φ, data).bytes().replaceAll("[\n\t]", "")
new BytesOf(data).asString().replaceAll("[\n\t]", "")
)
);
}
Expand Down
10 changes: 5 additions & 5 deletions eo-runtime/src/main/java/org/eolang/PhConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ public Phi take(final String name) {
return this.primitive().take(name);
}

@Override
public Phi take(final String name, final Phi rho) {
return this.primitive().take(name, rho);
}

@Override
public void put(final int pos, final Phi object) {
this.primitive().put(pos, object);
Expand All @@ -121,6 +116,11 @@ 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();
Expand Down
118 changes: 22 additions & 96 deletions eo-runtime/src/main/java/org/eolang/PhData.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,108 +25,34 @@
package org.eolang;

/**
* Object with data.
* Object with attached data.
* @since 0.36.0
*/
public final class PhData implements Phi {

/**
* The original object.
*/
private final Phi object;

/**
* Data.
*/
private final byte[] data;
public final class PhData extends PhOnce {

/**
* Ctor.
* @param obj Original object
* @param data Data
* @param phi Object
* @param bytes Data to attach
*/
public PhData(final Phi obj, final byte[] data) {
this.object = obj;
this.data = data;
}

@Override
public Phi copy() {
return new PhData(this.object.copy(), this.data);
}

@Override
public Phi take(final String name) {
return this.take(name, this);
}

@Override
public Phi take(final String name, final Phi rho) {
return this.object.take(name, rho);
}

@Override
public void put(final int pos, final Phi obj) {
this.object.put(pos, obj);
}

@Override
public void put(final String name, final Phi obj) {
this.object.put(name, obj);
}

@Override
public String locator() {
return this.object.locator();
}

@Override
public String forma() {
return this.object.forma();
}

@Override
public String φTerm() {
return this.object.φTerm()
.replaceFirst(
"⟦\n",
String.format(
"⟦\n\t%s ↦ %s,\n",
Attr.DELTA,
this.bytes()
)
);
}

@Override
public byte[] delta() {
return this.data;
}

@Override
public String toString() {
return String.format(
"%s=%s",
this.object.toString(),
this.bytes()
public PhData(final Phi phi, final byte[] bytes) {
super(
() -> {
phi.attach(bytes);
return phi;
},
() -> String.format(
"%s[%s=%s]",
phi,
Attr.DELTA,
new BytesOf(bytes).asString()
),
() -> String.format(
"%s(%s ↦ %s)",
phi.φTerm(),
Attr.DELTA,
new BytesOf(bytes).asString()
)
);
}

/**
* Converts byte array to string.
* @return Byte array as string
*/
public String bytes() {
final StringBuilder out = new StringBuilder(0);
for (final byte bte : this.data) {
if (out.length() > 0) {
out.append('-');
}
out.append(String.format("%02X", bte));
}
if (this.data.length == 0) {
out.append("--");
}
return out.toString();
}
}

2 comments on commit 6f50ce2

@0pdd
Copy link

@0pdd 0pdd commented on 6f50ce2 Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2931-58eca492 discovered in eo-runtime/src/main/java/org/eolang/Data.java) and submitted as #3067. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 6f50ce2 Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 2931-cf3a8437 discovered in eo-runtime/src/test/eo/org/eolang/cage-tests.eo) and submitted as #3068. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.