mirror of
https://github.com/LucasVbr/interpreteur-lir.git
synced 2026-07-09 15:08:05 +00:00
Codes sources prototype 1
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Instruction.java 9 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.motscles.Commande;
|
||||
|
||||
/**
|
||||
* Instruction du langage LIR. Chaque instruction se caractérise par une
|
||||
* expression et un contexte d'exécution
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class Instruction extends Commande {
|
||||
|
||||
/** Contexte d'exécution de cette instruction */
|
||||
protected Contexte contexteGlobal;
|
||||
|
||||
/** Expression qui sera exécutée par la commande */
|
||||
protected Expression aExecuter;
|
||||
|
||||
/**
|
||||
* Initialise une instruction à partir du contexte d'éxécution et de
|
||||
* l'expression à exécuter
|
||||
* @param arguments expression qui sera exécutée
|
||||
* @param contexte global de l'application
|
||||
*/
|
||||
public Instruction(String arguments, Contexte contexte) {
|
||||
super(arguments,contexte);
|
||||
// arguments non utilisés pour Instruction générale
|
||||
}
|
||||
|
||||
/*
|
||||
* Non - javadoc
|
||||
* @see interpreteurlir.motscles.Commande#executer()
|
||||
*/
|
||||
@Override
|
||||
public boolean executer() {
|
||||
return super.executer();
|
||||
}
|
||||
|
||||
/*
|
||||
* Non - javadoc
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " " + aExecuter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* InstructionVar.java 9 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.expressions.ExpressionChaine;
|
||||
|
||||
/**
|
||||
* Instruction de déclaration et d'affectation de variables. La syntaxe de
|
||||
* cette expression est de la forme var identificateur = expression. Si
|
||||
* expression non renseignée, l'interpréteur affichera un message d'erreur ;
|
||||
* l'instruction doit effectuer systématiquement une affectation.
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class InstructionVar extends Instruction {
|
||||
|
||||
/**
|
||||
* TODO comment initial state of the object
|
||||
* @param arguments expression à exécuter
|
||||
* @param contexte global de l'interpréteur
|
||||
*/
|
||||
public InstructionVar(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
if (arguments == null || arguments.isBlank()
|
||||
|| ExpressionChaine.indexOperateur(arguments, '=') <= 0)
|
||||
throw new InterpreteurException("erreur de syntaxe");
|
||||
|
||||
aExecuter = Expression.determinerTypeExpression(arguments.trim());
|
||||
}
|
||||
|
||||
/*
|
||||
* Non - javadoc
|
||||
* @see interpreteurlir.motscles.instructions.Instruction#executer()
|
||||
*/
|
||||
@Override
|
||||
public boolean executer() {
|
||||
aExecuter.calculer();
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Non - javadoc
|
||||
* @see interpreteurlir.motscles.instructions.Instruction#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "var " + aExecuter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* TestInstruction.java 9 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions.tests;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.motscles.instructions.Instruction;
|
||||
|
||||
/**
|
||||
* Tests unitaires des instructions
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestInstruction {
|
||||
|
||||
/**
|
||||
* Test du constructeur
|
||||
*/
|
||||
public static void testInstruction() {
|
||||
System.out.println("Test du constructeur");
|
||||
Instruction aTester = new Instruction("Bonjour", new Contexte());
|
||||
System.out.println("==> OK\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test de toString()
|
||||
*/
|
||||
public static void testToString() {
|
||||
System.out.println("Test de toString()");
|
||||
Instruction aTester = new Instruction("Bonjour", new Contexte());
|
||||
|
||||
if (!aTester.toString().equals("Instruction null"))
|
||||
System.err.println("Echec du test");
|
||||
else
|
||||
System.out.println("==> OK\n");
|
||||
|
||||
System.out.println(aTester);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lancement des tests
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testInstruction();
|
||||
testToString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* TestInstructionVar.java 9 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions.tests;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.motscles.instructions.InstructionVar;
|
||||
|
||||
/**
|
||||
* Tests unitaires de l'instruction var
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestInstructionVar {
|
||||
|
||||
/** jeu de données pour tests */
|
||||
public static final String[] VALIDES = {
|
||||
"$toto = $tata", "entier=2+2", "$coucou = $toto + \"titi\"",
|
||||
"anneeNaissance = 1898"
|
||||
};
|
||||
|
||||
/**
|
||||
* Test du constructeur InstructionVar
|
||||
*/
|
||||
public static void testInstructionVar() {
|
||||
final String[] EXPRESSIONS_INVALIDES = {
|
||||
"bonjour", "", null, "$toto $tata",
|
||||
};
|
||||
|
||||
System.out.println("Test du constructeur\navec expressions invalides");
|
||||
for (String aTester : EXPRESSIONS_INVALIDES) {
|
||||
try {
|
||||
new InstructionVar(aTester, new Contexte());
|
||||
throw new RuntimeException("Echec du test");
|
||||
} catch (InterpreteurException lancee) {
|
||||
System.out.println(aTester + " ==> OK");
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Avec expressions valides");
|
||||
for (String aTester : VALIDES)
|
||||
new InstructionVar(aTester, new Contexte());
|
||||
|
||||
System.out.println("Fin du test\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test de toString
|
||||
*/
|
||||
public static void testToString() {
|
||||
final String[] CHAINES_ATTENDUES = {
|
||||
"var $toto = $tata", "var entier=2+2",
|
||||
"var $coucou = $toto + \\\"titi\\\"",
|
||||
"var anneeNaissance = 1898"
|
||||
};
|
||||
|
||||
System.out.println("Test de toString()");
|
||||
for (int i = 0 ; i < VALIDES.length ; i++) {
|
||||
|
||||
try {
|
||||
assert VALIDES[i].toString().equals(CHAINES_ATTENDUES[i]);
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test à l'indice " + i);
|
||||
}
|
||||
}
|
||||
System.out.println("Fin du test\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO comment method responsibilities
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testInstructionVar();
|
||||
testToString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user