Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#765): Unroll Annotations Correctly #766

Merged
merged 6 commits into from
Oct 16, 2024
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
4 changes: 4 additions & 0 deletions src/it/phi-unphi/src/main/java/org/eolang/hone/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
* App.
* @since 0.1
*/
@Parameter("some-parameter")
public class App {

@Deprecated
@SuppressWarnings("unchecked")
public static void main(String[] args) {
double angle = 42.0;
double sin = Math.sin(angle);
Expand Down
35 changes: 35 additions & 0 deletions src/it/phi-unphi/src/main/java/org/eolang/hone/Parameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.hone;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Parameter {
String value();
}
18 changes: 14 additions & 4 deletions src/main/java/org/eolang/jeo/representation/CanonicalXmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,29 @@ private XML parse(final String eoprog) throws IOException {
private static XML unroll(final XML parsed) {
return new Xsline(
new TrJoined<Shift>(
new TrClasspath<>(
new TrClasspath<Shift>(
"/org/eolang/parser/wrap-method-calls.xsl"
).back(),
new TrDefault<>(
new TrDefault<Shift>(
new StEndless(
new StClasspath(
"/org/eolang/parser/roll-bases.xsl"
)
)
),
new TrClasspath<>(
new TrClasspath<Shift>(
"/org/eolang/parser/add-refs.xsl",
"/org/eolang/parser/vars-float-down.xsl"
"/org/eolang/parser/add-cuts.xsl"
).back(),
new TrDefault<Shift>(
new StEndless(
new StClasspath(
"/org/eolang/parser/vars-float-down.xsl"
)
)
),
new TrClasspath<Shift>(
"/org/eolang/parser/remove-cuts.xsl"
).back()
)
).pass(parsed);
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/eolang/jeo/representation/xmir/Parameter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.jeo.representation.xmir;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Parameter {
String value();
}
45 changes: 45 additions & 0 deletions src/main/resources/org/eolang/parser/add-cuts.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="add-refs" version="2.0">
<!--
Here we go through all objects which 'line' attribute value
exists as value of 'ref' attribute in some other object.
If the object is found, it gets 'cut' attribute to identify the
object to be removed later.
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[@name and @line=//o[@ref]/@ref]">
<xsl:copy>
<xsl:attribute name="cut"/>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
37 changes: 37 additions & 0 deletions src/main/resources/org/eolang/parser/remove-cuts.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="add-refs" version="2.0">
<!--
Here we go through all objects with 'cut' attribute and
remove them.
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="o[@cut]"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
26 changes: 18 additions & 8 deletions src/main/resources/org/eolang/parser/vars-float-down.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="vars-float-down" version="2.0">
<!--
The transformation does the opposite of "/org/eolang/parser/vars-float-up.xsl".
The transformation does almost the opposite of "/org/eolang/parser/vars-float-up.xsl".

At the level of EO it looks like:

[] > object [] > object
opcode > somebody seq > @
42 => opcode > somebody
seq > @ 42
somebody
somebody. opcode > somebody
42

In XMIR it works via "ref" and "line" attributes, it the transformation must
be applied after "/org/eolang/parser/add-refs.xsl"
Expand All @@ -43,17 +44,26 @@ SOFTWARE.
...
<o line="22" base="xyz" name="hello"/>

To achieve the right result, the transformation must be applied a several times.
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="//o[@ref and @ref!='']">
<xsl:variable name="reference" select="@ref"/>
<xsl:variable name="found" select="//o[@line=$reference]"/>
<xsl:copy-of select="$found"/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="o[@line=//o[@ref]/@ref and not(preceding-sibling::o[@ref=@line])]"/>
<xsl:template match="//o[@ref and @ref!='' and not(ancestor::o[@ref])]">
<xsl:variable name="reference" select="@ref"/>
<xsl:variable name="found" select="//o[@line=$reference]"/>
<xsl:element name="o">
<xsl:for-each select="$found/@*[name()!='cut']">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="$found/o">
<xsl:apply-templates select="."/>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Loading