Cleanups clang asked for

This commit is contained in:
K. Lange 2021-01-14 09:15:18 +09:00
parent 2b02ef457e
commit 9015176bbb
5 changed files with 23 additions and 10 deletions

View File

@ -196,9 +196,13 @@ static void errorAt(KrkToken * token, const char * message) {
size_t i = (token->col - 1);
while (token->linePtr[i] && token->linePtr[i] != '\n') i++;
fprintf(stderr, "Parse error in \"%s\" on line %d col %d (%s): %s\n"
" %.*s\033[31m%.*s\033[39m%.*s\n"
" %-*s\033[31m^\033[39m\n",
const char fancyError[] = "Parse error in \"%s\" on line %d col %d (%s): %s\n"
" %.*s\033[31m%.*s\033[39m%.*s\n"
" %-*s\033[31m^\033[39m\n";
const char plainError[] = "Parse error in \"%s\" on line %d col %d (%s): %s\n"
" %.*s%.*s%.*s\n"
" %-*s^\n";
fprintf(stderr, (vm.flags & KRK_NO_ESCAPE) ? plainError: fancyError,
currentChunk()->filename->chars,
(int)token->line,
(int)token->col,
@ -2091,7 +2095,6 @@ static void declareVariable() {
if (local->depth != -1 && local->depth < (ssize_t)current->scopeDepth) break;
if (identifiersEqual(name, &local->name)) {
error("Duplicate definition");
__asm__("int $3");
}
}
addLocal(*name);

View File

@ -179,7 +179,7 @@ KrkString * krk_copyString(const char * chars, size_t length) {
return allocateString(heapChars, length, hash);
}
KrkFunction * krk_newFunction() {
KrkFunction * krk_newFunction(void) {
KrkFunction * function = ALLOCATE_OBJECT(KrkFunction, OBJ_FUNCTION);
function->requiredArgs = 0;
function->keywordArgs = 0;

View File

@ -177,7 +177,7 @@ static inline int isObjType(KrkValue value, ObjType type) {
extern KrkString * krk_takeString(char * chars, size_t length);
extern KrkString * krk_copyString(const char * chars, size_t length);
extern KrkFunction * krk_newFunction();
extern KrkFunction * krk_newFunction(void);
extern KrkNative * krk_newNative(NativeFn function, const char * name, int type);
extern KrkClosure * krk_newClosure(KrkFunction * function);
extern KrkUpvalue * krk_newUpvalue(int slot);

17
vm.c
View File

@ -236,7 +236,6 @@ KrkValue krk_pop() {
vm.stackTop--;
if (vm.stackTop < vm.stack) {
fprintf(stderr, "Fatal error: stack underflow detected in VM, issuing breakpoint.\n");
__asm__ ("int $3");
return NONE_VAL();
}
return *vm.stackTop;
@ -427,7 +426,7 @@ static KrkValue _list_init(int argc, KrkValue argv[]) {
krk_runtimeError(vm.exceptions.argumentError, "Can not initialize list from iterable (unsupported, try again later)");
return NONE_VAL();
}
KrkFunction * list = krk_newFunction(NULL);
KrkFunction * list = krk_newFunction();
krk_push(OBJECT_VAL(list));
krk_tableSet(&AS_INSTANCE(argv[0])->fields, vm.specialMethodNames[METHOD_LIST_INT], OBJECT_VAL(list));
AS_INSTANCE(argv[0])->_internal = (KrkObj*)list;
@ -557,7 +556,7 @@ KrkValue krk_list_of(int argc, KrkValue argv[]) {
krk_tableGet(&vm.builtins->fields,OBJECT_VAL(S("list")), &Class);
KrkInstance * outList = krk_newInstance(AS_CLASS(Class));
krk_push(OBJECT_VAL(outList));
KrkFunction * listContents = krk_newFunction(NULL);
KrkFunction * listContents = krk_newFunction();
krk_push(OBJECT_VAL(listContents));
krk_tableSet(&outList->fields, vm.specialMethodNames[METHOD_LIST_INT], OBJECT_VAL(listContents));
outList->_internal = (KrkObj*)listContents;
@ -1853,7 +1852,7 @@ static KrkValue _string_join(int argc, KrkValue argv[], int hasKw) {
}
/* TODO: Support any object with an __iter__ - kinda need an internal method to do that well. */
if (!IS_INSTANCE(argv[1]) || !AS_INSTANCE(argv[1])->_internal || !((KrkObj*)AS_INSTANCE(argv[1])->_internal)->type == OBJ_FUNCTION) {
if (!IS_INSTANCE(argv[1]) || !AS_INSTANCE(argv[1])->_internal || !(((KrkObj*)AS_INSTANCE(argv[1])->_internal)->type == OBJ_FUNCTION)) {
krk_runtimeError(vm.exceptions.typeError, "str.join(): expected a list");
return NONE_VAL();
}
@ -3014,6 +3013,15 @@ static KrkValue krk_getsize(int argc, KrkValue argv[]) {
return INTEGER_VAL(mySize);
}
static KrkValue krk_setclean(int argc, KrkValue argv[]) {
if (!argc || (IS_BOOLEAN(argv[0]) && AS_BOOLEAN(argv[0]))) {
vm.flags |= KRK_NO_ESCAPE;
} else {
vm.flags &= ~KRK_NO_ESCAPE;
}
return NONE_VAL();
}
void krk_initVM(int flags) {
vm.flags = flags;
KRK_PAUSE_GC();
@ -3101,6 +3109,7 @@ void krk_initVM(int flags) {
krk_attachNamedObject(&vm.system->fields, "buildenv", (KrkObj*)S(KRK_BUILD_COMPILER));
krk_attachNamedObject(&vm.system->fields, "builddate", (KrkObj*)S(KRK_BUILD_DATE));
krk_defineNative(&vm.system->fields, "getsizeof", krk_getsize);
krk_defineNative(&vm.system->fields, "set_clean_output", krk_setclean);
KrkInstance * gcModule = krk_newInstance(vm.moduleClass);
krk_attachNamedObject(&vm.modules, "gc", (KrkObj*)gcModule);

1
vm.h
View File

@ -114,6 +114,7 @@ typedef struct {
#define KRK_ENABLE_DISASSEMBLY (1 << 1)
#define KRK_ENABLE_SCAN_TRACING (1 << 2)
#define KRK_ENABLE_STRESS_GC (1 << 3)
#define KRK_NO_ESCAPE (1 << 4)
#define KRK_GC_PAUSED (1 << 10)
#define KRK_HAS_EXCEPTION (1 << 11)