瀏覽代碼

Support basic commandline arguments

Including preparations for running code from files, and support for
'-e'.
Lucas Stadler 9 年之前
父節點
當前提交
9bc70ed00f
共有 1 個文件被更改,包括 90 次插入4 次删除
  1. 90 4
      c/ton/main.c

+ 90 - 4
c/ton/main.c

@ -1,5 +1,6 @@
1 1
#include <assert.h>
2 2
#include <errno.h>
3
#include <getopt.h>
3 4
#include <stdio.h>
4 5
#include <stdlib.h>
5 6
#include <string.h>
@ -241,7 +242,92 @@ void register_global_function(JSContextRef ctx, char *name, JSObjectCallAsFuncti
241 242
	JSObjectSetProperty(ctx, global_obj, fn_name, fn_obj, kJSPropertyAttributeNone, NULL);
242 243
}
243 244
245
void usage(char *program_name) {
246
	printf("%s [flags]\n", program_name);
247
	printf("\n");
248
	printf(
249
		"  -h, --help       Display this help message\n"
250
		"  -v, --verbose    Print verbose diagnostics\n"
251
		"  -r, --repl       Whether to start a REPL\n"
252
		"  -s, --static-fns Whether to use the :static-fns compiler option\n"
253
		"  -k, --cache      The directory to cache compiler results in\n"
254
		"  -j, --javascript Whether to run a JavaScript REPL\n"
255
		"  -e, --eval       Evaluate the given expression\n"
256
	);
257
}
258
259
bool verbose = false;
260
bool repl = false;
261
bool static_fns = false;
262
char *cache_path = NULL;
263
264
bool javascript = false;
265
244 266
int main(int argc, char **argv) {
267
	int num_eval_args = 0;
268
	char **eval_args = NULL;
269
270
	struct option long_options[] = {
271
		{"help", no_argument, NULL, 'h'},
272
		{"verbose", no_argument, NULL, 'v'},
273
		{"repl", no_argument, NULL, 'r'},
274
		{"static-fns", no_argument, NULL, 's'},
275
		{"cache", required_argument, NULL, 'k'},
276
		{"javascript", no_argument, NULL, 'j'},
277
		{"eval", required_argument, NULL, 'e'},
278
279
		{0, 0, 0, 0}
280
	};
281
	int opt, option_index;
282
	while ((opt = getopt_long(argc, argv, "hvrsk:je:", long_options, &option_index)) != -1) {
283
		switch (opt) {
284
		case 'h':
285
			usage(argv[0]);
286
			exit(0);
287
		case 'v':
288
			verbose = true;
289
			break;
290
		case 'r':
291
			repl = true;
292
			break;
293
		case 's':
294
			static_fns = true;
295
			break;
296
		case 'k':
297
			cache_path = argv[optind - 1];
298
			break;
299
		case 'j':
300
			javascript = true;
301
			break;
302
		case 'e':
303
			printf("should eval '%s'\n", optarg);
304
			num_eval_args += 1;
305
			eval_args = realloc(eval_args, num_eval_args * sizeof(char*));
306
			eval_args[num_eval_args - 1] = argv[optind - 1];
307
			break;
308
		case '?':
309
			usage(argv[0]);
310
			exit(1);
311
		default:
312
			printf("unhandled argument: %c\n", opt);
313
		}
314
	}
315
316
	int num_rest_args = 0;
317
	char **rest_args = NULL;
318
	if (optind < argc) {
319
		num_rest_args = argc - optind;
320
		rest_args = malloc((argc - optind) * sizeof(char*));
321
		int i = 0;
322
		while (optind < argc) {
323
			rest_args[i++] = argv[optind++];
324
		}
325
	}
326
327
	if (num_rest_args == 0) {
328
		repl = true;
329
	}
330
245 331
	JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
246 332
247 333
	JSStringRef nameRef = JSStringCreateWithUTF8CString("jsc-test");
@ -280,11 +366,8 @@ int main(int argc, char **argv) {
280 366
	evaluate_script(ctx, "cljs.core.set_print_fn_BANG_.call(null,PLANCK_PRINT_FN);", "<init>");
281 367
	evaluate_script(ctx, "cljs.core.set_print_err_fn_BANG_.call(null,PLANCK_PRINT_ERR_FN);", "<init>");
282 368
283
	bool repl = true;
284
285 369
	{
286 370
		JSValueRef arguments[4];
287
		bool verbose = true, static_fns = true;
288 371
		arguments[0] = JSValueMakeBoolean(ctx, repl);
289 372
		arguments[1] = JSValueMakeBoolean(ctx, verbose);
290 373
		JSStringRef cache_path_str = JSStringCreateWithUTF8CString(".planck_cache");
@ -305,13 +388,16 @@ int main(int argc, char **argv) {
305 388
306 389
	evaluate_script(ctx, "cljs.core._STAR_assert_STAR_ = true;", "<init>");
307 390
391
	for (int i = 0; i < num_eval_args; i++) {
392
		evaluate_source(ctx, "text", eval_args[i], true, "cljs.user");
393
	}
394
308 395
	//evaluate_source(ctx, "text", "(empty? \"\")", true, "cljs.user");
309 396
310 397
	printf("---\nrequire macros\n---\n");
311 398
	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");
312 399
313 400
	if (repl) {
314
		bool javascript = false;
315 401
		char *prompt = javascript ? " > " : " => ";
316 402
317 403
		char *line;