diff --git a/ArbreAbstrait/DeclarationConstant.java b/ArbreAbstrait/DeclarationConstant.java index 58fbcc7a099e5f35736ff8ea24cda7ea64052e85..6447ac9e5958ac0f0040726effe70a77d1d72e12 100755 --- a/ArbreAbstrait/DeclarationConstant.java +++ b/ArbreAbstrait/DeclarationConstant.java @@ -10,7 +10,7 @@ public class DeclarationConstant extends Declaration{ super(new Symbole(type, true), fl, line , col); this.exp = exp; this.id = id; - TDS.getInstance().ajouter(new Symbole(type, null), new Entree(id.getNom())); + TDS.getInstance().ajouter(new Symbole(type, true), new Entree(id.getNom())); } public Idf getIdentifier(){ diff --git a/ArbreAbstrait/DeclarationVariable.java b/ArbreAbstrait/DeclarationVariable.java new file mode 100755 index 0000000000000000000000000000000000000000..817e09b10e2dd10ed938571fd6d327cdda8328b9 --- /dev/null +++ b/ArbreAbstrait/DeclarationVariable.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; + +/* + * Represent a function declaration instruction node inside the AST. + */ + +public class DeclarationVariable extends Declaration{ + ArrayList<Idf> listeId; + + public DeclarationVariable(Ttype type, ArrayList<Idf> listeId, String fl, int line, int col){ + super(new Symbole(type, false), fl, line , col); + this.listeId = listeId; + for(Idf i : listeId){ + TDS.getInstance().ajouter(new Symbole(type, false), new Entree(i.getNom())); + } + } + + public Idf getIdentifier(){ + return listeId.get(0); + + } + + + /** + * Accepts a AST visitor + */ + Object accept(ASTVisitor visitor){ + return visitor.visit(this); + } +} diff --git a/Visitors/ASTVisitor.java b/Visitors/ASTVisitor.java index a5e185c84267257bbcab075c68b082543003d7e0..335f81881abb376339042c825e2298acc8b31e95 100755 --- a/Visitors/ASTVisitor.java +++ b/Visitors/ASTVisitor.java @@ -22,7 +22,7 @@ public interface ASTVisitor { Object visit(DeclarationConstant node); Object visit(DeclarationProgramme node); - // Object visit(DeclarationVariable node); + Object visit(DeclarationVariable node); Object visit(Diff node); Object visit(Division node); diff --git a/Visitors/ByteCodeGenerator.java b/Visitors/ByteCodeGenerator.java index fc9ebe531eb1c36e1526bf9439d0aa226d5c05a9..624ad0efd641fdeb0ae53b7e12c66d6246685f6d 100755 --- a/Visitors/ByteCodeGenerator.java +++ b/Visitors/ByteCodeGenerator.java @@ -32,7 +32,7 @@ public class ByteCodeGenerator implements ASTVisitor { return null; } - // public Object visit(DeclarationVariable node) { return null; } + public Object visit(DeclarationVariable node) { return null; } public Object visit(Diff node) { return null; } diff --git a/Visitors/SemanticAnalyzer.java b/Visitors/SemanticAnalyzer.java index 693460ca840b5e3b28c11e5664c03af8c6b7e47e..481a6a0851405f0199310fea4f34afe98cf96394 100755 --- a/Visitors/SemanticAnalyzer.java +++ b/Visitors/SemanticAnalyzer.java @@ -34,7 +34,7 @@ public class SemanticAnalyzer implements ASTVisitor { return null; } - // public Object visit(DeclarationVariable node) { return null; } + public Object visit(DeclarationVariable node) { return null; } public Object visit(Diff node) { return null; } diff --git a/Visitors/SourceCodeGenerator.java b/Visitors/SourceCodeGenerator.java index 4aea495afed79aa777f9c0d91669689e8408680d..0cef16661c8d798294f264f88fb65980f51afe57 100755 --- a/Visitors/SourceCodeGenerator.java +++ b/Visitors/SourceCodeGenerator.java @@ -107,16 +107,16 @@ public class SourceCodeGenerator implements ASTVisitor { return null; } - // public Object visit(DeclarationVariable node){ - // Symbole sym = TDS.getInstance().identifier(new - // Entree(node.getIdentifier().getNom())); - // - // code += sym + " "; - // node.getIdentifier().accept(this); - // if (!isParameterDeclaration) - // code += ";"; - // return null; - // } + public Object visit(DeclarationVariable node){ + Symbole sym = TDS.getInstance().identifier(new + Entree(node.getIdentifier().getNom())); + + code += sym + " "; + node.getIdentifier().accept(this); + if (!isParameterDeclaration) + code += ";"; + return null; + } public Object visit(Diff node) { node.getGauche().accept(this); diff --git a/hepial.cup b/hepial.cup index e10208c0205aac42ab61ac0c48ff8f694e938368..4187add024094e032c3740451642f58764871584 100755 --- a/hepial.cup +++ b/hepial.cup @@ -18,8 +18,10 @@ terminal int INTEGERCONST; // TODO: N'oubliez pas de spécifier les types des non terminaux (f.e. le non terminal "op_bin" est de type Binaire) non terminal DeclarationProgramme program, header; non terminal Bloc declar_lst, body; -non terminal ArrayList<Instruction> instr_lst, declars, declar, declar_var; -non terminal declar_const; +non terminal ArrayList<Instruction> instr_lst, declars; +non terminal Declaration declar; +non terminal DeclarationVariable declar_var; +non terminal DeclarationConstant declar_const; non terminal Expression expr, access, operand; non terminal Pour for_instr; non terminal Tantque while_instr; @@ -31,7 +33,7 @@ non terminal Affectation assign; non terminal Unaire op_una; non terminal Binaire op_bin; non terminal Relation relation; -non terminal l_ident; +non terminal ArrayList<Idf> l_ident; non terminal Ttype type; @@ -59,16 +61,21 @@ header ::= PRG IDENT:id {: RESULT = new DeclarationProgramme(new Idf( declar_lst ::= declars {: :}; -declars ::= {: :} - | declars declar {: :}; +declars ::= {: RESULT = new ArrayList();:} + | declars:ds declar:d {: ds.add(d); + RESULT = ds;:}; -declar ::= declar_var {: :} - | declar_const {: :}; +declar ::= declar_var:var {: RESULT = var;:} + | declar_const:cst {: RESULT = cst;:}; -declar_var ::= type l_ident SEMICOLON {: :}; +declar_var ::= type:t l_ident:l SEMICOLON {: RESULT = new DeclarationVariable(t, l, "", tleft, tright);:}; -l_ident ::= IDENT {: :} - | l_ident COMMA IDENT {: :}; +l_ident ::= IDENT:id {: ArrayList<Idf> liste = new ArrayList<Idf>(); + liste.add(new Idf(id,"",idleft,idright)); + RESULT = liste;:} + | l_ident:idList COMMA IDENT:id {: ArrayList<Idf> idl = new ArrayList<Idf>((Collection<? extends Idf>) idList); + idl.add(new Idf(id,"",idleft,idright)); + RESULT = idl;:}; type ::= TINTEGER {: RESULT = (new entier());:} | TBOOLEAN {: RESULT = (new booleen());:};