bshell: re-organise build into three separate components

bshell: the front-end binary
bshell.runtime: contains the parser, compiler, and classes needed to run bshell scripts
bshell.core: contains the builtin commandlets and aliases
This commit is contained in:
2026-05-25 10:33:29 +01:00
parent 58c76a1f57
commit edfb3e24a3
165 changed files with 5173 additions and 4995 deletions
+923
View File
@@ -0,0 +1,923 @@
#include "../syntax.h"
#include <bshell/runtime/operator.h>
#include <fx/queue.h>
enum expr_component {
EXPR_C_NONE = 0,
EXPR_C_OPERAND,
EXPR_C_BINARY_OP,
EXPR_C_UNARY_OP,
};
struct expr_bshell_parse_ctx {
fx_queue expr_bshell_operator_stack, expr_out_queue;
enum expr_component expr_prev;
unsigned int expr_prev_symbol;
enum bshell_operator_precedence expr_minimum_precedence;
bool expr_done, expr_fail;
};
static bool op_node_is_complete(struct bshell_op_ast_node *node)
{
if (!node->n_op) {
return false;
}
switch (node->n_op->op_arity) {
case BSHELL_OPA_UNARY:
return node->n_right != NULL;
case BSHELL_OPA_BINARY:
return (node->n_left != NULL && node->n_right != NULL);
default:
return false;
}
}
static bool finalise_expr(
struct expr_bshell_parse_ctx *ctx,
struct bshell_ast_node **out,
enum bshell_operator_precedence minimum_precedence)
{
fx_queue_entry *entry = NULL;
while (true) {
entry = fx_queue_pop_back(&ctx->expr_bshell_operator_stack);
if (!entry) {
break;
}
struct bshell_op_ast_node *node = fx_unbox(
struct bshell_op_ast_node,
entry,
n_base.n_entry);
if (!node) {
/* this should never happen */
return false;
}
const struct bshell_operator_info *op = node->n_op;
/* if we aren't processing operators below a certain precedence
* then leave them on the stack and stop here. */
if (op->op_precedence < minimum_precedence) {
fx_queue_push_back(
&ctx->expr_bshell_operator_stack,
entry);
break;
}
fx_queue_push_back(&ctx->expr_out_queue, entry);
}
fx_queue q = FX_QUEUE_INIT;
fx_queue_entry *tmp = NULL;
entry = fx_queue_first(&ctx->expr_out_queue);
int i = 0;
while (entry) {
struct bshell_ast_node *item
= fx_unbox(struct bshell_ast_node, entry, n_entry);
fx_queue_entry *next = fx_queue_next(entry);
fx_queue_delete(&ctx->expr_out_queue, entry);
/* if the node is an operand, just push it to a
* temporary queue and come back to it later. */
if (item->n_type != BSHELL_AST_OP) {
/* operand */
fx_queue_push_back(&q, &item->n_entry);
goto next;
}
const struct bshell_operator_info *op = NULL;
struct bshell_op_ast_node *op_node
= (struct bshell_op_ast_node *)item;
/* if an operator node is already complete (i.e. it
* already has all the operands it needs, it can be
* pushed to the operand queue as-is */
if (op_node_is_complete(op_node)) {
fx_queue_push_back(&q, &item->n_entry);
goto next;
}
/* otherwise, pop the relevant operands from the operand
* queue... */
op = op_node->n_op;
tmp = fx_queue_pop_back(&q);
op_node->n_right
= fx_unbox(struct bshell_ast_node, tmp, n_entry);
if (op_node->n_right) {
op_node->n_right->n_parent
= (struct bshell_ast_node *)op_node;
#if 0
bshell_ast_node_extend_bounds_recursive(
(struct ivy_bshell_ast_node *)op_node,
(struct ivy_bshell_ast_node *)tmp);
#endif
}
if (op->op_arity == BSHELL_OPA_BINARY) {
tmp = fx_queue_pop_back(&q);
op_node->n_left = fx_unbox(
struct bshell_ast_node,
tmp,
n_entry);
if (op_node->n_left) {
op_node->n_left->n_parent
= (struct bshell_ast_node *)op_node;
#if 0
bshell_ast_node_extend_bounds_recursive(
(struct ivy_bshell_ast_node *)op_node,
(struct ivy_bshell_ast_node *)tmp);
#endif
}
}
/* ...and push the newly-completed operator node to the
* operand queue */
fx_queue_push_back(&q, &op_node->n_base.n_entry);
next:
entry = next;
}
#if 0
debug_printf("** after hierarchisation:\n");
print_expr_queues(state);
#endif
/* if we are not processing operators below a certain precedence,
* i.e. when determining the recipient of a keyword-message), these
* operators will still be on the parser state's operator stack, but
* their operands have just been moved to the temporary operand stack
* used above. move them back to the parser state's output queue here
* so they can be used later. */
entry = fx_queue_first(&ctx->expr_bshell_operator_stack);
while (entry) {
fx_queue_entry *entry2 = fx_queue_pop_front(&q);
if (!entry2) {
return false;
}
fx_queue_push_back(&ctx->expr_out_queue, entry2);
entry = fx_queue_next(entry);
}
#if 0
debug_printf("** after de-linearisation:\n");
print_expr_queues(state);
ivy_bshell_ast_node_print(*expr_tree);
debug_printf("------\n");
#endif
/* the final node remaining on the temp operand stack is the
* root node of the new expression tree */
tmp = fx_queue_pop_back(&q);
*out = fx_unbox(struct bshell_ast_node, tmp, n_entry);
return true;
}
bool peek_arith_expr(struct bshell_parse_ctx *ctx)
{
switch (peek_token_type(ctx)) {
case BSHELL_TOK_SYMBOL:
return bshell_operator_get_by_token(peek_unknown_symbol(ctx));
case BSHELL_TOK_INT:
case BSHELL_TOK_DOUBLE:
case BSHELL_TOK_STRING:
case BSHELL_TOK_VAR:
case BSHELL_TOK_STR_START:
case BSHELL_TOK_OPERATOR:
return true;
default:
return false;
}
}
static bool parse_subexpr(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_symbol(ctx, BSHELL_SYM_LEFT_PAREN)) {
report_error(ctx, "expected `(`");
}
struct bshell_ast_node *v = NULL;
if (!parse_expr(ctx, &v)) {
report_error(ctx, "error while parsing parenthesis expression");
return false;
}
if (!parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
report_error(ctx, "expected `)` after parenthesis expression");
return false;
}
*out = v;
return true;
}
static bool parse_stmt_block(
struct bshell_parse_ctx *ctx,
struct bshell_ast_node **out)
{
if (!parse_symbol(ctx, BSHELL_SYM_DOLLAR_LEFT_PAREN)) {
report_error(ctx, "expected `$(`");
return false;
}
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
*out = bshell_ast_node_create(BSHELL_AST_NULL);
return true;
}
struct bshell_ast_node *v = NULL;
if (!parse_statement_list(ctx, &v)) {
return false;
}
if (!parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
report_error(ctx, "expected ')' after subexpression");
bshell_ast_node_destroy(v);
return false;
}
*out = v;
return true;
}
static bool parse_hashtable(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_symbol(ctx, BSHELL_SYM_AT_LEFT_BRACE)) {
report_error(ctx, "expected `@{`");
return false;
}
parse_linefeed(ctx);
struct bshell_hashtable_ast_node *table
= (struct bshell_hashtable_ast_node *)bshell_ast_node_create(
BSHELL_AST_HASHTABLE);
if (!table) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
size_t nr_items = 0;
bool ok = true;
while (ok) {
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) {
break;
}
parse_linefeed(ctx);
struct bshell_hashtable_item_ast_node *item
= (struct bshell_hashtable_item_ast_node *)
bshell_ast_node_create(
BSHELL_AST_HASHTABLE_ITEM);
struct bshell_lex_token *tok = NULL;
if (parse_word(ctx, &tok)) {
struct bshell_string_ast_node *v
= (struct bshell_string_ast_node *)
bshell_ast_node_create(
BSHELL_AST_STRING);
v->n_value = tok;
item->n_key = (struct bshell_ast_node *)v;
} else if (!parse_arith_value(ctx, &item->n_key)) {
report_error(ctx, "failed to parse hashtable key");
bshell_ast_node_destroy((struct bshell_ast_node *)item);
ok = false;
break;
}
if (!parse_symbol(ctx, BSHELL_SYM_EQUAL)) {
report_error(ctx, "expected `=` after hashtable key");
bshell_ast_node_destroy((struct bshell_ast_node *)item);
ok = false;
break;
}
if (!parse_expr(ctx, &item->n_value)) {
report_error(ctx, "failed to parse hashtable value");
bshell_ast_node_destroy((struct bshell_ast_node *)item);
ok = false;
break;
}
fx_queue_push_back(&table->n_items, &item->n_base.n_entry);
nr_items++;
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) {
break;
}
if (!parse_linefeed(ctx)
&& !parse_symbol(ctx, BSHELL_SYM_SEMICOLON)) {
report_error(
ctx,
"expected `;`, `}`, or linefeed after "
"hashtable value");
ok = false;
break;
}
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)table);
return false;
}
*out = (struct bshell_ast_node *)table;
return true;
}
static bool parse_array(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_symbol(ctx, BSHELL_SYM_AT_LEFT_PAREN)) {
report_error(ctx, "expected `@(`");
return false;
}
struct bshell_array_ast_node *array
= (struct bshell_array_ast_node *)bshell_ast_node_create(
BSHELL_AST_ARRAY);
if (!array) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
size_t nr_items = 0;
bool ok = true;
while (ok) {
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
break;
}
if (nr_items && !parse_symbol(ctx, BSHELL_SYM_COMMA)) {
report_error(
ctx,
"expected `,` or `)` after array value");
ok = false;
}
struct bshell_ast_node *item = NULL;
if (!parse_arith_value(ctx, &item)) {
report_error(ctx, "failed to parse array item");
ok = false;
break;
}
fx_queue_push_back(&array->n_items, &item->n_entry);
nr_items++;
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)array);
return false;
}
*out = (struct bshell_ast_node *)array;
return true;
}
bool parse_fstring(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (peek_token_type(ctx) != BSHELL_TOK_STR_START) {
return false;
}
discard_token(ctx);
struct bshell_fstring_ast_node *fstring
= (struct bshell_fstring_ast_node *)bshell_ast_node_create(
BSHELL_AST_FSTRING);
if (!fstring) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
bool ok = true;
while (ok) {
if (peek_token_type(ctx) == BSHELL_TOK_STR_END) {
discard_token(ctx);
break;
}
struct bshell_ast_node *item = NULL;
if (!parse_arith_value(ctx, &item)) {
ok = false;
break;
}
fx_queue_push_back(&fstring->n_elements, &item->n_entry);
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)fstring);
fstring = NULL;
}
*out = (struct bshell_ast_node *)fstring;
return ok;
}
bool parse_arith_value(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
struct bshell_lex_token *tok = peek_token(ctx);
switch (tok->tok_type) {
case BSHELL_TOK_INT: {
struct bshell_int_ast_node *v
= (struct bshell_int_ast_node *)bshell_ast_node_create(
BSHELL_AST_INT);
v->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)v;
return true;
}
case BSHELL_TOK_DOUBLE: {
struct bshell_double_ast_node *v
= (struct bshell_double_ast_node *)
bshell_ast_node_create(BSHELL_AST_DOUBLE);
v->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)v;
return true;
}
case BSHELL_TOK_STRING: {
struct bshell_string_ast_node *v
= (struct bshell_string_ast_node *)
bshell_ast_node_create(BSHELL_AST_STRING);
v->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)v;
return true;
}
case BSHELL_TOK_WORD: {
struct bshell_word_ast_node *v
= (struct bshell_word_ast_node *)bshell_ast_node_create(
BSHELL_AST_WORD);
v->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)v;
return true;
}
case BSHELL_TOK_VAR: {
struct bshell_var_ast_node *v
= (struct bshell_var_ast_node *)bshell_ast_node_create(
BSHELL_AST_VAR);
v->n_ident = claim_token(ctx);
*out = (struct bshell_ast_node *)v;
return true;
}
case BSHELL_TOK_STR_START:
return parse_fstring(ctx, out);
case BSHELL_TOK_SYMBOL:
switch (tok->tok_symbol) {
case BSHELL_SYM_LEFT_PAREN:
return parse_subexpr(ctx, out);
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
return parse_stmt_block(ctx, out);
case BSHELL_SYM_AT_LEFT_BRACE:
return parse_hashtable(ctx, out);
case BSHELL_SYM_AT_LEFT_PAREN:
return parse_array(ctx, out);
case BSHELL_SYM_LEFT_BRACE:
return parse_block(ctx, out);
default:
report_error(ctx, "token is not a valid operand");
return false;
}
break;
default:
report_error(ctx, "token is not a valid operand");
return false;
}
}
static bool parse_operand(struct bshell_parse_ctx *ctx, struct expr_bshell_parse_ctx *expr)
{
if (expr->expr_prev == EXPR_C_OPERAND) {
report_error(ctx, "encountered two operands in a row");
return false;
}
expr->expr_prev = EXPR_C_OPERAND;
struct bshell_ast_node *v = NULL;
if (!parse_arith_value(ctx, &v)) {
return false;
}
fx_queue_push_back(&expr->expr_out_queue, &v->n_entry);
return true;
}
void arith_push_operator(
struct expr_bshell_parse_ctx *state,
struct bshell_op_ast_node *node)
{
const struct bshell_operator_info *op = node->n_op;
if (!op) {
return;
}
while (true) {
fx_queue_entry *top
= fx_queue_last(&state->expr_bshell_operator_stack);
if (!top) {
break;
}
struct bshell_ast_node *top_node
= fx_unbox(struct bshell_ast_node, top, n_entry);
const struct bshell_operator_info *top_op = NULL;
switch (top_node->n_type) {
case BSHELL_AST_OP: {
struct bshell_op_ast_node *op_node
= (struct bshell_op_ast_node *)top_node;
top_op = op_node->n_op;
break;
}
default:
return;
}
if (top_op->op_precedence < op->op_precedence
|| (top_op->op_precedence == op->op_precedence
&& op->op_associativity != BSHELL_ASSOCIATIVITY_LEFT)) {
break;
}
fx_queue_delete(&state->expr_bshell_operator_stack, top);
fx_queue_push_back(&state->expr_out_queue, top);
}
fx_queue_push_back(
&state->expr_bshell_operator_stack,
&node->n_base.n_entry);
}
static bool parse_unary_operator(
struct bshell_parse_ctx *ctx,
struct expr_bshell_parse_ctx *expr)
{
struct bshell_lex_token *tok = peek_token(ctx);
const struct bshell_operator_info *op = NULL;
switch (tok->tok_type) {
case BSHELL_TOK_SYMBOL:
op = bshell_operator_get_by_token(tok->tok_symbol);
break;
case BSHELL_TOK_OPERATOR:
switch (tok->tok_operator) {
case BSHELL_TKOP_SPLIT:
op = bshell_operator_get_by_id(BSHELL_OP_USPLIT);
break;
case BSHELL_TKOP_JOIN:
op = bshell_operator_get_by_id(BSHELL_OP_USPLIT);
break;
default:
op = bshell_operator_get_by_token(tok->tok_operator);
break;
}
break;
default:
break;
}
if (expr->expr_prev == EXPR_C_OPERAND
&& op->op_location == BSHELL_OPL_PREFIX) {
report_error(
ctx,
"unexpected operand before unary "
"operator");
return false;
}
if (!op) {
report_error(ctx, "unknown unary operator");
return false;
}
if (op->op_precedence < expr->expr_minimum_precedence) {
expr->expr_done = true;
return true;
}
expr->expr_prev = EXPR_C_BINARY_OP;
struct bshell_op_ast_node *op_node
= (struct bshell_op_ast_node *)bshell_ast_node_create(
BSHELL_AST_OP);
if (!op_node) {
return false;
}
op_node->n_op = op;
discard_token(ctx);
arith_push_operator(expr, op_node);
return true;
}
static bool parse_binary_operator(
struct bshell_parse_ctx *ctx,
struct expr_bshell_parse_ctx *expr)
{
struct bshell_lex_token *tok = peek_token(ctx);
const struct bshell_operator_info *op = NULL;
switch (tok->tok_type) {
case BSHELL_TOK_SYMBOL:
op = bshell_operator_get_by_token(tok->tok_symbol);
break;
case BSHELL_TOK_OPERATOR:
switch (tok->tok_operator) {
case BSHELL_TKOP_SPLIT:
op = bshell_operator_get_by_id(BSHELL_OP_BSPLIT);
break;
case BSHELL_TKOP_JOIN:
op = bshell_operator_get_by_id(BSHELL_OP_BJOIN);
break;
default:
op = bshell_operator_get_by_token(tok->tok_operator);
break;
}
default:
break;
}
if (!op) {
report_error(ctx, "unknown binary operator");
return false;
}
if (op->op_precedence < expr->expr_minimum_precedence) {
expr->expr_done = true;
return true;
}
if (expr->expr_prev != EXPR_C_OPERAND) {
switch (op->op_id) {
case BSHELL_OP_PAREN:
break;
default:
report_error(
ctx,
"expected operand before binary "
"operator");
return false;
}
}
expr->expr_prev = EXPR_C_BINARY_OP;
struct bshell_op_ast_node *op_node
= (struct bshell_op_ast_node *)bshell_ast_node_create(
BSHELL_AST_OP);
if (!op_node) {
return false;
}
op_node->n_op = op;
discard_token(ctx);
arith_push_operator(expr, op_node);
return true;
}
static bool parse_call(struct bshell_parse_ctx *ctx, struct expr_bshell_parse_ctx *expr)
{
return false;
}
static bool parse_comma(struct bshell_parse_ctx *ctx, struct expr_bshell_parse_ctx *expr)
{
if (BSHELL_PRECEDENCE_ARRAY < expr->expr_minimum_precedence) {
expr->expr_done = true;
return true;
}
struct bshell_ast_node *item = NULL;
if (!finalise_expr(expr, &item, BSHELL_PRECEDENCE_ARRAY)) {
report_error(ctx, "failed to collect first array item.");
return false;
}
struct bshell_array_ast_node *array
= (struct bshell_array_ast_node *)bshell_ast_node_create(
BSHELL_AST_ARRAY);
if (!array) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_ast_node_destroy(item);
return false;
}
if (item) {
fx_queue_push_back(&array->n_items, &item->n_entry);
}
while (1) {
if (!parse_symbol(ctx, BSHELL_SYM_COMMA)) {
break;
}
if (!parse_arith_expr(
ctx,
BSHELL_PRECEDENCE_ARRAY + 1,
&item)) {
report_error(ctx, "failed to parse array item.");
bshell_ast_node_destroy(
(struct bshell_ast_node *)array);
return false;
}
fx_queue_push_back(&array->n_items, &item->n_entry);
}
fx_queue_push_back(&expr->expr_out_queue, &array->n_base.n_entry);
expr->expr_prev = EXPR_C_OPERAND;
return true;
}
static bool can_use_command(struct expr_bshell_parse_ctx *ctx)
{
switch (ctx->expr_prev_symbol) {
case BSHELL_TOK_NONE:
case BSHELL_SYM_EQUAL:
case BSHELL_SYM_PLUS_EQUAL:
case BSHELL_SYM_HYPHEN_EQUAL:
case BSHELL_SYM_ASTERISK_EQUAL:
case BSHELL_SYM_FORWARD_SLASH_EQUAL:
case BSHELL_SYM_PERCENT_EQUAL:
return true;
default:
return false;
}
}
bool parse_arith_expr(
struct bshell_parse_ctx *ctx,
enum bshell_operator_precedence minimum_precedence,
struct bshell_ast_node **out)
{
struct expr_bshell_parse_ctx expr = {
.expr_minimum_precedence = minimum_precedence,
};
while (!expr.expr_fail && !expr.expr_done) {
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
break;
}
switch (tok->tok_type) {
case BSHELL_TOK_LINEFEED:
expr.expr_prev_symbol = tok->tok_type;
expr.expr_done = true;
break;
case BSHELL_TOK_WORD: {
if (!can_use_command(&expr)) {
expr.expr_fail = !parse_operand(ctx, &expr);
break;
}
expr.expr_prev_symbol = tok->tok_type;
struct bshell_ast_node *value = NULL;
if (!parse_command(ctx, &value)) {
expr.expr_fail = true;
break;
}
fx_queue_push_back(
&expr.expr_out_queue,
&value->n_entry);
break;
}
case BSHELL_TOK_VAR:
case BSHELL_TOK_INT:
case BSHELL_TOK_DOUBLE:
case BSHELL_TOK_STRING:
case BSHELL_TOK_STR_START:
expr.expr_prev_symbol = tok->tok_type;
expr.expr_fail = !parse_operand(ctx, &expr);
expr.expr_prev_symbol = tok->tok_type;
break;
case BSHELL_TOK_OPERATOR:
expr.expr_prev_symbol = tok->tok_operator;
switch (tok->tok_operator) {
/* these two are special cases, as they are both
* unary AND binary operators */
case BSHELL_TKOP_SPLIT:
case BSHELL_TKOP_JOIN:
if (expr.expr_prev == EXPR_C_OPERAND) {
expr.expr_fail = !parse_binary_operator(
ctx,
&expr);
} else {
expr.expr_fail = !parse_unary_operator(
ctx,
&expr);
}
break;
case BSHELL_TKOP_BNOT:
case BSHELL_TKOP_NOT:
expr.expr_fail
= !parse_unary_operator(ctx, &expr);
break;
default:
expr.expr_fail
= !parse_binary_operator(ctx, &expr);
break;
}
expr.expr_prev_symbol = tok->tok_operator;
break;
case BSHELL_TOK_SYMBOL:
expr.expr_prev_symbol = tok->tok_symbol;
switch (tok->tok_symbol) {
case BSHELL_SYM_SEMICOLON:
case BSHELL_SYM_AMPERSAND:
case BSHELL_SYM_PIPE:
case BSHELL_SYM_RIGHT_PAREN:
case BSHELL_SYM_RIGHT_BRACE:
case BSHELL_SYM_RIGHT_BRACKET:
expr.expr_done = true;
break;
case BSHELL_SYM_COMMA:
expr.expr_fail = !parse_comma(ctx, &expr);
break;
case BSHELL_SYM_LEFT_PAREN: {
if (expr.expr_prev == EXPR_C_OPERAND) {
return parse_call(ctx, &expr);
}
struct bshell_ast_node *v = NULL;
expr.expr_fail = !parse_subexpr(ctx, &v);
if (expr.expr_fail) {
break;
}
fx_queue_push_back(
&expr.expr_out_queue,
&v->n_entry);
expr.expr_prev = EXPR_C_OPERAND;
break;
}
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
case BSHELL_SYM_AT_LEFT_PAREN:
case BSHELL_SYM_AT_LEFT_BRACE:
case BSHELL_SYM_LEFT_BRACE:
expr.expr_fail = !parse_operand(ctx, &expr);
break;
default: {
const struct bshell_operator_info *op
= bshell_operator_get_by_token(
tok->tok_symbol);
if (op->op_arity == BSHELL_OPA_BINARY) {
expr.expr_fail = !parse_binary_operator(
ctx,
&expr);
} else {
expr.expr_fail = !parse_unary_operator(
ctx,
&expr);
}
break;
}
}
break;
default:
report_error(
ctx,
"unexpected token in arithmetic "
"expression");
expr.expr_fail = true;
break;
}
}
if (expr.expr_fail) {
/* TODO cleanup */
return false;
}
struct bshell_ast_node *value = NULL;
if (!finalise_expr(&expr, &value, BSHELL_PRECEDENCE_ASSIGN)) {
report_error(ctx, "failed to convert expression to AST");
/* TODO cleanup */
return false;
}
if (BSHELL_PRECEDENCE_PIPELINE >= expr.expr_minimum_precedence) {
if (peek_symbol(ctx, BSHELL_SYM_PIPE)) {
return parse_pipeline(ctx, value, out);
}
}
*out = value;
return true;
}
+53
View File
@@ -0,0 +1,53 @@
#include "../syntax.h"
bool parse_block(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_symbol(ctx, BSHELL_SYM_LEFT_BRACE)) {
return false;
}
parse_linefeed(ctx);
struct bshell_block_ast_node *block
= (struct bshell_block_ast_node *)bshell_ast_node_create(
BSHELL_AST_BLOCK);
bool ok = true;
while (1) {
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) {
break;
}
parse_linefeed(ctx);
struct bshell_ast_node *stmt = NULL;
if (!parse_statement(ctx, &stmt)) {
ok = false;
break;
}
fx_queue_push_back(&block->n_statements, &stmt->n_entry);
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_BRACE)) {
break;
}
if (!parse_linefeed(ctx)
&& !parse_symbol(ctx, BSHELL_SYM_SEMICOLON)) {
report_error(
ctx,
"expected `;`, `}`, or linefeed after "
"statement");
ok = false;
break;
}
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)block);
block = NULL;
}
*out = (struct bshell_ast_node *)block;
return true;
}
+523
View File
@@ -0,0 +1,523 @@
#include "../syntax.h"
#include <fx/encoding.h>
static bool parse_fword(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (peek_token_type(ctx) != BSHELL_TOK_WORD_START) {
return false;
}
discard_token(ctx);
struct bshell_fstring_ast_node *fstring
= (struct bshell_fstring_ast_node *)bshell_ast_node_create(
BSHELL_AST_FSTRING);
if (!fstring) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
bool ok = true;
while (ok) {
if (peek_token_type(ctx) == BSHELL_TOK_WORD_END) {
discard_token(ctx);
break;
}
struct bshell_ast_node *item = NULL;
if (peek_token_type(ctx) == BSHELL_TOK_WORD) {
struct bshell_word_ast_node *n
= (struct bshell_word_ast_node *)
bshell_ast_node_create(BSHELL_AST_WORD);
if (!n) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
ok = false;
break;
}
n->n_value = claim_token(ctx);
item = (struct bshell_ast_node *)n;
} else {
if (!parse_arith_value(ctx, &item)) {
ok = false;
break;
}
}
fx_queue_push_back(&fstring->n_elements, &item->n_entry);
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)fstring);
fstring = NULL;
}
*out = (struct bshell_ast_node *)fstring;
return ok;
return false;
}
static bool parse_cmdcall_arg(
struct bshell_parse_ctx *ctx,
struct bshell_ast_node **out)
{
if (ctx->p_status != BSHELL_SUCCESS) {
return false;
}
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
struct bshell_ast_node *arg = NULL;
switch (tok->tok_type) {
case BSHELL_TOK_WORD_START:
return parse_fword(ctx, out);
case BSHELL_TOK_STR_START:
return parse_fstring(ctx, out);
case BSHELL_TOK_WORD: {
struct bshell_word_ast_node *n
= (struct bshell_word_ast_node *)bshell_ast_node_create(
BSHELL_AST_WORD);
if (!n) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
n->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)n;
return true;
}
case BSHELL_TOK_VAR: {
struct bshell_var_ast_node *n
= (struct bshell_var_ast_node *)bshell_ast_node_create(
BSHELL_AST_VAR);
if (!n) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
n->n_ident = claim_token(ctx);
*out = (struct bshell_ast_node *)n;
return true;
}
case BSHELL_TOK_VAR_SPLAT: {
struct bshell_var_splat_ast_node *n
= (struct bshell_var_splat_ast_node *)
bshell_ast_node_create(BSHELL_AST_VAR_SPLAT);
if (!n) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
n->n_ident = claim_token(ctx);
*out = (struct bshell_ast_node *)n;
return true;
}
case BSHELL_TOK_STRING: {
struct bshell_string_ast_node *n
= (struct bshell_string_ast_node *)
bshell_ast_node_create(BSHELL_AST_STRING);
if (!n) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
n->n_value = claim_token(ctx);
*out = (struct bshell_ast_node *)n;
return true;
}
case BSHELL_TOK_SYMBOL:
switch (tok->tok_symbol) {
case BSHELL_SYM_LEFT_PAREN:
case BSHELL_SYM_LEFT_BRACE:
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
case BSHELL_SYM_AT_LEFT_BRACE:
case BSHELL_SYM_AT_LEFT_PAREN:
return parse_arith_value(ctx, out);
default:
report_error(
ctx,
"encountered unsupported command arg");
return false;
}
break;
default:
report_error(ctx, "encountered unsupported command arg");
return false;
}
return true;
}
static bool parse_redirect_to_fd(
struct bshell_parse_ctx *ctx,
unsigned int in_fd,
bool append,
struct bshell_ast_node **out)
{
if (ctx->p_status != BSHELL_SUCCESS) {
return false;
}
struct bshell_redirection_ast_node *redirect
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
BSHELL_AST_REDIRECTION);
redirect->n_in = in_fd;
redirect->n_append = append;
if (!parse_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
bshell_ast_node_destroy((struct bshell_ast_node *)redirect);
return false;
}
struct bshell_lex_token *out_tok = NULL;
struct bshell_ast_node *out_expr = NULL;
long long out_fd = -1;
if (peek_word(ctx, &out_tok)) {
const char *s = out_tok->tok_str;
char *ep;
out_fd = strtoll(s, &ep, 10);
if (*ep == '\0') {
discard_token(ctx);
out_tok = NULL;
} else {
out_fd = -1;
}
} else if (!parse_cmdcall_arg(ctx, &out_expr)) {
return false;
}
redirect->n_out_is_fd = (out_fd >= 0) || out_expr;
redirect->n_out_is_expr = out_expr != NULL;
redirect->n_out = (unsigned int)out_fd;
redirect->n_out_path_expr = out_expr;
if (out_tok) {
redirect->n_out_tok = claim_token(ctx);
redirect->n_out_path = out_tok->tok_str;
}
*out = (struct bshell_ast_node *)redirect;
return true;
}
static bool parse_redirect_to_file_squashed(
struct bshell_parse_ctx *ctx,
unsigned int in_fd,
bool append,
const char *str,
struct bshell_ast_node **out)
{
if (ctx->p_status != BSHELL_SUCCESS) {
return false;
}
struct bshell_lex_token *tok = peek_token(ctx);
if (*str == '\0') {
return false;
}
struct bshell_redirection_ast_node *redirect
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
BSHELL_AST_REDIRECTION);
redirect->n_in = in_fd;
redirect->n_append = append;
redirect->n_out_is_fd = false;
redirect->n_out_is_expr = false;
redirect->n_out_path = str;
redirect->n_out_tok = claim_token(ctx);
*out = (struct bshell_ast_node *)redirect;
return true;
}
static bool parse_redirect_to_file_separate(
struct bshell_parse_ctx *ctx,
unsigned int in_fd,
bool append,
struct bshell_ast_node **out)
{
if (ctx->p_status != BSHELL_SUCCESS) {
return false;
}
struct bshell_ast_node *out_path = NULL;
if (!parse_cmdcall_arg(ctx, &out_path)) {
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
return false;
}
struct bshell_redirection_ast_node *redirect
= (struct bshell_redirection_ast_node *)bshell_ast_node_create(
BSHELL_AST_REDIRECTION);
redirect->n_in = in_fd;
redirect->n_append = append;
redirect->n_out_is_fd = false;
redirect->n_out_is_expr = true;
redirect->n_out_path_expr = out_path;
*out = (struct bshell_ast_node *)redirect;
return true;
}
bool parse_redirect(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok || tok->tok_type != BSHELL_TOK_WORD) {
return false;
}
unsigned int in_fd = 1;
const char *str = tok->tok_str;
bool append = false;
if (fx_wchar_is_number(*str)) {
in_fd = 0;
while (fx_wchar_is_number(*str)) {
in_fd *= 10;
in_fd += *str - '0';
str++;
}
}
if (*str != '>') {
return false;
}
str++;
if (*str == '>') {
append = true;
str++;
}
if (*str != '\0') {
return parse_redirect_to_file_squashed(
ctx,
in_fd,
append,
str,
out);
}
discard_token(ctx);
if (parse_redirect_to_fd(ctx, in_fd, append, out)) {
return true;
}
if (parse_redirect_to_file_separate(ctx, in_fd, append, out)) {
return true;
}
return false;
}
static bool peek_cmdcall_item(struct bshell_parse_ctx *ctx, bool unrestricted)
{
/* each token type falls into one of three categories:
* - cmdcall item: the token can be used as part of a command call. the
* token indicates the start of a command call.
* - NOT a cmdcall item: the token cannot be used as part of a command
* call, usually because it as a cmdcall operator like | or &.
* encountering one of these tokens ends the cmdcall currently being
* parsed.
* - RESTRICTED cmdcall item: the token can be used as part of a
* command, but will not be considered the start of a cmdcall. to run
* a command with this token as its name, the call operator must be
* used.
*/
switch (peek_token_type(ctx)) {
case BSHELL_TOK_KEYWORD:
case BSHELL_TOK_INT:
case BSHELL_TOK_DOUBLE:
case BSHELL_TOK_VAR:
case BSHELL_TOK_VAR_SPLAT:
case BSHELL_TOK_STRING:
case BSHELL_TOK_WORD_START:
return unrestricted;
case BSHELL_TOK_SYMBOL:
switch (peek_unknown_symbol(ctx)) {
case BSHELL_SYM_PLUS:
case BSHELL_SYM_HYPHEN:
return unrestricted;
case BSHELL_SYM_PIPE:
case BSHELL_SYM_AMPERSAND:
case BSHELL_SYM_SEMICOLON:
case BSHELL_SYM_RIGHT_PAREN:
case BSHELL_SYM_RIGHT_BRACE:
case BSHELL_SYM_RIGHT_BRACKET:
return false;
default:
return true;
}
case BSHELL_TOK_NONE:
case BSHELL_TOK_LINEFEED:
return false;
default:
return true;
}
}
bool parse_cmdcall(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
struct bshell_cmdcall_ast_node *node
= (struct bshell_cmdcall_ast_node *)bshell_ast_node_create(
BSHELL_AST_CMDCALL);
if (!node) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
return false;
}
struct bshell_ast_node *child = NULL;
bool unrestricted = false;
bool ok = true;
bool stop = false;
if (parse_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
unrestricted = true;
}
if (!peek_cmdcall_item(ctx, unrestricted)) {
return false;
}
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (!parse_cmdcall_arg(ctx, &child)) {
return false;
}
fx_queue_push_back(&node->n_args, &child->n_entry);
while (ok && !stop) {
if (!peek_cmdcall_item(ctx, true)) {
break;
}
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
break;
}
if (parse_redirect(ctx, &child)) {
fx_queue_push_back(&node->n_redirect, &child->n_entry);
} else if (parse_cmdcall_arg(ctx, &child)) {
fx_queue_push_back(&node->n_args, &child->n_entry);
} else {
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
ok = false;
break;
}
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)node);
node = NULL;
}
*out = (struct bshell_ast_node *)node;
return ok;
}
bool peek_command(struct bshell_parse_ctx *ctx)
{
if (peek_symbol(ctx, BSHELL_SYM_AMPERSAND)) {
return true;
}
return peek_cmdcall_item(ctx, false);
}
bool parse_command(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
struct bshell_ast_node *cmdcall = NULL;
if (!parse_cmdcall(ctx, &cmdcall)) {
return false;
}
struct bshell_pipeline_ast_node *pipeline = NULL;
while (1) {
if (peek_symbol(ctx, BSHELL_SYM_SEMICOLON)
|| peek_linefeed(ctx)) {
break;
}
if (!parse_symbol(ctx, BSHELL_SYM_PIPE)) {
break;
}
if (!pipeline) {
pipeline = (struct bshell_pipeline_ast_node *)
bshell_ast_node_create(BSHELL_AST_PIPELINE);
if (!pipeline) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_ast_node_destroy(cmdcall);
return false;
}
fx_queue_push_back(
&pipeline->n_stages,
&cmdcall->n_entry);
}
if (!parse_cmdcall(ctx, &cmdcall)) {
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
return false;
}
fx_queue_push_back(&pipeline->n_stages, &cmdcall->n_entry);
}
if (pipeline) {
*out = (struct bshell_ast_node *)pipeline;
} else {
*out = cmdcall;
}
return true;
}
bool parse_pipeline(
struct bshell_parse_ctx *ctx,
struct bshell_ast_node *first_item,
struct bshell_ast_node **out)
{
struct bshell_pipeline_ast_node *pipeline
= (struct bshell_pipeline_ast_node *)bshell_ast_node_create(
BSHELL_AST_PIPELINE);
fx_queue_push_back(&pipeline->n_stages, &first_item->n_entry);
while (1) {
if (!parse_symbol(ctx, BSHELL_SYM_PIPE)) {
break;
}
struct bshell_ast_node *cmdcall = NULL;
if (!parse_cmdcall(ctx, &cmdcall)) {
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
return false;
}
fx_queue_push_back(&pipeline->n_stages, &cmdcall->n_entry);
}
*out = (struct bshell_ast_node *)pipeline;
return true;
}
+15
View File
@@ -0,0 +1,15 @@
#include "../syntax.h"
bool parse_expr(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
bool ok = false;
if (!ok && peek_arith_expr(ctx)) {
ok = parse_arith_expr(ctx, BSHELL_PRECEDENCE_MINIMUM, out);
}
if (!ok && peek_command(ctx)) {
ok = parse_command(ctx, out);
}
return ok;
}
+88
View File
@@ -0,0 +1,88 @@
#include "../syntax.h"
bool parse_func(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_keyword(ctx, BSHELL_KW_FUNC)) {
return false;
}
struct bshell_lex_token *name = NULL;
if (!parse_word(ctx, &name)) {
report_error(ctx, "expected function identifier");
return false;
}
struct bshell_func_ast_node *func
= (struct bshell_func_ast_node *)bshell_ast_node_create(
BSHELL_AST_FUNC);
if (!func) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_lex_token_destroy(name);
return false;
}
func->n_name = name;
if (!parse_symbol(ctx, BSHELL_SYM_LEFT_PAREN)) {
report_error(ctx, "expected `(` after function identifier");
bshell_ast_node_destroy((struct bshell_ast_node *)func);
return false;
}
size_t nr_args = 0;
bool ok = true;
while (1) {
if (parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
break;
}
if (nr_args > 0 && !parse_symbol(ctx, BSHELL_SYM_COMMA)) {
report_error(
ctx,
"expected `,` or `)` after parameter name");
ok = false;
break;
}
struct bshell_lex_token *param_token = NULL;
struct bshell_var_ast_node *param_node = NULL;
if (!parse_var(ctx, &param_token)) {
report_error(ctx, "expected parameter variable");
ok = false;
break;
}
param_node
= (struct bshell_var_ast_node *)bshell_ast_node_create(
BSHELL_AST_VAR);
if (!param_node) {
ok = false;
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_lex_token_destroy(param_token);
break;
}
param_node->n_ident = param_token;
fx_queue_push_back(
&func->n_params,
&param_node->n_base.n_entry);
}
if (!ok) {
if (ctx->p_status == BSHELL_SUCCESS) {
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
}
bshell_ast_node_destroy((struct bshell_ast_node *)func);
return false;
}
if (!parse_block(ctx, &func->n_body)) {
report_error(ctx, "failed to parse function body");
bshell_ast_node_destroy((struct bshell_ast_node *)func);
return false;
}
*out = (struct bshell_ast_node *)func;
return true;
}
+158
View File
@@ -0,0 +1,158 @@
#include "../syntax.h"
#include <bshell/parse/lex.h>
#include <bshell/parse/parse.h>
#include <bshell/parse/token.h>
struct bshell_lex_token *claim_token(struct bshell_parse_ctx *ctx)
{
return bshell_lex_ctx_claim(ctx->p_src);
}
void discard_token(struct bshell_parse_ctx *ctx)
{
return bshell_lex_ctx_discard(ctx->p_src);
}
struct bshell_lex_token *peek_token(struct bshell_parse_ctx *ctx)
{
return bshell_lex_ctx_peek(ctx->p_src);
}
enum bshell_lex_token_type peek_token_type(struct bshell_parse_ctx *ctx)
{
struct bshell_lex_token *tok = peek_token(ctx);
return tok ? tok->tok_type : BSHELL_TOK_NONE;
}
enum bshell_lex_symbol peek_unknown_symbol(struct bshell_parse_ctx *ctx)
{
struct bshell_lex_token *tok = peek_token(ctx);
return (tok && tok->tok_type == BSHELL_TOK_SYMBOL) ? tok->tok_symbol
: BSHELL_SYM_NONE;
}
enum bshell_lex_keyword peek_unknown_keyword(struct bshell_parse_ctx *ctx)
{
struct bshell_lex_token *tok = peek_token(ctx);
return (tok && tok->tok_type == BSHELL_TOK_KEYWORD) ? tok->tok_keyword
: BSHELL_KW_NONE;
}
bool peek_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (tok && tok->tok_type == BSHELL_TOK_WORD) {
*out = tok;
return true;
}
return false;
}
bool peek_linefeed(struct bshell_parse_ctx *ctx)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (tok && tok->tok_type == BSHELL_TOK_LINEFEED) {
return true;
}
return false;
}
bool peek_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (tok->tok_type != BSHELL_TOK_SYMBOL) {
return false;
}
if (tok->tok_symbol != sym) {
return false;
}
return true;
}
bool parse_linefeed(struct bshell_parse_ctx *ctx)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (tok && tok->tok_type == BSHELL_TOK_LINEFEED) {
discard_token(ctx);
return true;
}
return false;
}
bool parse_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (tok->tok_type != BSHELL_TOK_SYMBOL) {
return false;
}
if (tok->tok_symbol != sym) {
return false;
}
discard_token(ctx);
return true;
}
bool parse_keyword(struct bshell_parse_ctx *ctx, enum bshell_lex_keyword kw)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (tok->tok_type != BSHELL_TOK_KEYWORD) {
return false;
}
if (tok->tok_keyword != kw) {
return false;
}
discard_token(ctx);
return true;
}
bool parse_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (tok->tok_type != BSHELL_TOK_WORD) {
return false;
}
*out = claim_token(ctx);
return true;
}
bool parse_var(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
{
struct bshell_lex_token *tok = peek_token(ctx);
if (!tok) {
return false;
}
if (tok->tok_type != BSHELL_TOK_VAR) {
return false;
}
*out = claim_token(ctx);
return true;
}
+130
View File
@@ -0,0 +1,130 @@
#include "../syntax.h"
static bool add_branch(
struct bshell_if_ast_node *group,
struct bshell_ast_node *cond,
struct bshell_ast_node *body)
{
struct bshell_if_branch_ast_node *branch
= (struct bshell_if_branch_ast_node *)bshell_ast_node_create(
BSHELL_AST_IF_BRANCH);
if (!branch) {
return false;
}
branch->n_cond = cond;
branch->n_body = body;
fx_queue_push_back(&group->n_branches, &branch->n_base.n_entry);
return true;
}
bool parse_if(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!parse_keyword(ctx, BSHELL_KW_IF)) {
return false;
}
if (!parse_symbol(ctx, BSHELL_SYM_LEFT_PAREN)) {
report_error(ctx, "expected `(` after `if`");
return false;
}
struct bshell_ast_node *if_cond = NULL, *if_body = NULL;
if (!parse_expr(ctx, &if_cond)) {
report_error(ctx, "invalid if condition");
return false;
}
if (!parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
report_error(ctx, "expected `)` after if-condition");
bshell_ast_node_destroy(if_cond);
return false;
}
if (!parse_block(ctx, &if_body)) {
report_error(ctx, "invalid if body");
bshell_ast_node_destroy(if_cond);
return false;
}
struct bshell_if_ast_node *if_group
= (struct bshell_if_ast_node *)bshell_ast_node_create(
BSHELL_AST_IF);
if (!if_group) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_ast_node_destroy(if_cond);
bshell_ast_node_destroy(if_body);
return false;
}
if (!add_branch(if_group, if_cond, if_body)) {
ctx->p_status = BSHELL_ERR_NO_MEMORY;
bshell_ast_node_destroy(if_cond);
bshell_ast_node_destroy(if_body);
bshell_ast_node_destroy((struct bshell_ast_node *)if_group);
return false;
}
bool done = false;
while (!done) {
struct bshell_ast_node *cond = NULL, *body = NULL;
if (parse_keyword(ctx, BSHELL_KW_ELSE)) {
done = true;
} else if (parse_keyword(ctx, BSHELL_KW_ELSEIF)) {
if (!parse_symbol(ctx, BSHELL_SYM_LEFT_PAREN)) {
report_error(
ctx,
"expected `(` after `elseif`");
return false;
}
if (!parse_expr(ctx, &cond)) {
report_error(
ctx,
"invalid conditional expression");
bshell_ast_node_destroy(
(struct bshell_ast_node *)if_group);
return false;
}
if (!parse_symbol(ctx, BSHELL_SYM_RIGHT_PAREN)) {
report_error(
ctx,
"expected `)` after elseif-condition");
bshell_ast_node_destroy(if_cond);
return false;
}
} else {
done = true;
break;
}
if (!parse_block(ctx, &body)) {
report_error(ctx, "invalid conditional body");
if (cond) {
bshell_ast_node_destroy(cond);
}
bshell_ast_node_destroy(
(struct bshell_ast_node *)if_group);
return false;
}
if (!add_branch(if_group, cond, body)) {
report_error(ctx, "failed to add branch to if-group");
if (cond) {
bshell_ast_node_destroy(cond);
}
bshell_ast_node_destroy(body);
bshell_ast_node_destroy(
(struct bshell_ast_node *)if_group);
return false;
}
}
*out = (struct bshell_ast_node *)if_group;
return true;
}
+21
View File
@@ -0,0 +1,21 @@
#include "../syntax.h"
bool peek_keyword_expr(struct bshell_parse_ctx *ctx)
{
return peek_unknown_keyword(ctx) != BSHELL_KW_NONE;
}
bool parse_keyword_expr(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
switch (peek_unknown_keyword(ctx)) {
case BSHELL_KW_NONE:
return false;
case BSHELL_KW_IF:
return parse_if(ctx, out);
case BSHELL_KW_FUNC:
return parse_func(ctx, out);
default:
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
return false;
}
}
+100
View File
@@ -0,0 +1,100 @@
#include "../syntax.h"
bool peek_statement(struct bshell_parse_ctx *ctx)
{
if (peek_keyword_expr(ctx)) {
return true;
}
if (peek_arith_expr(ctx)) {
return true;
}
if (peek_command(ctx)) {
return true;
}
return false;
}
bool parse_statement(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
if (!peek_token(ctx)) {
/* error, or EOF */
return false;
}
bool unknown = true;
bool ok = false;
if (peek_keyword_expr(ctx)) {
unknown = false;
ok = parse_keyword_expr(ctx, out);
}
if (!ok && peek_arith_expr(ctx)) {
unknown = false;
ok = parse_arith_expr(ctx, BSHELL_PRECEDENCE_MINIMUM, out);
}
if (!ok && peek_command(ctx)) {
unknown = false;
ok = parse_command(ctx, out);
}
if (!ok && unknown) {
report_error(
ctx,
"encountered unknown token while parsing statement");
return false;
}
return ok;
}
static struct bshell_ast_node *convert_single_statement(
struct bshell_stmt_list_ast_node *list)
{
fx_queue_entry *first_entry = fx_queue_first(&list->n_statements);
if (!first_entry || fx_queue_next(first_entry)) {
return (struct bshell_ast_node *)list;
}
fx_queue_delete(&list->n_statements, first_entry);
struct bshell_ast_node *first
= fx_unbox(struct bshell_ast_node, first_entry, n_entry);
bshell_ast_node_destroy((struct bshell_ast_node *)list);
return first;
}
bool parse_statement_list(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out)
{
struct bshell_stmt_list_ast_node *stmt_list
= (struct bshell_stmt_list_ast_node *)bshell_ast_node_create(
BSHELL_AST_STMT_LIST);
bool ok = true;
while (ok) {
parse_linefeed(ctx);
struct bshell_ast_node *stmt = NULL;
if (!parse_statement(ctx, &stmt)) {
ok = false;
break;
}
fx_queue_push_back(&stmt_list->n_statements, &stmt->n_entry);
if (!parse_symbol(ctx, BSHELL_SYM_SEMICOLON)) {
break;
}
}
if (!ok) {
bshell_ast_node_destroy((struct bshell_ast_node *)stmt_list);
return false;
}
*out = convert_single_statement(stmt_list);
return true;
}