This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.c
148 lines (129 loc) · 4.06 KB
/
value.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "value.h"
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
bool value_is_falsy(const Value v) {
return IS_NIL(v) || (IS_BOOL(v) && AS_BOOL(v) == false);
}
bool value_eq(Value lhs, Value rhs) {
if (lhs.type != rhs.type) {
return false;
}
switch (lhs.type) {
case VAL_BOOL:
return AS_BOOL(lhs) == AS_BOOL(rhs);
case VAL_NIL:
return true;
case VAL_NUMBER:
return fabs(AS_NUMBER(lhs) - AS_NUMBER(rhs)) < DBL_EPSILON;
case VAL_OBJ:
if (!IS_STRING(lhs) || !IS_STRING(rhs)) {
return false;
}
if (AS_STRING(lhs)->len != AS_STRING(rhs)->len) {
return false;
}
return memcmp(AS_STRING(lhs)->s, AS_STRING(rhs)->s,
AS_STRING(lhs)->len) == 0;
}
}
static ObjString* value_obj_str_allocate(Obj** objects, size_t size) {
ObjString* obj = NULL;
REALLOC_SAFE(&obj, size);
obj->obj.next = *objects;
*objects = &obj->obj;
return obj;
}
ObjString* value_make_string(Obj** objects, size_t s_len) {
ObjString* os = value_obj_str_allocate(objects, sizeof(ObjString) + s_len);
os->len = s_len;
LOG("allocated string size=%zu\n", os->len);
os->obj.type = OBJ_STRING;
return os;
}
bool value_obj_is_type(Value v, ObjType type) {
return IS_OBJ(v) && AS_OBJ(v)->type == type;
}
ObjFunction* value_make_fn(const char* name, size_t name_len) {
ObjFunction* fn = NULL;
REALLOC_SAFE(&fn, sizeof(ObjFunction) + name_len);
fn->obj.type = OBJ_FUNCTION;
fn->arity = 0;
fn->chunk = (Chunk){0};
memcpy(fn->name, name, name_len);
fn->name_len = name_len;
return fn;
}
ObjNative* value_make_fn_native(NativeFn fn) {
ObjNative* n = NULL;
REALLOC_SAFE(&n, sizeof(ObjNative));
n->obj.type = OBJ_NATIVE;
n->fn = fn;
return n;
}
const char* value_to_str(Value v) {
static char str[UINT8_MAX + 1] = "";
memset(str, 0, UINT8_MAX + 1);
switch (v.type) {
case VAL_BOOL:
snprintf(str, UINT8_MAX, "%s", v.as.boolean ? "true" : "false");
break;
case VAL_NIL:
snprintf(str, UINT8_MAX, "nil");
break;
case VAL_NUMBER:
snprintf(str, UINT8_MAX, "%g", v.as.number);
break;
case VAL_OBJ:
switch (AS_OBJ(v)->type) {
case OBJ_STRING:
snprintf(str, UINT8_MAX, "%.*s", (int)AS_STRING(v)->len,
AS_CSTRING(v));
break;
case OBJ_FUNCTION:
snprintf(str, UINT8_MAX, "fn@%.*s", (int)AS_FN(v)->name_len,
AS_FN(v)->name);
break;
case OBJ_NATIVE:
snprintf(str, UINT8_MAX, "fn@<native>");
break;
}
break;
}
return str;
}
const char* value_to_str_debug(Value v) {
static char str[UINT8_MAX + 1] = "";
memset(str, 0, UINT8_MAX + 1);
switch (v.type) {
case VAL_BOOL:
snprintf(str, UINT8_MAX, "boolean (%s)",
v.as.boolean ? "true" : "false");
break;
case VAL_NIL:
snprintf(str, UINT8_MAX, "nil");
break;
case VAL_NUMBER:
snprintf(str, UINT8_MAX, "number (%g)", v.as.number);
break;
case VAL_OBJ:
switch (AS_OBJ(v)->type) {
case OBJ_STRING:
snprintf(str, UINT8_MAX, "string (\"%.*s\")",
(int)AS_STRING(v)->len, AS_CSTRING(v));
break;
case OBJ_FUNCTION:
snprintf(str, UINT8_MAX, "function (fn@%.*s)",
(int)AS_FN(v)->name_len, AS_FN(v)->name);
break;
case OBJ_NATIVE:
snprintf(str, UINT8_MAX, "fn@<native>");
break;
}
break;
}
return str;
}