-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialization.c
104 lines (96 loc) · 2.22 KB
/
initialization.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "initialization.h"
#include "mfcalc.tab.h"
#include "abb.h"
#include "commands.h"
//Initializes the name of the symbol
void _initializeName(tipoelem* t, const char* name){
size_t nameSize = strlen(name)+1;
t->name = (char*)malloc(nameSize*sizeof(char));
strncpy(t->name, name, nameSize);
}
/*
* Initializes the symbol table of the calculator with a set of
* commands, constants and functions.
*/
void initializeSymbolTable(abb* sym_table) {
//Struct for initialization of functions
struct init_fun
{
char const *name;
func_t *fun;
};
//Functions which will be inserted in the symbol table
struct init_fun const funs[] =
{
{ "atan", atan },
{ "cos", cos },
{ "exp", exp },
{ "ln", log },
{ "sin", sin },
{ "sqrt", sqrt },
{ 0, 0 },
};
//Struct for initialization of commands
struct init_com
{
char const *name;
command_t *comm;
};
//Commands which will be inserted in the symbol table
struct init_com const coms[] =
{
{ "workspace", comWorkspace },
{ "clean", comClean },
{ "load", comLoad },
{ "read", comRead },
{ "help", comHelp },
{ "echo", comEcho },
{ 0, 0 }
};
//Struct for initialization of constants
struct init_const
{
char const *name;
double var;
};
//Constants which will be inserted in the symbol table
struct init_const const consts[] =
{
{ "pi", M_PI },
{ "e" , exp(1) },
{ 0, 0 }
};
/* Put functions in table. */
for (int i = 0; funs[i].name; i++)
{
tipoelem t;
_initializeName(&t, funs[i].name);
t.type = FUN;
t.lvalue = 0;
t.value.fun = funs[i].fun;
insertar(sym_table, t);
}
/* Put commands in table. */
for (int i = 0; coms[i].name; i++)
{
tipoelem t;
_initializeName(&t, coms[i].name);
t.type = COM;
t.lvalue = 0;
t.value.comm = coms[i].comm;
insertar(sym_table, t);
}
/* Put constants in table. */
for (int i = 0; consts[i].name; i++)
{
tipoelem t;
_initializeName(&t, consts[i].name);
t.type = VAR;
t.lvalue = 0;
t.value.var = consts[i].var;
insertar(sym_table, t);
}
}