Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <[email protected]>
  • Loading branch information
ShawSumma committed Feb 13, 2024
1 parent 2c37f9d commit 18d56c2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 34 deletions.
56 changes: 31 additions & 25 deletions vm/lua/repl.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#include "./repl.h"
#include "./parser.h"
#include "../ir/ir.h"
#include "../../vendor/trees/api.h"
#include "../ast/ast.h"
#include "../ast/comp.h"
#include "../ast/print.h"
#include "../backend/tb.h"
#include "../ir/ir.h"
#include "../std/io.h"
#include "../std/util.h"
#include "../../vendor/trees/api.h"
#include "./parser.h"

const TSLanguage *tree_sitter_lua(void);
vm_ast_node_t vm_lang_lua_parse(vm_config_t *config, const char *str);

vm_std_value_t vm_lang_lua_repl_table_get(vm_table_t *table, const char *key) {
vm_pair_t pair = (vm_pair_t) {
vm_pair_t pair = (vm_pair_t){
.key_tag = VM_TAG_STR,
.key_val.str = key,
};
vm_table_get_pair(table, &pair);
return (vm_std_value_t) {
return (vm_std_value_t){
.value = pair.val_val,
.tag = pair.val_tag,
};
Expand Down Expand Up @@ -62,11 +62,12 @@ void vm_lang_lua_repl_table_set_config(vm_table_t *table, vm_config_t *config) {
VM_STD_SET_BOOL(dump, "time", config->dump_time);
}

#if !defined(EMSCRIPTEN)
void vm_lang_lua_repl_completer(ic_completion_env_t *cenv, const char *prefix) {
vm_lang_lua_repl_complete_state_t *state = cenv->arg;
ptrdiff_t len = strlen(prefix);
ptrdiff_t head = len - 1;
while (head >= 0 &&(iswalnum(prefix[head]) || prefix[head] == '.')) {
while (head >= 0 && (iswalnum(prefix[head]) || prefix[head] == '.')) {
head -= 1;
}
head += 1;
Expand All @@ -81,7 +82,7 @@ with_new_std:;
while (got[i] != '\0') {
if (last_word[i] == '\0') {
const char *completions[2];
completions[0] = got+i;
completions[0] = got + i;
completions[1] = NULL;
if (!ic_add_completions(cenv, "", completions)) {
goto ret;
Expand All @@ -97,7 +98,7 @@ with_new_std:;
if (pair->val_tag == VM_TAG_TAB) {
if (last_word[i] == '.') {
std = pair->val_val.table;
last_word = &last_word[i+1];
last_word = &last_word[i + 1];
goto with_new_std;
}
if (!strcmp(last_word, got)) {
Expand Down Expand Up @@ -129,7 +130,7 @@ const char *vm_lang_lua_repl_highlight_bracket_color(vm_table_t *repl, size_t de
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);
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 || sub->val_tag != VM_TAG_STR) {
return "";
}
Expand Down Expand Up @@ -214,6 +215,7 @@ void vm_lang_lua_repl_highlight(ic_highlight_env_t *henv, const char *input, voi
// fclose(out);
// ic_highlight(henv, 1, strlen(input) - 2, "keyword");
}
#endif

void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks) {
config->is_repl = true;
Expand All @@ -222,13 +224,15 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
vm_lang_lua_repl_table_set_config(repl, config);
VM_STD_SET_BOOL(repl, "echo", true);
vm_table_t *parens = vm_table_new();
vm_table_set(parens, (vm_value_t) {.i32 = 1}, (vm_value_t) {.str = "yellow"}, VM_TAG_I32, VM_TAG_STR);
vm_table_set(parens, (vm_value_t) {.i32 = 2}, (vm_value_t) {.str = "magenta"}, VM_TAG_I32, VM_TAG_STR);
vm_table_set(parens, (vm_value_t) {.i32 = 3}, (vm_value_t) {.str = "blue"}, VM_TAG_I32, VM_TAG_STR);
vm_table_set(parens, (vm_value_t){.i32 = 1}, (vm_value_t){.str = "yellow"}, VM_TAG_I32, VM_TAG_STR);
vm_table_set(parens, (vm_value_t){.i32 = 2}, (vm_value_t){.str = "magenta"}, VM_TAG_I32, VM_TAG_STR);
vm_table_set(parens, (vm_value_t){.i32 = 3}, (vm_value_t){.str = "blue"}, VM_TAG_I32, VM_TAG_STR);
VM_STD_SET_TAB(repl, "parens", parens);
VM_STD_SET_TAB(std, "config", repl);

#if !defined(EMSCRIPTEN)
ic_set_history(".minivm-history", 2000);
#endif

vm_lang_lua_repl_complete_state_t complete_state = (vm_lang_lua_repl_complete_state_t){
.config = config,
Expand All @@ -239,25 +243,25 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
.std = std,
};

#if defined(EMSCRIPTEN)
#if defined(EMSCRIPTEN)
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
#endif
#endif

while (true) {
#if !defined(EMSCRIPTEN)
#if !defined(EMSCRIPTEN)
char *input = ic_readline_ex(
"lua",
"lua",
vm_lang_lua_repl_completer,
&complete_state,
vm_lang_lua_repl_highlight,
vm_lang_lua_repl_highlight,
&highlight_state
);
if (input == NULL) {
break;
}
#else
#else
printf("lua> ");
char input[256];
size_t head = 0;
Expand All @@ -270,16 +274,18 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
printf("\x1B[D \x1B[D");
head -= 1;
} else {
printf("%c", (int) c);
printf("%c", (int)c);
}
input[head++] = c;
if (c == '\0') {
break;
}
}
#endif
#endif
vm_lang_lua_repl_table_get_config(repl, config);
#if !defined(EMSCRIPTEN)
ic_history_add(input);
#endif
clock_t start = clock();

if (config->dump_src) {
Expand All @@ -288,14 +294,14 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
}

vm_ast_node_t node = vm_lang_lua_parse(config, input);
#if !defined(EMSCRIPTEN)
#if !defined(EMSCRIPTEN)
free(input);
#endif
#endif

if (config->dump_ast) {
vm_io_buffer_t buf = {0};
vm_ast_print_node(&buf, 0, "", node);
printf("\n--- ast ---\n%.*s", (int) buf.len, buf.buf);
printf("\n--- ast ---\n%.*s", (int)buf.len, buf.buf);
}

vm_ast_comp_more(node, blocks);
Expand All @@ -304,7 +310,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
if (config->dump_ir) {
vm_io_buffer_t buf = {0};
vm_io_format_blocks(&buf, blocks);
printf("%.*s", (int) buf.len, buf.buf);
printf("%.*s", (int)buf.len, buf.buf);
}

vm_std_value_t value = vm_tb_run_repl(config, blocks->entry, blocks, std);
Expand All @@ -313,7 +319,7 @@ void vm_lang_lua_repl(vm_config_t *config, vm_table_t *std, vm_blocks_t *blocks)
} else if (vm_lang_lua_repl_table_get_bool(repl, "echo") && 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);
printf("%.*s", (int)buf.len, buf.buf);
}

if (config->dump_time) {
Expand Down
2 changes: 2 additions & 0 deletions vm/lua/repl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include "../config.h"
#include "../obj.h"
#include "../lib.h"
#if !defined(EMSCRIPTEN)
#include "../../vendor/isocline/isocline.h"
#include "../../vendor/isocline/completions.h"
#endif

struct vm_lang_lua_repl_complete_state_t;
struct vm_lang_lua_repl_complete_state_t;
Expand Down
3 changes: 2 additions & 1 deletion web.mak
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
EXE ?= .js
CC = emcc
TCC_SRCS =
ISOCLINE_SRCS =

CFLAGS := -fPIC $(CFLAGS)
LDFLAGS := -s BINARYEN_ASYNC_COMPILATION=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s ENVIRONMENT=web -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" $(LDFLAGS)
LDFLAGS := -s BINARYEN_ASYNC_COMPILATION=0 -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s ALLOW_MEMORY_GROWTH=1 -s EXPORT_ES6=1 -s ENVIRONMENT=worker -s MAIN_MODULE=2 -s EXPORTED_RUNTIME_METHODS="['FS','callMain']" $(LDFLAGS)

include makefile

1 change: 0 additions & 1 deletion web/src/lib/comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let flags2 = '-L/lazy/emscripten/cache/sysroot/lib/wasm32-emscripten/pic --no-wh
let comps = 0;
export const comp = (cBuf) => {
comps += 1;
console.log(cBuf);
emception.fileSystem.writeFile(`/working/in${comps}.c`, cBuf);
const result1 = emception.runx(`/usr/bin/clang -O2 -c ${flags1} /working/in${comps}.c -o /working/mid${comps}.o`);
if (result1.returncode !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/emception.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Emception {
}

fileSystem.mkdirTree("/usr/local");
fileSystem.symlink("/lazy/emscripten", "/emscripten");
fileSystem.symlink("/lazy/cpython", "/usr/local/lib");
// fileSystem.symlink("/lazy/emscripten", "/emscripten");
// fileSystem.symlink("/lazy/cpython", "/usr/local/lib");
fileSystem.symlink("/lazy/wasm", "/wasm");

for (const preload of preloads) {
Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const run = (args, opts) => {
mod.FS.writeFile(`/out${n}.so`, soBuf);
};

globalThis.vm_compile_c_to_wasm = comp;
self.vm_compile_c_to_wasm = comp;

const mod = Module({
noInitialRun: true,
Expand Down
21 changes: 17 additions & 4 deletions web/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import CompressionPlugin from 'compression-webpack-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import path from 'path';
import { fileURLToPath } from 'url';
import webpack from 'webpack';

const dev = false;
const dev = true;

export default {
mode: dev ? 'development' : 'production',
Expand All @@ -29,6 +31,10 @@ export default {
process: false,
},
},
externals: {
'brotli.mjs': false,
'brotli.wasm': false,
},
module: {
rules: [
{
Expand All @@ -53,6 +59,9 @@ export default {
{
test: /\.wasm$/,
type: "asset/resource",
generator: {
filename: 'wasm/[name].wasm',
},
},
{
test: /\.(pack|br|a)$/,
Expand Down Expand Up @@ -90,9 +99,13 @@ export default {
{ from: "src/index.html", to: "index.html" },
],
}),
new CompressionPlugin({
exclude: /\.br$/,
}),
new webpack.NormalModuleReplacementPlugin(
/emception\/brotli\/brotli\.mjs$/,
path.resolve(fileURLToPath(new URL('.', import.meta.url)), './src/empty.js'),
),
// new CompressionPlugin({
// exclude: /\.br$/,
// }),
],
devServer: {
headers: {
Expand Down

0 comments on commit 18d56c2

Please sign in to comment.