2020-12-26 03:32:21 +03:00
|
|
|
#include <stdio.h>
|
2020-12-28 04:54:25 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2020-12-28 03:08:35 +03:00
|
|
|
#include <unistd.h>
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
#include "kuroko.h"
|
|
|
|
#include "chunk.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "vm.h"
|
2020-12-28 04:54:25 +03:00
|
|
|
#include "memory.h"
|
2020-12-26 03:32:21 +03:00
|
|
|
|
2020-12-28 05:37:38 +03:00
|
|
|
#include "rline.h"
|
|
|
|
|
2020-12-26 03:32:21 +03:00
|
|
|
int main(int argc, char * argv[]) {
|
2020-12-28 14:38:26 +03:00
|
|
|
int flags = 0;
|
2020-12-28 03:08:35 +03:00
|
|
|
int opt;
|
2020-12-28 07:51:14 +03:00
|
|
|
while ((opt = getopt(argc, argv, "tdgs")) != -1) {
|
2020-12-28 03:08:35 +03:00
|
|
|
switch (opt) {
|
|
|
|
case 't':
|
2020-12-28 14:38:26 +03:00
|
|
|
flags |= KRK_ENABLE_TRACING;
|
2020-12-28 03:08:35 +03:00
|
|
|
break;
|
|
|
|
case 'd':
|
2020-12-28 14:38:26 +03:00
|
|
|
flags |= KRK_ENABLE_DEBUGGING;
|
2020-12-28 03:08:35 +03:00
|
|
|
break;
|
2020-12-28 04:54:25 +03:00
|
|
|
case 's':
|
2020-12-28 14:38:26 +03:00
|
|
|
flags |= KRK_ENABLE_SCAN_TRACING;
|
2020-12-28 04:54:25 +03:00
|
|
|
break;
|
2020-12-28 07:51:14 +03:00
|
|
|
case 'g':
|
2020-12-28 14:38:26 +03:00
|
|
|
flags |= KRK_ENABLE_STRESS_GC;
|
2020-12-28 07:51:14 +03:00
|
|
|
break;
|
2020-12-28 03:08:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 14:38:26 +03:00
|
|
|
krk_initVM(flags);
|
|
|
|
|
2020-12-28 04:54:25 +03:00
|
|
|
KrkValue result = INTEGER_VAL(0);
|
|
|
|
|
2020-12-28 03:08:35 +03:00
|
|
|
if (optind == argc) {
|
2020-12-28 04:54:25 +03:00
|
|
|
/* Run the repl */
|
|
|
|
int exit = 0;
|
|
|
|
|
2020-12-28 05:37:38 +03:00
|
|
|
rline_exit_string="";
|
2020-12-28 15:31:46 +03:00
|
|
|
rline_exp_set_syntax("krk");
|
2020-12-28 05:37:38 +03:00
|
|
|
//rline_exp_set_shell_commands(shell_commands, shell_commands_len);
|
|
|
|
//rline_exp_set_tab_complete_func(tab_complete_func);
|
|
|
|
|
2020-12-28 04:54:25 +03:00
|
|
|
while (!exit) {
|
|
|
|
size_t lineCapacity = 8;
|
|
|
|
size_t lineCount = 0;
|
|
|
|
char ** lines = ALLOCATE(char *, lineCapacity);
|
|
|
|
size_t totalData = 0;
|
|
|
|
int valid = 1;
|
|
|
|
char * allData = NULL;
|
|
|
|
int inBlock = 0;
|
2020-12-28 05:37:38 +03:00
|
|
|
int blockWidth = 0;
|
2020-12-28 04:54:25 +03:00
|
|
|
|
2020-12-28 05:37:38 +03:00
|
|
|
rline_exp_set_prompts(">>> ", "", 4, 0);
|
2020-12-28 04:54:25 +03:00
|
|
|
|
|
|
|
while (1) {
|
2020-12-28 05:37:38 +03:00
|
|
|
/* This would be a nice place for line editing */
|
|
|
|
char buf[4096] = {0};
|
|
|
|
|
2020-12-28 04:54:25 +03:00
|
|
|
if (inBlock) {
|
2020-12-28 05:37:38 +03:00
|
|
|
rline_exp_set_prompts(" > ", "", 4, 0);
|
|
|
|
rline_preload = malloc(blockWidth + 1);
|
|
|
|
for (int i = 0; i < blockWidth; ++i) {
|
|
|
|
rline_preload[i] = ' ';
|
|
|
|
}
|
|
|
|
rline_preload[blockWidth] = '\0';
|
2020-12-28 04:54:25 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 08:00:12 +03:00
|
|
|
rline_scroll = 0;
|
2020-12-28 05:37:38 +03:00
|
|
|
if (rline(buf, 4096) == 0) {
|
|
|
|
valid = 0;
|
2020-12-28 04:54:25 +03:00
|
|
|
exit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (buf[strlen(buf)-1] != '\n') {
|
|
|
|
fprintf(stderr, "Expected end of line in repl input. Did you ^D early?\n");
|
|
|
|
valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lineCapacity < lineCount + 1) {
|
|
|
|
size_t old = lineCapacity;
|
|
|
|
lineCapacity = GROW_CAPACITY(old);
|
|
|
|
lines = GROW_ARRAY(char *,lines,old,lineCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = lineCount++;
|
|
|
|
lines[i] = strdup(buf);
|
2020-12-28 03:08:35 +03:00
|
|
|
|
2020-12-28 04:54:25 +03:00
|
|
|
size_t lineLength = strlen(lines[i]);
|
|
|
|
totalData += lineLength;
|
2020-12-28 03:08:35 +03:00
|
|
|
|
2020-12-28 05:37:38 +03:00
|
|
|
int is_spaces = 1;
|
|
|
|
int count_spaces = 0;
|
2020-12-28 05:11:50 +03:00
|
|
|
for (size_t j = 0; j < lineLength; ++j) {
|
2020-12-28 05:37:38 +03:00
|
|
|
if (lines[i][j] != ' ' && lines[i][j] != '\n') {
|
|
|
|
is_spaces = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count_spaces += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineLength > 2 && lines[i][lineLength-2] == ':') {
|
2020-12-28 04:54:25 +03:00
|
|
|
inBlock = 1;
|
2020-12-28 05:37:38 +03:00
|
|
|
blockWidth = count_spaces + 4;
|
|
|
|
continue;
|
|
|
|
} else if (inBlock && lineLength != 1) {
|
|
|
|
if (is_spaces) {
|
|
|
|
free(lines[i]);
|
|
|
|
totalData -= lineLength;
|
|
|
|
lineCount--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
blockWidth = count_spaces;
|
2020-12-28 04:54:25 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid) {
|
2020-12-28 07:51:14 +03:00
|
|
|
allData = malloc(totalData + 1);
|
2020-12-28 04:54:25 +03:00
|
|
|
allData[0] = '\0';
|
|
|
|
}
|
2020-12-28 05:11:50 +03:00
|
|
|
for (size_t i = 0; i < lineCount; ++i) {
|
2020-12-28 04:54:25 +03:00
|
|
|
if (valid) strcat(allData, lines[i]);
|
2020-12-28 06:16:44 +03:00
|
|
|
rline_history_insert(strdup(lines[i]));
|
2020-12-28 04:54:25 +03:00
|
|
|
free(lines[i]);
|
|
|
|
}
|
|
|
|
FREE_ARRAY(char *, lines, lineCapacity);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
KrkValue result = krk_interpret(allData, 0, "<module>","<stdin>");
|
|
|
|
if (!IS_NONE(result)) {
|
|
|
|
fprintf(stdout, " \033[1;30m=> ");
|
|
|
|
krk_printValue(stdout, result);
|
|
|
|
fprintf(stdout, "\033[0m\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for (int i = optind; i < argc; ++i) {
|
|
|
|
KrkValue out = krk_runfile(argv[i],0,"<module>",argv[i]);
|
|
|
|
if (i + 1 == argc) result = out;
|
|
|
|
}
|
2020-12-28 03:08:35 +03:00
|
|
|
}
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
krk_freeVM();
|
2020-12-26 12:39:29 +03:00
|
|
|
|
2020-12-27 12:55:52 +03:00
|
|
|
if (IS_INTEGER(result)) return AS_INTEGER(result);
|
|
|
|
|
2020-12-26 03:32:21 +03:00
|
|
|
return 0;
|
|
|
|
}
|