From 90cad13fefd1f1fe9870c56e58eebbebd00c0d4e Mon Sep 17 00:00:00 2001
From: chris <christian.agodomou@etu.hesge.ch>
Date: Wed, 4 Jan 2023 15:06:30 +0100
Subject: [PATCH] declarationconstant done?

---
 ArbreAbstrait/Declaration.java             | 13 ++++++++
 ArbreAbstrait/DeclarationConstant.java     | 32 +++++++++++++++++++
 ArbreAbstrait/Entree.java                  | 22 +++++++++++++
 ArbreAbstrait/Symbole.java                 | 14 +++++++++
 ArbreAbstrait/TDS.java                     | 36 ++++++++++++++++++++++
 ArbreAbstrait/{tdstype.java => Ttype.java} |  4 +--
 ArbreAbstrait/booleen.java                 |  2 +-
 ArbreAbstrait/entier.java                  |  2 +-
 Visitors/ASTVisitor.java                   |  2 +-
 Visitors/ByteCodeGenerator.java            |  2 +-
 Visitors/SemanticAnalyzer.java             |  2 +-
 Visitors/SourceCodeGenerator.java          | 22 ++++++-------
 12 files changed, 135 insertions(+), 18 deletions(-)
 create mode 100755 ArbreAbstrait/Declaration.java
 create mode 100755 ArbreAbstrait/DeclarationConstant.java
 create mode 100755 ArbreAbstrait/Entree.java
 create mode 100755 ArbreAbstrait/Symbole.java
 create mode 100755 ArbreAbstrait/TDS.java
 rename ArbreAbstrait/{tdstype.java => Ttype.java} (83%)

diff --git a/ArbreAbstrait/Declaration.java b/ArbreAbstrait/Declaration.java
new file mode 100755
index 0000000..836f383
--- /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 0000000..58fbcc7
--- /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 0000000..598fce9
--- /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 0000000..196c36f
--- /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 0000000..4c9bfe8
--- /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 cd23164..aae914d 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 5ca6477..5c07971 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 28e09f8..7923ddd 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 a9a159d..a5e185c 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 981afde..fc9ebe5 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 b1913b3..693460c 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 42b59d3..4aea495 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 ";
-- 
GitLab