-
Notifications
You must be signed in to change notification settings - Fork 11
/
Util.HC
executable file
·97 lines (83 loc) · 1.44 KB
/
Util.HC
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
#ifndef UTIL_HC
#define UTIL_HC
// Utility functions.
#include "Exception.HC"
extern class String;
extern VOID StringConcatC(String *s, CHAR *c);
extern class Malval;
extern Malval *MalStringMk(String *s);
VOID NullCheck(CHAR *x, CHAR *xname)
{
String *s;
if (!x) {
s = StringMk("NullCheck failed for ");
StringConcatC(s, xname);
throwval(MalStringMk(s));
}
}
VOID NullThrow(CHAR *s)
{
throws(s);
}
// assert x >= y
VOID LtCheck(INT x, INT y, CHAR *xname)
{
if (x < y) {
"%s is less than %ld.\n", xname, y;
throws("");
}
}
// assert x > y
VOID LeCheck(INT x, INT y, CHAR *xname)
{
if (x <= y) {
"%s is not greater than %ld.\n", xname, y;
throws("");
}
}
// assert x <= y
VOID GtCheck(INT x, INT y, CHAR *xname)
{
if (x > y) {
"%s is greater than than %ld.\n", xname, y;
throws("");
}
}
// assert x < y
VOID GeCheck(INT x, INT y, CHAR *xname)
{
if (x >= y) {
"%s is not less than %ld.\n", xname, y;
throws("");
}
}
// assert y <= x <= z
VOID BoundsCheck(INT x, INT y, INT z, CHAR *xname)
{
if (x < y) {
"%s is less than %ld.\n", xname, y;
throws("");
}
if (z < x) {
"%s is greater than %ld", xname, z;
throws("");
}
}
// Return the least power of 2 that is greater than or equal to n
INT NextPowerOf2(INT n)
{
INT i = 1;
while (i < n) {
i = i << 1;
}
return i;
}
BOOL IsDigit(CHAR c)
{
return 48 <= c < 58;
}
VOID PrintError(CHAR *c)
{
"%s\n", c;
}
#endif