Skip to content
Snippets Groups Projects
Commit 25f347c7 authored by lucien.noel's avatar lucien.noel
Browse files

parentheses et conditions terminé

parent d947971e
No related branches found
No related tags found
No related merge requests found
/*
* Represent an equal comparaison expression node inside the AST.
*/
public class Condition extends Instruction {
private Expression condition;
private Instruction thenInstruction;
private Instruction elseInstruction;
/**
* Constructor
*/
public Condition(Expression condition, Instruction thenInstruction, Instruction elseInstruction, String fl, int line, int col) {
super(fl, line, col);
this.condition = condition;
this.elseInstruction = elseInstruction;
this.thenInstruction = thenInstruction;
}
/**
* Return the expression of the if condition
*/
public Expression getCondition(){
return this.condition;
}
/**
* Return the instruction in the then statement
*/
public Instruction getThenInstruction(){
return this.thenInstruction;
}
/**
* Return the instruction in the else statement
*/
public Instruction getElseInstruction(){
return this.elseInstruction;
}
/**
* Check if there is an else instruction
*/
public boolean hasElse(){
return this.elseInstruction != null;
}
/**
* Accepts a AST visitor
*/
Object accept(ASTVisitor visitor){
return visitor.visit(this);
}
}
/*
* Represent an identifier node inside the AST.
*/
public class Parentheses extends Expression {
/**
* Name of the
*/
private Expression exp;
/**
* Constructor
*/
public Parentheses(Expression exp, String fl, int line, int col) {
super(fl, line, col);
this.exp = exp;
}
/**
* Get the expression
*/
public Expression getExpression() {
return this.exp;
}
/**
* Accepts a AST visitor
*/
Object accept(ASTVisitor visitor){
return visitor.visit(this);
}
}
programme Program
debutprg
c = -2;
c = 1;
si c alors c = 2;sinon c = 0;
finsi
finprg
......@@ -18,7 +18,7 @@ public interface ASTVisitor {
Object visit(Chaine node);
// Object visit(Condition node);
Object visit(Condition node);
// Object visit(DeclarationConstant node);
Object visit(DeclarationProgramme node);
......@@ -42,7 +42,7 @@ public interface ASTVisitor {
Object visit(Non node);
Object visit(Ou node);
// Object visit(Parentheses node);
Object visit(Parentheses node);
// Object visit(Pour node);
Object visit(Produit node);
......
......@@ -24,7 +24,7 @@ public class ByteCodeGenerator implements ASTVisitor {
return null;
}
// public Object visit(Condition node) { return null; }
public Object visit(Condition node) { return null; }
// public Object visit(DeclarationConstant node) { return null; }
......@@ -72,7 +72,7 @@ public class ByteCodeGenerator implements ASTVisitor {
public Object visit(Ou node) { return null; }
// public Object visit(Parentheses node) { return null; }
public Object visit(Parentheses node) { return null; }
// public Object visit(Pour node) { return null; }
......
......@@ -26,7 +26,7 @@ public class SemanticAnalyzer implements ASTVisitor {
return null;
}
// public Object visit(Condition node) { return null; }
public Object visit(Condition node) { return null; }
// public Object visit(DeclarationConstant node) { return null; }
......@@ -74,7 +74,7 @@ public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Ou node) { return null; }
// public Object visit(Parentheses node) { return null; }
public Object visit(Parentheses node) { return null; }
// public Object visit(Pour node) { return null; }
......
......@@ -65,25 +65,25 @@ public class SourceCodeGenerator implements ASTVisitor {
return null;
}
// public Object visit(Condition node){
// code += "si ";
// node.getCondition().accept(this);
// code += " alors";
// level += 1;
// node.getThenInstruction().accept(this);
// if (node.hasElse()){
// code += "\n";
// addTabulation(level - 1);
// code += "sinon";
// node.getElseInstruction().accept(this);
// }
// level -= 1;
// code += "\n";
// addTabulation();
// code += "finsi";
// return null;
// }
//
public Object visit(Condition node) {
code += "si ";
node.getCondition().accept(this);
code += " alors";
level += 1;
node.getThenInstruction().accept(this);
if (node.hasElse()) {
code += "\n";
addTabulation(level - 1);
code += "sinon";
node.getElseInstruction().accept(this);
}
level -= 1;
code += "\n";
addTabulation();
code += "finsi";
return null;
}
// public Object visit(DeclarationConstant node){
// Symbole sym = TDS.getInstance().identifier(new
// Entree(node.getIdentifier().getNom()));
......@@ -208,13 +208,13 @@ public class SourceCodeGenerator implements ASTVisitor {
return null;
}
//
// public Object visit(Parentheses node){
// code += "(";
// node.getExpression().accept(this);
// code += ")";
// return null;
// }
public Object visit(Parentheses node) {
code += "(";
node.getExpression().accept(this);
code += ")";
return null;
}
//
// public Object visit(Pour node){
// code += "pour ";
......
......@@ -23,7 +23,7 @@ non terminal declar_const;
non terminal Expression expr, access, operand;
non terminal for_instr;
non terminal while_instr;
non terminal cond_instr;
non terminal Condition cond_instr;
non terminal Ecrire write_instr;
non terminal Lire read_instr;
non terminal Instruction instr;
......@@ -104,7 +104,7 @@ expr ::= op_bin:exp {: RESULT = exp; :}
| relation:exp {: RESULT = exp; :}
| op_una:exp {: RESULT = exp; :}
| operand:operand {: RESULT = operand; :}
| OPENPARENT expr CLOSEPARENT {: :};
| OPENPARENT expr:exp CLOSEPARENT {: RESULT = new Parentheses(exp, "", expleft, expright); :};
body ::= instr_lst:instructions {: RESULT = new Bloc(instructions, "", instructionsleft, instructionsright); :};
......@@ -118,10 +118,10 @@ instr_lst ::= {: RESULT = new ArrayList(); :}
instr ::= assign:inst {: RESULT = inst; :}
| write_instr:e {: RESULT = e; :}
| read_instr {: :}
| cond_instr {: :}
| while_instr {: :}
| for_instr {: :};
| read_instr:r {: RESULT = r;:}
| cond_instr:c {: RESULT = c;:}
| while_instr:w {: :}
| for_instr:f {: :};
assign ::= access:dest EQUAL:e expr:src SEMICOLON {: RESULT = new Affectation(dest, src, "", eleft, eright); :};
......@@ -130,8 +130,8 @@ write_instr ::= WRITE expr:a SEMICOLON {: RESULT = new Ecrire(a, "", aleft
read_instr ::= READ IDENT:a SEMICOLON {: RESULT = new Lire(new Idf(a, "", aleft, aright), "", aleft, aright); :};
cond_instr ::= IF expr THEN body ELSE body ENDIF {: :}
| IF expr THEN body ENDIF {: :};
cond_instr ::= IF expr:condition THEN body:then_instr ELSE body:else_instr ENDIF {: RESULT = new Condition(condition, then_instr, else_instr, "", conditionleft, conditionright); :}
| IF expr:condition THEN body:then_instr ENDIF {: RESULT = new Condition(condition, then_instr, null, "", conditionleft, conditionright); :};
while_instr ::= WHILE expr DO body ENDWHILE {: :};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment