diff --git a/.rultor.yml b/.rultor.yml index 9254853608..43b966d16f 100644 --- a/.rultor.yml +++ b/.rultor.yml @@ -12,9 +12,9 @@ install: | pdd --file=/dev/null merge: script: | - mvn clean install -Peo -Pqulice --errors --settings ../settings.xml + mvn clean install -Peo -Pqulice --errors --settings ../settings.xml -Dstyle.color=never release: script: |- - mvn versions:set "-DnewVersion=${tag}" + mvn versions:set "-DnewVersion=${tag}" -Dstyle.color=never git commit -am "${tag}" - mvn clean deploy -Peo -Psonatype --errors --settings ../settings.xml + mvn clean deploy -Peo -Psonatype --errors --settings ../settings.xml -Dstyle.color=never diff --git a/README.md b/README.md index 8210ea51ba..9e7676ee7a 100644 --- a/README.md +++ b/README.md @@ -79,18 +79,18 @@ whether it's leap or not: [args] > main [y] > leap - or - and - eq (mod y 4) 0 - not (eq (mod y 100) 0) - eq (mod y 400) 0 - and + or. + and. + eq. (mod. y 4) 0 + not. (eq. (mod. y 100) 0) + eq. (mod. y 400) 0 + and. > @ stdout "Enter a year:" stdout - concat - (scanner stdin).nextInt > !year - " is a leap year?" - leap year:y + sprintf + "%d is %sa leap year!" + (scanner stdin).nextInt > year! + if (leap year:y) "" "not " ``` More examples are [here](https://github.com/yegor256/eo/tree/master/eo-maven-plugin/src/it). diff --git a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 index 56c009ac13..c0aef84b86 100644 --- a/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 +++ b/eo-compiler/src/main/antlr4/org/eolang/compiler/Program.g4 @@ -31,13 +31,11 @@ program license : (COMMENT EOL)+ - EOL ; metas : (META EOL)+ - EOL ; objects @@ -46,7 +44,6 @@ objects (COMMENT EOL)* object EOL - EOL? )+ ; @@ -67,10 +64,24 @@ object abstraction : + (COMMENT EOL)* LSQ - (NAME (SPACE NAME)*)? + (attribute (SPACE attribute)*)? RSQ - suffix? + (suffix (SPACE SLASH NAME)?)? + ; + +attribute + : + label + ; + +label + : + AT + | + NAME + DOTS? ; tail @@ -86,8 +97,9 @@ suffix SPACE ARROW SPACE + label + QUESTION? CONST? - NAME ; method @@ -148,8 +160,12 @@ head : AT | + SELF + | NAME | + NAME DOT + | data ; @@ -161,11 +177,13 @@ has data : + BYTES + | BOOL | STRING | - INTEGER + INT | FLOAT | @@ -177,9 +195,13 @@ data COMMENT: HASH ~[\r\n]*; META: PLUS NAME (SPACE ~[\r\n]+)?; +DOTS: '...'; CONST: '!'; +QUESTION: '?'; +SLASH: '/'; COLON: ':'; ARROW: '>'; +SELF: '$'; PLUS: '+'; MINUS: '-'; SPACE: ' '; @@ -192,7 +214,7 @@ AT: '@'; HASH: '#'; EOL : - [\r\n] + [\r\n]+ SPACE* { int tabs = getText().replaceAll("[\r\n]+", "").length() / 2; @@ -210,16 +232,15 @@ EOL } ; +fragment BYTE: [0-9A-F][0-9A-F] MINUS; +BYTES: BYTE (BYTE* [0-9A-F][0-9A-F])?; + BOOL: 'true' | 'false'; -CHAR: '\'' (LETTER | DIGIT) '\''; +CHAR: '\'' [0-9a-zA-Z] '\''; STRING: '"' ('\\"' | ~'"')* '"'; -INTEGER: (PLUS | MINUS)? DIGIT+; -FLOAT: (PLUS | MINUS)? DIGIT+ DOT DIGIT+; -HEX: '0x' (DIGIT | 'a'..'f')+; +INT: (PLUS | MINUS)? [0-9]+; +FLOAT: (PLUS | MINUS)? [0-9]+ DOT [0-9]+; +HEX: '0x' [0-9a-f]+; -NAME: LO (LETTER | DIGIT)*; +NAME: [a-z][a-z0-9_A-Z]*; -LETTER: (HI | LO); -HI: [A-Z]; -LO: [a-z]; -DIGIT: [0-9]; diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Pack.java b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java new file mode 100644 index 0000000000..bd7f35cf1f --- /dev/null +++ b/eo-compiler/src/main/java/org/eolang/compiler/Pack.java @@ -0,0 +1,105 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.compiler; + +import com.jcabi.xml.ClasspathSources; +import com.jcabi.xml.XSL; +import com.jcabi.xml.XSLDocument; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import org.cactoos.list.ListOf; +import org.cactoos.list.Mapped; + +/** + * Pack of all XSL sheets. + * + * @since 0.1 + */ +public final class Pack implements Iterable { + + /** + * All of them. + */ + private final Collection sheets; + + /** + * Ctor, with the default sequence of sheets. + */ + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public Pack() { + this( + new ListOf<>( + "errors/not-empty-atoms.xsl", + "errors/middle-varargs.xsl", + "errors/duplicate-names.xsl", + "errors/broken-aliases.xsl", + "errors/duplicate-aliases.xsl", + "errors/global-nonames.xsl", + "errors/same-line-names.xsl", + "errors/self-naming.xsl", + "add-refs.xsl", + "wrap-method-calls.xsl", + "vars-float-up.xsl", + "add-refs.xsl", + "resolve-aliases.xsl", + "add-default-package.xsl", + "errors/broken-refs.xsl", + "errors/unknown-names.xsl", + "errors/noname-attributes.xsl" + ) + ); + } + + /** + * Ctor. + * + * @param names Names of them in classpath + */ + public Pack(final Collection names) { + this.sheets = Collections.unmodifiableCollection(names); + } + + @Override + public Iterator iterator() { + return new Mapped<>( + doc -> new XSLDocument( + Program.class.getResourceAsStream(doc) + ).with(new ClasspathSources()), + this.sheets + ).iterator(); + } + + /** + * Make a copy, with an additional XSL. + * @param xsl Name in classpath + * @return New pack + */ + public Pack with(final String xsl) { + final Collection after = new LinkedList<>(this.sheets); + after.add(xsl); + return new Pack(after); + } +} diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Program.java b/eo-compiler/src/main/java/org/eolang/compiler/Program.java index a1c34ca15c..673ba9e6ba 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/Program.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/Program.java @@ -24,28 +24,18 @@ package org.eolang.compiler; import com.jcabi.log.Logger; -import com.jcabi.xml.XSLChain; +import com.jcabi.xml.ClasspathSources; +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import com.jcabi.xml.XSL; import com.jcabi.xml.XSLDocument; -import java.io.IOException; import java.nio.file.Path; -import org.antlr.v4.runtime.ANTLRErrorListener; -import org.antlr.v4.runtime.BaseErrorListener; -import org.antlr.v4.runtime.CharStreams; -import org.antlr.v4.runtime.CommonTokenStream; -import org.antlr.v4.runtime.RecognitionException; -import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.cactoos.Input; import org.cactoos.Output; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; -import org.cactoos.io.UncheckedInput; -import org.cactoos.list.ListOf; -import org.cactoos.list.Mapped; import org.cactoos.scalar.LengthOf; import org.cactoos.scalar.Unchecked; -import org.cactoos.text.TextOf; /** * Program. @@ -56,128 +46,135 @@ public final class Program { /** - * The name of it. + * XML to optimize. */ - private final String name; + private final XML source; /** - * Text to parse. - */ - private final Input input; - - /** - * Target to save XML to. + * Target to save XMLs to. */ private final Output target; /** * Ctor. * - * @param nme The name of it - * @param ipt Input text + * @param dom XML to optimize * @param file The file to write the XML to */ - public Program(final String nme, final Input ipt, final Path file) { - this(nme, ipt, new OutputTo(file)); + public Program(final XML dom, final Path file) { + this(dom, new OutputTo(file)); } /** * Ctor. * - * @param nme The name of it - * @param ipt Input text + * @param dom XML to optimize * @param tgt Target */ - public Program(final String nme, final Input ipt, final Output tgt) { - this.name = nme; - this.input = ipt; + public Program(final XML dom, final Output tgt) { + this.source = dom; this.target = tgt; } + /** + * Compile it to XMLs and save (with default set of XSLs). + */ + public void compile() { + this.compile(new Program.Spy.None()); + } + /** * Compile it to XML and save (with default set of XSLs). * - * @throws IOException If fails + * @param spy The spy */ - public void compile() throws IOException { - this.compile( - new ListOf<>( - "errors/broken-aliases.xsl", - "errors/duplicate-aliases.xsl", - "errors/one-body.xsl", - "errors/reserved-atoms.xsl", - "errors/same-line-names.xsl", - "errors/self-naming.xsl", - "01-add-refs.xsl", - "02-resolve-aliases.xsl", - "errors/unknown-names.xsl", - "03-abstracts-float-up.xsl", - "04-rename-bases.xsl", - "05-wrap-method-calls.xsl" - ) - ); + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void compile(final Program.Spy spy) { + this.compile(new Pack(), spy); } /** * Compile it to XML and save. * * @param xsls List of XSLs to apply - * @throws IOException If fails */ - public void compile(final Iterable xsls) throws IOException { - final String[] lines = new TextOf(this.input).asString().split("\n"); - final ANTLRErrorListener errors = new BaseErrorListener() { - // @checkstyle ParameterNumberCheck (10 lines) - @Override - public void syntaxError(final Recognizer recognizer, - final Object symbol, final int line, - final int position, final String msg, - final RecognitionException error) { - throw new CompileException( - String.format( - "[%d:%d] %s: \"%s\"", - line, position, msg, - // @checkstyle AvoidInlineConditionalsCheck (1 line) - lines.length < line ? "EOF" : lines[line - 1] - ), - error - ); - } - }; - final ProgramLexer lexer = - new ProgramLexer( - CharStreams.fromStream( - new UncheckedInput(this.input).stream() - ) - ); - lexer.removeErrorListeners(); - lexer.addErrorListener(errors); - final ProgramParser parser = - new ProgramParser( - new CommonTokenStream(lexer) + public void compile(final Iterable xsls) { + this.compile(xsls, new Program.Spy.None()); + } + + /** + * Compile it to XML and save. + * + * @param xsls List of XSLs to apply + * @param spy The spy + */ + public void compile(final Iterable xsls, final Program.Spy spy) { + final XSL each = new XSLDocument( + Program.class.getResourceAsStream("/org/eolang/compiler/_each.xsl") + ).with(new ClasspathSources()); + int index = 0; + XML before = this.source; + for (final XSL xsl : xsls) { + final XML after = each.with("step", index).transform( + xsl.transform(before) ); - parser.removeErrorListeners(); - parser.addErrorListener(errors); - final XeListener xel = new XeListener(this.name); - new ParseTreeWalker().walk(xel, parser.program()); + spy.push(index, xsl, after); + ++index; + before = after; + } new Unchecked<>( new LengthOf( new TeeInput( - new InputOf( - new XSLChain( - new Mapped<>( - node -> new XSLDocument( - Program.class.getResourceAsStream(node) - ), - xsls - ) - ).transform(xel.xml()).toString() - ), + new InputOf(before.toString()), this.target ) ) ).value(); - Logger.debug(this, "Input of %d EO lines compiled", lines.length); + } + + /** + * Spy. + * + * @since 0.1 + */ + public interface Spy { + /** + * New XSL produced. + * @param index The index of the XSL + * @param xsl The XSL used + * @param xml The XML produced + */ + void push(int index, XSL xsl, XML xml); + + /** + * Empty spy. + * + * @since 0.1 + */ + final class None implements Program.Spy { + @Override + public void push(final int index, final XSL xsl, final XML xml) { + // Nothing + } + } + + /** + * Empty spy. + * + * @since 0.1 + */ + final class Verbose implements Program.Spy { + @Override + public void push(final int index, final XSL xsl, final XML xml) { + Logger.debug( + this, + "Parsed #%d via %s\n%s", + index, + new XMLDocument(xsl.toString()).xpath("/*/@id").get(0), + xml + ); + } + } } } diff --git a/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java b/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java new file mode 100644 index 0000000000..45fc89b9bc --- /dev/null +++ b/eo-compiler/src/main/java/org/eolang/compiler/Syntax.java @@ -0,0 +1,150 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.compiler; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XML; +import java.io.IOException; +import java.nio.file.Path; +import org.antlr.v4.runtime.ANTLRErrorListener; +import org.antlr.v4.runtime.BaseErrorListener; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; +import org.antlr.v4.runtime.tree.ParseTreeWalker; +import org.cactoos.Input; +import org.cactoos.Output; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.io.UncheckedInput; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; +import org.cactoos.text.TextOf; + +/** + * Syntax parser. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class Syntax { + + /** + * The name of it. + */ + private final String name; + + /** + * Text to parse. + */ + private final Input input; + + /** + * Target to save XML to. + */ + private final Output target; + + /** + * Ctor. + * + * @param nme The name of it + * @param ipt Input text + * @param file The file to write the XML to + */ + public Syntax(final String nme, final Input ipt, final Path file) { + this(nme, ipt, new OutputTo(file)); + } + + /** + * Ctor. + * + * @param nme The name of it + * @param ipt Input text + * @param tgt Target + */ + public Syntax(final String nme, final Input ipt, final Output tgt) { + this.name = nme; + this.input = ipt; + this.target = tgt; + } + + /** + * Compile it to XML and save. + * + * @return XML parsed + * @throws IOException If fails + */ + public XML parse() throws IOException { + final String[] lines = new TextOf(this.input).asString().split("\n"); + final ANTLRErrorListener errors = new BaseErrorListener() { + // @checkstyle ParameterNumberCheck (10 lines) + @Override + public void syntaxError(final Recognizer recognizer, + final Object symbol, final int line, + final int position, final String msg, + final RecognitionException error) { + throw new CompileException( + String.format( + "[%d:%d] %s: \"%s\"", + line, position, msg, + // @checkstyle AvoidInlineConditionalsCheck (1 line) + lines.length < line ? "EOF" : lines[line - 1] + ), + error + ); + } + }; + final ProgramLexer lexer = + new ProgramLexer( + CharStreams.fromStream( + new UncheckedInput(this.input).stream() + ) + ); + lexer.removeErrorListeners(); + lexer.addErrorListener(errors); + final ProgramParser parser = + new ProgramParser( + new CommonTokenStream(lexer) + ); + parser.removeErrorListeners(); + parser.addErrorListener(errors); + final XeListener xel = new XeListener(this.name); + new ParseTreeWalker().walk(xel, parser.program()); + final XML dom = xel.xml(); + Logger.debug(this, "Raw XML:\n%s", dom.toString()); + new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(dom.toString()), + this.target + ) + ) + ).value(); + Logger.debug(this, "Input of %d EO lines compiled", lines.length); + return dom; + } + +} diff --git a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java index 5c302abf0a..17b1fd5fcb 100644 --- a/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java +++ b/eo-compiler/src/main/java/org/eolang/compiler/XeListener.java @@ -55,6 +55,11 @@ public final class XeListener implements ProgramListener { */ private final Directives dirs; + /** + * When we start. + */ + private final long start; + /** * Ctor. * @param nme Tha name of it @@ -62,6 +67,7 @@ public final class XeListener implements ProgramListener { public XeListener(final String nme) { this.name = nme; this.dirs = new Directives(); + this.start = System.nanoTime(); } /** @@ -83,12 +89,16 @@ public void enterProgram(final ProgramParser.ProgramContext ctx) { DateTimeFormatter.ISO_INSTANT ) ) + .add("listing").set(ctx.getText()).up() .add("errors").up(); } @Override public void exitProgram(final ProgramParser.ProgramContext ctx) { - this.dirs.up(); + this.dirs + // @checkstyle MagicNumber (1 line) + .attr("ms", (System.nanoTime() - this.start) / (1000L * 1000L)) + .up(); } @Override @@ -154,18 +164,43 @@ public void exitObject(final ProgramParser.ObjectContext ctx) { @Override public void enterAbstraction(final ProgramParser.AbstractionContext ctx) { this.dirs.add("o").attr("line", ctx.getStart().getLine()); - for (final TerminalNode attr : ctx.NAME()) { - this.dirs.add("o") - .attr("line", ctx.getStart().getLine()) - .attr("name", attr.getText()) - .up(); + if (ctx.SLASH() != null) { + this.dirs.attr("atom", ctx.NAME()); } this.dirs.up(); } @Override public void exitAbstraction(final ProgramParser.AbstractionContext ctx) { - // This method is created by ANTLR and can't be removed + // Nothing here + } + + @Override + public void enterAttribute(final ProgramParser.AttributeContext ctx) { + this.enter(); + this.dirs.add("o").attr("line", ctx.getStart().getLine()); + } + + @Override + public void exitAttribute(final ProgramParser.AttributeContext ctx) { + this.dirs.up().up(); + } + + @Override + public void enterLabel(final ProgramParser.LabelContext ctx) { + if (ctx.AT() != null) { + this.dirs.attr("name", ctx.AT().getText()); + } + if (ctx.NAME() != null) { + this.dirs.attr("name", ctx.NAME().getText()); + } + if (ctx.DOTS() != null) { + this.dirs.attr("vararg", ""); + } + } + + @Override + public void exitLabel(final ProgramParser.LabelContext ctx) { } @Override @@ -181,10 +216,12 @@ public void exitTail(final ProgramParser.TailContext ctx) { @Override public void enterSuffix(final ProgramParser.SuffixContext ctx) { this.enter(); - this.dirs.attr("name", ctx.NAME().getText()); if (ctx.CONST() != null) { this.dirs.attr("const", ""); } + if (ctx.QUESTION() != null) { + this.dirs.attr("rw", ""); + } } @Override @@ -195,6 +232,7 @@ public void exitSuffix(final ProgramParser.SuffixContext ctx) { @Override public void enterMethod(final ProgramParser.MethodContext ctx) { this.dirs.add("o") + .attr("method", "") .attr("line", ctx.getStart().getLine()) .attr("base", ctx.getText()).up(); } @@ -208,11 +246,18 @@ public void exitMethod(final ProgramParser.MethodContext ctx) { public void enterHead(final ProgramParser.HeadContext ctx) { this.dirs.add("o").attr("line", ctx.getStart().getLine()); if (ctx.NAME() != null) { - this.dirs.attr("base", ctx.NAME().getText()); + String base = ctx.NAME().getText(); + if (ctx.DOT() != null) { + base = String.format(".%s", base); + } + this.dirs.attr("base", base); } if (ctx.AT() != null) { this.dirs.attr("base", "@"); } + if (ctx.SELF() != null) { + this.dirs.attr("base", "$"); + } } @Override @@ -251,12 +296,16 @@ public void exitHtail(final ProgramParser.HtailContext ctx) { this.dirs.up(); } + // @checkstyle ExecutableStatementCountCheck (100 lines) @Override @SuppressWarnings("PMD.ConfusingTernary") public void enterData(final ProgramParser.DataContext ctx) { final String type; final String data; - if (ctx.BOOL() != null) { + if (ctx.BYTES() != null) { + type = "bytes"; + data = ctx.getText().replace("-", " ").trim(); + } else if (ctx.BOOL() != null) { type = "bool"; data = Boolean.toString(Boolean.parseBoolean(ctx.getText())); } else if (ctx.CHAR() != null) { @@ -265,18 +314,14 @@ public void enterData(final ProgramParser.DataContext ctx) { } else if (ctx.FLOAT() != null) { type = "float"; data = Float.toString(Float.parseFloat(ctx.getText())); - } else if (ctx.INTEGER() != null) { - type = "integer"; + } else if (ctx.INT() != null) { + type = "int"; data = Long.toString(Long.parseLong(ctx.getText())); } else if (ctx.HEX() != null) { - type = "hex"; - data = String.format( - "0x%s", - Long.toString( - // @checkstyle MagicNumberCheck (2 line) - Long.parseLong(ctx.getText().substring(2), 16), - 16 - ) + type = "int"; + data = Long.toString( + // @checkstyle MagicNumberCheck (1 line) + Long.parseLong(ctx.getText().substring(2), 16) ); } else if (ctx.STRING() != null) { type = "string"; @@ -284,6 +329,7 @@ public void enterData(final ProgramParser.DataContext ctx) { } else { throw new CompileException("Unknown data type"); } + this.dirs.attr("data", type); this.dirs.attr("base", type); this.dirs.set(data); } diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/01-add-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl similarity index 80% rename from eo-compiler/src/main/resources/org/eolang/compiler/01-add-refs.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl index 1d34a4d568..2217a784fd 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/01-add-refs.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_each.xsl @@ -23,16 +23,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - - + + - - - - - - - + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl new file mode 100644 index 0000000000..bda02203f3 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/_funcs.xsl @@ -0,0 +1,30 @@ + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl similarity index 74% rename from eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl index ed1a17c93f..e63e86044f 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/03-abstracts-float-up.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/abstracts-float-up.xsl @@ -22,11 +22,44 @@ 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. --> - + + - + + - + + + + + + + + + + + + $ + + + + @@ -52,19 +85,16 @@ SOFTWARE. + - - - $ - - - - - + - + + + + @@ -72,18 +102,12 @@ SOFTWARE. - - + - - - - - - + - + + + @@ -91,14 +115,10 @@ SOFTWARE. + - - - - $ - - + @@ -108,11 +128,13 @@ SOFTWARE. - - + - + + + + + . diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl similarity index 74% rename from eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl index a9aa2f7157..032cf89df5 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/05-wrap-method-calls.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-default-package.xsl @@ -22,17 +22,21 @@ 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. --> - + + - + - - - + + org.eolang. + + + - - diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/04-rename-bases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl similarity index 52% rename from eo-compiler/src/main/resources/org/eolang/compiler/04-rename-bases.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl index 0632102fd4..74090fcb7f 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/04-rename-bases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/add-refs.xsl @@ -22,34 +22,43 @@ 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. --> - + + - - - - - + - - - - Can't find ancestor of " - - " at line - - - - - - - + + + + + + + Attribute @line is absent at " + + " + + + + + Duplicate names inside " + + " + + + + + - - - - - + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl new file mode 100644 index 0000000000..038bb5e4c1 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/abstract-decoratee.xsl @@ -0,0 +1,49 @@ + + + + + + + + + + + + abstract-decoratee + + + + + Abstract object can't be used as a decoratee + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl index 32ed949515..879f128a42 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-aliases.xsl @@ -22,13 +22,17 @@ 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. --> - + + + + broken-aliases + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl new file mode 100644 index 0000000000..f3c6d99efd --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/broken-refs.xsl @@ -0,0 +1,83 @@ + + + + + + + + + + + broken-refs + + + + + The ref at " + + " is wrongly formatted: " + + " + + + + + + broken-refs + + + + + The ref at " + + " is empty + + + + + + + + broken-refs + + + + + The object " + + " is absent, but is referenced as " + + " + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl index d147063820..9253b65305 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-aliases.xsl @@ -22,7 +22,8 @@ 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. --> - + + @@ -34,6 +35,9 @@ SOFTWARE. + + duplicate-aliases + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl index 610c7f887c..54786d0e1d 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/duplicate-names.xsl @@ -22,20 +22,24 @@ 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. --> - + + - + - + + + duplicate-names + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl similarity index 71% rename from eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl index 4cb6ad23eb..e2b57c0d76 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/reserved-atoms.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/global-nonames.xsl @@ -22,22 +22,21 @@ 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. --> - + + - - - - - - - - You can't copy " - - " as a normal object - - + + + + global-nonames + + + + + The global object doesn't have a name + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl new file mode 100644 index 0000000000..2449e6d66b --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/middle-varargs.xsl @@ -0,0 +1,50 @@ + + + + + + + + + + + middle-varags + + + + + Varargs param + + must be the last one + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl similarity index 77% rename from eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl index 6881809e55..1624d63ca7 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/one-body.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/noname-attributes.xsl @@ -22,18 +22,23 @@ 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. --> - + + + - - + + - - + + + + noname-attributes + @@ -43,7 +48,7 @@ SOFTWARE. " - may have only one body + has attribute without a name diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl new file mode 100644 index 0000000000..c3d63e502b --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/not-empty-atoms.xsl @@ -0,0 +1,50 @@ + + + + + + + + + + + not-empty-atoms + + + + + Atoms + + may not have any attributes + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl index 39bd0767c0..3808cdc824 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/same-line-names.xsl @@ -22,7 +22,8 @@ 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. --> - + + @@ -35,6 +36,9 @@ SOFTWARE. + + same-line-names + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl index 5ea60abc4a..5341521255 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/self-naming.xsl @@ -22,12 +22,16 @@ 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. --> - + + + + self-naming + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl index d613960b23..6c8c107205 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/errors/unknown-names.xsl @@ -22,12 +22,16 @@ 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. --> - + + - + + + unknown-names + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl new file mode 100644 index 0000000000..6bcae052a1 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/globals-to-abstracts.xsl @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl new file mode 100644 index 0000000000..5b82df498d --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/remove-levels.xsl @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-objects/LICENSE.txt b/eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl similarity index 73% rename from eo-objects/LICENSE.txt rename to eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl index 0fad6fb4d6..a1f74d31b9 100644 --- a/eo-objects/LICENSE.txt +++ b/eo-compiler/src/main/resources/org/eolang/compiler/remove-refs.xsl @@ -1,3 +1,5 @@ + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/02-resolve-aliases.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl similarity index 86% rename from eo-compiler/src/main/resources/org/eolang/compiler/02-resolve-aliases.xsl rename to eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl index d360030680..9025cc17ff 100644 --- a/eo-compiler/src/main/resources/org/eolang/compiler/02-resolve-aliases.xsl +++ b/eo-compiler/src/main/resources/org/eolang/compiler/resolve-aliases.xsl @@ -22,7 +22,17 @@ 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. --> - + + @@ -50,7 +60,6 @@ SOFTWARE. - org.eolang. diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl new file mode 100644 index 0000000000..d0f72eff89 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/vars-float-up.xsl @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl new file mode 100644 index 0000000000..f97124aec8 --- /dev/null +++ b/eo-compiler/src/main/resources/org/eolang/compiler/wrap-method-calls.xsl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java index 6b1376925e..cf8f78dc3e 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/PacksTest.java @@ -23,7 +23,10 @@ */ package org.eolang.compiler; +import com.jcabi.log.Logger; import com.jcabi.matchers.XhtmlMatchers; +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; import java.io.ByteArrayOutputStream; import java.util.Collection; import java.util.Map; @@ -54,12 +57,21 @@ public void testPacks(final String pack) throws Exception { ) ); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( + new Syntax( pack, new InputOf(String.format("%s\n", map.get("eo"))), new OutputTo(baos) - ); - program.compile((Iterable) map.get("xsls")); + ).parse(); + final XML xml = new XMLDocument(baos.toString()); + baos.reset(); + final Program program = new Program(xml, new OutputTo(baos)); + final Collection xsls = (Collection) map.get("xsls"); + if (xsls == null) { + program.compile(new Program.Spy.Verbose()); + } else { + program.compile(new Pack(xsls), new Program.Spy.Verbose()); + } + Logger.debug(this, "Output XML:\n%s", baos.toString()); for (final String xpath : (Iterable) map.get("tests")) { MatcherAssert.assertThat( XhtmlMatchers.xhtml(baos.toString()), @@ -74,15 +86,22 @@ private static Collection yamlPacks() { "simple.yaml", "full-syntax.yaml", "catches-name-duplicates.yaml", + "catches-abstract-decoratee.yaml", "catches-alias-duplicates.yaml", + "catches-global-nonames.yaml", "catches-broken-aliases.yaml", + "catches-middle-vararg.yaml", + "catches-nonempty-atoms.yaml", "catches-unknown-names.yaml", "catches-self-naming.yaml", - "catches-reserved-atoms.yaml", - "catches-two-bodies.yaml", + "catches-noname-attrs.yaml", "catches-same-line-name.yaml", + "all-data-types.yaml", + "fixes-globals.yaml", "adds-refs.yaml", - "flatten-abstracts.yaml", + "adds-default-package.yaml", + "float-vars.yaml", + "float-abstracts.yaml", "resolves-aliases.yaml", "wraps-methods.yaml", "leap-year.yaml" diff --git a/eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java b/eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java similarity index 90% rename from eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java rename to eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java index 4578eccf30..8d03becb30 100644 --- a/eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java +++ b/eo-compiler/src/test/java/org/eolang/compiler/SyntaxTest.java @@ -38,17 +38,17 @@ * * @since 0.1 */ -public final class ProgramTest { +public final class SyntaxTest { @Test public void compilesSimpleCode() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( - "test-works", + final Syntax syntax = new Syntax( + "test", new ResourceOf("org/eolang/compiler/fibonacci.eo"), new OutputTo(baos) ); - program.compile(); + syntax.parse(); MatcherAssert.assertThat( XhtmlMatchers.xhtml( new String(baos.toByteArray()) @@ -62,12 +62,12 @@ public void failsOnBrokenSyntax() { Assertions.assertThrows( CompileException.class, () -> { - final Program program = new Program( - "broken-test", + final Syntax syntax = new Syntax( + "test-it", new InputOf("this code is definitely wrong"), new DeadOutput() ); - program.compile(); + syntax.parse(); } ); } diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml new file mode 100644 index 0000000000..27084d6dee --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-default-package.yaml @@ -0,0 +1,20 @@ +xsls: + - add-refs.xsl + - resolve-aliases.xsl + - add-default-package.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=1] + - //o[@base='org.eolang.and' and @line='7'] + - //o[@base='$'] +eo: | + +alias stdout org.eolang.io.stdout + +alias stdin org.eolang.io.stdin + +alias scanner org.eolang.txt.scanner + +foo Some other meta + + [args] > main + and + (scanner stdin).nextLine > line! + (stdout "You entered" line).print + $ diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml index 880a8af5fc..334216737e 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/adds-refs.yaml @@ -2,15 +2,19 @@ xsls: - errors/duplicate-names.xsl - errors/self-naming.xsl - errors/same-line-names.xsl - - 01-add-refs.xsl + - add-refs.xsl + - errors/broken-refs.xsl tests: - - /program/objects[count(o)=2] + - /program/errors[count(*)=0] + - /program/objects[count(o)=3] + - //objects[not(//o[@ref and @base='@'])] - //o[@base='x' and @ref='1'] - //o[@base='x' and @ref='4'] - //o[@base='x' and @line='11' and @ref='4'] - //o[@base='a' and @line='9' and @ref='12'] - //o[@base='something' and @line='12' and @name='a' and not(@ref)] - //o[@base='x' and @line='16' and @ref='14'] + - //o[@base='first' and @line='17' and @ref='1'] eo: | [x a] > first (stdout x) @@ -20,7 +24,7 @@ eo: | one x two - a + a > @ three x something > a @@ -28,3 +32,9 @@ eo: | [a] > x tt x 4 + first + + [] + one + [f] + 1 > ooo diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml new file mode 100644 index 0000000000..2b208f8e68 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/all-data-types.yaml @@ -0,0 +1,24 @@ +xsls: [] +tests: + - /program/errors[count(*)=0] + - //objects[count(o[@base='bytes'])=2] + - //o[@base='bytes' and @name='bytesX' and @data] + - //o[@base='bool' and @name='boolX' and @data] + - //o[@base='int' and @name='intX' and @data] + - //o[@base='float' and @name='floatX' and @data] + - //o[@base='string' and @name='stringX' and @data] + - //o[@base='char' and @name='charX' and @data] +eo: | + 01- > bytesX + + 00-02-4F-1A-FF-01 > bytesX + + 3.1415926 > floatX + + 0x1ffff > intX + + "Hello, друг! 谢谢!" > stringX + + 'z' > charX + + true > boolX diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml new file mode 100644 index 0000000000..71848adb5c --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-abstract-decoratee.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/abstract-decoratee.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='2'] +eo: | + [] > main + [] > @ + hello > test diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml new file mode 100644 index 0000000000..7f0d565645 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-global-nonames.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/global-nonames.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='3'] +eo: | + 3.14 > pi + + fake diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml new file mode 100644 index 0000000000..a470b21e54 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-middle-vararg.yaml @@ -0,0 +1,9 @@ +xsls: + - errors/middle-varargs.xsl +tests: + - /program/errors[count(error)=1] +eo: | + [x a... b] > bad + withoutaname + + [x a...] > good diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml index 00e5b79a1c..158425ed73 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-name-duplicates.yaml @@ -1,9 +1,11 @@ xsls: - errors/duplicate-names.xsl tests: - - /program/errors[count(error)=1] + - /program/errors[count(error)=2] - /program/errors/error[@line='2'] - /program/objects/o[@name='first']/o[@name='x'] eo: | [x] > first second > x + + 18 > first diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml similarity index 55% rename from eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml rename to eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml index 08ee6b74ea..dcbeca5fab 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-two-bodies.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-noname-attrs.yaml @@ -1,8 +1,9 @@ xsls: - - errors/one-body.xsl + - errors/noname-attributes.xsl tests: - - /program/errors[count(error)=1] + - /program/errors[count(error)=2] - /program/errors/error[@line='1'] + - /program/errors/error[@line='8'] - //o[@base='first' and @line='2'] - //o[@base='second' and @line='3'] eo: | @@ -12,3 +13,7 @@ eo: | third > hey 23 33 + + [args] > main + (stdout "Hello!").print + diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml new file mode 100644 index 0000000000..f07a790f66 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-nonempty-atoms.yaml @@ -0,0 +1,10 @@ +xsls: + - errors/not-empty-atoms.xsl +tests: + - /program/errors[count(error)=1] + - /program/errors/error[@line='3'] +eo: | + [] > test /int + + [] > foo /string + a > hey diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml deleted file mode 100644 index 327f80e231..0000000000 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-reserved-atoms.yaml +++ /dev/null @@ -1,10 +0,0 @@ -xsls: - - errors/reserved-atoms.xsl -tests: - - /program/errors[count(error)=2] -eo: | - [x] > first - string > x - "test" > works - float > test - 123 > string diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml index 6aa3fad816..ae9646590c 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/catches-unknown-names.yaml @@ -5,5 +5,6 @@ tests: - /program/errors/error[@line='2'] - //o[@base='test' and @line='2'] eo: | - [] - test + [] > fff + test > @ + $ > t diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml new file mode 100644 index 0000000000..23eb3e7ecd --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/fixes-globals.yaml @@ -0,0 +1,15 @@ +xsls: + - globals-to-abstracts.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=2] + - //objects/o[@name='pi'] + - //objects/o[@name='b'] + - //objects[count(o[@base])=0] +eo: | + 3.1415926 > pi + + a > b + c + d + "Hello, world!" diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml deleted file mode 100644 index b37a031a1a..0000000000 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/flatten-abstracts.yaml +++ /dev/null @@ -1,20 +0,0 @@ -xsls: - - 01-add-refs.xsl - - 03-abstracts-float-up.xsl - - 04-rename-bases.xsl -tests: - - /program/objects[count(o)=3] - - //o[@name='first'] - - //o[@name='first']/o[@base='test' and @as='foo'] - - //o[@name='first$second'] - - //o[@name='first$second$third'] - - //o[@name='first$second$third']/o[@base='stdout']/o[@base='a++' and @ref='1'] -eo: | - [a b] > first - test:foo - a > yes - [x b] > second - a > no - [b c] > third - no > yes - (stdout a b c x).print diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml new file mode 100644 index 0000000000..8f5af66aef --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-abstracts.yaml @@ -0,0 +1,42 @@ +xsls: + - remove-refs.xsl + - abstracts-float-up.xsl + - remove-levels.xsl + - add-refs.xsl + - errors/same-line-names.xsl + - errors/duplicate-names.xsl + - errors/broken-refs.xsl +tests: + - /program/objects[count(o)=9] + - /program/errors[count(*)=0] + - //objects[not(.//o[@name=''])] + - //o[@name='first$2$native' and @atom='int'] + - //o[@base='first$2$native' and @atom='int'] + - //o[@name='first'] + - //o[@name='first']/o[@base='test' and @as='foo'] + - //o[@name='first$2$second'] + - //o[@name='first$2$second$third'] + - //o[@name='first$2$second$third']/o[@base='stdout']/o[@base='a'] + - //o[@line='20.19' and @name='aa'] +eo: | + [a b] > first + test:foo + a > yes + [] > native /int + [x b] > second + a > no + [b c] > third + no > yes + (stdout a b c x).print + t + [] > third + "hello, world!" > msg + f + [] > third + "hello, world!" > msg + [] + "Failure" + + [aa] > ooo + [bbb] > fff + aa.test > a diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml new file mode 100644 index 0000000000..4db9b82989 --- /dev/null +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/float-vars.yaml @@ -0,0 +1,34 @@ +xsls: + - add-refs.xsl + - vars-float-up.xsl + - errors/duplicate-names.xsl +tests: + - /program/errors[count(*)=0] + - /program/objects[count(o)=2] + - //o[@base='input' and @name='t' and @line='6' and @const and count(o)=2] + - //objects[count(.//o[@base='string'])=1] + - //o[@name='y']/o[@name='t'] + - //o[@name='y']/o[@name='oops'] + - //o[@name='last' and not(@cut)] + - //objects[count(//o[@name='oops'])=1] + - //objects[count(//o[@cut='0'])=0] + - //o[@name='aaa' and not(@cut)] + - //o[@name='ooo' and not(@cut)] +eo: | + [] > x + first > ff + one + [] > y + two + input > t! + "Hello, world!" + [] > oops + 50 + three t + [] > last + three 1 + + [ppp] + one + [aaa] > kkk + 1 > ooo diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml index 7616c6e108..77b15dfde8 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/full-syntax.yaml @@ -3,16 +3,18 @@ tests: - /program/license[text()!=''] - /program/metas[count(meta)=3] - /program/metas/meta[head='foo' and tail=''] - - /program/objects[count(o)=5] + - /program/objects[count(o)=7] - //o[@base='bool'] - //o[@base='string'] - //o[@base='char' and text()='e'] - //o[@base='float' and text()='500.43'] - - //o[@base='hex' and text()='0x1f'] + - //o[@base='int' and text()='31'] - //o[@base='string' and text()='Hello, world!'] - //o[@name='hello' and @const] - //o[@as='i'] - //o[@base='.five'] + - //objects[not(.//o[@name=''])] + - //o[@atom and @name='atom' and count(o)=2 and o[@vararg and @name='a']] eo: | # The purpose of this test case is to make # sure all possible syntax scenarios can @@ -25,13 +27,20 @@ eo: | 500.43 > one # This is just a simple string - "Hello, world!" > !hello + "Hello, world!" > hello! + [tt a...] > atom /int + + # This is very good object [x] > first - second > hello - third:foo > x + second > hello? + $.add 5 > i + third:foo > x...?! f 12 false - (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > !a + (((t 22) r:t 8.54 "yes" 'e').print 88 0x1f):hey true > a! + # This object is also very good + [] + hey > you... kid f:u z @@ -51,3 +60,10 @@ eo: | test me now:i (f (f (f (f 1)))).f + + [] > ooo + # This is one + [] > o1 + + # This is two + [] > o2 diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml index 0b94345573..d73fb64ded 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/leap-year.yaml @@ -1,19 +1,7 @@ -xsls: - - errors/reserved-atoms.xsl - - errors/self-naming.xsl - - errors/same-line-names.xsl - - errors/one-body.xsl - - errors/duplicate-aliases.xsl - - errors/broken-aliases.xsl - - errors/duplicate-names.xsl - - 01-add-refs.xsl - - 02-resolve-aliases.xsl - - errors/unknown-names.xsl - - 03-abstracts-float-up.xsl - - 04-rename-bases.xsl - - 05-wrap-method-calls.xsl tests: - - /program/objects[count(o)=2] + - /program/errors[count(*)=0] + - /program/objects[count(o)=1] + - //o[@base='.and'] eo: | # This program is from the EOLANG original paper # and must produce a working command line tool @@ -26,15 +14,15 @@ eo: | [args] > main [y] > leap - or - and - eq (mod y 4) 0 - not (eq (mod y 100) 0) - eq (mod y 400) 0 - and + or. > @ + and. + eq. (mod. y 4) 0 + not. (eq. (mod. y 100) 0) + eq. (mod. y 400) 0 + and > @ stdout "Enter a year:" stdout - concat - (scanner stdin).nextInt > !year - " is a leap year?" - leap year:y + sprintf + "%s is %sa leap year!" + (scanner stdin).nextInt > year! + if (leap year:y) "" "not " diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml index ae3b2e2490..1dd395c989 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/resolves-aliases.yaml @@ -1,12 +1,14 @@ xsls: - - 01-add-refs.xsl - - 02-resolve-aliases.xsl + - add-refs.xsl + - resolve-aliases.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=1] - /program/metas/meta[head='foo' and @line='4'] - /program/metas[count(meta)=1] - //o[@base='org.eolang.io.stdout' and @line='9'] - - //o[@base='org.eolang.and' and @line='7'] + - //o[@base='and' and @line='7'] + - //o[@base='$'] eo: | +alias stdout org.eolang.io.stdout +alias stdin org.eolang.io.stdin @@ -15,5 +17,6 @@ eo: | [args] > main and - (scanner stdin).nextLine > !line + (scanner stdin).nextLine > line! (stdout "You entered" line).print + $ diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml index 43d8c6739b..c50dba8985 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/simple.yaml @@ -1,5 +1,6 @@ xsls: [] tests: + - /program/errors[count(*)=0] - /program[@name='simple.yaml'] - /program[@version != ''] - /program[@time != ''] diff --git a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml index 631a2f2e35..a5073a4d47 100644 --- a/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml +++ b/eo-compiler/src/test/resources/org/eolang/compiler/packs/wraps-methods.yaml @@ -1,15 +1,18 @@ xsls: - - 05-wrap-method-calls.xsl + - wrap-method-calls.xsl tests: + - /program/errors[count(*)=0] - /program/objects[count(o)=1] + - //objects[not(o[@method])] - //o[@base='sprintf' and count(./o)=3] - - //o[@base='.get' and count(./o)=2] - - //o[@base='.get']/o[1][@base='args'] - - //o[@base='.get']/o[2][@base='integer'] + - //o[@base='.foo' and count(./o)=1] + - //o[@base='.trim' and count(./o)=2] + - //o[@base='.trim']/o[1][@base='.get'] + - //o[@base='.trim']/o[2][@base='int'] eo: | [args] > app stdout sprintf "%dth Fibonacci number is %d" - (args.get 1) > !n + (args.get.trim 1).foo.tohex > n! fibo n diff --git a/eo-maven-plugin/pom.xml b/eo-maven-plugin/pom.xml index 96f987c8bb..59a7a70039 100644 --- a/eo-maven-plugin/pom.xml +++ b/eo-maven-plugin/pom.xml @@ -42,12 +42,6 @@ SOFTWARE. eo-compiler ${project.version} - - org.eolang - eo-runtime - ${project.version} - provided - org.antlr antlr4-runtime @@ -103,6 +97,11 @@ SOFTWARE. 3.0.1 provided + + com.jcabi + jcabi-matchers + test + @@ -155,8 +154,8 @@ SOFTWARE. qulice-maven-plugin - checkstyle:/src/it/.* - pmd:/src/it/.* + checkstyle:/src/it.* + pmd:/src/it.* duplicatefinder:.* xml:/src/test/resources/META-INF/maven/plugin.xml diff --git a/eo-maven-plugin/src/it/README.md b/eo-maven-plugin/src/it-disabled/README.md similarity index 100% rename from eo-maven-plugin/src/it/README.md rename to eo-maven-plugin/src/it-disabled/README.md diff --git a/eo-maven-plugin/src/it/fibonacci/invoker.properties b/eo-maven-plugin/src/it-disabled/fibonacci/invoker.properties similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/invoker.properties rename to eo-maven-plugin/src/it-disabled/fibonacci/invoker.properties diff --git a/eo-maven-plugin/src/it/fibonacci/pom.xml b/eo-maven-plugin/src/it-disabled/fibonacci/pom.xml similarity index 78% rename from eo-maven-plugin/src/it/fibonacci/pom.xml rename to eo-maven-plugin/src/it-disabled/fibonacci/pom.xml index d81288a1c7..b7412b2ffa 100644 --- a/eo-maven-plugin/src/it/fibonacci/pom.xml +++ b/eo-maven-plugin/src/it-disabled/fibonacci/pom.xml @@ -44,11 +44,33 @@ SOFTWARE. + parse + optimize + pull compile + + org.codehaus.mojo + exec-maven-plugin + + + test + + java + + + + + org.eolang.examples.Main + + 14 + 377 + + + diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/app.eo similarity index 83% rename from eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/app.eo index 39f16a77c1..d95296f324 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/app.eo +++ b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/app.eo @@ -26,8 +26,19 @@ +alias sprintf org.eolang.txt.sprintf [args] > app - stdout - sprintf - "%dth Fibonacci number is %d" - (args.get 1) > !n - fibo n + and + stdout + sprintf + "%dth Fibonacci number is %d" + (args.get 0) > n! + fibo n + if + not + eq + args.get 1 > e + n + [] + sprintf > msg + "%d not equal to %d" + n + e diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/eo/org/eolang/examples/fibonacci.eo diff --git a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/java/org/eolang/examples/Main.java similarity index 81% rename from eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java rename to eo-maven-plugin/src/it-disabled/fibonacci/src/main/java/org/eolang/examples/Main.java index ee77e956ef..1063e2d00e 100644 --- a/eo-maven-plugin/src/it/fibonacci/src/main/java/org/eolang/examples/Main.java +++ b/eo-maven-plugin/src/it-disabled/fibonacci/src/main/java/org/eolang/examples/Main.java @@ -24,12 +24,8 @@ package org.eolang.examples; -import org.eolang.EOarray; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; - /** - * Eo entry point. + * EO entry point. * * @since 0.1 */ @@ -43,7 +39,15 @@ public final class Main { public static void main(final String... args) throws Exception { new EOapp( new ArgsOf( - new Entry("args", new EOarray(new ArgsOf((Object[]) args))) + new Entry( + "args", + new EOarray( + new ArgsOf( + new Entry("01", Long.parseLong(args[0])), + new Entry("02", Long.parseLong(args[1])) + ) + ) + ) ) ).call(); } diff --git a/eo-maven-plugin/src/it/fibonacci/verify.groovy b/eo-maven-plugin/src/it-disabled/fibonacci/verify.groovy similarity index 100% rename from eo-maven-plugin/src/it/fibonacci/verify.groovy rename to eo-maven-plugin/src/it-disabled/fibonacci/verify.groovy diff --git a/eo-maven-plugin/src/it/settings.xml b/eo-maven-plugin/src/it-disabled/settings.xml similarity index 100% rename from eo-maven-plugin/src/it/settings.xml rename to eo-maven-plugin/src/it-disabled/settings.xml diff --git a/eo-maven-plugin/src/it/simple/invoker.properties b/eo-maven-plugin/src/it-disabled/simple/invoker.properties similarity index 100% rename from eo-maven-plugin/src/it/simple/invoker.properties rename to eo-maven-plugin/src/it-disabled/simple/invoker.properties diff --git a/eo-maven-plugin/src/it/simple/pom.xml b/eo-maven-plugin/src/it-disabled/simple/pom.xml similarity index 95% rename from eo-maven-plugin/src/it/simple/pom.xml rename to eo-maven-plugin/src/it-disabled/simple/pom.xml index c72892e76f..1e4ede170a 100644 --- a/eo-maven-plugin/src/it/simple/pom.xml +++ b/eo-maven-plugin/src/it-disabled/simple/pom.xml @@ -50,6 +50,9 @@ SOFTWARE. + parse + optimize + pull compile diff --git a/eo-maven-plugin/src/it/simple/src/main/eo/pixel.eo b/eo-maven-plugin/src/it-disabled/simple/src/main/eo/pixel.eo similarity index 100% rename from eo-maven-plugin/src/it/simple/src/main/eo/pixel.eo rename to eo-maven-plugin/src/it-disabled/simple/src/main/eo/pixel.eo diff --git a/eo-maven-plugin/src/it/simple/verify.groovy b/eo-maven-plugin/src/it-disabled/simple/verify.groovy similarity index 100% rename from eo-maven-plugin/src/it/simple/verify.groovy rename to eo-maven-plugin/src/it-disabled/simple/verify.groovy diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java index 30fccf22bc..340e62a8aa 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CompileMojo.java @@ -24,13 +24,15 @@ package org.eolang.maven; import com.jcabi.log.Logger; +import com.jcabi.xml.ClasspathSources; +import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; -import java.io.ByteArrayOutputStream; +import com.jcabi.xml.XSLDocument; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; +import java.nio.file.Paths; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -40,12 +42,13 @@ import org.apache.maven.project.MavenProject; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; +import org.cactoos.io.ResourceOf; import org.cactoos.io.TeeInput; import org.cactoos.scalar.IoChecked; import org.cactoos.scalar.LengthOf; import org.cactoos.text.FormattedText; +import org.cactoos.text.TextOf; import org.cactoos.text.UncheckedText; -import org.eolang.compiler.Program; import org.slf4j.impl.StaticLoggerBinder; /** @@ -73,8 +76,7 @@ public final class CompileMojo extends AbstractMojo { * @checkstyle MemberNameCheck (7 lines) */ @Parameter( - required = false, - readonly = false, + required = true, defaultValue = "${project.build.directory}/generated-sources/eo" ) private transient File generatedDir; @@ -84,34 +86,20 @@ public final class CompileMojo extends AbstractMojo { * @checkstyle MemberNameCheck (7 lines) */ @Parameter( - required = false, - readonly = false, + required = true, defaultValue = "${project.build.directory}" ) private transient File targetDir; - /** - * Directory in which .eo files are located. - * @checkstyle MemberNameCheck (7 lines) - */ - @Parameter( - required = false, - readonly = false, - defaultValue = "${project.basedir}/src/main/eo" - ) - private transient File sourcesDirectory; - @Override public void execute() throws MojoFailureException { StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); if (this.generatedDir.mkdirs()) { Logger.info(this, "Gen directory created: %s", this.generatedDir); } - if (this.targetDir.mkdirs()) { - Logger.info(this, "Target directory created: %s", this.targetDir); - } + final Path dir = this.targetDir.toPath().resolve("eo/optimize"); try { - Files.walk(this.sourcesDirectory.toPath()) + Files.walk(dir) .filter(file -> !file.toFile().isDirectory()) .forEach(this::compile); } catch (final IOException ex) { @@ -119,7 +107,7 @@ public void execute() throws MojoFailureException { new UncheckedText( new FormattedText( "Can't list EO files in %s", - this.sourcesDirectory + dir ) ).asString(), ex @@ -135,56 +123,43 @@ public void execute() throws MojoFailureException { } /** - * Compile one EO file. - * @param file EO file + * Compile one XML file. + * + * @param file XML file */ private void compile(final Path file) { - final String name = file.toString().substring( - this.sourcesDirectory.toString().length() + 1 - ); + final Path temp = this.targetDir.toPath().resolve("eo/compile"); try { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new Program( - name, - new InputOf(file), - new OutputTo(baos) - ).compile(new ArrayList<>(0)); - final String xml = String.format("%s.xml", name); + final XML xml = new XMLDocument(file); + final String name = xml.xpath("/program/@name").get(0); + final XML out = new XSLDocument( + new TextOf( + new ResourceOf("org/eolang/maven/to-java.xsl") + ).asString() + ).with(new ClasspathSources()).transform(xml); + Logger.debug(this, "Raw Java output of %s:\n%s", file, out); new IoChecked<>( new LengthOf( new TeeInput( - new InputOf(baos.toString()), + new InputOf(out.toString()), new OutputTo( - this.targetDir.toPath() - .resolve("eo-compiler-raw") - .resolve(xml) + temp.resolve(String.format("%s.xml", name)) ) ) ) ).value(); - baos.reset(); - new Program( - name, - new InputOf(file), - new OutputTo(baos) - ).compile(); - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(baos.toString()), - new OutputTo( - this.targetDir.toPath() - .resolve("eo-compiler") - .resolve(xml) + for (final XML java : out.nodes("/program/objects/o[java]")) { + CompileMojo.save( + this.generatedDir.toPath().resolve( + Paths.get( + String.format( + "%s.java", java.xpath("@java-name").get(0) + ) ) - ) - ) - ).value(); - new ToJava( - new XMLDocument(baos.toString()), - this.generatedDir.toPath(), - this.targetDir.toPath().resolve("eo-to-java") - ).compile(); + ), + java.xpath("java/text()").get(0) + ); + } } catch (final IOException ex) { throw new IllegalStateException( new UncheckedText( @@ -199,4 +174,28 @@ private void compile(final Path file) { Logger.info(this, "%s compiled to %s", file, this.generatedDir); } + /** + * Save one Java file. + * @param path The path + * @param content The content + * @throws IOException If fails + */ + private static void save(final Path path, final String content) + throws IOException { + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(content), + path + ) + ) + ).value(); + Logger.info( + CompileMojo.class, + "Saved %d chars to %s", + content.length(), + path.toAbsolutePath() + ); + } + } diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java new file mode 100644 index 0000000000..ea0d1ccb87 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java @@ -0,0 +1,175 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.eolang.compiler.Pack; +import org.eolang.compiler.Program; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Optimize XML files. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "optimize", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class OptimizeMojo extends AbstractMojo { + + /** + * From directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + final Path dir = this.targetDir.toPath().resolve("eo/parse"); + try { + Files.walk(dir) + .filter(file -> !file.toFile().isDirectory()) + .forEach(file -> this.optimize(dir, file)); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list XML files in %s", + dir + ) + ).asString(), + ex + ); + } + } + + /** + * Optimize XML file after parsing. + * + * @param home Where it was found + * @param file EO file + */ + private void optimize(final Path home, final Path file) { + final String name = file.toString().substring( + home.toString().length() + 1 + ); + final Path dir = this.targetDir.toPath() + .resolve("eo/steps") + .resolve(name); + try { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Program(new XMLDocument(file), new OutputTo(baos)).compile( + new Pack() + .with("globals-to-abstracts.xsl") + .with("remove-refs.xsl") + .with("abstracts-float-up.xsl") + .with("remove-levels.xsl") + .with("add-refs.xsl") + .with("errors/broken-refs.xsl"), + new TargetSpy(dir) + ); + final Path target = this.targetDir.toPath() + .resolve("eo/optimize") + .resolve(name); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(baos.toString()), + new OutputTo(target) + ) + ) + ).value(); + Logger.info( + this, "%s optimized to %s, all steps are in %s", + file, target, dir + ); + Logger.debug( + this, "Optimized XML saved to %s:\n%s", + target, baos.toString() + ); + final List errors = new XMLDocument(baos.toString()) + .nodes("/program/errors/error"); + for (final XML error : errors) { + Logger.error( + this, + "[%s:%s] %s (%s:%s)", + name, + error.xpath("@line").get(0), + error.xpath("text()").get(0), + error.xpath("@check").get(0), + error.xpath("@step").get(0) + ); + } + if (!errors.isEmpty()) { + throw new IllegalStateException( + String.format( + "There are %d errors in %s, see log above", + errors.size(), file + ) + ); + } + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't compile %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java new file mode 100644 index 0000000000..96f6fea6b2 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/ParseMojo.java @@ -0,0 +1,146 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import com.jcabi.log.Logger; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.eolang.compiler.Syntax; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Parse EO to XML. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "parse", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class ParseMojo extends AbstractMojo { + + /** + * Target directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + /** + * Directory in which .eo files are located. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.basedir}/src/main/eo" + ) + private transient File sourcesDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + if (this.targetDir.mkdirs()) { + Logger.info(this, "Target directory created: %s", this.targetDir); + } + try { + Files.walk(this.sourcesDir.toPath()) + .filter(file -> !file.toFile().isDirectory()) + .forEach(this::parse); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list EO files in %s", + this.sourcesDir + ) + ).asString(), + ex + ); + } + } + + /** + * Parse EO file to XML. + * @param file EO file + */ + private void parse(final Path file) { + final String name = file.toString().substring( + this.sourcesDir.toString().length() + 1 + ); + final String xml = String.format("%s.xml", name); + final Path path = this.targetDir.toPath() + .resolve("eo/parse") + .resolve(xml); + try { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new Syntax( + name, + new InputOf(file), + new OutputTo(baos) + ).parse(); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(baos.toString()), + new OutputTo(path) + ) + ) + ).value(); + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't parse %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + Logger.info(this, "%s parsed to %s", file, path); + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java new file mode 100644 index 0000000000..9754287b6a --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/PullMojo.java @@ -0,0 +1,152 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XMLDocument; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.IoChecked; +import org.cactoos.scalar.LengthOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; +import org.slf4j.impl.StaticLoggerBinder; + +/** + * Pull EO XML files. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@Mojo( + name = "pull", + defaultPhase = LifecyclePhase.GENERATE_SOURCES, + threadSafe = true, + requiresDependencyResolution = ResolutionScope.COMPILE +) +public final class PullMojo extends AbstractMojo { + + /** + * From directory. + * @checkstyle MemberNameCheck (7 lines) + */ + @Parameter( + required = true, + defaultValue = "${project.build.directory}" + ) + private transient File targetDir; + + @Override + public void execute() throws MojoFailureException { + StaticLoggerBinder.getSingleton().setMavenLog(this.getLog()); + final Path dir = this.targetDir.toPath().resolve("eo/optimize"); + try { + Files.walk(dir) + .filter(file -> !file.toFile().isDirectory()) + .forEach(this::pull); + } catch (final IOException ex) { + throw new MojoFailureException( + new UncheckedText( + new FormattedText( + "Can't list XML files in %s", + dir + ) + ).asString(), + ex + ); + } + } + + /** + * Pull all deps found in XML file. + * + * @param file EO file + */ + private void pull(final Path file) { + try { + final String xpath = String.join( + "", + "//o[@base and contains(@base, '.') ", + "and not(starts-with(@base, '.'))]/@base" + ); + for (final String name : new XMLDocument(file).xpath(xpath)) { + this.pull(name); + } + } catch (final IOException ex) { + throw new IllegalStateException( + new UncheckedText( + new FormattedText( + "Can't pull %s into %s", + file, this.targetDir + ) + ).asString(), + ex + ); + } + } + + /** + * Pull one dep. + * + * @param name Name of the object, e.g. "org.eolang.io.stdout" + * @throws IOException If fails + */ + private void pull(final String name) throws IOException { + final String res = String.format("/%s.eo.xml", name.replace(".", "/")); + final InputStream input = this.getClass().getResourceAsStream(res); + if (input == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" in classpath", + res + ) + ); + } + final Path path = this.targetDir.toPath() + .resolve("eo/pull") + .resolve(String.format("%s.eo.xml", name.replace(".", "/"))); + new IoChecked<>( + new LengthOf( + new TeeInput( + new InputOf(input), + new OutputTo(path) + ) + ) + ).value(); + Logger.info(this, "%s pulled to %s", name, path); + } + +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java new file mode 100644 index 0000000000..d0fbbb2b26 --- /dev/null +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/TargetSpy.java @@ -0,0 +1,83 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import com.jcabi.log.Logger; +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import com.jcabi.xml.XSL; +import java.nio.file.Path; +import java.util.List; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.Unchecked; +import org.eolang.compiler.Program; + +/** + * The spy to log all results. + * + * @since 0.1 + */ +final class TargetSpy implements Program.Spy { + /** + * The dir. + */ + private final Path dir; + + /** + * Ctor. + * @param target The path + */ + TargetSpy(final Path target) { + this.dir = target; + } + + @Override + public void push(final int index, final XSL xsl, final XML xml) { + final List names = new XMLDocument( + xsl.toString() + ).xpath("/*/@id"); + final String file; + if (names.isEmpty()) { + file = String.format("%d", index); + } else { + file = names.get(0).replaceAll("[^a-z0-9]", "-"); + } + new Unchecked<>( + new LengthOf( + new TeeInput( + new InputOf(xml.toString()), + new OutputTo( + this.dir.resolve( + String.format("%02d-%s.xml", index, file) + ) + ) + ) + ) + ).value(); + Logger.debug(this, "Step #%d by %s:\n%s", index, file, xml); + } +} diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java b/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java deleted file mode 100644 index ae13831ba0..0000000000 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/ToJava.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.maven; - -import com.jcabi.log.Logger; -import com.jcabi.xml.XML; -import com.jcabi.xml.XSLDocument; -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import org.cactoos.io.InputOf; -import org.cactoos.io.OutputTo; -import org.cactoos.io.TeeInput; -import org.cactoos.scalar.IoChecked; -import org.cactoos.scalar.LengthOf; - -/** - * ToJava. - * - * @since 0.1 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -public final class ToJava { - - /** - * The XML with EO parsed already. - */ - private final XML xml; - - /** - * Directory to save Java files to. - */ - private final Path dir; - - /** - * Directory to save temp files to. - */ - private final Path temp; - - /** - * Ctor. - * - * @param input Input text - * @param file The file to write the XML to - * @param tmp Temp dir - */ - public ToJava(final XML input, final Path file, final Path tmp) { - this.xml = input; - this.dir = file; - this.temp = tmp; - } - - /** - * Compile it to XML and save. - * @throws IOException If fails - */ - public void compile() throws IOException { - final XML out = new XSLDocument( - ToJava.class.getResourceAsStream("to-java.xsl") - ).transform(this.xml); - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(out.toString()), - new OutputTo( - this.temp.resolve( - String.format( - "%s.xml", - this.xml.xpath("/program/@name").get(0) - ) - ) - ) - ) - ) - ).value(); - for (final XML error : out.nodes("/program/errors/error")) { - Logger.error( - this, - "[%s] %s", - error.xpath("@line").get(0), - error.xpath("text()").get(0) - ); - } - for (final XML file : out.nodes("/program/objects/o[java]")) { - ToJava.save( - this.dir.resolve( - Paths.get( - String.format( - "%s.java", file.xpath("@name").get(0) - ) - ) - ), - file.xpath("java/text()").get(0) - ); - } - } - - /** - * Save one Java file. - * @param path The path - * @param content The content - * @throws IOException If fails - */ - private static void save(final Path path, final String content) - throws IOException { - new IoChecked<>( - new LengthOf( - new TeeInput( - new InputOf(content), - path - ) - ) - ).value(); - Logger.info( - ToJava.class, - "Saved %d chars to %s", - content.length(), - path.toAbsolutePath() - ); - } - -} diff --git a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl index c88e90e94b..ea9778cac8 100644 --- a/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl +++ b/eo-maven-plugin/src/main/resources/org/eolang/maven/to-java.xsl @@ -23,13 +23,37 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -51,246 +75,264 @@ SOFTWARE. - + + + + + + + origin + + + + + - - + + + + + + + + + - - - - + - + import org.eolang.*; - - import org.eolang.sys.*; - - + + public final class - - implements Phi - - { - - - private final Args args; - - - public - - (final Args a) { - - - - - assert a.has(" - - "); - - - - - this.args = a; - - - } - - - - + extends Phi { + + } - - - /* - - * This file was auto-generated by eolang-maven-plugin - - * on - - . Don't edit it, - - * your changes will be discarded on the next build. - - * The sources were compiled to XML on - - * - - by the EO compiler v. - - . - - */ - - + + + public + + () { + + super( + + + , + + " + + " + + ); + + + + + + + + }; + - - package - - ; - - + + + this.put(" + + ", () -> { + + + + + + + + + return ret; + + }); + - - + + + + + + + final Phi + + = - - public Object - - (final Args a) + + this + + + new + + () + + + this.get(" + + ") + + .copy() + - @Override - - - public Object call() throws Exception + new + + () - { - - - - - Object - - ; - - - - - return - - - - - - ; - - - } - - - - this.args.get(" - - ") - - - - - - = - - new Call(" - - ", - - - - + + + + - , new ArgsOf( - - - - - - - + + + - )) - + - - - - - - - = - - new - - (new ArgsOf( - - a - - , - - - - - - - + + + + + _base + - - - - - )) + + final Phi + + = + + _base.get(" + + ").copy(); + + + + + + - + - - - new Entry(" - - - - - - - ", + + + + + + _ + + + - + - ) - - , - - - - + + + + .inherit(this); + + + + + + .put( + + + " + + " + + + + + + , () -> + + _ + + ); + - - - L + + + + + + .put("data", () -> new Data.Value< + + ); + - + + String>(" - L + ") - + + Integer>( + ) - - " + + Boolean>( - " + ) - - ' + + Double>( - ' + ) - - + + package + + ; + + + + + /* + + * This file was auto-generated by eolang-maven-plugin + + * on + + . Don't edit it, + + * your changes will be discarded on the next build. + + * The sources were compiled to XML on + + * + + by the EO compiler v. + + . + + */ + + diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java index afe1bdd8b9..85e6316e91 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CompileMojoTest.java @@ -23,20 +23,20 @@ */ package org.eolang.maven; +import com.jcabi.log.Logger; import java.nio.file.Files; import java.nio.file.Path; -import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.cactoos.Input; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; +import org.cactoos.io.ResourceOf; import org.cactoos.io.TeeInput; import org.cactoos.scalar.LengthOf; -import org.eolang.compiler.CompileException; +import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; /** * Test case for {@link CompileMojo}. @@ -45,65 +45,65 @@ * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") -public final class CompileMojoTest extends AbstractMojoTestCase { - - /** - * Temp dir for tests. - * @checkstyle VisibilityModifierCheck (4 lines) - */ - @TempDir - public Path temp; +public final class CompileMojoTest { @Test public void testSimpleCompilation() throws Exception { - final CompileMojo mojo = new CompileMojo(); - final Path src = this.temp.resolve("src"); - this.setVariableValueToObject(mojo, "sourcesDirectory", src.toFile()); - new LengthOf( - new TeeInput( - new InputOf( - "[args] > main\n (stdout \"Hello!\").print\n" - ), - new OutputTo(src.resolve("main.eo")) - ) - ).value(); - final Path target = this.temp.resolve("target"); - this.setVariableValueToObject( - mojo, "targetDir", target.toFile() - ); - final Path generated = this.temp.resolve("generated"); - this.setVariableValueToObject( - mojo, "generatedDir", generated.toFile() - ); - this.setVariableValueToObject(mojo, "project", new MavenProjectStub()); - mojo.execute(); - MatcherAssert.assertThat( - Files.exists(generated.resolve("EOmain.java")), - Matchers.is(true) + final String java = this.compile( + new ResourceOf("org/eolang/maven/mess.eo"), + "EOmess.java" ); + MatcherAssert.assertThat(java, Matchers.containsString("class EOmess")); } @Test - public void testCrashOnInvalidSyntax() throws Exception { - final CompileMojo mojo = new CompileMojo(); - final Path src = this.temp.resolve("src"); - this.setVariableValueToObject(mojo, "sourcesDirectory", src.toFile()); - this.setVariableValueToObject( - mojo, "generatedDir", this.temp.resolve("generated").toFile() - ); - this.setVariableValueToObject( - mojo, "targetDir", this.temp.resolve("target").toFile() + public void testRealCompilation() throws Exception { + final String java = this.compile( + new ResourceOf("org/eolang/maven/array.eo"), + "EOarray.java" ); + MatcherAssert.assertThat(java, Matchers.containsString("class")); + } + + /** + * Compile EO to Java. + * @param code EO sources + * @param file The file to return + * @return All Java code + * @throws Exception If fails + */ + private String compile(final Input code, + final String file) throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); new LengthOf( new TeeInput( - new InputOf("something is wrong here"), - new OutputTo(src.resolve("f.eo")) + code, + new OutputTo(src.resolve("code.eo")) ) ).value(); - Assertions.assertThrows( - CompileException.class, - mojo::execute + final Path target = temp.resolve("target"); + final Path generated = temp.resolve("generated"); + new Mojo<>(ParseMojo.class) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("targetDir", target.toFile()) + .execute(); + new Mojo<>(CompileMojo.class) + .with("project", new MavenProjectStub()) + .with("targetDir", target.toFile()) + .with("generatedDir", generated.toFile()) + .execute(); + final Path java = generated.resolve(file); + MatcherAssert.assertThat( + Files.exists(java), + Matchers.is(true) ); + final String out = new TextOf(new InputOf(java)).asString(); + Logger.info(this, "Java output:\n%s", out); + return out; } } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java new file mode 100644 index 0000000000..d2915a61a1 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/Mojo.java @@ -0,0 +1,104 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + +/** + * Mutable mojo builder. + * + * @param Type of mojo + * @since 0.1 + */ +final class Mojo { + + /** + * The type of mojo. + */ + private final Class type; + + /** + * All attributes. + */ + private final Map attrs; + + /** + * Ctor. + * + * @param tpe The type + */ + Mojo(final Class tpe) { + this.type = tpe; + this.attrs = new HashMap<>(0); + } + + /** + * Add one more attribute and return self. + * + * @param attr The name + * @param value The value + * @return Itself + */ + public Mojo with(final String attr, final Object value) { + this.attrs.put(attr, value); + return this; + } + + /** + * Execute it. + */ + public void execute() { + try { + final AbstractMojo mojo = this.type.getConstructor().newInstance(); + for (final Map.Entry ent : this.attrs.entrySet()) { + final Field field = this.type.getDeclaredField(ent.getKey()); + if (field == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" of %s in %s", + ent.getKey(), + ent.getValue().getClass().getCanonicalName(), + this.type.getCanonicalName() + ) + ); + } + field.setAccessible(true); + field.set(mojo, ent.getValue()); + } + mojo.execute(); + } catch (final MojoExecutionException | MojoFailureException + | InstantiationException | IllegalAccessException + | NoSuchMethodException | InvocationTargetException + | NoSuchFieldException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/EOaddTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java similarity index 52% rename from eo-runtime/src/test/java/org/eolang/EOaddTest.java rename to eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java index 3b8980cf8a..a61f2b69fe 100644 --- a/eo-runtime/src/test/java/org/eolang/EOaddTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java @@ -21,48 +21,50 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; +package org.eolang.maven; -import org.eolang.sys.ArgsException; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; +import java.nio.file.Files; +import java.nio.file.Path; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Test case for {@link AddOf}. + * Test case for {@link OptimizeMojo}. * * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -public final class EOaddTest { +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class OptimizeMojoTest { @Test - public void addsTwo() throws Exception { + public void testSimpleOptimize() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + new LengthOf( + new TeeInput( + new InputOf( + "[args] > main\n (stdout \"Hello!\").print > @\n" + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + final Path target = temp.resolve("target"); + new Mojo<>(ParseMojo.class) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("targetDir", target.toFile()) + .execute(); MatcherAssert.assertThat( - new EOadd( - new ArgsOf(new Entry("01", 1L), new Entry("02", -1L)) - ).call(), - Matchers.equalTo(0L) - ); - } - - @Test - public void addsOneArg() throws Exception { - MatcherAssert.assertThat( - new EOadd( - new ArgsOf(new Entry("01", 1L)) - ).call(), - Matchers.equalTo(1L) - ); - } - - @Test - public void addsNoArgs() { - Assertions.assertThrows( - ArgsException.class, - () -> new EOadd(new ArgsOf()).call() + Files.exists(target.resolve("eo/steps/main.eo.xml")), + Matchers.is(true) ); } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java new file mode 100644 index 0000000000..91bcbd0549 --- /dev/null +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/ParseMojoTest.java @@ -0,0 +1,90 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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.maven; + +import java.nio.file.Files; +import java.nio.file.Path; +import org.cactoos.io.InputOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; +import org.eolang.compiler.CompileException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link ParseMojo}. + * + * @since 0.1 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class ParseMojoTest { + + @Test + public void testSimpleCompilation() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + final Path target = temp.resolve("target"); + new LengthOf( + new TeeInput( + new InputOf( + "[args] > main\n (stdout \"Hello!\").print\n" + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + new Mojo<>(ParseMojo.class) + .with("sourcesDir", src.toFile()) + .with("targetDir", target.toFile()) + .execute(); + MatcherAssert.assertThat( + Files.exists(target.resolve("eo/parse/main.eo.xml")), + Matchers.is(true) + ); + } + + @Test + public void testCrashOnInvalidSyntax() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + final Path target = temp.resolve("target"); + new LengthOf( + new TeeInput( + new InputOf("something is wrong here"), + new OutputTo(src.resolve("f.eo")) + ) + ).value(); + Assertions.assertThrows( + CompileException.class, + () -> new Mojo<>(ParseMojo.class) + .with("sourcesDir", src.toFile()) + .with("targetDir", target.toFile()) + .execute() + ); + } + +} diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java similarity index 51% rename from eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java rename to eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java index e6db1d0f92..9d76a984cf 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/ToJavaTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/PullMojoTest.java @@ -23,64 +23,59 @@ */ package org.eolang.maven; -import com.jcabi.log.Logger; -import com.jcabi.xml.XMLDocument; -import java.io.ByteArrayOutputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; -import org.cactoos.io.ResourceOf; -import org.cactoos.text.TextOf; -import org.eolang.compiler.Program; +import org.cactoos.io.TeeInput; +import org.cactoos.scalar.LengthOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; /** - * Test case for {@link ToJava}. + * Test case for {@link PullMojo}. * - * @since 1.0 + * @since 0.1 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -public final class ToJavaTest { - - /** - * Temp dir for tests. - * @checkstyle VisibilityModifierCheck (4 lines) - */ - @TempDir - public Path temp; +@SuppressWarnings("PMD.AvoidDuplicateLiterals") +public final class PullMojoTest { @Test - public void compilesSimpleCodeToJava() throws Exception { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Program program = new Program( - "test", - new ResourceOf("org/eolang/maven/fibo.eo"), - new OutputTo(baos) - ); - program.compile(); - Logger.info(this, "XML output: %s\n", baos.toString()); - final ToJava tojava = new ToJava( - new XMLDocument(baos.toString()), - this.temp.resolve("generated"), - this.temp.resolve("eo-to-java") - ); - tojava.compile(); - final Path file = this.temp.resolve(Paths.get("generated/EOfibo.java")); + @Disabled + public void testSimpleOptimize() throws Exception { + final Path temp = Files.createTempDirectory("eo"); + final Path src = temp.resolve("src"); + new LengthOf( + new TeeInput( + new InputOf( + String.join( + "\n", + "+alias stdout org.eolang.io.stdout", + "", + "[args] > main\n (stdout \"Hello!\").print\n" + ) + ), + new OutputTo(src.resolve("main.eo")) + ) + ).value(); + final Path target = temp.resolve("target"); + new Mojo<>(ParseMojo.class) + .with("targetDir", target.toFile()) + .with("sourcesDir", src.toFile()) + .execute(); + new Mojo<>(OptimizeMojo.class) + .with("targetDir", target.toFile()) + .execute(); + new Mojo<>(PullMojo.class) + .with("targetDir", target.toFile()) + .execute(); MatcherAssert.assertThat( - Files.exists(file), + Files.exists(target.resolve("eo/pull/org/stdout/io/stdout.eo.xml")), Matchers.is(true) ); - final String java = new TextOf(new InputOf(file)).asString(); - Logger.info(this, "Java output: %s\n", java); - MatcherAssert.assertThat( - java, - Matchers.containsString("public final class EOfibo") - ); } } diff --git a/eo-maven-plugin/src/test/resources/log4j.properties b/eo-maven-plugin/src/test/resources/log4j.properties index f8675dabb6..56991bfea6 100644 --- a/eo-maven-plugin/src/test/resources/log4j.properties +++ b/eo-maven-plugin/src/test/resources/log4j.properties @@ -5,3 +5,5 @@ log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout log4j.appender.CONSOLE.layout.ConversionPattern=[%color{%p}] %c: %m%n log4j.logger.org.eolang=DEBUG +log4j.logger.org.eolang.compiler.Program=INFO +log4j.logger.org.eolang.maven.TargetSpy=INFO diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo new file mode 120000 index 0000000000..bcf97e5312 --- /dev/null +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/array.eo @@ -0,0 +1 @@ +../../../../../../../eo-runtime/src/main/eo/org/eolang/array.eo \ No newline at end of file diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo deleted file mode 100644 index e9dc598068..0000000000 --- a/eo-maven-plugin/src/test/resources/org/eolang/maven/fibo.eo +++ /dev/null @@ -1,23 +0,0 @@ -# This is the header comment of this simple EO -# program, to be ignored. - -+package org.eolang.examples -+alias stdout org.eolang.io.stdout -+alias sprintf org.eolang.txt.sprintf -+meta This is just a test META -+meta2 And yet another one - -[n] > fibo - if - less n 2 - n - add - fibo (sub n 1) - fibo (sub n 2) - -[args] > app - stdout - sprintf - "%dth Fibonacci number is %d" - (args.get 1) > !n - fibo n diff --git a/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo new file mode 100644 index 0000000000..3c40d90aa7 --- /dev/null +++ b/eo-maven-plugin/src/test/resources/org/eolang/maven/mess.eo @@ -0,0 +1,27 @@ +# This is the header comment of this simple EO +# program, to be ignored. + ++package org.eolang.examples ++alias stdout org.eolang.io.stdout + +# 3.14 > pi +# +# [n] > alpha +# [] > x +# a pi 666 > pp +# [] > n +# wild > tff +# two.f1.f2 x +# stdout > z +# sprintf > f! +# "Hello, ".trim +# x 0x1fff n +# +# alpha > beta +# alpha +# 'Z' +[n x] > mess + [a] > test /int + n.hello > f + "Hello, world!" + x diff --git a/eo-objects/pom.xml b/eo-objects/pom.xml deleted file mode 100644 index 3de3e395ed..0000000000 --- a/eo-objects/pom.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - 4.0.0 - - org.eolang - eo-parent - 1.0-SNAPSHOT - - eo-objects - jar - eo-objects - eolang supplementary objects - - - org.eolang - eo-runtime - ${project.version} - - - - - - org.eolang - eo-maven-plugin - ${project.version} - - - - compile - - - - - - - diff --git a/eo-objects/src/main/eo/org/eolang/Not.eo b/eo-objects/src/main/eo/org/eolang/Not.eo deleted file mode 100644 index ae0b969cf2..0000000000 --- a/eo-objects/src/main/eo/org/eolang/Not.eo +++ /dev/null @@ -1,27 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016-2020 Yegor Bugayenko -# -# 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 - -# Returns TRUE if argument is FALSE and the other way around -[x] > not - if x false true diff --git a/eo-objects/src/main/eo/org/eolang/Sub.eo b/eo-objects/src/main/eo/org/eolang/Sub.eo deleted file mode 100644 index 4d93262356..0000000000 --- a/eo-objects/src/main/eo/org/eolang/Sub.eo +++ /dev/null @@ -1,29 +0,0 @@ -# The MIT License (MIT) -# -# Copyright (c) 2016-2020 Yegor Bugayenko -# -# 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 - -# Calculates algebraic difference between x and y -[x y] > sub - add - x - mul y -1 diff --git a/eo-runtime/pom.xml b/eo-runtime/pom.xml index c078feb51d..5d9597af1c 100644 --- a/eo-runtime/pom.xml +++ b/eo-runtime/pom.xml @@ -33,10 +33,42 @@ SOFTWARE. jar eo-runtime eolang runtime library - - - org.cactoos - cactoos - - + + + + org.eolang + eo-maven-plugin + ${project.version} + + + + parse + optimize + pull + compile + + + + + + + + + qulice + + + + com.qulice + qulice-maven-plugin + + + checkstyle:.* + pmd:.* + + + + + + + diff --git a/eo-runtime/src/main/eo/org/eolang/array.eo b/eo-runtime/src/main/eo/org/eolang/array.eo new file mode 100644 index 0000000000..0f5ee04e8f --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/array.eo @@ -0,0 +1,20 @@ ++package org.eolang + +[b] > array + + # Reduce from start "a" using the function "f" + [a f] > reduce /a + + # Map with index. Here "f" must be an abstract + # object with two free attributes. The first + # one for the element of the array, the second one + # for the index. + [f] > mapi /array + + # Map without index. Here "f" must be an abstract + # object with one free attribute, for the element + # of the array. + [f] > map + mapi > @ + [x i] + f x > @ diff --git a/eo-runtime/src/main/eo/org/eolang/bool.eo b/eo-runtime/src/main/eo/org/eolang/bool.eo new file mode 100644 index 0000000000..b0695a592a --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/bool.eo @@ -0,0 +1,5 @@ ++package org.eolang + +[] > bool + true > self? + [t f] > if /t diff --git a/eo-runtime/src/main/eo/org/eolang/int.eo b/eo-runtime/src/main/eo/org/eolang/int.eo new file mode 100644 index 0000000000..7ca6f47218 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/int.eo @@ -0,0 +1,27 @@ ++package org.eolang + +[] > int + 0 > self? + + # Change the sign of the number + [] > neg /int + + # Add to the current one + [x] > add /int + + # Subtract from the current one + [x] > sub + add x.neg > @ + + # Multiply this one by another int + [x] > mul /int + + # Divide this one by another int + [x] > div + mul x.pow -1 > @ + + # Calculate MOD + [x] > mod /int + + # Make it power x + [x] > pow /int diff --git a/eo-runtime/src/main/eo/org/eolang/io/stdout.eo b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo new file mode 100644 index 0000000000..d9842f7dc4 --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/io/stdout.eo @@ -0,0 +1,4 @@ ++package org.eolang.io + +[text] > stdout + [] > @ /bool diff --git a/eo-runtime/src/main/eo/org/eolang/string.eo b/eo-runtime/src/main/eo/org/eolang/string.eo new file mode 100644 index 0000000000..9238c8426b --- /dev/null +++ b/eo-runtime/src/main/eo/org/eolang/string.eo @@ -0,0 +1,5 @@ ++package org.eolang + +[] > string + "" > self? + [] > trim /string diff --git a/eo-runtime/src/main/java/org/eolang/sys/Phi.java b/eo-runtime/src/main/java/org/eolang/Attr.java similarity index 88% rename from eo-runtime/src/main/java/org/eolang/sys/Phi.java rename to eo-runtime/src/main/java/org/eolang/Attr.java index d3d3d93adb..4ae514b5db 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/Phi.java +++ b/eo-runtime/src/main/java/org/eolang/Attr.java @@ -22,14 +22,20 @@ * SOFTWARE. */ -package org.eolang.sys; - -import java.util.concurrent.Callable; +package org.eolang; /** - * Object with body. + * Attribute. * * @since 0.1 */ -public interface Phi extends Callable { +public interface Attr { + + /** + * Take the object out. + * + * @return The object + */ + Phi get(); + } diff --git a/eo-runtime/src/main/java/org/eolang/EOor.java b/eo-runtime/src/main/java/org/eolang/Data.java similarity index 65% rename from eo-runtime/src/main/java/org/eolang/EOor.java rename to eo-runtime/src/main/java/org/eolang/Data.java index df7b20012e..8f77250b06 100644 --- a/eo-runtime/src/main/java/org/eolang/EOor.java +++ b/eo-runtime/src/main/java/org/eolang/Data.java @@ -24,39 +24,36 @@ package org.eolang; -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.Phi; - /** - * OR. + * A data container. * * @since 0.1 */ -public final class EOor implements Phi { +public interface Data { - /** - * Args. - */ - private final Args args; + T take(); - /** - * Ctor. - * @param arg Args - */ - public EOor(final Args arg) { - this.args = arg; + final class Value extends Phi implements Data { + private final T val; + public Value(final T value) { + super(); + this.val = value; + } + @Override + public T take() { + return this.val; + } } - @Override - public Object call() throws Exception { - boolean result = false; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - result |= this.args.call(key, Boolean.class).equals(Boolean.TRUE); + final class Take { + private final Phi phi; + public Take(final Phi src) { + this.phi = src; + } + @SuppressWarnings("unchecked") + public T take(final Class type) { + return type.cast(((Data) Data.class.cast(this.phi)).take()); } - return result; } + } diff --git a/eo-runtime/src/main/java/org/eolang/EOadd.java b/eo-runtime/src/main/java/org/eolang/EOadd.java deleted file mode 100644 index 3d8bf16dfa..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOadd.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; - -/** - * ADD. - * - * @since 0.1 - */ -public final class EOadd implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOadd(final Args arg) { - this.args = arg; - } - - @Override - @SuppressWarnings("PMD.CyclomaticComplexity") - public Object call() throws Exception { - Object result = null; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - final Object nxt = this.args.call(key, Object.class); - if (!(nxt instanceof Long) && !(nxt instanceof Float)) { - throw new TypeMismatchException( - String.format( - "Can't use ADD with %s", - nxt.getClass() - ) - ); - } - if (result != null && !nxt.getClass().equals(result.getClass())) { - throw new TypeMismatchException( - String.format( - "Can't ADD %s to %s", - result.getClass(), - nxt.getClass() - ) - ); - } - if (result == null) { - result = nxt; - } else { - if (result instanceof Long) { - result = Long.class.cast(result) + Long.class.cast(nxt); - } else { - result = Float.class.cast(result) + Float.class.cast(nxt); - } - } - } - if (result == null) { - throw new ArgsException( - "At least one argument required by ADD" - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOand.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java similarity index 61% rename from eo-runtime/src/main/java/org/eolang/EOand.java rename to eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java index 29270e9262..bf8ceab1a8 100644 --- a/eo-runtime/src/main/java/org/eolang/EOand.java +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOmapi.java @@ -24,42 +24,31 @@ package org.eolang; -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.Phi; - /** - * AND. + * IF. * - * @since 0.1 + * @since 1.0 */ -public final class EOand implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOand(final Args arg) { - this.args = arg; - } +public class EOarray$EOmapi extends Phi { - @Override - public Object call() throws Exception { - boolean result = true; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - result &= this.args.call(key, Boolean.class).equals(Boolean.TRUE); - if (!result) { - break; + public EOarray$EOmapi() { + super(); + this.put("origin", () -> { + final Phi[] array = new Data.Take( + this.get("eo_self") + ).take(Phi[].class); + for (long idx = 0; idx < array.length; ++idx) { + final Phi before = array[(int) idx]; + final Phi after = this.get("eo_f").copy(); + after.put(0, () -> before); + final long index = idx; + after.put(1, () -> new Data.Value<>(index)); + array[(int) idx] = after; } - } - return result; + final Phi out = new org.eolang.EOarray(); + out.put("eo_self", () -> new Data.Value<>(array)); + return out; + }); } + } diff --git a/eo-runtime/src/main/java/org/eolang/EOif.java b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java similarity index 66% rename from eo-runtime/src/main/java/org/eolang/EOif.java rename to eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java index cdc5ef71c7..61dc8c27a3 100644 --- a/eo-runtime/src/main/java/org/eolang/EOif.java +++ b/eo-runtime/src/main/java/org/eolang/EOarray$EOreduce.java @@ -24,37 +24,29 @@ package org.eolang; -import org.eolang.sys.Args; -import org.eolang.sys.Phi; - /** * IF. * - * @since 0.2 + * @since 1.0 */ -public final class EOif implements Phi { - - /** - * Args. - */ - private final Args args; +public class EOarray$EOreduce extends Phi { - /** - * Ctor. - * @param arg Args - */ - public EOif(final Args arg) { - this.args = arg; + public EOarray$EOreduce() { + super(); + this.put("origin", () -> { + final Phi[] array = new Data.Take( + this.get("eo_self") + ).take(Phi[].class); + Phi out = this.get("eo_a"); + for (final Phi arg : array) { + final Phi before = out; + final Phi after = this.get("eo_f").copy(); + after.put(0, () -> before); + after.put(1, () -> arg); + out = after; + } + return out; + }); } - @Override - public Object call() throws Exception { - Object result = false; - if (this.args.call("01", Boolean.class).equals(Boolean.TRUE)) { - result = this.args.get("02"); - } else if (this.args.has("03")) { - result = this.args.get("03"); - } - return result; - } } diff --git a/eo-runtime/src/main/java/org/eolang/EOarray.java b/eo-runtime/src/main/java/org/eolang/EOarray.java deleted file mode 100644 index a4f006ef1d..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOarray.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; - -/** - * ARRAY. - * - * @since 0.1 - */ -public final class EOarray { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOarray(final Args arg) { - this.args = arg; - } - - /** - * The length of it. - * @param input Input args - * @return Length - */ - public Object length(final Args input) { - int size = 0; - for (final String key : this.args.keys()) { - if (key.charAt(0) != '0') { - continue; - } - ++size; - } - return size; - } - - /** - * Get element by index. - * @param input Input args - * @return The object - * @throws Exception If fails - */ - public Object get(final Args input) throws Exception { - final int index = input.call("01", Integer.class); - final Object result = this.args.call( - String.format("%02d", index), Object.class - ); - if (result == null) { - throw new ArgsException( - String.format("The item #%d is absent in the array", index) - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java b/eo-runtime/src/main/java/org/eolang/EObool$EOif.java similarity index 71% rename from eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java rename to eo-runtime/src/main/java/org/eolang/EObool$EOif.java index d5f91dea53..ee0a14a01b 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/TypeMismatchException.java +++ b/eo-runtime/src/main/java/org/eolang/EObool$EOif.java @@ -22,26 +22,29 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; /** - * TypeMismatch Exception. + * IF. * - * @since 0.1 + * @since 1.0 */ -public final class TypeMismatchException extends RuntimeException { +public class EObool$EOif extends Phi { - /** - * Serial ID. - */ - private static final long serialVersionUID = -6643350804302660951L; - - /** - * Ctor. - * @param reason The reason - */ - public TypeMismatchException(final String reason) { - super(reason); + public EObool$EOif() { + super(); + this.put("origin", () -> { + final Boolean term = new Data.Take( + this.get("eo_self") + ).take(Boolean.class); + final Phi out; + if (term) { + out = this.get("eo_t"); + } else { + out = this.get("eo_f"); + } + return out; + }); } } diff --git a/eo-runtime/src/test/java/org/eolang/EOlessTest.java b/eo-runtime/src/main/java/org/eolang/EOint$EOadd.java similarity index 67% rename from eo-runtime/src/test/java/org/eolang/EOlessTest.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOadd.java index d9a469db0f..f2d66b50bf 100644 --- a/eo-runtime/src/test/java/org/eolang/EOlessTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOadd.java @@ -21,32 +21,34 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EOless}. + * ADD. * - * @since 0.2 + * @since 1.0 */ -public final class EOlessTest { +public class EOint$EOadd extends Phi { - @Test - public void compares() throws Exception { - MatcherAssert.assertThat( - new EOless( - new ArgsOf( - new Entry("01", -1L), - new Entry("02", 1L) + public EOint$EOadd() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) + + + new Data.Take( + this.get("eo_x") + ).take(Long.class) ) - ).call(), - Matchers.equalTo(true) - ); + ); + return out; + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOint$EOmod.java b/eo-runtime/src/main/java/org/eolang/EOint$EOmod.java new file mode 100644 index 0000000000..8aae5c4c45 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOmod.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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; + +/** + * MOD. + * + * @since 1.0 + */ +public class EOint$EOmod extends Phi { + + public EOint$EOmod() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + Math.floorMod( + new Data.Take( + this.get("eo_self") + ).take(Long.class), + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) + + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java b/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java new file mode 100644 index 0000000000..f4849c90ba --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOmul.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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; + +/** + * MUL. + * + * @since 1.0 + */ +public class EOint$EOmul extends Phi { + + public EOint$EOmul() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) + * + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/package-info.java b/eo-runtime/src/main/java/org/eolang/EOint$EOneg.java similarity index 69% rename from eo-runtime/src/main/java/org/eolang/sys/package-info.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOneg.java index 3661289c42..1f05afa723 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/package-info.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOneg.java @@ -22,9 +22,29 @@ * SOFTWARE. */ +package org.eolang; + /** - * EO runtime, system. + * NEG. * - * @since 0.2 + * @since 1.0 */ -package org.eolang.sys; +public class EOint$EOneg extends Phi { + + public EOint$EOneg() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_self") + ).take(Long.class) * -1L + ) + ); + return out; + }); + } + +} diff --git a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java b/eo-runtime/src/main/java/org/eolang/EOint$EOpow.java similarity index 64% rename from eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java rename to eo-runtime/src/main/java/org/eolang/EOint$EOpow.java index 77cbf7dab7..a6eee52601 100644 --- a/eo-runtime/src/test/java/org/eolang/txt/EOsprintfTest.java +++ b/eo-runtime/src/main/java/org/eolang/EOint$EOpow.java @@ -21,33 +21,36 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.txt; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; +package org.eolang; /** - * Test case for {@link EOsprintf}. + * POW. * - * @since 0.2 + * @since 1.0 */ -public final class EOsprintfTest { +public class EOint$EOpow extends Phi { + + public EOint$EOpow() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOint(); + out.put( + "eo_self", + () -> new Data.Value<>( + Math.pow( + new Data.Take( + this.get("eo_self") + ).take(Long.class), + new Data.Take( + this.get("eo_x") + ).take(Long.class) + ) - @Test - public void printsSimpleText() throws Exception { - MatcherAssert.assertThat( - new EOsprintf( - new ArgsOf( - new Entry("01", "Hello, %s %d!"), - new Entry("02", "John"), - new Entry("03", 2) ) - ).call(), - Matchers.equalTo("Hello, John 2!") - ); + ); + return out; + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/EOless.java b/eo-runtime/src/main/java/org/eolang/EOless.java deleted file mode 100644 index 82e68115f0..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOless.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.eolang.sys.Args; -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; - -/** - * LESS. - * - * @since 0.2 - */ -public final class EOless implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOless(final Args arg) { - this.args = arg; - } - - @Override - public Object call() throws Exception { - final Object left = this.args.call("01", Number.class); - final Object right = this.args.call("02", Number.class); - final boolean result; - if (left instanceof Long && right instanceof Long) { - result = Long.class.cast(left).compareTo( - Long.class.cast(right) - ) < 0; - } else if (left instanceof Float && right instanceof Float) { - result = Float.class.cast(left).compareTo( - Float.class.cast(right) - ) < 0; - } else { - throw new TypeMismatchException( - String.format( - "Can't compare %s with %s", - left.getClass(), - right.getClass() - ) - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/EOmul.java b/eo-runtime/src/main/java/org/eolang/EOmul.java deleted file mode 100644 index 815138b858..0000000000 --- a/eo-runtime/src/main/java/org/eolang/EOmul.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.ArgsException; -import org.eolang.sys.Phi; -import org.eolang.sys.TypeMismatchException; - -/** - * MUL. - * - * @since 0.2 - */ -public final class EOmul implements Phi { - - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOmul(final Args arg) { - this.args = arg; - } - - @Override - @SuppressWarnings("PMD.CyclomaticComplexity") - public Object call() throws Exception { - Object result = null; - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - final Number nxt = this.args.call(key, Number.class); - if (!(nxt instanceof Long) && !(nxt instanceof Float)) { - throw new TypeMismatchException( - String.format( - "Can't use MUL with %s", - nxt.getClass() - ) - ); - } - if (result != null && !nxt.getClass().equals(result.getClass())) { - throw new TypeMismatchException( - String.format( - "Can't MUL %s to %s", - result.getClass(), - nxt.getClass() - ) - ); - } - if (result == null) { - result = nxt; - } else { - if (result instanceof Long) { - result = Long.class.cast(result) * Long.class.cast(nxt); - } else { - result = Float.class.cast(result) * Float.class.cast(nxt); - } - } - } - if (result == null) { - throw new ArgsException( - "At least one argument required by MUL" - ); - } - return result; - } -} diff --git a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java similarity index 69% rename from eo-runtime/src/main/java/org/eolang/io/EOstdout.java rename to eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java index 2ca0f1aac1..57e71ab294 100644 --- a/eo-runtime/src/main/java/org/eolang/io/EOstdout.java +++ b/eo-runtime/src/main/java/org/eolang/EOstring$EOtrim.java @@ -22,35 +22,29 @@ * SOFTWARE. */ -package org.eolang.io; - -import org.eolang.sys.Args; -import org.eolang.sys.Phi; +package org.eolang; /** - * Stdout. + * TRIM. * - * @since 0.1 + * @since 1.0 */ -public final class EOstdout implements Phi { - - /** - * Args. - */ - private final Args args; +public class EOstring$EOtrim extends Phi { - /** - * Ctor. - * @param arg Args - */ - public EOstdout(final Args arg) { - this.args = arg; + public EOstring$EOtrim() { + super(); + this.put("origin", () -> { + final Phi out = new org.eolang.EOstring(); + out.put( + "eo_self", + () -> new Data.Value<>( + new Data.Take( + this.get("eo_text+") + ).take(String.class).trim() + ) + ); + return out; + }); } - @Override - @SuppressWarnings("PMD.SystemPrintln") - public Object call() throws Exception { - System.out.print(this.args.call("01", String.class)); - return true; - } } diff --git a/eo-runtime/src/main/java/org/eolang/Phi.java b/eo-runtime/src/main/java/org/eolang/Phi.java new file mode 100644 index 0000000000..ecb54af725 --- /dev/null +++ b/eo-runtime/src/main/java/org/eolang/Phi.java @@ -0,0 +1,142 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2020 Yegor Bugayenko + * + * 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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A simple object. + * + * @since 0.1 + */ +public class Phi { + + /** + * Bound attributes. + */ + private final Map bound; + + /** + * Names of free attributes. + */ + private final List free; + + /** + * Ctor. + */ + public Phi(final String... names) { + this(new HashMap<>(0), new ArrayList<>(Arrays.asList(names))); + } + + /** + * Ctor. + */ + private Phi(final Map bnd, final List names) { + this.bound = new HashMap<>(bnd); + this.free = names; + } + + /** + * Make a copy. + * + * @return A copy + */ + public final Phi copy() { + return new Phi(this.bound, this.free); + } + + /** + * Set the attribute. + * + * @param name The name of the attribute to set + * @param phi The value to set + */ + public final void put(final String name, final Attr phi) { + if (this.bound.containsKey(name) && !this.free.contains(name)) { + throw new IllegalStateException( + String.format( + "The attribute \"%s\" is already bound in %s", + name, this.getClass().getCanonicalName() + ) + ); + } + this.bound.put(name, phi); + } + + /** + * Set the attribute by position. + * + * @param pos The position of it + * @param phi The value to set + */ + public final void put(final int pos, final Attr phi) { + this.bound.put(this.free.get(pos), phi); + } + + /** + * Inherit attributes from parent object. + * + * @param parent The parent + */ + public final void inherit(final Phi parent) { + for (final Map.Entry attr : parent.bound.entrySet()) { + this.put( + String.format("%s+", attr.getKey()), + attr.getValue() + ); + } + } + + /** + * Get the attribute. + * + * @param name The name of the attribute to call + * @return The object + */ + public final Phi get(final String name) { + final Attr attr = this.bound.get(name); + final Phi phi; + if (attr == null) { + final Attr origin = this.bound.get("origin"); + if (origin == null) { + throw new IllegalStateException( + String.format( + "Can't find \"%s\" attr and there is no origin in %s", + name, this.getClass().getCanonicalName() + ) + ); + } + phi = origin.get().get(name); + } else { + phi = attr.get(); + } + return phi; + } + +} diff --git a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java similarity index 73% rename from eo-runtime/src/main/java/org/eolang/sys/ArgsException.java rename to eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java index 72746effb6..dac1ce3d33 100644 --- a/eo-runtime/src/main/java/org/eolang/sys/ArgsException.java +++ b/eo-runtime/src/main/java/org/eolang/io/EOstdout$EOorigin.java @@ -22,26 +22,29 @@ * SOFTWARE. */ -package org.eolang.sys; +package org.eolang.io; + +import org.eolang.Data; +import org.eolang.EObool; +import org.eolang.Phi; /** - * Args Exception. + * Stdout. * * @since 0.1 */ -public final class ArgsException extends RuntimeException { - - /** - * Serial ID. - */ - private static final long serialVersionUID = -6643350804302660951L; +public class EOstdout$EOorigin extends Phi { - /** - * Ctor. - * @param reason The reason - */ - public ArgsException(final String reason) { - super(reason); + public EOstdout$EOorigin() { + super(); + this.put("origin", () -> { + System.out.print( + new Data.Take( + this.get("eo_text") + ).take(String.class) + ); + return new EObool(); + }); } } diff --git a/eo-runtime/src/main/java/org/eolang/sys/Args.java b/eo-runtime/src/main/java/org/eolang/sys/Args.java deleted file mode 100644 index 1214f98d1d..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Args.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.sys; - -/** - * Arguments of an object. - * - * @since 0.1 - */ -public interface Args { - - /** - * Get one argument. - * @param key The name - * @return The argument - */ - Object get(String key); - - /** - * Get one argument and call it. - * @param key The name - * @param type The type of expected result - * @return The argument - * @param The type of result - * @throws Exception If fails - */ - T call(String key, Class type) throws Exception; - - /** - * This one exists? - * @param key The name - * @return TRUE if exists - */ - boolean has(String key); - - /** - * Get all keys. - * @return All keys - */ - Iterable keys(); -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java b/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java deleted file mode 100644 index 52f5a985d6..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/ArgsOf.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.sys; - -import java.util.HashMap; -import java.util.Map; - -/** - * Arguments of an object. - * - * @since 0.1 - */ -public final class ArgsOf implements Args { - - /** - * The map. - */ - private final Map map; - - /** - * Ctor. - */ - public ArgsOf() { - this(new HashMap<>(0)); - } - - /** - * Ctor. - * @param items Items - */ - public ArgsOf(final Object... items) { - this(new ArgsOf(), ArgsOf.toEntries(items)); - } - - /** - * Ctor. - * @param entries Entries - */ - public ArgsOf(final Entry... entries) { - this(new ArgsOf(), entries); - } - - /** - * Ctor. - * @param before Args before - * @param entries Entries - */ - public ArgsOf(final Args before, final Entry... entries) { - this(ArgsOf.combine(before, entries)); - } - - /** - * Ctor. - * @param args The args - */ - private ArgsOf(final Map args) { - this.map = args; - } - - @Override - public Object get(final String key) { - final Object result = this.map.get(key); - if (result == null) { - throw new ArgsException( - String.format("The argument \"%s\" is absent", key) - ); - } - return result; - } - - @Override - public T call(final String key, final Class type) throws Exception { - Object result = this.get(key); - if (result instanceof Phi) { - result = Phi.class.cast(result).call(); - } - return type.cast(result); - } - - @Override - public boolean has(final String key) { - return this.map.containsKey(key); - } - - @Override - public Iterable keys() { - return this.map.keySet(); - } - - /** - * Combine them. - * @param before Map before - * @param entries Entries - * @return New map - */ - private static Map combine( - final Args before, final Entry... entries) { - final Map after = new HashMap<>(entries.length); - for (final String key : before.keys()) { - after.put(key, before.get(key)); - } - for (final Entry entry : entries) { - after.put(entry.key(), entry.value()); - } - return after; - } - - /** - * Array of objects to entries. - * @param items The items - * @return Array of entries - */ - private static Entry[] toEntries(final Object... items) { - final Entry[] array = new Entry[items.length]; - for (int idx = 0; idx < items.length; ++idx) { - array[idx] = new Entry(String.format("%02d", idx), items[idx]); - } - return array; - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Call.java b/eo-runtime/src/main/java/org/eolang/sys/Call.java deleted file mode 100644 index 2fdaeb287b..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Call.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.sys; - -import java.lang.reflect.Method; - -/** - * Call. - * - * @since 0.1 - */ -public final class Call implements Phi { - - /** - * The name of the method to call. - */ - private final String method; - - /** - * The object. - */ - private final Object object; - - /** - * Arguments to pass. - */ - private final Args args; - - /** - * Ctor. - * @param mtd The method to call - * @param obj The object - * @param input Input arguments - */ - public Call(final String mtd, final Object obj, final Args input) { - this.method = mtd; - this.object = obj; - this.args = input; - } - - @Override - public Object call() throws Exception { - final Method mtd = this.object.getClass().getDeclaredMethod( - this.method, Args.class - ); - return mtd.invoke(this.object, this.args); - } -} diff --git a/eo-runtime/src/main/java/org/eolang/sys/Entry.java b/eo-runtime/src/main/java/org/eolang/sys/Entry.java deleted file mode 100644 index 74c7cad87a..0000000000 --- a/eo-runtime/src/main/java/org/eolang/sys/Entry.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.sys; - -/** - * Entry. - * - * @since 0.1 - */ -public final class Entry { - - /** - * The key. - */ - private final String akey; - - /** - * The value. - */ - private final Object avalue; - - /** - * Ctor. - * @param key The key - * @param value The value - */ - public Entry(final String key, final Object value) { - this.akey = key; - this.avalue = value; - } - - /** - * Get key. - * @return The key - */ - public String key() { - return this.akey; - } - - /** - * Get value. - * @return The value - */ - public Object value() { - return this.avalue; - } - -} diff --git a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java index c6525bae02..ba0ddfaaf6 100644 --- a/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java +++ b/eo-runtime/src/main/java/org/eolang/txt/EOsprintf.java @@ -26,46 +26,53 @@ import java.util.Collection; import java.util.LinkedList; -import org.cactoos.iterable.Sorted; -import org.eolang.sys.Args; -import org.eolang.sys.Phi; +import org.eolang.Data; +import org.eolang.EObool; +import org.eolang.EOstring; +import org.eolang.Phi; /** * Sprintf. * * @since 0.2 */ -public final class EOsprintf implements Phi { +public class EOsprintf extends Phi { - /** - * Args. - */ - private final Args args; - - /** - * Ctor. - * @param arg Args - */ - public EOsprintf(final Args arg) { - this.args = arg; + public EOsprintf() { + super("eo_format", "eo_args..."); + this.put("origin", () -> { + final Phi out = new org.eolang.EOstring(); + final String format = new Data.Take( + this.get("eo_format") + ).take(String.class); + final Phi[] args = new Data.Take( + this.get("eo_args") + ).take(Phi[].class); + final Collection items = new LinkedList<>(); + for (final Phi arg : args) { + items.add(EOsprintf.toArg(arg)); + } + out.put( + "eo_self", + () -> new Data.Value<>( + String.format(format, items.toArray()) + ) + ); + return out; + }); } - @Override - @SuppressWarnings("PMD.SystemPrintln") - public Object call() throws Exception { - final Collection items = new LinkedList<>(); - for (final String key : new Sorted<>(this.args.keys())) { - if (key.charAt(0) != '0') { - continue; - } - if ("01".equals(key)) { - continue; - } - items.add(this.args.call(key, Object.class)); + private static Object toArg(final Phi phi) { + final Object result; + final Data.Take take = new Data.Take(phi.get("eo_self")); + if (phi instanceof EObool) { + result = take.take(Boolean.class); + } else if (phi instanceof EOstring) { + result = take.take(String.class); + } else { + throw new UnsupportedOperationException(""); } - return String.format( - this.args.call("01", String.class), - items.toArray() - ); + return result; } + } diff --git a/eo-runtime/src/test/java/org/eolang/EOifTest.java b/eo-runtime/src/test/java/org/eolang/EOifTest.java deleted file mode 100644 index 8cf12fb6fb..0000000000 --- a/eo-runtime/src/test/java/org/eolang/EOifTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link EOif}. - * - * @since 0.2 - */ -public final class EOifTest { - - @Test - public void compares() throws Exception { - MatcherAssert.assertThat( - new EOif( - new ArgsOf( - new Entry("01", true), - new Entry("02", -1), - new Entry("03", 0) - ) - ).call(), - Matchers.equalTo(-1) - ); - } - - @Test - public void comparesWithoutElse() throws Exception { - MatcherAssert.assertThat( - new EOif( - new ArgsOf( - new Entry("01", false), - new Entry("02", -1) - ) - ).call(), - Matchers.equalTo(false) - ); - } - -} diff --git a/eo-runtime/src/test/java/org/eolang/EOmulTest.java b/eo-runtime/src/test/java/org/eolang/EOmulTest.java deleted file mode 100644 index cd625ef717..0000000000 --- a/eo-runtime/src/test/java/org/eolang/EOmulTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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; - -import org.eolang.sys.ArgsException; -import org.eolang.sys.ArgsOf; -import org.eolang.sys.Entry; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link AddOf}. - * - * @since 0.2 - */ -public final class EOmulTest { - - @Test - public void mulsTwo() throws Exception { - MatcherAssert.assertThat( - new EOmul( - new ArgsOf(new Entry("01", 1L), new Entry("02", -1L)) - ).call(), - Matchers.equalTo(-1L) - ); - } - - @Test - public void mulsOneArg() throws Exception { - MatcherAssert.assertThat( - new EOmul( - new ArgsOf(new Entry("01", 1L)) - ).call(), - Matchers.equalTo(1L) - ); - } - - @Test - public void mulsNoArgs() { - Assertions.assertThrows( - ArgsException.class, - () -> new EOmul(new ArgsOf()).call() - ); - } - -} diff --git a/eo-runtime/src/test/java/org/eolang/sys/CallTest.java b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java similarity index 77% rename from eo-runtime/src/test/java/org/eolang/sys/CallTest.java rename to eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java index 37d9109188..62facaa321 100644 --- a/eo-runtime/src/test/java/org/eolang/sys/CallTest.java +++ b/eo-runtime/src/test/java/org/eolang/EOstring$EOtrimTest.java @@ -21,31 +21,26 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.eolang.sys; +package org.eolang; -import org.eolang.EOarray; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; /** - * Test case for {@link Call}. + * Test case for {@link EOstring}. * * @since 0.1 */ -public final class CallTest { +public final class EOstring$EOtrimTest { @Test - public void makesCall() throws Exception { + public void trimsString() { + final Phi phi = new EOstring$EOtrim(); + phi.put("eo_text+", () -> new Data.Value<>("Hello, world! ")); MatcherAssert.assertThat( - new Call( - "length", - new EOarray( - new ArgsOf(new Entry("01", 1)) - ), - new ArgsOf() - ).call(), - Matchers.equalTo(1) + new Data.Take(phi.get("eo_self")).take(String.class), + Matchers.equalTo("Hello, world!") ); } diff --git a/eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java b/eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java deleted file mode 100644 index c0d0c162fb..0000000000 --- a/eo-runtime/src/test/java/org/eolang/sys/ArgsOfTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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.sys; - -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; - -/** - * Test case for {@link ArgsOf}. - * @since 0.1 - */ -public final class ArgsOfTest { - - @Test - public void createsArgs() { - final String key = "hey"; - MatcherAssert.assertThat( - new ArgsOf(new Entry(key, 1)).get(key), - Matchers.equalTo(1) - ); - } - - @Test - public void createsArgsWithBefore() { - final String key = "hello"; - MatcherAssert.assertThat( - new ArgsOf( - new ArgsOf(new Entry(key, 0)), - new Entry(key, -1) - ).get(key), - Matchers.equalTo(-1) - ); - } - -} diff --git a/eo-runtime/src/test/java/org/eolang/sys/package-info.java b/eo-runtime/src/test/java/org/eolang/sys/package-info.java deleted file mode 100644 index 248f6771dc..0000000000 --- a/eo-runtime/src/test/java/org/eolang/sys/package-info.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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. - */ - -/** - * EO system objects. - * - * @since 0.1 - */ -package org.eolang.sys; diff --git a/eo-runtime/src/test/java/org/eolang/txt/package-info.java b/eo-runtime/src/test/java/org/eolang/txt/package-info.java deleted file mode 100644 index 4321ef6281..0000000000 --- a/eo-runtime/src/test/java/org/eolang/txt/package-info.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2020 Yegor Bugayenko - * - * 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. - */ - -/** - * EO runtime, texts. - * - * @since 0.2 - */ -package org.eolang.txt; diff --git a/pom.xml b/pom.xml index 590d52638e..cc0e5f5a3d 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ SOFTWARE. com.jcabi parent - 0.50.9 + 0.51.1 org.eolang eo-parent @@ -38,7 +38,6 @@ SOFTWARE. eo-compiler eo-maven-plugin eo-archetype - eo-objects eo-runtime EO Language