1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 09:17:17 +00:00

Remove ValuePrint. Improve ValueToString for reference types.

This commit is contained in:
Calvin Rose 2017-02-13 00:11:30 -05:00
parent 37faac1f8a
commit 42ecaf301a
4 changed files with 256 additions and 313 deletions

26
main.c
View File

@ -7,16 +7,21 @@
#include "value.h" #include "value.h"
#include "disasm.h" #include "disasm.h"
void StringPut(uint8_t * string) {
uint32_t i;
uint32_t len = VStringSize(string);
for (i = 0; i < len; ++i)
fputc(string[i], stdout);
}
/* Test c function */ /* Test c function */
Value print(VM * vm) { Value print(VM * vm) {
uint32_t i, j, count; uint32_t j, count;
Value nil; Value nil;
count = VMCountArgs(vm); count = VMCountArgs(vm);
for (j = 0; j < count; ++j) { for (j = 0; j < count; ++j) {
uint8_t * string = ValueToString(vm, VMGetArg(vm, j)); uint8_t * string = ValueToString(vm, VMGetArg(vm, j));
uint32_t len = VStringSize(string); StringPut(string);
for (i = 0; i < len; ++i)
fputc(string[i], stdout);
fputc('\n', stdout); fputc('\n', stdout);
} }
nil.type = TYPE_NIL; nil.type = TYPE_NIL;
@ -25,7 +30,7 @@ Value print(VM * vm) {
/* A simple repl for debugging */ /* A simple repl for debugging */
void debugRepl() { void debugRepl() {
char buffer[128] = {0}; char buffer[1024] = {0};
const char * reader = buffer; const char * reader = buffer;
Value func; Value func;
VM vm; VM vm;
@ -46,7 +51,7 @@ void debugRepl() {
while (p.status == PARSER_PENDING) { while (p.status == PARSER_PENDING) {
/* Get some input if we are done */ /* Get some input if we are done */
if (*reader == '\0') { if (*reader == '\0') {
printf("> "); printf(">> ");
if (!fgets(buffer, sizeof(buffer), stdin)) { if (!fgets(buffer, sizeof(buffer), stdin)) {
return; return;
} }
@ -86,9 +91,9 @@ void debugRepl() {
} }
/* Print asm */ /* Print asm */
printf("\n"); //printf("\n");
dasmFunc(stdout, func.data.func); //dasmFunc(stdout, func.data.func);
printf("\n"); //printf("\n");
/* Execute function */ /* Execute function */
VMLoad(&vm, func); VMLoad(&vm, func);
@ -98,7 +103,8 @@ void debugRepl() {
buffer[0] = 0; buffer[0] = 0;
continue; continue;
} else { } else {
ValuePrint(vm.ret, 0); uint8_t * string = ValueToString(&vm, vm.ret);
StringPut(string);
printf("\n"); printf("\n");
} }
} }

86
value.c
View File

@ -5,79 +5,6 @@
#include "ds.h" #include "ds.h"
#include "vm.h" #include "vm.h"
/* Print the bytecode for a FuncDef */
static void FuncDefBytecodePrint(FuncDef * def) {
uint32_t count, i;
count = def->byteCodeLen;
printf("(bytecode)[");
if (count) {
for (i = 0; i < count - 1; ++i) {
printf("%04x ", def->byteCode[i]);
}
printf("%04x", def->byteCode[i]);
}
printf("]");
}
/* Print a value recursively. Used for debugging */
void ValuePrint(Value x, uint32_t indent) {
uint32_t i;
for (i = 0; i < indent; ++i)
fputc(' ', stdout);
switch (x.type) {
case TYPE_NIL:
printf("<nil>");
break;
case TYPE_BOOLEAN:
printf(x.data.boolean ? "<true>" : "<false>");
break;
case TYPE_NUMBER:
printf("%f", x.data.number);
break;
case TYPE_FORM:
case TYPE_ARRAY:
if (x.type == TYPE_ARRAY) printf(" [\n"); else printf(" (\n");
for (i = 0; i < x.data.array->count; ++i) {
ValuePrint(x.data.array->data[i], indent + 4);
printf("\n");
}
for (i = 0; i < indent; ++i) fputc(' ', stdout);
if (x.type == TYPE_ARRAY) printf(" ]\n"); else printf(" )\n");
break;
case TYPE_STRING:
printf("\"%.*s\"", VStringSize(x.data.string), (char *) x.data.string);
break;
case TYPE_SYMBOL:
printf("%.*s", VStringSize(x.data.string), (char *) x.data.string);
break;
case TYPE_CFUNCTION:
printf("<cfunction>");
break;
case TYPE_FUNCTION:
printf("<function ");
FuncDefBytecodePrint(x.data.func->def);
printf(">");
break;
case TYPE_DICTIONARY:
printf("<dictionary>");
break;
case TYPE_BYTEBUFFER:
printf("<bytebuffer>");
break;
case TYPE_FUNCDEF:
printf("<funcdef ");
FuncDefBytecodePrint(x.data.funcdef);
printf(">");
break;
case TYPE_FUNCENV:
printf("<funcenv>");
break;
case TYPE_THREAD:
printf("<thread>");
break;
}
}
static uint8_t * LoadCString(VM * vm, const char * string, uint32_t len) { static uint8_t * LoadCString(VM * vm, const char * string, uint32_t len) {
uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t)); uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t));
data += 2 * sizeof(uint32_t); data += 2 * sizeof(uint32_t);
@ -104,12 +31,12 @@ static uint8_t * NumberToString(VM * vm, Number x) {
return data; return data;
} }
static const char * HEX_CHARACTERS = "0123456789ABCDEF"; static const char * HEX_CHARACTERS = "0123456789abcdef";
#define HEX(i) (((uint8_t *) HEX_CHARACTERS)[(i)]) #define HEX(i) (((uint8_t *) HEX_CHARACTERS)[(i)])
/* Returns a string description for a pointer */ /* Returns a string description for a pointer */
static uint8_t * StringDescription(VM * vm, const char * title, uint32_t titlelen, void * pointer) { static uint8_t * StringDescription(VM * vm, const char * title, uint32_t titlelen, void * pointer) {
uint32_t len = 3 + titlelen + sizeof(pointer) * 2; uint32_t len = 5 + titlelen + sizeof(void *) * 2;
uint32_t i; uint32_t i;
uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t)); uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t));
uint8_t * c; uint8_t * c;
@ -125,12 +52,17 @@ static uint8_t * StringDescription(VM * vm, const char * title, uint32_t titlele
*c++ = ((uint8_t *)title) [i]; *c++ = ((uint8_t *)title) [i];
} }
*c++ = ' '; *c++ = ' ';
for (i = 0; i < sizeof(void *); ++i) { *c++ = '0';
uint8_t byte = buf.bytes[i]; *c++ = 'x';
for (i = sizeof(void *); i > 0; --i) {
uint8_t byte = buf.bytes[i - 1];
if (!byte) continue;
*c++ = HEX(byte >> 4); *c++ = HEX(byte >> 4);
*c++ = HEX(byte & 0xF); *c++ = HEX(byte & 0xF);
} }
*c++ = '>'; *c++ = '>';
VStringHash(data) = 0;
VStringSize(data) = len;
return data; return data;
} }

View File

@ -3,8 +3,6 @@
#include "datatypes.h" #include "datatypes.h"
void ValuePrint(Value x, uint32_t indent);
int ValueCompare(Value x, Value y); int ValueCompare(Value x, Value y);
int ValueEqual(Value x, Value y); int ValueEqual(Value x, Value y);

9
vm.c
View File

@ -467,10 +467,10 @@ int VMStart(VM * vm) {
} }
for (;;) { for (;;) {
Value temp, v1, v2;
uint16_t opcode = *vm->pc; uint16_t opcode = *vm->pc;
switch (opcode) { switch (opcode) {
Value temp, v1, v2;
#define DO_BINARY_MATH(op) \ #define DO_BINARY_MATH(op) \
v1 = vm->base[vm->pc[2]]; \ v1 = vm->base[vm->pc[2]]; \
@ -720,6 +720,9 @@ int VMStart(VM * vm) {
VMError(vm, "Unknown opcode"); VMError(vm, "Unknown opcode");
break; break;
} }
/* Move collection only to places that allocate memory */
/* This, however, is good for testing */
VMMaybeCollect(vm); VMMaybeCollect(vm);
} }
} }
@ -753,6 +756,10 @@ void VMInit(VM * vm) {
/* Garbage collection */ /* Garbage collection */
vm->blocks = NULL; vm->blocks = NULL;
vm->nextCollection = 0; vm->nextCollection = 0;
/* Setting memoryInterval to zero currently forces
* a collection pretty much every cycle, which is
* obviously horrible for performance. It helps ensure
* there are no memory bugs during dev */
vm->memoryInterval = 0; vm->memoryInterval = 0;
vm->black = 0; vm->black = 0;
vm->lock = 0; vm->lock = 0;