mirror of
https://github.com/LucasVbr/interpreteur-lir.git
synced 2026-05-13 17:21:52 +00:00
feature Littéral
1 compareTo & 1 toString a modifier et 1 concaténation a dégager
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* SyntaxeException.java 7 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir;
|
||||
|
||||
/**
|
||||
* Exception levée lors d'une erreur dans l'interpreteur LIR.
|
||||
* (Erreur de syntaxe, erreur de types)
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class InterpreteurException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Une exception de syntaxe expliquée par un message
|
||||
* @param message explication succincte de cette exception
|
||||
*/
|
||||
public InterpreteurException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Chaine.java 7 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.donnees.litteraux;
|
||||
|
||||
import interpreteurlir.InterpreteurException;
|
||||
|
||||
/**
|
||||
* Constante littérale de type chaîne de caractères.
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class Chaine extends Litteral {
|
||||
|
||||
/** Longueur maximale d'une chaîne */
|
||||
public static final int LG_MAX_CHAINE = 70;
|
||||
|
||||
/** Erreur chaîne trop longue */
|
||||
private static final String ERREUR_LG_MAX = "Longueur maximale dépassée";
|
||||
|
||||
/**
|
||||
* initialise cette chaîne avec une valeur par défaut.
|
||||
*/
|
||||
public Chaine() {
|
||||
valeur = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise une chaîne avec la séquence de caractères passée en argument.
|
||||
* @param uneValeur
|
||||
*/
|
||||
public Chaine(String uneValeur) {
|
||||
|
||||
if (uneValeur.length() > LG_MAX_CHAINE)
|
||||
throw new InterpreteurException(ERREUR_LG_MAX);
|
||||
|
||||
valeur = uneValeur;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatène deux chaînes ensemble. Opération non commutative:
|
||||
* a + b != b + a
|
||||
* @param a une Chaîne
|
||||
* @param b une autre Chaîne
|
||||
* @return une nouvelle Chaîne.
|
||||
*/
|
||||
public static Chaine concatener(Chaine a, Chaine b) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/* non javadoc
|
||||
* @see interpreteurlir.donnees.litteraux.Litteral#compareTo(interpreteurlir.donnees.litteraux.Litteral)
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Litteral autre) {
|
||||
// TODO Auto-generated method stub
|
||||
return this.valeur.toString().compareTo(autre.valeur.toString());
|
||||
}
|
||||
|
||||
/* non javadoc
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return '\"' + valeur.toString() + '\"';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Litteral.java 7 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.donnees.litteraux;
|
||||
|
||||
/**
|
||||
* Valeur littérale utilisée dans une expression.
|
||||
* Chaque littérale est reconnue par sont type.
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class Litteral implements Comparable<Litteral> {
|
||||
|
||||
/** valeur de ce littéral */
|
||||
protected Object valeur;
|
||||
|
||||
/**
|
||||
* Initialise ce littéral par défaut.
|
||||
*/
|
||||
protected Litteral() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise cette valeur avec un objet argument.
|
||||
* @param valeur
|
||||
*/
|
||||
public Litteral(Object valeur) { // TODO public >>> protected
|
||||
super();
|
||||
this.valeur = valeur;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return la valeur de valeur
|
||||
*/
|
||||
public Object getValeur() {
|
||||
return valeur;
|
||||
}
|
||||
|
||||
/* non javadoc
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return valeur.toString();
|
||||
}
|
||||
|
||||
/* non javadoc
|
||||
* @see java.lang.Comparable#compareTo(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Litteral autre) {
|
||||
|
||||
if (autre.valeur.getClass() == this.valeur.getClass())
|
||||
return 0;
|
||||
|
||||
return this.valeur.hashCode() - autre.valeur.hashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* TestChaine.java 8 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.donnees.litteraux.test;
|
||||
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.donnees.litteraux.Chaine;
|
||||
|
||||
/**
|
||||
* Tests unitaires de Chaine
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestChaine {
|
||||
|
||||
|
||||
/** test de Chaine(String) */
|
||||
public static void testChaine() {
|
||||
|
||||
final String[] VALIDE = {
|
||||
"arztyehjklmpoijhghnbghjklmpoiuytrf" +
|
||||
"ghjnklmpoiuytrezaqsdfghnjklmpjbfrtyu", "","coucou ",
|
||||
Integer.toString(42)
|
||||
};
|
||||
|
||||
final String INVALIDE =
|
||||
"arztyehjklmpoijhghnbghjklmpoiuytrf" +
|
||||
"yeryghjnklmpoiuytrezaqsdfghnjklmpjbfrtyu";
|
||||
|
||||
System.out.println("test de Chaine(String)");
|
||||
|
||||
for (String aTester : VALIDE)
|
||||
new Chaine(aTester);
|
||||
|
||||
try {
|
||||
new Chaine(INVALIDE);
|
||||
throw new RuntimeException("Instanciation interdite");
|
||||
} catch (InterpreteurException lancee) {
|
||||
System.out.println("Revoi d'exception OK\nfin du test");
|
||||
}
|
||||
}
|
||||
|
||||
/** test de compareTo */
|
||||
public static void testCompareTo() {
|
||||
|
||||
// TODO
|
||||
}
|
||||
|
||||
// TODO tester toString
|
||||
/**
|
||||
* Lancement des tests
|
||||
* @param args non utilisés
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testChaine();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* TestLitteraux.java 7 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.donnees.litteraux.test;
|
||||
|
||||
import interpreteurlir.donnees.litteraux.Litteral;
|
||||
|
||||
/**
|
||||
* Test unitaires des constantes littérales de l'interpréteurlir
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestLitteraux {
|
||||
|
||||
/** Jeux de littéraux pour test. */
|
||||
private static final Litteral[] VALIDES = {
|
||||
/* Caractères */
|
||||
new Litteral('a'),
|
||||
new Litteral('!'),
|
||||
new Litteral('\"'),
|
||||
new Litteral('1'),
|
||||
new Litteral('\t'),
|
||||
/* Chaînes */
|
||||
new Litteral("ceci est une chaine"),
|
||||
new Litteral("bonjour"),
|
||||
new Litteral(" bonjour "),
|
||||
new Litteral(""),
|
||||
new Litteral(" "),
|
||||
/* Entier */
|
||||
new Litteral(123),
|
||||
new Litteral(-123),
|
||||
new Litteral(0),
|
||||
new Litteral(Integer.MAX_VALUE),
|
||||
/* Double */
|
||||
new Litteral(14.258),
|
||||
new Litteral(-14.128),
|
||||
new Litteral(0.0),
|
||||
new Litteral(Double.NaN),
|
||||
new Litteral(Double.NEGATIVE_INFINITY),
|
||||
new Litteral(Double.MAX_VALUE),
|
||||
new Litteral(Double.MIN_VALUE),
|
||||
new Litteral(Double.MIN_NORMAL),
|
||||
/* Boolean */
|
||||
new Litteral(true),
|
||||
new Litteral(false),
|
||||
new Litteral(3 >= 4),
|
||||
new Litteral(true),
|
||||
new Litteral(true),
|
||||
new Litteral(true)
|
||||
};
|
||||
|
||||
/** test de getValeur */
|
||||
public static void testGetValeur() {
|
||||
|
||||
final Object[] VALEURS_ATTENDUES = {
|
||||
'a', '!', '\"', '1', '\t' ,"ceci est une chaîne", "bonjour",
|
||||
" bonjour ", "", " ", 123, -123, 0, 2147483647, 14.258, -14.128,
|
||||
0.0, Double.NaN, Double.NEGATIVE_INFINITY, Double.MAX_VALUE,
|
||||
Double.MIN_VALUE, Double.MIN_NORMAL, true, false, false, true, true,
|
||||
true
|
||||
};
|
||||
|
||||
System.out.println("test de getValeur\n");
|
||||
|
||||
for (int i = 0 ; i < VALIDES.length ; i++) {
|
||||
|
||||
try {
|
||||
assert (VALIDES[i].getValeur().equals(VALEURS_ATTENDUES[i]));
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test a l'indice " + i);
|
||||
}
|
||||
}
|
||||
System.out.println("==>test terminé\n");
|
||||
}
|
||||
|
||||
/** test de toString */
|
||||
public static void testToString() {
|
||||
|
||||
final String[] STRING_ATTENDUE = {
|
||||
"a", "!", "\"", "1", "\t", "ceci est une chaîne", "bonjour",
|
||||
" bonjour ", "", " ", "123", "-123", "0", "2147483647", "14.258",
|
||||
"-14.128", "0.0", "NaN", "-Infinity", "1.7976931348623157E308",
|
||||
"4.9E-324", "2.2250738585072014E-308", "true", "false", "false",
|
||||
"true", "true", "true"
|
||||
};
|
||||
|
||||
System.out.println("test de toString\n");
|
||||
|
||||
for (int i = 0 ; i < VALIDES.length ; i++ ) {
|
||||
|
||||
try {
|
||||
assert (VALIDES[i].toString().equals(STRING_ATTENDUE[i]));
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test a l'indice " + i);
|
||||
}
|
||||
}
|
||||
System.out.println("==>test terminé\n");
|
||||
}
|
||||
|
||||
/** test de compareTo */
|
||||
public static void testCompareTo() {
|
||||
|
||||
final Litteral[] MEMES_TYPES = {
|
||||
new Litteral('a'),
|
||||
new Litteral('!'),
|
||||
new Litteral('\"'),
|
||||
new Litteral('Z'),
|
||||
new Litteral('s'),
|
||||
new Litteral("bonjour"),
|
||||
new Litteral("bonjour"),
|
||||
new Litteral("arar"),
|
||||
new Litteral("zarar za "),
|
||||
new Litteral("CAFE_BABE"),
|
||||
new Litteral(123),
|
||||
new Litteral(123),
|
||||
new Litteral(0),
|
||||
new Litteral(-123),
|
||||
new Litteral(Double.MAX_VALUE),
|
||||
new Litteral(Double.NaN),
|
||||
new Litteral(12.3),
|
||||
new Litteral(Double.NaN),
|
||||
new Litteral(45.7),
|
||||
new Litteral(-12.6),
|
||||
new Litteral(0.0),
|
||||
new Litteral(Double.MIN_NORMAL),
|
||||
new Litteral(false),
|
||||
new Litteral(false),
|
||||
new Litteral(true),
|
||||
new Litteral(true),
|
||||
new Litteral(true),
|
||||
new Litteral(true)
|
||||
};
|
||||
|
||||
System.out.println("test de compareTo\nAvec des types identiques");
|
||||
|
||||
for (int i = 0 ; i < VALIDES.length ; i++ ) {
|
||||
|
||||
try {
|
||||
assert (VALIDES[i].compareTo(MEMES_TYPES[i]) == 0);
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test a l'indice " + i);
|
||||
}
|
||||
}
|
||||
System.out.println("Avec des types différents");
|
||||
|
||||
for (int i = 0 ; i < VALIDES.length ; i++ ) {
|
||||
|
||||
try {
|
||||
assert (VALIDES[i].compareTo(MEMES_TYPES[MEMES_TYPES.length
|
||||
- (i + 1)]) != 0);
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test a l'indice " + i);
|
||||
}
|
||||
}
|
||||
System.out.println("==>test terminé\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Lancement des test
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testGetValeur();
|
||||
testToString();
|
||||
testCompareTo();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user