From 4856cbf23be2c1a2f5b8cac563e37278e6ac568e Mon Sep 17 00:00:00 2001 From: chris <christian.agodomou@etu.hesge.ch> Date: Thu, 5 Jan 2023 17:09:53 +0100 Subject: [PATCH] declar var declars lident done --- ArbreAbstrait/DeclarationConstant.java | 2 +- ArbreAbstrait/DeclarationVariable.java | 30 ++++++++++++++++++++++++++ Visitors/ASTVisitor.java | 2 +- Visitors/ByteCodeGenerator.java | 2 +- Visitors/SemanticAnalyzer.java | 2 +- Visitors/SourceCodeGenerator.java | 20 ++++++++--------- hepial.cup | 27 ++++++++++++++--------- 7 files changed, 61 insertions(+), 24 deletions(-) create mode 100755 ArbreAbstrait/DeclarationVariable.java diff --git a/ArbreAbstrait/DeclarationConstant.java b/ArbreAbstrait/DeclarationConstant.java index 58fbcc7..6447ac9 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 0000000..817e09b --- /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 a5e185c..335f818 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 fc9ebe5..624ad0e 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 693460c..481a6a0 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 4aea495..0cef166 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 e10208c..4187add 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());:}; -- GitLab