Selaa lähdekoodia

Add a simple REPL using linenoise

Lucas Stadler 9 vuotta sitten
vanhempi
commit
c21db3c52f
3 muutettua tiedostoa jossa 37 lisäystä ja 16 poistoa
  1. 3 0
      c/ton/.gitignore
  2. 7 1
      c/ton/Makefile
  3. 27 15
      c/ton/main.c

+ 3 - 0
c/ton/.gitignore

@ -2,6 +2,9 @@
2 2
/out
3 3
/jsc-funcs
4 4
5
/linenoise.c
6
/linenoise.h
7
5 8
/planck
6 9
7 10
/.vscode

+ 7 - 1
c/ton/Makefile

@ -3,8 +3,14 @@ CC = clang
3 3
JSC_CFLAGS = $(shell pkg-config javascriptcoregtk-4.0 --cflags --libs)
4 4
LIBZIP_CFLAGS = $(shell pkg-config libzip --cflags --libs)
5 5
6
ton: *.c
6
ton: *.c linenoise.c
7 7
	$(CC) $(JSC_CFLAGS) $(LIBZIP_CFLAGS) -DDEBUG $^ -o $@
8 8
9
linenoise.c: linenoise.h
10
	curl -LsSfo $@ https://github.com/antirez/linenoise/raw/master/linenoise.c
11
12
linenoise.h:
13
	curl -LsSfo $@ https://github.com/antirez/linenoise/raw/master/linenoise.h
14
9 15
jsc-funcs:
10 16
	grep -Rh JS_EXPORT /usr/include/webkitgtk-4.0/JavaScriptCore | sed 's/^JS_EXPORT //' | grep -v '^#' > $@

+ 27 - 15
c/ton/main.c

@ -7,6 +7,8 @@
7 7
8 8
#include <JavaScriptCore/JavaScript.h>
9 9
10
#include "linenoise.h"
11
10 12
#include "zip.h"
11 13
12 14
#define CONSOLE_LOG_BUF_SIZE 1000
@ -15,7 +17,7 @@ char console_log_buf[CONSOLE_LOG_BUF_SIZE];
15 17
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
16 18
JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source);
17 19
18
void evaluate_source(JSContextRef ctx, char *type, char *source_value, bool expression, char *set_ns);
20
JSValueRef evaluate_source(JSContextRef ctx, char *type, char *source_value, bool expression, char *set_ns);
19 21
char *munge(char *s);
20 22
21 23
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
@ -304,19 +306,27 @@ int main(int argc, char **argv) {
304 306
	printf("---\nrequire macros\n---\n");
305 307
	evaluate_source(ctx, "text", "(require-macros 'planck.repl 'planck.core 'planck.shell 'planck.from.io.aviso.ansi 'clojure.template 'cljs.spec 'cljs.spec.impl.gen 'cljs.test)", true, "cljs.user");
306 308
307
	char *script;
308
	if (argc == 0) {
309
		script = "CONSOLE_LOG(\"Hello, World!\");";
310
	} else {
311
		script = argv[1];
309
	if (repl) {
310
		bool javascript = false;
311
		char *prompt = javascript ? " > " : " => ";
312
313
		char *line;
314
		while ((line = linenoise(prompt)) != NULL) {
315
			JSValueRef res = NULL;
316
			if (javascript) {
317
				res = evaluate_script(ctx, line, "<stdin>");
318
			} else {
319
				res = evaluate_source(ctx, "text", line, true, "cljs.user");
320
			}
321
			free(line);
322
323
			char res_buf[1000];
324
			res_buf[0] = '\0';
325
			JSStringRef res_str = to_string(ctx, res);
326
			JSStringGetUTF8CString(res_str, res_buf, 1000);
327
			printf("%s\n", res_buf);
328
		}
312 329
	}
313
	JSValueRef res = evaluate_script(ctx, script, "<inline>");
314
315
	char res_buf[1000];
316
	res_buf[0] = '\0';
317
	JSStringRef res_str = to_string(ctx, res);
318
	JSStringGetUTF8CString(res_str, res_buf, 1000);
319
	printf("%s\n", res_buf);
320 330
}
321 331
322 332
JSStringRef to_string(JSContextRef ctx, JSValueRef val) {
@ -394,7 +404,7 @@ JSObjectRef get_function(JSContextRef ctx, char *namespace, char *name) {
394 404
	return JSValueToObject(ctx, val, NULL);
395 405
}
396 406
397
void evaluate_source(JSContextRef ctx, char *type, char *source, bool expression, char *set_ns) {
407
JSValueRef evaluate_source(JSContextRef ctx, char *type, char *source, bool expression, char *set_ns) {
398 408
	JSValueRef args[7];
399 409
	int num_args = 7;
400 410
@ -419,10 +429,12 @@ void evaluate_source(JSContextRef ctx, char *type, char *source, bool expression
419 429
	JSObjectRef execute_fn = get_function(ctx, "planck.repl", "execute");
420 430
	JSObjectRef global_obj = JSContextGetGlobalObject(ctx);
421 431
	JSValueRef ex = NULL;
422
	JSObjectCallAsFunction(ctx, execute_fn, global_obj, num_args, args, &ex);
432
	JSValueRef val = JSObjectCallAsFunction(ctx, execute_fn, global_obj, num_args, args, &ex);
423 433
424 434
	debug_print_value
425 435
("planck.repl/execute", ctx, ex);
436
437
	return ex != NULL ? ex : val;
426 438
}
427 439
428 440
char *munge(char *s) {