aboutsummaryrefslogtreecommitdiff
path: root/src/symboltable.c
blob: 3f22b44e0e7080c68797b1e5a1a80a1459375ee1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "symboltable.h"

extern int lineno; /* from lexical analyser */

SymbolTable symbol_table = {{{{0},0}},MAXSYMBOLS,0};

void addVar(const char name[], int type) {
  int count;
  for (count = 0; count < symbol_table.size; count++) {
    if (!strcmp(symbol_table.entries[count].name, name)) {
      printf("semantic error, redefinition of variable %s near line %d\n", name,
             lineno);
      return;
    }
  }
  if (++symbol_table.size > symbol_table.maxsize) {
    printf("too many variables near line %d\n", lineno);
    exit(1);
  }
  strcpy(symbol_table.entries[symbol_table.size - 1].name, name);
  symbol_table.entries[symbol_table.size - 1].type = type;
}

void lookup(const char name[]) {
  int count;

  for (count = 0; count < symbol_table.size; count++) {
    if (!strcmp(symbol_table.entries[count].name, name)) {
      return;
    }
  }
  printf("No definition of the variable %s near line %d\n", name,
           lineno);
}

void display_table(){
    int count;
    for (count=0;count<symbol_table.size;count++) {
        if(symbol_table.entries[count].type == INT)
            printf("entier: %s, pos: %d     \n", symbol_table.entries[count].name, symbol_table.entries[count].addr);
        else
            printf("caractere: %s, pos: %d       \n", symbol_table.entries[count].name, symbol_table.entries[count].addr);
    }
    printf("\n");
}

int get_type(const char name[]){
	int count;

  for (count = 0; count < symbol_table.size; count++) {
    if (!strcmp(symbol_table.entries[count].name, name)) {
      return symbol_table.entries[count].type;
    }
  }
  return -1;
}