-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol-table.c
47 lines (42 loc) · 905 Bytes
/
symbol-table.c
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
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "tokens.h"
#include "symbol-table.h"
SymbolTable *new_symbol_table()
{
SymbolTable *table = calloc(1, sizeof(SymbolTable));
table->variables = calloc(LINE_LIMIT, sizeof(Variable));
table->num_variables = 0;
return table;
}
void add_variable(SymbolTable *table, Variable var)
{
table->variables[table->num_variables] = var;
table->num_variables++;
}
Variable *lookup_variable(SymbolTable *table, char *name)
{
for (int i = 0; i < table->num_variables; i++)
{
if (strcmp(table->variables[i].name, name) == 0)
{
return &table->variables[i];
}
}
return NULL;
}
VariableType get_var_type(TokenType type)
{
switch (type)
{
case TKN_TYPE_SCALAR:
return TYPE_SCALAR;
case TKN_TYPE_VECTOR:
return TYPE_VECTOR;
case TKN_TYPE_MATRIX:
return TYPE_MATRIX;
default:
return 0;
}
}