Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<xsl:text>");</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>new PhMethod(</xsl:text>
<xsl:text>new PhDispatch(</xsl:text>
<xsl:value-of select="$base"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="eo:attr-name($mtd, true())"/>
Expand Down Expand Up @@ -626,14 +626,6 @@
<xsl:param name="skip" select="0"/>
<xsl:variable name="inners" select="o[position() &gt; $skip and not(@level)]"/>
<xsl:for-each select="$inners">
<xsl:if test="position() = 1">
<xsl:value-of select="eo:eol($indent)"/>
<xsl:value-of select="$name"/>
<xsl:text> = </xsl:text>
<xsl:text>new PhCopy(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>);</xsl:text>
</xsl:if>
<xsl:variable name="next">
<xsl:value-of select="$name"/>
<xsl:value-of select="position()"/>
Expand All @@ -644,25 +636,28 @@
<xsl:with-param name="rho" select="$rho"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:for-each select="$inners">
<xsl:if test="$inners">
<xsl:value-of select="eo:eol($indent)"/>
<xsl:value-of select="$name"/>
<xsl:text> = new PhWith(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>, </xsl:text>
<xsl:choose>
<xsl:when test="@as">
<xsl:value-of select="eo:attr-name(@as, true())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position() - 1"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>, </xsl:text>
<xsl:text> = new PhApplication(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:value-of select="position()"/>
<xsl:for-each select="$inners">
<xsl:text>, new Bind(</xsl:text>
<xsl:choose>
<xsl:when test="@as">
<xsl:value-of select="eo:attr-name(@as, true())"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="position() - 1"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>, </xsl:text>
<xsl:value-of select="$name"/>
<xsl:value-of select="position()"/>
<xsl:text>)</xsl:text>
</xsl:for-each>
<xsl:text>);</xsl:text>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates select="value">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="indent" select="$indent">
Expand All @@ -675,7 +670,7 @@
<xsl:param name="name"/>
<xsl:value-of select="eo:eol($indent)"/>
<xsl:value-of select="$name"/>
<xsl:text> = new PhWith(</xsl:text>
<xsl:text> = new PhApplication(</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>, 0, new PhDefault(</xsl:text>
<xsl:value-of select="text()"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ sheets:
- /org/eolang/maven/transpile/to-java.xsl
asserts:
- /object[not(errors)]
- //java/text()[contains(., 'new PhWith(r, 0')]
- //java/text()[contains(., 'new PhWith(r, "text"')]
- //java/text()[contains(., 'new PhApplication(r, new Bind(0')]
- //java/text()[contains(., 'new PhApplication(r, new Bind("text"')]
input: |
# No comments.
[] > main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ sheets:
- /org/eolang/maven/transpile/to-java.xsl
asserts:
- /object[not(errors)]
- //java[contains(text(), 'Phi r = new PhMethod(Phi.Φ, "io");')]
- //java[contains(text(), 'r = new PhMethod(r, "stdout");')]
- //java[contains(text(), 'Phi r = new PhDispatch(Phi.Φ, "io");')]
- //java[contains(text(), 'r = new PhDispatch(r, "stdout");')]
input: |
# No comments.
[args] > program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ sheets:
- /org/eolang/maven/transpile/to-java.xsl
asserts:
- /object[not(errors)]
- //java[contains(text(), 'new PhMethod(Phi.Φ, "true")')]
- //java[contains(text(), 'r1 = new PhWith(r1, 0, new PhDefault(new byte[] {(byte) 0x00}));')]
- //java[contains(text(), 'r1 = new PhWith(r1, 0, new PhDefault(new byte[] {(byte) 0x40, (byte) 0x45, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}));')]
- //java[contains(text(), 'r11 = new PhWith(r11, 0, new PhDefault(new byte[] {(byte) 0x40, (byte) 0x0C, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}));')]
- //java[contains(text(), 'r = new PhWith(r, 0, new PhDefault(new byte[] {(byte) 0x01, (byte) 0xAF}));')]
- //java[contains(text(), 'new PhDispatch(Phi.Φ, "true")')]
- //java[contains(text(), 'r1 = new PhApplication(r1, 0, new PhDefault(new byte[] {(byte) 0x00}));')]
- //java[contains(text(), 'r1 = new PhApplication(r1, 0, new PhDefault(new byte[] {(byte) 0x40, (byte) 0x45, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}));')]
- //java[contains(text(), 'r11 = new PhApplication(r11, 0, new PhDefault(new byte[] {(byte) 0x40, (byte) 0x0C, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}));')]
- //java[contains(text(), 'r = new PhApplication(r, 0, new PhDefault(new byte[] {(byte) 0x01, (byte) 0xAF}));')]
input: |
# No comments.
[] > data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ asserts:
- //java[contains(text(), '((PhDefault) rr).add("φ",')]
- //java[contains(text(), 'Phi rrr = rrh.take("ρ")')]
- //java[contains(text(), 'rrr = rrr.take("ρ")')]
- //java[contains(text(), 'rrr = new PhMethod(rrr, "this")')]
- //java[contains(text(), 'rrr = new PhMethod(rrr, "x")')]
- //java[contains(text(), 'rrr = new PhDispatch(rrr, "this")')]
- //java[contains(text(), 'rrr = new PhDispatch(rrr, "x")')]
- //java[contains(text(), 'return rrr;')]
input: |
# No comments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ sheets:
- /org/eolang/maven/transpile/to-java.xsl
asserts:
- /object[not(errors)]
- //java[contains(text(), 'Phi rb = new PhMethod(h, "x");')]
- //java[contains(text(), 'Phi rb = new PhDispatch(h, "x");')]
- //java[contains(text(), 'rb = rb.take("ρ");')]
- //java[contains(text(), 'rb = new PhMethod(rb, "or");')]
- //java[contains(text(), 'rb = new PhCopy(rb);')]
- //java[contains(text(), 'Phi rb1 = new PhMethod(Phi.Φ, "number");')]
- //java[contains(text(), 'Phi rb11 = new PhMethod(Phi.Φ, "bytes")')]
- //java[contains(text(), 'Phi r = new PhMethod(rb, "and");')]
- //java[contains(text(), 'rb = new PhDispatch(rb, "or");')]
- //java[contains(text(), 'Phi rb1 = new PhDispatch(Phi.Φ, "number");')]
- //java[contains(text(), 'Phi rb11 = new PhDispatch(Phi.Φ, "bytes")')]
- //java[contains(text(), 'Phi r = new PhDispatch(rb, "and");')]
input: |
# Comment.
[] > foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ sheets:
asserts:
- /object[not(errors)]
- //java[contains(text(), 'package org.eolang;')]
- //java[contains(text(), 'Phi rr11 = new PhMethod(Phi.Φ, "tuple");')]
- //java[contains(text(), 'rr11 = new PhCopy(rr11);')]
- //java[contains(text(), 'new PhMethod(rr111, "empty");')]
- //java[contains(text(), 'Phi rr112 = new PhMethod(Phi.Φ, "number");')]
- //java[contains(text(), 'Phi rr113 = new PhMethod(Phi.Φ, "number");')]
- //java[contains(text(), 'rr11 = new PhWith(rr11, 0, rr111);')]
- //java[contains(text(), 'rr11 = new PhWith(rr11, 1, rr112);')]
- //java[contains(text(), 'rr11 = new PhWith(rr11, 2, rr113);')]
- //java[contains(text(), 'Phi rr11 = new PhDispatch(Phi.Φ, "tuple");')]
- //java[contains(text(), 'new PhDispatch(rr111, "empty");')]
- //java[contains(text(), 'Phi rr112 = new PhDispatch(Phi.Φ, "number");')]
- //java[contains(text(), 'Phi rr113 = new PhDispatch(Phi.Φ, "number");')]
- //java[contains(text(), 'rr11 = new PhApplication(rr11, new Bind(0, rr111)')]
- //java[contains(text(), 'new Bind(1, rr112)')]
- //java[contains(text(), 'new Bind(2, rr113)')]
input: |
# No comments.
[] > tuples
Expand Down
85 changes: 85 additions & 0 deletions eo-runtime/src/main/java/org/eolang/Bind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
* SPDX-License-Identifier: MIT
*/

package org.eolang;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
* A single attribute binding inside an application: a position or a name
* paired with the object being bound.
*
* <p>It is passed to {@link PhApplication} so that several bindings can be
* applied to one object at once, instead of nesting an application per
* binding. The binding knows how to attach itself to a target object and
* how to render its own φ-term fragment.</p>
*
* @since 0.60
*/
public final class Bind {

/**
* Attaches the bound object to a target.
*/
private final Consumer<Phi> command;

/**
* Renders the φ-term fragment of this binding.
*/
private final Supplier<String> term;

/**
* Ctor.
* @param pos The position
* @param obj The object to bind
*/
public Bind(final int pos, final Phi obj) {
this(
target -> target.put(pos, obj),
() -> String.format("%d->%s", pos, obj.φTerm())
);
}

/**
* Ctor.
* @param name The name of the attribute
* @param obj The object to bind
*/
public Bind(final String name, final Phi obj) {
this(
target -> target.put(name, obj),
() -> String.format("%s->%s", name, obj.φTerm())
);
}

/**
* Ctor.
* @param command Attaches the bound object to a target
* @param term Renders the φ-term fragment
*/
private Bind(final Consumer<Phi> command, final Supplier<String> term) {
this.command = command;
this.term = term;
}

/**
* Attach this binding to the given object.
* @param phi The object to bind into
*/
void attach(final Phi phi) {
this.command.accept(phi);
}

/**
* The φ-term fragment of this binding, like {@code 0->x} or {@code name->y}.
* @return The fragment
* @checkstyle MethodNameCheck (5 lines)
*/
@SuppressWarnings("PMD.MethodNamingConventions")
String φTerm() {

Check warning on line 82 in eo-runtime/src/main/java/org/eolang/Bind.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=objectionary_eo&issues=AZ5pKmiyNX90yzliMag0&open=AZ5pKmiyNX90yzliMag0&pullRequest=5172
return this.term.get();
}
}
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/org/eolang/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public interface Data {
* In auto-generated Java code we do:
* {@code
* Phi bytes = Phi.Φ.take("bytes").copy();
* Phi attached = new PhWith(bytes, 0, new byte[] {...});
* Phi attached = new PhApplication(bytes, 0, new byte[] {...});
* Phi str = Phi.Φ.take("string").copy();
* Phi applied = new PhWith(str, 0, attached);
* Phi applied = new PhApplication(str, 0, attached);
* return applied;
* }
*
Expand Down
130 changes: 130 additions & 0 deletions eo-runtime/src/main/java/org/eolang/PhApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
* SPDX-License-Identifier: MIT
*/

package org.eolang;

import java.nio.charset.StandardCharsets;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* A attr-putting object.
* @since 0.1
*/
public final class PhApplication extends PhOnce {

/**
* Matcher of raw bytes inside a φ-term, like {@code [D> 40-45-00]}.
*/
private static final Pattern DATA = Pattern.compile("\\[D> ([0-9A-F-]*)]");

/**
* Ctor.
* @param phi The object
* @param binds The bindings to apply, in order
*/
public PhApplication(final Phi phi, final Bind... binds) {
this(
() -> {
final Phi copy = phi.copy();
for (final Bind bind : binds) {
bind.attach(copy);
}
return copy;
},
() -> PhApplication.applied(phi, binds)
);
}

/**
* Ctor.
* @param phi The object
* @param pos The position
* @param attr The value
*/
public PhApplication(final Phi phi, final int pos, final Phi attr) {
this(phi, new Bind(pos, attr));
}

/**
* Ctor.
* @param phi The object
* @param name The name of attr
* @param attr The value
*/
public PhApplication(final Phi phi, final String name, final Phi attr) {
this(phi, new Bind(name, attr));
}

/**
* Ctor.
* @param sup Supplier of the wrapped object
* @param term Supplier of the φ-term
*/
private PhApplication(final Supplier<Phi> sup, final Supplier<String> term) {
super(sup, term);
}

/**
* The φ-term of an application, rendering a literal number or string
* construction as its value, otherwise listing the bindings.
* @param phi The receiver
* @param binds The bindings
* @return The φ-term
*/
private static String applied(final Phi phi, final Bind... binds) {
final String head = phi.φTerm();
final String body = PhApplication.body(binds);
final Matcher data = PhApplication.DATA.matcher(body);
final boolean literal = binds.length == 1 && data.find();
final String result;
if (literal && "Φ.string".equals(head)) {
result = String.format(
"\"%s\"", new String(PhApplication.bytes(data.group(1)), StandardCharsets.UTF_8)
);
} else if (literal && "Φ.number".equals(head)) {
result = PhDefault.numeral(new BytesOf(PhApplication.bytes(data.group(1))).asNumber());
} else {
result = String.format("%s(%s)", head, body);
}
return result;
}

/**
* Join the φ-term fragments of the bindings, comma-separated.
* @param binds The bindings
* @return The joined fragments
*/
private static String body(final Bind... binds) {
final StringBuilder out = new StringBuilder();
for (int idx = 0; idx < binds.length; ++idx) {
if (idx > 0) {
out.append(',');
}
out.append(binds[idx].φTerm());
}
return out.toString();
}

/**
* Parse a dash-separated hex string into bytes.
* @param hex The hex, like "40-45-00"
* @return The byte array
*/
private static byte[] bytes(final String hex) {
final byte[] bytes;
if (hex.isEmpty()) {
bytes = new byte[0];
} else {
final String[] parts = hex.split("-");
bytes = new byte[parts.length];
for (int idx = 0; idx < parts.length; ++idx) {
bytes[idx] = (byte) Integer.parseInt(parts[idx], 16);
}
}
return bytes;
}
}
Loading
Loading