Skip to content

Commit

Permalink
[lang] Add global functions for creating 2-dimension arrays.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 22, 2018
1 parent b0724d8 commit 46ed82f
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
1 change: 1 addition & 0 deletions main/coreplugins/io.sarl.lang.core/META-INF/MANIFEST.MF
Expand Up @@ -11,6 +11,7 @@ Require-Bundle: org.eclipse.xtext.xbase.lib;bundle-version="2.15.0";visibility:=
Export-Package: io.sarl.bootstrap,
io.sarl.lang,
io.sarl.lang.annotation,
io.sarl.lang.bugfixes.pending.pr106,
io.sarl.lang.core,
io.sarl.lang.scoping.batch,
io.sarl.lang.util
Expand Down
@@ -0,0 +1,170 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 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.bugfixes.pending.pr106;

import com.google.common.annotations.GwtCompatible;
import org.eclipse.xtext.xbase.lib.Inline;
import org.eclipse.xtext.xbase.lib.Pure;

/**
* Extend the ArrayLiterals class for creating multi-dimensional arrays.
*
* <p>FIXME: Remove when PR is merged: https://github.com/eclipse/xtext-lib/pull/106
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @see "https://github.com/eclipse/xtext-lib/pull/106"
*/
@SuppressWarnings("checkstyle:all")
@GwtCompatible
public class PR106ArrayLiterals {

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new $4[$1][$2]")
public static <T> T[][] newArrayOfSize(int size0, int size1) {
throw new UnsupportedOperationException(
"This method relies on the inlined compilation (see @Inline annotation), and cannot be used from Java or with an uncustomized interpreter."); //$NON-NLS-1$
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new char[$1][$2]")
public static char[][] newCharArrayOfSize(int size0, int size1) {
return new char[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new int[$1][$2]")
public static int[][] newIntArrayOfSize(int size0, int size1) {
return new int[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new boolean[$1][$2]")
public static boolean[][] newBooleanArrayOfSize(int size0, int size1) {
return new boolean[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new short[$1][$2]")
public static short[][] newShortArrayOfSize(int size0, int size1) {
return new short[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new long[$1][$2]")
public static long[][] newLongArrayOfSize(int size0, int size1) {
return new long[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new float[$1][$2]")
public static float[][] newFloatArrayOfSize(int size0, int size1) {
return new float[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new double[$1][$2]")
public static double[][] newDoubleArrayOfSize(int size0, int size1) {
return new double[size0][size1];
}

/**
* @param size0
* the first size for the array to be created
* @param size1
* the second size for the array to be created
* @return an array of the given sizes
* @since 15.0
*/
@Pure
@Inline("new byte[$1][$2]")
public static byte[][] newByteArrayOfSize(int size0, int size1) {
return new byte[size0][size1];
}

}
Expand Up @@ -28,6 +28,7 @@
import com.google.inject.Singleton;
import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures;

import io.sarl.lang.bugfixes.pending.pr106.PR106ArrayLiterals;
import io.sarl.lang.scoping.batch.SARLTimeExtensions;
import io.sarl.lang.scoping.numbers.NumberCastImplicitlyImportedFeatures;
import io.sarl.lang.scoping.numbers.NumberOperatorImplicitlyImportedFeatures;
Expand Down Expand Up @@ -55,6 +56,14 @@ public SARLImplicitlyImportedFeatures() {
super();
}

@Override
protected List<Class<?>> getStaticImportClasses() {
final List<Class<?>> xtextList = super.getStaticImportClasses();
// Insert at the beginning for ensuring the SARL extension is selected before any Xtext extension.
xtextList.add(0, PR106ArrayLiterals.class);
return xtextList;
}

@Override
protected List<Class<?>> getExtensionClasses() {
final List<Class<?>> xtextList = super.getExtensionClasses();
Expand Down

0 comments on commit 46ed82f

Please sign in to comment.