-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
98 lines (86 loc) · 1.66 KB
/
utils.cpp
File metadata and controls
98 lines (86 loc) · 1.66 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
//
// Utility functions
//
#include "consVM.h"
//
// Global variables
//
bool tracing = false;
//
// Global functions
//
bool is_true(Cell* p)
{
return (p != nil);
}
void make_list(int n)
{
push(nil);
while (n-- > 0) {
cons();
}
}
void init_tracing()
{
const char* TRACE = std::getenv("TRACE");
if (TRACE == NULL) {
tracing = false;
} else if (std::strcmp(TRACE, "on") == 0) {
tracing = true;
} else {
tracing = false;
}
}
void trace(const char* tag, Cell* cell, Cell* cell2)
{
if (tracing) {
std::cout << "[trace] " << tag << std::endl;
if (cell != NULL) {
std::cout << " arg1: "; print(cell); std::cout << std::endl;
}
if (cell2 != NULL) {
std::cout << " arg2: "; print(cell2); std::cout << std::endl;
}
std::cout << std::endl;
}
}
static bool is_valid_tag(Tag t)
{
switch (t) {
case Tag::ATOM_TAG:
case Tag::CONS_TAG:
case Tag::STRING_TAG:
return true;
default:
return false;
}
}
void validate_cell_ptr(Cell* p)
{
if (p == NULL) {
throw LispError("validate_cell_ptr: NULL pointer", true);
}
switch (p->type)
{
case Tag::ATOM_TAG:
break;
case Tag::CONS_TAG:
if (car(p) == NULL)
{
throw LispError("validate_cell_ptr: NULL car ptr", true);
}
if (cdr(p) == NULL)
{
throw LispError("validate_cell_ptr: NULL cdr ptr", true);
}
if (!is_valid_tag(car(p)->type)) {
throw LispError("validate_cell_ptr: Bad car ptr", true);
}
if (!is_valid_tag(cdr(p)->type)) {
throw LispError("validate_cell_ptr: Bad cdr ptr", true);
}
break;
default:
LispError("validate_cell_ptr: Invalid cell tag", true);
}
}