-
Notifications
You must be signed in to change notification settings - Fork 0
/
variables.h
62 lines (51 loc) · 1.3 KB
/
variables.h
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
//
// Created by Marty Spiewak on 12/9/17.
//
#include <stdlib.h>
#ifndef INTERPRETER_MARTY_SPIEWAK_VARIABLES_H
#define INTERPRETER_MARTY_SPIEWAK_VARIABLES_H
#endif //INTERPRETER_MARTY_SPIEWAK_VARIABLES_H
struct Variable {
//1 is num, 2 is char
int type;
char* name;
union {
int num;
char c;
} value;
};
typedef struct Variable Variable;
Variable* createNumVar(char* vName, int vNum) {
Variable* var = malloc(sizeof(Variable));
char* nameCopy;
nameCopy = malloc(sizeof(char) * strlen(vName));
strcpy(nameCopy, vName);
var->name = nameCopy;
var->value.num = vNum;
var->type = 1;
return var;
}
Variable* createCVar(char* vName, char vC) {
Variable* var = malloc(sizeof(Variable));
char* nameCopy;
nameCopy = malloc(sizeof(char) * strlen(vName));
strcpy(nameCopy, vName);
var->name = nameCopy;
var->value.c = vC;
var->type = 2;
return var;
}
char* varToString(Variable* v) {
char* string = (char*) malloc(sizeof(char) * 50);
strcpy(string, "Name: ");
strcat(string, v->name);
strcat(string, ", Value: ");
if(v->type == 1) {
sprintf(string, "%s %d",string, v->value.num);
}
else {
sprintf(string, "%s %c",string, v->value.c);
}
strcat(string, ".\n");
return string;
}