diff --git a/ArbreAbstrait/Declaration.java b/ArbreAbstrait/Declaration.java new file mode 100755 index 0000000000000000000000000000000000000000..836f38380af3290cdfedd9d7bfdde78ee2611f6a --- /dev/null +++ b/ArbreAbstrait/Declaration.java @@ -0,0 +1,13 @@ +/* + * Represent a function declaration instruction node inside the AST. + */ + +public abstract class Declaration extends Instruction{ + Symbole id; + + public Declaration(Symbole id, String fl, int line, int col){ + super(fl,line,col); + this.id = id; + } + +} diff --git a/ArbreAbstrait/DeclarationConstant.java b/ArbreAbstrait/DeclarationConstant.java new file mode 100755 index 0000000000000000000000000000000000000000..58fbcc7a099e5f35736ff8ea24cda7ea64052e85 --- /dev/null +++ b/ArbreAbstrait/DeclarationConstant.java @@ -0,0 +1,32 @@ +/* + * Represent a function declaration instruction node inside the AST. + */ + +public class DeclarationConstant extends Declaration{ + Expression exp; + Idf id; + + public DeclarationConstant(Ttype type, Idf id, Expression exp, String fl, int line, int col){ + 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())); + } + + public Idf getIdentifier(){ + return this.id; + + } + + public Expression getConstantExpression(){ + return this.exp; + } + + + /** + * Accepts a AST visitor + */ + Object accept(ASTVisitor visitor){ + return visitor.visit(this); + } +} diff --git a/ArbreAbstrait/Entree.java b/ArbreAbstrait/Entree.java new file mode 100755 index 0000000000000000000000000000000000000000..598fce917e883d13ce24a29039c6eb5a85d49256 --- /dev/null +++ b/ArbreAbstrait/Entree.java @@ -0,0 +1,22 @@ +import java.util.LinkedHashMap; + +/* + * Represent a function declaration instruction node inside the AST. + */ + +public class Entree{ + private String varName; + + public Entree(String str){ + this.varName = str; + } + + public String getNom() { + return this.varName; + } + @Override + public boolean equals(Object obj) { + // TODO Auto-generated method stub + return this.varName.equals(((Entree)obj).getNom()); + } +} diff --git a/ArbreAbstrait/Symbole.java b/ArbreAbstrait/Symbole.java new file mode 100755 index 0000000000000000000000000000000000000000..196c36fcb74d3bf971b1a92fcce19eb410c5abe8 --- /dev/null +++ b/ArbreAbstrait/Symbole.java @@ -0,0 +1,14 @@ +/* + * Represent a function declaration instruction node inside the AST. + */ + +public class Symbole{ + Ttype type; + Boolean cst; + + public Symbole(Ttype type, Boolean cst){ + this.type = type; + this.cst = cst; + } + +} diff --git a/ArbreAbstrait/TDS.java b/ArbreAbstrait/TDS.java new file mode 100755 index 0000000000000000000000000000000000000000..4c9bfe8e6812c131560e39b226c6b4521a0e0f4c --- /dev/null +++ b/ArbreAbstrait/TDS.java @@ -0,0 +1,36 @@ +import java.util.LinkedHashMap; + +/* + * Represent a function declaration instruction node inside the AST. + */ + +public class TDS{ + public Symbole s; + public Entree e; + + private static TDS crtInstance = new TDS(); + LinkedHashMap<Entree, Symbole> map; + + private TDS(){ + this.map = new LinkedHashMap<Entree, Symbole>(); + } + + public static TDS getInstance(){ + return crtInstance; + } + + + public Symbole identifier(Entree entree){ + for (Entree i : map.keySet()) { + if(i.getNom().equals(entree.getNom())){ + return map.get(i); + } + } + return null; + + } + public void ajouter(Symbole s, Entree e){ + map.put(e,s); + + } +} diff --git a/ArbreAbstrait/tdstype.java b/ArbreAbstrait/Ttype.java similarity index 83% rename from ArbreAbstrait/tdstype.java rename to ArbreAbstrait/Ttype.java index cd23164c9152697619c8f362c8e10a6162fb7c5d..aae914d38d4496293c876d15549baa780308aeff 100644 --- a/ArbreAbstrait/tdstype.java +++ b/ArbreAbstrait/Ttype.java @@ -1,6 +1,6 @@ -public abstract class tdstype{ +public abstract class Ttype{ String type; - public tdstype(String type) { + public Ttype(String type) { this.type = type; } diff --git a/ArbreAbstrait/booleen.java b/ArbreAbstrait/booleen.java index 5ca6477e3f0fda4519a6f520ca8c24f41b80254e..5c079718012405c3f8a14f89f48865fcc83b9f9a 100644 --- a/ArbreAbstrait/booleen.java +++ b/ArbreAbstrait/booleen.java @@ -1,4 +1,4 @@ -public class booleen extends tdstype{ +public class booleen extends Ttype{ public booleen() { super("booleen"); } diff --git a/ArbreAbstrait/entier.java b/ArbreAbstrait/entier.java index 28e09f81e0d60cde46ef22b24542c7389b2030d3..7923ddddc53bda54888ac5d048ab91254997564f 100644 --- a/ArbreAbstrait/entier.java +++ b/ArbreAbstrait/entier.java @@ -1,4 +1,4 @@ -public class entier extends tdstype{ +public class entier extends Ttype{ public entier() { super("entier"); } diff --git a/Visitors/ASTVisitor.java b/Visitors/ASTVisitor.java index a9a159d58e73ff62c3a2ed739796ab6cbe89fbdd..a5e185c84267257bbcab075c68b082543003d7e0 100755 --- a/Visitors/ASTVisitor.java +++ b/Visitors/ASTVisitor.java @@ -19,7 +19,7 @@ public interface ASTVisitor { Object visit(Chaine node); Object visit(Condition node); - // Object visit(DeclarationConstant node); + Object visit(DeclarationConstant node); Object visit(DeclarationProgramme node); // Object visit(DeclarationVariable node); diff --git a/Visitors/ByteCodeGenerator.java b/Visitors/ByteCodeGenerator.java index 981afdec81234db6f7bbd3a1494863c553676771..fc9ebe531eb1c36e1526bf9439d0aa226d5c05a9 100755 --- a/Visitors/ByteCodeGenerator.java +++ b/Visitors/ByteCodeGenerator.java @@ -26,7 +26,7 @@ public class ByteCodeGenerator implements ASTVisitor { public Object visit(Condition node) { return null; } - // public Object visit(DeclarationConstant node) { return null; } + public Object visit(DeclarationConstant node) { return null; } public Object visit(DeclarationProgramme node) { return null; diff --git a/Visitors/SemanticAnalyzer.java b/Visitors/SemanticAnalyzer.java index b1913b3e4b55768c40569dc4558d560dd2a5b973..693460ca840b5e3b28c11e5664c03af8c6b7e47e 100755 --- a/Visitors/SemanticAnalyzer.java +++ b/Visitors/SemanticAnalyzer.java @@ -28,7 +28,7 @@ public class SemanticAnalyzer implements ASTVisitor { public Object visit(Condition node) { return null; } - // public Object visit(DeclarationConstant node) { return null; } + public Object visit(DeclarationConstant node) { return null; } public Object visit(DeclarationProgramme node) { return null; diff --git a/Visitors/SourceCodeGenerator.java b/Visitors/SourceCodeGenerator.java index 42b59d302f4f05cdfc258214a482e3920c3a4a20..4aea495afed79aa777f9c0d91669689e8408680d 100755 --- a/Visitors/SourceCodeGenerator.java +++ b/Visitors/SourceCodeGenerator.java @@ -84,17 +84,17 @@ public class SourceCodeGenerator implements ASTVisitor { return null; } - // public Object visit(DeclarationConstant node){ - // Symbole sym = TDS.getInstance().identifier(new - // Entree(node.getIdentifier().getNom())); - // - // code += sym + " "; - // node.getIdentifier().accept(this); - // code += " = "; - // node.getConstantExpression().accept(this); - // code += ";"; - // return null; - // } + public Object visit(DeclarationConstant node){ + Symbole sym = TDS.getInstance().identifier(new + Entree(node.getIdentifier().getNom())); + + code += sym + " "; + node.getIdentifier().accept(this); + code += " = "; + node.getConstantExpression().accept(this); + code += ";"; + return null; + } public Object visit(DeclarationProgramme node) { code += "programme ";