summaryrefslogtreecommitdiff
path: root/val.c
diff options
context:
space:
mode:
Diffstat (limited to 'val.c')
-rw-r--r--val.c40
1 files changed, 33 insertions, 7 deletions
diff --git a/val.c b/val.c
index 9e832a6..41d3a06 100644
--- a/val.c
+++ b/val.c
@@ -1,5 +1,18 @@
#include <stdio.h>
+#include <string.h>
#include "val.h"
+#include "vm.h"
+
+ObjString *objstring_new(char *src, size_t len) {
+ char *d = M(NULL, (1 + len) * sizeof (char));
+ memcpy(d, src, len);
+ d[len] = '\0';
+ ObjString *o = M(NULL, sizeof(ObjString));
+ o->obj.oty = OTY_STRING;
+ o->len = len;
+ o->b = d;
+ return o;
+}
void print_val(Val v) {
switch (v.ty) {
@@ -12,20 +25,33 @@ void print_val(Val v) {
case TY_BOOL:
printf("%s",AS_BOOL(v) ? "true" : "false");
break;
+ case TY_OBJ:
+ switch (AS_OBJ(v)->oty) {
+ case OTY_STRING:
+ printf("%s", AS_CSTRING(v));
+ break;
+ }
+ break;
}
}
+
void println_val(Val v) {
print_val(v);
putchar('\n');
}
-static const char *ty_names[] = {
- "nil",
- "num",
- "bool",
-};
-const char *valty_str(ValTy ty) {
- return ty_names[ty];
+const char *valty_str(Val v) {
+ switch(v.ty) {
+ case TY_NIL: return "nil";
+ case TY_NUM: return "num";
+ case TY_BOOL: return "bool";
+ case TY_OBJ:
+ switch (AS_OBJ(v)->oty) {
+ case OTY_STRING: return "String";
+ }
+ break;
+ }
+ return "???";
}