-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal.c
More file actions
129 lines (106 loc) · 1.96 KB
/
internal.c
File metadata and controls
129 lines (106 loc) · 1.96 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
void error_out_of_bounds(int32_t index)
{
printf("Error: Array index %d is out of bounds.\n", index);
exit(-1);
}
void error_bad_char(int8_t c)
{
printf("Error: %d is not an ASCII character.\n", (int) c);
exit(-1);
}
void error_overflow(void)
{
printf("Error: Overflowed or underflowed integer.\n");
exit(-1);
}
void error_divide_zero(void)
{
printf("Error: Divide by zero.\n");
exit(-1);
}
void error_null(void)
{
printf("Error: Try to access a null pointer.\n");
exit(-1);
}
void error_pair_null(void)
{
printf("Error: Try to access a null pair.\n");
exit(-1);
}
void error_free_null(void)
{
printf("Error: Try to free a null pointer.\n");
exit(-1);
}
void error_out_of_memory(void)
{
printf("Error: Out of memory.\n");
exit(-1);
}
void print_string(const char* s)
{
printf("%s", s);
fflush(stdout);
}
void print_char(const char c)
{
putchar(c);
fflush(stdout);
}
void print_int(const int32_t i)
{
printf("%i", i);
fflush(stdout);
}
void print_bool(const char b)
{
printf("%s", b ? "true" : "false");
fflush(stdout);
}
void print_line_break(void)
{
puts("");
fflush(stdout);
}
void print_pointer(void* p)
{
if (p == NULL)
printf("(nil)");
else
printf("%p", p);
fflush(stdout);
}
int64_t seek_array_element1(int64_t array, int32_t index)
{
if (index < 0)
error_out_of_bounds(index);
return array + index;
}
int64_t seek_array_element4(int64_t array, int32_t index)
{
if (index < 0)
error_out_of_bounds(index);
return array + (index << 2);
}
int64_t seek_array_element8(int64_t array, int32_t index)
{
if (index < 0)
error_out_of_bounds(index);
return array + (index << 3);
}
int32_t read_int(void)
{
int i;
scanf("%d", &i);
return (int32_t) i;
}
int8_t read_char(void)
{
char c;
scanf("%c", &c);
return (int8_t) c;
}