Browse Source

Table des symboles

Nahom 3 years ago
parent
commit
2b9f6bde69
4 changed files with 186 additions and 0 deletions
  1. BIN
      .DS_Store
  2. 13
    0
      Table_Symboles/main.c
  3. 100
    0
      Table_Symboles/table_symboles.c
  4. 73
    0
      Table_Symboles/table_symboles.h

BIN
.DS_Store View File


+ 13
- 0
Table_Symboles/main.c View File

@@ -0,0 +1,13 @@
1
+#include <stdio.h>
2
+#include "table_symboles.h"
3
+
4
+int main() {
5
+    Table_Symboles T;
6
+    initialise_table(&T);
7
+    add_symbole_top(&T, "toto", 0, 1);
8
+    add_symbole_top(&T, "titi", 1, 0);
9
+    add_symbole_bottom(&T, "titi", 0, 1);
10
+    print_table(&T);
11
+
12
+    return 0;
13
+}

+ 100
- 0
Table_Symboles/table_symboles.c View File

@@ -0,0 +1,100 @@
1
+//
2
+// Created by Nahom Belay on 31/03/2021.
3
+//
4
+
5
+#include "table_symboles.h"
6
+#include <stdio.h>
7
+#include <string.h>
8
+
9
+
10
+void initialise_table(Table_Symboles * table){
11
+    table->indexAvailableBottom = TABLE_SIZE - 1;
12
+    table->indexAvailableTop = 0;
13
+}
14
+
15
+int variable_exists(Table_Symboles * table, char * varName){
16
+    for (int i = 0; i < table->indexAvailableTop; i++){
17
+        if (strcmp(varName, table->array[i].Variable_Name) == 0){
18
+            return -1;
19
+        }
20
+    }
21
+
22
+    for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){
23
+        if (strcmp(varName, table->array[i].Variable_Name) == 0){
24
+            return -1;
25
+        }
26
+    }
27
+    return 0;
28
+}
29
+int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){
30
+    Symbole symbole;
31
+    symbole.Variable_Name = varName;
32
+    symbole.addr = table->indexAvailableTop;
33
+    symbole.init = init;
34
+    symbole.type = type;
35
+    if (table->indexAvailableTop >= table->indexAvailableBottom){
36
+        return -1;
37
+    } else if (variable_exists(table, varName) == -1){
38
+        return -2;
39
+    } else {
40
+        table->array[table->indexAvailableTop] = symbole;
41
+        table->indexAvailableTop++;
42
+    }
43
+    return 0;
44
+}
45
+
46
+int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){
47
+    Symbole symbole;
48
+    symbole.Variable_Name = varName;
49
+    symbole.addr = table->indexAvailableBottom;
50
+    symbole.init = init;
51
+    symbole.type = type;
52
+    if (table->indexAvailableTop >= table->indexAvailableBottom){
53
+        return -1;
54
+    } else if (variable_exists(table, varName) == 1){
55
+        return -2;
56
+    } else {
57
+        table->array[table->indexAvailableBottom] = symbole;
58
+        table->indexAvailableBottom--;
59
+    }
60
+    return 0;
61
+}
62
+
63
+void print_symbole(Symbole * symbole){
64
+    char * var = symbole->Variable_Name;
65
+    int addr = symbole->addr;
66
+    int type = symbole->type;
67
+    char * typeStr;
68
+    if (type == 0){
69
+        typeStr = "INT";
70
+    } else{
71
+        typeStr = "CONST_INT";
72
+    }
73
+    int init = symbole->init;
74
+    char * initStr;
75
+    if (type == 0){
76
+        initStr = "INITIALISED";
77
+    } else{
78
+        initStr = "NOT_INITIALISED";
79
+    }
80
+    printf("%-20s\t\t %-12s\t\t %-12d\t %-12s\n", var, typeStr, addr, initStr);
81
+}
82
+
83
+void print_table(Table_Symboles * table){
84
+    printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised");
85
+    int indexTop = table->indexAvailableTop;
86
+    int indexBottom = table->indexAvailableBottom;
87
+    Symbole symbole;
88
+    for (int i = 0; i < indexTop; i++){
89
+        symbole = table->array[i];
90
+        print_symbole(&symbole);
91
+    }
92
+    if (table->indexAvailableBottom != TABLE_SIZE - 1){
93
+        printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "...", "...", "...", "...");
94
+        for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
95
+            symbole = table->array[i];
96
+            print_symbole(&symbole);
97
+        }
98
+    }
99
+
100
+}

+ 73
- 0
Table_Symboles/table_symboles.h View File

@@ -0,0 +1,73 @@
1
+//
2
+// Created by Nahom Belay on 31/03/2021.
3
+//
4
+
5
+#ifndef TABLE_SYMBOLES_H
6
+#define TABLE_SYMBOLES_H
7
+
8
+#define TABLE_SIZE 50
9
+
10
+enum Symbole_Type {TYPE_INT , TYPE_CONST_INT};
11
+enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
12
+
13
+typedef struct Symboles {
14
+    char * Variable_Name;
15
+    int addr ;
16
+    enum Symbole_Type type;
17
+    enum Initialised_Variable init;
18
+} Symbole;
19
+
20
+typedef struct Table_Symboles {
21
+    Symbole array[TABLE_SIZE];
22
+    int indexAvailableTop;
23
+    int indexAvailableBottom;
24
+} Table_Symboles;
25
+
26
+/**
27
+ * Initialises indexAvailableTop at 0 and indexAvailableBottom at TABLE_SIZE - 1
28
+ * @param table
29
+ */
30
+void initialise_table(Table_Symboles * table);
31
+
32
+/**
33
+ * Adds a symbole at the top (regular varaibles)
34
+ * @param table
35
+ * @param varName
36
+ * @param type
37
+ * @param init
38
+ * @return if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table
39
+ */
40
+int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init);
41
+
42
+/**
43
+ * Adds a symbole at the bottom (temp variables)
44
+ * @param table
45
+ * @param varName
46
+ * @param type
47
+ * @param init
48
+ * @return 0 if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table
49
+ */
50
+int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init);
51
+
52
+/**
53
+ * Verifies if a varaible name is already present in the table to avoid duplicates
54
+ * @param table
55
+ * @param varName
56
+ * @return -1 if the varaible name exists, 0 if it doesn't
57
+ */
58
+int variable_exists(Table_Symboles * table, char * varName);
59
+
60
+/**
61
+ * Prints a symbole with this format
62
+ * varName      | Type  | Address   | Initialised/Not_Initialised
63
+ * @param symbole
64
+ */
65
+void print_symbole(Symbole * symbole);
66
+
67
+/**
68
+ * Prints the table
69
+ * @param table
70
+ */
71
+void print_table(Table_Symboles * table);
72
+
73
+#endif TABLE_SYMBOLES_H

Loading…
Cancel
Save