Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 17, 2024
2 parents 036be9f + 9a4d3ef commit 878b3d5
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 102 deletions.
55 changes: 36 additions & 19 deletions eo-maven-plugin/src/main/java/org/eolang/maven/PhiMojo.java
Expand Up @@ -24,25 +24,29 @@
package org.eolang.maven;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.Text;
import org.cactoos.experimental.Threads;
import org.cactoos.io.ResourceOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.number.SumOf;
import org.cactoos.text.Sticky;
import org.cactoos.text.TextOf;
import org.eolang.maven.util.HmBase;
import org.eolang.maven.util.Home;
import org.eolang.maven.util.Rel;
import org.eolang.maven.util.Walk;
import org.eolang.parser.ParsingTrain;
import org.eolang.parser.Schema;

/**
* Read XMIR files and translate them to the phi-calculus expression.
Expand All @@ -59,15 +63,6 @@ public final class PhiMojo extends SafeMojo {
*/
public static final String EXT = "phi";

/**
* Translation to phi.
*/
private static final Text TRANSLATION = new Sticky(
new TextOf(
new ResourceOf("org/eolang/maven/phi/to-phi.xsl")
)
);

/**
* The directory where to take xmir files for translation from.
* @checkstyle MemberNameCheck (10 lines)
Expand All @@ -90,21 +85,40 @@ public final class PhiMojo extends SafeMojo {
)
private File phiOutputDir;

/**
* Pass XMIR to Optimizations train or not.
* This flag is used for test in order not to optimize XMIR twice:
* in {@link OptimizeMojo} and here.
* @checkstyle MemberNameCheck (5 lines)
*/
@SuppressWarnings("PMD.ImmutableField")
private boolean phiOptimize = true;

@Override
public void exec() {
final Home home = new HmBase(this.phiOutputDir);
final Train<Shift> train;
if (this.phiOptimize) {
train = new ParsingTrain();
} else {
train = new TrDefault<>();
}
final int count = new SumOf(
new Threads<>(
Runtime.getRuntime().availableProcessors(),
new Mapped<>(
xmir -> () -> {
final XML xml = new XMLDocument(
new TextOf(xmir).asString()
);
new Schema(xml).check();
final Path relative = Paths.get(
this.phiInputDir.toPath().relativize(xmir).toString().replace(
String.format(".%s", TranspileMojo.EXT),
String.format(".%s", PhiMojo.EXT)
)
);
home.save(PhiMojo.translated(new TextOf(xmir)), relative);
home.save(PhiMojo.translated(train, xml), relative);
Logger.info(
this,
"Translated to phi: %s -> %s",
Expand All @@ -131,13 +145,16 @@ count, new Rel(this.phiInputDir), new Rel(this.phiOutputDir)

/**
* Translate given xmir to phi calculus expression.
* @param train Train that optimize and traslates given xmir
* @param xmir Text of xmir
* @return Translated xmir
* @throws Exception If fail to translate
*/
private static String translated(final Text xmir) throws Exception {
return new XSLDocument(PhiMojo.TRANSLATION.asString()).applyTo(
new XMLDocument(xmir.asString())
);
private static String translated(final Train<Shift> train, final XML xmir) {
return new Xsline(
train.with(new StClasspath("/org/eolang/maven/phi/to-phi.xsl"))
)
.pass(xmir)
.xpath("phi/text()")
.get(0);
}
}
122 changes: 89 additions & 33 deletions eo-maven-plugin/src/main/resources/org/eolang/maven/phi/to-phi.xsl
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="to-phi" version="2.0">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:import href="/org/eolang/parser/_datas.xsl"/>
<!-- Variables -->
<xsl:variable name="aliases" select="program/metas/meta/part[last()]"/>
<xsl:variable name="xi">
Expand Down Expand Up @@ -73,13 +74,15 @@ SOFTWARE.
<select>∅</select>
</xsl:variable>
<!-- Functions -->
<!-- ADD XI OR NOT -->
<xsl:function name="eo:add-xi">
<xsl:param name="add"/>
<xsl:if test="$add">
<xsl:value-of select="$xi"/>
<xsl:text>.</xsl:text>
</xsl:if>
</xsl:function>
<!-- SPECIAL CHARACTERS -->
<xsl:function name="eo:specials">
<xsl:param name="n"/>
<xsl:param name="is-name"/>
Expand Down Expand Up @@ -138,6 +141,7 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!-- TOKENIZE BYTES -->
<xsl:function name="eo:bytes">
<xsl:param name="bts"/>
<xsl:choose>
Expand All @@ -158,44 +162,71 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<!-- COMMA WITH SPACE -->
<xsl:function name="eo:comma">
<xsl:param name="pos"/>
<xsl:param name="tabs"/>
<xsl:if test="$pos&gt;1">
<xsl:text>, </xsl:text>
<xsl:text>,</xsl:text>
<xsl:value-of select="eo:eol($tabs)"/>
</xsl:if>
</xsl:function>
<!-- EOL WITH INDENTATION -->
<xsl:function name="eo:eol">
<xsl:param name="tabs"/>
<xsl:value-of select="'&#10;'"/>
<xsl:for-each select="1 to $tabs">
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:function>
<!-- Program -->
<xsl:template match="program">
<xsl:text>{</xsl:text>
<xsl:variable name="has-package" select="metas/meta/head[text()='package']"/>
<xsl:variable name="parts" select="tokenize(metas/meta[head[text()='package']]/tail[1], '\.')"/>
<xsl:choose>
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
</xsl:for-each>
<xsl:apply-templates select="objects"/>
<xsl:for-each select="$parts">
<xsl:value-of select="eo:comma(2)"/>
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Package</xsl:text>
<xsl:value-of select="$rb"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="objects"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>}</xsl:text>
<phi>
<xsl:text>{</xsl:text>
<xsl:variable name="tabs" select="1"/>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:variable name="has-package" select="metas/meta/head[text()='package']"/>
<xsl:variable name="package" select="metas/meta[head[text()='package']]/tail[1]"/>
<xsl:variable name="parts" select="tokenize($package,'\.')"/>
<xsl:variable name="length" select="string-length($package)-string-length(replace($package,'\.',''))"/>
<xsl:choose>
<xsl:when test="$has-package">
<xsl:for-each select="$parts">
<xsl:value-of select="."/>
<xsl:value-of select="$arrow"/>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+position())"/>
</xsl:for-each>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs + $length + 1"/>
</xsl:apply-templates>
<xsl:for-each select="$parts">
<xsl:value-of select="eo:comma(2, $tabs + $length + 2 - position())"/>
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Package</xsl:text>
<xsl:value-of select="eo:eol($tabs + $length + 1 - position())"/>
<xsl:value-of select="$rb"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="objects">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="eo:eol(0)"/>
<xsl:text>}</xsl:text>
</phi>
</xsl:template>
<!-- Objects -->
<xsl:template match="objects">
<xsl:param name="tabs"/>
<xsl:for-each select="o">
<xsl:value-of select="eo:comma(position())"/>
<xsl:apply-templates select="."/>
<xsl:value-of select="eo:comma(position(), $tabs)"/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<!-- Free attribute -->
Expand Down Expand Up @@ -230,6 +261,7 @@ SOFTWARE.
</xsl:template>
<!-- Just object -->
<xsl:template match="o[@base]">
<xsl:param name="tabs"/>
<xsl:if test="@name">
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
Expand All @@ -251,63 +283,85 @@ SOFTWARE.
</xsl:choose>
<xsl:if test="count(o)&gt;0">
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:for-each select="o">
<xsl:apply-templates select="." mode="application">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:when>
<!-- Method -->
<xsl:otherwise>
<xsl:apply-templates select="o[position()=1]"/>
<xsl:apply-templates select="o[position()=1]">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
<xsl:value-of select="eo:specials(@base, true())"/>
<xsl:if test="count(o)&gt;1">
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:for-each select="o[position()!=1]">
<xsl:apply-templates select="." mode="application">
<xsl:with-param name="position" select="position()"/>
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
<!-- Data -->
<xsl:if test="@data">
<xsl:if test="not(@data='bytes')">
<xsl:message terminate="yes">
<xsl:text>Only 'bytes' is allowed as 'data' attribute to convert to phi-calculus expression. Given: </xsl:text>
<xsl:value-of select="@data"/>
</xsl:message>
</xsl:if>
<xsl:text>(</xsl:text>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:value-of select="$delta"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:value-of select="eo:bytes(.)"/>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:text>)</xsl:text>
</xsl:if>
</xsl:template>
<!-- Formation -->
<xsl:template match="o[not(@base) and (@abstract or @atom)]">
<xsl:param name="tabs"/>
<xsl:if test="@name">
<xsl:value-of select="eo:specials(@name, true())"/>
<xsl:value-of select="$arrow"/>
</xsl:if>
<xsl:value-of select="$lb"/>
<xsl:value-of select="eo:eol($tabs+1)"/>
<xsl:if test="@atom">
<xsl:value-of select="$lambda"/>
<xsl:value-of select="$dashed-arrow"/>
<xsl:text>Lambda</xsl:text>
<xsl:if test="count(o)&gt;0">
<xsl:text>, </xsl:text>
<xsl:value-of select="eo:comma(2, $tabs+1)"/>
</xsl:if>
</xsl:if>
<xsl:for-each select="o">
<xsl:value-of select="eo:comma(position())"/>
<xsl:apply-templates select="."/>
<xsl:value-of select="eo:comma(position(), $tabs+1)"/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:value-of select="eo:eol($tabs)"/>
<xsl:value-of select="$rb"/>
</xsl:template>
<!-- Application -->
<xsl:template match="o" mode="application">
<xsl:param name="tabs"/>
<xsl:param name="position" select="1"/>
<xsl:value-of select="eo:comma($position)"/>
<xsl:value-of select="eo:comma($position, $tabs)"/>
<xsl:choose>
<xsl:when test="@as">
<xsl:if test="matches(@as,'^[0-9][1-9]*$')">
Expand All @@ -321,7 +375,9 @@ SOFTWARE.
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$arrow"/>
<xsl:apply-templates select="."/>
<xsl:apply-templates select=".">
<xsl:with-param name="tabs" select="$tabs"/>
</xsl:apply-templates>
</xsl:template>
<!-- Ignore other elements -->
<xsl:template match="node()|@*">
Expand Down
Expand Up @@ -238,6 +238,7 @@ public <T extends AbstractMojo> FakeMaven execute(final Class<T> mojo) throws IO
this.params.putIfAbsent("objectionaries", new Objectionaries.Fake());
this.params.putIfAbsent("rewriteBinaries", true);
this.params.putIfAbsent("offline", false);
this.params.putIfAbsent("phiOptimize", false);
this.params.putIfAbsent(
"eoPortalDir",
new File("../eo-runtime/src/main/rust/eo")
Expand Down
13 changes: 11 additions & 2 deletions eo-maven-plugin/src/test/resources/org/eolang/maven/phi/atoms.yaml
Expand Up @@ -2,5 +2,14 @@ eo: |
[] > main /int
[] > outer
[] > inner /bytes
phi:
"{main ↦ ⟦λ ⤍ Lambda⟧, outer ↦ ⟦inner ↦ ⟦λ ⤍ Lambda⟧⟧}"
phi: |
{
main ↦ ⟦
λ ⤍ Lambda
⟧,
outer ↦ ⟦
inner ↦ ⟦
λ ⤍ Lambda
}

0 comments on commit 878b3d5

Please sign in to comment.