Skip to content

Commit

Permalink
feat(objectionary#329): Add CheckCast ast node
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 9, 2024
1 parent 7175e64 commit 6c72bdb
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/main/java/org/eolang/opeo/ast/CheckCast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.opeo.ast;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eolang.jeo.representation.directives.DirectivesData;
import org.eolang.jeo.representation.xmir.HexString;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.compilation.Parser;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.xembly.Directive;
import org.xembly.Directives;

/**
* Check if the value is of the given type.
* @since 0.5
*/
public final class CheckCast implements AstNode, Typed {

/**
* Type to cast to.
*/
private final Type type;

/**
* Value to cast.
*/
private final AstNode value;

public CheckCast(final XmlNode node, final Parser parser) {
this(CheckCast.xtype(node), CheckCast.xvalue(node, parser));
}

private static AstNode xvalue(final XmlNode node, final Parser parser) {
return parser.parse(
node.children().findFirst()
.orElseThrow(() -> new IllegalArgumentException("CheckCast should have a child."))
);
}

private static Type xtype(final XmlNode node) {
return Type.getType(
new HexString(
node.children().findFirst().orElseThrow(
() -> new IllegalArgumentException(
"CheckCast should have a first child for the type."
)
).text()
).decode()
);
}


public CheckCast(final Type type, final AstNode value) {
this.type = type;
this.value = value;
}

@Override
public Iterable<Directive> toXmir() {
return new Directives().add("o").attr("base", "cast")
.append(new DirectivesData(this.type))
.append(this.value.toXmir())
.up();
}

@Override
public List<AstNode> opcodes() {
final List<AstNode> res = new ArrayList<>(1);
res.addAll(this.value.opcodes());
res.add(new Opcode(Opcodes.CHECKCAST, this.type));
return res;
}

@Override
public Type type() {
return this.type;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/eolang/opeo/ast/Opcode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import lombok.EqualsAndHashCode;
Expand Down
90 changes: 90 additions & 0 deletions src/test/java/org/eolang/opeo/ast/CheckCastTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.opeo.ast;

import org.eolang.jeo.representation.xmir.XmlNode;
import org.eolang.opeo.SameXml;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.xembly.ImpossibleModificationException;
import org.xembly.Xembler;

/**
* Test case for {@link CheckCast}.
* @since 0.5
*/
class CheckCastTest {

/**
* XMIR representation of the CheckCast.
*/
private static final String XMIR = String.join(
"\n",
"<o base='cast'>",
" <o base='type' data='bytes'>4A</o>",
" <o base='int' data='bytes'>00 00 00 00 00 00 00 03</o>",
"</o>"
);

@Test
void convertsCheckCastToXmir() throws ImpossibleModificationException {
MatcherAssert.assertThat(
"Can't convert CheckCast to XMIR",
new Xembler(
new CheckCast(
Type.LONG_TYPE,
new Literal(3)
).toXmir()
).xml(),
new SameXml(CheckCastTest.XMIR)
);
}

@Test
void createsCheckCastFromXmir() {
MatcherAssert.assertThat(
"Can't parse CheckCast type from XMIR",
new CheckCast(new XmlNode(CheckCastTest.XMIR), n -> new Literal(3)).type(),
Matchers.equalTo(Type.LONG_TYPE)
);
}

@Test
void transformsToOpcodes() {
final Type type = Type.INT_TYPE;
MatcherAssert.assertThat(
"Can't transform CheckCast to opcodes",
new CheckCast(type, new Literal((byte) 3)).opcodes(),
Matchers.hasItems(
new Opcode(Opcodes.BIPUSH, (byte) 3),
new Opcode(Opcodes.CHECKCAST, type)
)
);
}


}

0 comments on commit 6c72bdb

Please sign in to comment.