Skip to content

Commit

Permalink
[lang] Generate inline constant expressions with Java format.
Browse files Browse the repository at this point in the history
close #631

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 31, 2017
1 parent 7ee7c79 commit 0ecafba
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 8 deletions.
Expand Up @@ -38,7 +38,7 @@
* @since 0.4
* @see Inline
*/
@ImplementedBy(DefaultInlineExpressionCompiler.class)
@ImplementedBy(JavaInlineExpressionCompiler.class)
public interface IInlineExpressionCompiler {

/** Append the inline annotation to the given operation.
Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.util.PolymorphicDispatcher;
import org.eclipse.xtext.util.ReflectionUtil;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XBooleanLiteral;
Expand All @@ -65,7 +66,7 @@
import io.sarl.lang.generator.GeneratorConfigProvider2;


/** Compiler for crating inline expressions.
/** Compiler for creating inline expressions with Java syntax.
*
* @author $Author: sgalland$
* @version $FullVersion$
Expand All @@ -75,7 +76,7 @@
* @see Inline
*/
@Singleton
public class DefaultInlineExpressionCompiler implements IInlineExpressionCompiler {
public class JavaInlineExpressionCompiler implements IInlineExpressionCompiler {

@Inject
private JvmAnnotationReferenceBuilder.Factory annotationRefBuilderFactory;
Expand All @@ -99,7 +100,7 @@ public class DefaultInlineExpressionCompiler implements IInlineExpressionCompile

/** Constructor.
*/
public DefaultInlineExpressionCompiler() {
public JavaInlineExpressionCompiler() {
this.generateDispatcher = new PolymorphicDispatcher<Void>(
"_generate", 3, 3, //$NON-NLS-1$
Collections.singletonList(this)) {
Expand Down Expand Up @@ -232,6 +233,17 @@ protected void generate(XExpression expression, XtendExecutable feature, InlineA
output.appendStringConstant(evaluationResult.toString());
} else if (evaluationResult instanceof JvmTypeReference) {
output.appendTypeConstant(((JvmTypeReference) evaluationResult).getType());
} else if (evaluationResult instanceof Number) {
final Class<?> type = ReflectionUtil.getRawType(evaluationResult.getClass());
if (Byte.class.equals(type) || byte.class.equals(type)) {
output.appendConstant("(byte) (" + evaluationResult.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
} else if (Short.class.equals(type) || short.class.equals(type)) {
output.appendConstant("(short) (" + evaluationResult.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
} else if (Float.class.equals(type) || float.class.equals(type)) {
output.appendConstant(evaluationResult.toString() + "f"); //$NON-NLS-1$
} else {
output.appendConstant(evaluationResult.toString());
}
} else {
output.appendConstant(Objects.toString(evaluationResult));
}
Expand Down
91 changes: 91 additions & 0 deletions tests/io.sarl.lang.tests/src/io/sarl/lang/tests/bugs/Bug631.java
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.lang.tests.bugs;

import com.google.inject.Inject;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper;
import org.junit.Test;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.tests.api.AbstractSarlTest;

/** Testing class for issue: Invalid floating point value in inline expression.
*
* <p>https://github.com/sarl/sarl/issues/631
*
* @author $Author: sgalland$
* @version $Name$ $Revision$ $Date$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class Bug631 extends AbstractSarlTest {

private static final String SNIPSET1 = multilineString(
"package io.sarl.lang.tests.bug631",
"class XXX {",
" def fct : float {",
" 0f",
" }",
" def fct2 : float[] {",
" #[ 0f, fct ]",
" }",
"}");

@Inject
private CompilationTestHelper compiler;

@Test
public void parsing_01() throws Exception {
SarlScript mas = file(SNIPSET1);
final Validator validator = validate(mas);
validator.assertNoErrors();
}

@Test
public void compiling_01() throws Exception {
this.compiler.assertCompilesTo(SNIPSET1, multilineString(
"package io.sarl.lang.tests.bug631;",
"",
"import io.sarl.lang.annotation.SarlSpecification;",
"import io.sarl.lang.annotation.SyntheticMember;",
"import org.eclipse.xtext.xbase.lib.Inline;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SuppressWarnings(\"all\")",
"public class XXX {",
" @Inline(value = \"0.0f\", constantExpression = true)",
" public float fct() {",
" return 0f;",
" }",
" ",
" public float[] fct2() {",
" float _fct = 0.0f;",
" return new float[] { 0f, _fct };",
" }",
" ",
" @SyntheticMember",
" public XXX() {",
" super();",
" }",
"}",
""));
}

}
Expand Up @@ -1515,7 +1515,7 @@ public void compatibleReturnType_1() throws Exception {
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SuppressWarnings(\"all\")",
"public class S1 extends Skill implements C1 {",
" @Inline(value = \"0.0\", constantExpression = true)",
" @Inline(value = \"0.0f\", constantExpression = true)",
" public float myaction(final int a) {",
" return 0f;",
" }",
Expand All @@ -1541,7 +1541,7 @@ public void compatibleReturnType_1() throws Exception {
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SuppressWarnings(\"all\")",
"public class S2 extends S1 implements C2 {",
" @Inline(value = \"0.0\", constantExpression = true)",
" @Inline(value = \"0.0f\", constantExpression = true)",
" public float myaction(final int a) {",
" return 0f;",
" }",
Expand Down Expand Up @@ -1611,7 +1611,7 @@ public void compatibleReturnType_2() throws Exception {
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SuppressWarnings(\"all\")",
"public class S1 extends Skill implements C1 {",
" @Inline(value = \"0.0\", constantExpression = true)",
" @Inline(value = \"0.0f\", constantExpression = true)",
" public float myaction(final int a) {",
" return 0f;",
" }",
Expand Down Expand Up @@ -1685,7 +1685,7 @@ public void compatibleReturnType_3() throws Exception {
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SuppressWarnings(\"all\")",
"public class S1 extends Skill implements C1 {",
" @Inline(value = \"0.0\", constantExpression = true)",
" @Inline(value = \"0.0f\", constantExpression = true)",
" public float myaction(final int a) {",
" return 0f;",
" }",
Expand Down

0 comments on commit 0ecafba

Please sign in to comment.