Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawSumma committed May 16, 2024
1 parent 4560870 commit 02893f3
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 82 deletions.
27 changes: 23 additions & 4 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../vm/ir/ir.h"
#include "../vm/std/io.h"
#include "../vm/std/std.h"
#include "../vm/save/value.h"

vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str);
void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks);
Expand All @@ -22,11 +23,7 @@ int main(int argc, char **argv) {
.target = VM_TARGET_TB_EMCC,
.tb_tailcalls = true,
#else
#if defined(_WIN32)
.target = VM_TARGET_TB_TCC,
#else
.target = VM_TARGET_TB_CC,
#endif
.tb_tailcalls = true,
#endif
.cflags = NULL,
Expand Down Expand Up @@ -60,6 +57,28 @@ int main(int argc, char **argv) {
} else if (!strcmp(arg, "--repl")) {
vm_lang_lua_repl(config, std, blocks);
isrepl = false;
} else if (!strncmp(arg, "--load=", 7)) {
arg += 7;
FILE *f = fopen(arg, "rb");
if (f != NULL) {
vm_save_t save = vm_save_load(f);
fclose(f);
vm_save_loaded_t ld = vm_load_value(config, save);
if (ld.blocks != NULL) {
blocks = ld.blocks;
std = ld.env.value.table;
vm_io_buffer_t *buf = vm_io_buffer_new();
vm_io_format_blocks(buf, blocks);
}
}
} else if (!strncmp(arg, "--save=", 7)) {
arg += 7;
vm_save_t save = vm_save_value(config, blocks, (vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std});
FILE *f = fopen(arg, "wb");
if (f != NULL) {
fwrite(save.buf, 1, save.len, f);
fclose(f);
}
} else if (!strncmp(arg, "--cflag=", 8)) {
arg += 8;
const char *last = config->cflags;
Expand Down
2 changes: 1 addition & 1 deletion vm/ast/comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct vm_ast_comp_names_t {
vm_ast_comp_names_t *next;
};

static void vm_lua_comp_op_std_pow(vm_std_closure_t *closure, vm_std_value_t *args) {
void vm_lua_comp_op_std_pow(vm_std_closure_t *closure, vm_std_value_t *args) {
vm_std_value_t *ret = args;
double v = vm_value_to_f64(*args++);
while (!vm_type_eq(args->tag, VM_TAG_UNK)) {
Expand Down
2 changes: 1 addition & 1 deletion vm/lua/parser/scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static inline void reset_state(Scanner *scanner) {
scanner->level_count = 0;
}

void *tree_sitter_lua_external_scanner_create() {
void *tree_sitter_lua_external_scanner_create(void) {
Scanner *scanner = calloc(1, sizeof(Scanner));
return scanner;
}
Expand Down
86 changes: 14 additions & 72 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "../backend/tb.h"
#include "../ir/ir.h"
#include "../std/io.h"
#include "../save/value.h"

#include "../../vendor/tree-sitter/lib/include/tree_sitter/api.h"

Expand Down Expand Up @@ -114,29 +113,7 @@ with_new_std:;
ret:;
}

const char *vm_lang_lua_repl_highlight_bracket_color(vm_table_t *repl, size_t depth) {
if (repl == NULL) {
return "";
}
vm_pair_t *value = VM_TABLE_LOOKUP_STR(repl, "parens");
if (value == NULL) {
return "";
}
if (vm_type_eq(value->val_tag, VM_TAG_TAB)) {
return "";
}
if (value->val_val.table->len == 0) {
return "";
}
vm_table_t *tab = value->val_val.table;
vm_pair_t *sub = vm_table_lookup(tab, (vm_value_t){.i32 = (int32_t)depth % (int32_t)tab->len + 1}, VM_TAG_I32);
if (sub == NULL || vm_type_eq(sub->val_tag, VM_TAG_STR)) {
return "";
}
return sub->val_val.str;
}

void vm_lang_lua_repl_highlight_walk(ic_highlight_env_t *henv, vm_table_t *repl, size_t *depth, TSNode node) {
void vm_lang_lua_repl_highlight_walk(ic_highlight_env_t *henv, size_t *depth, TSNode node) {
const char *type = ts_node_type(node);
size_t start = ts_node_start_byte(node);
size_t end = ts_node_end_byte(node);
Expand All @@ -150,14 +127,6 @@ void vm_lang_lua_repl_highlight_walk(ic_highlight_env_t *henv, vm_table_t *repl,
if (!strcmp("identifier", type)) {
ic_highlight(henv, start, len, "white");
}
if (!strcmp("(", type)) {
ic_highlight(henv, start, len, vm_lang_lua_repl_highlight_bracket_color(repl, *depth));
*depth += 1;
}
if (!strcmp(")", type)) {
*depth -= 1;
ic_highlight(henv, start, len, vm_lang_lua_repl_highlight_bracket_color(repl, *depth));
}
const char *keywords[] = {
"or",
"and",
Expand All @@ -182,7 +151,7 @@ void vm_lang_lua_repl_highlight_walk(ic_highlight_env_t *henv, vm_table_t *repl,
size_t num_children = ts_node_child_count(node);
for (size_t i = 0; i < num_children; i++) {
TSNode sub = ts_node_child(node, i);
vm_lang_lua_repl_highlight_walk(henv, repl, depth, sub);
vm_lang_lua_repl_highlight_walk(henv, depth, sub);
}
}

Expand All @@ -199,11 +168,7 @@ void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, voi
TSNode root_node = ts_tree_root_node(tree);
size_t depth = 0;
vm_pair_t *value = VM_TABLE_LOOKUP_STR(state->std, "config");
vm_table_t *repl = NULL;
if (value != NULL && vm_type_eq(value->val_tag, VM_TAG_TAB)) {
repl = value->val_val.table;
}
vm_lang_lua_repl_highlight_walk(henv, repl, &depth, root_node);
vm_lang_lua_repl_highlight_walk(henv, &depth, root_node);
ts_tree_delete(tree);
ts_parser_delete(parser);
// FILE *out = fopen("out.log", "w");
Expand All @@ -215,15 +180,15 @@ void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, voi
void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) {
config->is_repl = true;

vm_table_t *repl = vm_table_new();
vm_lang_lua_repl_table_set_config(repl, config);
VM_TABLE_SET(repl, str, "echo", b, true);
vm_table_t *parens = vm_table_new();
VM_TABLE_SET(parens, i32, 1, str, "yellow");
VM_TABLE_SET(parens, i32, 2, str, "magenta");
VM_TABLE_SET(parens, i32, 3, str, "blue");
VM_TABLE_SET(repl, str, "parens", table, parens);
VM_TABLE_SET(std, str, "config", table, repl);
// vm_table_t *repl = vm_table_new();
// vm_lang_lua_repl_table_set_config(repl, config);
// VM_TABLE_SET(repl, str, "echo", b, true);
// vm_table_t *parens = vm_table_new();
// VM_TABLE_SET(parens, i32, 1, str, "yellow");
// VM_TABLE_SET(parens, i32, 2, str, "magenta");
// VM_TABLE_SET(parens, i32, 3, str, "blue");
// VM_TABLE_SET(repl, str, "parens", table, parens);
// VM_TABLE_SET(std, str, "config", table, repl);

ic_set_history(".minivm-history", 2000);

Expand All @@ -236,22 +201,6 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
.std = std,
};

{
FILE *f = fopen("out.bin", "rb");
if (f != NULL) {
vm_save_t save = vm_save_load(f);
fclose(f);
vm_save_loaded_t ld = vm_load_value(config, save);
if (ld.blocks != NULL) {
*blocks = *ld.blocks;
*std = *ld.env.value.table;
vm_io_buffer_t *buf = vm_io_buffer_new();
vm_io_format_blocks(buf, blocks);
printf("%s", buf->buf);
}
}
}

// #if defined(EMSCRIPTEN)
// setvbuf(stdin, NULL, _IONBF, 0);
// setvbuf(stdout, NULL, _IONBF, 0);
Expand Down Expand Up @@ -309,11 +258,11 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)

vm_std_value_t value = vm_tb_run_repl(config, blocks->entry, blocks, std);

vm_lang_lua_repl_table_get_config(repl, config);
// vm_lang_lua_repl_table_get_config(repl, config);

if (vm_type_eq(value.tag, VM_TAG_ERROR)) {
fprintf(stderr, "error: %s\n", value.value.str);
} else if (vm_lang_lua_repl_table_get_bool(repl, "echo") && !vm_type_eq(value.tag, VM_TAG_NIL)) {
} else if (!vm_type_eq(value.tag, VM_TAG_NIL)) {
vm_io_buffer_t buf = {0};
vm_io_debug(&buf, 0, "", value, NULL);
printf("%.*s", (int)buf.len, buf.buf);
Expand All @@ -326,12 +275,5 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)

printf("took: %.3fms\n", diff);
}

vm_save_t save = vm_save_value(config, blocks, (vm_std_value_t) {.tag = VM_TAG_TAB, .value.table = std});
FILE *f = fopen("out.bin", "wb");
if (f != NULL) {
fwrite(save.buf, 1, save.len, f);
fclose(f);
}
}
}
6 changes: 4 additions & 2 deletions vm/save/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ vm_save_loaded_t vm_load_value(vm_config_t *config, vm_save_t arg) {
case VM_TAG_ERROR:
case VM_TAG_STR: {
uint64_t len = vm_save_read_uleb(&read);
char *buf = vm_malloc(sizeof(char) * len);
char *buf = vm_malloc(sizeof(char) * (len + 1));
for (size_t i = 0; i < len; i++) {
buf[i] = vm_save_read_byte(&read);
}
buf[len] = '\0';
value.str = buf;
break;
}
Expand Down Expand Up @@ -222,10 +223,11 @@ outer:;
uint64_t nsrcs = vm_save_read_uleb(&read);
for (uint64_t i = 0; i < nsrcs; i++) {
uint64_t len = vm_save_read_uleb(&read);
char *src = vm_malloc(sizeof(char) * len);
char *src = vm_malloc(sizeof(char) * (len + 1));
for (uint64_t j = 0; j < len; j++) {
src[j] = (char) vm_save_read_byte(&read);
}
src[len] = '\0';
vm_ast_node_t node = vm_lang_lua_parse(config, src);
vm_ast_comp_more(node, blocks);
// vm_ast_free_node(node);
Expand Down
4 changes: 2 additions & 2 deletions vm/save/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ vm_save_t vm_save_value(vm_config_t *config, vm_blocks_t *blocks, vm_std_value_t
case VM_TAG_ERROR:
case VM_TAG_STR: {
const char *buf = value.value.str;
size_t len = strlen(buf) + 1;
size_t len = strlen(buf);
vm_save_write_uleb(&write, (uint64_t) len);
for (size_t i = 0; i < len; i++) {
vm_save_write_byte(&write, (uint8_t) buf[i]);
Expand Down Expand Up @@ -204,7 +204,7 @@ outer:;
}
for (vm_blocks_srcs_t *cur = blocks->srcs; cur; cur = cur->last) {
const char *src = srcs[nsrcs++];
size_t len = strlen(src) + 1;
size_t len = strlen(src);
vm_save_write_uleb(&write, (uint64_t) len);
for (size_t i = 0; i < len; i++) {
vm_save_write_byte(&write, (uint8_t) src[i]);
Expand Down
4 changes: 4 additions & 0 deletions vm/std/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "./io.h"

void vm_io_buffer_vformat(vm_io_buffer_t *buf, const char *fmt, va_list ap) {
if (buf->buf == NULL) {
buf->alloc = 16;
buf->buf = vm_malloc(sizeof(char) * buf->alloc);
}
while (true) {
int avail = buf->alloc - buf->len;
va_list ap_copy;
Expand Down
6 changes: 6 additions & 0 deletions vm/std/std.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ void vm_std_set_arg(vm_config_t *config, vm_table_t *std, const char *prog, cons
VM_TABLE_SET(std, str, "arg", table, arg);
}

void vm_lua_comp_op_std_pow(vm_std_closure_t *closure, vm_std_value_t *args);

vm_table_t *vm_std_new(vm_config_t *config) {
vm_table_t *std = vm_table_new();

Expand Down Expand Up @@ -533,5 +535,9 @@ vm_table_t *vm_std_new(vm_config_t *config) {
VM_TABLE_SET(std, str, "assert", ffi, VM_STD_REF(config, vm_std_assert));
VM_TABLE_SET(std, str, "load", ffi, VM_STD_REF(config, vm_std_load));

VM_STD_REF(config, vm_std_vm_concat);
VM_STD_REF(config, vm_lua_comp_op_std_pow);
VM_STD_REF(config, vm_std_vm_closure);

return std;
}

0 comments on commit 02893f3

Please sign in to comment.