General code cleanup.
This commit is contained in:
parent
9211b04fc1
commit
6164c3ebf4
@ -310,8 +310,8 @@ static void emitReturn() {
|
||||
static KrkFunction * endCompiler() {
|
||||
emitReturn();
|
||||
KrkFunction * function = current->function;
|
||||
#ifdef ENABLE_DEBUGGING
|
||||
if ((vm.flags & KRK_ENABLE_DEBUGGING) && !parser.hadError) {
|
||||
#ifdef ENABLE_DISASSEMBLY
|
||||
if ((vm.flags & KRK_ENABLE_DISASSEMBLY) && !parser.hadError) {
|
||||
krk_disassembleChunk(currentChunk(), function->name != NULL ? function->name->chars : "<module>");
|
||||
}
|
||||
#endif
|
||||
@ -613,8 +613,8 @@ static void block(size_t indentation, const char * blockName) {
|
||||
advance();
|
||||
declaration();
|
||||
};
|
||||
#ifdef ENABLE_DEBUGGING
|
||||
if (vm.flags & KRK_ENABLE_DEBUGGING) {
|
||||
#ifdef ENABLE_SCAN_TRACING
|
||||
if (vm.flags & KRK_ENABLE_SCAN_TRACING) {
|
||||
fprintf(stderr, "finished with block %s (ind=%d) on line %d, sitting on a %s (len=%d)\n",
|
||||
blockName, (int)indentation, (int)parser.current.line,
|
||||
getRule(parser.current.type)->name, (int)parser.current.length);
|
||||
|
5
debug.c
5
debug.c
@ -111,9 +111,8 @@ size_t krk_disassembleInstruction(KrkChunk * chunk, size_t offset) {
|
||||
JUMP(OP_JUMP_IF_TRUE,+)
|
||||
JUMP(OP_LOOP,-)
|
||||
JUMP(OP_PUSH_TRY,+)
|
||||
default:
|
||||
fprintf(stderr, "Unknown opcode: %02x\n", opcode);
|
||||
return offset + 1;
|
||||
}
|
||||
fprintf(stderr, "Unknown opcode: %02x\n", opcode);
|
||||
return offset + 1;
|
||||
}
|
||||
|
||||
|
61
kuroko.c
61
kuroko.c
@ -1,16 +1,28 @@
|
||||
/**
|
||||
* Kuroko interpreter main executable.
|
||||
*
|
||||
* Reads lines from stdin with the `rline` library and executes them,
|
||||
* or executes scripts from the argument list.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __toaru__
|
||||
#include <toaru/rline.h>
|
||||
#include <kuroko.h>
|
||||
#else
|
||||
#include "rline.h"
|
||||
#include "kuroko.h"
|
||||
#endif
|
||||
|
||||
#include "chunk.h"
|
||||
#include "debug.h"
|
||||
#include "vm.h"
|
||||
#include "memory.h"
|
||||
|
||||
#include "rline.h"
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
int flags = 0;
|
||||
@ -18,15 +30,19 @@ int main(int argc, char * argv[]) {
|
||||
while ((opt = getopt(argc, argv, "tdgs")) != -1) {
|
||||
switch (opt) {
|
||||
case 't':
|
||||
/* Disassemble instructions as they are executed. */
|
||||
flags |= KRK_ENABLE_TRACING;
|
||||
break;
|
||||
case 'd':
|
||||
flags |= KRK_ENABLE_DEBUGGING;
|
||||
/* Disassemble code blocks after compilation. */
|
||||
flags |= KRK_ENABLE_DISASSEMBLY;
|
||||
break;
|
||||
case 's':
|
||||
/* Print debug information during compilation. */
|
||||
flags |= KRK_ENABLE_SCAN_TRACING;
|
||||
break;
|
||||
case 'g':
|
||||
/* Always garbage collect during an allocation. */
|
||||
flags |= KRK_ENABLE_STRESS_GC;
|
||||
break;
|
||||
}
|
||||
@ -40,9 +56,11 @@ int main(int argc, char * argv[]) {
|
||||
/* Run the repl */
|
||||
int exit = 0;
|
||||
|
||||
/* Set ^D to send EOF */
|
||||
rline_exit_string="";
|
||||
/* Enable syntax highlight for Kuroko */
|
||||
rline_exp_set_syntax("krk");
|
||||
//rline_exp_set_shell_commands(shell_commands, shell_commands_len);
|
||||
/* TODO: Add tab completion for globals, known fields/methods... */
|
||||
//rline_exp_set_tab_complete_func(tab_complete_func);
|
||||
|
||||
while (!exit) {
|
||||
@ -55,6 +73,7 @@ int main(int argc, char * argv[]) {
|
||||
int inBlock = 0;
|
||||
int blockWidth = 0;
|
||||
|
||||
/* Main prompt is >>> like in Python */
|
||||
rline_exp_set_prompts(">>> ", "", 4, 0);
|
||||
|
||||
while (1) {
|
||||
@ -62,7 +81,10 @@ int main(int argc, char * argv[]) {
|
||||
char buf[4096] = {0};
|
||||
|
||||
if (inBlock) {
|
||||
/* When entering multiple lines, the additional lines
|
||||
* will show a single > (and keep the left side aligned) */
|
||||
rline_exp_set_prompts(" > ", "", 4, 0);
|
||||
/* Also add indentation as necessary */
|
||||
rline_preload = malloc(blockWidth + 1);
|
||||
for (int i = 0; i < blockWidth; ++i) {
|
||||
rline_preload[i] = ' ';
|
||||
@ -77,11 +99,15 @@ int main(int argc, char * argv[]) {
|
||||
break;
|
||||
}
|
||||
if (buf[strlen(buf)-1] != '\n') {
|
||||
/* rline shouldn't allow this as it doesn't accept ^D to submit input
|
||||
* unless the line is empty, but just in case... */
|
||||
fprintf(stderr, "Expected end of line in repl input. Did you ^D early?\n");
|
||||
valid = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (lineCapacity < lineCount + 1) {
|
||||
/* If we need more space, grow as needed... */
|
||||
size_t old = lineCapacity;
|
||||
lineCapacity = GROW_CAPACITY(old);
|
||||
lines = GROW_ARRAY(char *,lines,old,lineCapacity);
|
||||
@ -93,31 +119,41 @@ int main(int argc, char * argv[]) {
|
||||
size_t lineLength = strlen(lines[i]);
|
||||
totalData += lineLength;
|
||||
|
||||
int is_spaces = 1;
|
||||
int count_spaces = 0;
|
||||
/* Figure out indentation */
|
||||
int isSpaces = 1;
|
||||
int countSpaces = 0;
|
||||
for (size_t j = 0; j < lineLength; ++j) {
|
||||
if (lines[i][j] != ' ' && lines[i][j] != '\n') {
|
||||
is_spaces = 0;
|
||||
isSpaces = 0;
|
||||
break;
|
||||
}
|
||||
count_spaces += 1;
|
||||
countSpaces += 1;
|
||||
}
|
||||
|
||||
/* Naively detect the start of a new block so we can
|
||||
* continue to accept input. Our compiler isn't really
|
||||
* set up to let us compile "on the fly" so we can't just
|
||||
* run lines through it and see if it wants more... */
|
||||
if (lineLength > 2 && lines[i][lineLength-2] == ':') {
|
||||
inBlock = 1;
|
||||
blockWidth = count_spaces + 4;
|
||||
blockWidth = countSpaces + 4;
|
||||
continue;
|
||||
} else if (inBlock && lineLength != 1) {
|
||||
if (is_spaces) {
|
||||
if (isSpaces) {
|
||||
free(lines[i]);
|
||||
totalData -= lineLength;
|
||||
lineCount--;
|
||||
break;
|
||||
}
|
||||
blockWidth = count_spaces;
|
||||
blockWidth = countSpaces;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Ignore blank lines. */
|
||||
if (isSpaces) valid = 0;
|
||||
|
||||
/* If we're not in a block, or have entered a blank line,
|
||||
* we can stop reading new lines and jump to execution. */
|
||||
break;
|
||||
}
|
||||
|
||||
@ -125,6 +161,7 @@ int main(int argc, char * argv[]) {
|
||||
allData = malloc(totalData + 1);
|
||||
allData[0] = '\0';
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < lineCount; ++i) {
|
||||
if (valid) strcat(allData, lines[i]);
|
||||
rline_history_insert(strdup(lines[i]));
|
||||
@ -143,7 +180,9 @@ int main(int argc, char * argv[]) {
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
/* Expect the rest of the arguments to be scripts to run;
|
||||
* collect the result of the last one and use it as the
|
||||
* exit code if it's an integer. */
|
||||
for (int i = optind; i < argc; ++i) {
|
||||
KrkValue out = krk_runfile(argv[i],0,"<module>",argv[i]);
|
||||
if (i + 1 == argc) result = out;
|
||||
|
2
kuroko.h
2
kuroko.h
@ -5,7 +5,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef DEBUG
|
||||
#define ENABLE_DEBUGGING
|
||||
#define ENABLE_DISASSEBMLY
|
||||
#define ENABLE_TRACING
|
||||
#define ENABLE_SCAN_TRACING
|
||||
#define ENABLE_STRESS_GC
|
||||
|
2
table.c
2
table.c
@ -26,7 +26,7 @@ static uint32_t hashValue(KrkValue value) {
|
||||
if (IS_FLOATING(value)) return (uint32_t)(AS_FLOATING(value) * 1000); /* arbitrary; what's a good way to hash floats? */
|
||||
if (IS_BOOLEAN(value)) return (uint32_t)(AS_BOOLEAN(value));
|
||||
if (IS_NONE(value)) return 0;
|
||||
return (uint32_t)(intptr_t)AS_OBJECT(value);
|
||||
return (((uint32_t)(intptr_t)AS_OBJECT(value)) >> 4)| (((uint32_t)(intptr_t)AS_OBJECT(value)) << 28);
|
||||
}
|
||||
|
||||
static KrkTableEntry * findEntry(KrkTableEntry * entries, size_t capacity, KrkValue key) {
|
||||
|
Loading…
Reference in New Issue
Block a user