~sircmpwn/mrsh

0cd01c6f8cd59da0ebc8dd876ccb77062da3821f — Simon Ser 6 months ago eedf067
Rename node to and_or_list

This is the term used by the spec.
7 files changed, 91 insertions(+), 91 deletions(-)

M ast.c
M ast_print.c
M highlight.c
M include/mrsh/ast.h
M include/shell/task.h
M parser/program.c
M shell/task/task.c
M ast.c => ast.c +29 -29
@@ 177,14 177,14 @@ void mrsh_command_destroy(struct mrsh_command *cmd) {
	assert(0);
}

void mrsh_node_destroy(struct mrsh_node *node) {
	if (node == NULL) {
void mrsh_and_or_list_destroy(struct mrsh_and_or_list *and_or_list) {
	if (and_or_list == NULL) {
		return;
	}

	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *p = mrsh_node_get_pipeline(node);
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *p = mrsh_and_or_list_get_pipeline(and_or_list);
		for (size_t i = 0; i < p->commands.len; ++i) {
			struct mrsh_command *cmd = p->commands.data[i];
			mrsh_command_destroy(cmd);


@@ 192,10 192,10 @@ void mrsh_node_destroy(struct mrsh_node *node) {
		mrsh_array_finish(&p->commands);
		free(p);
		return;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		mrsh_node_destroy(binop->left);
		mrsh_node_destroy(binop->right);
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		mrsh_and_or_list_destroy(binop->left);
		mrsh_and_or_list_destroy(binop->right);
		free(binop);
		return;
	}


@@ 207,7 207,7 @@ void mrsh_command_list_destroy(struct mrsh_command_list *l) {
		return;
	}

	mrsh_node_destroy(l->node);
	mrsh_and_or_list_destroy(l->and_or_list);
	free(l);
}



@@ 424,30 424,30 @@ struct mrsh_function_definition *mrsh_command_get_function_definition(
struct mrsh_pipeline *mrsh_pipeline_create(struct mrsh_array *commands,
		bool bang) {
	struct mrsh_pipeline *pl = calloc(1, sizeof(struct mrsh_pipeline));
	pl->node.type = MRSH_NODE_PIPELINE;
	pl->and_or_list.type = MRSH_AND_OR_LIST_PIPELINE;
	pl->commands = *commands;
	pl->bang = bang;
	return pl;
}

struct mrsh_binop *mrsh_binop_create(enum mrsh_binop_type type,
		struct mrsh_node *left, struct mrsh_node *right) {
		struct mrsh_and_or_list *left, struct mrsh_and_or_list *right) {
	struct mrsh_binop *binop = calloc(1, sizeof(struct mrsh_binop));
	binop->node.type = MRSH_NODE_BINOP;
	binop->and_or_list.type = MRSH_AND_OR_LIST_BINOP;
	binop->type = type;
	binop->left = left;
	binop->right = right;
	return binop;
}

struct mrsh_pipeline *mrsh_node_get_pipeline(const struct mrsh_node *node) {
	assert(node->type == MRSH_NODE_PIPELINE);
	return (struct mrsh_pipeline *)node;
struct mrsh_pipeline *mrsh_and_or_list_get_pipeline(const struct mrsh_and_or_list *and_or_list) {
	assert(and_or_list->type == MRSH_AND_OR_LIST_PIPELINE);
	return (struct mrsh_pipeline *)and_or_list;
}

struct mrsh_binop *mrsh_node_get_binop(const struct mrsh_node *node) {
	assert(node->type == MRSH_NODE_BINOP);
	return (struct mrsh_binop *)node;
struct mrsh_binop *mrsh_and_or_list_get_binop(const struct mrsh_and_or_list *and_or_list) {
	assert(and_or_list->type == MRSH_AND_OR_LIST_BINOP);
	return (struct mrsh_binop *)and_or_list;
}

static void position_next(struct mrsh_position *dst,


@@ 833,10 833,10 @@ struct mrsh_command *mrsh_command_copy(const struct mrsh_command *cmd) {
	assert(0);
}

struct mrsh_node *mrsh_node_copy(const struct mrsh_node *node) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
struct mrsh_and_or_list *mrsh_and_or_list_copy(const struct mrsh_and_or_list *and_or_list) {
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_and_or_list_get_pipeline(and_or_list);
		struct mrsh_array commands = {0};
		mrsh_array_reserve(&commands, pl->commands.len);
		for (size_t i = 0; i < pl->commands.len; ++i) {


@@ 845,12 845,12 @@ struct mrsh_node *mrsh_node_copy(const struct mrsh_node *node) {
		}
		struct mrsh_pipeline *p_copy =
			mrsh_pipeline_create(&commands, pl->bang);
		return &p_copy->node;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		return &p_copy->and_or_list;
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		struct mrsh_binop *binop_copy = mrsh_binop_create(binop->type,
			mrsh_node_copy(binop->left), mrsh_node_copy(binop->right));
		return &binop_copy->node;
			mrsh_and_or_list_copy(binop->left), mrsh_and_or_list_copy(binop->right));
		return &binop_copy->and_or_list;
	}
	assert(0);
}


@@ 859,7 859,7 @@ struct mrsh_command_list *mrsh_command_list_copy(
		const struct mrsh_command_list *l) {
	struct mrsh_command_list *l_copy =
		calloc(1, sizeof(struct mrsh_command_list));
	l_copy->node = mrsh_node_copy(l->node);
	l_copy->and_or_list = mrsh_and_or_list_copy(l->and_or_list);
	l_copy->ampersand = l->ampersand;
	return l_copy;
}

M ast_print.c => ast_print.c +10 -10
@@ 444,7 444,7 @@ static const char *binop_type_str(enum mrsh_binop_type type) {
	return NULL;
}

static void print_node(struct mrsh_node *node, const char *prefix);
static void print_and_or_list(struct mrsh_and_or_list *and_or_list, const char *prefix);

static void print_binop(struct mrsh_binop *binop, const char *prefix) {
	printf("binop %s\n", binop_type_str(binop->type));


@@ 453,21 453,21 @@ static void print_binop(struct mrsh_binop *binop, const char *prefix) {

	make_sub_prefix(prefix, false, sub_prefix);
	print_prefix(prefix, false);
	print_node(binop->left, sub_prefix);
	print_and_or_list(binop->left, sub_prefix);

	make_sub_prefix(prefix, true, sub_prefix);
	print_prefix(prefix, true);
	print_node(binop->right, sub_prefix);
	print_and_or_list(binop->right, sub_prefix);
}

static void print_node(struct mrsh_node *node, const char *prefix) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
static void print_and_or_list(struct mrsh_and_or_list *and_or_list, const char *prefix) {
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_and_or_list_get_pipeline(and_or_list);
		print_pipeline(pl, prefix);
		break;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		print_binop(binop, prefix);
		break;
	}


@@ 477,7 477,7 @@ static void print_command_list(struct mrsh_command_list *list,
		const char *prefix) {
	printf("command_list%s ─ ", list->ampersand ? " &" : "");

	print_node(list->node, prefix);
	print_and_or_list(list->and_or_list, prefix);
}

static void print_program(struct mrsh_program *prog, const char *prefix) {

M highlight.c => highlight.c +10 -10
@@ 250,11 250,11 @@ static void highlight_command(struct highlight_state *state,
	}
}

static void highlight_node(struct highlight_state *state,
		struct mrsh_node *node) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
static void highlight_and_or_list(struct highlight_state *state,
		struct mrsh_and_or_list *and_or_list) {
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_and_or_list_get_pipeline(and_or_list);
		if (mrsh_position_valid(&pl->bang_pos)) {
			highlight_char(state, &pl->bang_pos, FORMAT_GREEN);
		}


@@ 263,18 263,18 @@ static void highlight_node(struct highlight_state *state,
			highlight_command(state, cmd);
		}
		break;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		highlight_node(state, binop->left);
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		highlight_and_or_list(state, binop->left);
		highlight_str(state, &binop->op_range, FORMAT_GREEN);
		highlight_node(state, binop->right);
		highlight_and_or_list(state, binop->right);
		break;
	}
}

static void highlight_command_list(struct highlight_state *state,
		struct mrsh_command_list *list) {
	highlight_node(state, list->node);
	highlight_and_or_list(state, list->and_or_list);
	if (mrsh_position_valid(&list->separator_pos)) {
		highlight_char(state, &list->separator_pos, FORMAT_GREEN);
	}

M include/mrsh/ast.h => include/mrsh/ast.h +17 -17
@@ 322,25 322,25 @@ struct mrsh_function_definition {
	struct mrsh_position lparen_pos, rparen_pos;
};

enum mrsh_node_type {
	MRSH_NODE_PIPELINE,
	MRSH_NODE_BINOP,
enum mrsh_and_or_list_type {
	MRSH_AND_OR_LIST_PIPELINE,
	MRSH_AND_OR_LIST_BINOP,
};

/**
 * A node is an AND-OR list component. It is either a pipeline or a binary
 * A and_or_list is an AND-OR list component. It is either a pipeline or a binary
 * operation.
 */
struct mrsh_node {
	enum mrsh_node_type type;
struct mrsh_and_or_list {
	enum mrsh_and_or_list_type type;
};

/**
 * A pipeline is a type of node which consists of multiple commands
 * A pipeline is a type of and_or_list which consists of multiple commands
 * separated by `|`. The format is: `[!] command1 [ | command2 ...]`.
 */
struct mrsh_pipeline {
	struct mrsh_node node;
	struct mrsh_and_or_list and_or_list;
	struct mrsh_array commands; // struct mrsh_command *
	bool bang; // whether the pipeline begins with `!`



@@ 354,13 354,13 @@ enum mrsh_binop_type {
};

/**
 * A binary operation is a type of node which consists of multiple pipelines
 * A binary operation is a type of and_or_list which consists of multiple pipelines
 * separated by `&&` or `||`.
 */
struct mrsh_binop {
	struct mrsh_node node;
	struct mrsh_and_or_list and_or_list;
	enum mrsh_binop_type type;
	struct mrsh_node *left, *right;
	struct mrsh_and_or_list *left, *right;

	struct mrsh_range op_range;
};


@@ 370,7 370,7 @@ struct mrsh_binop {
 * execution) or `&` (for asynchronous execution).
 */
struct mrsh_command_list {
	struct mrsh_node *node;
	struct mrsh_and_or_list *and_or_list;
	bool ampersand; // whether the command list ends with `&`

	struct mrsh_position separator_pos; // can be invalid


@@ 390,7 390,7 @@ void mrsh_word_destroy(struct mrsh_word *word);
void mrsh_io_redirect_destroy(struct mrsh_io_redirect *redir);
void mrsh_assignment_destroy(struct mrsh_assignment *assign);
void mrsh_command_destroy(struct mrsh_command *cmd);
void mrsh_node_destroy(struct mrsh_node *node);
void mrsh_and_or_list_destroy(struct mrsh_and_or_list *and_or_list);
void mrsh_command_list_destroy(struct mrsh_command_list *l);
void mrsh_program_destroy(struct mrsh_program *prog);



@@ 447,9 447,9 @@ struct mrsh_function_definition *mrsh_command_get_function_definition(
struct mrsh_pipeline *mrsh_pipeline_create(struct mrsh_array *commands,
	bool bang);
struct mrsh_binop *mrsh_binop_create(enum mrsh_binop_type type,
	struct mrsh_node *left, struct mrsh_node *right);
struct mrsh_pipeline *mrsh_node_get_pipeline(const struct mrsh_node *node);
struct mrsh_binop *mrsh_node_get_binop(const struct mrsh_node *node);
	struct mrsh_and_or_list *left, struct mrsh_and_or_list *right);
struct mrsh_pipeline *mrsh_and_or_list_get_pipeline(const struct mrsh_and_or_list *and_or_list);
struct mrsh_binop *mrsh_and_or_list_get_binop(const struct mrsh_and_or_list *and_or_list);

void mrsh_word_range(struct mrsh_word *word, struct mrsh_position *begin,
	struct mrsh_position *end);


@@ 464,7 464,7 @@ struct mrsh_io_redirect *mrsh_io_redirect_copy(
struct mrsh_assignment *mrsh_assignment_copy(
	const struct mrsh_assignment *assign);
struct mrsh_command *mrsh_command_copy(const struct mrsh_command *cmd);
struct mrsh_node *mrsh_node_copy(const struct mrsh_node *node);
struct mrsh_and_or_list *mrsh_and_or_list_copy(const struct mrsh_and_or_list *and_or_list);
struct mrsh_command_list *mrsh_command_list_copy(
	const struct mrsh_command_list *l);
struct mrsh_program *mrsh_program_copy(const struct mrsh_program *prog);

M include/shell/task.h => include/shell/task.h +1 -1
@@ 27,7 27,7 @@ int run_word(struct context *ctx, struct mrsh_word **word_ptr,
	enum tilde_expansion tilde_expansion);
int run_simple_command(struct context *ctx, struct mrsh_simple_command *sc);
int run_command(struct context *ctx, struct mrsh_command *cmd);
int run_node(struct context *ctx, struct mrsh_node *node);
int run_and_or_list(struct context *ctx, struct mrsh_and_or_list *and_or_list);
int run_pipeline(struct context *ctx, struct mrsh_pipeline *pipeline);
int run_command_list_array(struct context *ctx, struct mrsh_array *array);


M parser/program.c => parser/program.c +14 -14
@@ 360,16 360,16 @@ static int separator(struct mrsh_parser *state) {
	return -1;
}

static struct mrsh_node *and_or(struct mrsh_parser *state);
static struct mrsh_and_or_list *and_or(struct mrsh_parser *state);

static struct mrsh_command_list *term(struct mrsh_parser *state) {
	struct mrsh_node *node = and_or(state);
	if (node == NULL) {
	struct mrsh_and_or_list *and_or_list = and_or(state);
	if (and_or_list == NULL) {
		return NULL;
	}

	struct mrsh_command_list *cmd = calloc(1, sizeof(struct mrsh_command_list));
	cmd->node = node;
	cmd->and_or_list = and_or_list;

	struct mrsh_position separator_pos = state->pos;
	int sep = separator(state);


@@ 955,7 955,7 @@ error_commands:
	return NULL;
}

static struct mrsh_node *and_or(struct mrsh_parser *state) {
static struct mrsh_and_or_list *and_or(struct mrsh_parser *state) {
	struct mrsh_pipeline *pl = pipeline(state);
	if (pl == NULL) {
		return NULL;


@@ 968,30 968,30 @@ static struct mrsh_node *and_or(struct mrsh_parser *state) {
	} else if (operator(state, OR_IF, &op_range)) {
		binop_type = MRSH_BINOP_OR;
	} else {
		return &pl->node;
		return &pl->and_or_list;
	}

	linebreak(state);
	struct mrsh_node *node = and_or(state);
	if (node == NULL) {
		mrsh_node_destroy(&pl->node);
	struct mrsh_and_or_list *and_or_list = and_or(state);
	if (and_or_list == NULL) {
		mrsh_and_or_list_destroy(&pl->and_or_list);
		parser_set_error(state, "expected an AND-OR list");
		return NULL;
	}

	struct mrsh_binop *binop = mrsh_binop_create(binop_type, &pl->node, node);
	struct mrsh_binop *binop = mrsh_binop_create(binop_type, &pl->and_or_list, and_or_list);
	binop->op_range = op_range;
	return &binop->node;
	return &binop->and_or_list;
}

static struct mrsh_command_list *list(struct mrsh_parser *state) {
	struct mrsh_node *node = and_or(state);
	if (node == NULL) {
	struct mrsh_and_or_list *and_or_list = and_or(state);
	if (and_or_list == NULL) {
		return NULL;
	}

	struct mrsh_command_list *cmd = calloc(1, sizeof(struct mrsh_command_list));
	cmd->node = node;
	cmd->and_or_list = and_or_list;

	struct mrsh_position separator_pos = state->pos;
	int sep = separator_op(state);

M shell/task/task.c => shell/task/task.c +10 -10
@@ 255,14 255,14 @@ int run_command(struct context *ctx, struct mrsh_command *cmd) {
	assert(false);
}

int run_node(struct context *ctx, struct mrsh_node *node) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
int run_and_or_list(struct context *ctx, struct mrsh_and_or_list *and_or_list) {
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_and_or_list_get_pipeline(and_or_list);
		return run_pipeline(ctx, pl);
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		int left_status = run_node(ctx, binop->left);
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		int left_status = run_and_or_list(ctx, binop->left);
		switch (binop->type) {
		case MRSH_BINOP_AND:
			if (left_status != 0) {


@@ 275,7 275,7 @@ int run_node(struct context *ctx, struct mrsh_node *node) {
			}
			break;
		}
		return run_node(ctx, binop->right);
		return run_and_or_list(ctx, binop->right);
	}
	assert(false);
}


@@ 304,7 304,7 @@ int run_command_list_array(struct context *ctx, struct mrsh_array *array) {
					close(fd);
				}

				int ret = run_node(&child_ctx, list->node);
				int ret = run_and_or_list(&child_ctx, list->and_or_list);
				if (ret < 0) {
					exit(127);
				}


@@ 312,7 312,7 @@ int run_command_list_array(struct context *ctx, struct mrsh_array *array) {
			}
			ret = 0;
		} else {
			ret = run_node(ctx, list->node);
			ret = run_and_or_list(ctx, list->and_or_list);
			if (ret < 0) {
				return ret;
			}