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
/out
2
/out
3
/jsc-funcs
3
/jsc-funcs
4
4
5
/linenoise.c
6
/linenoise.h
7
5
/planck
8
/planck
6
9
7
/.vscode
10
/.vscode

+ 7 - 1
c/ton/Makefile

3
JSC_CFLAGS = $(shell pkg-config javascriptcoregtk-4.0 --cflags --libs)
3
JSC_CFLAGS = $(shell pkg-config javascriptcoregtk-4.0 --cflags --libs)
4
LIBZIP_CFLAGS = $(shell pkg-config libzip --cflags --libs)
4
LIBZIP_CFLAGS = $(shell pkg-config libzip --cflags --libs)
5
5
6
ton: *.c
6
ton: *.c linenoise.c
7
	$(CC) $(JSC_CFLAGS) $(LIBZIP_CFLAGS) -DDEBUG $^ -o $@
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
jsc-funcs:
15
jsc-funcs:
10
	grep -Rh JS_EXPORT /usr/include/webkitgtk-4.0/JavaScriptCore | sed 's/^JS_EXPORT //' | grep -v '^#' > $@
16
	grep -Rh JS_EXPORT /usr/include/webkitgtk-4.0/JavaScriptCore | sed 's/^JS_EXPORT //' | grep -v '^#' > $@

+ 27 - 15
c/ton/main.c

7
7
8
#include <JavaScriptCore/JavaScript.h>
8
#include <JavaScriptCore/JavaScript.h>
9
9
10
#include "linenoise.h"
11
10
#include "zip.h"
12
#include "zip.h"
11
13
12
#define CONSOLE_LOG_BUF_SIZE 1000
14
#define CONSOLE_LOG_BUF_SIZE 1000
15
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
17
JSStringRef to_string(JSContextRef ctx, JSValueRef val);
16
JSValueRef evaluate_script(JSContextRef ctx, char *script, char *source);
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
char *munge(char *s);
21
char *munge(char *s);
20
22
21
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
23
void bootstrap(JSContextRef ctx, char *deps_file_path, char *goog_base_path);
304
	printf("---\nrequire macros\n---\n");
306
	printf("---\nrequire macros\n---\n");
305
	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");
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
JSStringRef to_string(JSContextRef ctx, JSValueRef val) {
332
JSStringRef to_string(JSContextRef ctx, JSValueRef val) {
394
	return JSValueToObject(ctx, val, NULL);
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
	JSValueRef args[7];
408
	JSValueRef args[7];
399
	int num_args = 7;
409
	int num_args = 7;
400
410
419
	JSObjectRef execute_fn = get_function(ctx, "planck.repl", "execute");
429
	JSObjectRef execute_fn = get_function(ctx, "planck.repl", "execute");
420
	JSObjectRef global_obj = JSContextGetGlobalObject(ctx);
430
	JSObjectRef global_obj = JSContextGetGlobalObject(ctx);
421
	JSValueRef ex = NULL;
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
	debug_print_value
434
	debug_print_value
425
("planck.repl/execute", ctx, ex);
435
("planck.repl/execute", ctx, ex);
436
437
	return ex != NULL ? ex : val;
426
}
438
}
427
439
428
char *munge(char *s) {
440
char *munge(char *s) {