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 13, 2023
2 parents 23ba909 + a2421b5 commit 7ad77c1
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 120 deletions.
66 changes: 66 additions & 0 deletions src/main/java/org/eolang/speco/ClearXmirSpeco.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Objectionary
*
* 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.speco;

import com.jcabi.xml.XML;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Xsline;
import java.io.IOException;

/**
* The class encapsulating specialization logic with the clearing of the resulting xmir.
*
* @since 0.0.3
*/
final class ClearXmirSpeco implements Speco {

/**
* Encapsulated speco.
*/
private final Speco origin;

/**
* Ctor.
*
* @param origin Encapsulated speco.
*/
ClearXmirSpeco(final Speco origin) {
this.origin = origin;
}

@Override
public XML transform(final XML xml) throws IOException {
return new Xsline(
new TrDefault<>(new StClasspath("/org/eolang/speco/clear.xsl"))
).pass(
new Xsline(
new TrDefault<Shift>().with(
new StClasspath("/org/eolang/parser/wrap-method-calls.xsl")
)
).pass(this.origin.transform(xml))
);
}
}
72 changes: 72 additions & 0 deletions src/main/java/org/eolang/speco/DefaultSpeco.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Objectionary
*
* 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.speco;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.StEndless;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.eolang.parser.Syntax;
import org.eolang.parser.XMIR;
import org.objectionary.aoi.launch.LauncherKt;

/**
* The class encapsulating specialization logic for xmir programs.
*
* @since 0.0.3
*/
final class DefaultSpeco implements Speco {

@Override
public XML transform(final XML xml) throws IOException {
return new Xsline(new TrDefault<Shift>()
.with(new StClasspath("/org/eolang/speco/1-1-coping.xsl"))
.with(new StEndless(new StClasspath("/org/eolang/speco/1-2-specialization.xsl")))
.with(new StClasspath("/org/eolang/speco/1-3-extension.xsl"))
.with(new StClasspath("/org/eolang/speco/2-1-substitute-applications.xsl"))
.with(new StClasspath("/org/eolang/speco/3-1-add-with.xsl"))
.with(new StClasspath("/org/eolang/speco/4-1-fence-tuples.xsl"))
.with(new StClasspath("/org/eolang/speco/5-1-substitute-fence.xsl"))
.with(new StClasspath("/org/eolang/speco/6-1-substitute-dominant.xsl"))
.with(new StClasspath("/org/eolang/speco/7-1-substitute-returned.xsl"))
).pass(
new Xsline(
new TrDefault<Shift>().with(
new StClasspath("/org/eolang/parser/wrap-method-calls.xsl")
)
).pass(xml)
);
}
}
107 changes: 107 additions & 0 deletions src/main/java/org/eolang/speco/EoWalk.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2022 Objectionary
*
* 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.speco;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.cactoos.io.InputOf;
import org.cactoos.io.OutputTo;
import org.eolang.parser.Syntax;
import org.eolang.parser.XMIR;
import org.objectionary.aoi.launch.LauncherKt;

/**
* The class encapsulating applying of specialization to EO.
*
* @since 0.0.3
*/
public final class EoWalk implements Walk {
/**
* Absolute path to the directory with input files.
*/
private final Path input;

/**
* Absolute path to the directory with output files.
*/
private final Path output;

/**
* Origin speco.
*/
private final Speco speco;

/**
* Ctor.
*
* @param input Absolute path to the directory with input files
* @param output Absolute path to the directory with output files
* @param speco Origin speco
*/
public EoWalk(
final Path input,
final Path output,
final Speco speco
) {
this.input = input;
this.output = output;
this.speco = speco;
}

@Override
public void exec() throws IOException {
Files.createDirectories(this.output);
for (final Path path : Files.newDirectoryStream(EoWalk.parse(this.input))) {
Files.write(
this.output.resolve(path.getFileName()),
new XMIR(this.speco.transform(Walk.toXml(path))).toEO().getBytes()
);
}
}

/**
* Takes source codes on EO, converts to xmir and applies the AOI tool.
*
* @param input Path of the source directory
* @return Path to the directory with the parsed files
* @throws IOException When Parsing EO fails
*/
private static Path parse(final Path input) throws IOException {
final StringBuilder name = new StringBuilder(input.toString());
final Path source = Path.of(name.append("_prs").toString());
FileUtils.copyDirectory(input.toFile(), source.toFile());
for (final Path path : Files.newDirectoryStream(source)) {
new Syntax(
"scenario",
new InputOf(String.format("%s%n", Files.readString(path))),
new OutputTo(new FileOutputStream(path.toFile()))
).parse();
}
LauncherKt.launch(source.toString());
return Path.of(name.append("_aoi").toString());
}
}
20 changes: 19 additions & 1 deletion src/main/java/org/eolang/speco/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,27 @@ public final class Main implements Callable<Integer> {
description = "If the input program is in EO")
private boolean eolang;

/**
* Flag indicating whether the temporary tags should be deleted.
*/
@CommandLine.Option(names = { "--clear-xmir" },
defaultValue = "false",
description = "If delete temporary tags")
private boolean clearxmir;

@Override
public Integer call() throws IOException {
new Speco(this.input, this.output, this.eolang).exec();
Speco speco = new DefaultSpeco();
if (this.clearxmir) {
speco = new ClearXmirSpeco(speco);
}
final Walk walk;
if (this.eolang) {
walk = new EoWalk(this.input, this.output, speco);
} else {
walk = new XmirWalk(this.input, this.output, speco);
}
walk.exec();
return 0;
}

Expand Down
Loading

1 comment on commit 7ad77c1

@0pdd
Copy link

@0pdd 0pdd commented on 7ad77c1 Apr 13, 2023

Choose a reason for hiding this comment

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

Puzzle 39-efbdd83b discovered in src/main/java/org/eolang/speco/Walk.java) and submitted as #109. 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.