Skip to content

Commit

Permalink
feat(objectionary#2555): fix + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Oct 27, 2023
1 parent 8e1cd27 commit 134efc7
Show file tree
Hide file tree
Showing 14 changed files with 272 additions and 119 deletions.
3 changes: 3 additions & 0 deletions eo-parser/src/main/java/org/eolang/parser/ParsingTrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public final class ParsingTrain extends TrEnvelope {
"each.xsl"
).with(new ClasspathSources(ParsingTrain.class));

/**
* Transform stars to tuples.
*/
private static final XSL TUPLES = new XSLDocument(
ParsingTrain.class.getResourceAsStream("stars-to-tuples.xsl"),
"stars-to-tuples.xsl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SOFTWARE.
<xsl:choose>
<xsl:when test="count(o)=0">
<xsl:choose>
<xsl:when test="following-sibling::o[@base!='.empty']">
<xsl:when test="following-sibling::o[position()=1 and @base!='.empty']">
<o>
<xsl:for-each select="@*[name()!='star' and name()!='name']">
<xsl:attribute name="{name()}">
Expand Down Expand Up @@ -61,7 +61,7 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="count(o)=1">
<xsl:when test="count(o[not(@method)])=1">
<o>
<xsl:for-each select="@*[name()!='star']">
<xsl:attribute name="{name()}">
Expand All @@ -70,22 +70,22 @@ SOFTWARE.
</xsl:for-each>
<o base="tuple"/>
<o base=".empty" method=""/>
<xsl:copy-of select="o[last()]"/>
<xsl:copy-of select="o"/>
</o>
</xsl:when>
<xsl:when test="count(o)&gt;1">
<xsl:when test="count(o[not(@method)])&gt;1">
<o>
<xsl:for-each select="@*[name()!='star']">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<o base="tuple" star="">
<xsl:for-each select="o[position()!=last()]">
<xsl:for-each select="o[not(@method)][last()]/preceding-sibling::o">
<xsl:copy-of select="."/>
</xsl:for-each>
</o>
<xsl:copy-of select="o[last()]"/>
<xsl:copy-of select="o[not(@method)][last()] | o[not(@method)][last()]/following-sibling::o"/>
</o>
</xsl:when>
</xsl:choose>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
xsls:
# []
- /org/eolang/parser/stars-to-tuples.xsl
- /org/eolang/parser/stars-to-tuples.xsl
- /org/eolang/parser/stars-to-tuples.xsl
tests:
- //objects[count(//objects/o[@base='tuple'])=4]
- //objects[count(//objects/o[@base='tuple'])=5]
- //o[@base='tuple' and @line=1 and not(@name)]
- //o[@base='.empty' and @name='xs']
- //o[@base='tuple' and @line=1 and not(@name)]/following-sibling::o[@base='.empty' and @name='xs']
- //o[@base='tuple' and @line=2 and @name='xl']/o[@base='tuple']
- //o[@base='tuple' and @line=2 and @name='xl']/o[@base='.empty' and @method]
- //o[@base='tuple' and @line=3]/o[@base='tuple' and @star]/o[@base='int']
- //o[@base='tuple' and @line=2 and @name='xl']/o[@base='int' and @line=2]
- //o[@base='tuple' and @line=3]/o[@base='tuple']/o[@base='tuple']
- //o[@base='tuple' and @line=3]/o[@base='tuple']/o[@base='.empty' and @method]
- //o[@base='tuple' and @line=3]/o[@base='tuple']/o[@base='int' and @line=3]
- //o[@base='tuple' and @line=3]/o[@base='int' and @line=3]
- //o[@base='tuple' and @line=3]/o[@base='int']
- //o[@base='tuple' and @line=4 and not(@star)]/following-sibling::o[@base='.empty' and @name='empty']
- //o[@base='tuple' and @line=5 and count(o)=4 and count(o[@method])=2]
- //o[@base='tuple' and @line=5]/o[@base='tuple' and count(o)=4 and count(o[@method])=2]
- //o[@base='tuple' and @line=5]/o[@base='tuple']/o[@base='tuple']
- //o[@base='tuple' and @line=5]/o[@base='tuple']/o[@base='tuple']/o[@base='.empty' and @method]
eo: |
* > xs
* 1 > xl
* 1 2
*.empty > empty
*
a.b.c
d.e.f
g.h.i
18 changes: 9 additions & 9 deletions eo-runtime/src/main/eo/org/eolang/switch.eo
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
#
# This object returns value of only first truly statement.
[cases...] > switch
[index cases] > check-case-at
cases.length > len!
[index] > case-at
if. > @
index.eq cases.length
index.eq ^.len
TRUE
if.
(cases.at index).at 0
(cases.at index).at 1
check-case-at
at.
^.cases.at index > case!
0
case.at 1
^.case-at
index.plus 1
cases
if. > @
cases.length.eq 0
error "switch cases are empty"
check-case-at
0
cases
case-at 0
20 changes: 15 additions & 5 deletions eo-runtime/src/main/eo/org/eolang/tuple.eo
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,22 @@

# Take one element from the tuple, at the given position.
[i] > at
^.length > len!
if. > index!
i.lt 0
len.plus i
i
if. > @
lt.
i
^.length.plus -1
^.head.at i
^.tail
or.
index.lt 0
index.gte len
error "Given index is out of tuple bounds"
if.
lt.
index
len.plus -1
^.head.at index
^.tail

# Create a new tuple with this element added to the end of it.
[x] > with
Expand Down
69 changes: 69 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOint$EOplus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* PLUS.
*
* @since 0.23
* @checkstyle TypeNameCheck (5 lines)
*/
@Versionized
@XmirObject(oname = "int.plus")
public class EOint$EOplus extends PhDefault {

/**
* Ctor.
* @param sigma Sigma
*/
public EOint$EOplus(final Phi sigma) {
super(sigma);
this.add("x", new AtFree());
this.add(
"φ",
new AtComposite(
this,
rho -> new Data.ToPhi(
Long.sum(
new Param(rho).strong(Long.class),
new Param(rho, "x").strong(Long.class)
)
)
)
);
}
}
67 changes: 67 additions & 0 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOint$EOtimes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 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.
*/

/*
* @checkstyle PackageNameCheck (4 lines)
*/
package EOorg.EOeolang;

import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Param;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* TIMES.
*
* @since 1.0
* @checkstyle TypeNameCheck (5 lines)
*/
@Versionized
@XmirObject(oname = "int.times")
public class EOint$EOtimes extends PhDefault {

/**
* Ctor.
* @param sigma Sigma
*/
public EOint$EOtimes(final Phi sigma) {
super(sigma);
this.add("x", new AtFree());
this.add(
"φ",
new AtComposite(
this,
rho -> new Data.ToPhi(
new Param(rho).strong(Long.class)
* new Param(rho, "x").strong(Long.class)
)
)
);
}
}
15 changes: 7 additions & 8 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOrust.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
import org.eolang.AtComposite;
import org.eolang.AtFree;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExFailure;
import org.eolang.ExNative;
import org.eolang.PhDefault;
import org.eolang.PhWith;
import org.eolang.Phi;
import org.eolang.Universe;
import org.eolang.UniverseDefault;
Expand Down Expand Up @@ -117,7 +117,7 @@ public class EOrust extends PhDefault {
.resolve("eo-test")
.resolve("Lib").toFile();
if (libs.isDirectory()) {
for (final File subdir: libs.listFiles()) {
for (final File subdir : libs.listFiles()) {
final Path path = subdir.toPath()
.resolve("target")
.resolve("debug")
Expand Down Expand Up @@ -160,11 +160,10 @@ public EOrust(final Phi sigma) {
byte[].class
);
}
final Phi portal = new Dataized(
rho
.attr("params").get()
.attr("Δ").get()
).take(Phi[].class)[0];
final Phi portal = new PhWith(
rho.attr("params").get().attr("at").get().copy(),
0, new Data.ToPhi(0L)
);
return this.translate(
(byte[]) method.invoke(
null,
Expand Down Expand Up @@ -245,7 +244,7 @@ private Phi translate(final byte[] message, final String insert) {
"Returned phi with vertex %d (%s in bytes) was not indexed",
vertex,
Arrays.toString(content)
)
)
);
}
break;
Expand Down
28 changes: 19 additions & 9 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOseq.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
import org.eolang.AtVararg;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.PhConst;
import org.eolang.PhDefault;
import org.eolang.PhWith;
import org.eolang.Phi;
import org.eolang.Versionized;
import org.eolang.XmirObject;

/**
* SEQ.
*
* @since 1.0
* @checkstyle TypeNameCheck (5 lines)
* @since 1.0
*/
@Versionized
@XmirObject(oname = "seq")
Expand All @@ -58,15 +59,24 @@ public EOseq(final Phi sigma) {
new AtComposite(
this,
self -> {
final Phi[] args = new Dataized(
self.attr("steps").get()
).take(Phi[].class);
for (int idx = 0; idx < args.length - 1; ++idx) {
new Dataized(args[idx]).take();
final Phi args = new PhConst(self.attr("steps").get());
final Long length = new Dataized(
args.attr("length").get()
).take(Long.class);
for (long idx = 0; idx < length - 1; ++idx) {
new Dataized(
new PhWith(
args.attr("at").get().copy(),
0, new Data.ToPhi(idx)
)
).take();
}
final Phi ret;
if (args.length > 0) {
ret = args[args.length - 1];
if (length > 0) {
ret = new PhWith(
args.attr("at").get().copy(),
0, new Data.ToPhi(length - 1)
);
} else {
ret = new Data.ToPhi(false);
}
Expand Down
7 changes: 6 additions & 1 deletion eo-runtime/src/test/eo/org/eolang/runtime-tests.eo
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,15 @@
build 2 > @
9

# @todo #2555:30min Enable the test when it's possible normalize object before application.
# Now this test does not work because we (arr.at 0).@ "returns" "if" from tuple.at which is
# wrong. Possibly we would need to extend EO syntax and phi calculus in order to be able to
# support such behavior.
[] > application-to-abstract-inside-tuple
[x] > add
x.plus 1 > @
* add > arr
eq. > @
eq. > res
(arr.at 0).@ 5
6
nop > @
Loading

0 comments on commit 134efc7

Please sign in to comment.