-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbase.c
More file actions
executable file
·109 lines (98 loc) · 2.27 KB
/
Copy pathbase.c
File metadata and controls
executable file
·109 lines (98 loc) · 2.27 KB
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
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base.h"
Type
type_get_from_lexeme(const char *lexeme)
{
if (!strcasecmp (lexeme, "Int"))
return INTEGER;
else if (!strcasecmp (lexeme, "Bool"))
return BOOLEAN;
else if (!strcasecmp (lexeme, "Char"))
return CHAR;
else
return VOID;
}
char *
type_get_lexeme(Type type)
{
switch (type) {
case VOID:
return "void";
case INTEGER:
return "int";
case BOOLEAN:
return "bool";
case CHAR:
return "char";
case ERROR:
return "error";
default:
return "";
}
}
void
value_print(FILE *file, Value *value, Type type)
{
if (type == INTEGER) {
fprintf(file, "%d", value->integer);
} else if (type == BOOLEAN) {
fprintf(file, "%s", value->boolean ? "true" : "false");
} else if (type == CHAR) {
fprintf(file, "'%c'", value->character);
}
}
void
value_get(Value *value, Type type, void *val)
{
if (value == NULL) {
fprintf(stderr, "base.c: value_get: value == NULL\n");
exit(1);
}
if (type == INTEGER) {
*((int *) val) = value->integer;
} else if (type == BOOLEAN) {
*((bool *) val) = value->boolean;
} else if (type == CHAR) {
*((char *) val) = value->character;
} else {
fprintf(stderr, "base.c: value_get: unknow type\n");
exit(1);
}
}
void
value_set(Value *value, Type type, void *val)
{
if (value == NULL) {
fprintf(stderr, "base.c: value_set: value == NULL\n");
exit(1);
}
if (type == VOID || val == NULL) {
value->integer = 0;
} else if (type == INTEGER) {
value->integer = *((int *) val);
} else if (type == BOOLEAN) {
value->boolean = *((bool *) val);
} else if (type == CHAR) {
value->character = *((char *) val);
} else {
fprintf(stderr, "base.c: value_set: unknow type\n");
exit(1);
}
}
void
value_set_from_int(Value *value, int val)
{
value_set(value, INTEGER, VOID(val));
}
void
value_set_from_bool(Value *value, bool val)
{
value_set(value, BOOLEAN, VOID(val));
}
void
value_set_from_char(Value *value, char val)
{
value_set(value, CHAR, VOID(val));
}