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:
parent
37faac1f8a
commit
42ecaf301a
26
main.c
26
main.c
@ -7,16 +7,21 @@
|
||||
#include "value.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 */
|
||||
Value print(VM * vm) {
|
||||
uint32_t i, j, count;
|
||||
uint32_t j, count;
|
||||
Value nil;
|
||||
count = VMCountArgs(vm);
|
||||
for (j = 0; j < count; ++j) {
|
||||
uint8_t * string = ValueToString(vm, VMGetArg(vm, j));
|
||||
uint32_t len = VStringSize(string);
|
||||
for (i = 0; i < len; ++i)
|
||||
fputc(string[i], stdout);
|
||||
StringPut(string);
|
||||
fputc('\n', stdout);
|
||||
}
|
||||
nil.type = TYPE_NIL;
|
||||
@ -25,7 +30,7 @@ Value print(VM * vm) {
|
||||
|
||||
/* A simple repl for debugging */
|
||||
void debugRepl() {
|
||||
char buffer[128] = {0};
|
||||
char buffer[1024] = {0};
|
||||
const char * reader = buffer;
|
||||
Value func;
|
||||
VM vm;
|
||||
@ -46,7 +51,7 @@ void debugRepl() {
|
||||
while (p.status == PARSER_PENDING) {
|
||||
/* Get some input if we are done */
|
||||
if (*reader == '\0') {
|
||||
printf("> ");
|
||||
printf(">> ");
|
||||
if (!fgets(buffer, sizeof(buffer), stdin)) {
|
||||
return;
|
||||
}
|
||||
@ -86,9 +91,9 @@ void debugRepl() {
|
||||
}
|
||||
|
||||
/* Print asm */
|
||||
printf("\n");
|
||||
dasmFunc(stdout, func.data.func);
|
||||
printf("\n");
|
||||
//printf("\n");
|
||||
//dasmFunc(stdout, func.data.func);
|
||||
//printf("\n");
|
||||
|
||||
/* Execute function */
|
||||
VMLoad(&vm, func);
|
||||
@ -98,7 +103,8 @@ void debugRepl() {
|
||||
buffer[0] = 0;
|
||||
continue;
|
||||
} else {
|
||||
ValuePrint(vm.ret, 0);
|
||||
uint8_t * string = ValueToString(&vm, vm.ret);
|
||||
StringPut(string);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
86
value.c
86
value.c
@ -5,79 +5,6 @@
|
||||
#include "ds.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) {
|
||||
uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t));
|
||||
data += 2 * sizeof(uint32_t);
|
||||
@ -104,12 +31,12 @@ static uint8_t * NumberToString(VM * vm, Number x) {
|
||||
return data;
|
||||
}
|
||||
|
||||
static const char * HEX_CHARACTERS = "0123456789ABCDEF";
|
||||
static const char * HEX_CHARACTERS = "0123456789abcdef";
|
||||
#define HEX(i) (((uint8_t *) HEX_CHARACTERS)[(i)])
|
||||
|
||||
/* Returns a string description for a 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;
|
||||
uint8_t * data = VMAlloc(vm, len + 2 * sizeof(uint32_t));
|
||||
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++ = ' ';
|
||||
for (i = 0; i < sizeof(void *); ++i) {
|
||||
uint8_t byte = buf.bytes[i];
|
||||
*c++ = '0';
|
||||
*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 & 0xF);
|
||||
}
|
||||
*c++ = '>';
|
||||
VStringHash(data) = 0;
|
||||
VStringSize(data) = len;
|
||||
return data;
|
||||
}
|
||||
|
||||
|
2
value.h
2
value.h
@ -3,8 +3,6 @@
|
||||
|
||||
#include "datatypes.h"
|
||||
|
||||
void ValuePrint(Value x, uint32_t indent);
|
||||
|
||||
int ValueCompare(Value x, Value y);
|
||||
|
||||
int ValueEqual(Value x, Value y);
|
||||
|
9
vm.c
9
vm.c
@ -467,10 +467,10 @@ int VMStart(VM * vm) {
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
Value temp, v1, v2;
|
||||
uint16_t opcode = *vm->pc;
|
||||
|
||||
switch (opcode) {
|
||||
Value temp, v1, v2;
|
||||
|
||||
#define DO_BINARY_MATH(op) \
|
||||
v1 = vm->base[vm->pc[2]]; \
|
||||
@ -720,6 +720,9 @@ int VMStart(VM * vm) {
|
||||
VMError(vm, "Unknown opcode");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Move collection only to places that allocate memory */
|
||||
/* This, however, is good for testing */
|
||||
VMMaybeCollect(vm);
|
||||
}
|
||||
}
|
||||
@ -753,6 +756,10 @@ void VMInit(VM * vm) {
|
||||
/* Garbage collection */
|
||||
vm->blocks = NULL;
|
||||
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->black = 0;
|
||||
vm->lock = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user