Ejemplo n.º 1
0
void repl() {
	char *input;

	while ((input = readline("> ")) != NULL) {
		int ss = stack_size;
	read_start:;
#ifdef READLINE
		if (input && *input)
			add_history(input);
#endif

		const char *p = input;
		error err;

		atom expr;
		err = read_expr(p, &p, &expr);
		if (err == ERROR_FILE) { /* read more lines */
			char *line = readline("  ");
			if (!line) break;
			input = strcat_alloc(&input, "\n");
			input = strcat_alloc(&input, line);
			free(line);
			goto read_start;
		}
		if (!err) {
			while (1) {
				atom result;
				error err = macex_eval(expr, &result);
				if (err) {
					print_error(err);
					printf("error in expression:\n");
					print_expr(expr);
					putchar('\n');
					break;
				}
				else {
					print_expr(result);
					puts("");
				}
				err = read_expr(p, &p, &expr);
				if (err != ERROR_OK) {
					break;
				}
			}
		} else {
			print_error(err);
		}
		stack_restore(ss);
		free(input);
	}
}
Ejemplo n.º 2
0
void repl() {
	char *input;

	while ((input = readline("> ")) != NULL) {
		int ss = stack_size;
	read_start:
		arc_reader_unclosed = 0;
#ifdef READLINE
		if (input && *input)
			add_history(input);
#endif

		char *buf = (char *)malloc(strlen(input) + 4);
		sprintf(buf, "(%s\n)", input);
		const char *p = buf;
		error err;
		atom result;

		atom code_expr;
		err = read_expr(p, &p, &code_expr);
		if (arc_reader_unclosed > 0) { /* read more lines */
			char *line = readline("  ");
			if (!line) break;
			input = strcat_alloc(&input, "\n");
			input = strcat_alloc(&input, line);
			goto read_start;
		}
		if (!err) {
			while (!no(code_expr)) {
				err = macex_eval(car(code_expr), &result);
				if (err) {
					print_error(err);
					break;
				}
				else {
					print_expr(result);
					putchar('\n');
				}
				code_expr = cdr(code_expr);
			}
		}
		stack_restore(ss);
		free(buf);
		free(input);
	}
}