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:
@@ -0,0 +1,22 @@
|
||||
set(source_dirs
|
||||
ast compile parse parse/lex parse/syntax
|
||||
format runtime command)
|
||||
|
||||
file(GLOB sources
|
||||
*.c
|
||||
*.h)
|
||||
|
||||
foreach (d ${source_dirs})
|
||||
file(GLOB d_sources
|
||||
${d}/*.c
|
||||
${d}/*.h)
|
||||
set(sources ${sources} ${d_sources})
|
||||
endforeach (d)
|
||||
|
||||
message(STATUS ${sources})
|
||||
add_library(bshell.runtime SHARED ${sources})
|
||||
|
||||
target_link_libraries(bshell.runtime FX::Runtime FX::Collections FX::Term)
|
||||
target_include_directories(bshell.runtime PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
target_compile_definitions(bshell.runtime PUBLIC
|
||||
BSHELL_VERSION="${bshell_version}")
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
|
||||
FX_ASSEMBLY_BEGIN(bshell_runtime)
|
||||
FX_ASSEMBLY_NAME("bshell.runtime");
|
||||
FX_ASSEMBLY_VERSION(0, 1, 0, 0);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("bshell.runtime", "command", bshell_command);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("bshell.runtime", "alias", bshell_alias);
|
||||
// FX_ASSEMBLY_EXPORT_TYPE("bshell.runtime", "function",
|
||||
// bshell_function);
|
||||
FX_ASSEMBLY_EXPORT_TYPE("bshell.runtime", "cmdlet", bshell_cmdlet);
|
||||
FX_ASSEMBLY_EXPORT_TYPE(
|
||||
"bshell.runtime",
|
||||
"scriptblock",
|
||||
bshell_scriptblock);
|
||||
FX_ASSEMBLY_END(bshell_runtime)
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_array_ast_node *array = (struct bshell_array_ast_node *)
|
||||
node;
|
||||
|
||||
fx_queue_entry *cur = fx_queue_first(&array->n_items);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition array_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_ARRAY,
|
||||
.def_node_size = sizeof(struct bshell_array_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,277 @@
|
||||
#include <assert.h>
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/status.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern struct bshell_ast_node_definition null_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition int_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition double_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition word_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition var_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition string_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition fstring_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition cmdcall_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition pipeline_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition redirection_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition block_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition stmt_list_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition func_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition array_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition hashtable_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition hashtable_item_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition if_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition if_branch_bshell_ast_node;
|
||||
extern struct bshell_ast_node_definition op_bshell_ast_node;
|
||||
|
||||
static const struct bshell_ast_node_definition *bshell_ast_node_definitions[]
|
||||
= {
|
||||
[BSHELL_AST_NULL] = &null_bshell_ast_node,
|
||||
[BSHELL_AST_INT] = &int_bshell_ast_node,
|
||||
[BSHELL_AST_DOUBLE] = &double_bshell_ast_node,
|
||||
[BSHELL_AST_WORD] = &word_bshell_ast_node,
|
||||
[BSHELL_AST_VAR] = &var_bshell_ast_node,
|
||||
[BSHELL_AST_STRING] = &string_bshell_ast_node,
|
||||
[BSHELL_AST_FSTRING] = &fstring_bshell_ast_node,
|
||||
[BSHELL_AST_CMDCALL] = &cmdcall_bshell_ast_node,
|
||||
[BSHELL_AST_PIPELINE] = &pipeline_bshell_ast_node,
|
||||
[BSHELL_AST_REDIRECTION] = &redirection_bshell_ast_node,
|
||||
[BSHELL_AST_BLOCK] = &block_bshell_ast_node,
|
||||
[BSHELL_AST_STMT_LIST] = &stmt_list_bshell_ast_node,
|
||||
[BSHELL_AST_FUNC] = &func_bshell_ast_node,
|
||||
[BSHELL_AST_ARRAY] = &array_bshell_ast_node,
|
||||
[BSHELL_AST_IF] = &if_bshell_ast_node,
|
||||
[BSHELL_AST_IF_BRANCH] = &if_branch_bshell_ast_node,
|
||||
[BSHELL_AST_HASHTABLE] = &hashtable_bshell_ast_node,
|
||||
[BSHELL_AST_HASHTABLE_ITEM] = &hashtable_item_bshell_ast_node,
|
||||
[BSHELL_AST_OP] = &op_bshell_ast_node,
|
||||
};
|
||||
static const size_t nr_bshell_ast_node_definitions
|
||||
= sizeof bshell_ast_node_definitions
|
||||
/ sizeof bshell_ast_node_definitions[0];
|
||||
|
||||
struct bshell_ast_node *bshell_ast_node_create(enum bshell_ast_node_type type)
|
||||
{
|
||||
assert(type < nr_bshell_ast_node_definitions);
|
||||
|
||||
const struct bshell_ast_node_definition *def
|
||||
= bshell_ast_node_definitions[type];
|
||||
struct bshell_ast_node *out = malloc(def->def_node_size);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, def->def_node_size);
|
||||
|
||||
out->n_type = type;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void bshell_ast_node_destroy(struct bshell_ast_node *node)
|
||||
{
|
||||
assert(node->n_type < nr_bshell_ast_node_definitions);
|
||||
|
||||
struct bshell_ast_iterator it = {0};
|
||||
bshell_ast_iterator_enqueue(&it, node);
|
||||
|
||||
while (1) {
|
||||
node = bshell_ast_iterator_peek(&it);
|
||||
if (!node) {
|
||||
break;
|
||||
}
|
||||
|
||||
const struct bshell_ast_node_definition *def
|
||||
= bshell_ast_node_definitions[node->n_type];
|
||||
|
||||
if (def->def_cleanup) {
|
||||
def->def_cleanup(node);
|
||||
}
|
||||
|
||||
bshell_ast_iterator_dequeue(&it);
|
||||
free(node);
|
||||
}
|
||||
}
|
||||
|
||||
void bshell_ast_node_to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_ast_node_definition *def
|
||||
= bshell_ast_node_definitions[node->n_type];
|
||||
if (def->def_to_string) {
|
||||
def->def_to_string(node, out);
|
||||
}
|
||||
}
|
||||
|
||||
#define ENUM_STR(x) \
|
||||
case x: \
|
||||
return #x
|
||||
|
||||
const char *bshell_ast_node_type_to_string(enum bshell_ast_node_type type)
|
||||
{
|
||||
switch (type) {
|
||||
ENUM_STR(BSHELL_AST_NONE);
|
||||
ENUM_STR(BSHELL_AST_NULL);
|
||||
ENUM_STR(BSHELL_AST_STMT_LIST);
|
||||
ENUM_STR(BSHELL_AST_INT);
|
||||
ENUM_STR(BSHELL_AST_DOUBLE);
|
||||
ENUM_STR(BSHELL_AST_WORD);
|
||||
ENUM_STR(BSHELL_AST_STRING);
|
||||
ENUM_STR(BSHELL_AST_FSTRING);
|
||||
ENUM_STR(BSHELL_AST_VAR);
|
||||
ENUM_STR(BSHELL_AST_VAR_SPLAT);
|
||||
ENUM_STR(BSHELL_AST_FLAG);
|
||||
ENUM_STR(BSHELL_AST_CMDCALL);
|
||||
ENUM_STR(BSHELL_AST_PIPELINE);
|
||||
ENUM_STR(BSHELL_AST_REDIRECTION);
|
||||
ENUM_STR(BSHELL_AST_BLOCK);
|
||||
ENUM_STR(BSHELL_AST_FUNC);
|
||||
ENUM_STR(BSHELL_AST_IF);
|
||||
ENUM_STR(BSHELL_AST_IF_BRANCH);
|
||||
ENUM_STR(BSHELL_AST_OP);
|
||||
ENUM_STR(BSHELL_AST_ARRAY);
|
||||
ENUM_STR(BSHELL_AST_HASHTABLE);
|
||||
ENUM_STR(BSHELL_AST_HASHTABLE_ITEM);
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
struct bshell_ast_node *bshell_ast_iterator_peek(struct bshell_ast_iterator *it)
|
||||
{
|
||||
fx_queue_entry *cur = fx_queue_first(&it->it_queue);
|
||||
if (!cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_unbox(struct bshell_ast_node, cur, n_it.e_entry);
|
||||
}
|
||||
|
||||
struct bshell_ast_node *bshell_ast_iterator_dequeue(
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
fx_queue_entry *cur = fx_queue_first(&it->it_queue);
|
||||
if (!cur) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct bshell_ast_node *node = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_it.e_entry);
|
||||
const struct bshell_ast_node_definition *def
|
||||
= bshell_ast_node_definitions[node->n_type];
|
||||
|
||||
it->it_insert_after = cur;
|
||||
if (def->def_collect_children) {
|
||||
def->def_collect_children(node, it);
|
||||
}
|
||||
|
||||
fx_queue_pop_front(&it->it_queue);
|
||||
return fx_unbox(struct bshell_ast_node, cur, n_it.e_entry);
|
||||
}
|
||||
|
||||
void bshell_ast_iterator_enqueue(
|
||||
struct bshell_ast_iterator *it,
|
||||
struct bshell_ast_node *node)
|
||||
{
|
||||
unsigned long new_depth = 0;
|
||||
|
||||
fx_queue_entry *cur = fx_queue_first(&it->it_queue);
|
||||
if (cur) {
|
||||
struct bshell_ast_node *cur_node = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_it.e_entry);
|
||||
new_depth = cur_node->n_it.e_depth + 1;
|
||||
}
|
||||
|
||||
node->n_it.e_depth = new_depth;
|
||||
|
||||
if (!it->it_insert_after) {
|
||||
fx_queue_push_back(&it->it_queue, &node->n_it.e_entry);
|
||||
return;
|
||||
}
|
||||
|
||||
fx_queue_insert_after(
|
||||
&it->it_queue,
|
||||
&node->n_it.e_entry,
|
||||
it->it_insert_after);
|
||||
it->it_insert_after = &node->n_it.e_entry;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_ast_node_iterate(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it,
|
||||
bshell_ast_iterator_callback callback,
|
||||
void *arg)
|
||||
{
|
||||
fx_queue_push_back(&it->it_queue, &node->n_it.e_entry);
|
||||
node->n_it.e_depth = 0;
|
||||
|
||||
fx_queue_entry *entry = fx_queue_first(&it->it_queue);
|
||||
|
||||
while (entry) {
|
||||
struct bshell_ast_iterator_entry *it_entry = fx_unbox(
|
||||
struct bshell_ast_iterator_entry,
|
||||
entry,
|
||||
e_entry);
|
||||
node = fx_unbox(struct bshell_ast_node, it_entry, n_it);
|
||||
|
||||
if (!node) {
|
||||
/* this should never happen. */
|
||||
return BSHELL_ERR_INTERNAL_FAILURE;
|
||||
}
|
||||
|
||||
struct bshell_ast_iterate_result result = callback(
|
||||
node,
|
||||
BSHELL_AST_ITERATION_PRE,
|
||||
it,
|
||||
arg);
|
||||
if (result.r_status != BSHELL_SUCCESS
|
||||
|| result.r_flags & BSHELL_AST_ITERATE_STOP) {
|
||||
return result.r_status;
|
||||
}
|
||||
|
||||
const struct bshell_ast_node_definition *type
|
||||
= bshell_ast_node_definitions[node->n_type];
|
||||
if (type->def_collect_children
|
||||
&& result.r_flags & BSHELL_AST_ITERATE_ADD_CHILDREN) {
|
||||
it->it_insert_after = entry;
|
||||
type->def_collect_children(node, it);
|
||||
}
|
||||
|
||||
if (!(result.r_flags & BSHELL_AST_ITERATE_REPEAT)) {
|
||||
entry = fx_queue_next(entry);
|
||||
}
|
||||
}
|
||||
|
||||
while (!fx_queue_empty(&it->it_queue)) {
|
||||
fx_queue_entry *entry = fx_queue_last(&it->it_queue);
|
||||
if (!entry) {
|
||||
break;
|
||||
}
|
||||
|
||||
node = fx_unbox(struct bshell_ast_node, entry, n_it);
|
||||
|
||||
if (!node) {
|
||||
/* this should never happen. */
|
||||
return BSHELL_ERR_INTERNAL_FAILURE;
|
||||
}
|
||||
|
||||
struct bshell_ast_iterate_result result = callback(
|
||||
node,
|
||||
BSHELL_AST_ITERATION_POST,
|
||||
it,
|
||||
arg);
|
||||
if (result.r_status != BSHELL_SUCCESS
|
||||
|| result.r_flags & BSHELL_AST_ITERATE_STOP) {
|
||||
return result.r_status;
|
||||
}
|
||||
|
||||
if (!(result.r_flags & BSHELL_AST_ITERATE_REPEAT)) {
|
||||
fx_queue_pop_back(&it->it_queue);
|
||||
}
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_block_ast_node *block = (struct bshell_block_ast_node *)
|
||||
node;
|
||||
fx_queue_entry *cur = fx_queue_first(&block->n_statements);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition block_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_BLOCK,
|
||||
.def_node_size = sizeof(struct bshell_block_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_cmdcall_ast_node *cmdcall = (struct
|
||||
bshell_cmdcall_ast_node *)
|
||||
node;
|
||||
fx_queue_entry *cur = fx_queue_first(&cmdcall->n_args);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
cur = fx_queue_first(&cmdcall->n_redirect);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition cmdcall_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_CMDCALL,
|
||||
.def_node_size = sizeof(struct bshell_cmdcall_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <fx/stream.h>
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
struct bshell_double_ast_node *i = (struct bshell_double_ast_node *)
|
||||
node;
|
||||
fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition double_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_DOUBLE,
|
||||
.def_node_size = sizeof(struct bshell_double_ast_node),
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_fstring_ast_node *fstring;
|
||||
fstring = (struct bshell_fstring_ast_node *)node;
|
||||
fx_queue_entry *cur = fx_queue_first(&fstring->n_elements);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition fstring_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_FSTRING,
|
||||
.def_node_size = sizeof(struct bshell_fstring_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_func_ast_node *func = (struct bshell_func_ast_node *)node;
|
||||
|
||||
fx_queue_entry *cur = fx_queue_first(&func->n_params);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
if (func->n_body) {
|
||||
bshell_ast_iterator_enqueue(it, func->n_body);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_func_ast_node *func = (const struct
|
||||
bshell_func_ast_node *)node;
|
||||
fx_bstr_write_fmt(out, NULL, "%s", func->n_name->tok_str);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition func_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_FUNC,
|
||||
.def_node_size = sizeof(struct bshell_func_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_hashtable_item_ast_node *item
|
||||
= (struct bshell_hashtable_item_ast_node *)node;
|
||||
|
||||
if (item->n_key) {
|
||||
bshell_ast_iterator_enqueue(it, item->n_key);
|
||||
}
|
||||
|
||||
if (item->n_value) {
|
||||
bshell_ast_iterator_enqueue(it, item->n_value);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition hashtable_item_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_HASHTABLE_ITEM,
|
||||
.def_node_size = sizeof(struct bshell_hashtable_item_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_hashtable_ast_node *hashtable
|
||||
= (struct bshell_hashtable_ast_node *)node;
|
||||
fx_queue_entry *cur = fx_queue_first(&hashtable->n_items);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child
|
||||
= fx_unbox(struct bshell_ast_node, cur, n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition hashtable_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_HASHTABLE,
|
||||
.def_node_size = sizeof(struct bshell_hashtable_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_if_branch_ast_node *if_branch
|
||||
= (struct bshell_if_branch_ast_node *)node;
|
||||
if (if_branch->n_cond) {
|
||||
bshell_ast_iterator_enqueue(it, if_branch->n_cond);
|
||||
}
|
||||
|
||||
if (if_branch->n_body) {
|
||||
bshell_ast_iterator_enqueue(it, if_branch->n_body);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition if_branch_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_IF_BRANCH,
|
||||
.def_node_size = sizeof(struct bshell_if_branch_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_if_ast_node *if_group = (struct bshell_if_ast_node *)node;
|
||||
fx_queue_entry *cur = fx_queue_first(&if_group->n_branches);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child
|
||||
= fx_unbox(struct bshell_ast_node, cur, n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition if_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_IF,
|
||||
.def_node_size = sizeof(struct bshell_if_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <fx/stream.h>
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
struct bshell_int_ast_node *i = (struct bshell_int_ast_node *)node;
|
||||
fx_value_to_string(&i->n_value->tok_number, (fx_stream *)out, NULL);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition int_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_INT,
|
||||
.def_node_size = sizeof(struct bshell_int_ast_node),
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
struct bshell_ast_node_definition null_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_NULL,
|
||||
.def_node_size = sizeof(struct bshell_null_ast_node),
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/runtime/operator.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_op_ast_node *op = (struct bshell_op_ast_node *)node;
|
||||
|
||||
if (op->n_left) {
|
||||
bshell_ast_iterator_enqueue(it, op->n_left);
|
||||
}
|
||||
|
||||
if (op->n_right) {
|
||||
bshell_ast_iterator_enqueue(it, op->n_right);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_op_ast_node *op = (const struct bshell_op_ast_node
|
||||
*)node;
|
||||
fx_bstr_write_fmt(
|
||||
out,
|
||||
NULL,
|
||||
"%s",
|
||||
bshell_operator_id_to_string(op->n_op->op_id));
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition op_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_OP,
|
||||
.def_node_size = sizeof(struct bshell_op_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_pipeline_ast_node *pipeline;
|
||||
pipeline = (struct bshell_pipeline_ast_node *)node;
|
||||
fx_queue_entry *cur = fx_queue_first(&pipeline->n_stages);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition pipeline_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_PIPELINE,
|
||||
.def_node_size = sizeof(struct bshell_pipeline_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_redirection_ast_node *redirection
|
||||
= (struct bshell_redirection_ast_node *)node;
|
||||
|
||||
if (redirection->n_out_path_expr) {
|
||||
bshell_ast_iterator_enqueue(it, redirection->n_out_path_expr);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
struct bshell_redirection_ast_node *redirection
|
||||
= (struct bshell_redirection_ast_node *)node;
|
||||
fx_bstr_write_fmt(out, NULL, "&%u", redirection->n_in);
|
||||
|
||||
if (redirection->n_append) {
|
||||
fx_bstr_write_fmt(out, NULL, " >>");
|
||||
} else {
|
||||
fx_bstr_write_fmt(out, NULL, " >");
|
||||
}
|
||||
|
||||
if (redirection->n_out_is_fd) {
|
||||
fx_bstr_write_fmt(out, NULL, " &");
|
||||
} else {
|
||||
fx_bstr_write_fmt(out, NULL, " ");
|
||||
}
|
||||
|
||||
if (redirection->n_out_is_expr) {
|
||||
fx_bstr_write_fmt(out, NULL, "<expr>");
|
||||
} else if (redirection->n_out_path) {
|
||||
fx_bstr_write_fmt(out, NULL, "'%s'", redirection->n_out_path);
|
||||
} else {
|
||||
fx_bstr_write_fmt(out, NULL, "%u", redirection->n_out);
|
||||
}
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition redirection_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_REDIRECTION,
|
||||
.def_node_size = sizeof(struct bshell_redirection_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <bshell/ast.h>
|
||||
|
||||
static enum bshell_status collect_children(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it)
|
||||
{
|
||||
struct bshell_stmt_list_ast_node *stmt_list
|
||||
= (struct bshell_stmt_list_ast_node *)node;
|
||||
fx_queue_entry *cur = fx_queue_first(&stmt_list->n_statements);
|
||||
while (cur) {
|
||||
struct bshell_ast_node *child
|
||||
= fx_unbox(struct bshell_ast_node, cur, n_entry);
|
||||
bshell_ast_iterator_enqueue(it, child);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition stmt_list_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_STMT_LIST,
|
||||
.def_node_size = sizeof(struct bshell_stmt_list_ast_node),
|
||||
.def_collect_children = collect_children,
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_string_ast_node *string = (const struct
|
||||
bshell_string_ast_node *)
|
||||
node;
|
||||
fx_bstr_write_fmt(out, NULL, "%s", string->n_value->tok_str);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition string_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_STRING,
|
||||
.def_node_size = sizeof(struct bshell_string_ast_node),
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_var_ast_node *var = (const struct
|
||||
bshell_var_ast_node *)node;
|
||||
fx_bstr_write_fmt(out, NULL, "%s", var->n_ident->tok_str);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition var_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_VAR,
|
||||
.def_node_size = sizeof(struct bshell_var_ast_node),
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static void to_string(const struct bshell_ast_node *node, fx_bstr *out)
|
||||
{
|
||||
const struct bshell_word_ast_node *word = (const struct
|
||||
bshell_word_ast_node *)node;
|
||||
fx_bstr_write_fmt(out, NULL, "%s", word->n_value->tok_str);
|
||||
}
|
||||
|
||||
struct bshell_ast_node_definition word_bshell_ast_node = {
|
||||
.def_id = BSHELL_AST_WORD,
|
||||
.def_node_size = sizeof(struct bshell_word_ast_node),
|
||||
.def_to_string = to_string,
|
||||
};
|
||||
@@ -0,0 +1,134 @@
|
||||
#include <bshell/command/alias.h>
|
||||
#include <bshell/command/command.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
struct bshell_alias_p {
|
||||
};
|
||||
|
||||
static void init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
enum bshell_status bshell_alias_get_callable_name(
|
||||
const bshell_alias *alias,
|
||||
fx_string *out)
|
||||
{
|
||||
bshell_alias_class *c
|
||||
= fx_object_get_interface(alias, BSHELL_TYPE_ALIAS);
|
||||
if (!c) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const char *callable_name = c->a_callable_name;
|
||||
if (c->a_get_callable_name) {
|
||||
callable_name = c->a_get_callable_name(alias);
|
||||
}
|
||||
|
||||
if (callable_name) {
|
||||
fx_string_append_cstr(out, callable_name);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_alias_get_target_name(
|
||||
const bshell_alias *alias,
|
||||
fx_string *out)
|
||||
{
|
||||
bshell_alias_class *c
|
||||
= fx_object_get_interface(alias, BSHELL_TYPE_ALIAS);
|
||||
if (!c) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const char *target_name = c->a_target_name;
|
||||
if (c->a_get_target_name) {
|
||||
target_name = c->a_get_target_name(alias);
|
||||
}
|
||||
|
||||
if (target_name) {
|
||||
fx_string_append_cstr(out, target_name);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status get_callable_name_static(
|
||||
const fx_type *ty,
|
||||
fx_string *out)
|
||||
{
|
||||
bshell_alias_class *c = fx_type_get_interface(ty, BSHELL_TYPE_ALIAS);
|
||||
if (!c) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (c->a_callable_name) {
|
||||
fx_string_append_cstr(out, c->a_callable_name);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status get_description(
|
||||
const bshell_command *cmd,
|
||||
fx_string *out)
|
||||
{
|
||||
bshell_alias_class *c = fx_object_get_interface(cmd, BSHELL_TYPE_ALIAS);
|
||||
if (!c) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const char *callable_name = c->a_callable_name;
|
||||
if (c->a_get_callable_name) {
|
||||
callable_name = c->a_get_callable_name(cmd);
|
||||
}
|
||||
|
||||
const char *target_name = c->a_target_name;
|
||||
if (c->a_get_target_name) {
|
||||
target_name = c->a_get_target_name(cmd);
|
||||
}
|
||||
|
||||
if (!callable_name || !target_name) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
fx_string_append_cstr(out, callable_name);
|
||||
fx_string_append_cstr(out, " -> ");
|
||||
fx_string_append_cstr(out, target_name);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status get_description_static(const fx_type *ty, fx_string *out)
|
||||
{
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(bshell_alias)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(bshell_command, BSHELL_TYPE_COMMAND)
|
||||
FX_INTERFACE_ENTRY(c_command_type) = "Alias";
|
||||
FX_INTERFACE_ENTRY(c_get_callable_name)
|
||||
= bshell_alias_get_callable_name;
|
||||
FX_INTERFACE_ENTRY(c_get_callable_name_static)
|
||||
= get_callable_name_static;
|
||||
FX_INTERFACE_ENTRY(c_get_description) = get_description;
|
||||
FX_INTERFACE_ENTRY(c_get_description_static)
|
||||
= get_description_static;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
|
||||
FX_TYPE_CLASS_END(bshell_alias)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(bshell_alias)
|
||||
FX_TYPE_ID(0x1736c10e, 0xebe6, 0x4ba5, 0x83d9, 0x5da3b7dc706c);
|
||||
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||
FX_TYPE_NAME("bshell.runtime.alias");
|
||||
FX_TYPE_EXTENDS(BSHELL_TYPE_COMMAND);
|
||||
FX_TYPE_CLASS(bshell_alias_class);
|
||||
FX_TYPE_INSTANCE_INIT(init);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct bshell_alias_p);
|
||||
FX_TYPE_DEFINITION_END(bshell_alias)
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <bshell/command/cmdlet.h>
|
||||
#include <bshell/command/command.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
struct bshell_cmdlet_p {
|
||||
};
|
||||
|
||||
static void init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static enum bshell_status get_callable_name_static(
|
||||
const fx_type *ty,
|
||||
fx_string *out)
|
||||
{
|
||||
bshell_cmdlet_class *class = fx_type_get_interface(
|
||||
ty,
|
||||
BSHELL_TYPE_CMDLET);
|
||||
if (!class) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const bshell_verb *verb_info = bshell_verb_get_by_id(class->c_verb);
|
||||
if (!verb_info) {
|
||||
return BSHELL_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
fx_string_append_cstrf(
|
||||
out,
|
||||
"%s-%s",
|
||||
bshell_verb_get_name(verb_info),
|
||||
class->c_noun);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(bshell_cmdlet)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(bshell_command, BSHELL_TYPE_COMMAND)
|
||||
FX_INTERFACE_ENTRY(c_command_type) = "Cmdlet";
|
||||
FX_INTERFACE_ENTRY(c_get_callable_name_static)
|
||||
= get_callable_name_static;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(bshell_command, BSHELL_TYPE_COMMAND)
|
||||
FX_TYPE_CLASS_END(bshell_cmdlet)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(bshell_cmdlet)
|
||||
FX_TYPE_ID(0xc71f4d59, 0x8066, 0x4294, 0xa6b0, 0xe1f3eb04f454);
|
||||
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||
FX_TYPE_NAME("bshell.runtime.cmdlet");
|
||||
FX_TYPE_EXTENDS(BSHELL_TYPE_COMMAND);
|
||||
FX_TYPE_CLASS(bshell_cmdlet_class);
|
||||
FX_TYPE_INSTANCE_INIT(init);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct bshell_cmdlet_p);
|
||||
FX_TYPE_DEFINITION_END(bshell_cmdlet)
|
||||
@@ -0,0 +1,287 @@
|
||||
#include <bshell/command/command.h>
|
||||
#include <bshell/runtime/pipeline.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
struct bshell_command_p {
|
||||
fx_value *cmd_args;
|
||||
size_t cmd_nr_args;
|
||||
bool cmd_args_reversed;
|
||||
};
|
||||
|
||||
static void init(fx_object *obj, void *priv)
|
||||
{
|
||||
}
|
||||
|
||||
static void command_set_args(
|
||||
struct bshell_command_p *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args)
|
||||
{
|
||||
cmd->cmd_args = args;
|
||||
cmd->cmd_nr_args = nr_args;
|
||||
cmd->cmd_args_reversed = false;
|
||||
}
|
||||
|
||||
static void command_set_args_reverse(
|
||||
struct bshell_command_p *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args)
|
||||
{
|
||||
cmd->cmd_args = args;
|
||||
cmd->cmd_nr_args = nr_args;
|
||||
cmd->cmd_args_reversed = true;
|
||||
}
|
||||
|
||||
static const fx_value *command_get_arg(
|
||||
struct bshell_command_p *cmd,
|
||||
size_t index)
|
||||
{
|
||||
if (index >= cmd->cmd_nr_args) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cmd->cmd_args_reversed) {
|
||||
return &cmd->cmd_args[cmd->cmd_nr_args - index - 1];
|
||||
}
|
||||
|
||||
return &cmd->cmd_args[index];
|
||||
}
|
||||
|
||||
static fx_status get_command_type(
|
||||
const fx_value *cmd_v,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
bshell_command *cmd = NULL;
|
||||
fx_value_get_object(cmd_v, &cmd);
|
||||
bshell_command_class *cmd_class
|
||||
= fx_object_get_interface(cmd, BSHELL_TYPE_COMMAND);
|
||||
|
||||
*out = FX_CSTR(cmd_class->c_command_type);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_name(
|
||||
const fx_value *cmd_v,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
bshell_command *cmd = NULL;
|
||||
fx_value_get_object(cmd_v, &cmd);
|
||||
bshell_command_class *cmd_class
|
||||
= fx_object_get_interface(cmd, BSHELL_TYPE_COMMAND);
|
||||
|
||||
fx_string *result = fx_string_create();
|
||||
const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd));
|
||||
|
||||
if (cmd_class->c_get_description) {
|
||||
cmd_class->c_get_description(cmd, result);
|
||||
} else if (cmd_class->c_get_description_static) {
|
||||
cmd_class->c_get_description_static(ty, result);
|
||||
} else if (cmd_class->c_get_callable_name) {
|
||||
cmd_class->c_get_callable_name(cmd, result);
|
||||
} else if (cmd_class->c_get_callable_name_static) {
|
||||
cmd_class->c_get_callable_name_static(ty, result);
|
||||
}
|
||||
|
||||
*out = FX_VALUE_OBJECT(result);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_version(
|
||||
const fx_value *cmd_v,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
bshell_command *cmd = NULL;
|
||||
fx_value_get_object(cmd_v, &cmd);
|
||||
const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd));
|
||||
const fx_assembly *assembly = fx_type_get_assembly(ty);
|
||||
|
||||
long major = 0, minor = 0, build = 0, revision = 0;
|
||||
fx_assembly_get_version(assembly, &major, &minor, &build, &revision);
|
||||
|
||||
fx_string *result = fx_string_create();
|
||||
if (revision != 0) {
|
||||
fx_string_append_cstrf(
|
||||
result,
|
||||
"%ld.%ld.%ld.%ld",
|
||||
major,
|
||||
minor,
|
||||
build,
|
||||
revision);
|
||||
} else {
|
||||
fx_string_append_cstrf(
|
||||
result,
|
||||
"%ld.%ld.%ld",
|
||||
major,
|
||||
minor,
|
||||
build);
|
||||
}
|
||||
|
||||
*out = FX_VALUE_OBJECT(result);
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static fx_status get_source(
|
||||
const fx_value *cmd_v,
|
||||
const fx_property *prop,
|
||||
fx_value *out)
|
||||
{
|
||||
bshell_command *cmd = NULL;
|
||||
fx_value_get_object(cmd_v, &cmd);
|
||||
const fx_type *ty = fx_type_get_by_id(fx_object_query_type(cmd));
|
||||
const fx_assembly *assembly = fx_type_get_assembly(ty);
|
||||
|
||||
*out = FX_CSTR(fx_assembly_get_name(assembly));
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
extern const fx_assembly *bshell_runtime_assembly_get(void);
|
||||
|
||||
bshell_command *bshell_command_find_static(const char *name)
|
||||
{
|
||||
fx_string *target_name = fx_string_create();
|
||||
fx_string *call_name = fx_string_create_from_cstr(name);
|
||||
fx_string_transform_lowercase(call_name);
|
||||
|
||||
bshell_command *result = NULL;
|
||||
|
||||
fx_iterator *assemblies = fx_assembly_get_all();
|
||||
fx_foreach(asm_v, assemblies)
|
||||
{
|
||||
fx_assembly *assembly = NULL;
|
||||
fx_value_get_object(&asm_v, &assembly);
|
||||
fx_iterator *types = fx_assembly_get_types(assembly);
|
||||
fx_foreach(v, types)
|
||||
{
|
||||
fx_type *ty = NULL;
|
||||
fx_value_get_object(&v, &ty);
|
||||
if (fx_type_id_compare(
|
||||
fx_type_get_id(ty),
|
||||
BSHELL_TYPE_COMMAND)
|
||||
== 0) {
|
||||
continue;
|
||||
}
|
||||
bshell_command_class *cmd = fx_type_get_interface(
|
||||
ty,
|
||||
BSHELL_TYPE_COMMAND);
|
||||
if (!cmd || !cmd->c_get_callable_name_static) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_string_clear(target_name);
|
||||
cmd->c_get_callable_name_static(ty, target_name);
|
||||
|
||||
if (!fx_strcmp_nocase(
|
||||
fx_string_get_cstr(call_name),
|
||||
fx_string_get_cstr(target_name))) {
|
||||
result = fx_object_create(fx_type_get_id(ty));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fx_iterator_unref(types);
|
||||
}
|
||||
|
||||
fx_iterator_unref(assemblies);
|
||||
fx_string_unref(call_name);
|
||||
fx_string_unref(target_name);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void bshell_command_set_args(
|
||||
bshell_command *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
BSHELL_TYPE_COMMAND,
|
||||
command_set_args,
|
||||
cmd,
|
||||
args,
|
||||
nr_args);
|
||||
}
|
||||
|
||||
void bshell_command_set_args_reverse(
|
||||
bshell_command *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_V(
|
||||
BSHELL_TYPE_COMMAND,
|
||||
command_set_args_reverse,
|
||||
cmd,
|
||||
args,
|
||||
nr_args);
|
||||
}
|
||||
|
||||
const fx_value *bshell_command_get_arg(bshell_command *cmd, size_t index)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_COMMAND,
|
||||
command_get_arg,
|
||||
cmd,
|
||||
index);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_command_begin_processing(bshell_command *command)
|
||||
{
|
||||
FX_CLASS_DISPATCH_VIRTUAL_0(
|
||||
bshell_command,
|
||||
BSHELL_TYPE_COMMAND,
|
||||
BSHELL_ERR_NOT_SUPPORTED,
|
||||
c_begin_processing,
|
||||
command);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_command_process_record(
|
||||
bshell_command *command,
|
||||
bshell_pipeline *pipeline,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
FX_CLASS_DISPATCH_VIRTUAL(
|
||||
bshell_command,
|
||||
BSHELL_TYPE_COMMAND,
|
||||
BSHELL_ERR_NOT_SUPPORTED,
|
||||
c_process_record,
|
||||
command,
|
||||
pipeline,
|
||||
rt);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_command_end_processing(bshell_command *command)
|
||||
{
|
||||
FX_CLASS_DISPATCH_VIRTUAL_0(
|
||||
bshell_command,
|
||||
BSHELL_TYPE_COMMAND,
|
||||
BSHELL_ERR_NOT_SUPPORTED,
|
||||
c_end_processing,
|
||||
command);
|
||||
}
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(bshell_command)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_PROPERTY("command_type", get_command_type, NULL);
|
||||
FX_TYPE_PROPERTY("name", get_name, NULL);
|
||||
FX_TYPE_PROPERTY("version", get_version, NULL);
|
||||
FX_TYPE_PROPERTY("source", get_source, NULL);
|
||||
FX_TYPE_CLASS_END(bshell_command)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(bshell_command)
|
||||
FX_TYPE_ID(0x5c50630d, 0x7535, 0x40ed, 0xbae5, 0x88aabc274d79);
|
||||
FX_TYPE_FLAGS(FX_TYPE_F_ABSTRACT);
|
||||
FX_TYPE_NAME("bshell.runtime.command");
|
||||
FX_TYPE_CLASS(bshell_command_class);
|
||||
FX_TYPE_INSTANCE_INIT(init);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct bshell_command_p);
|
||||
FX_TYPE_DEFINITION_END(bshell_command)
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
static const struct compile_value_type array_type = {};
|
||||
static const struct compile_value_type array_item_type = {};
|
||||
|
||||
struct compile_result compile_array(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_array_ast_node *array;
|
||||
array = (struct bshell_array_ast_node *)src;
|
||||
size_t nr_items = fx_queue_length(&array->n_items);
|
||||
for (size_t i = 0; i < nr_items; i++) {
|
||||
struct compile_value *item = compile_pop_value(ctx);
|
||||
compile_value_load(ctx, item);
|
||||
compile_value_destroy(item);
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_MK_ARRAY,
|
||||
nr_items);
|
||||
compile_push_value(ctx, &array_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
#include <bshell/compile.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static enum bshell_status block_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_LDBLOCK,
|
||||
value->v_pool);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type block_type = {
|
||||
.v_load = block_load,
|
||||
};
|
||||
|
||||
struct compile_result compile_block(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
bshell_scriptblock *sub_block = bshell_scriptblock_create();
|
||||
if (!sub_block) {
|
||||
return COMPILE_ERR(BSHELL_ERR_NO_MEMORY);
|
||||
}
|
||||
|
||||
enum bshell_status status = bshell_compile(src, sub_block);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
bshell_scriptblock_unref(sub_block);
|
||||
return COMPILE_ERR(status);
|
||||
}
|
||||
|
||||
fx_value sub_block_value = FX_VALUE_OBJECT(sub_block);
|
||||
unsigned long slot = bshell_scriptblock_add_pool_value(
|
||||
ctx->c_block,
|
||||
&sub_block_value);
|
||||
fx_value_unset(&sub_block_value);
|
||||
compile_push_pool_value(ctx, &block_type, slot);
|
||||
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
enum bshell_status compile_block_immediate(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
enum bshell_status status = BSHELL_SUCCESS;
|
||||
struct bshell_block_ast_node *block = (struct bshell_block_ast_node *)
|
||||
src;
|
||||
|
||||
fx_queue_entry *cur = fx_queue_first(&block->n_statements);
|
||||
while (cur && status == BSHELL_SUCCESS) {
|
||||
struct bshell_ast_node *node = fx_unbox(
|
||||
struct bshell_ast_node,
|
||||
cur,
|
||||
n_entry);
|
||||
status = compile_node(ctx, node);
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status cmdcall_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_cmdcall_ast_node *cmdcall;
|
||||
cmdcall = (struct bshell_cmdcall_ast_node *)value->v_node;
|
||||
bshell_scriptblock_push_instruction(ctx->c_block, BSHELL_OPCODE_PEXEC, 1);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status pipeline_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_pipeline_ast_node *pipeline;
|
||||
pipeline = (struct bshell_pipeline_ast_node *)value->v_node;
|
||||
size_t nr_items = fx_queue_length(&pipeline->n_stages);
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_PEXEC,
|
||||
nr_items);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type cmdcall_type = {
|
||||
.v_load = cmdcall_load,
|
||||
};
|
||||
|
||||
static const struct compile_value_type pipeline_type = {
|
||||
.v_load = pipeline_load,
|
||||
};
|
||||
|
||||
struct compile_result compile_cmdcall(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_cmdcall_ast_node *cmdcall;
|
||||
cmdcall = (struct bshell_cmdcall_ast_node *)src;
|
||||
size_t nr_args = fx_queue_length(&cmdcall->n_args);
|
||||
for (size_t i = 0; i < nr_args; i++) {
|
||||
struct compile_value *arg = compile_pop_value(ctx);
|
||||
compile_value_load(ctx, arg);
|
||||
compile_value_destroy(arg);
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_LDCMD,
|
||||
nr_args);
|
||||
|
||||
compile_push_value(ctx, &cmdcall_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result compile_pipeline(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_pipeline_ast_node *pipeline;
|
||||
pipeline = (struct bshell_pipeline_ast_node *)src;
|
||||
size_t nr_items = fx_queue_length(&pipeline->n_stages);
|
||||
for (size_t i = 0; i < nr_items; i++) {
|
||||
struct compile_value *item = compile_pop_value(ctx);
|
||||
if (item->v_node->n_type != BSHELL_AST_CMDCALL) {
|
||||
compile_value_load(ctx, item);
|
||||
}
|
||||
compile_value_destroy(item);
|
||||
}
|
||||
|
||||
compile_push_value(ctx, &pipeline_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
#ifndef COMPILE_COMPILE_NODE_H_
|
||||
#define COMPILE_COMPILE_NODE_H_
|
||||
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/runtime/operator.h>
|
||||
#include <bshell/runtime/script-block.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
#define COMPILE_OK(flags) \
|
||||
((struct compile_result) {.r_status = BSHELL_SUCCESS, \
|
||||
.r_flags = (flags)})
|
||||
#define COMPILE_ERR(status) ((struct compile_result) {.r_status = (status)})
|
||||
|
||||
struct compile_ctx;
|
||||
struct compile_value;
|
||||
|
||||
struct compile_result {
|
||||
enum bshell_status r_status;
|
||||
enum {
|
||||
COMPILE_REPEAT_NODE = 0x00u,
|
||||
} r_flags;
|
||||
};
|
||||
|
||||
typedef struct compile_result (*compile_begin_impl)(struct compile_ctx *);
|
||||
typedef struct compile_result (*compile_end_impl)(struct compile_ctx *);
|
||||
typedef struct compile_result (
|
||||
*compile_node_impl)(struct compile_ctx *, struct bshell_ast_node *);
|
||||
|
||||
enum compile_state_type_id {
|
||||
COMPILE_NORMAL = 0,
|
||||
COMPILE_FSTRING,
|
||||
};
|
||||
|
||||
struct compile_value_type {
|
||||
enum bshell_status (
|
||||
*v_load)(struct compile_ctx *, struct compile_value *);
|
||||
enum bshell_status (
|
||||
*v_store)(struct compile_ctx *, struct compile_value *);
|
||||
};
|
||||
|
||||
struct compile_value {
|
||||
const struct compile_value_type *v_type;
|
||||
enum bshell_operator_id v_op;
|
||||
struct compile_value *v_left, *v_right;
|
||||
struct compile_value **v_args;
|
||||
size_t v_nr_args;
|
||||
struct bshell_ast_node *v_node;
|
||||
unsigned long v_pool;
|
||||
fx_queue_entry v_entry;
|
||||
};
|
||||
|
||||
struct compile_label {
|
||||
size_t l_ip;
|
||||
};
|
||||
|
||||
struct compile_label_ref {
|
||||
fx_queue_entry ref_entry;
|
||||
size_t ref_offset;
|
||||
size_t ref_label_id;
|
||||
};
|
||||
|
||||
struct compile_ctx {
|
||||
fx_queue c_state, c_stack;
|
||||
bshell_scriptblock *c_block;
|
||||
FX_VECTOR_DECLARE(struct compile_label, c_labels);
|
||||
fx_queue c_label_refs;
|
||||
size_t c_depth;
|
||||
};
|
||||
|
||||
struct compile_state_type {
|
||||
size_t s_size;
|
||||
compile_begin_impl s_compile_begin;
|
||||
compile_end_impl s_compile_end;
|
||||
const compile_node_impl *s_compile_node;
|
||||
compile_node_impl s_compile_node_generic;
|
||||
size_t s_nr_compile_node;
|
||||
};
|
||||
|
||||
struct compile_state {
|
||||
const struct compile_state_type *s_type;
|
||||
fx_queue_entry s_entry;
|
||||
size_t s_depth;
|
||||
};
|
||||
|
||||
extern struct compile_state *compile_push_state(
|
||||
struct compile_ctx *ctx,
|
||||
enum compile_state_type_id state_type);
|
||||
extern void compile_pop_state(struct compile_ctx *ctx);
|
||||
extern struct compile_state *compile_get_state(const struct compile_ctx *ctx);
|
||||
|
||||
extern enum bshell_status compile_push_value(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
struct bshell_ast_node *node);
|
||||
extern enum bshell_status compile_push_value_array(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
struct bshell_ast_node *node,
|
||||
struct compile_value **values,
|
||||
size_t nr_values);
|
||||
extern enum bshell_status compile_push_pool_value(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
unsigned long pool_slot);
|
||||
extern enum bshell_status compile_push_op(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *node,
|
||||
const struct compile_value_type *type,
|
||||
enum bshell_operator_id op,
|
||||
struct compile_value *left,
|
||||
struct compile_value *right);
|
||||
extern struct compile_value *compile_pop_value(struct compile_ctx *ctx);
|
||||
extern void compile_value_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *item);
|
||||
extern void compile_value_store(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *item);
|
||||
extern void compile_value_destroy(struct compile_value *item);
|
||||
|
||||
extern size_t compile_declare_label(struct compile_ctx *ctx);
|
||||
extern void compile_define_label(struct compile_ctx *ctx, size_t label_id);
|
||||
extern void compile_ref_label(struct compile_ctx *ctx, size_t label_id);
|
||||
extern enum bshell_status compile_resolve_labels(struct compile_ctx *ctx);
|
||||
|
||||
extern struct compile_result compile_int(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_double(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_string(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_word(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_op(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_var(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern struct compile_result compile_fstring(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_array(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_hashtable_item(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_hashtable(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern struct compile_result compile_block(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern struct compile_result compile_cmdcall(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern struct compile_result compile_pipeline(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern enum bshell_status compile_if(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern enum bshell_status compile_block_immediate(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
extern enum bshell_status compile_expression(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
extern enum bshell_status compile_node(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/compile.h>
|
||||
#include <bshell/status.h>
|
||||
|
||||
extern enum bshell_status compile_node(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
switch (src->n_type) {
|
||||
case BSHELL_AST_IF:
|
||||
return compile_if(ctx, src);
|
||||
case BSHELL_AST_BLOCK:
|
||||
return compile_block_immediate(ctx, src);
|
||||
default:
|
||||
return compile_expression(ctx, src);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_compile(
|
||||
struct bshell_ast_node *src,
|
||||
bshell_scriptblock *dest)
|
||||
{
|
||||
struct compile_ctx ctx = {.c_block = dest};
|
||||
|
||||
enum bshell_status status = compile_node(&ctx, src);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = compile_resolve_labels(&ctx);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status int_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_int_ast_node *i
|
||||
= (struct bshell_int_ast_node *)value->v_node;
|
||||
long long v;
|
||||
fx_value_get_longlong(&i->n_value->tok_number, &v);
|
||||
bshell_scriptblock_push_instruction(ctx->c_block, BSHELL_OPCODE_LDC_INT, v);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status double_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_double_ast_node *d
|
||||
= (struct bshell_double_ast_node *)value->v_node;
|
||||
double v;
|
||||
fx_value_get_double(&d->n_value->tok_number, &v);
|
||||
unsigned long index = bshell_scriptblock_get_double(ctx->c_block, v);
|
||||
bshell_scriptblock_push_instruction(ctx->c_block, BSHELL_OPCODE_LDC_FP, index);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status string_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_string_ast_node *s
|
||||
= (struct bshell_string_ast_node *)value->v_node;
|
||||
unsigned long index = bshell_scriptblock_get_string(
|
||||
ctx->c_block,
|
||||
s->n_value->tok_str);
|
||||
if (index == POOL_INDEX_INVALID) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_LDC_STR,
|
||||
index);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status word_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_word_ast_node *s
|
||||
= (struct bshell_word_ast_node *)value->v_node;
|
||||
unsigned long index = bshell_scriptblock_get_string(
|
||||
ctx->c_block,
|
||||
s->n_value->tok_str);
|
||||
if (index == POOL_INDEX_INVALID) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_LDC_STR,
|
||||
index);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type int_type = {
|
||||
.v_load = int_load,
|
||||
};
|
||||
|
||||
static const struct compile_value_type double_type = {
|
||||
.v_load = double_load,
|
||||
};
|
||||
|
||||
static const struct compile_value_type string_type = {
|
||||
.v_load = string_load,
|
||||
};
|
||||
|
||||
static const struct compile_value_type word_type = {
|
||||
.v_load = word_load,
|
||||
};
|
||||
|
||||
struct compile_result compile_int(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_value(ctx, &int_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result compile_double(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_value(ctx, &double_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result compile_string(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_value(ctx, &string_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result compile_word(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_value(ctx, &word_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
static struct bshell_ast_iterate_result do_compile_node(
|
||||
struct bshell_ast_node *node,
|
||||
enum bshell_ast_iteration_type it_type,
|
||||
struct bshell_ast_iterator *it,
|
||||
struct compile_ctx *ctx)
|
||||
{
|
||||
if (it_type != BSHELL_AST_ITERATION_POST) {
|
||||
int flags = 0;
|
||||
switch (node->n_type) {
|
||||
case BSHELL_AST_BLOCK:
|
||||
break;
|
||||
default:
|
||||
flags |= BSHELL_AST_ITERATE_ADD_CHILDREN;
|
||||
break;
|
||||
}
|
||||
|
||||
return BSHELL_AST_ITERATE_OK(flags);
|
||||
}
|
||||
|
||||
ctx->c_depth = node->n_it.e_depth;
|
||||
struct compile_state *state = compile_get_state(ctx);
|
||||
while (state->s_depth >= ctx->c_depth && state->s_depth > 0) {
|
||||
compile_pop_state(ctx);
|
||||
state = compile_get_state(ctx);
|
||||
}
|
||||
|
||||
compile_node_impl impl = NULL;
|
||||
|
||||
if (node->n_type < state->s_type->s_nr_compile_node) {
|
||||
impl = state->s_type->s_compile_node[node->n_type];
|
||||
}
|
||||
|
||||
if (!impl) {
|
||||
impl = state->s_type->s_compile_node_generic;
|
||||
}
|
||||
|
||||
if (!impl) {
|
||||
return BSHELL_AST_ITERATE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result result = impl(ctx, node);
|
||||
unsigned int flags = 0;
|
||||
if (result.r_flags & COMPILE_REPEAT_NODE) {
|
||||
flags |= BSHELL_AST_ITERATE_REPEAT;
|
||||
}
|
||||
|
||||
if (result.r_status == BSHELL_SUCCESS) {
|
||||
return BSHELL_AST_ITERATE_OK(flags);
|
||||
}
|
||||
|
||||
return BSHELL_AST_ITERATE_ERR(result.r_status);
|
||||
}
|
||||
|
||||
enum bshell_status compile_expression(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_state(ctx, COMPILE_NORMAL);
|
||||
|
||||
struct bshell_ast_iterator it = {0};
|
||||
bshell_ast_node_iterate(
|
||||
src,
|
||||
&it,
|
||||
(bshell_ast_iterator_callback)do_compile_node,
|
||||
ctx);
|
||||
|
||||
struct compile_value *result = compile_pop_value(ctx);
|
||||
if (result) {
|
||||
compile_value_load(ctx, result);
|
||||
compile_value_destroy(result);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status fstring_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_fstring_ast_node *fstring
|
||||
= (struct bshell_fstring_ast_node *)value->v_node;
|
||||
for (size_t i = 0; i < value->v_nr_args; i++) {
|
||||
compile_value_load(ctx, value->v_args[i]);
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_MK_STR,
|
||||
fx_queue_length(&fstring->n_elements));
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type fstring_type = {
|
||||
.v_load = fstring_load,
|
||||
};
|
||||
|
||||
struct compile_result compile_fstring(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_fstring_ast_node *fstring
|
||||
= (struct bshell_fstring_ast_node *)src;
|
||||
size_t nr_items = fx_queue_length(&fstring->n_elements);
|
||||
struct compile_value **items = calloc(nr_items, sizeof *items);
|
||||
|
||||
for (size_t i = 0; i < nr_items; i++) {
|
||||
struct compile_value *item = compile_pop_value(ctx);
|
||||
items[i] = item;
|
||||
}
|
||||
|
||||
compile_push_value_array(ctx, &fstring_type, src, items, nr_items);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
static const struct compile_value_type hashtable_type = {};
|
||||
static const struct compile_value_type hashtable_item_type = {};
|
||||
|
||||
struct compile_result compile_hashtable(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_hashtable_ast_node *hashtable
|
||||
= (struct bshell_hashtable_ast_node *)src;
|
||||
size_t nr_items = fx_queue_length(&hashtable->n_items);
|
||||
for (size_t i = 0; i < nr_items; i++) {
|
||||
struct compile_value *item = compile_pop_value(ctx);
|
||||
compile_value_load(ctx, item);
|
||||
compile_value_destroy(item);
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_MK_HTAB,
|
||||
nr_items);
|
||||
compile_push_value(ctx, &hashtable_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
|
||||
struct compile_result compile_hashtable_item(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct compile_value *value = compile_pop_value(ctx);
|
||||
struct compile_value *key = compile_pop_value(ctx);
|
||||
compile_value_load(ctx, key);
|
||||
compile_value_load(ctx, value);
|
||||
compile_value_destroy(key);
|
||||
compile_value_destroy(value);
|
||||
|
||||
compile_push_value(ctx, &hashtable_item_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status compile_if(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_if_ast_node *if_group;
|
||||
if_group = (struct bshell_if_ast_node *)src;
|
||||
size_t nr_labels = fx_queue_length(&if_group->n_branches);
|
||||
// add one extra label for the end of the if group
|
||||
nr_labels++;
|
||||
|
||||
size_t *label_ids = calloc(nr_labels, sizeof *label_ids);
|
||||
if (!label_ids) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
for (i = 0; i < nr_labels; i++) {
|
||||
label_ids[i] = compile_declare_label(ctx);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
fx_queue_entry *cur = fx_queue_first(&if_group->n_branches);
|
||||
while (cur) {
|
||||
struct bshell_if_branch_ast_node *branch;
|
||||
branch = fx_unbox(
|
||||
struct bshell_if_branch_ast_node,
|
||||
cur,
|
||||
n_base.n_entry);
|
||||
if (!branch->n_cond) {
|
||||
compile_block_immediate(ctx, branch->n_body);
|
||||
compile_ref_label(ctx, label_ids[nr_labels - 1]);
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_BR,
|
||||
0);
|
||||
break;
|
||||
}
|
||||
|
||||
compile_expression(ctx, branch->n_cond);
|
||||
compile_ref_label(ctx, label_ids[i++]);
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_BR_TRUE,
|
||||
0);
|
||||
|
||||
cur = fx_queue_next(cur);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
cur = fx_queue_first(&if_group->n_branches);
|
||||
while (cur) {
|
||||
struct bshell_if_branch_ast_node *branch = fx_unbox(
|
||||
struct bshell_if_branch_ast_node,
|
||||
cur,
|
||||
n_base.n_entry);
|
||||
if (!branch->n_cond) {
|
||||
break;
|
||||
}
|
||||
|
||||
compile_define_label(ctx, label_ids[i++]);
|
||||
compile_block_immediate(ctx, branch->n_body);
|
||||
cur = fx_queue_next(cur);
|
||||
|
||||
if (i < nr_labels - 2) {
|
||||
compile_ref_label(ctx, label_ids[nr_labels - 1]);
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_BR,
|
||||
0);
|
||||
}
|
||||
}
|
||||
|
||||
compile_define_label(ctx, label_ids[nr_labels - 1]);
|
||||
free(label_ids);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
size_t compile_declare_label(struct compile_ctx *ctx)
|
||||
{
|
||||
size_t id = ctx->c_labels.count;
|
||||
struct compile_label *label = fx_vector_emplace_back(
|
||||
ctx->c_labels,
|
||||
NULL);
|
||||
return id;
|
||||
}
|
||||
|
||||
void compile_define_label(struct compile_ctx *ctx, size_t label_id)
|
||||
{
|
||||
size_t offset = 0;
|
||||
bshell_scriptblock_get_text(ctx->c_block, NULL, &offset);
|
||||
offset *= sizeof(bshell_instruction);
|
||||
ctx->c_labels.items[label_id].l_ip = offset;
|
||||
}
|
||||
|
||||
void compile_ref_label(struct compile_ctx *ctx, size_t label_id)
|
||||
{
|
||||
struct compile_label_ref *ref = malloc(sizeof *ref);
|
||||
if (!ref) {
|
||||
return;
|
||||
}
|
||||
|
||||
memset(ref, 0x0, sizeof *ref);
|
||||
ref->ref_label_id = label_id;
|
||||
bshell_scriptblock_get_text(ctx->c_block, NULL, &ref->ref_offset);
|
||||
ref->ref_offset *= sizeof(bshell_instruction);
|
||||
fx_queue_push_back(&ctx->c_label_refs, &ref->ref_entry);
|
||||
}
|
||||
|
||||
enum bshell_status compile_resolve_labels(struct compile_ctx *ctx)
|
||||
{
|
||||
fx_queue_entry *cur = fx_queue_pop_front(&ctx->c_label_refs);
|
||||
while (cur) {
|
||||
struct compile_label_ref *ref = fx_unbox(
|
||||
struct compile_label_ref,
|
||||
cur,
|
||||
ref_entry);
|
||||
struct compile_label *label = &ctx->c_labels.items
|
||||
[ref->ref_label_id];
|
||||
|
||||
long long rel_value = (long long)label->l_ip
|
||||
- (long long)ref->ref_offset;
|
||||
|
||||
enum bshell_opcode op;
|
||||
uint32_t arg;
|
||||
bshell_scriptblock_read_instruction(
|
||||
ctx->c_block,
|
||||
ref->ref_offset,
|
||||
&op,
|
||||
&arg);
|
||||
arg = (uint32_t)rel_value;
|
||||
bshell_scriptblock_patch_instruction(
|
||||
ctx->c_block,
|
||||
ref->ref_offset,
|
||||
op,
|
||||
arg);
|
||||
|
||||
free(ref);
|
||||
cur = fx_queue_pop_front(&ctx->c_label_refs);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
static const compile_node_impl node_compilers[] = {
|
||||
[BSHELL_AST_INT] = compile_int,
|
||||
[BSHELL_AST_DOUBLE] = compile_double,
|
||||
[BSHELL_AST_STRING] = compile_string,
|
||||
[BSHELL_AST_WORD] = compile_word,
|
||||
[BSHELL_AST_OP] = compile_op,
|
||||
[BSHELL_AST_VAR] = compile_var,
|
||||
[BSHELL_AST_FSTRING] = compile_fstring,
|
||||
[BSHELL_AST_ARRAY] = compile_array,
|
||||
[BSHELL_AST_HASHTABLE] = compile_hashtable,
|
||||
[BSHELL_AST_HASHTABLE_ITEM] = compile_hashtable_item,
|
||||
[BSHELL_AST_CMDCALL] = compile_cmdcall,
|
||||
[BSHELL_AST_PIPELINE] = compile_pipeline,
|
||||
[BSHELL_AST_BLOCK] = compile_block,
|
||||
};
|
||||
static const size_t nr_node_compilers = sizeof node_compilers
|
||||
/ sizeof node_compilers[0];
|
||||
|
||||
const struct compile_state_type normal_state = {
|
||||
.s_size = sizeof(struct compile_state),
|
||||
.s_compile_node = node_compilers,
|
||||
.s_nr_compile_node = nr_node_compilers,
|
||||
};
|
||||
@@ -0,0 +1,126 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
#include <bshell/runtime/operator.h>
|
||||
|
||||
static enum bshell_opcode opcode_for_op(enum bshell_operator_id op)
|
||||
{
|
||||
switch (op) {
|
||||
case BSHELL_OP_ADD:
|
||||
return BSHELL_OPCODE_ADD;
|
||||
case BSHELL_OP_SUBTRACT:
|
||||
return BSHELL_OPCODE_SUB;
|
||||
case BSHELL_OP_MULTIPLY:
|
||||
return BSHELL_OPCODE_MUL;
|
||||
case BSHELL_OP_DIVIDE:
|
||||
return BSHELL_OPCODE_DIV;
|
||||
case BSHELL_OP_MODULO:
|
||||
return BSHELL_OPCODE_MOD;
|
||||
case BSHELL_OP_INCREMENT:
|
||||
return BSHELL_OPCODE_INC;
|
||||
case BSHELL_OP_DECREMENT:
|
||||
return BSHELL_OPCODE_DEC;
|
||||
case BSHELL_OP_LEFT_SHIFT:
|
||||
return BSHELL_OPCODE_SHL;
|
||||
case BSHELL_OP_RIGHT_SHIFT:
|
||||
return BSHELL_OPCODE_SHR;
|
||||
case BSHELL_OP_BINARY_AND:
|
||||
return BSHELL_OPCODE_BAND;
|
||||
case BSHELL_OP_BINARY_OR:
|
||||
return BSHELL_OPCODE_BOR;
|
||||
case BSHELL_OP_BINARY_XOR:
|
||||
return BSHELL_OPCODE_BXOR;
|
||||
case BSHELL_OP_BINARY_NOT:
|
||||
return BSHELL_OPCODE_BNOT;
|
||||
case BSHELL_OP_LESS_THAN:
|
||||
return BSHELL_OPCODE_CMP_LT;
|
||||
case BSHELL_OP_GREATER_THAN:
|
||||
return BSHELL_OPCODE_CMP_GT;
|
||||
case BSHELL_OP_EQUAL:
|
||||
return BSHELL_OPCODE_CMP_EQ;
|
||||
case BSHELL_OP_NOT_EQUAL:
|
||||
return BSHELL_OPCODE_CMP_NE;
|
||||
case BSHELL_OP_LESS_EQUAL:
|
||||
return BSHELL_OPCODE_CMP_LE;
|
||||
case BSHELL_OP_GREATER_EQUAL:
|
||||
return BSHELL_OPCODE_CMP_GE;
|
||||
case BSHELL_OP_LOGICAL_AND:
|
||||
return BSHELL_OPCODE_LAND;
|
||||
case BSHELL_OP_LOGICAL_OR:
|
||||
return BSHELL_OPCODE_LOR;
|
||||
case BSHELL_OP_LOGICAL_XOR:
|
||||
return BSHELL_OPCODE_LXOR;
|
||||
case BSHELL_OP_LOGICAL_NOT:
|
||||
return BSHELL_OPCODE_LNOT;
|
||||
case BSHELL_OP_RANGE:
|
||||
return BSHELL_OPCODE_RANGE;
|
||||
case BSHELL_OP_MATCH:
|
||||
return BSHELL_OPCODE_MATCH;
|
||||
case BSHELL_OP_REPLACE:
|
||||
return BSHELL_OPCODE_REPLACE;
|
||||
case BSHELL_OP_LIKE:
|
||||
return BSHELL_OPCODE_LIKE;
|
||||
case BSHELL_OP_IN:
|
||||
return BSHELL_OPCODE_IN;
|
||||
case BSHELL_OP_FORMAT:
|
||||
return BSHELL_OPCODE_FMT;
|
||||
case BSHELL_OP_CONTAINS:
|
||||
return BSHELL_OPCODE_CONTAINS;
|
||||
case BSHELL_OP_USPLIT:
|
||||
return BSHELL_OPCODE_SPLIT;
|
||||
case BSHELL_OP_BSPLIT:
|
||||
return BSHELL_OPCODE_SPLIT;
|
||||
case BSHELL_OP_UJOIN:
|
||||
return BSHELL_OPCODE_JOIN;
|
||||
case BSHELL_OP_BJOIN:
|
||||
return BSHELL_OPCODE_JOIN;
|
||||
case BSHELL_OP_IS:
|
||||
return BSHELL_OPCODE_IS;
|
||||
case BSHELL_OP_AS:
|
||||
return BSHELL_OPCODE_AS;
|
||||
case BSHELL_OP_ACCESS:
|
||||
return BSHELL_OPCODE_LDPROP;
|
||||
default:
|
||||
return BSHELL_OPCODE_NOP;
|
||||
}
|
||||
}
|
||||
|
||||
enum bshell_status op_load(struct compile_ctx *ctx, struct compile_value *value)
|
||||
{
|
||||
enum bshell_opcode opcode = opcode_for_op(value->v_op);
|
||||
if (opcode != BSHELL_OPCODE_NOP) {
|
||||
compile_value_load(ctx, value->v_right);
|
||||
compile_value_load(ctx, value->v_left);
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
opcode_for_op(value->v_op),
|
||||
0);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
switch (value->v_op) {
|
||||
case BSHELL_OP_ASSIGN:
|
||||
compile_value_load(ctx, value->v_right);
|
||||
compile_value_store(ctx, value->v_left);
|
||||
break;
|
||||
default:
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type op_type = {
|
||||
.v_load = op_load,
|
||||
};
|
||||
|
||||
struct compile_result compile_op(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
struct bshell_op_ast_node *op = (struct bshell_op_ast_node *)src;
|
||||
struct compile_value *left = NULL, *right = NULL;
|
||||
left = compile_pop_value(ctx);
|
||||
right = compile_pop_value(ctx);
|
||||
compile_push_op(ctx, src, &op_type, op->n_op->op_id, left, right);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status compile_push_value(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
struct bshell_ast_node *node)
|
||||
{
|
||||
struct compile_value *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
|
||||
item->v_type = type;
|
||||
item->v_node = node;
|
||||
fx_queue_push_back(&ctx->c_stack, &item->v_entry);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status compile_push_value_array(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
struct bshell_ast_node *node,
|
||||
struct compile_value **values,
|
||||
size_t nr_values)
|
||||
{
|
||||
struct compile_value *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
|
||||
item->v_type = type;
|
||||
item->v_node = node;
|
||||
item->v_args = values;
|
||||
item->v_nr_args = nr_values;
|
||||
fx_queue_push_back(&ctx->c_stack, &item->v_entry);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status compile_push_pool_value(
|
||||
struct compile_ctx *ctx,
|
||||
const struct compile_value_type *type,
|
||||
unsigned long pool_slot)
|
||||
{
|
||||
struct compile_value *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
|
||||
item->v_type = type;
|
||||
item->v_pool = pool_slot;
|
||||
fx_queue_push_back(&ctx->c_stack, &item->v_entry);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status compile_push_op(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *node,
|
||||
const struct compile_value_type *type,
|
||||
enum bshell_operator_id op,
|
||||
struct compile_value *left,
|
||||
struct compile_value *right)
|
||||
{
|
||||
struct compile_value *item = malloc(sizeof *item);
|
||||
if (!item) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(item, 0x0, sizeof *item);
|
||||
|
||||
item->v_type = type;
|
||||
item->v_node = node;
|
||||
item->v_op = op;
|
||||
item->v_left = left;
|
||||
item->v_right = right;
|
||||
fx_queue_push_back(&ctx->c_stack, &item->v_entry);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct compile_value *compile_pop_value(struct compile_ctx *ctx)
|
||||
{
|
||||
fx_queue_entry *entry = fx_queue_pop_back(&ctx->c_stack);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_unbox(struct compile_value, entry, v_entry);
|
||||
}
|
||||
|
||||
void compile_value_load(struct compile_ctx *ctx, struct compile_value *item)
|
||||
{
|
||||
if (item->v_type->v_load) {
|
||||
item->v_type->v_load(ctx, item);
|
||||
}
|
||||
}
|
||||
|
||||
void compile_value_store(struct compile_ctx *ctx, struct compile_value *item)
|
||||
{
|
||||
if (item->v_type->v_store) {
|
||||
item->v_type->v_store(ctx, item);
|
||||
}
|
||||
}
|
||||
|
||||
void compile_value_destroy(struct compile_value *value)
|
||||
{
|
||||
if (value->v_left) {
|
||||
compile_value_destroy(value->v_left);
|
||||
}
|
||||
|
||||
if (value->v_right) {
|
||||
compile_value_destroy(value->v_right);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < value->v_nr_args; i++) {
|
||||
compile_value_destroy(value->v_args[i]);
|
||||
}
|
||||
|
||||
if (value->v_args) {
|
||||
free(value->v_args);
|
||||
}
|
||||
|
||||
free(value);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
extern const struct compile_state_type normal_state;
|
||||
static const struct compile_state_type *state_types[] = {
|
||||
[COMPILE_NORMAL] = &normal_state,
|
||||
};
|
||||
static const size_t nr_state_types = sizeof state_types / sizeof state_types[0];
|
||||
|
||||
struct compile_state *compile_push_state(
|
||||
struct compile_ctx *ctx,
|
||||
enum compile_state_type_id state_type_id)
|
||||
{
|
||||
if (state_type_id >= nr_state_types) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct compile_state_type *state_type = state_types
|
||||
[state_type_id];
|
||||
if (!state_type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct compile_state *state = malloc(state_type->s_size);
|
||||
if (!state) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
state->s_type = state_type;
|
||||
state->s_depth = ctx->c_depth;
|
||||
fx_queue_push_back(&ctx->c_state, &state->s_entry);
|
||||
|
||||
if (state_type->s_compile_begin) {
|
||||
state_type->s_compile_begin(ctx);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
void compile_pop_state(struct compile_ctx *ctx)
|
||||
{
|
||||
struct compile_state *state = compile_get_state(ctx);
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fx_queue_prev(&state->s_entry)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (state->s_type->s_compile_end) {
|
||||
state->s_type->s_compile_end(ctx);
|
||||
}
|
||||
|
||||
fx_queue_pop_back(&ctx->c_state);
|
||||
free(state);
|
||||
}
|
||||
|
||||
struct compile_state *compile_get_state(const struct compile_ctx *ctx)
|
||||
{
|
||||
fx_queue_entry *entry = fx_queue_last(&ctx->c_state);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_unbox(struct compile_state, entry, s_entry);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "compile-node.h"
|
||||
|
||||
enum bshell_status var_load(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_var_ast_node *var
|
||||
= (struct bshell_var_ast_node *)value->v_node;
|
||||
unsigned long index = bshell_scriptblock_get_string(
|
||||
ctx->c_block,
|
||||
var->n_ident->tok_str);
|
||||
if (index == POOL_INDEX_INVALID) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_LDLOCAL,
|
||||
index);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status var_store(
|
||||
struct compile_ctx *ctx,
|
||||
struct compile_value *value)
|
||||
{
|
||||
struct bshell_var_ast_node *var
|
||||
= (struct bshell_var_ast_node *)value->v_node;
|
||||
unsigned long index = bshell_scriptblock_get_string(
|
||||
ctx->c_block,
|
||||
var->n_ident->tok_str);
|
||||
if (index == POOL_INDEX_INVALID) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
bshell_scriptblock_push_instruction(
|
||||
ctx->c_block,
|
||||
BSHELL_OPCODE_STLOCAL,
|
||||
index);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const struct compile_value_type var_type = {
|
||||
.v_load = var_load,
|
||||
.v_store = var_store,
|
||||
};
|
||||
|
||||
struct compile_result compile_var(
|
||||
struct compile_ctx *ctx,
|
||||
struct bshell_ast_node *src)
|
||||
{
|
||||
compile_push_value(ctx, &var_type, src);
|
||||
return COMPILE_OK(0);
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
#include <bshell/file.h>
|
||||
#include <bshell/line-source.h>
|
||||
#include <errno.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static enum bshell_status get_name(
|
||||
struct bshell_line_source *src,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read)
|
||||
{
|
||||
struct bshell_file *f = (struct bshell_file *)src;
|
||||
*nr_read = snprintf(buf, count, "%s", f->f_path);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status get_row(
|
||||
struct bshell_line_source *src,
|
||||
size_t row,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read)
|
||||
{
|
||||
struct bshell_file *f = (struct bshell_file *)src;
|
||||
size_t nr_rows = fx_array_get_size(f->f_lines);
|
||||
|
||||
if (row > nr_rows) {
|
||||
return BSHELL_ERR_EOF;
|
||||
}
|
||||
|
||||
fx_value *line_v = fx_array_get_ref(f->f_lines, row - 1);
|
||||
fx_string *line = NULL;
|
||||
fx_value_get_object(line_v, &line);
|
||||
|
||||
const char *line_str = fx_string_get_cstr(line);
|
||||
size_t line_len = fx_string_get_size(line, FX_STRLEN_NORMAL);
|
||||
size_t copy_len = fx_min(ulong, count, line_len);
|
||||
|
||||
memcpy(buf, line_str, copy_len);
|
||||
buf[copy_len] = 0;
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
*nr_read = copy_len;
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status readline(
|
||||
struct bshell_line_source *src,
|
||||
fx_stringstream *out)
|
||||
{
|
||||
struct bshell_file *f = (struct bshell_file *)src;
|
||||
fx_wchar c = FX_WCHAR_INVALID;
|
||||
size_t nr_read = 0;
|
||||
|
||||
while (1) {
|
||||
fx_status status = fx_stream_read_char(f->f_strp, &c);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
|
||||
fx_stream_write_char(out, c);
|
||||
nr_read++;
|
||||
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (nr_read == 0) {
|
||||
return BSHELL_ERR_EOF;
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_file_open(const char *path, struct bshell_file **out)
|
||||
{
|
||||
FILE *fp = fopen(path, "r");
|
||||
if (!fp) {
|
||||
return bshell_status_from_errno(errno);
|
||||
}
|
||||
|
||||
fx_stream *strp = fx_stream_open_fp(fp);
|
||||
|
||||
struct bshell_file *file = malloc(sizeof *file);
|
||||
if (!file) {
|
||||
fclose(fp);
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(file, 0x0, sizeof *file);
|
||||
|
||||
file->f_base.s_get_name = get_name;
|
||||
file->f_base.s_get_row = get_row;
|
||||
file->f_base.s_readline = readline;
|
||||
file->f_fp = fp;
|
||||
file->f_strp = strp;
|
||||
file->f_path = fx_strdup(path);
|
||||
file->f_lines = fx_array_create();
|
||||
|
||||
*out = file;
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
void bshell_file_close(struct bshell_file *file)
|
||||
{
|
||||
fx_stream_unref(file->f_strp);
|
||||
fx_array_unref(file->f_lines);
|
||||
free(file->f_path);
|
||||
fclose(file->f_fp);
|
||||
free(file);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <bshell/format.h>
|
||||
|
||||
static struct bshell_table_column command_table_columns[] = {
|
||||
TABLE_COLUMN("command_type", "command_type", 13),
|
||||
TABLE_COLUMN("name", "name", COL_WIDTH_INFINITE),
|
||||
TABLE_COLUMN("version", "version", 12),
|
||||
TABLE_COLUMN("source", "source", 35),
|
||||
};
|
||||
|
||||
struct bshell_table command_bshell_table = {
|
||||
.fmt_columns = command_table_columns,
|
||||
.fmt_column_count
|
||||
= sizeof command_table_columns / sizeof command_table_columns[0],
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <bshell/format.h>
|
||||
#include <fx/term/print.h>
|
||||
#include <fx/type.h>
|
||||
#include <fx/value-type.h>
|
||||
|
||||
static void format_value_fallback(const fx_value *value, fx_stream *dest)
|
||||
{
|
||||
fx_printf("[yellow]");
|
||||
fx_value_to_string(value, dest, NULL);
|
||||
fx_printf("[reset]\n");
|
||||
}
|
||||
|
||||
enum bshell_status format_value_default(const fx_value *value, fx_stream *dest)
|
||||
{
|
||||
if (fx_type_is_value_type(value->v_type)) {
|
||||
format_value_fallback(value, dest);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
fx_object *object = NULL;
|
||||
fx_value_get_object(value, &object);
|
||||
enum bshell_status status = format_object_table(object, dest);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
format_value_fallback(value, dest);
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <bshell/format.h>
|
||||
|
||||
static struct bshell_table_column hashtable_table_columns[] = {
|
||||
TABLE_COLUMN("key", "key", 37),
|
||||
TABLE_COLUMN("value", "value", COL_WIDTH_INFINITE),
|
||||
};
|
||||
|
||||
struct bshell_table hashtable_bshell_table = {
|
||||
.fmt_columns = hashtable_table_columns,
|
||||
.fmt_column_count
|
||||
= sizeof hashtable_table_columns / sizeof hashtable_table_columns[0],
|
||||
};
|
||||
@@ -0,0 +1,350 @@
|
||||
#include <bshell/format.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/iterator.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/stringstream.h>
|
||||
#include <fx/term/print.h>
|
||||
#include <fx/term/tty.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
static fx_namemap bshell_tables = FX_NAMEMAP_INIT;
|
||||
|
||||
extern struct bshell_table hashtable_bshell_table;
|
||||
extern struct bshell_table verb_bshell_table;
|
||||
extern struct bshell_table command_bshell_table;
|
||||
|
||||
enum bshell_status bshell_table_init(void)
|
||||
{
|
||||
fx_namemap_put(
|
||||
&bshell_tables,
|
||||
"fx.collections.hashtable",
|
||||
&hashtable_bshell_table.fmt_entry);
|
||||
fx_namemap_put(
|
||||
&bshell_tables,
|
||||
"bshell.verb",
|
||||
&verb_bshell_table.fmt_entry);
|
||||
fx_namemap_put(
|
||||
&bshell_tables,
|
||||
"bshell.command",
|
||||
&command_bshell_table.fmt_entry);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
const struct bshell_table *bshell_table_get_for_type(const fx_type *ty)
|
||||
{
|
||||
while (ty) {
|
||||
const char *name = fx_type_get_name(ty);
|
||||
fx_namemap_entry *entry = fx_namemap_get(&bshell_tables, name);
|
||||
if (entry) {
|
||||
return fx_unbox(struct bshell_table, entry, fmt_entry);
|
||||
}
|
||||
|
||||
ty = fx_type_get_parent(ty);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enum bshell_status format_object_table(fx_object *object, fx_stream *dest)
|
||||
{
|
||||
const fx_type *type = fx_type_get_by_id(fx_object_query_type(object));
|
||||
if (!type) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
const struct bshell_table *fmt = bshell_table_get_for_type(type);
|
||||
if (!fmt) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
struct bshell_table_ctx ctx;
|
||||
bshell_table_ctx_init(&ctx, dest);
|
||||
fx_iterator *it = fx_iterator_begin(object);
|
||||
if (!it) {
|
||||
bshell_table_ctx_prepare_columns_from_format(
|
||||
&ctx,
|
||||
fx_type_get_by_id(fx_object_query_type(object)),
|
||||
fmt);
|
||||
bshell_table_ctx_print_headers(&ctx);
|
||||
bshell_table_ctx_print_record(&ctx, &FX_VALUE_OBJECT(object));
|
||||
} else {
|
||||
fx_value first = fx_iterator_get_value(it);
|
||||
if (first.v_type) {
|
||||
bshell_table_ctx_prepare_columns_from_format(
|
||||
&ctx,
|
||||
fx_type_get_by_id(first.v_type),
|
||||
fmt);
|
||||
}
|
||||
bshell_table_ctx_print_headers(&ctx);
|
||||
fx_foreach(value, it)
|
||||
{
|
||||
bshell_table_ctx_print_record(&ctx, &value);
|
||||
}
|
||||
}
|
||||
|
||||
bshell_table_ctx_cleanup(&ctx);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_init(
|
||||
struct bshell_table_ctx *ctx,
|
||||
fx_stream *out)
|
||||
{
|
||||
memset(ctx, 0x0, sizeof *ctx);
|
||||
ctx->ctx_out = out;
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_cleanup(struct bshell_table_ctx *ctx)
|
||||
{
|
||||
if (ctx->ctx_columns) {
|
||||
free(ctx->ctx_columns);
|
||||
}
|
||||
|
||||
memset(ctx, 0x0, sizeof *ctx);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_prepare_columns_from_format(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_type *record_type,
|
||||
const struct bshell_table *fmt)
|
||||
{
|
||||
if (!fmt && !record_type) {
|
||||
return BSHELL_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
if (!fmt) {
|
||||
fmt = bshell_table_get_for_type(record_type);
|
||||
}
|
||||
|
||||
if (!fmt) {
|
||||
return bshell_table_ctx_prepare_columns_from_object(
|
||||
ctx,
|
||||
record_type);
|
||||
}
|
||||
|
||||
ctx->ctx_fmt = fmt;
|
||||
struct bshell_table_ctx_column *columns
|
||||
= calloc(fmt->fmt_column_count, sizeof *columns);
|
||||
|
||||
if (!columns) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
const struct bshell_table_column *column_defs = fmt->fmt_columns;
|
||||
|
||||
ctx->ctx_columns = columns;
|
||||
ctx->ctx_columns_count = fmt->fmt_column_count;
|
||||
|
||||
fx_tty_get_dimensions(fx_stdtty, &ctx->ctx_tty_width, NULL);
|
||||
if (!ctx->ctx_tty_width) {
|
||||
ctx->ctx_tty_width = 150;
|
||||
}
|
||||
|
||||
ctx->ctx_variable_columns_count = 0;
|
||||
|
||||
unsigned int tty_width = ctx->ctx_tty_width;
|
||||
for (size_t i = 0; i < fmt->fmt_column_count; i++) {
|
||||
columns[i].col_property = fx_type_get_property(
|
||||
record_type,
|
||||
column_defs[i].col_property_name);
|
||||
columns[i].col_title = column_defs[i].col_display_name;
|
||||
columns[i].col_title_ptr = column_defs[i].col_display_name;
|
||||
|
||||
if (column_defs[i].col_width == COL_WIDTH_INFINITE) {
|
||||
ctx->ctx_variable_columns_count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (column_defs[i].col_width <= tty_width) {
|
||||
columns[i].col_width = column_defs[i].col_width;
|
||||
tty_width -= columns[i].col_width;
|
||||
continue;
|
||||
}
|
||||
|
||||
columns[i].col_width = tty_width;
|
||||
tty_width = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < fmt->fmt_column_count; i++) {
|
||||
if (column_defs[i].col_width == COL_WIDTH_INFINITE) {
|
||||
columns[i].col_width
|
||||
= tty_width / ctx->ctx_variable_columns_count;
|
||||
}
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_prepare_columns_from_object(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_type *record_type)
|
||||
{
|
||||
fx_array *properties = fx_array_create();
|
||||
|
||||
fx_iterator *it = fx_type_get_properties(record_type);
|
||||
fx_foreach(prop_v, it)
|
||||
{
|
||||
fx_array_push_back(properties, prop_v);
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
size_t column_count = fx_array_get_size(properties);
|
||||
if (!column_count) {
|
||||
fx_array_unref(properties);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_table_ctx_column *columns
|
||||
= calloc(column_count, sizeof *columns);
|
||||
|
||||
if (!columns) {
|
||||
fx_array_unref(properties);
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
ctx->ctx_columns = columns;
|
||||
ctx->ctx_columns_count = column_count;
|
||||
|
||||
fx_tty_get_dimensions(fx_stdtty, &ctx->ctx_tty_width, NULL);
|
||||
if (!ctx->ctx_tty_width) {
|
||||
ctx->ctx_tty_width = 150;
|
||||
}
|
||||
|
||||
ctx->ctx_variable_columns_count = 0;
|
||||
|
||||
unsigned int tty_width = ctx->ctx_tty_width;
|
||||
|
||||
for (size_t i = 0; i < column_count; i++) {
|
||||
fx_value *prop_v = fx_array_get_ref(properties, i);
|
||||
fx_property *prop = NULL;
|
||||
fx_value_get_object(prop_v, &prop);
|
||||
columns[i].col_property = prop;
|
||||
columns[i].col_title = fx_property_get_name(prop);
|
||||
columns[i].col_title_ptr = columns[i].col_title;
|
||||
|
||||
columns[i].col_width = tty_width / column_count;
|
||||
}
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_print_headers(struct bshell_table_ctx *ctx)
|
||||
{
|
||||
fx_printf("[bold,bright_green]");
|
||||
bool repeat = false;
|
||||
struct bshell_table_ctx_column *c = NULL;
|
||||
|
||||
do {
|
||||
repeat = false;
|
||||
for (size_t i = 0; i < ctx->ctx_columns_count; i++) {
|
||||
c = &ctx->ctx_columns[i];
|
||||
if (c->col_width == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t title_chars_remaining = strlen(c->col_title_ptr);
|
||||
size_t to_write = title_chars_remaining;
|
||||
if (to_write > c->col_width) {
|
||||
to_write = c->col_width;
|
||||
}
|
||||
|
||||
size_t x;
|
||||
for (x = 0; x < to_write; x++) {
|
||||
fx_stream_write_char(
|
||||
ctx->ctx_out,
|
||||
*c->col_title_ptr);
|
||||
c->col_title_ptr++;
|
||||
}
|
||||
|
||||
for (; x < c->col_width; x++) {
|
||||
fx_stream_write_char(ctx->ctx_out, ' ');
|
||||
}
|
||||
|
||||
if (to_write != title_chars_remaining) {
|
||||
repeat = true;
|
||||
}
|
||||
}
|
||||
} while (repeat);
|
||||
|
||||
fx_stream_write_char(ctx->ctx_out, '\n');
|
||||
|
||||
for (size_t i = 0; i < ctx->ctx_columns_count; i++) {
|
||||
struct bshell_table_ctx_column *c = &ctx->ctx_columns[i];
|
||||
if (c->col_width == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t title_len = strlen(c->col_title);
|
||||
if (title_len > c->col_width) {
|
||||
title_len = c->col_width;
|
||||
}
|
||||
|
||||
size_t x;
|
||||
for (x = 0; x < title_len; x++) {
|
||||
fx_stream_write_char(ctx->ctx_out, '-');
|
||||
}
|
||||
|
||||
for (; x < c->col_width; x++) {
|
||||
fx_stream_write_char(ctx->ctx_out, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
fx_printf("[reset]");
|
||||
fx_stream_write_char(ctx->ctx_out, '\n');
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_table_ctx_print_record(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_value *record)
|
||||
{
|
||||
fx_stringstream *strm = fx_stringstream_create();
|
||||
for (size_t i = 0; i < ctx->ctx_columns_count; i++) {
|
||||
struct bshell_table_ctx_column *c = &ctx->ctx_columns[i];
|
||||
if (c->col_width == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fx_stringstream_reset(strm);
|
||||
fx_value column_value = FX_VALUE_EMPTY;
|
||||
if (c->col_property) {
|
||||
fx_status status = fx_property_get_value(
|
||||
c->col_property,
|
||||
record,
|
||||
&column_value);
|
||||
if (FX_OK(status)) {
|
||||
fx_value_to_string(&column_value, strm, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
size_t to_write = fx_stringstream_get_length(strm);
|
||||
const char *s = fx_stringstream_ptr(strm);
|
||||
bool overflow = false;
|
||||
if (to_write > c->col_width) {
|
||||
to_write = c->col_width - 2;
|
||||
overflow = true;
|
||||
}
|
||||
|
||||
size_t x;
|
||||
for (x = 0; x < to_write; x++) {
|
||||
fx_stream_write_char(ctx->ctx_out, s[x]);
|
||||
}
|
||||
|
||||
if (overflow) {
|
||||
fx_stream_write_char(ctx->ctx_out, L'…');
|
||||
x++;
|
||||
}
|
||||
|
||||
for (; x < c->col_width; x++) {
|
||||
fx_stream_write_char(ctx->ctx_out, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
fx_stream_write_char(ctx->ctx_out, '\n');
|
||||
fx_stringstream_unref(strm);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <bshell/format.h>
|
||||
|
||||
static struct bshell_table_column verb_table_columns[] = {
|
||||
TABLE_COLUMN("verb", "verb", 13),
|
||||
TABLE_COLUMN("alias_prefix", "alias_prefix", 13),
|
||||
TABLE_COLUMN("group", "group", 13),
|
||||
TABLE_COLUMN("description", "description", COL_WIDTH_INFINITE),
|
||||
};
|
||||
|
||||
struct bshell_table verb_bshell_table = {
|
||||
.fmt_columns = verb_table_columns,
|
||||
.fmt_column_count = sizeof verb_table_columns
|
||||
/ sizeof verb_table_columns[0],
|
||||
};
|
||||
@@ -0,0 +1,232 @@
|
||||
#ifndef BSHELL_AST_H_
|
||||
#define BSHELL_AST_H_
|
||||
|
||||
#include <bshell/status.h>
|
||||
#include <fx/bstr.h>
|
||||
#include <fx/queue.h>
|
||||
|
||||
struct bshell_lex_token;
|
||||
|
||||
#define BSHELL_AST_ITERATE_OK(flags) \
|
||||
((struct bshell_ast_iterate_result) {.r_status = BSHELL_SUCCESS, \
|
||||
.r_flags = (flags)})
|
||||
#define BSHELL_AST_ITERATE_ERR(status) \
|
||||
((struct bshell_ast_iterate_result) {.r_status = (status)})
|
||||
|
||||
enum bshell_ast_node_type {
|
||||
BSHELL_AST_NONE = 0x00u,
|
||||
BSHELL_AST_NULL,
|
||||
BSHELL_AST_STMT_LIST,
|
||||
BSHELL_AST_INT,
|
||||
BSHELL_AST_DOUBLE,
|
||||
BSHELL_AST_WORD,
|
||||
BSHELL_AST_STRING,
|
||||
BSHELL_AST_FSTRING,
|
||||
BSHELL_AST_VAR,
|
||||
BSHELL_AST_VAR_SPLAT,
|
||||
BSHELL_AST_FLAG,
|
||||
BSHELL_AST_CMDCALL,
|
||||
BSHELL_AST_FUNCALL,
|
||||
BSHELL_AST_PIPELINE,
|
||||
BSHELL_AST_REDIRECTION,
|
||||
BSHELL_AST_BLOCK,
|
||||
BSHELL_AST_FUNC,
|
||||
BSHELL_AST_ARRAY,
|
||||
BSHELL_AST_HASHTABLE,
|
||||
BSHELL_AST_HASHTABLE_ITEM,
|
||||
BSHELL_AST_OP,
|
||||
BSHELL_AST_IF,
|
||||
BSHELL_AST_IF_BRANCH,
|
||||
};
|
||||
|
||||
struct bshell_ast_iterator_entry {
|
||||
fx_queue_entry e_entry;
|
||||
unsigned long e_depth;
|
||||
};
|
||||
|
||||
struct bshell_ast_node {
|
||||
enum bshell_ast_node_type n_type;
|
||||
struct bshell_ast_node *n_parent;
|
||||
fx_queue_entry n_entry;
|
||||
struct bshell_ast_iterator_entry n_it;
|
||||
};
|
||||
|
||||
struct bshell_null_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
};
|
||||
|
||||
struct bshell_int_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_value;
|
||||
};
|
||||
|
||||
struct bshell_double_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_value;
|
||||
};
|
||||
|
||||
struct bshell_word_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_value;
|
||||
};
|
||||
|
||||
struct bshell_string_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_value;
|
||||
};
|
||||
|
||||
struct bshell_fstring_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_elements;
|
||||
};
|
||||
|
||||
struct bshell_var_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_ident;
|
||||
};
|
||||
|
||||
struct bshell_var_splat_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_ident;
|
||||
};
|
||||
|
||||
struct bshell_cmdcall_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_args;
|
||||
fx_queue n_redirect;
|
||||
};
|
||||
|
||||
struct bshell_funcall_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_ast_node *n_func;
|
||||
fx_queue n_args;
|
||||
};
|
||||
|
||||
struct bshell_pipeline_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_stages;
|
||||
};
|
||||
|
||||
struct bshell_redirection_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
bool n_append : 1;
|
||||
bool n_out_is_fd : 1;
|
||||
bool n_out_is_expr : 1;
|
||||
|
||||
unsigned int n_in, n_out;
|
||||
struct bshell_ast_node *n_out_path_expr;
|
||||
const char *n_out_path;
|
||||
struct bshell_lex_token *n_out_tok;
|
||||
};
|
||||
|
||||
struct bshell_stmt_list_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_statements;
|
||||
};
|
||||
|
||||
struct bshell_block_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_statements;
|
||||
};
|
||||
|
||||
struct bshell_func_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_lex_token *n_name;
|
||||
fx_queue n_params;
|
||||
struct bshell_ast_node *n_body;
|
||||
};
|
||||
|
||||
struct bshell_array_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_items;
|
||||
};
|
||||
|
||||
struct bshell_hashtable_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_items;
|
||||
};
|
||||
|
||||
struct bshell_hashtable_item_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_ast_node *n_key, *n_value;
|
||||
};
|
||||
|
||||
struct bshell_op_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
const struct bshell_operator_info *n_op;
|
||||
struct bshell_ast_node *n_left, *n_right;
|
||||
};
|
||||
|
||||
struct bshell_if_branch_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
struct bshell_ast_node *n_cond;
|
||||
struct bshell_ast_node *n_body;
|
||||
};
|
||||
|
||||
struct bshell_if_ast_node {
|
||||
struct bshell_ast_node n_base;
|
||||
fx_queue n_branches;
|
||||
};
|
||||
|
||||
struct bshell_ast_iterator {
|
||||
struct bshell_ast_node *it_cur;
|
||||
fx_queue it_queue;
|
||||
unsigned int it_depth;
|
||||
fx_queue_entry *it_insert_after;
|
||||
};
|
||||
|
||||
struct bshell_ast_iterate_result {
|
||||
enum bshell_status r_status;
|
||||
enum {
|
||||
BSHELL_AST_ITERATE_CONTINUE = 0x00u,
|
||||
BSHELL_AST_ITERATE_STOP = 0x01u,
|
||||
BSHELL_AST_ITERATE_ADD_CHILDREN = 0x02u,
|
||||
BSHELL_AST_ITERATE_REPEAT = 0x04u,
|
||||
} r_flags;
|
||||
};
|
||||
|
||||
enum bshell_ast_iteration_type {
|
||||
BSHELL_AST_ITERATION_PRE,
|
||||
BSHELL_AST_ITERATION_POST,
|
||||
};
|
||||
|
||||
typedef struct bshell_ast_iterate_result (*bshell_ast_iterator_callback)(
|
||||
struct bshell_ast_node *,
|
||||
enum bshell_ast_iteration_type,
|
||||
struct bshell_ast_iterator *,
|
||||
void *);
|
||||
|
||||
struct bshell_ast_node_definition {
|
||||
enum bshell_ast_node_type def_id;
|
||||
size_t def_node_size;
|
||||
enum bshell_status (*def_collect_children)(
|
||||
struct bshell_ast_node *,
|
||||
struct bshell_ast_iterator *);
|
||||
enum bshell_status (*def_cleanup)(struct bshell_ast_node *);
|
||||
void (*def_to_string)(const struct bshell_ast_node *, fx_bstr *);
|
||||
};
|
||||
|
||||
extern struct bshell_ast_node *bshell_ast_node_create(
|
||||
enum bshell_ast_node_type type);
|
||||
extern void bshell_ast_node_destroy(struct bshell_ast_node *node);
|
||||
extern void bshell_ast_node_to_string(
|
||||
const struct bshell_ast_node *node,
|
||||
fx_bstr *out);
|
||||
extern enum bshell_status bshell_ast_node_iterate(
|
||||
struct bshell_ast_node *node,
|
||||
struct bshell_ast_iterator *it,
|
||||
bshell_ast_iterator_callback callback,
|
||||
void *arg);
|
||||
|
||||
extern const char *bshell_ast_node_type_to_string(
|
||||
enum bshell_ast_node_type type);
|
||||
|
||||
extern struct bshell_ast_node *bshell_ast_iterator_peek(
|
||||
struct bshell_ast_iterator *it);
|
||||
extern struct bshell_ast_node *bshell_ast_iterator_dequeue(
|
||||
struct bshell_ast_iterator *it);
|
||||
extern void bshell_ast_iterator_enqueue(
|
||||
struct bshell_ast_iterator *it,
|
||||
struct bshell_ast_node *node);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef BSHELL_COMMAND_ALIAS_H_
|
||||
#define BSHELL_COMMAND_ALIAS_H_
|
||||
|
||||
#include "../verb.h"
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/string.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_ALIAS (bshell_alias_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_alias);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_alias)
|
||||
const char *a_callable_name;
|
||||
const char *a_target_name;
|
||||
const char *(*a_get_callable_name)(const bshell_alias *);
|
||||
const char *(*a_get_target_name)(const bshell_alias *);
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_alias)
|
||||
|
||||
extern fx_type_id bshell_alias_get_type(void);
|
||||
|
||||
extern enum bshell_status bshell_alias_get_callable_name(
|
||||
const bshell_alias *alias,
|
||||
fx_string *out);
|
||||
extern enum bshell_status bshell_alias_get_target_name(
|
||||
const bshell_alias *alias,
|
||||
fx_string *out);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef BSHELL_COMMAND_CMDLET_H_
|
||||
#define BSHELL_COMMAND_CMDLET_H_
|
||||
|
||||
#include "../verb.h"
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_CMDLET (bshell_cmdlet_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_cmdlet);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_cmdlet)
|
||||
enum bshell_verb_id c_verb;
|
||||
const char *c_noun;
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_cmdlet)
|
||||
|
||||
extern fx_type_id bshell_cmdlet_get_type(void);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef BSHELL_COMMAND_COMMAND_H_
|
||||
#define BSHELL_COMMAND_COMMAND_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_COMMAND (bshell_command_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_command);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_command)
|
||||
const char *c_command_type;
|
||||
enum bshell_status (
|
||||
*c_get_callable_name)(const bshell_command *, fx_string *);
|
||||
enum bshell_status (
|
||||
*c_get_callable_name_static)(const fx_type *, fx_string *);
|
||||
enum bshell_status (
|
||||
*c_get_description)(const bshell_command *, fx_string *);
|
||||
enum bshell_status (
|
||||
*c_get_description_static)(const fx_type *, fx_string *);
|
||||
enum bshell_status (*c_begin_processing)(bshell_command *);
|
||||
enum bshell_status (*c_process_record)(
|
||||
bshell_command *,
|
||||
FX_TYPE_FWDREF(bshell_pipeline) * pipeline,
|
||||
struct bshell_runtime *);
|
||||
enum bshell_status (*c_end_processing)(bshell_command *);
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_command)
|
||||
|
||||
extern fx_type_id bshell_command_get_type(void);
|
||||
|
||||
extern bshell_command *bshell_command_find_static(const char *callable_name);
|
||||
|
||||
extern void bshell_command_set_args(
|
||||
bshell_command *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args);
|
||||
extern void bshell_command_set_args_reverse(
|
||||
bshell_command *cmd,
|
||||
fx_value *args,
|
||||
size_t nr_args);
|
||||
extern const fx_value *bshell_command_get_arg(
|
||||
bshell_command *cmd,
|
||||
size_t index);
|
||||
|
||||
extern enum bshell_status bshell_command_begin_processing(
|
||||
bshell_command *command);
|
||||
extern enum bshell_status bshell_command_process_record(
|
||||
bshell_command *command,
|
||||
FX_TYPE_FWDREF(bshell_pipeline) * pipeline,
|
||||
struct bshell_runtime *rt);
|
||||
extern enum bshell_status bshell_command_end_processing(
|
||||
bshell_command *command);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef BSHELL_COMMAND_FUNCTION_H_
|
||||
#define BSHELL_COMMAND_FUNCTION_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_FUNCTION (bshell_function_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_function);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_function)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_function)
|
||||
|
||||
extern fx_type_id bshell_function_get_type(void);
|
||||
|
||||
extern bshell_function *bshell_function_create(const char *name);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef BSHELL_COMPILE_H_
|
||||
#define BSHELL_COMPILE_H_
|
||||
|
||||
#include <bshell/runtime/script-block.h>
|
||||
|
||||
struct bshell_ast_node;
|
||||
|
||||
extern enum bshell_status bshell_compile(
|
||||
struct bshell_ast_node *src,
|
||||
bshell_scriptblock *dest);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef BSHELL_FILE_H_
|
||||
#define BSHELL_FILE_H_
|
||||
|
||||
#include <bshell/line-source.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct bshell_file {
|
||||
struct bshell_line_source f_base;
|
||||
fx_array *f_lines;
|
||||
char *f_path;
|
||||
fx_stream *f_strp;
|
||||
FILE *f_fp;
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_file_open(
|
||||
const char *path,
|
||||
struct bshell_file **out);
|
||||
extern void bshell_file_close(struct bshell_file *file);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
#ifndef BSHELL_FORMAT_H_
|
||||
#define BSHELL_FORMAT_H_
|
||||
|
||||
#include <bshell/status.h>
|
||||
#include <fx/namemap.h>
|
||||
#include <fx/object.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/reflection/property.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/stream.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
#define TABLE_COLUMN(display_name, property_name, width) \
|
||||
{ \
|
||||
.col_display_name = (display_name), \
|
||||
.col_property_name = (property_name), \
|
||||
.col_width = (width), \
|
||||
}
|
||||
#define COL_WIDTH_INFINITE ((size_t)-1)
|
||||
|
||||
struct bshell_table_column {
|
||||
const char *col_display_name;
|
||||
const char *col_property_name;
|
||||
size_t col_width;
|
||||
};
|
||||
|
||||
struct bshell_table {
|
||||
fx_namemap_entry fmt_entry;
|
||||
const struct bshell_table_column *fmt_columns;
|
||||
size_t fmt_column_count;
|
||||
};
|
||||
|
||||
struct bshell_table_ctx_column {
|
||||
const char *col_title;
|
||||
size_t col_width;
|
||||
size_t col_title_chars_written;
|
||||
const char *col_title_ptr;
|
||||
const fx_property *col_property;
|
||||
};
|
||||
|
||||
struct bshell_table_ctx {
|
||||
const struct bshell_table *ctx_fmt;
|
||||
struct bshell_table_ctx_column *ctx_columns;
|
||||
size_t ctx_columns_count;
|
||||
unsigned int ctx_tty_width;
|
||||
size_t ctx_variable_columns_count;
|
||||
fx_stream *ctx_out;
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_table_init(void);
|
||||
extern const struct bshell_table *bshell_table_get_for_type(const fx_type *ty);
|
||||
extern enum bshell_status format_object_table(
|
||||
fx_object *object,
|
||||
fx_stream *dest);
|
||||
extern enum bshell_status format_value_default(
|
||||
const fx_value *value,
|
||||
fx_stream *dest);
|
||||
|
||||
extern enum bshell_status bshell_table_ctx_init(
|
||||
struct bshell_table_ctx *ctx,
|
||||
fx_stream *out);
|
||||
extern enum bshell_status bshell_table_ctx_cleanup(
|
||||
struct bshell_table_ctx *ctx);
|
||||
extern enum bshell_status bshell_table_ctx_prepare_columns_from_format(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_type *record_type,
|
||||
const struct bshell_table *fmt);
|
||||
extern enum bshell_status bshell_table_ctx_prepare_columns_from_object(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_type *record_type);
|
||||
extern enum bshell_status bshell_table_ctx_print_headers(
|
||||
struct bshell_table_ctx *ctx);
|
||||
extern enum bshell_status bshell_table_ctx_print_record(
|
||||
struct bshell_table_ctx *ctx,
|
||||
const fx_value *record);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef BSHELL_LINE_SOURCE_H_
|
||||
#define BSHELL_LINE_SOURCE_H_
|
||||
|
||||
#include "status.h"
|
||||
|
||||
#include <fx/stringstream.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct bshell_line_source {
|
||||
enum bshell_status (*s_get_name)(
|
||||
struct bshell_line_source *,
|
||||
char *,
|
||||
size_t,
|
||||
size_t *);
|
||||
enum bshell_status (
|
||||
*s_readline)(struct bshell_line_source *, fx_stringstream *);
|
||||
enum bshell_status (*s_get_row)(
|
||||
struct bshell_line_source *,
|
||||
size_t,
|
||||
char *,
|
||||
size_t,
|
||||
size_t *);
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_line_source_get_name(
|
||||
struct bshell_line_source *src,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read);
|
||||
extern enum bshell_status bshell_line_source_readline(
|
||||
struct bshell_line_source *src,
|
||||
fx_stringstream *out);
|
||||
extern enum bshell_status bshell_line_source_get_row(
|
||||
struct bshell_line_source *src,
|
||||
size_t row,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef BSHELL_PARSE_LEX_H_
|
||||
#define BSHELL_PARSE_LEX_H_
|
||||
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/stringstream.h>
|
||||
|
||||
#define BSHELL_LEX_STATE_MAX_TERMINATORS 16
|
||||
|
||||
struct bshell_line_source;
|
||||
|
||||
enum bshell_lex_flags {
|
||||
LEX_PRINT_TOKENS = 0x01u,
|
||||
};
|
||||
|
||||
enum bshell_lex_token_flags {
|
||||
/* a token with this flag not only interrupts the word currently being
|
||||
* scanned, but also stops multi-words */
|
||||
BSHELL_LEX_TOKEN_TERMINATES_WORD = 0x01u,
|
||||
/* a token with this flag can appear at the start of an arithmetic
|
||||
* expression. a statement that encounters this token as its first char
|
||||
* will switch to arithmetic mode */
|
||||
BSHELL_LEX_TOKEN_UNARY_ARITHMETIC = 0x02u,
|
||||
/* if a token has this flag defined, the lexer will
|
||||
* switch to command mode after encountering it. */
|
||||
BSHELL_LEX_TOKEN_COMMAND_MODE = 0x08u,
|
||||
/* if a token has this flag defined, the lexer will
|
||||
* switch to statement mode after encountering it. */
|
||||
BSHELL_LEX_TOKEN_STATEMENT_MODE = 0x10u,
|
||||
};
|
||||
|
||||
enum bshell_lex_state_id {
|
||||
BSHELL_LEX_STATE_STATEMENT = 0x01u,
|
||||
BSHELL_LEX_STATE_COMMAND = 0x02u,
|
||||
BSHELL_LEX_STATE_ARITHMETIC = 0x04u,
|
||||
BSHELL_LEX_STATE_STRING = 0x08u,
|
||||
BSHELL_LEX_STATE_WORD = 0x10u,
|
||||
BSHELL_LEX_STATE_HASHTABLE = 0x20u,
|
||||
};
|
||||
|
||||
struct bshell_lex_token_definition {
|
||||
int id;
|
||||
const char *name;
|
||||
uint64_t name_hash;
|
||||
enum bshell_lex_state_id enabled_states;
|
||||
enum bshell_lex_token_flags flags;
|
||||
};
|
||||
|
||||
struct bshell_lex_symbol_node {
|
||||
char s_char;
|
||||
struct bshell_lex_token_definition *s_def;
|
||||
|
||||
fx_queue_entry s_entry;
|
||||
fx_queue s_children;
|
||||
};
|
||||
|
||||
struct bshell_lex_state {
|
||||
const struct bshell_lex_state_type *s_type;
|
||||
unsigned int s_terminators[BSHELL_LEX_STATE_MAX_TERMINATORS];
|
||||
unsigned int s_nr_terminators;
|
||||
unsigned int s_paren_depth;
|
||||
fx_queue_entry s_entry;
|
||||
fx_string *s_tempstr;
|
||||
unsigned int s_flags;
|
||||
};
|
||||
|
||||
struct bshell_lex_ctx {
|
||||
enum bshell_lex_flags lex_flags;
|
||||
fx_queue lex_tokens;
|
||||
struct bshell_line_source *lex_src;
|
||||
fx_stringstream *lex_buf;
|
||||
fx_string *lex_tmp;
|
||||
fx_wchar lex_ch;
|
||||
fx_queue lex_state;
|
||||
void (*lex_token_scanned)(
|
||||
struct bshell_lex_ctx *,
|
||||
struct bshell_lex_token *);
|
||||
enum bshell_lex_token_type lex_prev_token;
|
||||
struct bshell_char_cell lex_cursor, lex_start, lex_end;
|
||||
struct bshell_lex_symbol_node *lex_sym_tree;
|
||||
enum bshell_status lex_status;
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_lex_ctx_init(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_flags flags,
|
||||
struct bshell_line_source *src);
|
||||
extern enum bshell_status bshell_lex_ctx_cleanup(struct bshell_lex_ctx *ctx);
|
||||
|
||||
extern struct bshell_lex_token *bshell_lex_ctx_peek(struct bshell_lex_ctx *ctx);
|
||||
extern struct bshell_lex_token *bshell_lex_ctx_claim(
|
||||
struct bshell_lex_ctx *ctx);
|
||||
extern void bshell_lex_ctx_discard(struct bshell_lex_ctx *ctx);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef BSHELL_PARSE_PARSE_H_
|
||||
#define BSHELL_PARSE_PARSE_H_
|
||||
|
||||
#include <bshell/status.h>
|
||||
|
||||
struct bshell_lex_ctx;
|
||||
struct bshell_ast_node;
|
||||
|
||||
struct bshell_parse_ctx {
|
||||
struct bshell_lex_ctx *p_src;
|
||||
enum bshell_status p_status;
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_parse_ctx_init(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_lex_ctx *src);
|
||||
extern void bshell_parse_ctx_cleanup(struct bshell_parse_ctx *ctx);
|
||||
|
||||
extern struct bshell_ast_node *bshell_parse_ctx_read_node(
|
||||
struct bshell_parse_ctx *ctx);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,190 @@
|
||||
#ifndef BSHELL_PARSE_TOKEN_H_
|
||||
#define BSHELL_PARSE_TOKEN_H_
|
||||
|
||||
#include <fx/queue.h>
|
||||
#include <fx/value.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct bshell_char_cell {
|
||||
unsigned long c_row, c_col;
|
||||
};
|
||||
|
||||
enum bshell_lex_token_type {
|
||||
BSHELL_TOK_NONE = 0,
|
||||
__BSHELL_TOK_INDEX_BASE = 100,
|
||||
BSHELL_TOK_KEYWORD,
|
||||
BSHELL_TOK_SYMBOL,
|
||||
BSHELL_TOK_INT,
|
||||
BSHELL_TOK_DOUBLE,
|
||||
BSHELL_TOK_WORD,
|
||||
BSHELL_TOK_WORD_START,
|
||||
BSHELL_TOK_WORD_END,
|
||||
BSHELL_TOK_FLAG,
|
||||
BSHELL_TOK_OPERATOR,
|
||||
BSHELL_TOK_VAR,
|
||||
BSHELL_TOK_VAR_SPLAT,
|
||||
BSHELL_TOK_STRING,
|
||||
BSHELL_TOK_STR_START,
|
||||
BSHELL_TOK_STR_END,
|
||||
BSHELL_TOK_LINEFEED,
|
||||
__BSHELL_TOK_INDEX_LIMIT,
|
||||
};
|
||||
|
||||
enum bshell_lex_keyword {
|
||||
BSHELL_KW_NONE = 0,
|
||||
__BSHELL_KW_INDEX_BASE = 200,
|
||||
BSHELL_KW_FUNC,
|
||||
BSHELL_KW_IF,
|
||||
BSHELL_KW_ELSEIF,
|
||||
BSHELL_KW_ELSE,
|
||||
__BSHELL_KW_INDEX_LIMIT,
|
||||
};
|
||||
|
||||
enum bshell_lex_operator {
|
||||
BSHELL_TKOP_NONE = 0,
|
||||
__BSHELL_TKOP_INDEX_BASE = 300,
|
||||
BSHELL_TKOP_F,
|
||||
BSHELL_TKOP_BAND,
|
||||
BSHELL_TKOP_BOR,
|
||||
BSHELL_TKOP_BXOR,
|
||||
BSHELL_TKOP_BNOT,
|
||||
BSHELL_TKOP_SHL,
|
||||
BSHELL_TKOP_SHR,
|
||||
BSHELL_TKOP_EQ,
|
||||
BSHELL_TKOP_NE,
|
||||
BSHELL_TKOP_GT,
|
||||
BSHELL_TKOP_LT,
|
||||
BSHELL_TKOP_GE,
|
||||
BSHELL_TKOP_LE,
|
||||
BSHELL_TKOP_MATCH,
|
||||
BSHELL_TKOP_NOTMATCH,
|
||||
BSHELL_TKOP_REPLACE,
|
||||
BSHELL_TKOP_LIKE,
|
||||
BSHELL_TKOP_NOTLIKE,
|
||||
BSHELL_TKOP_IN,
|
||||
BSHELL_TKOP_NOTIN,
|
||||
BSHELL_TKOP_CONTAINS,
|
||||
BSHELL_TKOP_NOTCONTAINS,
|
||||
BSHELL_TKOP_AND,
|
||||
BSHELL_TKOP_OR,
|
||||
BSHELL_TKOP_XOR,
|
||||
BSHELL_TKOP_NOT,
|
||||
BSHELL_TKOP_SPLIT,
|
||||
BSHELL_TKOP_JOIN,
|
||||
BSHELL_TKOP_IS,
|
||||
BSHELL_TKOP_ISNOT,
|
||||
BSHELL_TKOP_AS,
|
||||
__BSHELL_TKOP_INDEX_LIMIT,
|
||||
};
|
||||
|
||||
enum bshell_lex_symbol {
|
||||
BSHELL_SYM_NONE = 0,
|
||||
__BSHELL_SYM_INDEX_BASE = 400,
|
||||
BSHELL_SYM_BANG,
|
||||
BSHELL_SYM_PLUS,
|
||||
BSHELL_SYM_HYPHEN,
|
||||
BSHELL_SYM_FORWARD_SLASH,
|
||||
BSHELL_SYM_ASTERISK,
|
||||
BSHELL_SYM_AMPERSAND,
|
||||
BSHELL_SYM_PERCENT,
|
||||
BSHELL_SYM_SQUOTE,
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_HASH,
|
||||
BSHELL_SYM_COLON_COLON,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_COMMA,
|
||||
BSHELL_SYM_DOLLAR,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_SYM_DOT,
|
||||
BSHELL_SYM_DOT_DOT,
|
||||
BSHELL_SYM_PIPE,
|
||||
BSHELL_SYM_AT,
|
||||
BSHELL_SYM_AT_LEFT_PAREN,
|
||||
BSHELL_SYM_AT_LEFT_BRACE,
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_LEFT_BRACKET,
|
||||
BSHELL_SYM_RIGHT_BRACKET,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_RIGHT_PAREN,
|
||||
BSHELL_SYM_EQUAL,
|
||||
BSHELL_SYM_PLUS_EQUAL,
|
||||
BSHELL_SYM_HYPHEN_EQUAL,
|
||||
BSHELL_SYM_ASTERISK_EQUAL,
|
||||
BSHELL_SYM_FORWARD_SLASH_EQUAL,
|
||||
BSHELL_SYM_PERCENT_EQUAL,
|
||||
BSHELL_SYM_QUESTION_DOT,
|
||||
BSHELL_SYM_QUESTION_LEFT_BRACKET,
|
||||
__BSHELL_SYM_INDEX_LIMIT,
|
||||
};
|
||||
|
||||
struct bshell_lex_token {
|
||||
enum bshell_lex_token_type tok_type;
|
||||
|
||||
struct bshell_char_cell tok_start, tok_end;
|
||||
|
||||
fx_queue_entry tok_entry;
|
||||
|
||||
union {
|
||||
enum bshell_lex_keyword tok_keyword;
|
||||
enum bshell_lex_symbol tok_symbol;
|
||||
enum bshell_lex_operator tok_operator;
|
||||
fx_value tok_number;
|
||||
char *tok_str;
|
||||
};
|
||||
};
|
||||
|
||||
extern struct bshell_lex_token *bshell_lex_token_create(
|
||||
enum bshell_lex_token_type type);
|
||||
extern struct bshell_lex_token *bshell_lex_token_create_with_string(
|
||||
enum bshell_lex_token_type type,
|
||||
const char *s);
|
||||
extern void bshell_lex_token_destroy(struct bshell_lex_token *tok);
|
||||
|
||||
extern struct bshell_lex_token *bshell_lex_token_change_type(
|
||||
struct bshell_lex_token *tok,
|
||||
enum bshell_lex_token_type new_type);
|
||||
extern void bshell_lex_token_change_string(
|
||||
struct bshell_lex_token *tok,
|
||||
const char *s);
|
||||
|
||||
static inline bool bshell_lex_token_is_symbol(
|
||||
struct bshell_lex_token *tok,
|
||||
enum bshell_lex_symbol sym)
|
||||
{
|
||||
return (tok->tok_type == BSHELL_TOK_SYMBOL && tok->tok_symbol == sym);
|
||||
}
|
||||
static inline bool bshell_lex_token_is_keyword(
|
||||
struct bshell_lex_token *tok,
|
||||
enum bshell_lex_keyword kw)
|
||||
{
|
||||
return (tok->tok_type == BSHELL_TOK_KEYWORD && tok->tok_keyword == kw);
|
||||
}
|
||||
static inline bool bshell_lex_token_type_has_string_value(
|
||||
enum bshell_lex_token_type type)
|
||||
{
|
||||
switch (type) {
|
||||
case BSHELL_TOK_WORD:
|
||||
case BSHELL_TOK_STRING:
|
||||
case BSHELL_TOK_FLAG:
|
||||
case BSHELL_TOK_VAR:
|
||||
case BSHELL_TOK_VAR_SPLAT:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
static inline bool bshell_lex_token_has_string_value(
|
||||
const struct bshell_lex_token *tok)
|
||||
{
|
||||
return bshell_lex_token_type_has_string_value(tok->tok_type);
|
||||
}
|
||||
|
||||
extern const char *bshell_token_type_to_string(enum bshell_lex_token_type type);
|
||||
extern const char *bshell_lex_keyword_to_string(
|
||||
enum bshell_lex_keyword keyword);
|
||||
extern const char *bshell_lex_symbol_to_string(enum bshell_lex_symbol sym);
|
||||
extern const char *bshell_lex_operator_to_string(enum bshell_lex_operator op);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef BSHELL_RUNTIME_CMDCALL_H_
|
||||
#define BSHELL_RUNTIME_CMDCALL_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_CMDCALL (bshell_cmdcall_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_cmdcall);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_cmdcall)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_cmdcall)
|
||||
|
||||
extern fx_type_id bshell_cmdcall_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(bshell_cmdcall, BSHELL_TYPE_CMDCALL);
|
||||
|
||||
extern enum bshell_status bshell_cmdcall_push_arg(
|
||||
bshell_cmdcall *cmdcall,
|
||||
fx_value arg);
|
||||
|
||||
extern enum bshell_status bshell_cmdcall_resolve(
|
||||
bshell_cmdcall *cmdcall,
|
||||
struct bshell_runtime *rt);
|
||||
extern enum bshell_status bshell_cmdcall_process_record(
|
||||
bshell_cmdcall *cmdcall,
|
||||
FX_TYPE_FWDREF(bshell_pipeline) * pipeline,
|
||||
struct bshell_runtime *rt);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,82 @@
|
||||
#ifndef BSHELL_RUNTIME_OPCODE_H_
|
||||
#define BSHELL_RUNTIME_OPCODE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define BSHELL_INSTRUCTION_INVALID 0xFFFFFFFF
|
||||
|
||||
typedef uint32_t bshell_instruction;
|
||||
|
||||
enum bshell_opcode {
|
||||
BSHELL_OPCODE_NOP = 0,
|
||||
BSHELL_OPCODE_LDC_INT,
|
||||
BSHELL_OPCODE_LDC_STR,
|
||||
BSHELL_OPCODE_LDC_FP,
|
||||
BSHELL_OPCODE_LDGLOBAL,
|
||||
BSHELL_OPCODE_LDLOCAL,
|
||||
BSHELL_OPCODE_LDBLOCK,
|
||||
BSHELL_OPCODE_LDENV,
|
||||
BSHELL_OPCODE_LDSTATIC,
|
||||
BSHELL_OPCODE_LDARG,
|
||||
BSHELL_OPCODE_LDPROP,
|
||||
BSHELL_OPCODE_LDELEM,
|
||||
BSHELL_OPCODE_LDELEM_C,
|
||||
BSHELL_OPCODE_STLOCAL,
|
||||
BSHELL_OPCODE_STPROP,
|
||||
BSHELL_OPCODE_STELEM,
|
||||
BSHELL_OPCODE_LDCMD,
|
||||
BSHELL_OPCODE_PEXEC,
|
||||
BSHELL_OPCODE_CALL,
|
||||
BSHELL_OPCODE_MK_STR,
|
||||
BSHELL_OPCODE_MK_ARRAY,
|
||||
BSHELL_OPCODE_MK_HTAB,
|
||||
BSHELL_OPCODE_POP,
|
||||
BSHELL_OPCODE_ADD,
|
||||
BSHELL_OPCODE_SUB,
|
||||
BSHELL_OPCODE_MUL,
|
||||
BSHELL_OPCODE_DIV,
|
||||
BSHELL_OPCODE_ADD_IP,
|
||||
BSHELL_OPCODE_MOD,
|
||||
BSHELL_OPCODE_INC,
|
||||
BSHELL_OPCODE_DEC,
|
||||
BSHELL_OPCODE_SHL,
|
||||
BSHELL_OPCODE_SHR,
|
||||
BSHELL_OPCODE_BAND,
|
||||
BSHELL_OPCODE_BOR,
|
||||
BSHELL_OPCODE_BXOR,
|
||||
BSHELL_OPCODE_BNOT,
|
||||
BSHELL_OPCODE_CMP_NULL,
|
||||
BSHELL_OPCODE_CMP_EQ,
|
||||
BSHELL_OPCODE_CMP_NE,
|
||||
BSHELL_OPCODE_CMP_LT,
|
||||
BSHELL_OPCODE_CMP_GT,
|
||||
BSHELL_OPCODE_CMP_LE,
|
||||
BSHELL_OPCODE_CMP_GE,
|
||||
BSHELL_OPCODE_LAND,
|
||||
BSHELL_OPCODE_LOR,
|
||||
BSHELL_OPCODE_LXOR,
|
||||
BSHELL_OPCODE_LNOT,
|
||||
BSHELL_OPCODE_BR,
|
||||
BSHELL_OPCODE_BR_TRUE,
|
||||
BSHELL_OPCODE_RANGE,
|
||||
BSHELL_OPCODE_MATCH,
|
||||
BSHELL_OPCODE_REPLACE,
|
||||
BSHELL_OPCODE_LIKE,
|
||||
BSHELL_OPCODE_IN,
|
||||
BSHELL_OPCODE_FMT,
|
||||
BSHELL_OPCODE_CONTAINS,
|
||||
BSHELL_OPCODE_SPLIT,
|
||||
BSHELL_OPCODE_JOIN,
|
||||
BSHELL_OPCODE_IS,
|
||||
BSHELL_OPCODE_AS,
|
||||
};
|
||||
|
||||
extern bshell_instruction bshell_instruction_encode(
|
||||
enum bshell_opcode opcode,
|
||||
uint32_t arg);
|
||||
extern void bshell_instruction_decode(
|
||||
bshell_instruction instr,
|
||||
enum bshell_opcode *out_opcode,
|
||||
uint32_t *out_arg);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,126 @@
|
||||
#ifndef BSHELL_RUNTIME_OPERATOR_H_
|
||||
#define BSHELL_RUNTIME_OPERATOR_H_
|
||||
|
||||
enum bshell_operator_precedence {
|
||||
BSHELL_PRECEDENCE_MINIMUM = 0,
|
||||
BSHELL_PRECEDENCE_ASSIGN,
|
||||
BSHELL_PRECEDENCE_PIPELINE,
|
||||
BSHELL_PRECEDENCE_LOGICAL,
|
||||
BSHELL_PRECEDENCE_BITWISE,
|
||||
BSHELL_PRECEDENCE_COMPARISON,
|
||||
BSHELL_PRECEDENCE_ADDITION,
|
||||
BSHELL_PRECEDENCE_MULTIPLICATION,
|
||||
BSHELL_PRECEDENCE_NEGATE,
|
||||
BSHELL_PRECEDENCE_FORMAT,
|
||||
BSHELL_PRECEDENCE_RANGE,
|
||||
BSHELL_PRECEDENCE_NOT,
|
||||
BSHELL_PRECEDENCE_INCREMENT,
|
||||
BSHELL_PRECEDENCE_ARRAY,
|
||||
BSHELL_PRECEDENCE_JOIN,
|
||||
BSHELL_PRECEDENCE_SPLIT,
|
||||
BSHELL_PRECEDENCE_CAST,
|
||||
BSHELL_PRECEDENCE_SUBSCRIPT,
|
||||
BSHELL_PRECEDENCE_STATIC_ACCESS,
|
||||
BSHELL_PRECEDENCE_MEMBER_ACCESS,
|
||||
BSHELL_PRECEDENCE_PARENTHESIS,
|
||||
};
|
||||
|
||||
enum bshell_operator_associativity {
|
||||
BSHELL_ASSOCIATIVITY_LEFT,
|
||||
BSHELL_ASSOCIATIVITY_RIGHT,
|
||||
};
|
||||
|
||||
enum bshell_operator_location {
|
||||
BSHELL_OPL_PREFIX,
|
||||
BSHELL_OPL_INFIX,
|
||||
BSHELL_OPL_POSTFIX,
|
||||
};
|
||||
|
||||
enum bshell_operator_arity {
|
||||
BSHELL_OPA_UNARY,
|
||||
BSHELL_OPA_BINARY,
|
||||
};
|
||||
|
||||
enum bshell_operator_id {
|
||||
BSHELL_OP_NONE = 0,
|
||||
BSHELL_OP_ADD,
|
||||
BSHELL_OP_SUBTRACT,
|
||||
BSHELL_OP_MULTIPLY,
|
||||
BSHELL_OP_DIVIDE,
|
||||
BSHELL_OP_MODULO,
|
||||
BSHELL_OP_INCREMENT,
|
||||
BSHELL_OP_DECREMENT,
|
||||
BSHELL_OP_LEFT_SHIFT,
|
||||
BSHELL_OP_RIGHT_SHIFT,
|
||||
BSHELL_OP_BINARY_AND,
|
||||
BSHELL_OP_BINARY_OR,
|
||||
BSHELL_OP_BINARY_XOR,
|
||||
BSHELL_OP_BINARY_NOT,
|
||||
BSHELL_OP_LESS_THAN,
|
||||
BSHELL_OP_GREATER_THAN,
|
||||
BSHELL_OP_EQUAL,
|
||||
BSHELL_OP_NOT_EQUAL,
|
||||
BSHELL_OP_LESS_EQUAL,
|
||||
BSHELL_OP_GREATER_EQUAL,
|
||||
BSHELL_OP_ASSIGN,
|
||||
BSHELL_OP_ADD_ASSIGN,
|
||||
BSHELL_OP_SUBTRACT_ASSIGN,
|
||||
BSHELL_OP_MULTIPLY_ASSIGN,
|
||||
BSHELL_OP_DIVIDE_ASSIGN,
|
||||
BSHELL_OP_MODULO_ASSIGN,
|
||||
BSHELL_OP_LOGICAL_AND,
|
||||
BSHELL_OP_LOGICAL_OR,
|
||||
BSHELL_OP_LOGICAL_XOR,
|
||||
BSHELL_OP_LOGICAL_NOT,
|
||||
BSHELL_OP_RANGE,
|
||||
BSHELL_OP_MATCH,
|
||||
BSHELL_OP_NOTMATCH,
|
||||
BSHELL_OP_REPLACE,
|
||||
BSHELL_OP_LIKE,
|
||||
BSHELL_OP_NOTLIKE,
|
||||
BSHELL_OP_IN,
|
||||
BSHELL_OP_NOTIN,
|
||||
BSHELL_OP_FORMAT,
|
||||
BSHELL_OP_CONTAINS,
|
||||
BSHELL_OP_NOTCONTAINS,
|
||||
BSHELL_OP_USPLIT,
|
||||
BSHELL_OP_BSPLIT,
|
||||
BSHELL_OP_UJOIN,
|
||||
BSHELL_OP_BJOIN,
|
||||
BSHELL_OP_IS,
|
||||
BSHELL_OP_ISNOT,
|
||||
BSHELL_OP_AS,
|
||||
|
||||
BSHELL_OP_SUBSCRIPT,
|
||||
BSHELL_OP_CONDITIONAL_SUBSCRIPT,
|
||||
|
||||
BSHELL_OP_ARRAY_DELIMITER,
|
||||
BSHELL_OP_ACCESS,
|
||||
BSHELL_OP_STATIC_ACCESS,
|
||||
BSHELL_OP_CONDITIONAL_ACCESS,
|
||||
|
||||
/* these are not real operators, and are just used internally by the
|
||||
* parser. */
|
||||
BSHELL_OP_CAST,
|
||||
BSHELL_OP_SUBEXPR,
|
||||
BSHELL_OP_PAREN,
|
||||
BSHELL_OP_SCRIPTBLOCK,
|
||||
BSHELL_OP_ARRAY_START,
|
||||
BSHELL_OP_HASHTABLE_START,
|
||||
};
|
||||
|
||||
struct bshell_operator_info {
|
||||
enum bshell_operator_id op_id;
|
||||
enum bshell_operator_precedence op_precedence;
|
||||
enum bshell_operator_associativity op_associativity;
|
||||
enum bshell_operator_location op_location;
|
||||
enum bshell_operator_arity op_arity;
|
||||
};
|
||||
|
||||
extern const struct bshell_operator_info *bshell_operator_get_by_id(
|
||||
enum bshell_operator_id id);
|
||||
extern const struct bshell_operator_info *bshell_operator_get_by_token(
|
||||
unsigned int token);
|
||||
extern const char *bshell_operator_id_to_string(enum bshell_operator_id op);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef BSHELL_RUNTIME_PIPELINE_H_
|
||||
#define BSHELL_RUNTIME_PIPELINE_H_
|
||||
|
||||
#include "../command/command.h"
|
||||
#include "../runtime/cmdcall.h"
|
||||
#include "../status.h"
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
#define BSHELL_TYPE_PIPELINE (bshell_pipeline_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_pipeline);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_pipeline)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_pipeline)
|
||||
|
||||
extern fx_type_id bshell_pipeline_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(bshell_pipeline, BSHELL_TYPE_PIPELINE);
|
||||
|
||||
extern enum bshell_status bshell_pipeline_add_input_value(
|
||||
bshell_pipeline *pipeline,
|
||||
fx_value value,
|
||||
bool enumerate);
|
||||
extern enum bshell_status bshell_pipeline_add_cmdcall(
|
||||
bshell_pipeline *pipeline,
|
||||
bshell_cmdcall *cmd);
|
||||
extern enum bshell_status bshell_pipeline_execute_single(
|
||||
bshell_pipeline *pipeline);
|
||||
extern enum bshell_status bshell_pipeline_pump_record(
|
||||
bshell_pipeline *pipeline,
|
||||
struct bshell_runtime *rt,
|
||||
fx_value *out,
|
||||
int *out_end_of_data);
|
||||
|
||||
extern fx_value bshell_pipeline_read_value(bshell_pipeline *pipeline);
|
||||
extern enum bshell_status bshell_pipeline_write_value(
|
||||
bshell_pipeline *pipeline,
|
||||
fx_value val,
|
||||
bool enumerate);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef BSHELL_RUNTIME_RUNTIME_H_
|
||||
#define BSHELL_RUNTIME_RUNTIME_H_
|
||||
|
||||
#include <bshell/command/command.h>
|
||||
#include <bshell/runtime/script-block.h>
|
||||
#include <bshell/runtime/var.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
struct bshell_runtime_scope;
|
||||
|
||||
struct bshell_runtime {
|
||||
struct bshell_runtime_scope *rt_global;
|
||||
fx_hashtable *rt_aliases;
|
||||
fx_queue rt_scope;
|
||||
};
|
||||
|
||||
extern struct bshell_runtime *bshell_runtime_create(void);
|
||||
extern void bshell_runtime_destroy(struct bshell_runtime *rt);
|
||||
|
||||
extern fx_value bshell_runtime_eval_global(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block);
|
||||
extern fx_value bshell_runtime_eval_script(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block);
|
||||
|
||||
extern bshell_variable *bshell_runtime_find_var(
|
||||
struct bshell_runtime *rt,
|
||||
const char *name);
|
||||
extern bshell_variable *bshell_runtime_define_var(
|
||||
struct bshell_runtime *rt,
|
||||
const char *name);
|
||||
|
||||
extern bshell_command *bshell_runtime_find_command(
|
||||
struct bshell_runtime *rt,
|
||||
const char *callable_name);
|
||||
|
||||
extern enum bshell_status bshell_runtime_push_scope(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block);
|
||||
extern enum bshell_status bshell_runtime_pop_scope(struct bshell_runtime *rt);
|
||||
extern fx_value bshell_runtime_eval(struct bshell_runtime *rt);
|
||||
|
||||
extern struct bshell_runtime_scope *bshell_runtime_get_current_scope(
|
||||
struct bshell_runtime *rt);
|
||||
extern struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
|
||||
struct bshell_runtime *rt,
|
||||
struct bshell_runtime_scope *scope);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef BSHELL_RUNTIME_SCOPE_H_
|
||||
#define BSHELL_RUNTIME_SCOPE_H_
|
||||
|
||||
#include <fx/collections/hashtable.h>
|
||||
|
||||
struct bshell_runtime_scope;
|
||||
|
||||
extern fx_hashtable *bshell_runtime_scope_get_functions(
|
||||
struct bshell_runtime_scope *scope);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#ifndef BSHELL_RUNTIME_SCRIPT_BLOCK_H_
|
||||
#define BSHELL_RUNTIME_SCRIPT_BLOCK_H_
|
||||
|
||||
#include <bshell/runtime/opcode.h>
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
enum bshell_opcode;
|
||||
|
||||
#define POOL_INDEX_INVALID ((unsigned long)-1)
|
||||
#define BSHELL_TYPE_SCRIPTBLOCK (bshell_scriptblock_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_scriptblock);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_scriptblock)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_scriptblock)
|
||||
|
||||
extern fx_type_id bshell_scriptblock_get_type(void);
|
||||
|
||||
FX_TYPE_DEFAULT_CONSTRUCTOR(bshell_scriptblock, BSHELL_TYPE_SCRIPTBLOCK);
|
||||
|
||||
extern size_t bshell_scriptblock_push_instruction(
|
||||
bshell_scriptblock *block,
|
||||
enum bshell_opcode opcode,
|
||||
uint32_t arg);
|
||||
extern enum bshell_status bshell_scriptblock_read_instruction(
|
||||
bshell_scriptblock *block,
|
||||
size_t offset,
|
||||
enum bshell_opcode *out_opcode,
|
||||
uint32_t *out_arg);
|
||||
extern void bshell_scriptblock_patch_instruction(
|
||||
bshell_scriptblock *block,
|
||||
size_t offset,
|
||||
enum bshell_opcode opcode,
|
||||
uint32_t arg);
|
||||
|
||||
extern void bshell_scriptblock_clear_text(bshell_scriptblock *block);
|
||||
extern void bshell_scriptblock_get_text(
|
||||
const bshell_scriptblock *block,
|
||||
bshell_instruction **out_ptr,
|
||||
size_t *out_count);
|
||||
extern void bshell_scriptblock_get_pool(
|
||||
const bshell_scriptblock *block,
|
||||
fx_value **out_ptr,
|
||||
size_t *out_count);
|
||||
extern unsigned long bshell_scriptblock_add_pool_value(
|
||||
bshell_scriptblock *block,
|
||||
fx_value *value);
|
||||
extern fx_value *bshell_scriptblock_get_pool_value(
|
||||
const bshell_scriptblock *block,
|
||||
unsigned long index,
|
||||
fx_type_id expected_type);
|
||||
extern unsigned long bshell_scriptblock_get_string(
|
||||
bshell_scriptblock *block,
|
||||
const char *s);
|
||||
extern unsigned long bshell_scriptblock_get_double(
|
||||
bshell_scriptblock *block,
|
||||
double v);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef BSHELL_RUNTIME_VAR_H_
|
||||
#define BSHELL_RUNTIME_VAR_H_
|
||||
|
||||
#include <fx/macros.h>
|
||||
#include <fx/value.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define BSHELL_TYPE_VARIABLE (bshell_variable_get_type())
|
||||
|
||||
FX_DECLARE_TYPE(bshell_variable);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_variable)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_variable)
|
||||
|
||||
extern fx_type_id bshell_variable_get_type(void);
|
||||
|
||||
extern bshell_variable *bshell_variable_create(const char *name);
|
||||
|
||||
extern const char *bshell_variable_get_name(const bshell_variable *var);
|
||||
extern const fx_value *bshell_variable_get_value(const bshell_variable *var);
|
||||
extern void bshell_variable_set_value(bshell_variable *var, fx_value *value);
|
||||
|
||||
FX_DECLS_END;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef BSHELL_STATUS_H_
|
||||
#define BSHELL_STATUS_H_
|
||||
|
||||
enum bshell_status {
|
||||
BSHELL_SUCCESS = 0,
|
||||
BSHELL_ERR_EOF,
|
||||
BSHELL_ERR_BAD_SYNTAX,
|
||||
BSHELL_ERR_BAD_FORMAT,
|
||||
BSHELL_ERR_BAD_STATE,
|
||||
BSHELL_ERR_INVALID_VALUE,
|
||||
BSHELL_ERR_INVALID_ARGUMENT,
|
||||
BSHELL_ERR_NO_MEMORY,
|
||||
BSHELL_ERR_NO_ENTRY,
|
||||
BSHELL_ERR_NO_DATA,
|
||||
BSHELL_ERR_NAME_EXISTS,
|
||||
BSHELL_ERR_NOT_SUPPORTED,
|
||||
BSHELL_ERR_IO_FAILURE,
|
||||
BSHELL_ERR_ACCESS_DENIED,
|
||||
BSHELL_ERR_INTERNAL_FAILURE,
|
||||
};
|
||||
|
||||
extern enum bshell_status bshell_status_from_errno(int err);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,144 @@
|
||||
#ifndef BSHELL_VERB_H_
|
||||
#define BSHELL_VERB_H_
|
||||
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/macros.h>
|
||||
|
||||
FX_DECLS_BEGIN;
|
||||
|
||||
#define BSHELL_TYPE_VERB (bshell_verb_get_type())
|
||||
|
||||
enum bshell_verb_id {
|
||||
BSHELL_VERB_NONE = 0,
|
||||
BSHELL_VERB_ADD,
|
||||
BSHELL_VERB_CLEAR,
|
||||
BSHELL_VERB_CLOSE,
|
||||
BSHELL_VERB_COPY,
|
||||
BSHELL_VERB_ENTER,
|
||||
BSHELL_VERB_EXIT,
|
||||
BSHELL_VERB_FIND,
|
||||
BSHELL_VERB_FORMAT,
|
||||
BSHELL_VERB_GET,
|
||||
BSHELL_VERB_HIDE,
|
||||
BSHELL_VERB_JOIN,
|
||||
BSHELL_VERB_LOCK,
|
||||
BSHELL_VERB_MOVE,
|
||||
BSHELL_VERB_NEW,
|
||||
BSHELL_VERB_OPEN,
|
||||
BSHELL_VERB_OPTIMIZE,
|
||||
BSHELL_VERB_PUSH,
|
||||
BSHELL_VERB_POP,
|
||||
BSHELL_VERB_REDO,
|
||||
BSHELL_VERB_REMOVE,
|
||||
BSHELL_VERB_RENAME,
|
||||
BSHELL_VERB_RESET,
|
||||
BSHELL_VERB_RESIZE,
|
||||
BSHELL_VERB_SEARCH,
|
||||
BSHELL_VERB_SELECT,
|
||||
BSHELL_VERB_SET,
|
||||
BSHELL_VERB_SHOW,
|
||||
BSHELL_VERB_SKIP,
|
||||
BSHELL_VERB_SPLIT,
|
||||
BSHELL_VERB_STEP,
|
||||
BSHELL_VERB_SWITCH,
|
||||
BSHELL_VERB_UNDO,
|
||||
BSHELL_VERB_UNLOCK,
|
||||
BSHELL_VERB_WATCH,
|
||||
BSHELL_VERB_CONNECT,
|
||||
BSHELL_VERB_DISCONNECT,
|
||||
BSHELL_VERB_READ,
|
||||
BSHELL_VERB_RECEIVE,
|
||||
BSHELL_VERB_SEND,
|
||||
BSHELL_VERB_WRITE,
|
||||
BSHELL_VERB_BACKUP,
|
||||
BSHELL_VERB_CHECKPOINT,
|
||||
BSHELL_VERB_COMPARE,
|
||||
BSHELL_VERB_COMPRESS,
|
||||
BSHELL_VERB_CONVERT,
|
||||
BSHELL_VERB_CONVERTFROM,
|
||||
BSHELL_VERB_CONVERTTO,
|
||||
BSHELL_VERB_DISMOUNT,
|
||||
BSHELL_VERB_EDIT,
|
||||
BSHELL_VERB_EXPAND,
|
||||
BSHELL_VERB_EXPORT,
|
||||
BSHELL_VERB_GROUP,
|
||||
BSHELL_VERB_IMPORT,
|
||||
BSHELL_VERB_INITIALIZE,
|
||||
BSHELL_VERB_LIMIT,
|
||||
BSHELL_VERB_MERGE,
|
||||
BSHELL_VERB_MOUNT,
|
||||
BSHELL_VERB_OUT,
|
||||
BSHELL_VERB_PUBLISH,
|
||||
BSHELL_VERB_RESTORE,
|
||||
BSHELL_VERB_SAVE,
|
||||
BSHELL_VERB_SYNC,
|
||||
BSHELL_VERB_UNPUBLISH,
|
||||
BSHELL_VERB_UPDATE,
|
||||
BSHELL_VERB_DEBUG,
|
||||
BSHELL_VERB_MEASURE,
|
||||
BSHELL_VERB_REPAIR,
|
||||
BSHELL_VERB_RESOLVE,
|
||||
BSHELL_VERB_TEST,
|
||||
BSHELL_VERB_TRACE,
|
||||
BSHELL_VERB_APPROVE,
|
||||
BSHELL_VERB_ASSERT,
|
||||
BSHELL_VERB_BUILD,
|
||||
BSHELL_VERB_COMPLETE,
|
||||
BSHELL_VERB_CONFIRM,
|
||||
BSHELL_VERB_DENY,
|
||||
BSHELL_VERB_DEPLOY,
|
||||
BSHELL_VERB_DISABLE,
|
||||
BSHELL_VERB_ENABLE,
|
||||
BSHELL_VERB_INSTALL,
|
||||
BSHELL_VERB_INVOKE,
|
||||
BSHELL_VERB_REGISTER,
|
||||
BSHELL_VERB_REQUEST,
|
||||
BSHELL_VERB_RESTART,
|
||||
BSHELL_VERB_RESUME,
|
||||
BSHELL_VERB_START,
|
||||
BSHELL_VERB_STOP,
|
||||
BSHELL_VERB_SUBMIT,
|
||||
BSHELL_VERB_SUSPEND,
|
||||
BSHELL_VERB_UNINSTALL,
|
||||
BSHELL_VERB_UNREGISTER,
|
||||
BSHELL_VERB_WAIT,
|
||||
BSHELL_VERB_USE,
|
||||
BSHELL_VERB_BLOCK,
|
||||
BSHELL_VERB_GRANT,
|
||||
BSHELL_VERB_PROTECT,
|
||||
BSHELL_VERB_REVOKE,
|
||||
BSHELL_VERB_UNBLOCK,
|
||||
BSHELL_VERB_UNPROTECT,
|
||||
/* reserved verbs */
|
||||
BSHELL_VERB_FOREACH,
|
||||
BSHELL_VERB_PING,
|
||||
BSHELL_VERB_SORT,
|
||||
BSHELL_VERB_TEE,
|
||||
BSHELL_VERB_WHERE,
|
||||
__BSHELL_VERB_MAX,
|
||||
};
|
||||
|
||||
FX_DECLARE_TYPE(bshell_verb);
|
||||
|
||||
FX_TYPE_CLASS_DECLARATION_BEGIN(bshell_verb)
|
||||
FX_TYPE_CLASS_DECLARATION_END(bshell_verb)
|
||||
|
||||
extern enum bshell_status bshell_init_all_verbs(void);
|
||||
extern fx_hashtable *bshell_get_all_verbs(void);
|
||||
extern bshell_verb *bshell_verb_get_by_id(enum bshell_verb_id id);
|
||||
extern bshell_verb *bshell_verb_get_by_name(const char *name);
|
||||
|
||||
extern fx_type_id bshell_verb_get_type(void);
|
||||
|
||||
extern bshell_verb *bshell_verb_create(
|
||||
enum bshell_verb_id id,
|
||||
const char *name,
|
||||
const char *alias_prefix,
|
||||
const char *group,
|
||||
const char *description);
|
||||
|
||||
extern bool bshell_verb_is_reserved(const bshell_verb *verb);
|
||||
extern enum bshell_verb_id bshell_verb_get_id(const bshell_verb *verb);
|
||||
extern const char *bshell_verb_get_name(const bshell_verb *verb);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
#include <bshell/line-source.h>
|
||||
|
||||
enum bshell_status bshell_line_source_get_name(
|
||||
struct bshell_line_source *src,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read)
|
||||
{
|
||||
if (src->s_get_name) {
|
||||
return src->s_get_name(src, buf, count, nr_read);
|
||||
}
|
||||
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_line_source_readline(
|
||||
struct bshell_line_source *src,
|
||||
fx_stringstream *out)
|
||||
{
|
||||
if (src->s_readline) {
|
||||
return src->s_readline(src, out);
|
||||
}
|
||||
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_line_source_get_row(
|
||||
struct bshell_line_source *src,
|
||||
size_t row,
|
||||
char *buf,
|
||||
size_t count,
|
||||
size_t *nr_read)
|
||||
{
|
||||
if (src->s_get_row) {
|
||||
return src->s_get_row(src, row, buf, count, nr_read);
|
||||
}
|
||||
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/runtime/operator.h>
|
||||
|
||||
#define OP(id, p, a, l, u) \
|
||||
[BSHELL_OP_##id] = { \
|
||||
.op_id = (BSHELL_OP_##id), \
|
||||
.op_precedence = (BSHELL_PRECEDENCE_##p), \
|
||||
.op_associativity = (BSHELL_ASSOCIATIVITY_##a), \
|
||||
.op_location = (BSHELL_OPL_##l), \
|
||||
.op_arity = (BSHELL_OPA_##u), \
|
||||
}
|
||||
|
||||
#define BSHELL_TOK_OP(id, tok) \
|
||||
[BSHELL_TOK_##tok - __BSHELL_TOK_INDEX_BASE] = &operators[BSHELL_OP_##id]
|
||||
#define BSHELL_SYM_OP(id, sym) \
|
||||
[BSHELL_SYM_##sym - __BSHELL_SYM_INDEX_BASE] = &operators[BSHELL_OP_##id]
|
||||
#define BSHELL_KW_OP(id, kw) [BSHELL_KW_##kw - __BSHELL_KW_INDEX_BASE] = &operators[BSHELL_OP_##id]
|
||||
#define BSHELL_TKOP_OP(id, kw) \
|
||||
[BSHELL_TKOP_##kw - __BSHELL_TKOP_INDEX_BASE] \
|
||||
= &operators[BSHELL_OP_##id]
|
||||
|
||||
/* clang-format off */
|
||||
static const struct bshell_operator_info operators[] = {
|
||||
OP(ACCESS, MEMBER_ACCESS, LEFT, INFIX, BINARY),
|
||||
OP(CONDITIONAL_ACCESS, MEMBER_ACCESS, LEFT, INFIX, BINARY),
|
||||
OP(STATIC_ACCESS, STATIC_ACCESS, LEFT, INFIX, BINARY),
|
||||
OP(SUBSCRIPT, SUBSCRIPT, LEFT, INFIX, BINARY),
|
||||
OP(CONDITIONAL_SUBSCRIPT, SUBSCRIPT, LEFT, INFIX, BINARY),
|
||||
OP(CAST, CAST, LEFT, PREFIX, UNARY),
|
||||
OP(USPLIT, SPLIT, LEFT, PREFIX, UNARY),
|
||||
OP(UJOIN, JOIN, LEFT, PREFIX, UNARY),
|
||||
OP(ARRAY_DELIMITER, ARRAY, LEFT, INFIX, BINARY),
|
||||
OP(INCREMENT, INCREMENT, LEFT, INFIX, BINARY),
|
||||
OP(LOGICAL_NOT, NOT, LEFT, PREFIX, UNARY),
|
||||
OP(RANGE, RANGE, LEFT, INFIX, BINARY),
|
||||
OP(FORMAT, FORMAT, LEFT, INFIX, BINARY),
|
||||
OP(MULTIPLY, MULTIPLICATION, LEFT, INFIX, BINARY),
|
||||
OP(DIVIDE, MULTIPLICATION, LEFT, INFIX, BINARY),
|
||||
OP(MODULO, MULTIPLICATION, LEFT, INFIX, BINARY),
|
||||
OP(ADD, ADDITION, LEFT, INFIX, BINARY),
|
||||
OP(SUBTRACT, ADDITION, LEFT, INFIX, BINARY),
|
||||
|
||||
OP(BSPLIT, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(BJOIN, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(IS, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(ISNOT, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(AS, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(EQUAL, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(NOT_EQUAL, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(GREATER_THAN, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(LESS_THAN, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(GREATER_EQUAL, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(LESS_EQUAL, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(LIKE, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(NOTLIKE, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(MATCH, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(NOTMATCH, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(IN, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(NOTIN, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(CONTAINS, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(NOTCONTAINS, COMPARISON, LEFT, INFIX, BINARY),
|
||||
OP(REPLACE, COMPARISON, LEFT, INFIX, BINARY),
|
||||
|
||||
OP(LOGICAL_AND, LOGICAL, LEFT, INFIX, BINARY),
|
||||
OP(LOGICAL_OR, LOGICAL, LEFT, INFIX, BINARY),
|
||||
OP(LOGICAL_XOR, LOGICAL, LEFT, INFIX, BINARY),
|
||||
|
||||
OP(BINARY_AND, BITWISE, LEFT, INFIX, BINARY),
|
||||
OP(BINARY_OR, BITWISE, LEFT, INFIX, BINARY),
|
||||
OP(BINARY_NOT, BITWISE, LEFT, INFIX, BINARY),
|
||||
OP(BINARY_XOR, BITWISE, LEFT, INFIX, BINARY),
|
||||
OP(LEFT_SHIFT, BITWISE, LEFT, INFIX, BINARY),
|
||||
OP(RIGHT_SHIFT, BITWISE, LEFT, INFIX, BINARY),
|
||||
|
||||
OP(ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
OP(ADD_ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
OP(SUBTRACT_ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
OP(MULTIPLY_ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
OP(DIVIDE_ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
OP(MODULO_ASSIGN, ASSIGN, RIGHT, INFIX, BINARY),
|
||||
|
||||
/* these are not real operators, and are just used internally by the
|
||||
* parser. */
|
||||
OP(SUBEXPR, PARENTHESIS, LEFT, PREFIX, UNARY),
|
||||
OP(ARRAY_START, PARENTHESIS, LEFT, PREFIX, UNARY),
|
||||
OP(PAREN, PARENTHESIS, LEFT, PREFIX, UNARY),
|
||||
OP(HASHTABLE_START, PARENTHESIS, LEFT, PREFIX, UNARY),
|
||||
OP(SCRIPTBLOCK, PARENTHESIS, LEFT, PREFIX, UNARY),
|
||||
};
|
||||
static const size_t nr_operators = sizeof operators / sizeof operators[0];
|
||||
|
||||
static const struct bshell_operator_info *bshell_operator_symbols[] = {
|
||||
BSHELL_SYM_OP(LOGICAL_NOT, BANG),
|
||||
BSHELL_SYM_OP(ASSIGN, EQUAL),
|
||||
BSHELL_SYM_OP(ADD, PLUS),
|
||||
BSHELL_SYM_OP(SUBTRACT, HYPHEN),
|
||||
BSHELL_SYM_OP(MULTIPLY, ASTERISK),
|
||||
BSHELL_SYM_OP(DIVIDE, FORWARD_SLASH),
|
||||
BSHELL_SYM_OP(MODULO, PERCENT),
|
||||
BSHELL_SYM_OP(ADD_ASSIGN, PLUS_EQUAL),
|
||||
BSHELL_SYM_OP(SUBTRACT_ASSIGN, HYPHEN_EQUAL),
|
||||
BSHELL_SYM_OP(MULTIPLY_ASSIGN, ASTERISK_EQUAL),
|
||||
BSHELL_SYM_OP(DIVIDE_ASSIGN, FORWARD_SLASH_EQUAL),
|
||||
BSHELL_SYM_OP(MODULO_ASSIGN, PERCENT_EQUAL),
|
||||
BSHELL_SYM_OP(RANGE, DOT_DOT),
|
||||
BSHELL_SYM_OP(SUBSCRIPT, LEFT_BRACKET),
|
||||
BSHELL_SYM_OP(CONDITIONAL_SUBSCRIPT, QUESTION_LEFT_BRACKET),
|
||||
|
||||
BSHELL_SYM_OP(ACCESS, DOT),
|
||||
BSHELL_SYM_OP(CONDITIONAL_ACCESS, QUESTION_DOT),
|
||||
BSHELL_SYM_OP(STATIC_ACCESS, COLON_COLON),
|
||||
|
||||
/* parser-internal pseudo-operators. */
|
||||
|
||||
/* CAST uses the same symbol as SUBSCRIPT */
|
||||
/* BSHELL_SYM_OP(CAST, LEFT_BRACKET), */
|
||||
BSHELL_SYM_OP(SUBEXPR, DOLLAR_LEFT_PAREN),
|
||||
BSHELL_SYM_OP(PAREN, LEFT_PAREN),
|
||||
BSHELL_SYM_OP(ARRAY_START, AT_LEFT_PAREN),
|
||||
BSHELL_SYM_OP(HASHTABLE_START, AT_LEFT_BRACE),
|
||||
};
|
||||
static const size_t nr_bshell_operator_symbols = sizeof bshell_operator_symbols / sizeof bshell_operator_symbols[0];
|
||||
|
||||
static const struct bshell_operator_info *bshell_operator_token_ops[] = {
|
||||
BSHELL_TKOP_OP(FORMAT, F),
|
||||
BSHELL_TKOP_OP(BINARY_AND, BAND),
|
||||
BSHELL_TKOP_OP(BINARY_OR, BOR),
|
||||
BSHELL_TKOP_OP(BINARY_XOR, BXOR),
|
||||
BSHELL_TKOP_OP(BINARY_NOT, BNOT),
|
||||
BSHELL_TKOP_OP(LEFT_SHIFT, SHL),
|
||||
BSHELL_TKOP_OP(RIGHT_SHIFT, SHR),
|
||||
BSHELL_TKOP_OP(EQUAL, EQ),
|
||||
BSHELL_TKOP_OP(NOT_EQUAL, NE),
|
||||
BSHELL_TKOP_OP(GREATER_THAN, GT),
|
||||
BSHELL_TKOP_OP(LESS_THAN, LT),
|
||||
BSHELL_TKOP_OP(GREATER_EQUAL, GE),
|
||||
BSHELL_TKOP_OP(LESS_EQUAL, LE),
|
||||
BSHELL_TKOP_OP(MATCH, MATCH),
|
||||
BSHELL_TKOP_OP(NOTMATCH, NOTMATCH),
|
||||
BSHELL_TKOP_OP(REPLACE, REPLACE),
|
||||
BSHELL_TKOP_OP(LIKE, LIKE),
|
||||
BSHELL_TKOP_OP(NOTLIKE, NOTLIKE),
|
||||
BSHELL_TKOP_OP(IN, IN),
|
||||
BSHELL_TKOP_OP(NOTIN, NOTIN),
|
||||
BSHELL_TKOP_OP(CONTAINS, CONTAINS),
|
||||
BSHELL_TKOP_OP(NOTCONTAINS, NOTCONTAINS),
|
||||
BSHELL_TKOP_OP(LOGICAL_AND, AND),
|
||||
BSHELL_TKOP_OP(LOGICAL_OR, OR),
|
||||
BSHELL_TKOP_OP(LOGICAL_XOR, XOR),
|
||||
BSHELL_TKOP_OP(LOGICAL_NOT, NOT),
|
||||
/* there are also unary versions of these operators */
|
||||
BSHELL_TKOP_OP(BSPLIT, SPLIT),
|
||||
BSHELL_TKOP_OP(BJOIN, JOIN),
|
||||
|
||||
BSHELL_TKOP_OP(IS, IS),
|
||||
BSHELL_TKOP_OP(ISNOT, ISNOT),
|
||||
BSHELL_TKOP_OP(AS, AS),
|
||||
};
|
||||
static const size_t nr_bshell_operator_token_ops = sizeof bshell_operator_token_ops / sizeof bshell_operator_token_ops[0];
|
||||
|
||||
/* clang-format on */
|
||||
|
||||
const struct bshell_operator_info *bshell_operator_get_by_token(
|
||||
unsigned int token)
|
||||
{
|
||||
const struct bshell_operator_info **op_list = NULL;
|
||||
size_t base = 0;
|
||||
size_t op_list_size = 0;
|
||||
|
||||
if (token > __BSHELL_TKOP_INDEX_BASE
|
||||
&& token < __BSHELL_TKOP_INDEX_LIMIT) {
|
||||
op_list = bshell_operator_token_ops;
|
||||
base = __BSHELL_TKOP_INDEX_BASE;
|
||||
op_list_size = nr_bshell_operator_token_ops;
|
||||
} else if (token > __BSHELL_SYM_INDEX_BASE && token < __BSHELL_SYM_INDEX_LIMIT) {
|
||||
op_list = bshell_operator_symbols;
|
||||
base = __BSHELL_SYM_INDEX_BASE;
|
||||
op_list_size = nr_bshell_operator_symbols;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (token - base >= op_list_size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return op_list[token - base];
|
||||
}
|
||||
|
||||
const struct bshell_operator_info *bshell_operator_get_by_id(
|
||||
enum bshell_operator_id id)
|
||||
{
|
||||
if (id >= nr_operators) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const struct bshell_operator_info *op = &operators[id];
|
||||
if (op->op_id != id) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return op;
|
||||
}
|
||||
|
||||
#define ENUM_STR(x) \
|
||||
case x: \
|
||||
return #x
|
||||
|
||||
const char *bshell_operator_id_to_string(enum bshell_operator_id op)
|
||||
{
|
||||
switch (op) {
|
||||
ENUM_STR(BSHELL_OP_NONE);
|
||||
ENUM_STR(BSHELL_OP_ADD);
|
||||
ENUM_STR(BSHELL_OP_SUBTRACT);
|
||||
ENUM_STR(BSHELL_OP_MULTIPLY);
|
||||
ENUM_STR(BSHELL_OP_DIVIDE);
|
||||
ENUM_STR(BSHELL_OP_MODULO);
|
||||
ENUM_STR(BSHELL_OP_INCREMENT);
|
||||
ENUM_STR(BSHELL_OP_DECREMENT);
|
||||
ENUM_STR(BSHELL_OP_LEFT_SHIFT);
|
||||
ENUM_STR(BSHELL_OP_RIGHT_SHIFT);
|
||||
ENUM_STR(BSHELL_OP_BINARY_AND);
|
||||
ENUM_STR(BSHELL_OP_BINARY_OR);
|
||||
ENUM_STR(BSHELL_OP_BINARY_XOR);
|
||||
ENUM_STR(BSHELL_OP_BINARY_NOT);
|
||||
ENUM_STR(BSHELL_OP_LESS_THAN);
|
||||
ENUM_STR(BSHELL_OP_GREATER_THAN);
|
||||
ENUM_STR(BSHELL_OP_EQUAL);
|
||||
ENUM_STR(BSHELL_OP_NOT_EQUAL);
|
||||
ENUM_STR(BSHELL_OP_LESS_EQUAL);
|
||||
ENUM_STR(BSHELL_OP_GREATER_EQUAL);
|
||||
ENUM_STR(BSHELL_OP_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_ADD_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_SUBTRACT_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_MULTIPLY_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_DIVIDE_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_MODULO_ASSIGN);
|
||||
ENUM_STR(BSHELL_OP_LOGICAL_AND);
|
||||
ENUM_STR(BSHELL_OP_LOGICAL_OR);
|
||||
ENUM_STR(BSHELL_OP_LOGICAL_XOR);
|
||||
ENUM_STR(BSHELL_OP_LOGICAL_NOT);
|
||||
ENUM_STR(BSHELL_OP_RANGE);
|
||||
ENUM_STR(BSHELL_OP_MATCH);
|
||||
ENUM_STR(BSHELL_OP_NOTMATCH);
|
||||
ENUM_STR(BSHELL_OP_REPLACE);
|
||||
ENUM_STR(BSHELL_OP_LIKE);
|
||||
ENUM_STR(BSHELL_OP_NOTLIKE);
|
||||
ENUM_STR(BSHELL_OP_IN);
|
||||
ENUM_STR(BSHELL_OP_NOTIN);
|
||||
ENUM_STR(BSHELL_OP_FORMAT);
|
||||
ENUM_STR(BSHELL_OP_CONTAINS);
|
||||
ENUM_STR(BSHELL_OP_NOTCONTAINS);
|
||||
ENUM_STR(BSHELL_OP_USPLIT);
|
||||
ENUM_STR(BSHELL_OP_BSPLIT);
|
||||
ENUM_STR(BSHELL_OP_UJOIN);
|
||||
ENUM_STR(BSHELL_OP_BJOIN);
|
||||
ENUM_STR(BSHELL_OP_IS);
|
||||
ENUM_STR(BSHELL_OP_ISNOT);
|
||||
ENUM_STR(BSHELL_OP_AS);
|
||||
ENUM_STR(BSHELL_OP_SUBSCRIPT);
|
||||
ENUM_STR(BSHELL_OP_CONDITIONAL_SUBSCRIPT);
|
||||
ENUM_STR(BSHELL_OP_ARRAY_DELIMITER);
|
||||
ENUM_STR(BSHELL_OP_ACCESS);
|
||||
ENUM_STR(BSHELL_OP_STATIC_ACCESS);
|
||||
ENUM_STR(BSHELL_OP_CONDITIONAL_ACCESS);
|
||||
ENUM_STR(BSHELL_OP_CAST);
|
||||
ENUM_STR(BSHELL_OP_SUBEXPR);
|
||||
ENUM_STR(BSHELL_OP_PAREN);
|
||||
ENUM_STR(BSHELL_OP_ARRAY_START);
|
||||
ENUM_STR(BSHELL_OP_HASHTABLE_START);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
static enum bshell_status arithmetic_hyphen(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
if (!fx_wchar_is_alnum(c)) {
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
handle_lex_state_transition(ctx, BSHELL_SYM_HYPHEN);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
enum bshell_status status = read_word(
|
||||
ctx,
|
||||
READ_NO_SET_TOKEN_START | READ_APPEND_HYPHEN,
|
||||
&tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
unsigned int token_type = BSHELL_TOK_WORD;
|
||||
if (convert_word_to_number(tok)) {
|
||||
token_type = tok->tok_type;
|
||||
|
||||
/* because of APPEND_HYPHEN (which is needed to ensure operator
|
||||
* tokens are detected properly), the resulting number will be
|
||||
* negative.
|
||||
* this token will be preceded by a HYPHEN token, so the number
|
||||
* must be positive */
|
||||
fx_value neg = FX_INT(-1);
|
||||
fx_value_multiply(&tok->tok_number, &neg, &tok->tok_number);
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
} else if (convert_word_to_operator(ctx, tok)) {
|
||||
token_type = tok->tok_operator;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status arithmetic_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, sym->id);
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_DQUOTE:
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_SQUOTE:
|
||||
status = read_literal_string(ctx, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_HYPHEN:
|
||||
return arithmetic_hyphen(ctx);
|
||||
case BSHELL_SYM_HASH:
|
||||
return read_line_comment(ctx);
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
||||
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
push_symbol(ctx, sym->id);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status arithmetic_word(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *word = NULL;
|
||||
enum bshell_status status = read_word(ctx, 0, &word);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
unsigned int token_type = BSHELL_TOK_WORD;
|
||||
bool kw = false, number = false;
|
||||
if (convert_word_to_keyword(word)) {
|
||||
token_type = word->tok_keyword;
|
||||
} else if (convert_word_to_number(word)) {
|
||||
token_type = word->tok_type;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, token_type);
|
||||
|
||||
enqueue_token(ctx, word);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status arithmetic_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
bool newline = false;
|
||||
|
||||
set_token_start(ctx);
|
||||
while (fx_wchar_is_space(c)) {
|
||||
if (c == '\n') {
|
||||
newline = true;
|
||||
}
|
||||
|
||||
set_token_end(ctx);
|
||||
advance_char_noread(ctx);
|
||||
c = peek_char_noread(ctx);
|
||||
}
|
||||
|
||||
if (newline) {
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_LINEFEED);
|
||||
enqueue_token(ctx, tok);
|
||||
handle_lex_state_transition(ctx, BSHELL_TOK_LINEFEED);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return arithmetic_symbol(ctx);
|
||||
}
|
||||
|
||||
return arithmetic_word(ctx);
|
||||
}
|
||||
|
||||
static const struct bshell_lex_state_link links[] = {
|
||||
LINK_CHANGE(BSHELL_SYM_EQUAL, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
|
||||
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
||||
LINK_POP(BSHELL_SYM_RIGHT_PAREN),
|
||||
LINK_CHANGE(BSHELL_SYM_SEMICOLON, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_CHANGE(BSHELL_TOK_LINEFEED, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_CHANGE(BSHELL_SYM_PIPE, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_PUSH(BSHELL_SYM_AT_LEFT_BRACE, BSHELL_LEX_STATE_HASHTABLE, 0),
|
||||
LINK_PUSH(
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
STATEMENT_F_DISABLE_KEYWORDS),
|
||||
LINK_END,
|
||||
};
|
||||
|
||||
static const unsigned int keywords[] = {
|
||||
BSHELL_KW_IF,
|
||||
BSHELL_KW_ELSEIF,
|
||||
BSHELL_KW_ELSE,
|
||||
BSHELL_KW_NONE,
|
||||
};
|
||||
|
||||
static const unsigned int operators[] = {
|
||||
BSHELL_TKOP_F, BSHELL_TKOP_BAND, BSHELL_TKOP_BOR, BSHELL_TKOP_BXOR,
|
||||
BSHELL_TKOP_BNOT, BSHELL_TKOP_SHL, BSHELL_TKOP_SHR, BSHELL_TKOP_EQ,
|
||||
BSHELL_TKOP_NE, BSHELL_TKOP_GT, BSHELL_TKOP_LT, BSHELL_TKOP_GE,
|
||||
BSHELL_TKOP_LE, BSHELL_TKOP_MATCH, BSHELL_TKOP_NOTMATCH, BSHELL_TKOP_REPLACE,
|
||||
BSHELL_TKOP_LIKE, BSHELL_TKOP_NOTLIKE, BSHELL_TKOP_IN, BSHELL_TKOP_NOTIN,
|
||||
BSHELL_TKOP_CONTAINS, BSHELL_TKOP_NOTCONTAINS, BSHELL_TKOP_AND, BSHELL_TKOP_OR,
|
||||
BSHELL_TKOP_XOR, BSHELL_TKOP_NOT, BSHELL_TKOP_SPLIT, BSHELL_TKOP_JOIN,
|
||||
BSHELL_TKOP_IS, BSHELL_TKOP_ISNOT, BSHELL_TKOP_AS, BSHELL_TKOP_NONE,
|
||||
};
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_BANG,
|
||||
BSHELL_SYM_PLUS,
|
||||
BSHELL_SYM_HYPHEN,
|
||||
BSHELL_SYM_FORWARD_SLASH,
|
||||
BSHELL_SYM_ASTERISK,
|
||||
BSHELL_SYM_AMPERSAND,
|
||||
BSHELL_SYM_PERCENT,
|
||||
BSHELL_SYM_SQUOTE,
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_HASH,
|
||||
BSHELL_SYM_DOLLAR,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_SYM_AT,
|
||||
BSHELL_SYM_AT_LEFT_BRACE,
|
||||
BSHELL_SYM_PIPE,
|
||||
BSHELL_SYM_COMMA,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_RIGHT_PAREN,
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_LEFT_BRACKET,
|
||||
BSHELL_SYM_RIGHT_BRACKET,
|
||||
BSHELL_SYM_QUESTION_DOT,
|
||||
BSHELL_SYM_QUESTION_LEFT_BRACKET,
|
||||
BSHELL_SYM_EQUAL,
|
||||
BSHELL_SYM_PLUS_EQUAL,
|
||||
BSHELL_SYM_HYPHEN_EQUAL,
|
||||
BSHELL_SYM_FORWARD_SLASH_EQUAL,
|
||||
BSHELL_SYM_ASTERISK_EQUAL,
|
||||
BSHELL_SYM_PERCENT_EQUAL,
|
||||
BSHELL_SYM_DOT,
|
||||
BSHELL_SYM_DOT_DOT,
|
||||
BSHELL_SYM_COLON_COLON,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_arithmetic_state = {
|
||||
.s_id = BSHELL_LEX_STATE_ARITHMETIC,
|
||||
.s_pump_token = arithmetic_pump_token,
|
||||
.s_links = links,
|
||||
.s_keywords = keywords,
|
||||
.s_operators = operators,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,228 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
#include <bshell/parse/token.h>
|
||||
|
||||
static bool char_can_continue_word(struct bshell_lex_ctx *ctx, fx_wchar c)
|
||||
{
|
||||
if (fx_wchar_is_alnum(c)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (fx_wchar_is_space(c)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (c == '$') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol_in_state(ctx, c, BSHELL_LEX_STATE_WORD)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static enum bshell_status command_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, sym->id);
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_DQUOTE:
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_SQUOTE:
|
||||
status = read_literal_string(ctx, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
|
||||
case BSHELL_SYM_HASH:
|
||||
return read_line_comment(ctx);
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (char_can_continue_word(ctx, peek_char(ctx))) {
|
||||
lex_state_push(ctx, BSHELL_LEX_STATE_WORD, 0);
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
||||
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
if (char_can_continue_word(ctx, peek_char(ctx))) {
|
||||
lex_state_push(ctx, BSHELL_LEX_STATE_WORD, 0);
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
push_symbol(ctx, sym->id);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static bool string_is_redirection(const char *s)
|
||||
{
|
||||
if (!*s) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!strcmp(s, ">") || !strcmp(s, ">>")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
long nr_angles = 0;
|
||||
for (size_t i = 0; s[i];) {
|
||||
fx_wchar c = fx_wchar_utf8_codepoint_decode(s);
|
||||
if (fx_wchar_is_number(c)) {
|
||||
if (nr_angles) {
|
||||
return false;
|
||||
}
|
||||
} else if (c == '>') {
|
||||
nr_angles++;
|
||||
|
||||
if (nr_angles > 2) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
s += fx_wchar_utf8_codepoint_stride(s);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static enum bshell_status command_word(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *word = NULL;
|
||||
enum bshell_status status
|
||||
= read_word(ctx, READ_NO_NUMBER_RECOGNITION, &word);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
bool continue_word = false;
|
||||
|
||||
fx_wchar c = peek_char(ctx);
|
||||
const char *s = word->tok_str;
|
||||
if (char_can_begin_symbol_in_state(ctx, c, BSHELL_LEX_STATE_WORD)) {
|
||||
continue_word = true;
|
||||
}
|
||||
|
||||
if (char_has_flags(ctx, c, BSHELL_LEX_TOKEN_TERMINATES_WORD)) {
|
||||
continue_word = false;
|
||||
}
|
||||
|
||||
if (string_is_redirection(s)) {
|
||||
continue_word = false;
|
||||
}
|
||||
|
||||
if (continue_word) {
|
||||
lex_state_push(ctx, BSHELL_LEX_STATE_WORD, 0);
|
||||
}
|
||||
|
||||
enqueue_token(ctx, word);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
enum bshell_status command_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
bool newline = false;
|
||||
|
||||
set_token_start(ctx);
|
||||
while (fx_wchar_is_space(c)) {
|
||||
if (c == '\n') {
|
||||
newline = true;
|
||||
}
|
||||
|
||||
set_token_end(ctx);
|
||||
advance_char_noread(ctx);
|
||||
c = peek_char_noread(ctx);
|
||||
}
|
||||
|
||||
if (newline) {
|
||||
struct bshell_lex_token *tok
|
||||
= bshell_lex_token_create(BSHELL_TOK_LINEFEED);
|
||||
enqueue_token(ctx, tok);
|
||||
handle_lex_state_transition(ctx, BSHELL_TOK_LINEFEED);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return command_symbol(ctx);
|
||||
}
|
||||
|
||||
return command_word(ctx);
|
||||
}
|
||||
|
||||
const struct bshell_lex_state_link links[] = {
|
||||
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
|
||||
LINK_PUSH(
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
STATEMENT_F_DISABLE_KEYWORDS),
|
||||
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
||||
LINK_POP(BSHELL_SYM_RIGHT_PAREN),
|
||||
LINK_POP(BSHELL_SYM_RIGHT_BRACE),
|
||||
LINK_CHANGE(BSHELL_SYM_SEMICOLON, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_PUSH(BSHELL_SYM_AT_LEFT_BRACE, BSHELL_LEX_STATE_HASHTABLE, 0),
|
||||
LINK_CHANGE(BSHELL_TOK_LINEFEED, BSHELL_LEX_STATE_STATEMENT),
|
||||
LINK_END,
|
||||
};
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_SQUOTE,
|
||||
BSHELL_SYM_DOLLAR,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_SYM_AT,
|
||||
BSHELL_SYM_AT_LEFT_BRACE,
|
||||
BSHELL_SYM_AT_LEFT_PAREN,
|
||||
BSHELL_SYM_AMPERSAND,
|
||||
BSHELL_SYM_PIPE,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_RIGHT_PAREN,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_command_state = {
|
||||
.s_id = BSHELL_LEX_STATE_COMMAND,
|
||||
.s_pump_token = command_pump_token,
|
||||
.s_links = links,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,185 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
static enum bshell_status hashtable_hyphen(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
if (!fx_wchar_is_alnum(c)) {
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
handle_lex_state_transition(ctx, BSHELL_SYM_HYPHEN);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
enum bshell_status status = read_word(
|
||||
ctx,
|
||||
READ_NO_SET_TOKEN_START | READ_APPEND_HYPHEN,
|
||||
&tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
unsigned int token_type = BSHELL_TOK_WORD;
|
||||
if (convert_word_to_number(tok)) {
|
||||
token_type = tok->tok_type;
|
||||
/* because of APPEND_HYPHEN (which is needed to ensure operator
|
||||
* tokens are detected properly), the resulting number will be
|
||||
* negative.
|
||||
* this token will be preceded by a HYPHEN token, so the number
|
||||
* must be positive */
|
||||
fx_value neg = FX_INT(-1);
|
||||
fx_value_multiply(&tok->tok_number, &neg, &tok->tok_number);
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
} else if (convert_word_to_operator(ctx, tok)) {
|
||||
token_type = tok->tok_operator;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, token_type);
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status hashtable_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, sym->id);
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_SQUOTE:
|
||||
status = read_literal_string(ctx, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_HYPHEN:
|
||||
return hashtable_hyphen(ctx);
|
||||
case BSHELL_SYM_HASH:
|
||||
return read_line_comment(ctx);
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
||||
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
push_symbol(ctx, sym->id);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status hashtable_word(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *word = NULL;
|
||||
enum bshell_status status = read_word(ctx, 0, &word);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
convert_word_to_number(word);
|
||||
|
||||
handle_lex_state_transition(ctx, word->tok_type);
|
||||
enqueue_token(ctx, word);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status hashtable_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
bool newline = false;
|
||||
|
||||
set_token_start(ctx);
|
||||
while (fx_wchar_is_space(c)) {
|
||||
if (c == '\n') {
|
||||
newline = true;
|
||||
}
|
||||
|
||||
set_token_end(ctx);
|
||||
advance_char_noread(ctx);
|
||||
c = peek_char_noread(ctx);
|
||||
}
|
||||
|
||||
#if 1
|
||||
if (newline) {
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_LINEFEED);
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return hashtable_symbol(ctx);
|
||||
}
|
||||
|
||||
return hashtable_word(ctx);
|
||||
}
|
||||
|
||||
static const struct bshell_lex_state_link links[] = {
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_EQUAL,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
0,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_TOK_LINEFEED),
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_TOK_LINEFEED,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
0,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_TOK_LINEFEED),
|
||||
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
|
||||
LINK_PUSH(
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
STATEMENT_F_DISABLE_KEYWORDS),
|
||||
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
||||
LINK_POP2(BSHELL_SYM_RIGHT_BRACE, LINK_ALLOW_RECURSION),
|
||||
LINK_END,
|
||||
};
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_EQUAL,
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_SQUOTE,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_HASH,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_hashtable_state = {
|
||||
.s_id = BSHELL_LEX_STATE_HASHTABLE,
|
||||
.s_pump_token = hashtable_pump_token,
|
||||
.s_links = links,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,204 @@
|
||||
#ifndef PARSE_LEX_INTERNAL_H_
|
||||
#define PARSE_LEX_INTERNAL_H_
|
||||
|
||||
#include <bshell/parse/lex.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/status.h>
|
||||
|
||||
struct bshell_lex_ctx;
|
||||
|
||||
enum state_flags {
|
||||
/* statement: don't convert matching words to keywords */
|
||||
STATEMENT_F_DISABLE_KEYWORDS = 0x01u,
|
||||
/* arithmetic: don't switch back to statement mode even when
|
||||
* encountering a token that would otherwise require it. */
|
||||
ARITHMETIC_F_DISABLE_STATEMENTS = 0x01u,
|
||||
};
|
||||
|
||||
enum read_flags {
|
||||
READ_APPEND_HYPHEN = 0x01u,
|
||||
READ_NO_SET_TOKEN_START = 0x02u,
|
||||
READ_NO_NUMBER_RECOGNITION = 0x04u,
|
||||
};
|
||||
|
||||
enum link_flags {
|
||||
LINK_ALLOW_RECURSION = 0x01u,
|
||||
};
|
||||
|
||||
#define LINK_PUSH(tok, target, flags) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_PUSH, \
|
||||
.l_target = (target), \
|
||||
.l_target_flags = (flags), \
|
||||
})
|
||||
#define LINK_PUSH_WITH_TERM(tok, target, flags, ...) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_PUSH, \
|
||||
.l_target = (target), \
|
||||
.l_target_flags = (flags), \
|
||||
.l_terminators = {__VA_ARGS__, BSHELL_TOK_NONE}, \
|
||||
})
|
||||
#define LINK_CHANGE(tok, target) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_CHANGE, \
|
||||
.l_target = (target), \
|
||||
})
|
||||
#define LINK_POP(tok) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_POP, \
|
||||
})
|
||||
#define LINK_POP2(tok, flags) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_POP, \
|
||||
.l_flags = (flags), \
|
||||
})
|
||||
#define LINK_NONE(tok) \
|
||||
((struct bshell_lex_state_link) { \
|
||||
.l_token = (tok), \
|
||||
.l_type = BSHELL_LEX_STATE_LINK_NONE, \
|
||||
})
|
||||
#define LINK_END ((struct bshell_lex_state_link) {})
|
||||
|
||||
struct bshell_lex_state_link {
|
||||
unsigned int l_token;
|
||||
enum {
|
||||
BSHELL_LEX_STATE_LINK_NONE,
|
||||
BSHELL_LEX_STATE_LINK_PUSH,
|
||||
BSHELL_LEX_STATE_LINK_CHANGE,
|
||||
BSHELL_LEX_STATE_LINK_POP,
|
||||
} l_type;
|
||||
enum link_flags l_flags;
|
||||
enum bshell_lex_state_id l_target;
|
||||
enum state_flags l_target_flags;
|
||||
unsigned int l_terminators[BSHELL_LEX_STATE_MAX_TERMINATORS];
|
||||
};
|
||||
|
||||
typedef enum bshell_status (*lex_state_pump_token)(struct bshell_lex_ctx *);
|
||||
typedef enum bshell_status (*lex_state_begin)(struct bshell_lex_ctx *);
|
||||
typedef enum bshell_status (*lex_state_end)(struct bshell_lex_ctx *);
|
||||
|
||||
struct bshell_lex_state_type {
|
||||
enum bshell_lex_state_id s_id;
|
||||
lex_state_pump_token s_pump_token;
|
||||
lex_state_begin s_begin;
|
||||
lex_state_end s_end;
|
||||
|
||||
const unsigned int *s_keywords;
|
||||
const unsigned int *s_operators;
|
||||
const unsigned int *s_symbols;
|
||||
const struct bshell_lex_state_link *s_links;
|
||||
};
|
||||
|
||||
extern enum bshell_status pump_token_statement(struct bshell_lex_ctx *ctx);
|
||||
extern enum bshell_status pump_token_expression(struct bshell_lex_ctx *ctx);
|
||||
extern enum bshell_status pump_token_command(struct bshell_lex_ctx *ctx);
|
||||
extern enum bshell_status pump_token_arithmetic(struct bshell_lex_ctx *ctx);
|
||||
extern enum bshell_status pump_token_string(struct bshell_lex_ctx *ctx);
|
||||
|
||||
extern void set_token_start(struct bshell_lex_ctx *ctx);
|
||||
extern void set_token_end(struct bshell_lex_ctx *ctx);
|
||||
|
||||
extern struct bshell_lex_state *lex_state_push(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_state_id state_type,
|
||||
enum state_flags flags);
|
||||
extern void lex_state_pop(struct bshell_lex_ctx *ctx);
|
||||
extern struct bshell_lex_state *lex_state_get(struct bshell_lex_ctx *ctx);
|
||||
extern void lex_state_change(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_state_id type);
|
||||
extern fx_string *lex_state_get_tempstr(struct bshell_lex_ctx *ctx);
|
||||
extern void lex_state_add_terminator(
|
||||
struct bshell_lex_state *state,
|
||||
unsigned int tok);
|
||||
extern bool lex_state_terminates_at_token(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
unsigned int tok);
|
||||
|
||||
extern fx_wchar peek_char(struct bshell_lex_ctx *ctx);
|
||||
extern fx_wchar peek_char_noread(struct bshell_lex_ctx *ctx);
|
||||
extern fx_wchar peek2_char(struct bshell_lex_ctx *ctx);
|
||||
extern fx_wchar peek2_char_noread(struct bshell_lex_ctx *ctx);
|
||||
extern void advance_char(struct bshell_lex_ctx *ctx);
|
||||
extern void advance_char_noread(struct bshell_lex_ctx *ctx);
|
||||
|
||||
extern bool string_is_valid_number(
|
||||
const char *s,
|
||||
fx_value *out,
|
||||
enum bshell_lex_token_type *out_type);
|
||||
extern bool convert_word_to_number(struct bshell_lex_token *tok);
|
||||
extern bool convert_word_to_keyword(struct bshell_lex_token *tok);
|
||||
extern bool convert_word_to_operator(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
struct bshell_lex_token *tok);
|
||||
|
||||
extern void enqueue_token(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
struct bshell_lex_token *tok);
|
||||
extern void enqueue_token_with_coordinates(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
struct bshell_lex_token *tok,
|
||||
const struct bshell_char_cell *start,
|
||||
const struct bshell_char_cell *end);
|
||||
|
||||
extern enum bshell_status read_word(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum read_flags flags,
|
||||
struct bshell_lex_token **out);
|
||||
extern enum bshell_status read_symbol(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
const struct bshell_lex_token_definition **out);
|
||||
extern enum bshell_status read_literal_string(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
struct bshell_lex_token **out);
|
||||
extern enum bshell_status read_line_comment(struct bshell_lex_ctx *lex);
|
||||
extern enum bshell_status read_var(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_token_type type,
|
||||
struct bshell_lex_token **out);
|
||||
extern enum bshell_status read_braced_var(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_token_type type,
|
||||
struct bshell_lex_token **out);
|
||||
|
||||
extern enum bshell_status push_symbol(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_symbol sym);
|
||||
|
||||
extern bool char_can_begin_symbol(struct bshell_lex_ctx *ctx, char c);
|
||||
extern bool char_can_begin_symbol_in_state(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
char c,
|
||||
enum bshell_lex_state_id state_type);
|
||||
extern bool char_has_flags(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
char c,
|
||||
enum bshell_lex_token_flags flags);
|
||||
extern bool keyword_has_flags(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_keyword kw,
|
||||
enum bshell_lex_token_flags flags);
|
||||
extern enum bshell_lex_token_flags keyword_get_flags(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_keyword kw);
|
||||
extern bool symbol_has_flags(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_symbol sym,
|
||||
enum bshell_lex_token_flags flags);
|
||||
extern enum bshell_lex_token_flags symbol_get_flags(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
enum bshell_lex_symbol sym);
|
||||
extern enum bshell_lex_operator get_bshell_operator_with_string(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
const char *s);
|
||||
|
||||
extern void handle_lex_state_transition(
|
||||
struct bshell_lex_ctx *ctx,
|
||||
unsigned int token);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,254 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
static enum bshell_status statement_hyphen(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
if (!fx_wchar_is_alnum(c)) {
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
handle_lex_state_transition(ctx, BSHELL_SYM_HYPHEN);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
enum bshell_status status = read_word(
|
||||
ctx,
|
||||
READ_NO_SET_TOKEN_START | READ_APPEND_HYPHEN,
|
||||
&tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
unsigned int token_type = BSHELL_TOK_WORD;
|
||||
|
||||
if (convert_word_to_number(tok)) {
|
||||
token_type = tok->tok_type;
|
||||
|
||||
/* because of APPEND_HYPHEN (which is needed to ensure operator
|
||||
* tokens are detected properly), the resulting number will be
|
||||
* negative.
|
||||
* this token will be preceded by a HYPHEN token, so the number
|
||||
* must be positive */
|
||||
fx_value neg = FX_INT(-1);
|
||||
fx_value_multiply(&tok->tok_number, &neg, &tok->tok_number);
|
||||
push_symbol(ctx, BSHELL_SYM_HYPHEN);
|
||||
} else if (convert_word_to_operator(ctx, tok)) {
|
||||
token_type = tok->tok_operator;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, token_type);
|
||||
enqueue_token(ctx, tok);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status statement_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, sym->id);
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_DQUOTE:
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_HYPHEN:
|
||||
return statement_hyphen(ctx);
|
||||
case BSHELL_SYM_SQUOTE:
|
||||
status = read_literal_string(ctx, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
|
||||
case BSHELL_SYM_HASH:
|
||||
return read_line_comment(ctx);
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
||||
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
push_symbol(ctx, sym->id);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status statement_word(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *word = NULL;
|
||||
enum bshell_status status = read_word(ctx, 0, &word);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
struct bshell_lex_state *state = lex_state_get(ctx);
|
||||
|
||||
bool enable_keywords = !(state->s_flags & STATEMENT_F_DISABLE_KEYWORDS);
|
||||
unsigned int token = BSHELL_TOK_WORD;
|
||||
|
||||
if (enable_keywords && convert_word_to_keyword(word)) {
|
||||
token = word->tok_keyword;
|
||||
} else if (convert_word_to_number(word)) {
|
||||
token = word->tok_type;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, token);
|
||||
|
||||
enqueue_token(ctx, word);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status statement_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
bool newline = false;
|
||||
|
||||
set_token_start(ctx);
|
||||
while (fx_wchar_is_space(c)) {
|
||||
if (c == '\n') {
|
||||
newline = true;
|
||||
}
|
||||
|
||||
set_token_end(ctx);
|
||||
advance_char_noread(ctx);
|
||||
c = peek_char_noread(ctx);
|
||||
}
|
||||
|
||||
if (newline) {
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_LINEFEED);
|
||||
enqueue_token(ctx, tok);
|
||||
handle_lex_state_transition(ctx, BSHELL_TOK_LINEFEED);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return statement_symbol(ctx);
|
||||
}
|
||||
|
||||
if (char_has_flags(ctx, c, BSHELL_LEX_TOKEN_UNARY_ARITHMETIC)) {
|
||||
lex_state_change(ctx, BSHELL_LEX_STATE_ARITHMETIC);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
return statement_word(ctx);
|
||||
}
|
||||
|
||||
static const struct bshell_lex_state_link links[] = {
|
||||
LINK_PUSH(BSHELL_SYM_DQUOTE, BSHELL_LEX_STATE_STRING, 0),
|
||||
/* arithmetic tokens */
|
||||
LINK_CHANGE(BSHELL_TOK_KEYWORD, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_SYM_SQUOTE, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_TOK_INT, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_TOK_DOUBLE, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_PUSH(BSHELL_SYM_DOLLAR, BSHELL_LEX_STATE_ARITHMETIC, 0),
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_LEX_STATE_ARITHMETIC,
|
||||
0,
|
||||
BSHELL_SYM_RIGHT_BRACE),
|
||||
LINK_CHANGE(BSHELL_SYM_AT_LEFT_BRACE, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_SYM_AT_LEFT_PAREN, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_AT_LEFT_BRACE,
|
||||
BSHELL_LEX_STATE_HASHTABLE,
|
||||
0,
|
||||
BSHELL_SYM_RIGHT_BRACE),
|
||||
LINK_PUSH(BSHELL_SYM_AT, BSHELL_LEX_STATE_ARITHMETIC, 0),
|
||||
LINK_CHANGE(BSHELL_SYM_LEFT_PAREN, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_SYM_LEFT_BRACKET, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_CHANGE(BSHELL_SYM_BANG, BSHELL_LEX_STATE_ARITHMETIC),
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
STATEMENT_F_DISABLE_KEYWORDS,
|
||||
BSHELL_SYM_RIGHT_PAREN),
|
||||
|
||||
/* statement tokens */
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
0,
|
||||
BSHELL_SYM_RIGHT_BRACE),
|
||||
LINK_PUSH_WITH_TERM(
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_LEX_STATE_STATEMENT,
|
||||
0,
|
||||
BSHELL_SYM_RIGHT_PAREN),
|
||||
|
||||
/* command tokens */
|
||||
LINK_CHANGE(BSHELL_KW_FUNC, BSHELL_LEX_STATE_COMMAND),
|
||||
LINK_CHANGE(BSHELL_SYM_AMPERSAND, BSHELL_LEX_STATE_COMMAND),
|
||||
LINK_CHANGE(BSHELL_TOK_WORD, BSHELL_LEX_STATE_COMMAND),
|
||||
LINK_END,
|
||||
};
|
||||
|
||||
static const unsigned int keywords[] = {
|
||||
BSHELL_KW_FUNC,
|
||||
BSHELL_KW_IF,
|
||||
BSHELL_KW_ELSEIF,
|
||||
BSHELL_KW_ELSE,
|
||||
BSHELL_KW_NONE,
|
||||
};
|
||||
|
||||
static const unsigned int operators[] = {
|
||||
BSHELL_TKOP_BNOT,
|
||||
BSHELL_TKOP_NOT,
|
||||
BSHELL_TKOP_NONE,
|
||||
};
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_AMPERSAND,
|
||||
BSHELL_SYM_BANG,
|
||||
BSHELL_SYM_SQUOTE,
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_HASH,
|
||||
BSHELL_SYM_AT,
|
||||
BSHELL_SYM_AT_LEFT_PAREN,
|
||||
BSHELL_SYM_AT_LEFT_BRACE,
|
||||
BSHELL_SYM_PIPE,
|
||||
BSHELL_SYM_COMMA,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_LEFT_BRACKET,
|
||||
BSHELL_SYM_RIGHT_BRACKET,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_RIGHT_PAREN,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_statement_state = {
|
||||
.s_id = BSHELL_LEX_STATE_STATEMENT,
|
||||
.s_pump_token = statement_pump_token,
|
||||
.s_links = links,
|
||||
.s_keywords = keywords,
|
||||
.s_operators = operators,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,141 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
static enum bshell_status string_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
handle_lex_state_transition(ctx, sym->id);
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_DQUOTE:
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
|
||||
return push_symbol(ctx, sym->id);
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_DOLLAR_LEFT_BRACE:
|
||||
status = read_braced_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
static enum bshell_status string_content(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = FX_WCHAR_INVALID;
|
||||
fx_string *temp = lex_state_get_tempstr(ctx);
|
||||
set_token_start(ctx);
|
||||
fx_string_clear(temp);
|
||||
|
||||
while (1) {
|
||||
c = peek_char(ctx);
|
||||
if (c == FX_WCHAR_INVALID) {
|
||||
/* EOF without end of string */
|
||||
ctx->lex_status = BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
fx_string_append_wc(temp, c);
|
||||
set_token_end(ctx);
|
||||
advance_char(ctx);
|
||||
}
|
||||
|
||||
if (fx_string_get_size(temp, FX_STRLEN_NORMAL) == 0) {
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create_with_string(
|
||||
BSHELL_TOK_STRING,
|
||||
fx_string_get_cstr(temp));
|
||||
enqueue_token(ctx, tok);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status string_begin(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_STR_START);
|
||||
if (!tok) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status string_end(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_STR_END);
|
||||
if (!tok) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status string_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return string_symbol(ctx);
|
||||
}
|
||||
|
||||
return string_content(ctx);
|
||||
}
|
||||
|
||||
static const struct bshell_lex_state_link links[] = {
|
||||
LINK_PUSH(BSHELL_SYM_DOLLAR_LEFT_PAREN, BSHELL_LEX_STATE_STATEMENT, 0),
|
||||
LINK_POP(BSHELL_SYM_DQUOTE),
|
||||
LINK_END,
|
||||
};
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_DOLLAR,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_SYM_DQUOTE,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_string_state = {
|
||||
.s_id = BSHELL_LEX_STATE_STRING,
|
||||
.s_begin = string_begin,
|
||||
.s_end = string_end,
|
||||
.s_pump_token = string_pump_token,
|
||||
.s_links = links,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,162 @@
|
||||
#include "lex-internal.h"
|
||||
|
||||
static enum bshell_status word_symbol(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
const struct bshell_lex_token_definition *sym = NULL;
|
||||
enum bshell_status status = read_symbol(ctx, &sym);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = NULL;
|
||||
|
||||
switch (sym->id) {
|
||||
case BSHELL_SYM_DOLLAR_LEFT_PAREN:
|
||||
status = push_symbol(ctx, sym->id);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
lex_state_push(ctx, BSHELL_LEX_STATE_STATEMENT, 0);
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_RIGHT_PAREN:
|
||||
lex_state_pop(ctx);
|
||||
|
||||
status = push_symbol(ctx, sym->id);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
return BSHELL_SUCCESS;
|
||||
case BSHELL_SYM_DOLLAR:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
case BSHELL_SYM_AT:
|
||||
status = read_var(ctx, BSHELL_TOK_VAR_SPLAT, &tok);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
enqueue_token(ctx, tok);
|
||||
return status;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
static enum bshell_status word_content(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = FX_WCHAR_INVALID;
|
||||
fx_string *temp = lex_state_get_tempstr(ctx);
|
||||
set_token_start(ctx);
|
||||
fx_string_clear(temp);
|
||||
|
||||
while (1) {
|
||||
c = peek_char(ctx);
|
||||
if (c == FX_WCHAR_INVALID) {
|
||||
/* EOF without end of word */
|
||||
ctx->lex_status = BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
if (fx_wchar_is_space(c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
break;
|
||||
}
|
||||
|
||||
fx_string_append_wc(temp, c);
|
||||
set_token_end(ctx);
|
||||
advance_char(ctx);
|
||||
}
|
||||
|
||||
if (fx_string_get_size(temp, FX_STRLEN_NORMAL) == 0) {
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create_with_string(
|
||||
BSHELL_TOK_WORD,
|
||||
fx_string_get_cstr(temp));
|
||||
enqueue_token(ctx, tok);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status word_begin(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_WORD_START);
|
||||
if (!tok) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enqueue_token_with_coordinates(
|
||||
ctx,
|
||||
tok,
|
||||
&ctx->lex_start,
|
||||
&ctx->lex_start);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status word_end(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(BSHELL_TOK_WORD_END);
|
||||
if (!tok) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enqueue_token_with_coordinates(ctx, tok, &ctx->lex_end, &ctx->lex_end);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status word_pump_token(struct bshell_lex_ctx *ctx)
|
||||
{
|
||||
fx_wchar c = peek_char(ctx);
|
||||
|
||||
if (fx_wchar_is_space(c)) {
|
||||
lex_state_pop(ctx);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
if (char_has_flags(ctx, c, BSHELL_LEX_TOKEN_TERMINATES_WORD)) {
|
||||
lex_state_pop(ctx);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
if (char_can_begin_symbol(ctx, c)) {
|
||||
return word_symbol(ctx);
|
||||
}
|
||||
|
||||
return word_content(ctx);
|
||||
}
|
||||
|
||||
static const unsigned int symbols[] = {
|
||||
BSHELL_SYM_AMPERSAND,
|
||||
BSHELL_SYM_HASH,
|
||||
BSHELL_SYM_DOLLAR,
|
||||
BSHELL_SYM_DOLLAR_LEFT_PAREN,
|
||||
BSHELL_SYM_DOLLAR_LEFT_BRACE,
|
||||
BSHELL_SYM_PIPE,
|
||||
BSHELL_SYM_COMMA,
|
||||
BSHELL_SYM_SEMICOLON,
|
||||
BSHELL_SYM_LEFT_BRACE,
|
||||
BSHELL_SYM_RIGHT_BRACE,
|
||||
BSHELL_SYM_LEFT_PAREN,
|
||||
BSHELL_SYM_RIGHT_PAREN,
|
||||
BSHELL_SYM_NONE,
|
||||
};
|
||||
|
||||
const struct bshell_lex_state_type lex_word_state = {
|
||||
.s_id = BSHELL_LEX_STATE_WORD,
|
||||
.s_begin = word_begin,
|
||||
.s_end = word_end,
|
||||
.s_pump_token = word_pump_token,
|
||||
.s_symbols = symbols,
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "syntax.h"
|
||||
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/lex.h>
|
||||
#include <bshell/parse/parse.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
enum bshell_status bshell_parse_ctx_init(struct bshell_parse_ctx *ctx, struct bshell_lex_ctx *src)
|
||||
{
|
||||
memset(ctx, 0x0, sizeof *ctx);
|
||||
|
||||
ctx->p_src = src;
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
void bshell_parse_ctx_cleanup(struct bshell_parse_ctx *ctx)
|
||||
{
|
||||
}
|
||||
|
||||
struct bshell_ast_node *bshell_parse_ctx_read_node(struct bshell_parse_ctx *ctx)
|
||||
{
|
||||
parse_symbol(ctx, BSHELL_SYM_SEMICOLON);
|
||||
parse_linefeed(ctx);
|
||||
|
||||
struct bshell_ast_node *result = NULL;
|
||||
bool ok = parse_statement(ctx, &result);
|
||||
|
||||
return ok ? result : NULL;
|
||||
}
|
||||
|
||||
void report_error(struct bshell_parse_ctx *ctx, const char *format, ...)
|
||||
{
|
||||
ctx->p_status = BSHELL_ERR_BAD_SYNTAX;
|
||||
fprintf(stderr, "PARSE: ");
|
||||
|
||||
va_list arg;
|
||||
va_start(arg, format);
|
||||
vfprintf(stderr, format, arg);
|
||||
va_end(arg);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
struct bshell_lex_token *tok = peek_token(ctx);
|
||||
fprintf(stderr, " peek_token = ");
|
||||
#if 0
|
||||
if (tok) {
|
||||
print_bshell_lex_token(tok);
|
||||
} else {
|
||||
fprintf(stderr, " EOF\n");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#ifndef PARSE_SYNTAX_H_
|
||||
#define PARSE_SYNTAX_H_
|
||||
|
||||
#include <bshell/ast.h>
|
||||
#include <bshell/parse/lex.h>
|
||||
#include <bshell/parse/parse.h>
|
||||
#include <bshell/parse/token.h>
|
||||
#include <bshell/runtime/operator.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern void report_error(struct bshell_parse_ctx *ctx, const char *format, ...);
|
||||
|
||||
extern struct bshell_lex_token *peek_token(struct bshell_parse_ctx *ctx);
|
||||
extern enum bshell_lex_token_type peek_token_type(struct bshell_parse_ctx *ctx);
|
||||
extern enum bshell_lex_keyword peek_unknown_keyword(struct bshell_parse_ctx *ctx);
|
||||
extern enum bshell_lex_symbol peek_unknown_symbol(struct bshell_parse_ctx *ctx);
|
||||
|
||||
extern struct bshell_lex_token *claim_token(struct bshell_parse_ctx *ctx);
|
||||
extern void discard_token(struct bshell_parse_ctx *ctx);
|
||||
|
||||
extern bool peek_linefeed(struct bshell_parse_ctx *ctx);
|
||||
extern bool peek_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym);
|
||||
extern bool peek_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out);
|
||||
extern bool peek_int(struct bshell_parse_ctx *ctx);
|
||||
|
||||
extern bool parse_linefeed(struct bshell_parse_ctx *ctx);
|
||||
extern bool parse_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym);
|
||||
extern bool parse_keyword(struct bshell_parse_ctx *ctx, enum bshell_lex_keyword kw);
|
||||
extern bool parse_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out);
|
||||
extern bool parse_var(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out);
|
||||
extern bool parse_flag(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out);
|
||||
|
||||
extern bool peek_arith_expr(struct bshell_parse_ctx *ctx);
|
||||
extern bool parse_arith_value(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_ast_node **out);
|
||||
extern bool parse_arith_expr(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
enum bshell_operator_precedence minimum_precedence,
|
||||
struct bshell_ast_node **out);
|
||||
|
||||
extern bool peek_keyword_expr(struct bshell_parse_ctx *ctx);
|
||||
extern bool parse_keyword_expr(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_ast_node **out);
|
||||
|
||||
extern bool parse_if(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
extern bool parse_func(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
|
||||
extern bool parse_fstring(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
|
||||
extern bool parse_block(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
|
||||
extern bool peek_command(struct bshell_parse_ctx *ctx);
|
||||
extern bool parse_pipeline(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_ast_node *first_item,
|
||||
struct bshell_ast_node **out);
|
||||
extern bool parse_command(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
extern bool parse_cmdcall(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
extern bool parse_redirect(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
|
||||
extern bool parse_expr(struct bshell_parse_ctx *ctx, struct bshell_ast_node **out);
|
||||
extern bool peek_statement(struct bshell_parse_ctx *ctx);
|
||||
extern bool parse_statement(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_ast_node **out);
|
||||
extern bool parse_statement_list(
|
||||
struct bshell_parse_ctx *ctx,
|
||||
struct bshell_ast_node **out);
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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, ¶m_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,
|
||||
¶m_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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
#include <bshell/parse/token.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct bshell_lex_token *bshell_lex_token_create(
|
||||
enum bshell_lex_token_type type)
|
||||
{
|
||||
struct bshell_lex_token *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->tok_type = type;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
struct bshell_lex_token *bshell_lex_token_create_with_string(
|
||||
enum bshell_lex_token_type type,
|
||||
const char *s)
|
||||
{
|
||||
struct bshell_lex_token *tok = bshell_lex_token_create(type);
|
||||
if (!tok) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tok->tok_str = fx_strdup(s);
|
||||
if (!tok->tok_str) {
|
||||
free(tok);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return tok;
|
||||
}
|
||||
|
||||
void bshell_lex_token_destroy(struct bshell_lex_token *tok)
|
||||
{
|
||||
switch (tok->tok_type) {
|
||||
case BSHELL_TOK_WORD:
|
||||
case BSHELL_TOK_FLAG:
|
||||
case BSHELL_TOK_STRING:
|
||||
if (tok->tok_str) {
|
||||
free(tok->tok_str);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
free(tok);
|
||||
}
|
||||
|
||||
struct bshell_lex_token *bshell_lex_token_change_type(
|
||||
struct bshell_lex_token *tok,
|
||||
enum bshell_lex_token_type new_type)
|
||||
{
|
||||
switch (tok->tok_type) {
|
||||
case BSHELL_TOK_WORD:
|
||||
case BSHELL_TOK_FLAG:
|
||||
case BSHELL_TOK_STRING:
|
||||
if (tok->tok_str) {
|
||||
free(tok->tok_str);
|
||||
tok->tok_str = NULL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
tok->tok_type = new_type;
|
||||
return tok;
|
||||
}
|
||||
|
||||
void bshell_lex_token_change_string(struct bshell_lex_token *tok, const char *s)
|
||||
{
|
||||
if (!bshell_lex_token_has_string_value(tok)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (tok->tok_str) {
|
||||
free(tok->tok_str);
|
||||
}
|
||||
|
||||
tok->tok_str = fx_strdup(s);
|
||||
}
|
||||
|
||||
#define ENUM_STR(x) \
|
||||
case x: \
|
||||
return #x
|
||||
|
||||
const char *bshell_token_type_to_string(enum bshell_lex_token_type type)
|
||||
{
|
||||
switch (type) {
|
||||
ENUM_STR(BSHELL_TOK_NONE);
|
||||
ENUM_STR(BSHELL_TOK_KEYWORD);
|
||||
ENUM_STR(BSHELL_TOK_SYMBOL);
|
||||
ENUM_STR(BSHELL_TOK_INT);
|
||||
ENUM_STR(BSHELL_TOK_DOUBLE);
|
||||
ENUM_STR(BSHELL_TOK_WORD);
|
||||
ENUM_STR(BSHELL_TOK_WORD_START);
|
||||
ENUM_STR(BSHELL_TOK_WORD_END);
|
||||
ENUM_STR(BSHELL_TOK_OPERATOR);
|
||||
ENUM_STR(BSHELL_TOK_VAR);
|
||||
ENUM_STR(BSHELL_TOK_VAR_SPLAT);
|
||||
ENUM_STR(BSHELL_TOK_FLAG);
|
||||
ENUM_STR(BSHELL_TOK_STRING);
|
||||
ENUM_STR(BSHELL_TOK_STR_START);
|
||||
ENUM_STR(BSHELL_TOK_STR_END);
|
||||
ENUM_STR(BSHELL_TOK_LINEFEED);
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
const char *bshell_lex_keyword_to_string(enum bshell_lex_keyword keyword)
|
||||
{
|
||||
switch (keyword) {
|
||||
ENUM_STR(BSHELL_KW_NONE);
|
||||
ENUM_STR(BSHELL_KW_FUNC);
|
||||
ENUM_STR(BSHELL_KW_IF);
|
||||
ENUM_STR(BSHELL_KW_ELSEIF);
|
||||
ENUM_STR(BSHELL_KW_ELSE);
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
const char *bshell_lex_symbol_to_string(enum bshell_lex_symbol sym)
|
||||
{
|
||||
switch (sym) {
|
||||
ENUM_STR(BSHELL_SYM_NONE);
|
||||
ENUM_STR(BSHELL_SYM_PLUS);
|
||||
ENUM_STR(BSHELL_SYM_HYPHEN);
|
||||
ENUM_STR(BSHELL_SYM_FORWARD_SLASH);
|
||||
ENUM_STR(BSHELL_SYM_ASTERISK);
|
||||
ENUM_STR(BSHELL_SYM_AMPERSAND);
|
||||
ENUM_STR(BSHELL_SYM_PERCENT);
|
||||
ENUM_STR(BSHELL_SYM_SQUOTE);
|
||||
ENUM_STR(BSHELL_SYM_DQUOTE);
|
||||
ENUM_STR(BSHELL_SYM_HASH);
|
||||
ENUM_STR(BSHELL_SYM_COLON_COLON);
|
||||
ENUM_STR(BSHELL_SYM_SEMICOLON);
|
||||
ENUM_STR(BSHELL_SYM_COMMA);
|
||||
ENUM_STR(BSHELL_SYM_DOLLAR);
|
||||
ENUM_STR(BSHELL_SYM_DOLLAR_LEFT_PAREN);
|
||||
ENUM_STR(BSHELL_SYM_DOLLAR_LEFT_BRACE);
|
||||
ENUM_STR(BSHELL_SYM_DOT);
|
||||
ENUM_STR(BSHELL_SYM_DOT_DOT);
|
||||
ENUM_STR(BSHELL_SYM_PIPE);
|
||||
ENUM_STR(BSHELL_SYM_AT);
|
||||
ENUM_STR(BSHELL_SYM_AT_LEFT_PAREN);
|
||||
ENUM_STR(BSHELL_SYM_AT_LEFT_BRACE);
|
||||
ENUM_STR(BSHELL_SYM_LEFT_BRACE);
|
||||
ENUM_STR(BSHELL_SYM_RIGHT_BRACE);
|
||||
ENUM_STR(BSHELL_SYM_LEFT_BRACKET);
|
||||
ENUM_STR(BSHELL_SYM_RIGHT_BRACKET);
|
||||
ENUM_STR(BSHELL_SYM_LEFT_PAREN);
|
||||
ENUM_STR(BSHELL_SYM_RIGHT_PAREN);
|
||||
ENUM_STR(BSHELL_SYM_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_PLUS_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_HYPHEN_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_ASTERISK_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_FORWARD_SLASH_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_PERCENT_EQUAL);
|
||||
ENUM_STR(BSHELL_SYM_QUESTION_DOT);
|
||||
ENUM_STR(BSHELL_SYM_QUESTION_LEFT_BRACKET);
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
const char *bshell_lex_operator_to_string(enum bshell_lex_operator op)
|
||||
{
|
||||
switch (op) {
|
||||
ENUM_STR(BSHELL_TKOP_BAND);
|
||||
ENUM_STR(BSHELL_TKOP_BOR);
|
||||
ENUM_STR(BSHELL_TKOP_BXOR);
|
||||
ENUM_STR(BSHELL_TKOP_BNOT);
|
||||
ENUM_STR(BSHELL_TKOP_SHL);
|
||||
ENUM_STR(BSHELL_TKOP_SHR);
|
||||
ENUM_STR(BSHELL_TKOP_EQ);
|
||||
ENUM_STR(BSHELL_TKOP_NE);
|
||||
ENUM_STR(BSHELL_TKOP_GT);
|
||||
ENUM_STR(BSHELL_TKOP_LT);
|
||||
ENUM_STR(BSHELL_TKOP_GE);
|
||||
ENUM_STR(BSHELL_TKOP_LE);
|
||||
ENUM_STR(BSHELL_TKOP_MATCH);
|
||||
ENUM_STR(BSHELL_TKOP_NOTMATCH);
|
||||
ENUM_STR(BSHELL_TKOP_REPLACE);
|
||||
ENUM_STR(BSHELL_TKOP_LIKE);
|
||||
ENUM_STR(BSHELL_TKOP_NOTLIKE);
|
||||
ENUM_STR(BSHELL_TKOP_IN);
|
||||
ENUM_STR(BSHELL_TKOP_F);
|
||||
ENUM_STR(BSHELL_TKOP_NOTIN);
|
||||
ENUM_STR(BSHELL_TKOP_CONTAINS);
|
||||
ENUM_STR(BSHELL_TKOP_NOTCONTAINS);
|
||||
ENUM_STR(BSHELL_TKOP_AND);
|
||||
ENUM_STR(BSHELL_TKOP_OR);
|
||||
ENUM_STR(BSHELL_TKOP_XOR);
|
||||
ENUM_STR(BSHELL_TKOP_NOT);
|
||||
ENUM_STR(BSHELL_TKOP_SPLIT);
|
||||
ENUM_STR(BSHELL_TKOP_JOIN);
|
||||
ENUM_STR(BSHELL_TKOP_IS);
|
||||
ENUM_STR(BSHELL_TKOP_ISNOT);
|
||||
ENUM_STR(BSHELL_TKOP_AS);
|
||||
default:
|
||||
return "<unknown>";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
#include <bshell/command/alias.h>
|
||||
#include <bshell/command/command.h>
|
||||
#include <bshell/runtime/cmdcall.h>
|
||||
#include <bshell/runtime/pipeline.h>
|
||||
#include <bshell/runtime/runtime.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/reflection/assembly.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/reflection/type.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/term/print.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
enum cmdcall_type {
|
||||
CMDCALL_NONE = 0,
|
||||
CMDCALL_CMDLET,
|
||||
CMDCALL_NATIVE,
|
||||
};
|
||||
|
||||
struct bshell_cmdcall_p {
|
||||
enum cmdcall_type cmd_type;
|
||||
bshell_command *cmd_target;
|
||||
char *cmd_native_path;
|
||||
bool cmd_initialised;
|
||||
FX_VECTOR_DECLARE(fx_value, cmd_args);
|
||||
fx_array *cmd_args_processed;
|
||||
};
|
||||
|
||||
static void cmdcall_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct bshell_cmdcall_p *cmdcall = priv;
|
||||
|
||||
if (cmdcall->cmd_native_path) {
|
||||
free(cmdcall->cmd_native_path);
|
||||
}
|
||||
|
||||
if (cmdcall->cmd_args_processed) {
|
||||
fx_array_unref(cmdcall->cmd_args_processed);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < cmdcall->cmd_args.count; i++) {
|
||||
fx_value_unset(&cmdcall->cmd_args.items[i]);
|
||||
}
|
||||
|
||||
fx_vector_destroy(cmdcall->cmd_args, NULL);
|
||||
|
||||
if (cmdcall->cmd_target) {
|
||||
bshell_command_end_processing(cmdcall->cmd_target);
|
||||
bshell_command_unref(cmdcall->cmd_target);
|
||||
}
|
||||
}
|
||||
|
||||
static enum bshell_status cmdcall_push_arg(
|
||||
struct bshell_cmdcall_p *cmdcall,
|
||||
fx_value arg)
|
||||
{
|
||||
fx_value *slot = fx_vector_emplace_back(cmdcall->cmd_args, NULL);
|
||||
fx_value_copy(slot, &arg);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static const fx_value *get_arg(struct bshell_cmdcall_p *cmdcall, size_t index)
|
||||
{
|
||||
if (index >= cmdcall->cmd_args.count) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &cmdcall->cmd_args.items[cmdcall->cmd_args.count - index - 1];
|
||||
}
|
||||
|
||||
static bshell_command *__resolve_call_target(
|
||||
const char *callable_name,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
return bshell_runtime_find_command(rt, callable_name);
|
||||
}
|
||||
|
||||
static enum bshell_status resolve_call_target(
|
||||
struct bshell_cmdcall_p *cmdcall,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
if (cmdcall->cmd_type != CMDCALL_NONE) {
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
fx_value *cmd_name_v = fx_array_get_ref(cmdcall->cmd_args_processed, 0);
|
||||
if (!cmd_name_v) {
|
||||
return BSHELL_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
fx_string *cmd_name_s = NULL;
|
||||
fx_value_get_object(cmd_name_v, &cmd_name_s);
|
||||
|
||||
while (1) {
|
||||
const char *cmd_name_cstr = fx_string_get_cstr(cmd_name_s);
|
||||
bshell_command *cmd
|
||||
= bshell_runtime_find_command(rt, cmd_name_cstr);
|
||||
if (!cmd) {
|
||||
break;
|
||||
}
|
||||
|
||||
bshell_alias_class *alias
|
||||
= fx_object_get_interface(cmd, BSHELL_TYPE_ALIAS);
|
||||
if (alias) {
|
||||
fx_string_clear(cmd_name_s);
|
||||
fx_string_append_cstr(cmd_name_s, alias->a_target_name);
|
||||
bshell_command_unref(cmd);
|
||||
continue;
|
||||
}
|
||||
|
||||
cmdcall->cmd_target = cmd;
|
||||
cmdcall->cmd_type = CMDCALL_CMDLET;
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
const char *cmd_name_cstr = fx_string_get_cstr(cmd_name_s);
|
||||
fx_printf(
|
||||
"[bold,bright_red]%s: The term '%s' is not recognised as a "
|
||||
"name of a cmdlet, "
|
||||
"function, script file, or executable program.[reset]\n",
|
||||
cmd_name_cstr,
|
||||
cmd_name_cstr);
|
||||
return BSHELL_ERR_NO_ENTRY;
|
||||
}
|
||||
|
||||
static enum bshell_status process_args(struct bshell_cmdcall_p *cmdcall)
|
||||
{
|
||||
if (cmdcall->cmd_args_processed) {
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
fx_stringstream *str = fx_stringstream_create();
|
||||
if (!str) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
cmdcall->cmd_args_processed = fx_array_create();
|
||||
if (!cmdcall->cmd_args_processed) {
|
||||
fx_stringstream_unref(str);
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
for (i32 i = cmdcall->cmd_args.count - 1; i >= 0; i--) {
|
||||
fx_stringstream_reset(str);
|
||||
fx_value_to_string(&cmdcall->cmd_args.items[i], str, NULL);
|
||||
fx_string *arg_str
|
||||
= fx_string_create_from_cstr(fx_stringstream_ptr(str));
|
||||
fx_array_push_back(
|
||||
cmdcall->cmd_args_processed,
|
||||
FX_VALUE_OBJECT(arg_str));
|
||||
fx_string_unref(arg_str);
|
||||
}
|
||||
|
||||
fx_stringstream_unref(str);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status cmdcall_resolve(
|
||||
struct bshell_cmdcall_p *cmdcall,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
enum bshell_status status = process_args(cmdcall);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = resolve_call_target(cmdcall, rt);
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
|
||||
bshell_command_set_args_reverse(
|
||||
cmdcall->cmd_target,
|
||||
cmdcall->cmd_args.items,
|
||||
cmdcall->cmd_args.count);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status cmdcall_execute(struct bshell_cmdcall_p *cmdcall)
|
||||
{
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status cmdcall_process_record(
|
||||
struct bshell_cmdcall_p *cmdcall,
|
||||
bshell_pipeline *pipeline,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
if (!cmdcall->cmd_target) {
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
if (!cmdcall->cmd_initialised) {
|
||||
bshell_command_begin_processing(cmdcall->cmd_target);
|
||||
cmdcall->cmd_initialised = true;
|
||||
}
|
||||
|
||||
return bshell_command_process_record(cmdcall->cmd_target, pipeline, rt);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_cmdcall_push_arg(
|
||||
bshell_cmdcall *cmdcall,
|
||||
fx_value arg)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_CMDCALL,
|
||||
cmdcall_push_arg,
|
||||
cmdcall,
|
||||
arg);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_cmdcall_resolve(
|
||||
bshell_cmdcall *cmdcall,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_CMDCALL,
|
||||
cmdcall_resolve,
|
||||
cmdcall,
|
||||
rt);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_cmdcall_execute(bshell_cmdcall *cmdcall)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
BSHELL_TYPE_CMDCALL,
|
||||
cmdcall_execute,
|
||||
cmdcall);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_cmdcall_process_record(
|
||||
bshell_cmdcall *cmdcall,
|
||||
bshell_pipeline *pipeline,
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_CMDCALL,
|
||||
cmdcall_process_record,
|
||||
cmdcall,
|
||||
pipeline,
|
||||
rt);
|
||||
}
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(bshell_cmdcall)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_CONSTRUCTOR("create", bshell_cmdcall_create, 0, FX_TYPE_VOID);
|
||||
FX_TYPE_CLASS_END(bshell_cmdcall)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(bshell_cmdcall)
|
||||
FX_TYPE_ID(0x08aeb275, 0xc09b, 0x4c3a, 0xa8df, 0xd9fba0bb4571);
|
||||
FX_TYPE_NAME("bshell.cmdcall");
|
||||
FX_TYPE_CLASS(bshell_cmdcall_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct bshell_cmdcall_p);
|
||||
FX_TYPE_INSTANCE_FINI(cmdcall_fini);
|
||||
FX_TYPE_DEFINITION_END(bshell_cmdcall)
|
||||
@@ -0,0 +1,456 @@
|
||||
#include "scope.h"
|
||||
|
||||
#include <bshell/format.h>
|
||||
#include <bshell/runtime/cmdcall.h>
|
||||
#include <bshell/runtime/pipeline.h>
|
||||
#include <bshell/runtime/runtime.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static enum bshell_status eval_instruction(
|
||||
struct bshell_runtime *rt,
|
||||
struct bshell_runtime_scope *scope,
|
||||
bshell_instruction instr)
|
||||
{
|
||||
fx_value x, y, z;
|
||||
enum bshell_opcode opcode;
|
||||
fx_status status;
|
||||
enum bshell_status bstatus = BSHELL_SUCCESS;
|
||||
uint32_t arg;
|
||||
fx_type_id type;
|
||||
fx_value *pool_value = NULL;
|
||||
bshell_variable *var = NULL;
|
||||
const char *s = NULL;
|
||||
bshell_instruction_decode(instr, &opcode, &arg);
|
||||
|
||||
switch (opcode) {
|
||||
case BSHELL_OPCODE_LDC_INT:
|
||||
x = FX_INT(arg);
|
||||
bshell_runtime_scope_push_value(scope, &x);
|
||||
fx_value_unset(&x);
|
||||
break;
|
||||
case BSHELL_OPCODE_LDC_FP:
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
FX_TYPE_DOUBLE);
|
||||
if (pool_value) {
|
||||
bshell_runtime_scope_push_value(scope, pool_value);
|
||||
} else {
|
||||
fprintf(stderr, "RUNTIME: invalid ldc.fp operand\n");
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
break;
|
||||
case BSHELL_OPCODE_LDC_STR:
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
FX_TYPE_STRING);
|
||||
if (!pool_value) {
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
FX_TYPE_CSTR);
|
||||
}
|
||||
if (pool_value) {
|
||||
bshell_runtime_scope_push_value(scope, pool_value);
|
||||
} else {
|
||||
fprintf(stderr, "RUNTIME: invalid ldc.str operand\n");
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
break;
|
||||
case BSHELL_OPCODE_ADD:
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
y = bshell_runtime_scope_pop_value(scope);
|
||||
type = fx_value_get_common_type(&x, &y);
|
||||
if (!type) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: cannot apply operator to operands of "
|
||||
"incompatible type\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
status = fx_value_change_type(&y, &y, type);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: cannot convert operand to the "
|
||||
"necessary type\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
status = fx_value_change_type(&x, &x, type);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: cannot convert operand to the "
|
||||
"necessary type\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
status = fx_value_add(&x, &y, &z);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
bshell_runtime_scope_push_value(scope, &z);
|
||||
fx_value_unset(&z);
|
||||
break;
|
||||
case BSHELL_OPCODE_SUB:
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
y = bshell_runtime_scope_pop_value(scope);
|
||||
type = fx_value_get_common_type(&x, &y);
|
||||
if (!type) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: cannot apply operator to operands of "
|
||||
"incompatible type\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
status = fx_value_subtract(&x, &y, &z);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
bshell_runtime_scope_push_value(scope, &z);
|
||||
fx_value_unset(&z);
|
||||
break;
|
||||
case BSHELL_OPCODE_MUL:
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
y = bshell_runtime_scope_pop_value(scope);
|
||||
type = fx_value_get_common_type(&x, &y);
|
||||
if (!type) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: cannot apply operator to operands of "
|
||||
"incompatible type\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
status = fx_value_multiply(&x, &y, &z);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
bshell_runtime_scope_push_value(scope, &z);
|
||||
fx_value_unset(&z);
|
||||
break;
|
||||
case BSHELL_OPCODE_DIV:
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
y = bshell_runtime_scope_pop_value(scope);
|
||||
status = fx_value_change_type(&x, &x, FX_TYPE_DOUBLE);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
status = fx_value_change_type(&y, &y, FX_TYPE_DOUBLE);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
status = fx_value_divide(&x, &y, &z);
|
||||
if (!FX_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"RUNTIME: operands do not support specified "
|
||||
"operator\n");
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
bshell_runtime_scope_push_value(scope, &z);
|
||||
fx_value_unset(&z);
|
||||
break;
|
||||
case BSHELL_OPCODE_LDLOCAL:
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
FX_TYPE_STRING);
|
||||
if (!pool_value) {
|
||||
fprintf(stderr, "RUNTIME: invalid ldlocal operand\n");
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
fx_value_get_cstr(pool_value, &s);
|
||||
var = bshell_runtime_find_var(rt, s);
|
||||
if (var) {
|
||||
bshell_runtime_scope_push_value(
|
||||
scope,
|
||||
bshell_variable_get_value(var));
|
||||
} else {
|
||||
bshell_runtime_scope_push_value(scope, &FX_VALUE_EMPTY);
|
||||
}
|
||||
|
||||
break;
|
||||
case BSHELL_OPCODE_LDBLOCK:
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
BSHELL_TYPE_SCRIPTBLOCK);
|
||||
if (!pool_value) {
|
||||
fprintf(stderr, "RUNTIME: invalid ldblock operand\n");
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
bshell_runtime_scope_push_value(scope, pool_value);
|
||||
|
||||
break;
|
||||
case BSHELL_OPCODE_LDPROP: {
|
||||
/* container */
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
/* property */
|
||||
y = bshell_runtime_scope_pop_value(scope);
|
||||
|
||||
const fx_type *ty = fx_type_get_by_id(x.v_type);
|
||||
if (!ty) {
|
||||
fx_value_unset(&x);
|
||||
fx_value_unset(&y);
|
||||
bshell_runtime_scope_push_value(scope, &FX_VALUE_EMPTY);
|
||||
break;
|
||||
}
|
||||
|
||||
fx_stringstream *strm = fx_stringstream_create();
|
||||
fx_value_to_string(&y, strm, NULL);
|
||||
fx_value_unset(&y);
|
||||
|
||||
const fx_property *prop
|
||||
= fx_type_get_property(ty, fx_stringstream_ptr(strm));
|
||||
fx_stringstream_unref(strm);
|
||||
|
||||
if (!prop) {
|
||||
fx_value_unset(&x);
|
||||
bshell_runtime_scope_push_value(scope, &FX_VALUE_EMPTY);
|
||||
break;
|
||||
}
|
||||
|
||||
fx_property_get_value(prop, &x, &z);
|
||||
fx_value_unset(&x);
|
||||
bshell_runtime_scope_push_value(scope, &z);
|
||||
fx_value_unset(&z);
|
||||
|
||||
break;
|
||||
}
|
||||
case BSHELL_OPCODE_STLOCAL:
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
pool_value = bshell_scriptblock_get_pool_value(
|
||||
scope->s_block,
|
||||
arg,
|
||||
FX_TYPE_CSTR);
|
||||
if (!pool_value) {
|
||||
fprintf(stderr, "RUNTIME: invalid stlocal operand\n");
|
||||
return BSHELL_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
fx_value_get_cstr(pool_value, &s);
|
||||
var = bshell_runtime_find_var(rt, s);
|
||||
if (!var) {
|
||||
var = bshell_runtime_define_var(rt, s);
|
||||
}
|
||||
|
||||
bshell_variable_set_value(var, &x);
|
||||
bshell_runtime_scope_push_value(scope, &x);
|
||||
fx_value_unset(&x);
|
||||
break;
|
||||
case BSHELL_OPCODE_MK_STR: {
|
||||
fx_string *result = fx_string_create();
|
||||
fx_stringstream *strm = fx_stringstream_create();
|
||||
|
||||
while (arg > 0) {
|
||||
x = bshell_runtime_scope_pop_value(scope);
|
||||
fx_stringstream_reset(strm);
|
||||
fx_value_to_string(&x, strm, NULL);
|
||||
fx_string_prepend_cstr(
|
||||
result,
|
||||
fx_stringstream_ptr(strm));
|
||||
fx_value_unset(&x);
|
||||
arg--;
|
||||
}
|
||||
|
||||
fx_stringstream_unref(strm);
|
||||
y = FX_VALUE_OBJECT(result);
|
||||
bshell_runtime_scope_push_value(scope, &y);
|
||||
fx_value_unset(&y);
|
||||
break;
|
||||
}
|
||||
case BSHELL_OPCODE_MK_HTAB: {
|
||||
fx_hashtable *result = fx_hashtable_create();
|
||||
|
||||
while (arg > 0) {
|
||||
fx_value key = bshell_runtime_scope_pop_value(scope);
|
||||
fx_value value = bshell_runtime_scope_pop_value(scope);
|
||||
fx_hashtable_put(result, &key, &value);
|
||||
fx_value_unset(&key);
|
||||
fx_value_unset(&value);
|
||||
arg--;
|
||||
}
|
||||
|
||||
y = FX_VALUE_OBJECT(result);
|
||||
bshell_runtime_scope_push_value(scope, &y);
|
||||
fx_value_unset(&y);
|
||||
break;
|
||||
}
|
||||
case BSHELL_OPCODE_MK_ARRAY: {
|
||||
fx_array *result = fx_array_create();
|
||||
|
||||
while (arg > 0) {
|
||||
fx_value value = bshell_runtime_scope_pop_value(scope);
|
||||
fx_array_push_front(result, value);
|
||||
fx_value_unset(&value);
|
||||
arg--;
|
||||
}
|
||||
|
||||
y = FX_VALUE_OBJECT(result);
|
||||
bshell_runtime_scope_push_value(scope, &y);
|
||||
fx_value_unset(&y);
|
||||
break;
|
||||
}
|
||||
case BSHELL_OPCODE_LDCMD: {
|
||||
bshell_cmdcall *result = bshell_cmdcall_create();
|
||||
|
||||
while (arg > 0) {
|
||||
fx_value cmd_arg = bshell_runtime_scope_pop_value(scope);
|
||||
bshell_cmdcall_push_arg(result, cmd_arg);
|
||||
fx_value_unset(&cmd_arg);
|
||||
arg--;
|
||||
}
|
||||
|
||||
bstatus = bshell_cmdcall_resolve(result, rt);
|
||||
if (bstatus != BSHELL_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
y = FX_VALUE_OBJECT(result);
|
||||
bshell_runtime_scope_push_value(scope, &y);
|
||||
fx_value_unset(&y);
|
||||
break;
|
||||
}
|
||||
case BSHELL_OPCODE_PEXEC: {
|
||||
bshell_pipeline *pipeline = bshell_pipeline_create();
|
||||
bool single = (arg == 1);
|
||||
while (arg > 0) {
|
||||
fx_value cmd = bshell_runtime_scope_pop_value(scope);
|
||||
bshell_cmdcall *cmdcall = NULL;
|
||||
if (fx_value_is_type(&cmd, BSHELL_TYPE_CMDCALL)) {
|
||||
fx_value_get_object(&cmd, &cmdcall);
|
||||
bshell_pipeline_add_cmdcall(pipeline, cmdcall);
|
||||
} else {
|
||||
bshell_pipeline_add_input_value(
|
||||
pipeline,
|
||||
cmd,
|
||||
true);
|
||||
}
|
||||
|
||||
fx_value_unset(&cmd);
|
||||
arg--;
|
||||
}
|
||||
|
||||
int end_of_data = 0;
|
||||
bool columns_initialised = false;
|
||||
fx_value record = FX_VALUE_EMPTY;
|
||||
struct bshell_table_ctx table_ctx;
|
||||
bshell_table_ctx_init(&table_ctx, fx_stdout);
|
||||
while (1) {
|
||||
bstatus = bshell_pipeline_pump_record(
|
||||
pipeline,
|
||||
rt,
|
||||
&record,
|
||||
&end_of_data);
|
||||
if (bstatus != BSHELL_SUCCESS || end_of_data) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!record.v_type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!columns_initialised) {
|
||||
const fx_type *ty
|
||||
= fx_type_get_by_id(record.v_type);
|
||||
bshell_table_ctx_prepare_columns_from_format(
|
||||
&table_ctx,
|
||||
ty,
|
||||
NULL);
|
||||
bshell_table_ctx_print_headers(&table_ctx);
|
||||
columns_initialised = true;
|
||||
}
|
||||
|
||||
bshell_table_ctx_print_record(&table_ctx, &record);
|
||||
fx_value_unset(&record);
|
||||
}
|
||||
|
||||
bshell_table_ctx_cleanup(&table_ctx);
|
||||
bshell_pipeline_unref(pipeline);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"RUNTIME: encountered unknown opcode %02x\n",
|
||||
opcode);
|
||||
return BSHELL_ERR_BAD_FORMAT;
|
||||
}
|
||||
|
||||
scope->s_ip++;
|
||||
return bstatus;
|
||||
}
|
||||
|
||||
static fx_value runtime_eval(struct bshell_runtime *rt)
|
||||
{
|
||||
enum bshell_status status = BSHELL_SUCCESS;
|
||||
while (status == BSHELL_SUCCESS) {
|
||||
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
|
||||
if (scope->s_ip >= scope->s_nr_instr) {
|
||||
break;
|
||||
}
|
||||
|
||||
bshell_instruction instr = scope->s_instr[scope->s_ip];
|
||||
status = eval_instruction(rt, scope, instr);
|
||||
}
|
||||
|
||||
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
|
||||
return bshell_runtime_scope_pop_value(scope);
|
||||
}
|
||||
|
||||
fx_value bshell_runtime_eval_global(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block)
|
||||
{
|
||||
enum bshell_status status = BSHELL_SUCCESS;
|
||||
bshell_runtime_scope_set_block(rt->rt_global, block);
|
||||
return runtime_eval(rt);
|
||||
}
|
||||
|
||||
fx_value bshell_runtime_eval_script(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block)
|
||||
{
|
||||
runtime_push_scope(rt, RUNTIME_SCOPE_SCRIPT, block);
|
||||
fx_value result = runtime_eval(rt);
|
||||
runtime_pop_scope(rt);
|
||||
return result;
|
||||
}
|
||||
|
||||
fx_value bshell_runtime_eval(struct bshell_runtime *rt)
|
||||
{
|
||||
return runtime_eval(rt);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_runtime_push_scope(
|
||||
struct bshell_runtime *rt,
|
||||
bshell_scriptblock *block)
|
||||
{
|
||||
struct bshell_runtime_scope *scope
|
||||
= runtime_push_scope(rt, RUNTIME_SCOPE_SCRIPT, block);
|
||||
return scope ? BSHELL_SUCCESS : BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_runtime_pop_scope(struct bshell_runtime *rt)
|
||||
{
|
||||
runtime_pop_scope(rt);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <bshell/runtime/opcode.h>
|
||||
#include <fx/int.h>
|
||||
|
||||
#define BSHELL_OPCODE_MAX 0xFF
|
||||
#define ARG_MAX 0xFFFFFF
|
||||
|
||||
bshell_instruction bshell_instruction_encode(
|
||||
enum bshell_opcode opcode,
|
||||
uint32_t arg)
|
||||
{
|
||||
if (opcode > BSHELL_OPCODE_MAX) {
|
||||
return BSHELL_INSTRUCTION_INVALID;
|
||||
}
|
||||
|
||||
return fx_u32_htob((opcode & BSHELL_OPCODE_MAX) | (arg & ARG_MAX) << 8);
|
||||
}
|
||||
|
||||
void bshell_instruction_decode(
|
||||
bshell_instruction instr,
|
||||
enum bshell_opcode *out_opcode,
|
||||
uint32_t *out_arg)
|
||||
{
|
||||
instr = fx_u32_btoh(instr);
|
||||
*out_opcode = (instr & BSHELL_OPCODE_MAX);
|
||||
*out_arg = ((instr >> 8) & ARG_MAX);
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
#include <bshell/runtime/cmdcall.h>
|
||||
#include <bshell/runtime/pipeline.h>
|
||||
#include <bshell/status.h>
|
||||
#include <fx/collections/array.h>
|
||||
#include <fx/reflection/function.h>
|
||||
#include <fx/string.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
struct bshell_pipeline_p {
|
||||
bshell_pipeline *p_self;
|
||||
fx_array *p_in, *p_out;
|
||||
FX_VECTOR_DECLARE(bshell_cmdcall *, p_cmds);
|
||||
};
|
||||
|
||||
static void pipeline_init(fx_object *obj, void *priv)
|
||||
{
|
||||
struct bshell_pipeline_p *pipeline = priv;
|
||||
pipeline->p_self = obj;
|
||||
pipeline->p_in = fx_array_create();
|
||||
pipeline->p_out = fx_array_create();
|
||||
}
|
||||
|
||||
static void pipeline_fini(fx_object *obj, void *priv)
|
||||
{
|
||||
struct bshell_pipeline_p *pipeline = priv;
|
||||
for (size_t i = 0; i < pipeline->p_cmds.count; i++) {
|
||||
bshell_cmdcall_unref(pipeline->p_cmds.items[i]);
|
||||
}
|
||||
|
||||
if (pipeline->p_in) {
|
||||
fx_array_unref(pipeline->p_in);
|
||||
pipeline->p_in = NULL;
|
||||
}
|
||||
|
||||
if (pipeline->p_out) {
|
||||
fx_array_unref(pipeline->p_out);
|
||||
pipeline->p_out = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static enum bshell_status write_value(
|
||||
struct bshell_pipeline_p *pipeline,
|
||||
fx_array *dest,
|
||||
fx_value value,
|
||||
bool enumerate)
|
||||
{
|
||||
if (!enumerate) {
|
||||
fx_array_push_back(dest, value);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
fx_object *container = NULL;
|
||||
fx_value_get_object(&value, &container);
|
||||
if (!container) {
|
||||
fx_array_push_back(dest, value);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
fx_iterator *it = fx_iterator_begin(container);
|
||||
fx_foreach(v, it)
|
||||
{
|
||||
fx_array_push_back(dest, fx_value_copy_return(v));
|
||||
}
|
||||
fx_iterator_unref(it);
|
||||
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status pipeline_add_input_value(
|
||||
struct bshell_pipeline_p *pipeline,
|
||||
fx_value value,
|
||||
bool enumerate)
|
||||
{
|
||||
return write_value(pipeline, pipeline->p_in, value, enumerate);
|
||||
}
|
||||
|
||||
static enum bshell_status pipeline_add_cmdcall(
|
||||
struct bshell_pipeline_p *pipeline,
|
||||
bshell_cmdcall *cmdcall)
|
||||
{
|
||||
bshell_cmdcall **slot = fx_vector_emplace_back(pipeline->p_cmds, NULL);
|
||||
*slot = bshell_cmdcall_ref(cmdcall);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
static enum bshell_status pipeline_execute_single(
|
||||
struct bshell_pipeline_p *pipeline)
|
||||
{
|
||||
return BSHELL_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
static fx_value pipeline_read_value(struct bshell_pipeline_p *pipeline)
|
||||
{
|
||||
return fx_array_pop_front(pipeline->p_in);
|
||||
}
|
||||
|
||||
static enum bshell_status pipeline_write_value(
|
||||
struct bshell_pipeline_p *pipeline,
|
||||
fx_value value,
|
||||
bool enumerate)
|
||||
{
|
||||
return write_value(pipeline, pipeline->p_out, value, enumerate);
|
||||
}
|
||||
|
||||
static enum bshell_status pipeline_pump_record(
|
||||
struct bshell_pipeline_p *pipeline,
|
||||
struct bshell_runtime *rt,
|
||||
fx_value *result,
|
||||
int *out_end_of_data)
|
||||
{
|
||||
enum bshell_status status = BSHELL_SUCCESS;
|
||||
if (fx_array_get_size(pipeline->p_out)) {
|
||||
fx_value out = fx_array_pop_front(pipeline->p_out);
|
||||
*result = out;
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
for (i = 0; i < pipeline->p_cmds.count;) {
|
||||
status = bshell_cmdcall_process_record(
|
||||
pipeline->p_cmds.items[i],
|
||||
pipeline->p_self,
|
||||
rt);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
bool input_available = fx_array_get_size(pipeline->p_in) != 0;
|
||||
bool output_available = fx_array_get_size(pipeline->p_out) != 0;
|
||||
|
||||
if (input_available) {
|
||||
/* repeat this pipeline stage until all available input
|
||||
* records are consumed */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!output_available) {
|
||||
/* no output produced, and no more input available */
|
||||
break;
|
||||
}
|
||||
|
||||
if (i < pipeline->p_cmds.count - 1) {
|
||||
fx_array *tmp = pipeline->p_in;
|
||||
pipeline->p_in = pipeline->p_out;
|
||||
pipeline->p_out = tmp;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (fx_array_get_size(pipeline->p_out)) {
|
||||
fx_value out = fx_array_pop_front(pipeline->p_out);
|
||||
*result = out;
|
||||
return BSHELL_SUCCESS;
|
||||
} else if (i == 0) {
|
||||
*result = FX_VALUE_EMPTY;
|
||||
*out_end_of_data = 1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
size_t i = 0;
|
||||
for (i = 0; i < pipeline->p_cmds.count; i++) {
|
||||
status = bshell_command_process_record(
|
||||
pipeline->p_cmds.items[i],
|
||||
rt);
|
||||
|
||||
if (status != BSHELL_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
fx_array *tmp = pipeline->p_in;
|
||||
pipeline->p_in = pipeline->p_out;
|
||||
pipeline->p_out = tmp;
|
||||
|
||||
if (fx_array_size(pipeline->p_in)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (in->v_type) {
|
||||
fx_value_copy(result, in);
|
||||
} else if (i == 0) {
|
||||
*out_end_of_data = 1;
|
||||
}
|
||||
|
||||
fx_value_unset(in);
|
||||
fx_value_unset(out);
|
||||
#endif
|
||||
return status;
|
||||
}
|
||||
|
||||
enum bshell_status bshell_pipeline_add_input_value(
|
||||
bshell_pipeline *pipeline,
|
||||
fx_value value,
|
||||
bool enumerate)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_add_input_value,
|
||||
pipeline,
|
||||
value,
|
||||
enumerate);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_pipeline_add_cmdcall(
|
||||
bshell_pipeline *pipeline,
|
||||
bshell_cmdcall *cmd)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_add_cmdcall,
|
||||
pipeline,
|
||||
cmd);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_pipeline_execute_single(bshell_pipeline *pipeline)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_execute_single,
|
||||
pipeline);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_pipeline_pump_record(
|
||||
bshell_pipeline *pipeline,
|
||||
struct bshell_runtime *rt,
|
||||
fx_value *out,
|
||||
int *out_end_of_data)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_pump_record,
|
||||
pipeline,
|
||||
rt,
|
||||
out,
|
||||
out_end_of_data);
|
||||
}
|
||||
|
||||
fx_value bshell_pipeline_read_value(bshell_pipeline *pipeline)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_read_value,
|
||||
pipeline);
|
||||
}
|
||||
|
||||
enum bshell_status bshell_pipeline_write_value(
|
||||
bshell_pipeline *pipeline,
|
||||
fx_value val,
|
||||
bool enumerate)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
BSHELL_TYPE_PIPELINE,
|
||||
pipeline_write_value,
|
||||
pipeline,
|
||||
val,
|
||||
enumerate);
|
||||
}
|
||||
|
||||
FX_TYPE_CLASS_BEGIN(bshell_pipeline)
|
||||
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
|
||||
FX_INTERFACE_ENTRY(to_string) = NULL;
|
||||
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
|
||||
|
||||
FX_TYPE_CONSTRUCTOR("create", bshell_pipeline_create, 0, FX_TYPE_VOID);
|
||||
FX_TYPE_CLASS_END(bshell_pipeline)
|
||||
|
||||
FX_TYPE_DEFINITION_BEGIN(bshell_pipeline)
|
||||
FX_TYPE_ID(0x64159f21, 0xb1dd, 0x4838, 0xba62, 0x69fa65cc01d3);
|
||||
FX_TYPE_NAME("bshell.pipeline");
|
||||
FX_TYPE_CLASS(bshell_pipeline_class);
|
||||
FX_TYPE_INSTANCE_PRIVATE(struct bshell_pipeline_p);
|
||||
FX_TYPE_INSTANCE_INIT(pipeline_init);
|
||||
FX_TYPE_INSTANCE_FINI(pipeline_fini);
|
||||
FX_TYPE_DEFINITION_END(bshell_pipeline)
|
||||
@@ -0,0 +1,116 @@
|
||||
#include "scope.h"
|
||||
|
||||
#include <bshell/runtime/runtime.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct bshell_runtime *bshell_runtime_create(void)
|
||||
{
|
||||
struct bshell_runtime *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->rt_global = runtime_push_scope(out, RUNTIME_SCOPE_GLOBAL, NULL);
|
||||
out->rt_aliases = fx_hashtable_create();
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void bshell_runtime_destroy(struct bshell_runtime *rt)
|
||||
{
|
||||
fx_hashtable_unref(rt->rt_aliases);
|
||||
free(rt);
|
||||
}
|
||||
|
||||
bshell_variable *bshell_runtime_find_var(
|
||||
struct bshell_runtime *rt,
|
||||
const char *name)
|
||||
{
|
||||
bshell_variable *var = NULL;
|
||||
fx_queue_entry *cur = fx_queue_last(&rt->rt_scope);
|
||||
while (cur) {
|
||||
struct bshell_runtime_scope *scope
|
||||
= fx_unbox(struct bshell_runtime_scope, cur, s_entry);
|
||||
var = var_map_get(&scope->s_vars, name);
|
||||
if (var) {
|
||||
break;
|
||||
}
|
||||
|
||||
cur = fx_queue_prev(cur);
|
||||
}
|
||||
|
||||
return var;
|
||||
}
|
||||
|
||||
bshell_variable *bshell_runtime_define_var(
|
||||
struct bshell_runtime *rt,
|
||||
const char *name)
|
||||
{
|
||||
struct bshell_runtime_scope *scope = runtime_get_scope(rt);
|
||||
if (!scope) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bshell_variable *var = bshell_variable_create(name);
|
||||
if (!var) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
var_map_put(&scope->s_vars, var);
|
||||
return var;
|
||||
}
|
||||
|
||||
bshell_command *bshell_runtime_find_command(
|
||||
struct bshell_runtime *rt,
|
||||
const char *callable_name)
|
||||
{
|
||||
bshell_command *alias = NULL, *func = NULL;
|
||||
const fx_value *alias_v
|
||||
= fx_hashtable_get(rt->rt_aliases, &FX_CSTR(callable_name));
|
||||
if (alias_v) {
|
||||
fx_value_get_object(alias_v, &alias);
|
||||
bshell_command_ref(alias);
|
||||
return alias;
|
||||
}
|
||||
|
||||
fx_queue_entry *cur = fx_queue_last(&rt->rt_scope);
|
||||
while (cur) {
|
||||
struct bshell_runtime_scope *scope
|
||||
= fx_unbox(struct bshell_runtime_scope, cur, s_entry);
|
||||
const fx_value *func_v = fx_hashtable_get(
|
||||
scope->s_functions,
|
||||
&FX_CSTR(callable_name));
|
||||
if (func_v) {
|
||||
fx_value_get_object(func_v, &func);
|
||||
bshell_command_ref(func);
|
||||
return func;
|
||||
}
|
||||
|
||||
cur = fx_queue_prev(cur);
|
||||
}
|
||||
|
||||
bshell_command *cmd = bshell_command_find_static(callable_name);
|
||||
if (cmd) {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
/* TODO find native executables */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct bshell_runtime_scope *bshell_runtime_get_current_scope(
|
||||
struct bshell_runtime *rt)
|
||||
{
|
||||
return runtime_get_scope(rt);
|
||||
}
|
||||
|
||||
struct bshell_runtime_scope *bshell_runtime_get_parent_scope(
|
||||
struct bshell_runtime *rt,
|
||||
struct bshell_runtime_scope *scope)
|
||||
{
|
||||
fx_queue_entry *parent = fx_queue_prev(&scope->s_entry);
|
||||
return fx_unbox(struct bshell_runtime_scope, parent, s_entry);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "scope.h"
|
||||
|
||||
#include <bshell/runtime/runtime.h>
|
||||
#include <fx/vector.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct bshell_runtime_scope *runtime_push_scope(
|
||||
struct bshell_runtime *rt,
|
||||
enum bshell_runtime_scope_type type,
|
||||
bshell_scriptblock *block)
|
||||
{
|
||||
struct bshell_runtime_scope *out = malloc(sizeof *out);
|
||||
if (!out) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->s_type = type;
|
||||
out->s_functions = fx_hashtable_create();
|
||||
|
||||
if (block) {
|
||||
bshell_runtime_scope_set_block(out, block);
|
||||
}
|
||||
|
||||
fx_queue_push_back(&rt->rt_scope, &out->s_entry);
|
||||
return out;
|
||||
}
|
||||
|
||||
void runtime_pop_scope(struct bshell_runtime *rt)
|
||||
{
|
||||
fx_queue_entry *entry = fx_queue_pop_back(&rt->rt_scope);
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct bshell_runtime_scope *scope
|
||||
= fx_unbox(struct bshell_runtime_scope, entry, s_entry);
|
||||
/* TODO */
|
||||
fx_hashtable_unref(scope->s_functions);
|
||||
free(scope);
|
||||
}
|
||||
|
||||
struct bshell_runtime_scope *runtime_get_scope(struct bshell_runtime *rt)
|
||||
{
|
||||
fx_queue_entry *entry = fx_queue_last(&rt->rt_scope);
|
||||
if (!entry) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return fx_unbox(struct bshell_runtime_scope, entry, s_entry);
|
||||
}
|
||||
|
||||
void bshell_runtime_scope_push_value(
|
||||
struct bshell_runtime_scope *scope,
|
||||
const fx_value *value)
|
||||
{
|
||||
fx_value *slot = fx_vector_emplace_back(scope->s_stack, NULL);
|
||||
if (!slot) {
|
||||
return;
|
||||
}
|
||||
|
||||
fx_value_copy(slot, value);
|
||||
}
|
||||
|
||||
fx_value bshell_runtime_scope_pop_value(struct bshell_runtime_scope *scope)
|
||||
{
|
||||
if (scope->s_stack.count == 0) {
|
||||
return FX_VALUE_EMPTY;
|
||||
}
|
||||
|
||||
fx_value v = scope->s_stack.items[scope->s_stack.count - 1];
|
||||
fx_vector_pop_back(scope->s_stack, NULL);
|
||||
return v;
|
||||
}
|
||||
|
||||
void bshell_runtime_scope_set_block(
|
||||
struct bshell_runtime_scope *scope,
|
||||
bshell_scriptblock *block)
|
||||
{
|
||||
scope->s_block = block;
|
||||
scope->s_ip = 0;
|
||||
bshell_scriptblock_get_text(block, &scope->s_instr, &scope->s_nr_instr);
|
||||
bshell_scriptblock_get_pool(block, &scope->s_pool, &scope->s_nr_pool);
|
||||
}
|
||||
|
||||
fx_hashtable *bshell_runtime_scope_get_functions(
|
||||
struct bshell_runtime_scope *scope)
|
||||
{
|
||||
return scope->s_functions;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef BSHELL_RUNTIME_SCOPE_H_
|
||||
#define BSHELL_RUNTIME_SCOPE_H_
|
||||
|
||||
#include "var-map.h"
|
||||
|
||||
#include <bshell/runtime/script-block.h>
|
||||
#include <fx/collections/hashtable.h>
|
||||
#include <fx/namemap.h>
|
||||
#include <fx/queue.h>
|
||||
#include <fx/vector.h>
|
||||
|
||||
struct bshell_runtime;
|
||||
|
||||
enum bshell_runtime_scope_type {
|
||||
RUNTIME_SCOPE_OTHER = 0,
|
||||
RUNTIME_SCOPE_SCRIPT,
|
||||
RUNTIME_SCOPE_GLOBAL,
|
||||
};
|
||||
|
||||
struct bshell_runtime_scope {
|
||||
enum bshell_runtime_scope_type s_type;
|
||||
fx_queue_entry s_entry;
|
||||
struct var_map s_vars;
|
||||
fx_hashtable *s_functions;
|
||||
FX_VECTOR_DECLARE(fx_value, s_stack);
|
||||
size_t s_ip;
|
||||
|
||||
bshell_scriptblock *s_block;
|
||||
bshell_instruction *s_instr;
|
||||
size_t s_nr_instr;
|
||||
fx_value *s_pool;
|
||||
size_t s_nr_pool;
|
||||
};
|
||||
|
||||
extern struct bshell_runtime_scope *runtime_push_scope(
|
||||
struct bshell_runtime *rt,
|
||||
enum bshell_runtime_scope_type type,
|
||||
bshell_scriptblock *block);
|
||||
extern void runtime_pop_scope(struct bshell_runtime *rt);
|
||||
extern struct bshell_runtime_scope *runtime_get_scope(struct bshell_runtime *rt);
|
||||
|
||||
extern void bshell_runtime_scope_push_value(
|
||||
struct bshell_runtime_scope *scope,
|
||||
const fx_value *value);
|
||||
extern fx_value bshell_runtime_scope_pop_value(struct bshell_runtime_scope *scope);
|
||||
|
||||
extern void bshell_runtime_scope_set_block(
|
||||
struct bshell_runtime_scope *scope,
|
||||
bshell_scriptblock *block);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "var-map.h"
|
||||
|
||||
void var_map_cleanup(struct var_map *map)
|
||||
{
|
||||
}
|
||||
|
||||
enum bshell_status var_map_put(struct var_map *map, bshell_variable *var)
|
||||
{
|
||||
const char *name = bshell_variable_get_name(var);
|
||||
struct var_map_entry *entry = malloc(sizeof *entry);
|
||||
if (!entry) {
|
||||
return BSHELL_ERR_NO_MEMORY;
|
||||
}
|
||||
|
||||
memset(entry, 0x0, sizeof *entry);
|
||||
|
||||
entry->e_var = var;
|
||||
|
||||
fx_namemap_put(&map->m_vars, name, &entry->e_entry);
|
||||
return BSHELL_SUCCESS;
|
||||
}
|
||||
|
||||
bshell_variable *var_map_get(struct var_map *map, const char *name)
|
||||
{
|
||||
fx_namemap_entry *e = fx_namemap_get(&map->m_vars, name);
|
||||
if (!e) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct var_map_entry *entry = fx_unbox(
|
||||
struct var_map_entry,
|
||||
e,
|
||||
e_entry);
|
||||
return entry->e_var;
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user