-
Notifications
You must be signed in to change notification settings - Fork 0
/
char.c
executable file
·69 lines (57 loc) · 1.47 KB
/
char.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
#include <stdlib.h>
#include <stdio.h>
#include <gc.h>
#include "error.h"
#include "number.h"
#include "char.h"
#include "util.h"
TYPE*
mk_char(char c)
{
TYPE* result = mloc(sizeof(TYPE));
if (result == NULL)
{
fprintf(stderr, "MK_CHAR: could not allocate memory for type");
exit(1);
}
result->type = CHAR;
result->d.i = c;
return result;
}
int
is_char(const TYPE* sexp)
{
return sexp->type == CHAR;
}
int
is_char_equal(const TYPE* left, const TYPE* right)
{
assert_throw(is_char(left),
TYPE_ERROR,
"IS_CHAR_EQUAL: left must be a char");
assert_throw(is_char(right),
TYPE_ERROR,
"IS_CHAR_EQUAL: right must be a char");
return left->d.i == right->d.i;
}
TYPE*
char_to_integer(const TYPE* sexp)
{
assert_throw(is_char(sexp),
TYPE_ERROR,
"CHAR_TO_INTEGER: argument must be a char");
return mk_number_from_int(sexp->d.i);
}
TYPE* integer_to_char(const TYPE* sexp)
{
assert_throw(is_number(sexp), /* TODO numbers are integers now */
TYPE_ERROR,
"CHAR_TO_INTEGER: argument must be a integer");
assert_throw(is_number_gt_eq(sexp, mk_number_from_int(0)),
TYPE_ERROR,
"CHAR_TO_INTEGER: argument < 0");
assert_throw(is_number_lt_eq(sexp, mk_number_from_int(255)),
TYPE_ERROR,
"CHAR_TO_INTEGER: argument > 255");
return mk_char((char) sexp->d.i);
}