From 0b08da0f3c9406f2902e42c9183d58feb4df4aee Mon Sep 17 00:00:00 2001 From: boufala <lamia.boufala@eud.univ-paris13.fr> Date: Thu, 30 May 2024 18:21:00 +0200 Subject: [PATCH] done with fragement 3 --- AST/Commande.java | 4 ++- AST/DoWhileCommand.java | 18 +++++++++++ AST/EmptyCommand.java | 10 +++++++ AST/IfElseCommand.java | 26 ++++++++++++++++ AST/Programme.java | 5 +++- Compilateur$JJCalls.class | Bin 378 -> 378 bytes Compilateur$LookaheadSuccess.class | Bin 298 -> 298 bytes Compilateur.jj | 46 ++++++++++++++++++----------- test.js | 22 +++++++++++++- 9 files changed, 111 insertions(+), 20 deletions(-) create mode 100644 AST/DoWhileCommand.java create mode 100644 AST/EmptyCommand.java create mode 100644 AST/IfElseCommand.java diff --git a/AST/Commande.java b/AST/Commande.java index 6c6ea43..48a8a76 100644 --- a/AST/Commande.java +++ b/AST/Commande.java @@ -1,4 +1,6 @@ package AST; public abstract class Commande extends AST{ public abstract String toAssembly(); -} \ No newline at end of file +} + + diff --git a/AST/DoWhileCommand.java b/AST/DoWhileCommand.java new file mode 100644 index 0000000..c3e7629 --- /dev/null +++ b/AST/DoWhileCommand.java @@ -0,0 +1,18 @@ +package AST; + +// Classe pour représenter une variable +public class DoWhileCommand extends Commande { + private ExpressionA cond; + private Commande cmd; + + public DoWhileCommand(Commande cmd, ExpressionA expr) { + this.cmd=cmd; + this.cond=expr; + } + @Override + public String toAssembly() { + String s = (this.cmd).toAssembly(); + String c = (this.cond).toAssembly(); + return s + c + "ConJmp -" + (Tools.countNewLines(s) + Tools.countNewLines(c)-1) + "\n"; + } +} diff --git a/AST/EmptyCommand.java b/AST/EmptyCommand.java new file mode 100644 index 0000000..909bd31 --- /dev/null +++ b/AST/EmptyCommand.java @@ -0,0 +1,10 @@ +package AST; + +public class EmptyCommand extends Commande{ + public EmptyCommand(){} + + @Override + public String toAssembly(){ + return ""; + } +} \ No newline at end of file diff --git a/AST/IfElseCommand.java b/AST/IfElseCommand.java new file mode 100644 index 0000000..042def1 --- /dev/null +++ b/AST/IfElseCommand.java @@ -0,0 +1,26 @@ +package AST; + +// Classe pour représenter une commande if-then-else +public class IfElseCommand extends Commande { + private ExpressionA condition; + private Commande trueBranch; + private Commande falseBranch; + + public IfElseCommand(ExpressionA condition, Commande trueBranch, Commande falseBranch) { + this.condition = condition; + this.trueBranch = trueBranch; + this.falseBranch = falseBranch; + } + + @Override + public String toAssembly() { + + String t = trueBranch.toAssembly(); + String f = falseBranch.toAssembly(); + String e = this.condition.toAssembly(); + + + + return e + "Conjmp "+ Tools.countNewLines(t) +"\n"+ t + "Jump "+ Tools.countNewLines(f) + "\n" + t ; + } +} diff --git a/AST/Programme.java b/AST/Programme.java index d75d30a..75f2d5d 100644 --- a/AST/Programme.java +++ b/AST/Programme.java @@ -15,7 +15,10 @@ public class Programme extends AST { public String toAssembly() { String assembly = ""; for (Commande commande : commandes) { - assembly = assembly+commande.toAssembly()+"Drop\n"; + assembly = assembly+commande.toAssembly(); + if(commande instanceof ExpressionA){ + assembly+="Drop"+"\n"; + } } return assembly; diff --git a/Compilateur$JJCalls.class b/Compilateur$JJCalls.class index baf1265786d748a5b9397f27aaa1961250074ad0..3d0aa7fce1b2f43f24b9be3321d8f0397f1108c1 100644 GIT binary patch delta 13 Ucmeyx^owajI3r`m<OoJ3045#;cmMzZ delta 13 Ucmeyx^owajI3uIW<OoJ303~V!Q~&?~ diff --git a/Compilateur$LookaheadSuccess.class b/Compilateur$LookaheadSuccess.class index bb6b73a53335b13b437d3f2b5bb235dee20cf855..bd052506d726cd77265dab73e2ae256b1d557a5b 100644 GIT binary patch delta 13 UcmZ3*w2Em1J0s)i$sCMI0396zn*aa+ delta 13 UcmZ3*w2Em1J0s(&$sCMI033J&dH?_b diff --git a/Compilateur.jj b/Compilateur.jj index c8c5a64..ad7c78c 100644 --- a/Compilateur.jj +++ b/Compilateur.jj @@ -29,26 +29,34 @@ public class Compilateur { } PARSER_END(Compilateur) - TOKEN : { < FLOAT: (("0" | ["1"-"9"] (["0"-"9"])* )? "." (["0"-"9"])+ (("E" | "e") ("+" | "-")? (["0"-"9"])+)? ) | (["0"-"9"])+ (("E" | "e") ("+" | "-")? (["0"-"9"])+)> | < NOMBRE: (["1"-"9"] (["0"-"9"])* | "0") (("E" | "e") ("+" | "-")? (["0"-"9"])+)?> | < BOOLEAN: "True" | "False"> - | < EOL: ";\n" | ";" > | < NAN: "NaN"> | < IMPORT: "import"> - - | < SEMICOLON : ";" > - //| < EQUAL : "=" > - | < IDENT : ["a"-"z"] (["a"-"z", "A"-"Z", "0"-"9", "_"])* > + | < IF: "if" > + | < ELSE: "else" > + | < DO: "do" > + | < WHILE: "while" > + | < EOL: ";\n" | ";" > + | < SEMICOLON: ";" > + | < ASSIGN: "=" > + | < PLUS: "+" > + | < MINUS: "-" > + | < MULTIPLY: "*" > + | < DIVIDE: "/" > + | < LPAREN: "(" > + | < RPAREN: ")" > + | < LBRACE: "{" > + | < RBRACE: "}" > + | < IDENT: (["a"-"z"])+ (["a"-"z", "A"-"Z", "0"-"9", "_"])* > } + SKIP : { " " | "\t" | "\n" | < "//" (~["\n", "\r"])* ("\n" | "\r" | "\r\n") > | < "/*" (~["*"])* "*" (~["/", "*"] (~["*"])* "*" )* "/" >} - - - Programme mainNT() : { Programme programme; } { @@ -69,22 +77,26 @@ Programme programme() : { Commande commande() : { Token id; ExpressionA expr; + Commande cmd1, cmd2; Programme prg; } { - LOOKAHEAD(2) id=<IDENT> "=" expr=expression() <EOL> { return new Assignment(id.image, expr); } // le lockhead pour eviter les conflit !! + LOOKAHEAD(2) id=<IDENT> "=" expr=expression() <EOL> { return new Assignment(id.image, expr); } | expr=expression() <EOL> { return expr; } | <IMPORT> expr=facteur() <EOL> { return new Import(expr); } - | "{" prg=programme() "}" { return new Bloc(prg); } + | <LBRACE> prg=programme() <RBRACE> { return new Bloc(prg); } + | <EOL> {return new EmptyCommand();} + | <IF> <LPAREN> expr=expression() <RPAREN> cmd1=commande() <ELSE> cmd2=commande() { return new IfElseCommand(expr, cmd1, cmd2); } + | <DO> cmd1=commande() <WHILE> <LPAREN> expr=expression() <RPAREN> <EOL> { return new DoWhileCommand(cmd1, expr); } } ExpressionA expression() : { ExpressionA gauche, droite; } { - gauche=comp() ( - "==" droite=comp() { gauche = new Egal(gauche, droite); } + gauche=comp() ( + "==" droite=comp() { gauche = new Egal(gauche, droite); } | ">=" droite=comp() { gauche = new GrEgNb(gauche, droite); } - | ">" droite=comp() { gauche = new GrStNb(gauche, droite); } - | "<" droite=comp() { gauche = new LoStNb(gauche, droite); } + | ">" droite=comp() { gauche = new GrStNb(gauche, droite); } + | "<" droite=comp() { gauche = new LoStNb(gauche, droite); } | "<=" droite=comp() { gauche = new LoEqNb(gauche, droite); } | "!=" droite=comp() { gauche = new NotEql(gauche, droite); } | "&&" droite=comp() { gauche = new Et(gauche, droite); } @@ -108,7 +120,7 @@ ExpressionA terme() : { ExpressionA gauche, droite; } { gauche=facteur() ( - "*" droite=facteur() { gauche = new Mult(gauche, droite); } + "*" droite=facteur() { gauche = new Mult(gauche, droite); } | "/" droite=facteur() { gauche = new Div(gauche, droite); } | "%" droite=facteur() { gauche = new Modulo(gauche, droite); } )* { @@ -122,5 +134,5 @@ ExpressionA facteur() : { <NOMBRE> { return new Num(Integer.parseInt(token.image)); } | <FLOAT> { return new FloatT(Float.parseFloat(token.image)); } | <BOOLEAN> { return new Bool(Boolean.parseBoolean(token.image)); } - | <IDENT> { return new Ident(token.image);} + | <IDENT> { return new Ident(token.image); } } diff --git a/test.js b/test.js index 1308a88..4d63857 100644 --- a/test.js +++ b/test.js @@ -1 +1,21 @@ -2*10+x == 3*8; \ No newline at end of file +// to test the if else +if (x<10){ + x+1; +}else{ + y=10; +} +// to test the do while +do { + y=y+1; +}while(y<10); + +// to test the compressed sequence of commands + +{ + e+1; + a-10; + 3+1; +} +// to test the EmptyCommand +; + \ No newline at end of file -- GitLab