-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsupport.c
More file actions
104 lines (93 loc) · 2.84 KB
/
Copy pathsupport.c
File metadata and controls
104 lines (93 loc) · 2.84 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "headers/njvm.h"
#include "headers/heap.h"
#include "../lib/support.h"
#include "../lib/bigint.h"
/**
* Prints the supplied error message
*
* @param msg - the error message
*/
void fatalError(char* msg) {
changeTextColor(RED, TRANSPARENT, BRIGHT);
fprintf(stderr, "ERROR: %s\n", msg);
changeTextColor(WHITE, TRANSPARENT, RESET);
exit(E_ERR_BIG_INT);
}
/**
* Creates a new object instance on the heap
* and returns a pointer to the first byte of
* that object. The size component of the object is
* set aswell.
*
* @param size - the amount of bytes that the object shall hold
* @return A pointer to the first byte of this Object on the heap
*/
ObjRef newPrimObject(int dataSize) {
ObjRef object;
object = (ObjRef) allocate(dataSize + sizeof(int));
object->size = dataSize;
return object;
}
/**
* Creates a new complex object. These objects are used for storing
* records and arrays.
*
* @param refCount - The total amount of object references this object can hold
* @return a new object with sufficient amount of memory to hold all references
*/
ObjRef newComplexObject(int refCount) {
ObjRef object;
object = (ObjRef) allocate((sizeof(ObjRef) * refCount) + sizeof(int));
object->size = refCount | 0x1 << 31;
return object;
}
/**
* Prints details about an object to stdout.
*
* @param object - the object that is to be inspected
*/
void inspectObject(ObjRef object) {
if (object == NULL) { /* NULL-Pointer */
printf("\tNIL-Reference\n");
}
else if (IS_PRIM(object)) { /* Primitive-Object (BigInt) */
printf("\tType : Primitive\n");
printf("\tSize : %u Bytes\n", object->size);
printf("\tValue (in Base10): ");
bip.op1 = object;
bigPrint(stdout);
printf("\n");
}
else { /* Complex Object */
int i, size;
ObjRef* references;
size = GET_SIZE(object);
references = GET_REFS(object);
printf("\tType : Complex\n");
printf("\tReferencing: : %d\n", size);
for (i = 0; i < size; i++) {
printf("\t [%06d]: %p\n", i, (void*) references[i]);
}
}
}
/**
* Changes the text color of the terminal to the
* specified color. The following colors are available:
*
* RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN or WHITE
*
* If the specified color is unrecognized, WHITE will be used as default.
*
* @param color - A string representation of the color name.
*/
void changeTextColor(unsigned int foreground, unsigned int background, unsigned int state) {
char colorString[13];
sprintf(colorString, "%c[%d;%d;%dm", 0x1B, state, foreground + 30, background + 40);
printf("%s", colorString);
}
void resetTextColor(void) {
changeTextColor(WHITE, TRANSPARENT, RESET);
}